File indexing completed on 2025-03-09 05:11:45
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "editactionsmapper.h" 0008 0009 #include <KActionCollection> 0010 #include <QEvent> 0011 #include <QPlainTextEdit> 0012 0013 #include <KStandardAction> 0014 0015 EditActionsMapper::EditActionsMapper(QObject *parent) 0016 : QObject(parent) 0017 { 0018 } 0019 0020 void EditActionsMapper::addTextEdit(QPlainTextEdit *control) 0021 { 0022 mTextEdits.append(control); 0023 0024 control->installEventFilter(this); 0025 0026 control->setContextMenuPolicy(Qt::DefaultContextMenu); 0027 connect(control, &QPlainTextEdit::copyAvailable, this, &EditActionsMapper::control_copyAvailable); 0028 connect(control, &QPlainTextEdit::selectionChanged, this, &EditActionsMapper::control_selectionChanged); 0029 connect(control, &QPlainTextEdit::undoAvailable, this, &EditActionsMapper::control_undoAvailable); 0030 connect(control, &QPlainTextEdit::redoAvailable, this, &EditActionsMapper::control_redoAvailable); 0031 } 0032 0033 void EditActionsMapper::init(KActionCollection *actionCollection) 0034 { 0035 mActionCut = KStandardAction::cut(this, &EditActionsMapper::actionCut_triggered, actionCollection); 0036 mActionCopy = KStandardAction::copy(this, &EditActionsMapper::actionCopy_triggered, actionCollection); 0037 mActionPaste = KStandardAction::paste(this, &EditActionsMapper::actionPaste_triggered, actionCollection); 0038 mSelectAll = KStandardAction::selectAll(this, &EditActionsMapper::actionSelectAll_triggered, actionCollection); 0039 mActionUndo = KStandardAction::undo(this, &EditActionsMapper::actionUndo_triggered, actionCollection); 0040 mActionRedo = KStandardAction::redo(this, &EditActionsMapper::actionRedo_triggered, actionCollection); 0041 } 0042 0043 void EditActionsMapper::control_undoAvailable(bool b) 0044 { 0045 auto control = qobject_cast<QPlainTextEdit *>(sender()); 0046 if (control != mActiveControl) 0047 return; 0048 0049 mActionUndo->setEnabled(b); 0050 } 0051 0052 void EditActionsMapper::control_redoAvailable(bool b) 0053 { 0054 auto control = qobject_cast<QPlainTextEdit *>(sender()); 0055 if (control != mActiveControl) 0056 return; 0057 0058 mActionRedo->setEnabled(b); 0059 } 0060 0061 void EditActionsMapper::control_copyAvailable(bool b) 0062 { 0063 auto control = qobject_cast<QPlainTextEdit *>(sender()); 0064 if (control != mActiveControl) 0065 return; 0066 0067 mActionCopy->setEnabled(b); 0068 } 0069 0070 void EditActionsMapper::control_selectionChanged() 0071 { 0072 } 0073 0074 void EditActionsMapper::actionUndo_triggered() 0075 { 0076 if (mActiveControl) 0077 mActiveControl->undo(); 0078 } 0079 0080 void EditActionsMapper::actionRedo_triggered() 0081 { 0082 if (mActiveControl) 0083 mActiveControl->redo(); 0084 } 0085 0086 void EditActionsMapper::actionCopy_triggered() 0087 { 0088 if (mActiveControl) 0089 mActiveControl->copy(); 0090 } 0091 0092 void EditActionsMapper::actionCut_triggered() 0093 { 0094 if (mActiveControl) 0095 mActiveControl->cut(); 0096 } 0097 0098 void EditActionsMapper::actionPaste_triggered() 0099 { 0100 if (mActiveControl) 0101 mActiveControl->paste(); 0102 } 0103 0104 void EditActionsMapper::actionSelectAll_triggered() 0105 { 0106 if (mActiveControl) 0107 mActiveControl->selectAll(); 0108 } 0109 0110 void EditActionsMapper::actionDelete_triggered() 0111 { 0112 } 0113 0114 bool EditActionsMapper::eventFilter(QObject *watched, QEvent *event) 0115 { 0116 if (event->type() != QEvent::FocusIn && event->type() != QEvent::FocusOut) 0117 return QObject::eventFilter(watched, event); 0118 auto textEdit = qobject_cast<QPlainTextEdit *>(watched); 0119 if (!textEdit || textEdit == mActiveControl) 0120 return QObject::eventFilter(watched, event); 0121 // auto e = static_cast<QFocusEvent*>(event); 0122 0123 if (event->type() != QEvent::FocusIn) 0124 mActiveControl = textEdit; 0125 else 0126 mActiveControl = nullptr; 0127 0128 return QObject::eventFilter(watched, event); 0129 } 0130 0131 // #include "moc_editactionsmapper.cpp"