|
| 1 | +#include "lc_global.h" |
| 2 | +#include "lc_ladderwidget.h" |
| 3 | + |
| 4 | +lcLadderWidget::lcLadderWidget(QAbstractSpinBox* SpinBox) |
| 5 | + : QWidget(nullptr, Qt::Popup | Qt::Sheet), mSpinBox(SpinBox) |
| 6 | +{ |
| 7 | + mSpinBox->installEventFilter(this); |
| 8 | +} |
| 9 | + |
| 10 | +void lcLadderWidget::Show() |
| 11 | +{ |
| 12 | +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 13 | + QScreen* Screen = screen(); |
| 14 | + const QRect Desktop = Screen ? Screen->geometry() : QRect(); |
| 15 | +#else |
| 16 | + const QRect Desktop = QApplication::desktop()->geometry(); |
| 17 | +#endif |
| 18 | + |
| 19 | + mSteps = { 100, 10, 1 }; |
| 20 | + int LineHeight = QFontMetrics(font()).height(); |
| 21 | + int CellSize = LineHeight * 4; |
| 22 | + |
| 23 | + setFixedSize(CellSize, static_cast<int>(mSteps.size()) * CellSize); |
| 24 | + adjustSize(); |
| 25 | + |
| 26 | + int HalfWidth = width() / 2; |
| 27 | + int HalfHeight = height() / 2; |
| 28 | + QPoint Position = QCursor::pos() - QPoint(HalfWidth, HalfHeight); |
| 29 | + |
| 30 | + if (Position.x() - HalfWidth < Desktop.left()) |
| 31 | + Position.setX(Desktop.left() + HalfWidth); |
| 32 | + |
| 33 | + if (Position.y() - HalfHeight < Desktop.top()) |
| 34 | + Position.setY(Desktop.top() + HalfHeight); |
| 35 | + |
| 36 | + if ((Position.x() + HalfHeight) > Desktop.width()) |
| 37 | + Position.setX(Desktop.width() - HalfHeight); |
| 38 | + |
| 39 | + if (Position.y() + HalfHeight > Desktop.bottom()) |
| 40 | + Position.setY(Desktop.bottom() - HalfHeight); |
| 41 | + |
| 42 | + move(Position); |
| 43 | + |
| 44 | + mLastMousePositionX = mapFromGlobal(QCursor::pos()).x(); |
| 45 | + UpdateMousePosition(); |
| 46 | + |
| 47 | + show(); |
| 48 | + grabMouse(); |
| 49 | +} |
| 50 | + |
| 51 | +void lcLadderWidget::UpdateMousePosition() |
| 52 | +{ |
| 53 | + QPoint MousePosition = mapFromGlobal(QCursor::pos()); |
| 54 | + |
| 55 | + if (rect().contains(MousePosition)) |
| 56 | + { |
| 57 | + int CellHeight = height() / static_cast<int>(mSteps.size()); |
| 58 | + int CurrentStep = MousePosition.y() / CellHeight; |
| 59 | + |
| 60 | + if (CurrentStep != mCurrentStep) |
| 61 | + { |
| 62 | + mCurrentStep = CurrentStep; |
| 63 | + |
| 64 | + update(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (mCurrentStep != -1) |
| 69 | + { |
| 70 | + QDoubleSpinBox* SpinBox = qobject_cast<QDoubleSpinBox*>(mSpinBox); |
| 71 | + |
| 72 | + if (SpinBox) |
| 73 | + { |
| 74 | + int Steps = (MousePosition.x() - mLastMousePositionX) / 10; |
| 75 | + |
| 76 | + if (Steps) |
| 77 | + { |
| 78 | + mLastMousePositionX = MousePosition.x(); |
| 79 | + |
| 80 | + SpinBox->setValue(SpinBox->value() + mSteps[mCurrentStep] * Steps); |
| 81 | + |
| 82 | + update(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void lcLadderWidget::CancelEditing() |
| 89 | +{ |
| 90 | + emit EditingCanceled(); |
| 91 | + |
| 92 | + releaseMouse(); |
| 93 | + deleteLater(); |
| 94 | +} |
| 95 | + |
| 96 | +void lcLadderWidget::FinishEditing() |
| 97 | +{ |
| 98 | + emit EditingFinished(); |
| 99 | + |
| 100 | + releaseMouse(); |
| 101 | + deleteLater(); |
| 102 | +} |
| 103 | + |
| 104 | +void lcLadderWidget::paintEvent(QPaintEvent* Event) |
| 105 | +{ |
| 106 | + Q_UNUSED(Event); |
| 107 | + |
| 108 | + QPainter Painter(this); |
| 109 | + |
| 110 | + int CellWidth = width(); |
| 111 | + int CellHeight = height() / static_cast<int>(mSteps.size()); |
| 112 | + |
| 113 | + Painter.fillRect(rect(), palette().brush(QPalette::Base)); |
| 114 | + |
| 115 | + if (mCurrentStep != -1) |
| 116 | + { |
| 117 | + QRect Rect(0, mCurrentStep * CellHeight, CellWidth, CellHeight); |
| 118 | + |
| 119 | + Painter.fillRect(Rect, palette().brush(QPalette::Highlight)); |
| 120 | + } |
| 121 | + |
| 122 | + Painter.setFont(font()); |
| 123 | + Painter.setPen(palette().color(QPalette::Text)); |
| 124 | + |
| 125 | + for (int Step = 0; Step < static_cast<int>(mSteps.size()); Step++) |
| 126 | + { |
| 127 | + QRect Rect(0, Step * CellHeight, CellWidth, CellHeight); |
| 128 | + QTextOption TextOption(Qt::AlignCenter); |
| 129 | + QString Text = QString::number(mSteps[Step]); |
| 130 | + |
| 131 | + if (Step == mCurrentStep) |
| 132 | + Text = QString("%1\n\n(%2)").arg(Text, mSpinBox->text()); |
| 133 | + |
| 134 | + Painter.drawText(Rect, Text, TextOption); |
| 135 | + } |
| 136 | + |
| 137 | + Painter.setPen(palette().color(QPalette::Shadow)); |
| 138 | + |
| 139 | + QRect Rect(rect()); |
| 140 | + Rect.adjust(0, 0, -1, -1); |
| 141 | + |
| 142 | + Painter.drawRect(Rect); |
| 143 | + |
| 144 | + for (int Step = 1; Step < static_cast<int>(mSteps.size()); Step++) |
| 145 | + Painter.drawLine(0, Step * CellHeight, CellWidth, Step * CellHeight); |
| 146 | +} |
| 147 | + |
| 148 | +bool lcLadderWidget::eventFilter(QObject* Object, QEvent* Event) |
| 149 | +{ |
| 150 | + if (Event->type() == QEvent::ShortcutOverride) |
| 151 | + { |
| 152 | + QKeyEvent* KeyEvent = static_cast<QKeyEvent*>(Event); |
| 153 | + |
| 154 | + if (KeyEvent->key() == Qt::Key_Escape) |
| 155 | + { |
| 156 | + CancelEditing(); |
| 157 | + |
| 158 | + KeyEvent->accept(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return QWidget::eventFilter(Object, Event); |
| 163 | +} |
| 164 | + |
| 165 | +void lcLadderWidget::mousePressEvent(QMouseEvent* MouseEvent) |
| 166 | +{ |
| 167 | + CancelEditing(); |
| 168 | + |
| 169 | + QWidget::mousePressEvent(MouseEvent); |
| 170 | +} |
| 171 | + |
| 172 | +void lcLadderWidget::mouseMoveEvent(QMouseEvent* MouseEvent) |
| 173 | +{ |
| 174 | + UpdateMousePosition(); |
| 175 | + |
| 176 | + QWidget::mouseMoveEvent(MouseEvent); |
| 177 | +} |
| 178 | + |
| 179 | +void lcLadderWidget::mouseReleaseEvent(QMouseEvent* MouseEvent) |
| 180 | +{ |
| 181 | + FinishEditing(); |
| 182 | + |
| 183 | + QWidget::mouseReleaseEvent(MouseEvent); |
| 184 | +} |
0 commit comments