~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/plugins/forms/widgets/kexidbcheckbox.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
Import upstream version 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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
    setMinimumHeight(sizeHint().height());
 
34
    setMinimumWidth(minimumHeight());
 
35
    updateTristate();
 
36
    connect(this, SIGNAL(stateChanged(int)), this, SLOT(slotStateChanged(int)));
 
37
}
 
38
 
 
39
KexiDBCheckBox::~KexiDBCheckBox()
 
40
{
 
41
}
 
42
 
 
43
void KexiDBCheckBox::setInvalidState(const QString& displayText)
 
44
{
 
45
    setEnabled(false);
 
46
    setCheckState(Qt::PartiallyChecked);
 
47
    m_invalidState = true;
 
48
//! @todo move this to KexiDataItemInterface::setInvalidStateInternal() ?
 
49
    if (focusPolicy() & Qt::TabFocus)
 
50
        setFocusPolicy(Qt::ClickFocus);
 
51
    setText(displayText);
 
52
}
 
53
 
 
54
void
 
55
KexiDBCheckBox::setEnabled(bool enabled)
 
56
{
 
57
    if (enabled && m_invalidState)
 
58
        return;
 
59
    QCheckBox::setEnabled(enabled);
 
60
}
 
61
 
 
62
void KexiDBCheckBox::setReadOnly(bool readOnly)
 
63
{
 
64
    setEnabled(!readOnly);
 
65
}
 
66
 
 
67
void KexiDBCheckBox::setValueInternal(const QVariant &add, bool removeOld)
 
68
{
 
69
    Q_UNUSED(add);
 
70
    Q_UNUSED(removeOld);
 
71
    if (isTristateInternal())
 
72
        setCheckState(KexiDataItemInterface::originalValue().isNull()
 
73
                      ? Qt::PartiallyChecked : (KexiDataItemInterface::originalValue().toBool() ? Qt::Checked : Qt::Unchecked));
 
74
    else
 
75
        setCheckState(KexiDataItemInterface::originalValue().toBool() ? Qt::Checked : Qt::Unchecked);
 
76
}
 
77
 
 
78
QVariant KexiDBCheckBox::value()
 
79
{
 
80
    if (checkState() == Qt::PartiallyChecked)
 
81
        return QVariant();
 
82
    return checkState() == Qt::Checked;
 
83
}
 
84
 
 
85
void KexiDBCheckBox::slotStateChanged(int)
 
86
{
 
87
    signalValueChanged();
 
88
}
 
89
 
 
90
bool KexiDBCheckBox::valueIsNull()
 
91
{
 
92
    return checkState() == Qt::PartiallyChecked;
 
93
}
 
94
 
 
95
bool KexiDBCheckBox::valueIsEmpty()
 
96
{
 
97
    return false;
 
98
}
 
99
 
 
100
bool KexiDBCheckBox::isReadOnly() const
 
101
{
 
102
    return !isEnabled();
 
103
}
 
104
 
 
105
QWidget*
 
106
KexiDBCheckBox::widget()
 
107
{
 
108
    return this;
 
109
}
 
110
 
 
111
bool KexiDBCheckBox::cursorAtStart()
 
112
{
 
113
    return false; //! \todo ?
 
114
}
 
115
 
 
116
bool KexiDBCheckBox::cursorAtEnd()
 
117
{
 
118
    return false; //! \todo ?
 
119
}
 
120
 
 
121
void KexiDBCheckBox::clear()
 
122
{
 
123
    setCheckState(Qt::PartiallyChecked);
 
124
}
 
125
 
 
126
void KexiDBCheckBox::setTristate(KexiDBCheckBox::Tristate tristate)
 
127
{
 
128
    m_tristateChanged = true;
 
129
    m_tristate = tristate;
 
130
    updateTristate();
 
131
}
 
132
 
 
133
KexiDBCheckBox::Tristate KexiDBCheckBox::isTristate() const
 
134
{
 
135
    return m_tristate;
 
136
}
 
137
 
 
138
bool KexiDBCheckBox::isTristateInternal() const
 
139
{
 
140
    if (m_tristate == TristateDefault)
 
141
        return !dataSource().isEmpty();
 
142
 
 
143
    return m_tristate == TristateOn;
 
144
}
 
145
 
 
146
void KexiDBCheckBox::updateTristate()
 
147
{
 
148
    if (m_tristate == TristateDefault) {
 
149
//! @todo the data source may be defined as NOT NULL... thus disallowing NULL state
 
150
        QCheckBox::setTristate(!dataSource().isEmpty());
 
151
    } else {
 
152
        QCheckBox::setTristate(m_tristate == TristateOn);
 
153
    }
 
154
}
 
155
 
 
156
void KexiDBCheckBox::setDataSource(const QString &ds)
 
157
{
 
158
    KexiFormDataItemInterface::setDataSource(ds);
 
159
    updateTristate();
 
160
}
 
161
 
 
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
 
 
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