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