File indexing completed on 2024-05-12 05:09:49

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "spinbox.h"
0026 
0027 #include <QLineEdit>
0028 
0029 using Tellico::GUI::SpinBox;
0030 
0031 SpinBox::SpinBox(int min_, int max_, QWidget * parent_) : QSpinBox(parent_) {
0032   setMinimum(min_);
0033   setMaximum(max_);
0034   setAlignment(Qt::AlignRight);
0035   // I want to be able to have an empty value at the minimum
0036   // an empty string just removes the special value, so set white space
0037   setSpecialValueText(QStringLiteral(" "));
0038   connect(lineEdit(), &QLineEdit::textChanged, this, &SpinBox::checkValue);
0039 }
0040 
0041 void SpinBox::checkValue(const QString& text_) {
0042   Q_UNUSED(text_);
0043   // if we delete everything in the lineedit, then we want to have an empty value
0044   // which is equivalent to the minimum, or special value text
0045   if(cleanText().isEmpty()) {
0046     setValue(minimum());
0047   }
0048 }
0049 
0050 QValidator::State SpinBox::validate(QString& text_, int& pos_) const {
0051   if(text_.endsWith(QLatin1Char(' '))) {
0052     text_.remove(text_.length()-1, 1);
0053   }
0054   return QSpinBox::validate(text_, pos_);
0055 }
0056 
0057 void SpinBox::stepBy(int steps_) {
0058   const int oldValue = value();
0059   const QString oldText = lineEdit()->text();
0060 
0061   QSpinBox::stepBy(steps_);
0062 
0063   // QT bug? Apparently, after the line edit is cleared, the internal value is not changed
0064   // then when the little buttons are clicked, the internal value is inserted in the line edit
0065   // but the valueChanged signal is not emitted
0066   if(oldText != lineEdit()->text() && oldValue == value()) {
0067     emit valueChanged(value());
0068 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
0069     emit valueChanged(text());
0070 #else
0071     emit textChanged(text());
0072 #endif
0073   }
0074 }