A custom spinbox class for windows

This commit is contained in:
dreamsourcelabTAI 2024-04-24 11:12:18 +08:00
parent ba990b7cd3
commit f8cf75f4e5
4 changed files with 208 additions and 69 deletions

View File

@ -61,6 +61,123 @@ void KeywordLineEdit::SetInputText(QString text)
this->setText(text);
}
//---------KeyLineEdit
KeyLineEdit::KeyLineEdit(QWidget *parent)
:KeyLineEdit("", parent)
{
}
KeyLineEdit::KeyLineEdit(const QString &text, QWidget *parent)
:QLineEdit(text, parent)
{
_min = 0;
_max = 0;
_is_number_mode = false;
_is_spin_mode = false;
}
void KeyLineEdit::keyPressEvent(QKeyEvent *event)
{
QLineEdit::keyPressEvent(event);
if (_is_number_mode && (_min != 0 || _max != 0))
{
if (event->key() >= '0' && event->key() <= '9')
{
QString new_text = text();
if (new_text != "")
{
int v = new_text.toInt();
int old_v = v;
if (v < _min ){
v = _min;
}
else if (v > _max){
v = _max;
}
if (v != old_v){
setText(QString::number(v));
valueChanged(v);
}
}
}
}
}
void KeyLineEdit::wheelEvent(QWheelEvent *event)
{
if (_is_number_mode && _is_spin_mode)
{
QString new_text = text();
if (new_text != "")
{
int v = new_text.toInt();
int old_v = v;
if (event->delta() > 0){
v++;
}
else{
v--;
}
if (_min != 0 || _max != 0)
{
if (v < _min ){
v = _min;
}
else if (v > _max){
v = _max;
}
}
if (v != old_v){
setText(QString::number(v));
valueChanged(v);
}
}
event->accept();
return;
}
QLineEdit::wheelEvent(event);
}
void KeyLineEdit::setValue(int v)
{
_is_number_mode = true;
this->setText(QString::number(v));
}
int KeyLineEdit::value()
{
assert(_is_number_mode);
QString text = this->text();
if (text != ""){
return text.toInt();
}
return 0;
}
void KeyLineEdit::set_number_mode(bool isNumberMode)
{
_is_number_mode = isNumberMode;
if (_is_number_mode){
QIntValidator *validator = new QIntValidator();
setValidator(validator);
}
else{
setValidator(NULL);
}
}
//---------PopupLineEditInput
PopupLineEditInput::PopupLineEditInput(QWidget *parent)
:QDialog(parent)
@ -70,7 +187,7 @@ PopupLineEditInput::PopupLineEditInput(QWidget *parent)
QHBoxLayout *lay = new QHBoxLayout();
lay->setContentsMargins(0,0,0,0);
_textInput = new QLineEdit(this);
_textInput = new KeyLineEdit(this);
lay->addWidget(_textInput);
this->setLayout(lay);
@ -95,6 +212,7 @@ void PopupLineEditInput::changeEvent(QEvent *event)
QWidget::changeEvent(event);
}
void PopupLineEditInput::InputRelease()
{
sig_inputEnd(_textInput->text());
@ -108,7 +226,7 @@ void PopupLineEditInput::InputRelease()
}
}
void PopupLineEditInput::onCheckPostion()
void PopupLineEditInput::onCheckPositionTimeout()
{
if (_line != NULL){
QPoint p1 = _line->pos();
@ -150,7 +268,7 @@ void PopupLineEditInput::Popup(QWidget *editline)
move_timer = new QTimer(this);
move_timer->setInterval(100);
connect(move_timer, SIGNAL(timeout()), this, SLOT(onCheckPostion()));
connect(move_timer, SIGNAL(timeout()), this, SLOT(onCheckPositionTimeout()));
move_timer->start();
this->show();
@ -163,41 +281,42 @@ void PopupLineEditInput::Popup(QWidget *editline)
}
PopupLineEdit::PopupLineEdit(const QString &text, QWidget *parent)
:QLineEdit(text, parent)
:KeyLineEdit(text, parent)
{
_is_number_mode = false;
_is_instant = false;
_popup_input = NULL;
_min = 0;
_max = 0;
}
void PopupLineEdit::mousePressEvent(QMouseEvent *event)
{
showPupopInput();
QLineEdit::mousePressEvent(event);
KeyLineEdit::mousePressEvent(event);
}
void PopupLineEdit::showPupopInput()
{
#ifdef _WIN32
PopupLineEditInput *input = new PopupLineEditInput(this);
auto line = input->GetInput();
QString mask = this->inputMask();
if (mask != ""){
input->GetInput()->setInputMask(mask);
line->setInputMask(mask);
}
input->GetInput()->setMaxLength(this->maxLength());
input->GetInput()->setText(this->text());
input->setFont(this->font());
line->setMaxLength(this->maxLength());
line->setText(this->text());
line->setFont(this->font());
line->set_number_mode(_is_number_mode);
line->setRange(_min, _max);
if (_is_number_mode){
QIntValidator *validator = new QIntValidator();
input->GetInput()->setValidator(validator);
}
else{
if (!_is_number_mode){
auto regular = this->validator();
if (regular != NULL){
input->GetInput()->setValidator(regular);
line->setValidator(regular);
}
}
@ -205,6 +324,11 @@ void PopupLineEdit::showPupopInput()
_popup_input = input;
connect(input, SIGNAL(sig_inputEnd(QString)), this, SLOT(onPopupInputEditEnd(QString)));
if (_is_number_mode){
connect(line, SIGNAL(valueChanged(int)), this, SLOT(onPopupInputValueChanged(int)));
}
input->Popup(this);
#endif
}
@ -220,36 +344,17 @@ void PopupLineEdit::onPopupInputEditEnd(QString text)
if (text != _old_text){
setModified(true);
editingFinished();
}
}
void PopupLineEdit::set_number_mode(bool isNumberMode)
{
_is_number_mode = isNumberMode;
if (_is_number_mode){
QIntValidator *validator = new QIntValidator();
setValidator(validator);
valueChanged(value());
}
else{
setValidator(NULL);
}
}
int PopupLineEdit::value()
void PopupLineEdit::onPopupInputValueChanged(int v)
{
assert(_is_number_mode);
QString text = this->text();
if (text != ""){
return text.toInt();
}
return 0;
}
void PopupLineEdit::setValue(int value)
{
this->setText(QString::number(value));
setValue(v);
valueChanged(v);
}
void PopupLineEdit::show()

View File

@ -53,6 +53,51 @@ private:
bool _bText;
};
//---------KeyLineEdit
class KeyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit KeyLineEdit(QWidget *parent = nullptr);
explicit KeyLineEdit(const QString &text, QWidget *parent = nullptr);
inline void setRange(int min, int max){
_max = max;
_min = min;
_is_spin_mode = true;
set_number_mode(true);
}
inline bool is_number_mode(){
return _is_number_mode;
}
void set_number_mode(bool isNumberMode);
inline void enable_spin_mode(bool enabled){
_is_spin_mode = enabled;
}
int value();
signals:
void valueChanged(int v);
public slots:
void setValue(int v);
private:
void keyPressEvent(QKeyEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
protected:
int _min;
int _max;
bool _is_number_mode;
bool _is_spin_mode;
};
//---------PopupLineEditInput
class PopupLineEditInput : public QDialog
{
@ -63,7 +108,7 @@ public:
void Popup(QWidget *editline);
inline QLineEdit* GetInput(){
inline KeyLineEdit* GetInput(){
return _textInput;
}
@ -75,19 +120,19 @@ signals:
void sig_inputEnd(QString text);
private slots:
void onCheckPostion();
void onCheckPositionTimeout();
protected:
void changeEvent(QEvent *event) override;
void InputRelease();
private:
QLineEdit *_textInput;
KeyLineEdit *_textInput;
QWidget *_line;
};
//---------PopupLineEdit
class PopupLineEdit : public QLineEdit
class PopupLineEdit : public KeyLineEdit
{
Q_OBJECT
@ -95,37 +140,25 @@ public:
explicit PopupLineEdit(QWidget *parent = nullptr);
explicit PopupLineEdit(const QString &text, QWidget *parent = nullptr);
inline bool is_number_mode(){
return _is_number_mode;
}
void set_number_mode(bool isNumberMode);
inline bool is_instant_mode(){
return _is_instant;
}
inline void set_instant_mode(bool isInstant){
_is_instant = isInstant;
}
int value();
void setValue(int value);
void show();
void hide();
protected:
void mousePressEvent(QMouseEvent *event) override;
private slots:
void onPopupInputEditEnd(QString text);
void onPopupInputValueChanged(int v);
protected:
void mousePressEvent(QMouseEvent *event) override;
private:
void showPupopInput();
private:
QString _old_text;
bool _is_number_mode;
bool _is_instant;
PopupLineEditInput *_popup_input;
};

View File

@ -80,9 +80,10 @@ TriggerDock::TriggerDock(QWidget *parent, SigSession *session) :
_adv_radioButton = new QRadioButton(_widget);
_position_label = new QLabel(_widget);
_position_spinBox = new QSpinBox(_widget);
_position_spinBox = new PopupLineEdit(_widget);
_position_spinBox->setRange(MinTrigPosition, DS_MAX_TRIG_PERCENT);
_position_spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons);
_position_spinBox->setValue(1);
// _position_spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons);
_position_slider = new QSlider(Qt::Horizontal, _widget);
_position_slider->setRange(MinTrigPosition, DS_MAX_TRIG_PERCENT);
connect(_position_slider, SIGNAL(valueChanged(int)), _position_spinBox, SLOT(setValue(int)));
@ -594,7 +595,7 @@ void TriggerDock::setup_adv_tab()
_value0_lineEdit_list.push_back(_value0_lineEdit);
PopupLineEdit *_count_spinBox = new PopupLineEdit(_stage_tabWidget);
_count_spinBox->set_number_mode(true);
// _count_spinBox->setRange(1, INT32_MAX);
_count_spinBox->setRange(1, INT32_MAX);
//_count_spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons);
_count_spinBox_list.push_back(_count_spinBox);
DsComboBox *_inv0_comboBox = new DsComboBox(_stage_tabWidget);

View File

@ -108,7 +108,7 @@ private:
QRadioButton *_adv_radioButton;
QLabel *_position_label;
QSpinBox *_position_spinBox;
PopupLineEdit *_position_spinBox;
QSlider *_position_slider;
QLabel *_stages_label;