]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
69817bef70e368d143d8736bb59b73f7eaa7a79e
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"
22 #include <kio/global.h>
25 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
) :
28 m_blockFinishedSignal(false)
30 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
31 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
32 setAcceptRichText(false);
33 enableFindReplace(false);
34 document()->setDocumentMargin(0);
37 parent
->installEventFilter(this);
40 connect(this, &KItemListRoleEditor::textChanged
, this, &KItemListRoleEditor::autoAdjustSize
);
43 KItemListRoleEditor::~KItemListRoleEditor()
47 void KItemListRoleEditor::setRole(const QByteArray
& role
)
52 QByteArray
KItemListRoleEditor::role() const
57 bool KItemListRoleEditor::eventFilter(QObject
* watched
, QEvent
* event
)
59 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
60 emitRoleEditingFinished();
63 return KTextEdit::eventFilter(watched
, event
);
66 bool KItemListRoleEditor::event(QEvent
* event
)
68 if (event
->type() == QEvent::FocusOut
) {
69 QFocusEvent
* focusEvent
= static_cast<QFocusEvent
*>(event
);
70 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
71 emitRoleEditingFinished();
74 return KTextEdit::event(event
);
77 void KItemListRoleEditor::keyPressEvent(QKeyEvent
* event
)
79 switch (event
->key()) {
81 // Emitting the signal roleEditingCanceled might result
82 // in losing the focus. Per default losing the focus emits
83 // a roleEditingFinished signal (see KItemListRoleEditor::event),
84 // which is not wanted in this case.
85 m_blockFinishedSignal
= true;
86 emit
roleEditingCanceled(m_role
, KIO::encodeFileName(toPlainText()));
87 m_blockFinishedSignal
= false;
92 emitRoleEditingFinished();
97 QTextCursor cursor
= textCursor();
98 if (event
->modifiers() == Qt::NoModifier
&& cursor
.hasSelection()) {
99 if (event
->key() == Qt::Key_Left
) {
100 cursor
.setPosition(cursor
.selectionStart());
102 cursor
.setPosition(cursor
.selectionEnd());
104 cursor
.clearSelection();
105 setTextCursor(cursor
);
115 KTextEdit::keyPressEvent(event
);
118 void KItemListRoleEditor::autoAdjustSize()
120 const qreal frameBorder
= 2 * frameWidth();
122 const qreal requiredWidth
= document()->size().width();
123 const qreal availableWidth
= size().width() - frameBorder
;
124 if (requiredWidth
> availableWidth
) {
125 qreal newWidth
= requiredWidth
+ frameBorder
;
126 if (parentWidget() && pos().x() + newWidth
> parentWidget()->width()) {
127 newWidth
= parentWidget()->width() - pos().x();
129 resize(newWidth
, size().height());
132 const qreal requiredHeight
= document()->size().height();
133 const qreal availableHeight
= size().height() - frameBorder
;
134 if (requiredHeight
> availableHeight
) {
135 qreal newHeight
= requiredHeight
+ frameBorder
;
136 if (parentWidget() && pos().y() + newHeight
> parentWidget()->height()) {
137 newHeight
= parentWidget()->height() - pos().y();
139 resize(size().width(), newHeight
);
143 void KItemListRoleEditor::emitRoleEditingFinished()
145 if (!m_blockFinishedSignal
) {
146 emit
roleEditingFinished(m_role
, KIO::encodeFileName(toPlainText()));