]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
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"
23 #include <kio/global.h>
26 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
) :
29 m_blockFinishedSignal(false)
31 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
32 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
33 setAcceptRichText(false);
34 enableFindReplace(false);
35 document()->setDocumentMargin(0);
38 parent
->installEventFilter(this);
41 connect(this, SIGNAL(textChanged()), this, SLOT(autoAdjustSize()));
44 KItemListRoleEditor::~KItemListRoleEditor()
48 void KItemListRoleEditor::setRole(const QByteArray
& role
)
53 QByteArray
KItemListRoleEditor::role() const
58 bool KItemListRoleEditor::eventFilter(QObject
* watched
, QEvent
* event
)
60 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
61 emitRoleEditingFinished();
64 return KTextEdit::eventFilter(watched
, event
);
67 bool KItemListRoleEditor::event(QEvent
* event
)
69 if (event
->type() == QEvent::FocusOut
) {
70 QFocusEvent
* focusEvent
= static_cast<QFocusEvent
*>(event
);
71 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
72 emitRoleEditingFinished();
75 return KTextEdit::event(event
);
78 void KItemListRoleEditor::keyPressEvent(QKeyEvent
* event
)
80 switch (event
->key()) {
82 // Emitting the signal roleEditingCanceled might result
83 // in losing the focus. Per default losing the focus emits
84 // a roleEditingFinished signal (see KItemListRoleEditor::event),
85 // which is not wanted in this case.
86 m_blockFinishedSignal
= true;
87 emit
roleEditingCanceled(m_role
, KIO::encodeFileName(toPlainText()));
88 m_blockFinishedSignal
= false;
93 emitRoleEditingFinished();
100 KTextEdit::keyPressEvent(event
);
103 void KItemListRoleEditor::autoAdjustSize()
105 const qreal frameBorder
= 2 * frameWidth();
107 const qreal requiredWidth
= document()->size().width();
108 const qreal availableWidth
= size().width() - frameBorder
;
109 if (requiredWidth
> availableWidth
) {
110 qreal newWidth
= requiredWidth
+ frameBorder
;
111 if (parentWidget() && pos().x() + newWidth
> parentWidget()->width()) {
112 newWidth
= parentWidget()->width() - pos().x();
114 resize(newWidth
, size().height());
117 const qreal requiredHeight
= document()->size().height();
118 const qreal availableHeight
= size().height() - frameBorder
;
119 if (requiredHeight
> availableHeight
) {
120 qreal newHeight
= requiredHeight
+ frameBorder
;
121 if (parentWidget() && pos().y() + newHeight
> parentWidget()->height()) {
122 newHeight
= parentWidget()->height() - pos().y();
124 resize(size().width(), newHeight
);
128 void KItemListRoleEditor::emitRoleEditingFinished()
130 if (!m_blockFinishedSignal
) {
131 emit
roleEditingFinished(m_role
, KIO::encodeFileName(toPlainText()));
135 #include "kitemlistroleeditor.moc"