]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
Merge branch 'Applications/18.04'
[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 <KIO/Global>
23
24 KItemListRoleEditor::KItemListRoleEditor(QWidget *parent) :
25 KTextEdit(parent),
26 m_role(),
27 m_blockFinishedSignal(false)
28 {
29 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
30 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 setAcceptRichText(false);
32 enableFindReplace(false);
33 document()->setDocumentMargin(0);
34
35 if (parent) {
36 parent->installEventFilter(this);
37 }
38
39 connect(this, &KItemListRoleEditor::textChanged, this, &KItemListRoleEditor::autoAdjustSize);
40 }
41
42 KItemListRoleEditor::~KItemListRoleEditor()
43 {
44 }
45
46 void KItemListRoleEditor::setRole(const QByteArray& role)
47 {
48 m_role = role;
49 }
50
51 QByteArray KItemListRoleEditor::role() const
52 {
53 return m_role;
54 }
55
56 bool KItemListRoleEditor::eventFilter(QObject* watched, QEvent* event)
57 {
58 if (watched == parentWidget() && event->type() == QEvent::Resize) {
59 emitRoleEditingFinished();
60 }
61
62 return KTextEdit::eventFilter(watched, event);
63 }
64
65 bool KItemListRoleEditor::event(QEvent* event)
66 {
67 if (event->type() == QEvent::FocusOut) {
68 QFocusEvent* focusEvent = static_cast<QFocusEvent*>(event);
69 if (focusEvent->reason() != Qt::PopupFocusReason) {
70 emitRoleEditingFinished();
71 }
72 }
73 return KTextEdit::event(event);
74 }
75
76 void KItemListRoleEditor::keyPressEvent(QKeyEvent* event)
77 {
78 switch (event->key()) {
79 case Qt::Key_Escape:
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;
87 event->accept();
88 return;
89 case Qt::Key_Enter:
90 case Qt::Key_Return:
91 emitRoleEditingFinished();
92 event->accept();
93 return;
94 case Qt::Key_Left:
95 case Qt::Key_Right: {
96 QTextCursor cursor = textCursor();
97 if (event->modifiers() == Qt::NoModifier && cursor.hasSelection()) {
98 if (event->key() == Qt::Key_Left) {
99 cursor.setPosition(cursor.selectionStart());
100 } else {
101 cursor.setPosition(cursor.selectionEnd());
102 }
103 cursor.clearSelection();
104 setTextCursor(cursor);
105 event->accept();
106 return;
107 }
108 break;
109 }
110 default:
111 break;
112 }
113
114 KTextEdit::keyPressEvent(event);
115 }
116
117 void KItemListRoleEditor::autoAdjustSize()
118 {
119 const qreal frameBorder = 2 * frameWidth();
120
121 const qreal requiredWidth = document()->size().width();
122 const qreal availableWidth = size().width() - frameBorder;
123 if (requiredWidth > availableWidth) {
124 qreal newWidth = requiredWidth + frameBorder;
125 if (parentWidget() && pos().x() + newWidth > parentWidget()->width()) {
126 newWidth = parentWidget()->width() - pos().x();
127 }
128 resize(newWidth, size().height());
129 }
130
131 const qreal requiredHeight = document()->size().height();
132 const qreal availableHeight = size().height() - frameBorder;
133 if (requiredHeight > availableHeight) {
134 qreal newHeight = requiredHeight + frameBorder;
135 if (parentWidget() && pos().y() + newHeight > parentWidget()->height()) {
136 newHeight = parentWidget()->height() - pos().y();
137 }
138 resize(size().width(), newHeight);
139 }
140 }
141
142 void KItemListRoleEditor::emitRoleEditingFinished()
143 {
144 if (!m_blockFinishedSignal) {
145 emit roleEditingFinished(m_role, KIO::encodeFileName(toPlainText()));
146 }
147 }
148