1 /* This file is part of the KDE project
2 Copyright (C) 2013 Oleg Kukharchuk <oleg.kuh@gmail.com>
3 Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
4 Copyright (C) 2004-2005 Jarosław Staniek <staniek@kde.org>
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 "KexiDBPushButton.h"
23 #include <core/kexiproject.h>
24 #include <core/KexiMainWindowIface.h>
25
26 #include <KDbConnection>
27
28 #include <QUrl>
29
30 class KexiDBPushButtonPrivate
31 {
32 public:
KexiDBPushButtonPrivate()33 KexiDBPushButtonPrivate() {}
34
35 KexiFormEventAction::ActionData onClickActionData;
36 };
37
KexiDBPushButton(const QString & text,QWidget * parent)38 KexiDBPushButton::KexiDBPushButton(const QString & text, QWidget * parent)
39 : KexiPushButton(text, parent)
40 , d(new KexiDBPushButtonPrivate)
41 {
42 QString basePath = Kexi::basePathForProject(
43 KexiMainWindowIface::global()->project()->dbConnection()->data());
44 if (!basePath.isEmpty()) {
45 setLocalBasePath(basePath);
46 }
47 }
48
~KexiDBPushButton()49 KexiDBPushButton::~KexiDBPushButton()
50 {
51 delete d;
52 }
53
setValueInternal(const QVariant & add,bool removeOld)54 void KexiDBPushButton::setValueInternal(const QVariant& add, bool removeOld)
55 {
56 Q_UNUSED(add)
57 Q_UNUSED(removeOld)
58
59 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
60 KexiPushButton::setHyperlink(KexiDataItemInterface::originalValue().toString());
61 }
62 QUrl url(KexiDataItemInterface::originalValue().toString());
63 QFontMetrics f(font());
64 QString text;
65 QString path = url.url(QUrl::PreferLocalFile);
66 if (url.isLocalFile()){
67 QString fileName = url.fileName();
68 text = f.elidedText(path.left(path.size() - fileName.size()),
69 Qt::ElideMiddle, width()-f.width(fileName) - 10) + fileName;
70 } else {
71 text = f.elidedText(path, Qt::ElideMiddle, width() - 10);
72
73 }
74 setText(text);
75 setToolTip(url.url(QUrl::PreferLocalFile));
76 }
77
value()78 QVariant KexiDBPushButton::value()
79 {
80 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
81 return KexiPushButton::hyperlink();
82 }
83 return QVariant();
84 }
85
clear()86 void KexiDBPushButton::clear()
87 {
88 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
89 KexiPushButton::setHyperlink(QString());
90 }
91 }
92
cursorAtStart()93 bool KexiDBPushButton::cursorAtStart()
94 {
95 return false;
96 }
97
cursorAtEnd()98 bool KexiDBPushButton::cursorAtEnd()
99 {
100 return false;
101 }
102
valueIsNull()103 bool KexiDBPushButton::valueIsNull()
104 {
105 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
106 return KexiPushButton::hyperlink().isNull();
107 }
108 return true;
109 }
110
valueIsEmpty()111 bool KexiDBPushButton::valueIsEmpty()
112 {
113 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
114 return KexiPushButton::hyperlink().isEmpty();
115 }
116 return true;
117 }
118
setInvalidState(const QString & displayText)119 void KexiDBPushButton::setInvalidState(const QString &displayText)
120 {
121 if (KexiPushButton::hyperlinkType() == KexiPushButton::DynamicHyperlink) {
122 setText(displayText);
123 }
124 }
125
isReadOnly() const126 bool KexiDBPushButton::isReadOnly() const
127 {
128 return true;
129 }
130
setReadOnly(bool readOnly)131 void KexiDBPushButton::setReadOnly(bool readOnly)
132 {
133 Q_UNUSED(readOnly);
134 }
135
onClickAction() const136 QString KexiDBPushButton::onClickAction() const
137 {
138 return d->onClickActionData.string;
139 }
140
setOnClickAction(const QString & actionString)141 void KexiDBPushButton::setOnClickAction(const QString& actionString)
142 {
143 d->onClickActionData.string = actionString;
144 }
145
onClickActionOption() const146 QString KexiDBPushButton::onClickActionOption() const
147 {
148 return d->onClickActionData.option;
149 }
150
setOnClickActionOption(const QString & option)151 void KexiDBPushButton::setOnClickActionOption(const QString& option)
152 {
153 d->onClickActionData.option = option;
154 }
155