1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "basevalidatinglineedit.h"
36 #include <QtCore/QDebug>
42 struct BaseValidatingLineEditPrivate {
43 explicit BaseValidatingLineEditPrivate(const QWidget *w);
45 const QColor m_okTextColor;
46 QColor m_errorTextColor;
48 BaseValidatingLineEdit::State m_state;
49 QString m_errorMessage;
50 QString m_initialText;
54 BaseValidatingLineEditPrivate::BaseValidatingLineEditPrivate(const QWidget *w) :
55 m_okTextColor(BaseValidatingLineEdit::textColor(w)),
56 m_errorTextColor(Qt::red),
57 m_state(BaseValidatingLineEdit::Invalid),
62 BaseValidatingLineEdit::BaseValidatingLineEdit(QWidget *parent) :
64 m_bd(new BaseValidatingLineEditPrivate(this))
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)));
71 BaseValidatingLineEdit::~BaseValidatingLineEdit()
76 QString BaseValidatingLineEdit::initialText() const
78 return m_bd->m_initialText;
81 void BaseValidatingLineEdit::setInitialText(const QString &t)
83 if (m_bd->m_initialText != t) {
84 m_bd->m_initialText = t;
85 m_bd->m_firstChange = true;
90 QColor BaseValidatingLineEdit::errorColor() const
92 return m_bd->m_errorTextColor;
95 void BaseValidatingLineEdit::setErrorColor(const QColor &c)
97 m_bd->m_errorTextColor = c;
100 QColor BaseValidatingLineEdit::textColor(const QWidget *w)
102 return w->palette().color(QPalette::Active, QPalette::Text);
105 void BaseValidatingLineEdit::setTextColor(QWidget *w, const QColor &c)
107 QPalette palette = w->palette();
108 palette.setColor(QPalette::Active, QPalette::Text, c);
109 w->setPalette(palette);
112 BaseValidatingLineEdit::State BaseValidatingLineEdit::state() const
114 return m_bd->m_state;
117 bool BaseValidatingLineEdit::isValid() const
119 return m_bd->m_state == Valid;
122 QString BaseValidatingLineEdit::errorMessage() const
124 return m_bd->m_errorMessage;
127 void BaseValidatingLineEdit::slotChanged(const QString &t)
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);
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);
152 void BaseValidatingLineEdit::slotReturnPressed()
155 emit validReturnPressed();
158 void BaseValidatingLineEdit::triggerChanged()