Krita Source Code Documentation
Loading...
Searching...
No Matches
kshortcutschemeseditor.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2008 Alexander Dymo <adymo@kdevelop.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
8
9#include <QLabel>
10#include <QMenu>
11#include <QFile>
12#include <QPushButton>
13#include <QDomDocument>
14#include <QStandardPaths>
15#include <QInputDialog>
16#include <QComboBox>
17#include <QHBoxLayout>
18#include <QDebug>
19
20#include <kconfiggroup.h>
21#include <kmessagebox.h>
22#include <ksharedconfig.h>
23#include <KoFileDialog.h>
24
25#include "KisShortcutsDialog.h"
27#include "kactioncollection.h"
28#include "kxmlguiclient.h"
29
30#include "KoResourcePaths.h"
31
32
34 : m_dialog(parent)
35{
36 KConfigGroup group(KSharedConfig::openConfig(), "Shortcut Schemes");
37
38 QStringList schemes;
39 schemes << QStringLiteral("Default");
40
41 auto schemeFileLocations = KisKShortcutSchemesHelper::schemeFileLocations();
42 schemes << schemeFileLocations.keys();
43
44 QString currentScheme = group.readEntry("Current Scheme", "Default");
45 QString schemeFileName = KisKShortcutSchemesHelper::schemeFileLocations().value(currentScheme);
46 if (!QFileInfo(schemeFileName).exists()) {
47 currentScheme = "Default";
48 }
49 setContentsMargins(0, 0, 0, 0);
50
51 QLabel *schemesLabel = new QLabel(i18n("Shortcut Schemes:"), m_dialog);
52 addWidget(schemesLabel);
53
54 m_schemesList = new QComboBox(m_dialog);
55 m_schemesList->setEditable(false);
56 m_schemesList->addItems(schemes);
57 m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
58 schemesLabel->setBuddy(m_schemesList);
59 addWidget(m_schemesList);
60
61 m_newScheme = new QPushButton(i18nc("New shortcut scheme", "New..."));
62 addWidget(m_newScheme);
63
64 m_deleteScheme = new QPushButton(i18n("Delete"));
65 addWidget(m_deleteScheme);
66
67 QPushButton *moreActions = new QPushButton(i18n("Save/Load"));
68 addWidget(moreActions);
69
70 QMenu *moreActionsMenu = new QMenu(m_dialog);
71 // moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
72 // this, SLOT(saveAsDefaultsForScheme()));
73
74 moreActionsMenu->addAction(i18n("Save Custom Shortcuts"),
75 this, SLOT(saveCustomShortcuts()));
76 moreActionsMenu->addAction(i18n("Load Custom Shortcuts"),
77 this, SLOT(loadCustomShortcuts()));
78 moreActionsMenu->addAction(i18n("Export Scheme..."),
79 this, SLOT(exportShortcutsScheme()));
80 moreActionsMenu->addAction(i18n("Import Scheme..."),
81 this, SLOT(importShortcutsScheme()));
82 moreActions->setMenu(moreActionsMenu);
83
84 addStretch(1);
85
86 connect(m_schemesList, SIGNAL(textActivated(QString)),
87 this, SIGNAL(shortcutsSchemeChanged(QString)));
88 connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme()));
89 connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme()));
91}
92
94{
95 bool ok;
96 const QString newName = QInputDialog::getText(m_dialog, i18n("Name for New Scheme"),
97 i18n("Name for new scheme:"), QLineEdit::Normal, i18n("New Scheme"), &ok);
98 if (!ok) {
99 return;
100 }
101
102 if (m_schemesList->findText(newName) != -1) {
103 KMessageBox::error(m_dialog, i18n("A scheme with this name already exists."));
104 return;
105 }
106
107 const QString newSchemeFileName = KisKShortcutSchemesHelper::shortcutSchemeFileName(newName) + ".shortcuts";
108
109 QFile schemeFile(newSchemeFileName);
110 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) {
111 qDebug() << "Could not open scheme file.";
112 return;
113 }
114 schemeFile.close();
115
116 m_dialog->exportConfiguration(newSchemeFileName);
117 m_schemesList->addItem(newName);
118 m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
119 m_schemeFileLocations.insert(newName, newSchemeFileName);
121 Q_EMIT shortcutsSchemeChanged(newName);
122}
123
125{
126 if (KMessageBox::questionTwoActions(m_dialog,
127 i18n("Do you really want to delete the scheme %1?\n\
128 Note that this will not remove any system wide shortcut schemes.",
129 currentScheme()),
130 QString(),
131 KStandardGuiItem::del(),
132 KStandardGuiItem::cancel())
133 == KMessageBox::SecondaryAction) {
134 return;
135 }
136
137 //delete the scheme for the app itself
139
140 m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
143}
144
146{
147 return m_schemesList->currentText();
148}
149
151{
152 KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs");
153 QString proposedPath = group.readEntry("ExportShortcuts", KoResourcePaths::saveLocation("kis_shortcuts"));
154
155 KoFileDialog dialog(m_dialog, KoFileDialog::SaveFile, "ExportShortcuts");
156 dialog.setCaption(i18n("Export Shortcuts"));
157 dialog.setDefaultDir(proposedPath);
158 dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
159 QString path = dialog.filename();
160
161 if (!path.isEmpty()) {
163 }
164}
165
167{
168 KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs");
169 QString proposedPath = group.readEntry("SaveCustomShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
170
171 KoFileDialog dialog(m_dialog, KoFileDialog::SaveFile, "SaveCustomShortcuts");
172 dialog.setCaption(i18n("Save Shortcuts"));
173 dialog.setDefaultDir(proposedPath);
174 dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
175 QString path = dialog.filename();
176
177 if (!path.isEmpty()) {
179 }
180}
181
182
183
185{
186 KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs");
187 QString proposedPath = group.readEntry("ImportShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
188
189 KoFileDialog dialog(m_dialog, KoFileDialog::ImportFile, "ImportShortcuts");
190 dialog.setCaption(i18n("Import Shortcuts"));
191 dialog.setDefaultDir(proposedPath);
192 dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
193 QString path = dialog.filename();
194
195 if (path.isEmpty()) {
196 return;
197 }
198
199 // auto ar = KisActionRegistry::instance();
200 // ar->loadCustomShortcuts(path);
202
203}
204
206{
207 KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs");
208 QString proposedPath = group.readEntry("ImportShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
209
210 KoFileDialog dialog(m_dialog, KoFileDialog::ImportFile, "ImportShortcuts");
211 dialog.setCaption(i18n("Import Shortcuts"));
212 dialog.setDefaultDir(proposedPath);
213 dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
214 QString path = dialog.filename();
215
216 if (path.isEmpty()) {
217 return;
218 }
219
221}
222
223#if 0
224// XXX: Not implemented
225void KisKShortcutSchemesEditor::saveAsDefaultsForScheme()
226{
227 foreach (KisKActionCollection *collection, m_dialog->actionCollections()) {
228 KisKShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
229 }
230}
231#endif
232
234{
235 m_deleteScheme->setEnabled(m_schemesList->count() >= 1);
236}
QList< QString > QStringList
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
A container for a set of QAction objects.
void shortcutsSchemeChanged(const QString &)
KisKShortcutSchemesEditor(KisShortcutsDialog *parent)
QHash< QString, QString > m_schemeFileLocations
static QHash< QString, QString > schemeFileLocations()
static QString shortcutSchemeFileName(const QString &schemeName)
void exportConfiguration(const QString &path) const
void importConfiguration(const QString &path)
void loadCustomShortcuts(const QString &path)
void saveCustomShortcuts(const QString &path) const
QList< KisKActionCollection * > actionCollections() const
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)