]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
[Inline Rename] Move cursor to correct position on pressing Home and End
[dolphin.git] / src / kitemviews / private / kitemlistroleeditor.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistroleeditor.h"
21
22 #include <KIO/Global>
23
24 KItemListRoleEditor::KItemListRoleEditor(QWidget *parent) :
25 KTextEdit(parent),
26 m_role(),
27 m_blockFinishedSignal(false)
28 {
29 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
30 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 setAcceptRichText(false);
32 enableFindReplace(false);
33 document()->setDocumentMargin(0);
34
35 if (parent) {
36 parent->installEventFilter(this);
37 }
38
39 connect(this, &KItemListRoleEditor::textChanged, this, &KItemListRoleEditor::autoAdjustSize);
40 }
41
42 KItemListRoleEditor::~KItemListRoleEditor()
43 {
44 }
45
46 void KItemListRoleEditor::setRole(const QByteArray& role)
47 {
48 m_role = role;
49 }
50
51 QByteArray KItemListRoleEditor::role() const
52 {
53 return m_role;
54 }
55
56 bool KItemListRoleEditor::eventFilter(QObject* watched, QEvent* event)
57 {
58 if (watched == parentWidget() && event->type() == QEvent::Resize) {
59 emitRoleEditingFinished();
60 }
61
62 return KTextEdit::eventFilter(watched, event);
63 }
64
65 bool KItemListRoleEditor::event(QEvent* event)
66 {
67 if (event->type() == QEvent::FocusOut) {
68 QFocusEvent* focusEvent = static_cast<QFocusEvent*>(event);
69 if (focusEvent->reason() != Qt::PopupFocusReason) {
70 emitRoleEditingFinished();
71 }
72 }
73 return KTextEdit::event(event);
74 }
75
76 void KItemListRoleEditor::keyPressEvent(QKeyEvent* event)
77 {
78 switch (event->key()) {
79 case Qt::Key_Escape:
80 // Emitting the signal roleEditingCanceled might result
81 // in losing the focus. Per default losing the focus emits
82 // a roleEditingFinished signal (see KItemListRoleEditor::event),
83 // which is not wanted in this case.
84 m_blockFinishedSignal = true;
85 emit roleEditingCanceled(m_role, KIO::encodeFileName(toPlainText()));
86 m_blockFinishedSignal = false;
87 event->accept();
88 return;
89 case Qt::Key_Enter:
90 case Qt::Key_Return:
91 emitRoleEditingFinished();
92 event->accept();
93 return;
94 case Qt::Key_Left:
95 case Qt::Key_Right: {
96 QTextCursor cursor = textCursor();
97 if (event->modifiers() == Qt::NoModifier && cursor.hasSelection()) {
98 if (event->key() == Qt::Key_Left) {
99 cursor.setPosition(cursor.selectionStart());
100 } else {
101 cursor.setPosition(cursor.selectionEnd());
102 }
103 cursor.clearSelection();
104 setTextCursor(cursor);
105 event->accept();
106 return;
107 }
108 break;
109 }
110 case Qt::Key_Home:
111 case Qt::Key_End: {
112 if (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::ShiftModifier) {
113 const QTextCursor::MoveOperation op = event->key() == Qt::Key_Home
114 ? QTextCursor::Start
115 : QTextCursor::End;
116 const QTextCursor::MoveMode mode = event->modifiers() == Qt::NoModifier
117 ? QTextCursor::MoveAnchor
118 : QTextCursor::KeepAnchor;
119 QTextCursor cursor = textCursor();
120 cursor.movePosition(op, mode);
121 setTextCursor(cursor);
122 event->accept();
123 return;
124 }
125 break;
126 }
127 default:
128 break;
129 }
130
131 KTextEdit::keyPressEvent(event);
132 }
133
134 void KItemListRoleEditor::autoAdjustSize()
135 {
136 const qreal frameBorder = 2 * frameWidth();
137
138 const qreal requiredWidth = document()->size().width();
139 const qreal availableWidth = size().width() - frameBorder;
140 if (requiredWidth > availableWidth) {
141 qreal newWidth = requiredWidth + frameBorder;
142 if (parentWidget() && pos().x() + newWidth > parentWidget()->width()) {
143 newWidth = parentWidget()->width() - pos().x();
144 }
145 resize(newWidth, size().height());
146 }
147
148 const qreal requiredHeight = document()->size().height();
149 const qreal availableHeight = size().height() - frameBorder;
150 if (requiredHeight > availableHeight) {
151 qreal newHeight = requiredHeight + frameBorder;
152 if (parentWidget() && pos().y() + newHeight > parentWidget()->height()) {
153 newHeight = parentWidget()->height() - pos().y();
154 }
155 resize(size().width(), newHeight);
156 }
157 }
158
159 void KItemListRoleEditor::emitRoleEditingFinished()
160 {
161 if (!m_blockFinishedSignal) {
162 emit roleEditingFinished(m_role, KIO::encodeFileName(toPlainText()));
163 }
164 }
165