diff options
| author | Peter Penz <[email protected]> | 2012-04-17 18:13:31 +0200 |
|---|---|---|
| committer | Peter Penz <[email protected]> | 2012-04-17 18:15:12 +0200 |
| commit | b2e54c3a316b502ab4f7a95250f8316dc591c057 (patch) | |
| tree | 227eda97fcf699633f49fa7ea6ccdf38a19626e7 /src/kitemviews/private/kitemlistroleeditor.cpp | |
| parent | eb1b53103d67784c68bb33e5fe3fefcad4cdbdea (diff) | |
Implement inline-renaming for the new view-engine
BUG: 286893
FIXED-IN: 4.9.0
Diffstat (limited to 'src/kitemviews/private/kitemlistroleeditor.cpp')
| -rw-r--r-- | src/kitemviews/private/kitemlistroleeditor.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/kitemviews/private/kitemlistroleeditor.cpp b/src/kitemviews/private/kitemlistroleeditor.cpp new file mode 100644 index 000000000..55af6a9c7 --- /dev/null +++ b/src/kitemviews/private/kitemlistroleeditor.cpp @@ -0,0 +1,113 @@ +/*************************************************************************** + * Copyright (C) 2012 by Peter Penz <[email protected]> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "kitemlistroleeditor.h" + +#include <KDebug> +#include <QKeyEvent> + +KItemListRoleEditor::KItemListRoleEditor(QWidget *parent) : + KTextEdit(parent), + m_index(0), + m_role() +{ + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + document()->setDocumentMargin(0); + + if (parent) { + parent->installEventFilter(this); + } +} + +KItemListRoleEditor::~KItemListRoleEditor() +{ +} + +void KItemListRoleEditor::setIndex(int index) +{ + m_index = index; +} + +int KItemListRoleEditor::index() const +{ + return m_index; +} + +void KItemListRoleEditor::setRole(const QByteArray& role) +{ + m_role = role; +} + +QByteArray KItemListRoleEditor::role() const +{ + return m_role; +} + +bool KItemListRoleEditor::eventFilter(QObject* watched, QEvent* event) +{ + if (watched == parentWidget() && event->type() == QEvent::Resize) { + autoAdjustSize(); + } + + return KTextEdit::eventFilter(watched, event); +} + +bool KItemListRoleEditor::event(QEvent* event) +{ + if (event->type() == QEvent::FocusOut) { + emit roleEditingFinished(m_index, m_role, toPlainText()); + } + return KTextEdit::event(event); +} + +void KItemListRoleEditor::keyPressEvent(QKeyEvent* event) +{ + switch (event->key()) { + case Qt::Key_Escape: + emit roleEditingCanceled(m_index, m_role, toPlainText()); + event->accept(); + return; + case Qt::Key_Enter: + case Qt::Key_Return: + emit roleEditingFinished(m_index, m_role, toPlainText()); + event->accept(); + return; + default: + break; + } + + KTextEdit::keyPressEvent(event); + autoAdjustSize(); +} + +void KItemListRoleEditor::autoAdjustSize() +{ + const qreal requiredWidth = document()->size().width(); + const qreal availableWidth = size().width() - 2 * frameWidth(); + if (requiredWidth > availableWidth) { + qreal newWidth = requiredWidth + 2 * frameWidth(); + if (parentWidget() && pos().x() + newWidth > parentWidget()->width()) { + newWidth = parentWidget()->width() - pos().x(); + } + resize(newWidth, size().height()); + } +} + +#include "kitemlistroleeditor.moc" |
