Commit 472f1d3e authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
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 "radixbug/bug.hh"
#include <QApplication>
#include <QDesktopWidget>
#include <QKeyEvent>
#include <QPoint>
#include <QRect>
#include "radixbug/bug.hh"
#define NEXT_ROW_MARKER 0
namespace radix
{
......@@ -17,21 +16,17 @@ struct KeyboardLayoutEntry
const char *label;
};
const KeyboardLayoutEntry keyboardLayout[] = {{Qt::Key_7, "7"},
{Qt::Key_8, "8"},
{Qt::Key_9, "9"},
{NEXT_ROW_MARKER, nullptr},
{Qt::Key_4, "4"},
{Qt::Key_5, "5"},
{Qt::Key_6, "6"},
{NEXT_ROW_MARKER, nullptr},
{Qt::Key_1, "1"},
{Qt::Key_2, "2"},
{Qt::Key_3, "3"},
{NEXT_ROW_MARKER, nullptr},
{Qt::Key_0, "0"},
{Qt::Key_Period, "."},
{Qt::Key_Backspace, "Backspace"}};
const KeyboardLayoutEntry keyboardLayout[] = {
{Qt::Key_7, "7"}, {Qt::Key_8, "8"},
{Qt::Key_9, "9"}, {NEXT_ROW_MARKER, nullptr},
{Qt::Key_4, "4"}, {Qt::Key_5, "5"},
{Qt::Key_6, "6"}, {NEXT_ROW_MARKER, nullptr},
{Qt::Key_1, "1"}, {Qt::Key_2, "2"},
{Qt::Key_3, "3"}, {NEXT_ROW_MARKER, nullptr},
{Qt::Key_0, "0"}, {Qt::Key_Period, "."},
{Qt::Key_Plus, "+"}, {Qt::Key_Minus, "-"},
{NEXT_ROW_MARKER, nullptr}, {Qt::Key_Backspace, "Backspace"},
{Qt::Key_Delete, "Delete"}};
const int layoutSize = (sizeof(keyboardLayout) / sizeof(KeyboardLayoutEntry));
......@@ -47,9 +42,10 @@ QString keyToCharacter(int key)
return QString();
}
NumberPadWidget::NumberPadWidget(QWidget *parent)
NumberPadWidget::NumberPadWidget(QWidget *parent, bool toggle)
: QLineEdit(parent)
{
mToggle = toggle;
mMapper = new QSignalMapper(this);
connect(mMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
......@@ -74,12 +70,39 @@ NumberPadWidget::NumberPadWidget(QWidget *parent)
col = 0;
continue;
}
mButton = new QPushButton(this);
mButton->setObjectName("number_pad_button");
mButton->setText(QString::fromLatin1(keyboardLayout[i].label));
mMapper->setMapping(mButton, keyboardLayout[i].key);
connect(mButton, SIGNAL(clicked()), mMapper, SLOT(map()));
mNumLayout->addWidget(mButton, row, col);
QPushButton *button = new QPushButton(this);
int colSpan = 1;
switch (keyboardLayout[i].key)
{
case Qt::Key_Delete:
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++;
}
}
......@@ -90,6 +113,24 @@ void NumberPadWidget::buttonClicked(int key)
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)
{
QLineEdit::focusInEvent(event);
......@@ -98,8 +139,8 @@ void NumberPadWidget::focusInEvent(QFocusEvent *event)
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();
mNumberWidget->move(center.x() - mNumberWidget->width() * 0.5,
center.y() - mNumberWidget->height() * 0.5);
mNumberWidget->move(center.x() - int(mNumberWidget->width() * 0.5),
center.y() - int(mNumberWidget->height() * 0.5));
mNumberWidget->show();
}
......
......@@ -17,10 +17,10 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit
QSignalMapper* mMapper;
QWidget* mNumberWidget;
QGridLayout* mNumLayout;
QPushButton* mButton;
bool mToggle;
public:
NumberPadWidget(QWidget* parent = nullptr);
NumberPadWidget(QWidget* parent = nullptr, bool toggle = false);
public slots:
void focusInEvent(QFocusEvent*) override;
......@@ -29,6 +29,8 @@ class RADIX_PUBLIC NumberPadWidget : public QLineEdit
private slots:
void buttonClicked(int key);
void makePositive();
void makeNegative();
}; // class
} // namespace radix
#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