File indexing completed on 2024-03-24 17:24:30
0001 /** 0002 * SPDX-FileCopyrightText: (C) 2003 by Sébastien Laoût <slaout@linux62.org> 0003 * SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "focusedwidgets.h" 0007 0008 #include <QApplication> 0009 #include <QMimeData> 0010 #include <QScrollBar> 0011 #include <QtCore/QEvent> 0012 #include <QtGui/QKeyEvent> 0013 #include <QtGui/QWheelEvent> 0014 0015 #include "basketscene.h" 0016 #include "bnpview.h" 0017 #include "global.h" 0018 #include "settings.h" 0019 0020 #ifdef KeyPress 0021 #undef KeyPress 0022 #endif 0023 0024 /** class FocusedTextEdit */ 0025 0026 FocusedTextEdit::FocusedTextEdit(bool disableUpdatesOnKeyPress, QWidget *parent) 0027 : KTextEdit(parent) 0028 , m_disableUpdatesOnKeyPress(disableUpdatesOnKeyPress) 0029 { 0030 connect(this, &FocusedTextEdit::selectionChanged, this, &FocusedTextEdit::onSelectionChanged); 0031 } 0032 0033 FocusedTextEdit::~FocusedTextEdit() 0034 { 0035 } 0036 0037 void FocusedTextEdit::paste(QClipboard::Mode mode) 0038 { 0039 const QMimeData *md = QApplication::clipboard()->mimeData(mode); 0040 if (md != nullptr) { 0041 insertFromMimeData(md); 0042 } 0043 } 0044 0045 void FocusedTextEdit::keyPressEvent(QKeyEvent *event) 0046 { 0047 if (event->key() == Qt::Key_Escape) { 0048 Q_EMIT escapePressed(); 0049 return; 0050 } 0051 0052 if (m_disableUpdatesOnKeyPress) { 0053 setUpdatesEnabled(false); 0054 } 0055 0056 KTextEdit::keyPressEvent(event); 0057 0058 // Workaround (for ensuring the cursor to be visible): signal not emitted when pressing those keys: 0059 if (event->key() == Qt::Key_Home || event->key() == Qt::Key_End || event->key() == Qt::Key_PageUp || event->key() == Qt::Key_PageDown) { 0060 Q_EMIT cursorPositionChanged(); 0061 } 0062 0063 if (m_disableUpdatesOnKeyPress) { 0064 setUpdatesEnabled(true); 0065 if (!document()->isEmpty()) { 0066 ensureCursorVisible(); 0067 } 0068 } 0069 } 0070 0071 void FocusedTextEdit::wheelEvent(QWheelEvent *event) 0072 { 0073 // If we're already scrolled all the way to the top or bottom, we pass the 0074 // wheel event onto the basket. 0075 QScrollBar *sb = verticalScrollBar(); 0076 if ((event->angleDelta().y() > 0 && sb->value() > sb->minimum()) || (event->angleDelta().y() < 0 && sb->value() < sb->maximum())) { 0077 KTextEdit::wheelEvent(event); 0078 } 0079 // else 0080 // Global::bnpView->currentBasket()->graphicsView()->wheelEvent(event); 0081 } 0082 0083 void FocusedTextEdit::enterEvent(QEvent *event) 0084 { 0085 Q_EMIT mouseEntered(); 0086 KTextEdit::enterEvent(event); 0087 } 0088 0089 void FocusedTextEdit::insertFromMimeData(const QMimeData *source) 0090 { 0091 // When user always wants plaintext pasting, if both HTML and text data is 0092 // present, only send plain text data (the provided source is readonly and I 0093 // also can't just pass it to QMimeData constructor as the latter is 'private') 0094 if (Settings::pasteAsPlainText() && source->hasHtml() && source->hasText()) { 0095 QMimeData alteredSource; 0096 alteredSource.setData(QStringLiteral("text/plain"), source->data(QStringLiteral("text/plain"))); 0097 KTextEdit::insertFromMimeData(&alteredSource); 0098 } else { 0099 KTextEdit::insertFromMimeData(source); 0100 } 0101 } 0102 0103 void FocusedTextEdit::onSelectionChanged() 0104 { 0105 if (textCursor().selectedText().length() > 0) { 0106 QMimeData *md = createMimeDataFromSelection(); 0107 QApplication::clipboard()->setMimeData(md, QClipboard::Selection); 0108 } 0109 } 0110 0111 /** class FocusWidgetFilter */ 0112 FocusWidgetFilter::FocusWidgetFilter(QWidget *parent) 0113 : QObject(parent) 0114 { 0115 if (parent != nullptr) { 0116 parent->installEventFilter(this); 0117 } 0118 } 0119 0120 bool FocusWidgetFilter::eventFilter(QObject *object, QEvent *event) 0121 { 0122 switch (event->type()) { 0123 case QEvent::KeyPress: { 0124 auto *ke = dynamic_cast<QKeyEvent *>(event); 0125 switch (ke->key()) { 0126 case Qt::Key_Return: 0127 Q_EMIT returnPressed(); 0128 return true; 0129 case Qt::Key_Escape: 0130 Q_EMIT escapePressed(); 0131 return true; 0132 default: 0133 return false; 0134 }; 0135 } 0136 case QEvent::Enter: 0137 Q_EMIT mouseEntered(); 0138 Q_FALLTHROUGH(); 0139 default: 0140 return false; 0141 }; 0142 } 0143 0144 #include "moc_focusedwidgets.cpp"