LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / basevalidatinglineedit.cpp
blob894cccc0ba5b0b590778a66b2622d0aa6cdb4169
1 /**
2 ******************************************************************************
4 * @file basevalidatinglineedit.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
12 *****************************************************************************/
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.
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.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "basevalidatinglineedit.h"
31 #include <QtCore/QDebug>
33 enum { debug = 0 };
35 namespace Utils {
36 struct BaseValidatingLineEditPrivate {
37 explicit BaseValidatingLineEditPrivate(const QWidget *w);
39 const QColor m_okTextColor;
40 QColor m_errorTextColor;
42 BaseValidatingLineEdit::State m_state;
43 QString m_errorMessage;
44 QString m_initialText;
45 bool m_firstChange;
48 BaseValidatingLineEditPrivate::BaseValidatingLineEditPrivate(const QWidget *w) :
49 m_okTextColor(BaseValidatingLineEdit::textColor(w)),
50 m_errorTextColor(Qt::red),
51 m_state(BaseValidatingLineEdit::Invalid),
52 m_firstChange(true)
55 BaseValidatingLineEdit::BaseValidatingLineEdit(QWidget *parent) :
56 QLineEdit(parent),
57 m_bd(new BaseValidatingLineEditPrivate(this))
59 // Note that textChanged() is also triggered automagically by
60 // QLineEdit::setText(), no need to trigger manually.
61 connect(this, SIGNAL(textChanged(QString)), this, SLOT(slotChanged(QString)));
64 BaseValidatingLineEdit::~BaseValidatingLineEdit()
66 delete m_bd;
69 QString BaseValidatingLineEdit::initialText() const
71 return m_bd->m_initialText;
74 void BaseValidatingLineEdit::setInitialText(const QString &t)
76 if (m_bd->m_initialText != t) {
77 m_bd->m_initialText = t;
78 m_bd->m_firstChange = true;
79 setText(t);
83 QColor BaseValidatingLineEdit::errorColor() const
85 return m_bd->m_errorTextColor;
88 void BaseValidatingLineEdit::setErrorColor(const QColor &c)
90 m_bd->m_errorTextColor = c;
93 QColor BaseValidatingLineEdit::textColor(const QWidget *w)
95 return w->palette().color(QPalette::Active, QPalette::Text);
98 void BaseValidatingLineEdit::setTextColor(QWidget *w, const QColor &c)
100 QPalette palette = w->palette();
102 palette.setColor(QPalette::Active, QPalette::Text, c);
103 w->setPalette(palette);
106 BaseValidatingLineEdit::State BaseValidatingLineEdit::state() const
108 return m_bd->m_state;
111 bool BaseValidatingLineEdit::isValid() const
113 return m_bd->m_state == Valid;
116 QString BaseValidatingLineEdit::errorMessage() const
118 return m_bd->m_errorMessage;
121 void BaseValidatingLineEdit::slotChanged(const QString &t)
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;
133 // Changed..figure out if valid changed. DisplayingInitialText is not valid,
134 // but should not show error color. Also trigger on the first change.
135 if (newState != m_bd->m_state || m_bd->m_firstChange) {
136 const bool validHasChanged = (m_bd->m_state == Valid) != (newState == Valid);
137 m_bd->m_state = newState;
138 m_bd->m_firstChange = false;
139 setTextColor(this, newState == Invalid ? m_bd->m_errorTextColor : m_bd->m_okTextColor);
140 if (validHasChanged) {
141 emit validChanged(newState == Valid);
142 emit validChanged();
147 void BaseValidatingLineEdit::slotReturnPressed()
149 if (isValid()) {
150 emit validReturnPressed();
154 void BaseValidatingLineEdit::triggerChanged()
156 slotChanged(text());
158 } // namespace Utils