File indexing completed on 2024-06-16 04:15:39

0001 /* This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2008 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "kshortcutschemeseditor.h"
0007 #include "KisShortcutsDialog_p.h"
0008 
0009 #include <QLabel>
0010 #include <QMenu>
0011 #include <QFile>
0012 #include <QPushButton>
0013 #include <QDomDocument>
0014 #include <QStandardPaths>
0015 #include <QInputDialog>
0016 #include <QComboBox>
0017 #include <QHBoxLayout>
0018 #include <QDebug>
0019 
0020 #include <kconfiggroup.h>
0021 #include <kmessagebox.h>
0022 #include <ksharedconfig.h>
0023 #include <KoFileDialog.h>
0024 
0025 #include "KisShortcutsDialog.h"
0026 #include "kshortcutschemeshelper_p.h"
0027 #include "kactioncollection.h"
0028 #include "kxmlguiclient.h"
0029 
0030 #include "KoResourcePaths.h"
0031 
0032 
0033 KisKShortcutSchemesEditor::KisKShortcutSchemesEditor(KisShortcutsDialog *parent)
0034     : m_dialog(parent)
0035 {
0036     KConfigGroup group(KSharedConfig::openConfig(), "Shortcut Schemes");
0037 
0038     QStringList schemes;
0039     schemes << QStringLiteral("Default");
0040 
0041     auto schemeFileLocations = KisKShortcutSchemesHelper::schemeFileLocations();
0042     schemes << schemeFileLocations.keys();
0043 
0044     QString currentScheme = group.readEntry("Current Scheme", "Default");
0045     QString schemeFileName = KisKShortcutSchemesHelper::schemeFileLocations().value(currentScheme);
0046     if (!QFileInfo(schemeFileName).exists()) {
0047         currentScheme = "Default";
0048     }
0049     setMargin(0);
0050 
0051     QLabel *schemesLabel = new QLabel(i18n("Shortcut Schemes:"), m_dialog);
0052     addWidget(schemesLabel);
0053 
0054     m_schemesList = new QComboBox(m_dialog);
0055     m_schemesList->setEditable(false);
0056     m_schemesList->addItems(schemes);
0057     m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
0058     schemesLabel->setBuddy(m_schemesList);
0059     addWidget(m_schemesList);
0060 
0061     m_newScheme = new QPushButton(i18nc("New shortcut scheme", "New..."));
0062     addWidget(m_newScheme);
0063 
0064     m_deleteScheme = new QPushButton(i18n("Delete"));
0065     addWidget(m_deleteScheme);
0066 
0067     QPushButton *moreActions = new QPushButton(i18n("Save/Load"));
0068     addWidget(moreActions);
0069 
0070     QMenu *moreActionsMenu = new QMenu(m_dialog);
0071     // moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
0072                                // this, SLOT(saveAsDefaultsForScheme()));
0073 
0074     moreActionsMenu->addAction(i18n("Save Custom Shortcuts"),
0075                                this, SLOT(saveCustomShortcuts()));
0076     moreActionsMenu->addAction(i18n("Load Custom Shortcuts"),
0077                                this, SLOT(loadCustomShortcuts()));
0078     moreActionsMenu->addAction(i18n("Export Scheme..."),
0079                                this, SLOT(exportShortcutsScheme()));
0080     moreActionsMenu->addAction(i18n("Import Scheme..."),
0081                                this, SLOT(importShortcutsScheme()));
0082     moreActions->setMenu(moreActionsMenu);
0083 
0084     addStretch(1);
0085 
0086     connect(m_schemesList, SIGNAL(activated(QString)),
0087             this, SIGNAL(shortcutsSchemeChanged(QString)));
0088     connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme()));
0089     connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme()));
0090     updateDeleteButton();
0091 }
0092 
0093 void KisKShortcutSchemesEditor::newScheme()
0094 {
0095     bool ok;
0096     const QString newName = QInputDialog::getText(m_dialog, i18n("Name for New Scheme"),
0097                             i18n("Name for new scheme:"), QLineEdit::Normal, i18n("New Scheme"), &ok);
0098     if (!ok) {
0099         return;
0100     }
0101 
0102     if (m_schemesList->findText(newName) != -1) {
0103         KMessageBox::sorry(m_dialog, i18n("A scheme with this name already exists."));
0104         return;
0105     }
0106 
0107     const QString newSchemeFileName = KisKShortcutSchemesHelper::shortcutSchemeFileName(newName) + ".shortcuts";
0108 
0109     QFile schemeFile(newSchemeFileName);
0110     if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) {
0111         qDebug() << "Could not open scheme file.";
0112         return;
0113     }
0114     schemeFile.close();
0115 
0116     m_dialog->exportConfiguration(newSchemeFileName);
0117     m_schemesList->addItem(newName);
0118     m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
0119     m_schemeFileLocations.insert(newName, newSchemeFileName);
0120     updateDeleteButton();
0121     emit shortcutsSchemeChanged(newName);
0122 }
0123 
0124 void KisKShortcutSchemesEditor::deleteScheme()
0125 {
0126     if (KMessageBox::questionYesNo(m_dialog,
0127                                    i18n("Do you really want to delete the scheme %1?\n\
0128 Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No) {
0129         return;
0130     }
0131 
0132     //delete the scheme for the app itself
0133     QFile::remove(KisKShortcutSchemesHelper::shortcutSchemeFileName(currentScheme()));
0134 
0135     m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
0136     updateDeleteButton();
0137     emit shortcutsSchemeChanged(currentScheme());
0138 }
0139 
0140 QString KisKShortcutSchemesEditor::currentScheme()
0141 {
0142     return m_schemesList->currentText();
0143 }
0144 
0145 void KisKShortcutSchemesEditor::exportShortcutsScheme()
0146 {
0147     KConfigGroup group =  KSharedConfig::openConfig()->group("File Dialogs");
0148     QString proposedPath = group.readEntry("ExportShortcuts", KoResourcePaths::saveLocation("kis_shortcuts"));
0149 
0150     KoFileDialog dialog(m_dialog, KoFileDialog::SaveFile, "ExportShortcuts");
0151     dialog.setCaption(i18n("Export Shortcuts"));
0152     dialog.setDefaultDir(proposedPath);
0153     dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
0154     QString path = dialog.filename();
0155 
0156     if (!path.isEmpty()) {
0157         m_dialog->exportConfiguration(path);
0158     }
0159 }
0160 
0161 void KisKShortcutSchemesEditor::saveCustomShortcuts()
0162 {
0163     KConfigGroup group =  KSharedConfig::openConfig()->group("File Dialogs");
0164     QString proposedPath = group.readEntry("SaveCustomShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
0165 
0166     KoFileDialog dialog(m_dialog, KoFileDialog::SaveFile, "SaveCustomShortcuts");
0167     dialog.setCaption(i18n("Save Shortcuts"));
0168     dialog.setDefaultDir(proposedPath);
0169     dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
0170     QString path = dialog.filename();
0171 
0172     if (!path.isEmpty()) {
0173         m_dialog->saveCustomShortcuts(path);
0174     }
0175 }
0176 
0177 
0178 
0179 void KisKShortcutSchemesEditor::loadCustomShortcuts()
0180 {
0181     KConfigGroup group =  KSharedConfig::openConfig()->group("File Dialogs");
0182     QString proposedPath = group.readEntry("ImportShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
0183 
0184     KoFileDialog dialog(m_dialog, KoFileDialog::ImportFile, "ImportShortcuts");
0185     dialog.setCaption(i18n("Import Shortcuts"));
0186     dialog.setDefaultDir(proposedPath);
0187     dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
0188     QString path = dialog.filename();
0189 
0190     if (path.isEmpty()) {
0191         return;
0192     }
0193 
0194     // auto ar = KisActionRegistry::instance();
0195     // ar->loadCustomShortcuts(path);
0196     m_dialog->loadCustomShortcuts(path);
0197 
0198 }
0199 
0200 void KisKShortcutSchemesEditor::importShortcutsScheme()
0201 {
0202     KConfigGroup group =  KSharedConfig::openConfig()->group("File Dialogs");
0203     QString proposedPath = group.readEntry("ImportShortcuts", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
0204 
0205     KoFileDialog dialog(m_dialog, KoFileDialog::ImportFile, "ImportShortcuts");
0206     dialog.setCaption(i18n("Import Shortcuts"));
0207     dialog.setDefaultDir(proposedPath);
0208     dialog.setMimeTypeFilters(QStringList() << "application/x-krita-shortcuts", "application/x-krita-shortcuts");
0209     QString path = dialog.filename();
0210 
0211     if (path.isEmpty()) {
0212         return;
0213     }
0214 
0215     m_dialog->importConfiguration(path);
0216 }
0217 
0218 #if 0
0219 // XXX: Not implemented
0220 void KisKShortcutSchemesEditor::saveAsDefaultsForScheme()
0221 {
0222     foreach (KisKActionCollection *collection, m_dialog->actionCollections()) {
0223         KisKShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
0224     }
0225 }
0226 #endif
0227 
0228 void KisKShortcutSchemesEditor::updateDeleteButton()
0229 {
0230     m_deleteScheme->setEnabled(m_schemesList->count() >= 1);
0231 }