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