1 /*
2   SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3 
4   SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "kmcomposerglobalaction.h"
8 #include "kmcomposerwin.h"
9 
10 #include <PimCommon/LineEditWithAutoCorrection>
11 
12 #include <KLineEdit>
13 
14 #include <editor/kmcomposereditorng.h>
15 
KMComposerGlobalAction(KMComposerWin * composerWin,QObject * parent)16 KMComposerGlobalAction::KMComposerGlobalAction(KMComposerWin *composerWin, QObject *parent)
17     : QObject(parent)
18     , mComposerWin(composerWin)
19 {
20 }
21 
22 KMComposerGlobalAction::~KMComposerGlobalAction() = default;
23 
slotUndo()24 void KMComposerGlobalAction::slotUndo()
25 {
26     QWidget *fw = mComposerWin->focusWidget();
27     if (!fw) {
28         return;
29     }
30 
31     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
32         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->undo();
33     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
34         static_cast<QTextEdit *>(fw)->undo();
35     } else if (::qobject_cast<KLineEdit *>(fw)) {
36         static_cast<KLineEdit *>(fw)->undo();
37     }
38 }
39 
slotRedo()40 void KMComposerGlobalAction::slotRedo()
41 {
42     QWidget *fw = mComposerWin->focusWidget();
43     if (!fw) {
44         return;
45     }
46 
47     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
48         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->redo();
49     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
50         static_cast<QTextEdit *>(fw)->redo();
51     } else if (::qobject_cast<KLineEdit *>(fw)) {
52         static_cast<KLineEdit *>(fw)->redo();
53     }
54 }
55 
slotCut()56 void KMComposerGlobalAction::slotCut()
57 {
58     QWidget *fw = mComposerWin->focusWidget();
59     if (!fw) {
60         return;
61     }
62 
63     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
64         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->cut();
65     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
66         static_cast<QTextEdit *>(fw)->cut();
67     } else if (::qobject_cast<KLineEdit *>(fw)) {
68         static_cast<KLineEdit *>(fw)->cut();
69     }
70 }
71 
slotCopy()72 void KMComposerGlobalAction::slotCopy()
73 {
74     QWidget *fw = mComposerWin->focusWidget();
75     if (!fw) {
76         return;
77     }
78 
79     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
80         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->copy();
81     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
82         static_cast<QTextEdit *>(fw)->copy();
83     } else if (::qobject_cast<KLineEdit *>(fw)) {
84         static_cast<KLineEdit *>(fw)->copy();
85     }
86 }
87 
slotPaste()88 void KMComposerGlobalAction::slotPaste()
89 {
90     QWidget *const fw = mComposerWin->focusWidget();
91     if (!fw) {
92         return;
93     }
94     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
95         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->paste();
96     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
97         static_cast<QTextEdit *>(fw)->paste();
98     } else if (::qobject_cast<KLineEdit *>(fw)) {
99         static_cast<KLineEdit *>(fw)->paste();
100     }
101 }
102 
slotMarkAll()103 void KMComposerGlobalAction::slotMarkAll()
104 {
105     QWidget *fw = mComposerWin->focusWidget();
106     if (!fw) {
107         return;
108     }
109 
110     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
111         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->selectAll();
112     } else if (::qobject_cast<KLineEdit *>(fw)) {
113         static_cast<KLineEdit *>(fw)->selectAll();
114     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
115         static_cast<QTextEdit *>(fw)->selectAll();
116     }
117 }
118 
slotInsertEmoticon(const QString & str)119 void KMComposerGlobalAction::slotInsertEmoticon(const QString &str)
120 {
121     QWidget *fw = mComposerWin->focusWidget();
122     if (!fw) {
123         return;
124     }
125 
126     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
127         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->insertPlainText(str);
128     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
129         static_cast<QTextEdit *>(fw)->insertPlainText(str);
130     }
131     //} else if (::qobject_cast<KLineEdit *>(fw)) {
132     // Don't insert emoticon in mail linedit
133     // static_cast<KLineEdit *>(fw)->insert(str);
134 }
135 
slotInsertText(const QString & str)136 void KMComposerGlobalAction::slotInsertText(const QString &str)
137 {
138     QWidget *fw = mComposerWin->focusWidget();
139     if (!fw) {
140         return;
141     }
142 
143     if (::qobject_cast<PimCommon::LineEditWithAutoCorrection *>(fw)) {
144         static_cast<PimCommon::LineEditWithAutoCorrection *>(fw)->insertPlainText(str);
145     } else if (::qobject_cast<KMComposerEditorNg *>(fw)) {
146         static_cast<QTextEdit *>(fw)->insertPlainText(str);
147     }
148     // Don't insert text in mail linedit
149     //} else if (::qobject_cast<KLineEdit *>(fw)) {
150 }
151