2
* Copyright © 2009 Fredrik Höglund <fredrik@kde.org>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "itemeditor.h"
23
#include <QAbstractItemView>
24
#include <QAbstractItemModel>
25
#include <QAbstractItemDelegate>
30
ItemEditor::ItemEditor(QGraphicsWidget *parent, const QStyleOptionViewItemV4 &option,
31
const QModelIndex &index)
32
: QGraphicsProxyWidget(parent),
36
m_editor = new KTextEdit();
37
m_editor->setAttribute(Qt::WA_NoSystemBackground);
38
m_editor->setAcceptRichText(false);
39
m_editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40
m_editor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41
m_editor->setAlignment(option.displayAlignment);
42
m_editor->installEventFilter(this);
44
// Set the editor data
45
const QVariant value = index.data(Qt::EditRole);
46
const QString text = value.toString();
47
m_editor->insertPlainText(text);
48
m_editor->selectAll();
50
const QString extension = KMimeType::extractKnownExtension(text);
51
if (!extension.isEmpty()) {
52
// The filename contains an extension. Assure that only the filename
54
const int selectionLength = text.length() - extension.length() - 1;
55
QTextCursor cursor = m_editor->textCursor();
56
cursor.movePosition(QTextCursor::StartOfBlock);
57
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, selectionLength);
58
m_editor->setTextCursor(cursor);
64
ItemEditor::~ItemEditor()
68
void ItemEditor::commitData()
70
const_cast<QAbstractItemModel*>(m_index.model())->setData(m_index, m_editor->toPlainText(), Qt::EditRole);
73
bool ItemEditor::eventFilter(QObject *watched, QEvent *event)
75
KTextEdit *editor = qobject_cast<KTextEdit*>(watched);
79
switch (event->type())
81
case QEvent::KeyPress:
83
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
84
switch (keyEvent->key())
89
emit closeEditor(this, QAbstractItemDelegate::NoHint);
94
if (!editor->toPlainText().isEmpty()) {
96
emit closeEditor(this, QAbstractItemDelegate::SubmitModelCache);
101
emit closeEditor(this, QAbstractItemDelegate::RevertModelCache);
106
} // switch (keyEvent->key())
107
} // case QEvent::KeyPress
109
case QEvent::FocusOut:
112
emit closeEditor(this, QAbstractItemDelegate::NoHint);
118
} // switch (event->type())
121
#include "itemeditor.moc"