Skip to content

Commit f760907

Browse files
committed
Added ladder widget to float properties.
1 parent ed24c15 commit f760907

File tree

4 files changed

+244
-6
lines changed

4 files changed

+244
-6
lines changed

common/lc_doublespinbox.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "lc_global.h"
22
#include "lc_doublespinbox.h"
3+
#include "lc_ladderwidget.h"
34
#include "lc_qutils.h"
45

56
lcDoubleSpinBox::lcDoubleSpinBox(QWidget* Parent)
@@ -25,6 +26,9 @@ QString lcDoubleSpinBox::textFromValue(double Value) const
2526

2627
void lcDoubleSpinBox::CancelEditing()
2728
{
29+
if (value() == mInitialValue)
30+
return;
31+
2832
emit EditingCanceled();
2933

3034
setValue(mInitialValue);
@@ -38,12 +42,13 @@ void lcDoubleSpinBox::FinishEditing()
3842
emit EditingFinished();
3943

4044
mInitialValue = value();
45+
46+
clearFocus();
4147
}
4248

4349
void lcDoubleSpinBox::ReturnPressed()
4450
{
4551
FinishEditing();
46-
clearFocus();
4752
}
4853

4954
void lcDoubleSpinBox::HandleMousePressEvent(QMouseEvent* MouseEvent)
@@ -58,7 +63,23 @@ void lcDoubleSpinBox::HandleMousePressEvent(QMouseEvent* MouseEvent)
5863
if (mDragMode == DragMode::Value)
5964
CancelEditing();
6065

61-
mDragMode = DragMode::None;
66+
if (mDragMode != DragMode::None)
67+
{
68+
mDragMode = DragMode::None;
69+
return;
70+
}
71+
72+
if (MouseEvent->buttons() == Qt::MiddleButton)
73+
{
74+
lcLadderWidget* LadderWidget = new lcLadderWidget(this);
75+
76+
connect(LadderWidget, &lcLadderWidget::EditingCanceled, this, &lcDoubleSpinBox::CancelEditing);
77+
connect(LadderWidget, &lcLadderWidget::EditingFinished, this, &lcDoubleSpinBox::FinishEditing);
78+
79+
mInitialValue = value();
80+
81+
LadderWidget->Show();
82+
}
6283
}
6384
}
6485

@@ -126,12 +147,8 @@ void lcDoubleSpinBox::HandleMouseReleaseEvent(QMouseEvent* MouseEvent)
126147
Q_UNUSED(MouseEvent);
127148

128149
if (mDragMode == DragMode::Value)
129-
{
130150
FinishEditing();
131151

132-
clearFocus();
133-
}
134-
135152
mDragMode = DragMode::None;
136153
}
137154

common/lc_ladderwidget.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
}

common/lc_ladderwidget.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
class lcLadderWidget: public QWidget
4+
{
5+
Q_OBJECT
6+
7+
public:
8+
lcLadderWidget(QAbstractSpinBox* SpinBox);
9+
virtual ~lcLadderWidget() = default;
10+
11+
void Show();
12+
13+
bool eventFilter(QObject* Object, QEvent* Event) override;
14+
void mousePressEvent(QMouseEvent* MouseEvent) override;
15+
void mouseMoveEvent(QMouseEvent* MouseEvent) override;
16+
void mouseReleaseEvent(QMouseEvent* MouseEvent) override;
17+
18+
signals:
19+
void EditingCanceled();
20+
void EditingFinished();
21+
22+
protected slots:
23+
void CancelEditing();
24+
void FinishEditing();
25+
26+
protected:
27+
void UpdateMousePosition();
28+
29+
void paintEvent(QPaintEvent* PaintEvent) override;
30+
31+
QAbstractSpinBox* mSpinBox = nullptr;
32+
std::vector<double> mSteps;
33+
int mCurrentStep = -1;
34+
int mLastMousePositionX = 0;
35+
};

leocad.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ SOURCES += \
198198
common/lc_instructions.cpp \
199199
common/lc_instructionsdialog.cpp \
200200
common/lc_keyframewidget.cpp \
201+
common/lc_ladderwidget.cpp \
201202
common/lc_library.cpp \
202203
common/lc_lxf.cpp \
203204
common/lc_mainwindow.cpp \
@@ -273,6 +274,7 @@ HEADERS += \
273274
common/lc_instructions.h \
274275
common/lc_instructionsdialog.h \
275276
common/lc_keyframewidget.h \
277+
common/lc_ladderwidget.h \
276278
common/lc_library.h \
277279
common/lc_lxf.h \
278280
common/lc_mainwindow.h \

0 commit comments

Comments
 (0)