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

« back to all changes in this revision

Viewing changes to src/widget/tableview/kexiinputtableedit.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) 2002 Lucijan Busch <lucijan@gmx.at>
 
3
   Copyright (C) 2003-2016 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 "kexiinputtableedit.h"
 
22
#include <kexi_global.h>
 
23
#include <kexiutils/utils.h>
 
24
 
 
25
#include <KDbField>
 
26
#include <KDbFieldValidator>
 
27
#include <KDbLongLongValidator>
 
28
 
 
29
#include <KColorScheme>
 
30
 
 
31
#include <QPainter>
 
32
#include <QApplication>
 
33
#include <QClipboard>
 
34
#include <QHBoxLayout>
 
35
#include <QLineEdit>
 
36
#include <QLocale>
 
37
#include <QDebug>
 
38
 
 
39
//! @internal
 
40
class MyLineEdit : public QLineEdit
 
41
{
 
42
    Q_OBJECT
 
43
public:
 
44
    explicit MyLineEdit(QWidget *parent) : QLineEdit(parent) {}
 
45
protected:
 
46
    virtual void drawFrame(QPainter * p) {
 
47
        p->setPen(QPen(palette().text(), 1.0));
 
48
        QRect r = rect();
 
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());
 
54
    }
 
55
};
 
56
 
 
57
//======================================================
 
58
 
 
59
KexiInputTableEdit::KexiInputTableEdit(KDbTableViewColumn &column, QWidget *parent)
 
60
        : KexiTableEdit(column, parent)
 
61
{
 
62
    init();
 
63
}
 
64
 
 
65
KexiInputTableEdit::~KexiInputTableEdit()
 
66
{
 
67
}
 
68
 
 
69
void KexiInputTableEdit::init()
 
70
{
 
71
// qDebug() << "m_origValue.typeName()==" << m_origValue.typeName();
 
72
// qDebug() << "type== " << field()->typeName();
 
73
// qDebug() << "displayed type== " << displayedField()->typeName();
 
74
 
 
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)
 
81
 
 
82
    //create layer for internal editor
 
83
    QHBoxLayout *lyr =  new QHBoxLayout(this);
 
84
    lyr->setContentsMargins(0, 0, 0, 0);
 
85
 
 
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();
 
94
    if (align_right) {
 
95
        m_lineedit->setAlignment(Qt::AlignRight);
 
96
    }
 
97
 
 
98
    setViewWidget(m_lineedit);
 
99
    m_calculatedCell = false;
 
100
 
 
101
//! @todo
 
102
#if 0
 
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);
 
108
#endif
 
109
}
 
110
 
 
111
void KexiInputTableEdit::setValueInternal(const QVariant& add, bool removeOld)
 
112
{
 
113
    bool lengthExceeded;
 
114
    QString text(m_textFormatter.toString(removeOld ? QVariant() : KexiDataItemInterface::originalValue(), add.toString(),
 
115
                                          &lengthExceeded));
 
116
    if (text.isEmpty()) {
 
117
        if (KexiDataItemInterface::originalValue().toString().isEmpty()) {
 
118
            //we have to set NULL initial value:
 
119
            m_lineedit->setText(QString());
 
120
        }
 
121
    } else {
 
122
        m_lineedit->setText(text);
 
123
    }
 
124
 
 
125
//! @todo by default we're moving to the end of editor, ADD OPTION allowing "select all chars"
 
126
#if 0
 
127
    m_cview->selectAll();
 
128
#else
 
129
    m_lineedit->end(false);
 
130
#endif
 
131
    if (!m_lineedit->validator()) {
 
132
        QValidator *validator = new KDbFieldValidator(*field(), m_lineedit);
 
133
        validator->setObjectName("KexiInputTableEdit-validator");
 
134
        m_lineedit->setValidator(validator);
 
135
    }
 
136
    emitLengthExceededIfNeeded(lengthExceeded);
 
137
}
 
138
 
 
139
void KexiInputTableEdit::paintEvent(QPaintEvent * /*e*/)
 
140
{
 
141
    QPainter p(this);
 
142
    p.setPen(QPen(palette().text(), 1.0));
 
143
    p.drawRect(rect());
 
144
}
 
145
 
 
146
void
 
147
KexiInputTableEdit::setRestrictedCompletion()
 
148
{
 
149
//! @todo
 
150
#if 0
 
151
    qDebug();
 
152
    if (m_cview->text().isEmpty())
 
153
        return;
 
154
 
 
155
    qDebug() << "something to do";
 
156
    m_cview->useGlobalKeyBindings();
 
157
 
 
158
    QStringList newC;
 
159
    QStringList::ConstIterator it, end(m_comp.constEnd());
 
160
    for (it = m_comp.constBegin(); it != end; ++it) {
 
161
        if ((*it).startsWith(m_cview->text()))
 
162
            newC.append(*it);
 
163
    }
 
164
    m_cview->setCompletedItems(newC);
 
165
#endif
 
166
}
 
