1 /*
2 SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
3 SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 #include "parleyactions.h"
6
7 #include "prefs.h"
8
9 #include <KActionCollection>
10 #include <KLocalizedString>
11 #include <KNewStuff3/kns3/knewstuffaction.h>
12 #include <KStandardAction>
13 #include <KToggleAction>
14
15 #include <QIcon>
16
17 namespace ParleyActions
18 {
19 namespace Private
20 {
createCustomAction(const QObject * recvr,const char * slot,QObject * parent,const QString & name,const QString & text,const QString & helpText,const QString & iconName=QString (),bool toggle=false)21 QAction *createCustomAction(const QObject *recvr,
22 const char *slot,
23 QObject *parent,
24 const QString &name,
25 const QString &text,
26 const QString &helpText,
27 const QString &iconName = QString(),
28 bool toggle = false)
29 {
30 // Create QAction or KToggleAction
31 QAction *pAction;
32 if (toggle) {
33 pAction = new KToggleAction(parent);
34 } else {
35 pAction = new QAction(parent);
36 }
37 // Set ObjectName, Text and HelpText
38 pAction->setObjectName(name);
39 pAction->setText(text);
40 pAction->setToolTip(helpText);
41
42 // Icon
43 if (!iconName.isEmpty()) {
44 QIcon foundIcon(QIcon::fromTheme(iconName));
45 if (foundIcon.isNull()) {
46 // Note: If you are using an alternative /usr/share/icons directory you need to
47 // copy the /usr/share/icons/<theme>/index.theme into you alternate directory
48 qDebug() << "Missing QIcon " << iconName;
49 }
50 pAction->setIcon(foundIcon);
51 }
52
53 // Connect the action
54 pAction->connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
55
56 // If parent is a KActionCollection, add the new action to it
57 KActionCollection *collection = qobject_cast<KActionCollection *>(parent);
58 if (pAction && collection)
59 collection->addAction(pAction->objectName(), pAction);
60 return pAction;
61 }
62 }
63 }
64
create(ParleyAction id,const QObject * recvr,const char * slot,KActionCollection * parent)65 QAction *ParleyActions::create(ParleyAction id, const QObject *recvr, const char *slot, KActionCollection *parent)
66 {
67 QAction *pAction = 0;
68
69 switch (id) {
70 case FileNew:
71 pAction = KStandardAction::openNew(recvr, slot, parent);
72 pAction->setToolTip(i18n("Creates a new vocabulary collection"));
73 parent->setDefaultShortcut(pAction, QKeySequence::New);
74 break;
75 case FileOpen:
76 pAction = KStandardAction::open(recvr, slot, parent);
77 pAction->setToolTip(i18n("Opens an existing vocabulary collection"));
78 parent->setDefaultShortcut(pAction, QKeySequence::Open);
79 break;
80 case FileOpenDownloaded:
81 pAction = Private::createCustomAction(recvr,
82 slot,
83 parent,
84 QStringLiteral("file_open_downloaded"),
85 i18n("Open &Downloaded Vocabularies..."),
86 i18n("Open downloaded vocabulary collections"),
87 QStringLiteral("get-hot-new-stuff"));
88 break;
89 case FileSave:
90 pAction = KStandardAction::save(recvr, slot, parent);
91 pAction->setToolTip(i18n("Save the active vocabulary collection"));
92 parent->setDefaultShortcut(pAction, QKeySequence::Save);
93 break;
94 case FileSaveAs:
95 pAction = KStandardAction::saveAs(recvr, slot, parent);
96 pAction->setShortcut(QKeySequence::SaveAs);
97 parent->setDefaultShortcut(pAction, QKeySequence::SaveAs);
98 pAction->setToolTip(i18n("Save the active vocabulary collection with a different name"));
99 break;
100 case FileExport:
101 pAction = Private::createCustomAction(recvr,
102 slot,
103 parent,
104 QStringLiteral("file_export"),
105 i18n("&Export..."),
106 i18n("Export to HTML or CSV"),
107 QStringLiteral("document-export"));
108 break;
109 case FileProperties:
110 pAction = Private::createCustomAction(recvr,
111 slot,
112 parent,
113 QStringLiteral("file_properties"),
114 i18n("&Properties..."),
115 i18n("Edit document properties"),
116 QStringLiteral("document-properties"));
117 break;
118 case FileClose:
119 pAction = Private::createCustomAction(recvr,
120 slot,
121 parent,
122 QStringLiteral("file_close"),
123 i18n("Dashboard"),
124 i18n("Close the current vocabulary collection and show the dashboard"),
125 QStringLiteral("go-home"));
126 break;
127 case FileQuit:
128 pAction = KStandardAction::quit(recvr, slot, parent);
129 parent->setDefaultShortcut(pAction, QKeySequence::Quit);
130 pAction->setToolTip(i18n("Quit Parley"));
131 break;
132 case Preferences:
133 pAction = KStandardAction::preferences(recvr, slot, parent);
134 parent->setDefaultShortcut(pAction, QKeySequence::Preferences);
135 pAction->setToolTip(i18n("Show the configuration dialog"));
136 break;
137 case LanguagesProperties:
138 pAction = Private::createCustomAction(recvr,
139 slot,
140 parent,
141 QStringLiteral("edit_languages"),
142 i18n("&Languages..."),
143 i18n("Edit which languages are in the collection and their grammar properties."),
144 QStringLiteral("set-language"));
145 break;
146 case RemoveGrades:
147 pAction = Private::createCustomAction(recvr,
148 slot,
149 parent,
150 QStringLiteral("vocab_remove_grades"),
151 i18n("Remove Confidence Levels"),
152 i18n("Remove all confidence levels from the current document"),
153 QStringLiteral("edit-clear"));
154 break;
155 case CheckSpelling:
156 pAction = KStandardAction::spelling(recvr, slot, parent);
157 break;
158 case ToggleShowSublessons:
159 pAction = Private::createCustomAction(recvr,
160 slot,
161 parent,
162 QStringLiteral("lesson_showsublessonentries"),
163 i18n("Show Entries from Child Units"),
164 i18n("Enable to also see the entries of child units in each unit."),
165 QString(),
166 true);
167 pAction->setChecked(Prefs::showSublessonentries());
168 break;
169 case AutomaticTranslation:
170 pAction = Private::createCustomAction(recvr,
171 slot,
172 parent,
173 QStringLiteral("lesson_automatictranslation"),
174 i18n("Automatic Translation"),
175 i18n("Enable automatic translation of the unit entries."),
176 QString(),
177 true);
178 pAction->setChecked(Prefs::automaticTranslation());
179 break;
180 case StartPractice:
181 pAction = Private::createCustomAction(recvr,
182 slot,
183 parent,
184 QStringLiteral("practice_start"),
185 i18n("Start Practice..."),
186 i18n("Start practicing"),
187 QStringLiteral("practice-start"));
188 break;
189 case ConfigurePractice:
190 pAction = Private::createCustomAction(recvr,
191 slot,
192 parent,
193 QStringLiteral("practice_configure"),
194 i18n("Configure Practice..."),
195 i18n("Change practice settings"),
196 QStringLiteral("practice-setup"));
197 break;
198 case ExportPracticeResults:
199 pAction = Private::createCustomAction(recvr,
200 slot,
201 parent,
202 QStringLiteral("practice_export"),
203 i18n("Export Results..."),
204 i18n("Write a file with the results of the practice"),
205 QStringLiteral("document-export"));
206 break;
207 case EnterEditMode:
208 pAction = Private::createCustomAction(recvr,
209 slot,
210 parent,
211 QStringLiteral("document_edit"),
212 i18n("Editor"),
213 i18n("Switch to vocabulary editor"),
214 QStringLiteral("document-edit"));
215 break;
216 case ToggleSearchBar:
217 pAction = Private::createCustomAction(recvr,
218 slot,
219 parent,
220 QStringLiteral("config_show_search"),
221 i18n("Show Se&arch"),
222 i18n("Toggle display of the search bar"),
223 QString(),
224 true);
225 pAction->setChecked(Prefs::showSearch());
226 break;
227 case SearchVocabulary:
228 pAction = KStandardAction::find(recvr, slot, parent);
229 parent->setDefaultShortcut(pAction, QKeySequence::Find);
230 break;
231 }
232
233 Q_ASSERT(pAction);
234 return pAction;
235 }
236
createRecentFilesAction(const QObject * recvr,const char * slot,QObject * parent)237 KRecentFilesAction *ParleyActions::createRecentFilesAction(const QObject *recvr, const char *slot, QObject *parent)
238 {
239 return KStandardAction::openRecent(recvr, slot, parent);
240 }
241
createDownloadAction(const QObject * recvr,const char * slot,KActionCollection * collection)242 QAction *ParleyActions::createDownloadAction(const QObject *recvr, const char *slot, KActionCollection *collection)
243 {
244 QAction *pAction = new QAction(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")), i18n("Download New Vocabularies..."), collection);
245 collection->addAction(QStringLiteral("file_ghns"), pAction);
246 QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
247
248 pAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_G));
249 collection->setDefaultShortcut(pAction, QKeySequence(Qt::CTRL | Qt::Key_G));
250 pAction->setToolTip(i18n("Downloads new vocabulary collections"));
251 return pAction;
252 }
253