1 /* This file is part of the KDE project
2 Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004-2016 Jarosław Staniek <staniek@kde.org>
4 Copyright (C) 2012 Oleg Kukharchuk <oleg.kuh@gmail.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include "kexidbdatepicker.h"
23 #include <kexiutils/utils.h>
24
25 #include <QDate>
26 #include <QLineEdit>
27 #include <QInputMethodEvent>
inputs(MockFS & FS) const28
29 KexiDBDatePicker::KexiDBDatePicker(QWidget *parent)
30 : KDatePicker(parent)
31 , KexiFormDataItemInterface()
32 , m_invalidState(false)
33 , m_readOnly(false)
34 {
35 connect(this, SIGNAL(dateChanged(QDate)), this, SLOT(slotValueChanged()));
36 connect(this, SIGNAL(dateEntered(QDate)), this, SLOT(slotValueChanged()));
37 }
38
39 KexiDBDatePicker::~KexiDBDatePicker()
40 {
41 }
42
43 void KexiDBDatePicker::setInvalidState(const QString& displayText)
44 {
45 Q_UNUSED(displayText);
46 m_invalidState = true;
47 setEnabled(false);
48 setReadOnly(true);
49 //! @todo move this to KexiDataItemInterface::setInvalidStateInternal() ?
50 if (focusPolicy() & Qt::TabFocus)
51 setFocusPolicy(Qt::ClickFocus);
52 KDatePicker::setDate(QDate());
53 }
54
55 void KexiDBDatePicker::setEnabled(bool enabled)
56 {
57 // prevent the user from reenabling the widget when it is in invalid state
58 if (enabled && m_invalidState)
59 return;
60 KDatePicker::setEnabled(enabled);
61 }
62
63 void KexiDBDatePicker::setValueInternal(const QVariant&, bool)
64 {
65 KDatePicker::setDate(KexiDataItemInterface::originalValue().toDate());
66 }
67
68 QVariant KexiDBDatePicker::value()
69 {
70 return KDatePicker::date();
71 }
72
73 void KexiDBDatePicker::slotValueChanged()
74 {
75 signalValueChanged();
initializeModuleCache(CompilerInvocation & CI)76 }
77
78 bool KexiDBDatePicker::valueIsNull()
79 {
80 return !KDatePicker::date().isValid();
81 }
82
83 bool KexiDBDatePicker::valueIsEmpty()
84 {
deleteModuleCache(const std::string ModuleCachePath)85 return !KDatePicker::date().isValid();
86 }
87
88 bool KexiDBDatePicker::isReadOnly() const
89 {
90 return m_readOnly;
91 }
92
93 void KexiDBDatePicker::setReadOnly(bool set)
94 {
preamble(PreambleParsedCallback PreambleCallback) const95 m_readOnly = set;
96 }
97
98 QWidget* KexiDBDatePicker::widget()
99 {
100 return this;
101 }
102
103 bool KexiDBDatePicker::cursorAtStart()
104 {
105 const QLineEdit *lineEdit=findChild<QLineEdit*>();
106 return lineEdit && lineEdit->hasFocus() && lineEdit->cursorPosition() == 0;
107 }
108
build() const109 bool KexiDBDatePicker::cursorAtEnd()
110 {
111 const QLineEdit *lineEdit = findChild<QLineEdit*>();
112 return lineEdit && lineEdit->hasFocus() && KexiUtils::cursorAtEnd(lineEdit);
113 }
114
115 void KexiDBDatePicker::clear()
116 {
117 KDatePicker::setDate(QDate());
118 }
119
120