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