OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / basevalidatinglineedit.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "basevalidatinglineedit.h"
35
36 #include <QtCore/QDebug>
37
38 enum { debug = 0 };
39
40 namespace Utils {
41
42 struct BaseValidatingLineEditPrivate {
43     explicit BaseValidatingLineEditPrivate(const QWidget *w);
44
45     const QColor m_okTextColor;
46     QColor m_errorTextColor;
47
48     BaseValidatingLineEdit::State m_state;
49     QString m_errorMessage;
50     QString m_initialText;
51     bool m_firstChange;
52 };
53
54 BaseValidatingLineEditPrivate::BaseValidatingLineEditPrivate(const QWidget *w) :
55     m_okTextColor(BaseValidatingLineEdit::textColor(w)),
56     m_errorTextColor(Qt::red),
57     m_state(BaseValidatingLineEdit::Invalid),
58     m_firstChange(true)
59 {
60 }
61
62 BaseValidatingLineEdit::BaseValidatingLineEdit(QWidget *parent) :
63     QLineEdit(parent),
64     m_bd(new BaseValidatingLineEditPrivate(this))
65 {
66     // Note that textChanged() is also triggered automagically by
67     // QLineEdit::setText(), no need to trigger manually.
68     connect(this, SIGNAL(textChanged(QString)), this, SLOT(slotChanged(QString)));
69 }
70
71 BaseValidatingLineEdit::~BaseValidatingLineEdit()
72 {
73     delete m_bd;
74 }
75
76 QString BaseValidatingLineEdit::initialText() const
77 {
78     return m_bd->m_initialText;
79 }
80
81 void BaseValidatingLineEdit::setInitialText(const QString &t)
82 {
83     if (m_bd->m_initialText != t) {
84         m_bd->m_initialText = t;
85         m_bd->m_firstChange = true;
86         setText(t);
87     }
88 }
89
90 QColor BaseValidatingLineEdit::errorColor() const
91 {
92     return m_bd->m_errorTextColor;
93 }
94
95 void BaseValidatingLineEdit::setErrorColor(const  QColor &c)
96 {
97      m_bd->m_errorTextColor = c;
98 }
99
100 QColor BaseValidatingLineEdit::textColor(const QWidget *w)
101 {
102     return w->palette().color(QPalette::Active, QPalette::Text);
103 }
104
105 void BaseValidatingLineEdit::setTextColor(QWidget *w, const QColor &c)
106 {
107     QPalette palette = w->palette();
108     palette.setColor(QPalette::Active, QPalette::Text, c);
109     w->setPalette(palette);
110 }
111
112 BaseValidatingLineEdit::State BaseValidatingLineEdit::state() const
113 {
114     return m_bd->m_state;
115 }
116
117 bool BaseValidatingLineEdit::isValid() const
118 {
119     return m_bd->m_state == Valid;
120 }
121
122 QString BaseValidatingLineEdit::errorMessage() const
123 {
124     return m_bd->m_errorMessage;
125 }
126
127 void BaseValidatingLineEdit::slotChanged(const QString &t)
128 {
129     m_bd->m_errorMessage.clear();
130     // Are we displaying the initial text?
131     const bool isDisplayingInitialText = !m_bd->m_initialText.isEmpty() && t == m_bd->m_initialText;
132     const State newState = isDisplayingInitialText ?
133                                DisplayingInitialText :
134                                (validate(t, &m_bd->m_errorMessage) ? Valid : Invalid);
135     setToolTip(m_bd->m_errorMessage);
136     if (debug)
137         qDebug() << Q_FUNC_INFO << t << "State" <<  m_bd->m_state << "->" << newState << m_bd->m_errorMessage;
138     // Changed..figure out if valid changed. DisplayingInitialText is not valid,
139     // but should not show error color. Also trigger on the first change.
140     if (newState != m_bd->m_state || m_bd->m_firstChange) {
141         const bool validHasChanged = (m_bd->m_state == Valid) != (newState == Valid);
142         m_bd->m_state = newState;
143         m_bd->m_firstChange = false;
144         setTextColor(this, newState == Invalid ? m_bd->m_errorTextColor : m_bd->m_okTextColor);
145         if (validHasChanged) {
146             emit validChanged(newState == Valid);
147             emit validChanged();
148         }
149     }
150 }
151
152 void BaseValidatingLineEdit::slotReturnPressed()
153 {
154     if (isValid())
155         emit validReturnPressed();
156 }
157
158 void BaseValidatingLineEdit::triggerChanged()
159 {
160     slotChanged(text());
161 }
162
163 } // namespace Utils