167
 
 
168
void
 
169
KexiInputTableEdit::completed(const QString &s)
 
170
{
 
171
// qDebug() << s;
 
172
    m_lineedit->setText(s);
 
173
}
 
174
 
 
175
bool KexiInputTableEdit::valueChanged()
 
176
{
 
177
    return KexiTableEdit::valueChanged();
 
178
}
 
179
 
 
180
bool KexiInputTableEdit::valueIsNull()
 
181
{
 
182
    return m_lineedit->text().isNull();
 
183
}
 
184
 
 
185
bool KexiInputTableEdit::valueIsEmpty()
 
186
{
 
187
    return !m_lineedit->text().isNull() && m_lineedit->text().isEmpty();
 
188
}
 
189
 
 
190
QVariant KexiInputTableEdit::value()
 
191
{
 
192
    return m_textFormatter.fromString(m_lineedit->text());
 
193
}
 
194
 
 
195
void
 
196
KexiInputTableEdit::clear()
 
197
{
 
198
    m_lineedit->clear();
 
199
}
 
200
 
 
201
bool KexiInputTableEdit::cursorAtStart()
 
202
{
 
203
    return m_lineedit->cursorPosition() == 0;
 
204
}
 
205
 
 
206
bool KexiInputTableEdit::cursorAtEnd()
 
207
{
 
208
    return KexiUtils::cursorAtEnd(m_lineedit);
 
209
}
 
210
 
 
211
QSize KexiInputTableEdit::totalSize() const
 
212
{
 
213
    if (!m_lineedit)
 
214
        return size();
 
215
    return m_lineedit->size();
 
216
}
 
217
 
 
218
void KexiInputTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
 
219
{
 
220
    Q_UNUSED(visibleValue);
 
221
//! @todo handle rich text?
 
222
    bool lengthExceeded;
 
223
    qApp->clipboard()->setText(m_textFormatter.toString(value, QString(), &lengthExceeded));
 
224
}
 
225
 
 
226
void KexiInputTableEdit::handleAction(const QString& actionName)
 
227
{
 
228
    const bool alreadyVisible = m_lineedit->isVisible();
 
229
 
 
230
    if (actionName == "edit_paste") {
 
231
        if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
 
232
            emit editRequested();
 
233
            m_lineedit->clear();
 
234
        }
 
235
        m_lineedit->paste();
 
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();
 
241
        }
 
242
        m_lineedit->cut();
 
243
    }
 
244
}
 
245
 
 
246
bool KexiInputTableEdit::showToolTipIfNeeded(const QVariant& value, const QRect& rect,
 
247
        const QFontMetrics& fm, bool focused)
 
248
{
 
249
    bool lengthExceeded;
 
250
    QString text(value.type() == QVariant::String
 
251
        ? value.toString() : m_textFormatter.toString(value, QString(), &lengthExceeded));
 
252
 
 
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();
 
258
}
 
259
 
 
260
void KexiInputTableEdit::moveCursorToEnd()
 
261
{
 
262
    m_lineedit->end(false/*!mark*/);
 
263
}
 
264
 
 
265
void KexiInputTableEdit::moveCursorToStart()
 
266
{
 
267
    m_lineedit->home(false/*!mark*/);
 
268
}
 
269
 
 
270
void KexiInputTableEdit::selectAll()
 
271
{
 
272
    m_lineedit->selectAll();
 
273
}
 
274
 
 
275
void KexiInputTableEdit::slotTextEdited(const QString& text)
 
276
{
 
277
    signalValueChanged();
 
278
    bool lengthExceeded = m_textFormatter.lengthExceeded(text);
 
279
    emitLengthExceededIfNeeded(lengthExceeded);
 
280
}
 
281
 
 
282
bool KexiInputTableEdit::fixup()
 
283
{
 
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()));
 
288
    }
 
289
    return true;
 
290
}
 
291
 
 
292
void KexiInputTableEdit::updateLineEditStyleSheet()
 
293
{
 
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; }")
 
301
      .arg(focus.name())
 
302
      .arg(m_rightMarginWhenFocused) // right
 
303
      .arg(align_right ? 0 : 2) // left
 
304
    );
 
305
    //qDebug() << m_rightMarginWhenFocused << m_lineedit->styleSheet();
 
306
}
 
307
 
 
308
KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiInputEditorFactoryItem, KexiInputTableEdit)
 
309
 
 
310
#include "kexiinputtableedit.moc"