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,16 +61,133 @@ void KeywordLineEdit::SetInputText(QString text)
this->setText(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::PopupLineEditInput(QWidget *parent) PopupLineEditInput::PopupLineEditInput(QWidget *parent)
:QDialog(parent) :QDialog(parent)
{ {
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
_line = NULL; _line = NULL;
QHBoxLayout *lay = new QHBoxLayout(); QHBoxLayout *lay = new QHBoxLayout();
lay->setContentsMargins(0,0,0,0); lay->setContentsMargins(0,0,0,0);
_textInput = new QLineEdit(this); _textInput = new KeyLineEdit(this);
lay->addWidget(_textInput); lay->addWidget(_textInput);
this->setLayout(lay); this->setLayout(lay);
@ -95,6 +212,7 @@ void PopupLineEditInput::changeEvent(QEvent *event)
QWidget::changeEvent(event); QWidget::changeEvent(event);
} }
void PopupLineEditInput::InputRelease() void PopupLineEditInput::InputRelease()
{ {
sig_inputEnd(_textInput->text()); sig_inputEnd(_textInput->text());
@ -108,7 +226,7 @@ void PopupLineEditInput::InputRelease()
} }
} }
void PopupLineEditInput::onCheckPostion() void PopupLineEditInput::onCheckPositionTimeout()
{ {
if (_line != NULL){ if (_line != NULL){
QPoint p1 = _line->pos(); QPoint p1 = _line->pos();
@ -150,7 +268,7 @@ void PopupLineEditInput::Popup(QWidget *editline)
move_timer = new QTimer(this); move_timer = new QTimer(this);
move_timer->setInterval(100); move_timer->setInterval(100);
connect(move_timer, SIGNAL(timeout()), this, SLOT(onCheckPostion())); connect(move_timer, SIGNAL(timeout()), this, SLOT(onCheckPositionTimeout()));
move_timer->start(); move_timer->start();
this->show(); this->show();
@ -163,41 +281,42 @@ void PopupLineEditInput::Popup(QWidget *editline)
} }
PopupLineEdit::PopupLineEdit(const QString &text, QWidget *parent) PopupLineEdit::PopupLineEdit(const QString &text, QWidget *parent)
:QLineEdit(text, parent) :KeyLineEdit(text, parent)
{ {
_is_number_mode = false; _is_number_mode = false;
_is_instant = false; _is_instant = false;
_popup_input = NULL; _popup_input = NULL;
_min = 0;
_max = 0;
} }
void PopupLineEdit::mousePressEvent(QMouseEvent *event) void PopupLineEdit::mousePressEvent(QMouseEvent *event)
{ {
showPupopInput(); showPupopInput();
QLineEdit::mousePressEvent(event); KeyLineEdit::mousePressEvent(event);
} }
void PopupLineEdit::showPupopInput() void PopupLineEdit::showPupopInput()
{ {
#ifdef _WIN32 #ifdef _WIN32
PopupLineEditInput *input = new PopupLineEditInput(this); PopupLineEditInput *input = new PopupLineEditInput(this);
auto line = input->GetInput();
QString mask = this->inputMask(); QString mask = this->inputMask();
if (mask != ""){ if (mask != ""){
input->GetInput()->setInputMask(mask); line->setInputMask(mask);
} }
input->GetInput()->setMaxLength(this->maxLength()); line->setMaxLength(this->maxLength());
input->GetInput()->setText(this->text()); line->setText(this->text());
input->setFont(this->font()); line->setFont(this->font());
line->set_number_mode(_is_number_mode);
line->setRange(_min, _max);
if (_is_number_mode){ if (!_is_number_mode){
QIntValidator *validator = new QIntValidator();
input->GetInput()->setValidator(validator);
}
else{
auto regular = this->validator(); auto regular = this->validator();
if (regular != NULL){ if (regular != NULL){
input->GetInput()->setValidator(regular); line->setValidator(regular);
} }
} }
@ -205,6 +324,11 @@ void PopupLineEdit::showPupopInput()
_popup_input = input; _popup_input = input;
connect(input, SIGNAL(sig_inputEnd(QString)), this, SLOT(onPopupInputEditEnd(QString))); 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); input->Popup(this);
#endif #endif
} }
@ -220,36 +344,17 @@ void PopupLineEdit::onPopupInputEditEnd(QString text)
if (text != _old_text){ if (text != _old_text){
setModified(true); setModified(true);
editingFinished(); editingFinished();
if (_is_number_mode){
valueChanged(value());
}
} }
} }
void PopupLineEdit::set_number_mode(bool isNumberMode) void PopupLineEdit::onPopupInputValueChanged(int v)
{ {
_is_number_mode = isNumberMode; setValue(v);
valueChanged(v);
if (_is_number_mode){
QIntValidator *validator = new QIntValidator();
setValidator(validator);
}
else{
setValidator(NULL);
}
}
int PopupLineEdit::value()
{
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));
} }
void PopupLineEdit::show() void PopupLineEdit::show()
@ -272,4 +377,4 @@ void PopupLineEdit::hide()
} }
QLineEdit::hide(); QLineEdit::hide();
} }

