Ok, the long awaited config wizard is here (at least in a very basic state). There...
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtstringspinbox.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtstringspinbox.h"
25
26 class QxtStringSpinBoxPrivate : public QxtPrivate<QxtStringSpinBox>
27 {
28 public:
29     QXT_DECLARE_PUBLIC(QxtStringSpinBox);
30     int startsWith(const QString& start, QString& string) const;
31     QStringList strings;
32 };
33
34 int QxtStringSpinBoxPrivate::startsWith(const QString& start, QString& string) const
35 {
36     const int size = strings.size();
37     for (int i = 0; i < size; ++i)
38     {
39         if (strings.at(i).startsWith(start, Qt::CaseInsensitive))
40         {
41             // found
42             string = strings.at(i);
43             return i;
44         }
45     }
46     // not found
47     return -1;
48 }
49
50 /*!
51     \class QxtStringSpinBox QxtStringSpinBox
52     \ingroup QxtGui
53     \brief A spin box with string items.
54
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.
59
60     \image html qxtstringspinbox.png "QxtStringSpinBox in Cleanlooks style."
61  */
62
63 /*!
64     Constructs a new QxtStringSpinBox with \a parent.
65  */
66 QxtStringSpinBox::QxtStringSpinBox(QWidget* pParent) : QSpinBox(pParent)
67 {
68     setRange(0, 0);
69 }
70
71 /*!
72     Destructs the spin box.
73  */
74 QxtStringSpinBox::~QxtStringSpinBox()
75 {}
76
77 /*!
78     \property QxtStringSpinBox::strings
79     \brief This property holds the string items of the spin box.
80  */
81 const QStringList& QxtStringSpinBox::strings() const
82 {
83     return qxt_d().strings;
84 }
85
86 void QxtStringSpinBox::setStrings(const QStringList& strings)
87 {
88     qxt_d().strings = strings;
89     setRange(0, strings.size() - 1);
90     if (!strings.isEmpty())
91         setValue(0);
92 }
93
94 void QxtStringSpinBox::fixup(QString& input) const
95 {
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
99
100     // try finding a string from the list which starts with input
101     input = input.simplified();
102     if (!input.isEmpty())
103     {
104         qxt_d().startsWith(input, input);
105     }
106 }
107
108 QValidator::State QxtStringSpinBox::validate(QString& input, int& pos) const
109 {
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.
114     Q_UNUSED(pos);
115     QString temp;
116     QValidator::State state = QValidator::Invalid;
117     if (qxt_d().strings.contains(input))
118     {
119         // exact match
120         state = QValidator::Acceptable;
121     }
122     else if (input.isEmpty() || qxt_d().startsWith(input, temp) != -1)
123     {
124         // still empty or some string in the list starts with input
125         state = QValidator::Intermediate;
126     }
127     // else invalid
128     return state;
129 }
130
131 QString QxtStringSpinBox::textFromValue(int value) const
132 {
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);
135 }
136
137 int QxtStringSpinBox::valueFromText(const QString& text) const
138 {
139     return qxt_d().strings.indexOf(text);
140 }