1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtstringspinbox.h"
26 class QxtStringSpinBoxPrivate : public QxtPrivate<QxtStringSpinBox>
29 QXT_DECLARE_PUBLIC(QxtStringSpinBox);
30 int startsWith(const QString& start, QString& string) const;
34 int QxtStringSpinBoxPrivate::startsWith(const QString& start, QString& string) const
36 const int size = strings.size();
37 for (int i = 0; i < size; ++i)
39 if (strings.at(i).startsWith(start, Qt::CaseInsensitive))
42 string = strings.at(i);
51 \class QxtStringSpinBox QxtStringSpinBox
53 \brief A spin box with string items.
55 QxtStringSpinBox is spin box that takes strings. QxtStringSpinBox allows
56 the user to choose a value by clicking the up and down buttons or by
57 pressing Up or Down on the keyboard to increase or decrease the value
58 currently displayed. The user can also type the value in manually.
60 \image html qxtstringspinbox.png "QxtStringSpinBox in Cleanlooks style."
64 Constructs a new QxtStringSpinBox with \a parent.
66 QxtStringSpinBox::QxtStringSpinBox(QWidget* pParent) : QSpinBox(pParent)
72 Destructs the spin box.
74 QxtStringSpinBox::~QxtStringSpinBox()
78 \property QxtStringSpinBox::strings
79 \brief This property holds the string items of the spin box.
81 const QStringList& QxtStringSpinBox::strings() const
83 return qxt_d().strings;
86 void QxtStringSpinBox::setStrings(const QStringList& strings)
88 qxt_d().strings = strings;
89 setRange(0, strings.size() - 1);
90 if (!strings.isEmpty())
94 void QxtStringSpinBox::fixup(QString& input) const
96 // just attempt to change the input string to be valid according to the string list
97 // no need to result in a valid string, callers of this function are responsible to
98 // re-test the validity afterwards
100 // try finding a string from the list which starts with input
101 input = input.simplified();
102 if (!input.isEmpty())
104 qxt_d().startsWith(input, input);
108 QValidator::State QxtStringSpinBox::validate(QString& input, int& pos) const
110 // Invalid: input is invalid according to the string list
111 // Intermediate: it is likely that a little more editing will make the input acceptable
112 // (e.g. the user types "A" and stringlist contains "ABC")
113 // Acceptable: the input is valid.
116 QValidator::State state = QValidator::Invalid;
117 if (qxt_d().strings.contains(input))
120 state = QValidator::Acceptable;
122 else if (input.isEmpty() || qxt_d().startsWith(input, temp) != -1)
124 // still empty or some string in the list starts with input
125 state = QValidator::Intermediate;
131 QString QxtStringSpinBox::textFromValue(int value) const
133 Q_ASSERT(qxt_d().strings.isEmpty() || (value >= 0 && value < qxt_d().strings.size()));
134 return qxt_d().strings.isEmpty() ? QLatin1String("") : qxt_d().strings.at(value);
137 int QxtStringSpinBox::valueFromText(const QString& text) const
139 return qxt_d().strings.indexOf(text);