Commit 472f1d3e authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'numpad_sign' into 'master'

Numpad sign

See merge request !87
parents cdcb7b57 1fb2cac6
Pipeline #80014 passed with stages
in 18 minutes and 26 seconds
#include "radixwidgets/numberpadwidget.hh" #include "radixwidgets/numberpadwidget.hh"
#include "radixbug/bug.hh"
#include <QApplication> #include <QApplication>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QKeyEvent> #include <QKeyEvent>
#include <QPoint> #include <QPoint>
#include <QRect> #include <QRect>
#include "radixbug/bug.hh"
#define NEXT_ROW_MARKER 0 #define NEXT_ROW_MARKER 0
namespace radix namespace radix
{ {
...@@ -17,21 +16,17 @@ struct KeyboardLayoutEntry ...@@ -17,21 +16,17 @@ struct KeyboardLayoutEntry
const char *label; const char *label;
}; };
const KeyboardLayoutEntry keyboardLayout[] = {{Qt::Key_7, "7"}, const KeyboardLayoutEntry keyboardLayout[] = {
{Qt::Key_8, "8"}, {Qt::Key_7, "7"}, {Qt::Key_8, "8"},
{Qt::Key_9, "9"}, {Qt::Key_9, "9"}, {NEXT_ROW_MARKER, nullptr},
{NEXT_ROW_MARKER, nullptr}, {Qt::Key_4, "4"}, {Qt::Key_5, "5"},
{Qt::Key_4, "4"}, {Qt::Key_6, "6"}, {NEXT_ROW_MARKER, nullptr},
{Qt::Key_5, "5"}, {Qt::Key_1, "1"}, {Qt::Key_2, "2"},
{Qt::Key_6, "6"}, {Qt::Key_3, "3"}, {NEXT_ROW_MARKER, nullptr},
{NEXT_ROW_MARKER, nullptr}, {Qt::Key_0, "0"}, {Qt::Key_Period, "."},
{Qt::Key_1, "1"}, {Qt::Key_Plus, "+"}, {Qt::Key_Minus, "-"},
{Qt::Key_2, "2"}, {NEXT_ROW_MARKER, nullptr}, {Qt::Key_Backspace, "Backspace"},
{Qt::Key_3, "3"}, {Qt::Key_Delete, "Delete"}};
{NEXT_ROW_MARKER, nullptr},
{Qt::Key_0, "0"},
{Qt::Key_Period, "."},
{Qt::Key_Backspace, "Backspace"}};
const int layoutSize = (sizeof(keyboardLayout) / sizeof(KeyboardLayoutEntry)); const int layoutSize = (sizeof(keyboardLayout) / sizeof(KeyboardLayoutEntry));
...@@ -47,9 +42,10 @@ QString keyToCharacter(int key) ...@@ -47,9 +42,10 @@ QString keyToCharacter(int key)
return QString(); return QString();
} }
NumberPadWidget::NumberPadWidget(QWidget *parent) NumberPadWidget::NumberPadWidget(QWidget *parent, bool toggle)
: QLineEdit(parent) : QLineEdit(parent)
{ {
mToggle = toggle;
mMapper = new QSignalMapper(this); mMapper = new QSignalMapper(this);
connect(mMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int))); connect(mMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
...@@ -74,12 +70,39 @@ NumberPadWidget::NumberPadWidget(QWidget *parent) ...@@ -74,12 +70,39 @@ NumberPadWidget::NumberPadWidget(QWidget *parent)
col = 0; col = 0;
continue; continue;
} }
mButton = new QPushButton(this); QPushButton *button = new QPushButton(this);
mButton->setObjectName("number_pad_button"); int colSpan = 1;
mButton->setText(QString::fromLatin1(keyboardLayout[i].label)); switch (keyboardLayout[i].key)
mMapper->setMapping(mButton, keyboardLayout[i].key); {
connect(mButton, SIGNAL(clicked()), mMapper, SLOT(map())); case Qt::Key_Delete:
mNumLayout->addWidget(mButton, row, col); col = 2;
// [[fallthrough]]; C++17
case Qt::Key_Backspace:
case Qt::Key_3:
case Qt::Key_6:
case Qt::Key_9:
button->setObjectName("number_pad_button_wide");
colSpan = 2;
break;
default:
button->setObjectName("number_pad_button");
break;
}
button->setText(QString::fromLatin1(keyboardLayout[i].label));
if (mToggle && keyboardLayout[i].key == Qt::Key_Plus)
connect(button, SIGNAL(clicked()), this, SLOT(makePositive()));
else if (mToggle && keyboardLayout[i].key == Qt::Key_Minus)
connect(button, SIGNAL(clicked()), this, SLOT(makeNegative()));
else
{
mMapper->setMapping(button, keyboardLayout[i].key);
connect(button, SIGNAL(clicked()), mMapper, SLOT(map()));
}
mNumLayout->addWidget(button, row, col, 1, colSpan);
col++; col++;
} }
} }
...@@ -90,6 +113,24 @@ void NumberPadWidget::buttonClicked(int key) ...@@ -90,6 +113,24 @@ void NumberPadWidget::buttonClicked(int key)
QLineEdit::keyPressEvent(&event); QLineEdit::keyPressEvent(&event);
} }
void NumberPadWidget::makePositive()
{
QString sval = text().trimmed();
if (sval.startsWith("-"))
{
setText(sval.mid(1));
}
}
void NumberPadWidget::makeNegative()
{
QString sval = text().trimmed();
if (!sval.startsWith("-"))
{
setText(sval.prepend("-"));
}
}
void NumberPadWidget::focusInEvent(QFocusEvent *event) void NumberPadWidget::focusInEvent(QFocusEvent *event)
{ {
QLineEdit::focusInEvent(event); QLineEdit::focusInEvent(event);
...@@ -98,8 +139,8 @@ void NumberPadWidget::focusInEvent(QFocusEvent *event) ...@@ -98,8 +139,8 @@ void NumberPadWidget::focusInEvent(QFocusEvent *event)
QRect desktopRect = QApplication::desktop()->availableGeometry(this); QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center(); QPoint center = desktopRect.center();
mNumberWidget->move(center.x() - mNumberWidget->width() * 0.5, mNumberWidget->move(center.x() - int(mNumberWidget->width() * 0.5),
center.y() - mNumberWidget->height() * 0.5); center.y() - int(mNumberWidget->height() * 0.5));
mNumberWidget->show(); mNumberWidget->show();
} }
......
...@@ -17,10 +17,10 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit ...@@ -17,10 +17,10 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit
QSignalMapper* mMapper; QSignalMapper* mMapper;
QWidget* mNumberWidget; QWidget* mNumberWidget;
QGridLayout* mNumLayout; QGridLayout* mNumLayout;
QPushButton* mButton; bool mToggle;
public: public:
NumberPadWidget(QWidget* parent = nullptr); NumberPadWidget(QWidget* parent = nullptr, bool toggle = false);
public slots: public slots:
void focusInEvent(QFocusEvent*) override; void focusInEvent(QFocusEvent*) override;
...@@ -29,6 +29,8 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit ...@@ -29,6 +29,8 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit
private slots: private slots:
void buttonClicked(int key); void buttonClicked(int key);
void makePositive();
void makeNegative();
}; // class }; // class
} // namespace radix } // namespace radix
#endif /** RADIX_RADIXWIDGETS_NUMBERPADWIDGET_HH_ */ #endif /** RADIX_RADIXWIDGETS_NUMBERPADWIDGET_HH_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment