1 /* This file is part of the KDE project
2    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
3    Copyright (C) 2004-2006 Jarosław Staniek <staniek@kde.org>
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this program; see the file COPYING.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kexidbcheckbox.h"
22 #include <kexiutils/utils.h>
23 
24 #include <KDbQuerySchema>
25 
KexiDBCheckBox(const QString & text,QWidget * parent)26 KexiDBCheckBox::KexiDBCheckBox(const QString &text, QWidget *parent)
27         : QCheckBox(text, parent), KexiFormDataItemInterface()
28         , m_invalidState(false)
29         , m_tristateChanged(false)
30         , m_tristate(TristateDefault)
31 {
32     setFocusPolicy(Qt::StrongFocus);
33     updateTristate();
34     connect(this, SIGNAL(stateChanged(int)), this, SLOT(slotStateChanged(int)));
35 }
36 
~KexiDBCheckBox()37 KexiDBCheckBox::~KexiDBCheckBox()
38 {
39 }
40 
setInvalidState(const QString & displayText)41 void KexiDBCheckBox::setInvalidState(const QString& displayText)
42 {
43     setEnabled(false);
44     setCheckState(Qt::PartiallyChecked);
45     m_invalidState = true;
46 //! @todo move this to KexiDataItemInterface::setInvalidStateInternal() ?
47     if (focusPolicy() & Qt::TabFocus)
48         setFocusPolicy(Qt::ClickFocus);
49     setText(displayText);
50 }
51 
52 void
setEnabled(bool enabled)53 KexiDBCheckBox::setEnabled(bool enabled)
54 {
55     if (enabled && m_invalidState)
56         return;
57     QCheckBox::setEnabled(enabled);
58 }
59 
setReadOnly(bool readOnly)60 void KexiDBCheckBox::setReadOnly(bool readOnly)
61 {
62     setEnabled(!readOnly);
63 }
64 
setValueInternal(const QVariant & add,bool removeOld)65 void KexiDBCheckBox::setValueInternal(const QVariant &add, bool removeOld)
66 {
67     Q_UNUSED(add);
68     Q_UNUSED(removeOld);
69     if (isTristateInternal())
70         setCheckState(KexiDataItemInterface::originalValue().isNull()
71                       ? Qt::PartiallyChecked : (KexiDataItemInterface::originalValue().toBool() ? Qt::Checked : Qt::Unchecked));
72     else
73         setCheckState(KexiDataItemInterface::originalValue().toBool() ? Qt::Checked : Qt::Unchecked);
74 }
75 
value()76 QVariant KexiDBCheckBox::value()
77 {
78     if (checkState() == Qt::PartiallyChecked)
79         return QVariant();
80     return checkState() == Qt::Checked;
81 }
82 
slotStateChanged(int)83 void KexiDBCheckBox::slotStateChanged(int)
84 {
85     signalValueChanged();
86 }
87 
valueIsNull()88 bool KexiDBCheckBox::valueIsNull()
89 {
90     return checkState() == Qt::PartiallyChecked;
91 }
92 
valueIsEmpty()93 bool KexiDBCheckBox::valueIsEmpty()
94 {
95     return false;
96 }
97 
isReadOnly() const98 bool KexiDBCheckBox::isReadOnly() const
99 {
100     return !isEnabled();
101 }
102 
103 QWidget*
widget()104 KexiDBCheckBox::widget()
105 {
106     return this;
107 }
108 
cursorAtStart()109 bool KexiDBCheckBox::cursorAtStart()
110 {
111     return false; //! \todo ?
112 }
113 
cursorAtEnd()114 bool KexiDBCheckBox::cursorAtEnd()
115 {
116     return false; //! \todo ?
117 }
118 
clear()119 void KexiDBCheckBox::clear()
120 {
121     setCheckState(Qt::PartiallyChecked);
122 }
123 
setTristate(KexiDBCheckBox::Tristate tristate)124 void KexiDBCheckBox::setTristate(KexiDBCheckBox::Tristate tristate)
125 {
126     m_tristateChanged = true;
127     m_tristate = tristate;
128     updateTristate();
129 }
130 
isTristate() const131 KexiDBCheckBox::Tristate KexiDBCheckBox::isTristate() const
132 {
133     return m_tristate;
134 }
135 
isTristateInternal() const136 bool KexiDBCheckBox::isTristateInternal() const
137 {
138     if (m_tristate == TristateDefault)
139         return !dataSource().isEmpty();
140 
141     return m_tristate == TristateOn;
142 }
143 
updateTristate()144 void KexiDBCheckBox::updateTristate()
145 {
146     if (m_tristate == TristateDefault) {
147 //! @todo the data source may be defined as NOT NULL... thus disallowing NULL state
148 //! @todo Retrieve default Tristate value from global settings or so, for now we're defaulting
149 //!       to false because this covers 99% of use cases
150         QCheckBox::setTristate(false);
151     } else {
152         QCheckBox::setTristate(m_tristate == TristateOn);
153     }
154 }
155 
setDataSource(const QString & ds)156 void KexiDBCheckBox::setDataSource(const QString &ds)
157 {
158     KexiFormDataItemInterface::setDataSource(ds);
159     updateTristate();
160 }
161 
setDisplayDefaultValue(QWidget * widget,bool displayDefaultValue)162 void KexiDBCheckBox::setDisplayDefaultValue(QWidget *widget, bool displayDefaultValue)
163 {
164     KexiFormDataItemInterface::setDisplayDefaultValue(widget, displayDefaultValue);
165     // initialize display parameters for default / entered value
166     KexiDisplayUtils::DisplayParameters * const params
167         = displayDefaultValue ? m_displayParametersForDefaultValue : m_displayParametersForEnteredValue;
168     QPalette pal(palette());
169     pal.setColor(QPalette::Active, QPalette::Foreground, params->textColor);
170     setPalette(pal);
171 }
172 
paintEvent(QPaintEvent * e)173 void KexiDBCheckBox::paintEvent(QPaintEvent* e)
174 {
175     QPalette origPal;
176     if (editingMode()) {
177         origPal = palette();
178         QPalette pal(palette());
179         pal.setBrush(QPalette::WindowText, Qt::transparent);
180         setPalette(pal);
181     }
182     QCheckBox::paintEvent(e);
183     if (editingMode()) {
184         setPalette(origPal);
185     }
186 }
187 
188