]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistroleeditor.cpp
4f40060109807cfe8b832fc230ff8b4d1ce799c0
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemlistroleeditor.h"
23 #include <kio/global.h>
26 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
) :
30 m_blockFinishedSignal(false)
32 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
33 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
34 setAcceptRichText(false);
35 document()->setDocumentMargin(0);
38 parent
->installEventFilter(this);
41 connect(this, SIGNAL(textChanged()), this, SLOT(autoAdjustSize()));
44 KItemListRoleEditor::~KItemListRoleEditor()
48 void KItemListRoleEditor::setIndex(int index
)
53 int KItemListRoleEditor::index() const
58 void KItemListRoleEditor::setRole(const QByteArray
& role
)
63 QByteArray
KItemListRoleEditor::role() const
68 bool KItemListRoleEditor::eventFilter(QObject
* watched
, QEvent
* event
)
70 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
71 emitRoleEditingFinished();
74 return KTextEdit::eventFilter(watched
, event
);
77 bool KItemListRoleEditor::event(QEvent
* event
)
79 if (event
->type() == QEvent::FocusOut
) {
80 QFocusEvent
* focusEvent
= static_cast<QFocusEvent
*>(event
);
81 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
82 emitRoleEditingFinished();
85 return KTextEdit::event(event
);
88 void KItemListRoleEditor::keyPressEvent(QKeyEvent
* event
)
90 switch (event
->key()) {
92 // Emitting the signal roleEditingCanceled might result
93 // in losing the focus. Per default losing the focus emits
94 // a roleEditingFinished signal (see KItemListRoleEditor::event),
95 // which is not wanted in this case.
96 m_blockFinishedSignal
= true;
97 emit
roleEditingCanceled(m_index
, m_role
, KIO::encodeFileName(toPlainText()));
98 m_blockFinishedSignal
= false;
103 emitRoleEditingFinished();
110 KTextEdit::keyPressEvent(event
);
113 void KItemListRoleEditor::autoAdjustSize()
115 const qreal frameBorder
= 2 * frameWidth();
117 const qreal requiredWidth
= document()->size().width();
118 const qreal availableWidth
= size().width() - frameBorder
;
119 if (requiredWidth
> availableWidth
) {
120 qreal newWidth
= requiredWidth
+ frameBorder
;
121 if (parentWidget() && pos().x() + newWidth
> parentWidget()->width()) {
122 newWidth
= parentWidget()->width() - pos().x();
124 resize(newWidth
, size().height());
127 const qreal requiredHeight
= document()->size().height();
128 const qreal availableHeight
= size().height() - frameBorder
;
129 if (requiredHeight
> availableHeight
) {
130 qreal newHeight
= requiredHeight
+ frameBorder
;
131 if (parentWidget() && pos().y() + newHeight
> parentWidget()->height()) {
132 newHeight
= parentWidget()->height() - pos().y();
134 resize(size().width(), newHeight
);
138 void KItemListRoleEditor::emitRoleEditingFinished()
140 if (!m_blockFinishedSignal
) {
141 emit
roleEditingFinished(m_index
, m_role
, KIO::encodeFileName(toPlainText()));
145 #include "kitemlistroleeditor.moc"