]>
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"
24 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
) :
27 m_blockFinishedSignal(false)
29 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
30 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
31 setAcceptRichText(false);
32 enableFindReplace(false);
33 document()->setDocumentMargin(0);
36 parent
->installEventFilter(this);
39 connect(this, &KItemListRoleEditor::textChanged
, this, &KItemListRoleEditor::autoAdjustSize
);
42 KItemListRoleEditor::~KItemListRoleEditor()
46 void KItemListRoleEditor::setRole(const QByteArray
& role
)
51 QByteArray
KItemListRoleEditor::role() const
56 bool KItemListRoleEditor::eventFilter(QObject
* watched
, QEvent
* event
)
58 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
59 emitRoleEditingFinished();
62 return KTextEdit::eventFilter(watched
, event
);
65 bool KItemListRoleEditor::event(QEvent
* event
)
67 if (event
->type() == QEvent::FocusOut
) {
68 QFocusEvent
* focusEvent
= static_cast<QFocusEvent
*>(event
);
69 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
70 emitRoleEditingFinished();
73 return KTextEdit::event(event
);
76 void KItemListRoleEditor::keyPressEvent(QKeyEvent
* event
)
78 switch (event
->key()) {
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;
91 emitRoleEditingFinished();
96 QTextCursor cursor
= textCursor();
97 if (event
->modifiers() == Qt::NoModifier
&& cursor
.hasSelection()) {
98 if (event
->key() == Qt::Key_Left
) {
99 cursor
.setPosition(cursor
.selectionStart());
101 cursor
.setPosition(cursor
.selectionEnd());
103 cursor
.clearSelection();
104 setTextCursor(cursor
);
112 if (event
->modifiers() == Qt::NoModifier
|| event
->modifiers() == Qt::ShiftModifier
) {
113 const QTextCursor::MoveOperation op
= event
->key() == Qt::Key_Home
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
);
131 KTextEdit::keyPressEvent(event
);
134 void KItemListRoleEditor::autoAdjustSize()
136 const qreal frameBorder
= 2 * frameWidth();
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();
145 resize(newWidth
, size().height());
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();
155 resize(size().width(), newHeight
);
159 void KItemListRoleEditor::emitRoleEditingFinished()
161 if (!m_blockFinishedSignal
) {
162 emit
roleEditingFinished(m_role
, KIO::encodeFileName(toPlainText()));