dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
basevalidatinglineedit.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "basevalidatinglineedit.h"
29 
30 #include <QtCore/QDebug>
31 
32 enum { debug = 0 };
33 
34 namespace Utils {
35 
37  explicit BaseValidatingLineEditPrivate(const QWidget *w);
38 
39  const QColor m_okTextColor;
41 
42  BaseValidatingLineEdit::State m_state;
43  QString m_errorMessage;
44  QString m_initialText;
46 };
47 
49  m_okTextColor(BaseValidatingLineEdit::textColor(w)),
50  m_errorTextColor(Qt::red),
51  m_state(BaseValidatingLineEdit::Invalid),
52  m_firstChange(true)
53 {
54 }
55 
56 BaseValidatingLineEdit::BaseValidatingLineEdit(QWidget *parent) :
57  QLineEdit(parent),
58  m_bd(new BaseValidatingLineEditPrivate(this))
59 {
60  // Note that textChanged() is also triggered automagically by
61  // QLineEdit::setText(), no need to trigger manually.
62  connect(this, SIGNAL(textChanged(QString)), this, SLOT(slotChanged(QString)));
63 }
64 
65 BaseValidatingLineEdit::~BaseValidatingLineEdit()
66 {
67  delete m_bd;
68 }
69 
70 QString BaseValidatingLineEdit::initialText() const
71 {
72  return m_bd->m_initialText;
73 }
74 
75 void BaseValidatingLineEdit::setInitialText(const QString &t)
76 {
77  if (m_bd->m_initialText != t) {
78  m_bd->m_initialText = t;
79  m_bd->m_firstChange = true;
80  setText(t);
81  }
82 }
83 
84 QColor BaseValidatingLineEdit::errorColor() const
85 {
86  return m_bd->m_errorTextColor;
87 }
88 
89 void BaseValidatingLineEdit::setErrorColor(const QColor &c)
90 {
91  m_bd->m_errorTextColor = c;
92 }
93 
94 QColor BaseValidatingLineEdit::textColor(const QWidget *w)
95 {
96  return w->palette().color(QPalette::Active, QPalette::Text);
97 }
98 
99 void BaseValidatingLineEdit::setTextColor(QWidget *w, const QColor &c)
100 {
101  QPalette palette = w->palette();
102  palette.setColor(QPalette::Active, QPalette::Text, c);
103  w->setPalette(palette);
104 }
105 
106 BaseValidatingLineEdit::State BaseValidatingLineEdit::state() const
107 {
108  return m_bd->m_state;
109 }
110 
111 bool BaseValidatingLineEdit::isValid() const
112 {
113  return m_bd->m_state == Valid;
114 }
115 
116 QString BaseValidatingLineEdit::errorMessage() const
117 {
118  return m_bd->m_errorMessage;
119 }
120 
122 {
123  m_bd->m_errorMessage.clear();
124  // Are we displaying the initial text?
125  const bool isDisplayingInitialText = !m_bd->m_initialText.isEmpty() && t == m_bd->m_initialText;
126  const State newState = isDisplayingInitialText ?
127  DisplayingInitialText :
128  (validate(t, &m_bd->m_errorMessage) ? Valid : Invalid);
129  setToolTip(m_bd->m_errorMessage);
130  if (debug)
131  qDebug() << Q_FUNC_INFO << t << "State" << m_bd->m_state << "->" << newState << m_bd->m_errorMessage;
132  // Changed..figure out if valid changed. DisplayingInitialText is not valid,
133  // but should not show error color. Also trigger on the first change.
134  if (newState != m_bd->m_state || m_bd->m_firstChange) {
135  const bool validHasChanged = (m_bd->m_state == Valid) != (newState == Valid);
136  m_bd->m_state = newState;
137  m_bd->m_firstChange = false;
138  setTextColor(this, newState == Invalid ? m_bd->m_errorTextColor : m_bd->m_okTextColor);
139  if (validHasChanged) {
140  emit validChanged(newState == Valid);
141  emit validChanged();
142  }
143  }
144 }
145 
147 {
148  if (isValid())
149  emit validReturnPressed();
150 }
151 
152 void BaseValidatingLineEdit::triggerChanged()
153 {
154  slotChanged(text());
155 }
156 
157 } // namespace Utils
virtual bool validate(const QString &value, QString *errorMessage) const =0
const char t[]
Definition: coreconstants.h:40
virtual void slotChanged(const QString &t)