2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemlistroleeditor.h"
11 KItemListRoleEditor::KItemListRoleEditor(QWidget
*parent
)
14 , m_blockFinishedSignal(false)
16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
18 setAcceptRichText(false);
19 enableFindReplace(false);
20 document()->setDocumentMargin(0);
21 setCheckSpellingEnabled(false);
24 parent
->installEventFilter(this);
27 connect(this, &KItemListRoleEditor::textChanged
, this, &KItemListRoleEditor::autoAdjustSize
);
30 KItemListRoleEditor::~KItemListRoleEditor()
34 void KItemListRoleEditor::setRole(const QByteArray
&role
)
39 QByteArray
KItemListRoleEditor::role() const
44 void KItemListRoleEditor::setAllowUpDownKeyChainEdit(bool allowChainEdit
)
46 m_allowUpDownKeyChainEdit
= allowChainEdit
;
49 bool KItemListRoleEditor::eventFilter(QObject
*watched
, QEvent
*event
)
51 if (watched
== parentWidget() && event
->type() == QEvent::Resize
) {
52 emitRoleEditingFinished();
55 return KTextEdit::eventFilter(watched
, event
);
58 bool KItemListRoleEditor::event(QEvent
*event
)
60 if (event
->type() == QEvent::FocusOut
) {
61 QFocusEvent
*focusEvent
= static_cast<QFocusEvent
*>(event
);
62 if (focusEvent
->reason() != Qt::PopupFocusReason
) {
63 emitRoleEditingFinished();
66 return KTextEdit::event(event
);
69 void KItemListRoleEditor::keyPressEvent(QKeyEvent
*event
)
71 switch (event
->key()) {
73 // Emitting the signal roleEditingCanceled might result
74 // in losing the focus. Per default losing the focus emits
75 // a roleEditingFinished signal (see KItemListRoleEditor::event),
76 // which is not wanted in this case.
77 m_blockFinishedSignal
= true;
78 Q_EMIT
roleEditingCanceled(m_role
, KIO::encodeFileName(toPlainText()));
79 m_blockFinishedSignal
= false;
84 emitRoleEditingFinished();
89 if (m_allowUpDownKeyChainEdit
|| event
->key() == Qt::Key_Tab
) {
90 emitRoleEditingFinished(EditNext
);
97 if (m_allowUpDownKeyChainEdit
|| event
->key() == Qt::Key_Backtab
) {
98 emitRoleEditingFinished(EditPrevious
);
104 case Qt::Key_Right
: {
105 QTextCursor cursor
= textCursor();
106 if (event
->modifiers() == Qt::NoModifier
&& cursor
.hasSelection()) {
107 if (event
->key() == Qt::Key_Left
) {
108 cursor
.setPosition(cursor
.selectionStart());
110 cursor
.setPosition(cursor
.selectionEnd());
112 cursor
.clearSelection();
113 setTextCursor(cursor
);
121 if (event
->modifiers() == Qt::NoModifier
|| event
->modifiers() == Qt::ShiftModifier
) {
122 const QTextCursor::MoveOperation op
= event
->key() == Qt::Key_Home
? QTextCursor::Start
: QTextCursor::End
;
123 const QTextCursor::MoveMode mode
= event
->modifiers() == Qt::NoModifier
? QTextCursor::MoveAnchor
: QTextCursor::KeepAnchor
;
124 QTextCursor cursor
= textCursor();
125 cursor
.movePosition(op
, mode
);
126 setTextCursor(cursor
);
136 KTextEdit::keyPressEvent(event
);
139 void KItemListRoleEditor::autoAdjustSize()
141 const qreal frameBorder
= 2 * frameWidth();
143 const auto originalSize
= size();
144 auto newSize
= originalSize
;
146 document()->adjustSize();
147 const qreal requiredWidth
= document()->size().width();
148 const qreal availableWidth
= size().width() - frameBorder
;
149 if (requiredWidth
> availableWidth
) {
150 qreal newWidth
= requiredWidth
+ frameBorder
;
151 if (parentWidget() && pos().x() + newWidth
> parentWidget()->width()) {
152 newWidth
= parentWidget()->width() - pos().x();
154 newSize
.setWidth(newWidth
);
157 const qreal requiredHeight
= document()->size().height();
158 const qreal availableHeight
= size().height() - frameBorder
;
159 if (requiredHeight
> availableHeight
) {
160 qreal newHeight
= requiredHeight
+ frameBorder
;
161 if (parentWidget() && pos().y() + newHeight
> parentWidget()->height()) {
162 newHeight
= parentWidget()->height() - pos().y();
164 newSize
.setHeight(newHeight
);
167 if (originalSize
!= newSize
) {
170 // reset the document width to the widget width
171 // to allow alignment to be properly rendered
172 document()->setTextWidth(newSize
.width());
175 void KItemListRoleEditor::emitRoleEditingFinished(EditResultDirection direction
)
178 ret
.setValue(EditResult
{KIO::encodeFileName(toPlainText()), direction
});
180 if (!m_blockFinishedSignal
) {
181 Q_EMIT
roleEditingFinished(m_role
, ret
);
185 #include "moc_kitemlistroleeditor.cpp"