1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemlistroleeditor.h"
25 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
) :
29 m_blockFinishedSignal(false),
30 m_eventHandlingLevel(0),
31 m_deleteAfterEventHandling(false)
33 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
34 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
35 setAcceptRichText(false);
36 document()->setDocumentMargin(0);
39 parent
->installEventFilter(this);
42 connect(this, SIGNAL(textChanged()), this, SLOT(autoAdjustSize()));
45 KItemListRoleEditor::~KItemListRoleEditor()
49 void KItemListRoleEditor::setIndex(int index
)
54 int KItemListRoleEditor::index() const
59 void KItemListRoleEditor::setRole(const QByteArray
& role
)
64 QByteArray
KItemListRoleEditor::role() const
69 void KItemListRoleEditor::deleteWhenIdle()
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;
83 bool KItemListRoleEditor::eventFilter(QObject
* watched
, QEvent
* event
)
85 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
86 emitRoleEditingFinished();
89 return KTextEdit::eventFilter(watched
, event
);
92 bool KItemListRoleEditor::event(QEvent
* event
)
94 ++m_eventHandlingLevel
;
96 if (event
->type() == QEvent::FocusOut
) {
97 QFocusEvent
* focusEvent
= static_cast<QFocusEvent
*>(event
);
98 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
99 emitRoleEditingFinished();
103 const int result
= KTextEdit::event(event
);
104 --m_eventHandlingLevel
;
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.
110 m_deleteAfterEventHandling
= false;
116 bool KItemListRoleEditor::viewportEvent(QEvent
* event
)
118 ++m_eventHandlingLevel
;
119 const bool result
= KTextEdit::viewportEvent(event
);
120 --m_eventHandlingLevel
;
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.
126 m_deleteAfterEventHandling
= false;
132 void KItemListRoleEditor::keyPressEvent(QKeyEvent
* event
)
134 switch (event
->key()) {
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;
147 // TODO: find a better way to fix the bug 309760
148 clearFocus(); // emitRoleEditingFinished(); results in a crash
155 KTextEdit::keyPressEvent(event
);
158 void KItemListRoleEditor::autoAdjustSize()
160 const qreal frameBorder
= 2 * frameWidth();
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();
169 resize(newWidth
, size().height());
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();
179 resize(size().width(), newHeight
);
183 void KItemListRoleEditor::emitRoleEditingFinished()
185 if (!m_blockFinishedSignal
) {
186 emit
roleEditingFinished(m_index
, m_role
, toPlainText());
190 #include "kitemlistroleeditor.moc"