2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-10-06 23:19:18 +08:00
|
|
|
#include "shortcutmanager.h"
|
|
|
|
#include "systemconsts.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include <QAction>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
ShortcutManager::ShortcutManager(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::load()
|
|
|
|
{
|
|
|
|
//if config file not exists, copy it from data
|
|
|
|
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_SHORTCUT_FILE;
|
|
|
|
if (!fileExists(filename))
|
|
|
|
return;
|
|
|
|
//read config file
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
QMessageBox::critical(nullptr,
|
|
|
|
tr("Read shortcut config failed"),
|
|
|
|
tr("Can't open shortcut config file '%1' for read.")
|
|
|
|
.arg(filename));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray json = file.readAll();
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(json,&error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
QMessageBox::critical(nullptr,
|
|
|
|
tr("Read shortcut config failed"),
|
|
|
|
tr("Read shortcut config file '%1' failed:%2")
|
|
|
|
.arg(filename)
|
|
|
|
.arg(error.errorString()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mShortcuts.clear();
|
|
|
|
QJsonArray array = doc.array();
|
|
|
|
foreach (const QJsonValue& value,array) {
|
|
|
|
QJsonObject object = value.toObject();
|
|
|
|
PEnvironmentShortcut shortcut = std::make_shared<EnvironmentShortcut>();
|
|
|
|
shortcut->name = object["name"].toString();
|
|
|
|
if (shortcut->name.isEmpty())
|
|
|
|
continue;
|
|
|
|
shortcut->shortcut = object["shortcut"].toString();
|
2022-06-12 22:54:34 +08:00
|
|
|
if (object["isAction"].isNull())
|
|
|
|
shortcut->isAction = true;
|
|
|
|
else
|
|
|
|
shortcut->isAction = object["isAction"].toBool();
|
2021-10-06 23:19:18 +08:00
|
|
|
mShortcuts.insert(shortcut->name,shortcut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::save()
|
|
|
|
{
|
|
|
|
QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_SHORTCUT_FILE;
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
|
|
|
QMessageBox::critical(nullptr,
|
|
|
|
tr("Save shortcut config failed"),
|
|
|
|
tr("Can't open shortcut config file '%1' for write.")
|
|
|
|
.arg(filename));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonArray array;
|
|
|
|
foreach (const PEnvironmentShortcut& shortcut,mShortcuts) {
|
|
|
|
QJsonObject object;
|
|
|
|
object["name"]=shortcut->name;
|
|
|
|
object["shortcut"]=shortcut->shortcut;
|
2022-06-12 22:54:34 +08:00
|
|
|
object["isAction"]=shortcut->isAction;
|
2021-10-06 23:19:18 +08:00
|
|
|
array.append(object);
|
|
|
|
}
|
|
|
|
QJsonDocument doc;
|
|
|
|
doc.setArray(array);
|
|
|
|
if (file.write(doc.toJson())<0) {
|
|
|
|
QMessageBox::critical(nullptr,
|
|
|
|
tr("Save shortcut config failed"),
|
|
|
|
tr("Write to shortcut config file '%1' failed.")
|
|
|
|
.arg(filename));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::setShortcuts(QList<PEnvironmentShortcut> shortcuts)
|
|
|
|
{
|
|
|
|
mShortcuts.clear();
|
|
|
|
foreach(PEnvironmentShortcut shortcut, shortcuts) {
|
|
|
|
if (shortcut->name.isEmpty())
|
|
|
|
continue;
|
|
|
|
mShortcuts.insert(shortcut->name,shortcut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::applyTo(QList<QAction *> actions)
|
|
|
|
{
|
|
|
|
foreach (QAction* action ,actions) {
|
|
|
|
applyTo(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::applyTo(QAction *action)
|
|
|
|
{
|
|
|
|
PEnvironmentShortcut item = mShortcuts.value(action->objectName(), PEnvironmentShortcut());
|
2022-06-12 22:54:34 +08:00
|
|
|
if (item && item->isAction) {
|
2021-10-06 23:19:18 +08:00
|
|
|
action->setShortcut(QKeySequence::fromString(item->shortcut));
|
2023-03-17 17:57:49 +08:00
|
|
|
}
|
|
|
|
if (!action->shortcut().isEmpty()){
|
2023-03-17 15:37:11 +08:00
|
|
|
action->setToolTip(action->text()+QString("(%1)").arg(action->shortcut().toString()));
|
|
|
|
} else {
|
|
|
|
action->setToolTip(action->text());
|
2021-10-06 23:19:18 +08:00
|
|
|
}
|
|
|
|
}
|