Repository navigation
Expand file tree
/
Copy pathQelNumberInput.cpp
More file actions
255 lines (214 loc) · 8.17 KB
/
Copy pathQelNumberInput.cpp
File metadata and controls
255 lines (214 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "QelNumberInput.h"
#include <QEvent>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QStyleOption>
#include <limits>
namespace qel {
QelNumberInput::QelNumberInput(QWidget *parent,
int minValue,
int maxValue,
double initialValue,
double step,
bool readonly,
bool disabled,
bool controls,
ControlsPosition controlsPosition, // 修改为枚举类型
const QString &placeholder)
: QWidget(parent),
minValue(minValue),
maxValue(maxValue),
currentValue(initialValue),
step(step),
readonly(readonly),
disabled(disabled),
controls(controls),
controlsPosition(controlsPosition),
size(Size::Default),
precision(0)
{
// 创建按钮和显示框
decreaseButton = new QPushButton("-", this);
increaseButton = new QPushButton("+", this);
valueDisplay = new QLineEdit(QString::number(initialValue, 'f', precision), this);
// 设置默认按钮大小为正方形,并根据 size 设置整体大小
setSize(size);
// 设置 QLineEdit
valueDisplay->setReadOnly(readonly);
valueDisplay->setAlignment(Qt::AlignCenter);
valueDisplay->setPlaceholderText(placeholder);
// 设置按钮和输入框的大小策略
decreaseButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
increaseButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
valueDisplay->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
valueDisplay->setStyleSheet("QLineEdit { border-top: 1px solid #dcdfe6; border-bottom: 1px solid #dcdfe6; "
"border-left: 0px; border-right: 0px; background-color: white; }");
// 设置布局
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
setControlsPosition(controlsPosition); // 根据传入参数设置布局
setLayout(layout);
// 连接信号槽
connect(decreaseButton, &QPushButton::clicked, this, &QelNumberInput::onDecrease);
connect(increaseButton, &QPushButton::clicked, this, &QelNumberInput::onIncrease);
// 初始状态更新
setDisabled(disabled);
updateControlsState();
}
void QelNumberInput::setControlsPosition(ControlsPosition position) {
controlsPosition = position;
QString decreaseButtonStyle;
QString increaseButtonStyle;
QLayout *currentLayout = this->layout();
if (currentLayout) {
delete currentLayout; // 清除当前布局,防止布局冲突
}
setSize(size);
QVBoxLayout *rightLayout = nullptr;
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
if (controlsPosition == ControlsPosition::Right) {
decreaseButtonStyle = "QPushButton { border: 1px solid #dcdfe6; background-color: #f5f7fa; "
"border-bottom-right-radius: 6px; }"
"QPushButton:pressed { background-color: #e6e9ef; }"
"QPushButton:hover { border-color: #409EFF; color: #409EFF; }";
increaseButtonStyle = "QPushButton { border: 1px solid #dcdfe6; background-color: #f5f7fa; "
"border-top-right-radius: 6px; }"
"QPushButton:pressed { background-color: #e6e9ef; }"
"QPushButton:hover { border-color: #409EFF; color: #409EFF; }";
rightLayout = new QVBoxLayout();
rightLayout->setSpacing(0);
rightLayout->setContentsMargins(0, 0, 0, 0);
rightLayout->addWidget(increaseButton);
rightLayout->addWidget(decreaseButton);
layout->addWidget(valueDisplay);
layout->addLayout(rightLayout);
} else {
decreaseButtonStyle = "QPushButton { border: 1px solid #dcdfe6; background-color: #f5f7fa; "
"border-top-left-radius: 6px; border-bottom-left-radius: 6px; }"
"QPushButton:pressed { background-color: #e6e9ef; }"
"QPushButton:hover { border-color: #409EFF; color: #409EFF; }";
increaseButtonStyle = "QPushButton { border: 1px solid #dcdfe6; background-color: #f5f7fa; "
"border-top-right-radius: 6px; border-bottom-right-radius: 6px; }"
"QPushButton:pressed { background-color: #e6e9ef; }"
"QPushButton:hover { border-color: #409EFF; color: #409EFF; }";
layout->addWidget(decreaseButton);
layout->addWidget(valueDisplay);
layout->addWidget(increaseButton);
}
decreaseButton->setStyleSheet(decreaseButtonStyle);
increaseButton->setStyleSheet(increaseButtonStyle);
setLayout(layout);
}
void QelNumberInput::setSize(Size size) {
this->size = size;
int buttonWidth;
int buttonHeight;
int widgetHeight;
int widgetWidth = 0;
int fontSize;
switch (size) {
case Size::Small:
buttonWidth = 26;
buttonHeight = buttonWidth; // 默认情况下,按钮是正方形
widgetHeight = buttonWidth;
fontSize = 14;
break;
case Size::Default:
buttonWidth = 38;
buttonHeight = buttonWidth; // 默认情况下,按钮是正方形
widgetHeight = buttonWidth;
fontSize = 16;
break;
case Size::Large:
buttonWidth = 42;
buttonHeight = buttonWidth; // 默认情况下,按钮是正方形
widgetHeight = buttonWidth;
fontSize = 18;
break;
}
// 如果 controlsPosition 为 LeftRight,则调整按钮的高度为宽度的一半
if (controlsPosition == ControlsPosition::Right) {
buttonHeight = buttonWidth / 2;
widgetHeight = buttonHeight * 2;
}
// 设置加减按钮的大小
decreaseButton->setFixedSize(buttonWidth, buttonHeight);
increaseButton->setFixedSize(buttonWidth, buttonHeight);
valueDisplay->setFixedHeight(buttonWidth);
// 确保控件的整体宽度合适
int displayWidth = buttonWidth * 3;
if (controlsPosition == ControlsPosition::Right) {
widgetWidth = displayWidth + buttonWidth;
} else if (controlsPosition == ControlsPosition::Default) {
widgetWidth = buttonWidth * 2 + displayWidth ;
}
setFixedSize(widgetWidth, widgetHeight); // 设置整个控件的大小
// 设置按钮的字体大小
QFont font = decreaseButton->font();
font.setPointSize(fontSize);
decreaseButton->setFont(font);
increaseButton->setFont(font);
}
double QelNumberInput::value() const {
return currentValue;
}
void QelNumberInput::setValue(double value) {
if (value >= minValue && value <= maxValue) {
currentValue = value;
updateDisplay();
emit valueChanged(currentValue);
}
}
void QelNumberInput::setMinValue(int minValue) {
this->minValue = minValue;
}
void QelNumberInput::setMaxValue(int maxValue) {
this->maxValue = maxValue;
}
void QelNumberInput::setStep(double step) {
this->step = step;
}
void QelNumberInput::setReadonly(bool readonly) {
this->readonly = readonly;
valueDisplay->setReadOnly(readonly);
}
void QelNumberInput::setDisabled(bool disabled) {
this->disabled = disabled;
setEnabled(!disabled);
updateControlsState();
}
void QelNumberInput::setPlaceholder(const QString &placeholder) {
valueDisplay->setPlaceholderText(placeholder);
}
void QelNumberInput::setPrecision(int precision) {
this->precision = precision;
updateDisplay();
}
void QelNumberInput::onIncrease() {
if (currentValue + step <= maxValue) {
currentValue += step;
updateDisplay();
emit valueChanged(currentValue);
emit increaseIconClicked();
}
}
void QelNumberInput::onDecrease() {
if (currentValue - step >= minValue) {
currentValue -= step;
updateDisplay();
emit valueChanged(currentValue);
emit decreaseIconClicked();
}
}
void QelNumberInput::updateDisplay() {
valueDisplay->setText(QString::number(currentValue, 'f', precision));
}
void QelNumberInput::updateControlsState() {
decreaseButton->setEnabled(!disabled && controls);
increaseButton->setEnabled(!disabled && controls);
}
}