File indexing completed on 2024-04-21 03:41:52
0001 /*************************************************************************** 0002 * Copyright (C) 2004-2008 by Albert Astals Cid * 0003 * aacid@kde.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #include "integerinputdialog.h" 0012 0013 #include <QSpinBox> 0014 #include <QBoxLayout> 0015 #include <QLabel> 0016 #include <QSlider> 0017 #include <QDialogButtonBox> 0018 0019 IntegerInputDialog::IntegerInputDialog(QWidget *parent, const QString &title, const QString &question, 0020 int from, int upto, int byDefault) 0021 : QDialog(parent) 0022 { 0023 setWindowTitle(title); 0024 0025 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0026 0027 QLabel *questionLabel = new QLabel(question); 0028 mainLayout->addWidget(questionLabel); 0029 0030 QHBoxLayout *horizontalLayout = new QHBoxLayout(); 0031 mainLayout->addLayout(horizontalLayout); 0032 0033 _slider = new QSlider(Qt::Horizontal); 0034 _slider->setRange(from, upto); 0035 int value = ( from <= byDefault && byDefault <= upto ) ? byDefault : upto; 0036 _slider->setValue(value); 0037 connect(_slider, &QSlider::valueChanged, this, &IntegerInputDialog::sliderValueChanged); 0038 horizontalLayout->addWidget(_slider); 0039 0040 _spinBox = new QSpinBox(); 0041 _spinBox->setRange(from, upto); 0042 _spinBox->setValue(value); 0043 connect(_spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &IntegerInputDialog::spinboxValueChanged); 0044 horizontalLayout->addWidget(_spinBox); 0045 0046 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok 0047 | QDialogButtonBox::Cancel); 0048 connect(buttonBox, &QDialogButtonBox::accepted, this, &IntegerInputDialog::accept); 0049 connect(buttonBox, &QDialogButtonBox::rejected, this, &IntegerInputDialog::reject); 0050 mainLayout->addWidget(buttonBox); 0051 0052 setLayout(mainLayout); 0053 0054 _spinBox->setFocus(); 0055 } 0056 0057 int IntegerInputDialog::value() const 0058 { 0059 return _slider->value(); 0060 } 0061 0062 void IntegerInputDialog::setValue(int newValue) 0063 { 0064 if ( _slider->value() != newValue ) 0065 _slider->setValue(newValue); 0066 } 0067 0068 void IntegerInputDialog::sliderValueChanged(int newValue) 0069 { 0070 if ( _spinBox->value() != newValue ) 0071 _spinBox->setValue(newValue); 0072 } 0073 0074 void IntegerInputDialog::spinboxValueChanged(int newValue) 0075 { 0076 if ( _slider->value() != newValue ) 0077 _slider->setValue(newValue); 0078 } 0079 0080 int IntegerInputDialog::GetInteger(QWidget *parent, const QString &title, const QString &question, 0081 int from, int upto, int byDefault, bool *rOK) 0082 { 0083 int ret = -1; 0084 bool ok = false; 0085 IntegerInputDialog dialog(parent, title, question, from, upto, byDefault); 0086 if ( dialog.exec() == QDialog::Accepted ) 0087 { 0088 ok = true; 0089 ret = dialog.value(); 0090 } 0091 if ( rOK != nullptr ) *rOK = ok; 0092 return ret; 0093 0094 } 0095 0096 #include "moc_integerinputdialog.cpp"