1
/* This file is part of the KDE project
2
Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3
Copyright (C) 2003-2016 JarosÅaw Staniek <staniek@kde.org>
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.
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.
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.
21
#include "kexiinputtableedit.h"
22
#include <kexi_global.h>
23
#include <kexiutils/utils.h>
26
#include <KDbFieldValidator>
27
#include <KDbLongLongValidator>
29
#include <KColorScheme>
32
#include <QApplication>
34
#include <QHBoxLayout>
40
class MyLineEdit : public QLineEdit
44
explicit MyLineEdit(QWidget *parent) : QLineEdit(parent) {}
46
virtual void drawFrame(QPainter * p) {
47
p->setPen(QPen(palette().text(), 1.0));
49
p->drawLine(r.topLeft(), r.topRight());
50
p->drawLine(r.topRight(), r.bottomRight());
51
p->drawLine(r.topRight(), r.bottomLeft());
52
if (pos().x() == 0) //draw left side only when it is @ the edge
53
p->drawLine(r.bottomLeft(), r.topLeft());
57
//======================================================
59
KexiInputTableEdit::KexiInputTableEdit(KDbTableViewColumn &column, QWidget *parent)
60
: KexiTableEdit(column, parent)
65
KexiInputTableEdit::~KexiInputTableEdit()
69
void KexiInputTableEdit::init()
71
// qDebug() << "m_origValue.typeName()==" << m_origValue.typeName();
72
// qDebug() << "type== " << field()->typeName();
73
// qDebug() << "displayed type== " << displayedField()->typeName();
75
m_textFormatter.setField( field() );
76
KexiTextFormatter::OverrideDecimalPlaces overrideDecimalPlaces;
77
overrideDecimalPlaces.enabled = true;
78
overrideDecimalPlaces.value = -1; // all possible digits
79
m_textFormatter.setOverrideDecimalPlaces(overrideDecimalPlaces);
80
m_textFormatter.setGroupSeparatorsEnabled(false); // needed, otherwise text box contains separators (confusing)
82
//create layer for internal editor
83
QHBoxLayout *lyr = new QHBoxLayout(this);
84
lyr->setContentsMargins(0, 0, 0, 0);
86
//create internal editor
87
m_lineedit = new MyLineEdit(this);
88
m_lineedit->setObjectName("KexiInputTableEdit-MyLineEdit");
89
connect(m_lineedit, SIGNAL(textEdited(QString)),
90
this, SLOT(slotTextEdited(QString)));
91
updateLineEditStyleSheet();
92
lyr->addWidget(m_lineedit);
93
const bool align_right = displayedField()->isNumericType();
95
m_lineedit->setAlignment(Qt::AlignRight);
98
setViewWidget(m_lineedit);
99
m_calculatedCell = false;
103
connect(m_cview->completionBox(), SIGNAL(activated(QString)),
104
this, SLOT(completed(QString)));
105
connect(m_cview->completionBox(), SIGNAL(highlighted(QString)),
106
this, SLOT(completed(QString)));
107
m_cview->completionBox()->setTabHandling(true);
111
void KexiInputTableEdit::setValueInternal(const QVariant& add, bool removeOld)
114
QString text(m_textFormatter.toString(removeOld ? QVariant() : KexiDataItemInterface::originalValue(), add.toString(),
116
if (text.isEmpty()) {
117
if (KexiDataItemInterface::originalValue().toString().isEmpty()) {
118
//we have to set NULL initial value:
119
m_lineedit->setText(QString());
122
m_lineedit->setText(text);
125
//! @todo by default we're moving to the end of editor, ADD OPTION allowing "select all chars"
127
m_cview->selectAll();
129
m_lineedit->end(false);
131
if (!m_lineedit->validator()) {
132
QValidator *validator = new KDbFieldValidator(*field(), m_lineedit);
133
validator->setObjectName("KexiInputTableEdit-validator");
134
m_lineedit->setValidator(validator);
136
emitLengthExceededIfNeeded(lengthExceeded);
139
void KexiInputTableEdit::paintEvent(QPaintEvent * /*e*/)
142
p.setPen(QPen(palette().text(), 1.0));
147
KexiInputTableEdit::setRestrictedCompletion()
152
if (m_cview->text().isEmpty())
155
qDebug() << "something to do";
156
m_cview->useGlobalKeyBindings();
159
QStringList::ConstIterator it, end(m_comp.constEnd());
160
for (it = m_comp.constBegin(); it != end; ++it) {
161
if ((*it).startsWith(m_cview->text()))
164
m_cview->setCompletedItems(newC);
169
KexiInputTableEdit::completed(const QString &s)
172
m_lineedit->setText(s);
175
bool KexiInputTableEdit::valueChanged()
177
return KexiTableEdit::valueChanged();
180
bool KexiInputTableEdit::valueIsNull()
182
return m_lineedit->text().isNull();
185
bool KexiInputTableEdit::valueIsEmpty()
187
return !m_lineedit->text().isNull() && m_lineedit->text().isEmpty();
190
QVariant KexiInputTableEdit::value()
192
return m_textFormatter.fromString(m_lineedit->text());
196
KexiInputTableEdit::clear()
201
bool KexiInputTableEdit::cursorAtStart()
203
return m_lineedit->cursorPosition() == 0;
206
bool KexiInputTableEdit::cursorAtEnd()
208
return KexiUtils::cursorAtEnd(m_lineedit);
211
QSize KexiInputTableEdit::totalSize() const
215
return m_lineedit->size();
218
void KexiInputTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
220
Q_UNUSED(visibleValue);
221
//! @todo handle rich text?
223
qApp->clipboard()->setText(m_textFormatter.toString(value, QString(), &lengthExceeded));
226
void KexiInputTableEdit::handleAction(const QString& actionName)
228
const bool alreadyVisible = m_lineedit->isVisible();
230
if (actionName == "edit_paste") {
231
if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
232
emit editRequested();
236
} else if (actionName == "edit_cut") {
237
//! @todo handle rich text?
238
if (!alreadyVisible) { //cut the entire text if the cell was not in edit mode
239
emit editRequested();
240
m_lineedit->selectAll();
246
bool KexiInputTableEdit::showToolTipIfNeeded(const QVariant& value, const QRect& rect,
247
const QFontMetrics& fm, bool focused)
250
QString text(value.type() == QVariant::String
251
? value.toString() : m_textFormatter.toString(value, QString(), &lengthExceeded));
253
QRect internalRect(rect);
254
internalRect.setLeft(rect.x() + leftMargin());
255
internalRect.setWidth(internalRect.width() - rightMargin(focused) - 2*3);
256
qDebug() << rect << internalRect << fm.width(text);
257
return fm.width(text) > internalRect.width();
260
void KexiInputTableEdit::moveCursorToEnd()
262
m_lineedit->end(false/*!mark*/);
265
void KexiInputTableEdit::moveCursorToStart()
267
m_lineedit->home(false/*!mark*/);
270
void KexiInputTableEdit::selectAll()
272
m_lineedit->selectAll();
275
void KexiInputTableEdit::slotTextEdited(const QString& text)
277
signalValueChanged();
278
bool lengthExceeded = m_textFormatter.lengthExceeded(text);
279
emitLengthExceededIfNeeded(lengthExceeded);
282
bool KexiInputTableEdit::fixup()
284
const QString t(m_lineedit->text());
285
bool lengthExceeded = m_textFormatter.lengthExceeded(t);
286
if (lengthExceeded) {
287
m_lineedit->setText(t.left(field()->maxLength()));
292
void KexiInputTableEdit::updateLineEditStyleSheet()
294
KColorScheme cs(QPalette::Active);
295
QColor focus = cs.decoration(KColorScheme::FocusColor).color();
296
const bool align_right = displayedField()->isNumericType();
297
m_lineedit->setStyleSheet(QString("QLineEdit { \
298
border: 1px solid %1; \
299
border-radius: 0px; \
300
padding: 0px %2px 0px %3px; }")
302
.arg(m_rightMarginWhenFocused) // right
303
.arg(align_right ? 0 : 2) // left
305
//qDebug() << m_rightMarginWhenFocused << m_lineedit->styleSheet();
308
KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiInputEditorFactoryItem, KexiInputTableEdit)
310
#include "kexiinputtableedit.moc"