]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
Fix Bug 309760 - Crash while inline-renaming a file and apply change with return-key
[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 <KDebug>
23 #include <QKeyEvent>
24
25 KItemListRoleEditor::KItemListRoleEditor(QWidget *parent) :
26 KTextEdit(parent),
27 m_index(0),
28 m_role(),
29 m_blockFinishedSignal(false),
30 m_eventHandlingLevel(0),
31 m_deleteAfterEventHandling(false)
32 {
33 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
34 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35 setAcceptRichText(false);
36 document()->setDocumentMargin(0);
37
38 if (parent) {
39 parent->installEventFilter(this);
40 }
41
42 connect(this, SIGNAL(textChanged()), this, SLOT(autoAdjustSize()));
43 }
44
45 KItemListRoleEditor::~KItemListRoleEditor()
46 {
47 }
48
49 void KItemListRoleEditor::setIndex(int index)
50 {
51 m_index = index;
52 }
53
54 int KItemListRoleEditor::index() const
55 {
56 return m_index;
57 }
58
59 void KItemListRoleEditor::setRole(const QByteArray& role)
60 {
61 m_role = role;
62 }
63
64 QByteArray KItemListRoleEditor::role() const
65 {
66 return m_role;
67 }
68
69 void KItemListRoleEditor::deleteWhenIdle()
70 {
71 if (m_eventHandlingLevel > 0) {
72 // We are handling an event at the moment. It could be that we
73 // are in a nested event loop run by contextMenuEvent() or a
74 // call of mousePressEvent() which results in drag&drop.
75 // -> do not call deleteLater() to prevent a crash when we
76 // return from the nested event loop.
77 m_deleteAfterEventHandling = true;
78 } else {
79 deleteLater();
80 }
81 }
82
83 bool KItemListRoleEditor::eventFilter(QObject* watched, QEvent* event)
84 {
85 if (watched == parentWidget() && event->type() == QEvent::Resize) {
86 emitRoleEditingFinished();
87 }
88
89 return KTextEdit::eventFilter(watched, event);
90 }
91
92 bool KItemListRoleEditor::event(QEvent* event)
93 {
94 ++m_eventHandlingLevel;
95
96 if (event->type() == QEvent::FocusOut) {
97 QFocusEvent* focusEvent = static_cast<QFocusEvent*>(event);
98 if (focusEvent->reason() != Qt::PopupFocusReason) {
99 emitRoleEditingFinished();
100 }
101 }
102
103 const int result = KTextEdit::event(event);
104 --m_eventHandlingLevel;
105
106 if (m_deleteAfterEventHandling && m_eventHandlingLevel == 0) {
107 // Schedule this object for deletion and make sure that we do not try
108 // to deleteLater() again when the DeferredDelete event is received.
109 deleteLater();
110 m_deleteAfterEventHandling = false;
111 }
112
113 return result;
114 }
115
116 bool KItemListRoleEditor::viewportEvent(QEvent* event)
117 {
118 ++m_eventHandlingLevel;
119 const bool result = KTextEdit::viewportEvent(event);
120 --m_eventHandlingLevel;
121
122 if (m_deleteAfterEventHandling && m_eventHandlingLevel == 0) {
123 // Schedule this object for deletion and make sure that we do not try
124 // to deleteLater() again when the DeferredDelete event is received.
125 deleteLater();
126 m_deleteAfterEventHandling = false;
127 }
128
129 return result;
130 }
131
132 void KItemListRoleEditor::keyPressEvent(QKeyEvent* event)
133 {
134 switch (event->key()) {
135 case Qt::Key_Escape:
136 // Emitting the signal roleEditingCanceled might result
137 // in losing the focus. Per default losing the focus emits
138 // a roleEditingFinished signal (see KItemListRoleEditor::event),
139 // which is not wanted in this case.
140 m_blockFinishedSignal = true;
141 emit roleEditingCanceled(m_index, m_role, toPlainText());
142 m_blockFinishedSignal = false;
143 event->accept();
144 return;
145 case Qt::Key_Enter:
146 case Qt::Key_Return:
147 // TODO: find a better way to fix the bug 309760
148 clearFocus(); // emitRoleEditingFinished(); results in a crash
149 event->accept();
150 return;
151 default:
152 break;
153 }
154
155 KTextEdit::keyPressEvent(event);
156 }
157
158 void KItemListRoleEditor::autoAdjustSize()
159 {
160 const qreal frameBorder = 2 * frameWidth();
161
162 const qreal requiredWidth = document()->size().width();
163 const qreal availableWidth = size().width() - frameBorder;
164 if (requiredWidth > availableWidth) {
165 qreal newWidth = requiredWidth + frameBorder;
166 if (parentWidget() && pos().x() + newWidth > parentWidget()->width()) {
167 newWidth = parentWidget()->width() - pos().x();
168 }
169 resize(newWidth, size().height());
170 }
171
172 const qreal requiredHeight = document()->size().height();
173 const qreal availableHeight = size().height() - frameBorder;
174 if (requiredHeight > availableHeight) {
175 qreal newHeight = requiredHeight + frameBorder;
176 if (parentWidget() && pos().y() + newHeight > parentWidget()->height()) {
177 newHeight = parentWidget()->height() - pos().y();
178 }
179 resize(size().width(), newHeight);
180 }
181 }
182
183 void KItemListRoleEditor::emitRoleEditingFinished()
184 {
185 if (!m_blockFinishedSignal) {
186 emit roleEditingFinished(m_index, m_role, toPlainText());
187 }
188 }
189
190 #include "kitemlistroleeditor.moc"