numberpadwidget.cc 4 KB
Newer Older
1
2
3
4
5
6
7
#include "radixwidgets/numberpadwidget.hh"
#include <QApplication>
#include <QDesktopWidget>
#include <QKeyEvent>
#include <QPoint>
#include <QRect>

8
9
#include "radixbug/bug.hh"

10
11
12
13
14
15
16
17
18
#define NEXT_ROW_MARKER 0
namespace radix
{
struct KeyboardLayoutEntry
{
  int key;
  const char *label;
};

19
20
21
22
23
24
25
26
27
28
29
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"}};
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

const int layoutSize = (sizeof(keyboardLayout) / sizeof(KeyboardLayoutEntry));

QString keyToCharacter(int key)
{
  for (int i = 0; i < layoutSize; ++i)
  {
    if (keyboardLayout[i].key == key)
    {
      return QString::fromLatin1(keyboardLayout[i].label);
    }
  }
  return QString();
}

45
NumberPadWidget::NumberPadWidget(QWidget *parent, bool toggle)
46
47
    : QLineEdit(parent)
{
48
  mToggle = toggle;
Huff, Israel's avatar
Huff, Israel committed
49
50
  mMapper = new QSignalMapper(this);
  connect(mMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
51
52

  // add widget containing
Huff, Israel's avatar
Huff, Israel committed
53
54
  mNumberWidget = new QWidget(this);
  mNumLayout    = new QGridLayout(mNumberWidget);
55
56
57
58
59
  mNumberWidget->hide();

  mNumberWidget->setWindowFlags(Qt::WindowDoesNotAcceptFocus | Qt::Tool |
                                Qt::FramelessWindowHint |
                                Qt::WindowStaysOnTopHint);
60
  mNumberWidget->setObjectName("number_pad_widget");
61
62
63
64
65
66
67
68
69
70
71
72
  int row = 0;
  int col = 0;

  // Create keyboard widget
  for (int i = 0; i < layoutSize; ++i)
  {
    if (keyboardLayout[i].key == NEXT_ROW_MARKER)
    {
      row++;
      col = 0;
      continue;
    }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    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));
94
95
96
97
98
99
100
101
102
103
104

    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()));
    }

105
    mNumLayout->addWidget(button, row, col, 1, colSpan);
106
107
108
109
110
111
    col++;
  }
}

void NumberPadWidget::buttonClicked(int key)
{
Huff, Israel's avatar
Huff, Israel committed
112
113
  QKeyEvent event(QEvent::KeyPress, key, Qt::NoModifier, keyToCharacter(key));
  QLineEdit::keyPressEvent(&event);
114
}
Huff, Israel's avatar
Huff, Israel committed
115

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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("-"));
  }
}

134
135
136
137
138
139
140
141
void NumberPadWidget::focusInEvent(QFocusEvent *event)
{
  QLineEdit::focusInEvent(event);
  //
  // Determine where to place the widget
  QRect desktopRect = QApplication::desktop()->availableGeometry(this);
  QPoint center     = desktopRect.center();

142
143
  mNumberWidget->move(center.x() - int(mNumberWidget->width() * 0.5),
                      center.y() - int(mNumberWidget->height() * 0.5));
144
145
146
147
148
149
150
151
152
153
154
155
  mNumberWidget->show();
}

void NumberPadWidget::focusOutEvent(QFocusEvent *event)
{
  QLineEdit::focusOutEvent(event);
  mNumberWidget->hide();
}

bool NumberPadWidget::numberPadVisible() const { return QWidget::isVisible(); }

}  // namespace radix