| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "shortcutmanager.h" |
| 18 | #include "systemconsts.h" |
| 19 | #include "settings.h" |
| 20 | #include <QAction> |
| 21 | #include <QFile> |
| 22 | #include <QJsonArray> |
| 23 | #include <QJsonDocument> |
| 24 | #include <QJsonObject> |
| 25 | #include <QJsonValue> |
| 26 | #include <QMessageBox> |
| 27 | |
| 28 | ShortcutManager::ShortcutManager(QObject *parent) : QObject(parent) |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | void ShortcutManager::load() |
| 34 | { |
| 35 | //if config file not exists, copy it from data |
| 36 | QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_SHORTCUT_FILE; |
| 37 | if (!fileExists(filename)) |
| 38 | return; |
| 39 | //read config file |
| 40 | QFile file(filename); |
| 41 | if (!file.open(QFile::ReadOnly)) { |
| 42 | QMessageBox::critical(nullptr, |
| 43 | tr("Read shortcut config failed" ), |
| 44 | tr("Can't open shortcut config file '%1' for read." ) |
| 45 | .arg(filename)); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | QByteArray json = file.readAll(); |
| 50 | QJsonParseError error; |
| 51 | QJsonDocument doc = QJsonDocument::fromJson(json,&error); |
| 52 | if (error.error != QJsonParseError::NoError) { |
| 53 | QMessageBox::critical(nullptr, |
| 54 | tr("Read shortcut config failed" ), |
| 55 | tr("Read shortcut config file '%1' failed:%2" ) |
| 56 | .arg(filename) |
| 57 | .arg(error.errorString())); |
| 58 | return; |
| 59 | } |
| 60 | mShortcuts.clear(); |
| 61 | QJsonArray array = doc.array(); |
| 62 | foreach (const QJsonValue& value,array) { |
| 63 | QJsonObject object = value.toObject(); |
| 64 | PEnvironmentShortcut shortcut = std::make_shared<EnvironmentShortcut>(); |
| 65 | shortcut->name = object["name" ].toString(); |
| 66 | if (shortcut->name.isEmpty()) |
| 67 | continue; |
| 68 | shortcut->shortcut = object["shortcut" ].toString(); |
| 69 | if (object["isAction" ].isNull()) |
| 70 | shortcut->isAction = true; |
| 71 | else |
| 72 | shortcut->isAction = object["isAction" ].toBool(); |
| 73 | mShortcuts.insert(shortcut->name,shortcut); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void ShortcutManager::save() |
| 78 | { |
| 79 | QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_SHORTCUT_FILE; |
| 80 | QFile file(filename); |
| 81 | if (!file.open(QFile::WriteOnly | QFile::Truncate)) { |
| 82 | QMessageBox::critical(nullptr, |
| 83 | tr("Save shortcut config failed" ), |
| 84 | tr("Can't open shortcut config file '%1' for write." ) |
| 85 | .arg(filename)); |
| 86 | return; |
| 87 | } |
| 88 | QJsonArray array; |
| 89 | foreach (const PEnvironmentShortcut& shortcut,mShortcuts) { |
| 90 | QJsonObject object; |
| 91 | object["name" ]=shortcut->name; |
| 92 | object["shortcut" ]=shortcut->shortcut; |
| 93 | object["isAction" ]=shortcut->isAction; |
| 94 | array.append(object); |
| 95 | } |
| 96 | QJsonDocument doc; |
| 97 | doc.setArray(array); |
| 98 | if (file.write(doc.toJson())<0) { |
| 99 | QMessageBox::critical(nullptr, |
| 100 | tr("Save shortcut config failed" ), |
| 101 | tr("Write to shortcut config file '%1' failed." ) |
| 102 | .arg(filename)); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void ShortcutManager::setShortcuts(QList<PEnvironmentShortcut> shortcuts) |
| 108 | { |
| 109 | mShortcuts.clear(); |
| 110 | foreach(PEnvironmentShortcut shortcut, shortcuts) { |
| 111 | if (shortcut->name.isEmpty()) |
| 112 | continue; |
| 113 | mShortcuts.insert(shortcut->name,shortcut); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void ShortcutManager::applyTo(QList<QAction *> actions) |
| 118 | { |
| 119 | foreach (QAction* action ,actions) { |
| 120 | applyTo(action); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void ShortcutManager::applyTo(QAction *action) |
| 125 | { |
| 126 | PEnvironmentShortcut item = mShortcuts.value(action->objectName(), PEnvironmentShortcut()); |
| 127 | if (item && item->isAction) { |
| 128 | action->setShortcut(QKeySequence::fromString(item->shortcut)); |
| 129 | } |
| 130 | } |
| 131 | |