View File

@ -53,6 +53,51 @@ private:
bool _bText; 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 //---------PopupLineEditInput
class PopupLineEditInput : public QDialog class PopupLineEditInput : public QDialog
{ {
@ -63,7 +108,7 @@ public:
void Popup(QWidget *editline); void Popup(QWidget *editline);
inline QLineEdit* GetInput(){ inline KeyLineEdit* GetInput(){
return _textInput; return _textInput;
} }
@ -75,19 +120,19 @@ signals:
void sig_inputEnd(QString text); void sig_inputEnd(QString text);
private slots: private slots:
void onCheckPostion(); void onCheckPositionTimeout();
protected: protected:
void changeEvent(QEvent *event) override; void changeEvent(QEvent *event) override;
void InputRelease(); void InputRelease();
private: private:
QLineEdit *_textInput; KeyLineEdit *_textInput;
QWidget *_line; QWidget *_line;
}; };
//---------PopupLineEdit //---------PopupLineEdit
class PopupLineEdit : public QLineEdit class PopupLineEdit : public KeyLineEdit
{ {
Q_OBJECT Q_OBJECT
@ -95,37 +140,25 @@ public:
explicit PopupLineEdit(QWidget *parent = nullptr); explicit PopupLineEdit(QWidget *parent = nullptr);
explicit PopupLineEdit(const QString &text, 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){ inline void set_instant_mode(bool isInstant){
_is_instant = isInstant; _is_instant = isInstant;
} }
int value();
void setValue(int value);
void show(); void show();
void hide(); void hide();
private slots:
void onPopupInputEditEnd(QString text);
void onPopupInputValueChanged(int v);
protected: protected:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
private slots:
void onPopupInputEditEnd(QString text);
private: private:
void showPupopInput(); void showPupopInput();
private: private:
QString _old_text; QString _old_text;
bool _is_number_mode;
bool _is_instant; bool _is_instant;
PopupLineEditInput *_popup_input; PopupLineEditInput *_popup_input;
}; };

View File

@ -80,9 +80,10 @@ TriggerDock::TriggerDock(QWidget *parent, SigSession *session) :
_adv_radioButton = new QRadioButton(_widget); _adv_radioButton = new QRadioButton(_widget);
_position_label = new QLabel(_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->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 = new QSlider(Qt::Horizontal, _widget);
_position_slider->setRange(MinTrigPosition, DS_MAX_TRIG_PERCENT); _position_slider->setRange(MinTrigPosition, DS_MAX_TRIG_PERCENT);
connect(_position_slider, SIGNAL(valueChanged(int)), _position_spinBox, SLOT(setValue(int))); 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); _value0_lineEdit_list.push_back(_value0_lineEdit);
PopupLineEdit *_count_spinBox = new PopupLineEdit(_stage_tabWidget); PopupLineEdit *_count_spinBox = new PopupLineEdit(_stage_tabWidget);
_count_spinBox->set_number_mode(true); _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->setButtonSymbols(QAbstractSpinBox::NoButtons);
_count_spinBox_list.push_back(_count_spinBox); _count_spinBox_list.push_back(_count_spinBox);
DsComboBox *_inv0_comboBox = new DsComboBox(_stage_tabWidget); DsComboBox *_inv0_comboBox = new DsComboBox(_stage_tabWidget);

View File

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