]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[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_index(0),
29 m_role(),
30 m_blockFinishedSignal(false)
31 {
32 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
33 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
34 setAcceptRichText(false);
35 enableFindReplace(false);
36 document()->setDocumentMargin(0);
37
38 if (parent) {
39 parent->installEventFilter(this);
40 }
41
42 connect(this, SIGNAL(textChanged()), this, SLOT(autoAdjustSize()));
43 }
44
45 KItemListRoleEditor::~KItemListRoleEditor()
46 {
47 }
48
49 void KItemListRoleEditor::setIndex(int index)
50 {
51 m_index = index;
52 }
53
54 int KItemListRoleEditor::index() const
55 {
56 return m_index;
57 }
58
59 void KItemListRoleEditor::setRole(const QByteArray& role)
60 {
61 m_role = role;
62 }
63
64 QByteArray KItemListRoleEditor::role() const
65 {
66 return m_role;
67 }
68
69 bool KItemListRoleEditor::eventFilter(QObject* watched, QEvent* event)
70 {
71 if (watched == parentWidget() && event->type() == QEvent::Resize) {
72 emitRoleEditingFinished();
73 }
74
75 return KTextEdit::eventFilter(watched, event);
76 }
77
78 bool KItemListRoleEditor::event(QEvent* event)
79 {
80 if (event->type() == QEvent::FocusOut) {
81 QFocusEvent* focusEvent = static_cast<QFocusEvent*>(event);
82 if (focusEvent->reason() != Qt::PopupFocusReason) {
83 emitRoleEditingFinished();
84 }
85 }
86 return KTextEdit::event(event);
87 }
88
89 void KItemListRoleEditor::keyPressEvent(QKeyEvent* event)
90 {
91 switch (event->key()) {
92 case Qt::Key_Escape:
93 // Emitting the signal roleEditingCanceled might result
94 // in losing the focus. Per default losing the focus emits
95 // a roleEditingFinished signal (see KItemListRoleEditor::event),
96 // which is not wanted in this case.
97 m_blockFinishedSignal = true;
98 emit roleEditingCanceled(m_index, m_role, KIO::encodeFileName(toPlainText()));
99 m_blockFinishedSignal = false;
100 event->accept();
101 return;
102 case Qt::Key_Enter:
103 case Qt::Key_Return:
104 emitRoleEditingFinished();
105 event->accept();
106 return;
107 default:
108 break;
109 }
110
111 KTextEdit::keyPressEvent(event);
112 }
113
114 void KItemListRoleEditor::autoAdjustSize()
115 {
116 const qreal frameBorder = 2 * frameWidth();
117
118 const qreal requiredWidth = document()->size().width();
119 const qreal availableWidth = size().width() - frameBorder;
120 if (requiredWidth > availableWidth) {
121 qreal newWidth = requiredWidth + frameBorder;
122 if (parentWidget() && pos().x() + newWidth > parentWidget()->width()) {
123 newWidth = parentWidget()->width() - pos().x();
124 }
125 resize(newWidth, size().height());
126 }
127
128 const qreal requiredHeight = document()->size().height();
129 const qreal availableHeight = size().height() - frameBorder;
130 if (requiredHeight > availableHeight) {
131 qreal newHeight = requiredHeight + frameBorder;
132 if (parentWidget() && pos().y() + newHeight > parentWidget()->height()) {
133 newHeight = parentWidget()->height() - pos().y();
134 }
135 resize(size().width(), newHeight);
136 }
137 }
138
139 void KItemListRoleEditor::emitRoleEditingFinished()
140 {
141 if (!m_blockFinishedSignal) {
142 emit roleEditingFinished(m_index, m_role, KIO::encodeFileName(toPlainText()));
143 }
144 }
145
146 #include "kitemlistroleeditor.moc"