- implement: config shortcuts

This commit is contained in:
royqh1979@gmail.com 2021-10-06 23:19:18 +08:00
parent d88bab1e20
commit 48e97dc15d
12 changed files with 212 additions and 29 deletions

View File

@ -61,6 +61,7 @@ SOURCES += \
settingsdialog/projectoutputwidget.cpp \ settingsdialog/projectoutputwidget.cpp \
settingsdialog/projectprecompilewidget.cpp \ settingsdialog/projectprecompilewidget.cpp \
settingsdialog/projectversioninfowidget.cpp \ settingsdialog/projectversioninfowidget.cpp \
shortcutmanager.cpp \
symbolusagemanager.cpp \ symbolusagemanager.cpp \
todoparser.cpp \ todoparser.cpp \
widgets/aboutdialog.cpp \ widgets/aboutdialog.cpp \
@ -166,6 +167,7 @@ HEADERS += \
settingsdialog/projectoutputwidget.h \ settingsdialog/projectoutputwidget.h \
settingsdialog/projectprecompilewidget.h \ settingsdialog/projectprecompilewidget.h \
settingsdialog/projectversioninfowidget.h \ settingsdialog/projectversioninfowidget.h \
shortcutmanager.h \
symbolusagemanager.h \ symbolusagemanager.h \
todoparser.h \ todoparser.h \
widgets/aboutdialog.h \ widgets/aboutdialog.h \

View File

@ -5,6 +5,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include "systemconsts.h"
AutolinkManager* pAutolinkManager; AutolinkManager* pAutolinkManager;
@ -21,7 +22,7 @@ PAutolink AutolinkManager::getLink(const QString &header) const
void AutolinkManager::load() void AutolinkManager::load()
{ {
QDir dir(pSettings->dirs().config()); QDir dir(pSettings->dirs().config());
QString filename=dir.filePath(AUTOLINK_CONFIG); QString filename=dir.filePath(DEV_AUTOLINK_FILE);
QFile file(filename); QFile file(filename);
if (!file.exists()) { if (!file.exists()) {
QFile preFile(":/config/autolink.json"); QFile preFile(":/config/autolink.json");
@ -52,7 +53,7 @@ void AutolinkManager::load()
void AutolinkManager::save() void AutolinkManager::save()
{ {
QDir dir(pSettings->dirs().config()); QDir dir(pSettings->dirs().config());
QString filename=dir.filePath(AUTOLINK_CONFIG); QString filename=dir.filePath(DEV_AUTOLINK_FILE);
QFile file(filename); QFile file(filename);
if (file.open(QFile::WriteOnly|QFile::Truncate)) { if (file.open(QFile::WriteOnly|QFile::Truncate)) {
QJsonDocument doc(toJson()); QJsonDocument doc(toJson());

View File

@ -7,8 +7,6 @@
#include <QVector> #include <QVector>
#include <QMap> #include <QMap>
#define AUTOLINK_CONFIG "autolink.json"
struct Autolink { struct Autolink {
QString header; QString header;
QString linkOption; QString linkOption;

View File

@ -47,6 +47,7 @@ void CodeSnippetsManager::load()
tr("Read code snippets failed"), tr("Read code snippets failed"),
tr("Can't open code snippet file '%1' for read.") tr("Can't open code snippet file '%1' for read.")
.arg(filename)); .arg(filename));
return;
} }
QByteArray json = file.readAll(); QByteArray json = file.readAll();
@ -58,6 +59,7 @@ void CodeSnippetsManager::load()
tr("Read code snippet file '%1' failed:%2") tr("Read code snippet file '%1' failed:%2")
.arg(filename) .arg(filename)
.arg(error.errorString())); .arg(error.errorString()));
return;
} }
mSnippets.clear(); mSnippets.clear();
QJsonArray array = doc.array(); QJsonArray array = doc.array();

View File

@ -178,6 +178,7 @@ MainWindow::MainWindow(QWidget *parent)
updateEditorColorSchemes(); updateEditorColorSchemes();
updateShortcuts();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -666,6 +667,13 @@ void MainWindow::resetAutoSaveTimer()
} }
} }
void MainWindow::updateShortcuts()
{
ShortcutManager manager;
manager.load();
manager.applyTo(findChildren<QAction*>());
}
QPlainTextEdit *MainWindow::txtLocals() QPlainTextEdit *MainWindow::txtLocals()
{ {
return ui->txtLocals; return ui->txtLocals;

View File

@ -14,6 +14,7 @@
#include "symbolusagemanager.h" #include "symbolusagemanager.h"
#include "codesnippetsmanager.h" #include "codesnippetsmanager.h"
#include "todoparser.h" #include "todoparser.h"
#include "shortcutmanager.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -87,7 +88,7 @@ public:
void rebuildOpenedFileHisotryMenu(); void rebuildOpenedFileHisotryMenu();
void updateClassBrowserForEditor(Editor* editor); void updateClassBrowserForEditor(Editor* editor);
void resetAutoSaveTimer(); void resetAutoSaveTimer();
void updateShortcuts();
void saveLastOpens(); void saveLastOpens();
void loadLastOpens(); void loadLastOpens();

View File

@ -9,6 +9,8 @@ EnvironmentShortcutWidget::EnvironmentShortcutWidget(const QString& name, const
{ {
ui->setupUi(this); ui->setupUi(this);
ui->tblShortcut->setModel(&mModel); ui->tblShortcut->setModel(&mModel);
connect(&mModel, &EnvironmentShortcutModel::shortcutChanged,
this, &SettingsWidget::setSettingsChanged);
} }
EnvironmentShortcutWidget::~EnvironmentShortcutWidget() EnvironmentShortcutWidget::~EnvironmentShortcutWidget()
@ -23,10 +25,11 @@ void EnvironmentShortcutWidget::doLoad()
void EnvironmentShortcutWidget::doSave() void EnvironmentShortcutWidget::doSave()
{ {
foreach (const PEnvironmentShortCut& shortcut, mModel.shortcuts()) { ShortcutManager manager;
shortcut->action->setShortcut(QKeySequence::fromString(shortcut->shortcut)); manager.setShortcuts(mModel.shortcuts());
shortcut->shortcut = shortcut->action->shortcut().toString(); manager.save();
} pMainWindow->updateShortcuts();
mModel.reload();
} }
EnvironmentShortcutModel::EnvironmentShortcutModel(QObject *parent):QAbstractTableModel(parent) EnvironmentShortcutModel::EnvironmentShortcutModel(QObject *parent):QAbstractTableModel(parent)
@ -41,6 +44,8 @@ void EnvironmentShortcutModel::reload()
QList<QAction*> actions = pMainWindow->findChildren<QAction*>(QString(),Qt::FindDirectChildrenOnly); QList<QAction*> actions = pMainWindow->findChildren<QAction*>(QString(),Qt::FindDirectChildrenOnly);
QList<QMenu*> menus = pMainWindow->mainWidget()->menubar->findChildren<QMenu*>(); QList<QMenu*> menus = pMainWindow->mainWidget()->menubar->findChildren<QMenu*>();
foreach( const QMenu* menu, menus) { foreach( const QMenu* menu, menus) {
if (menu->title().isEmpty())
continue;
loadShortCutsOfMenu(menu, actions); loadShortCutsOfMenu(menu, actions);
} }
endResetModel(); endResetModel();
@ -62,7 +67,7 @@ QVariant EnvironmentShortcutModel::data(const QModelIndex &index, int role) cons
return QVariant(); return QVariant();
} }
if (role==Qt::DisplayRole || role == Qt::EditRole) { if (role==Qt::DisplayRole || role == Qt::EditRole) {
PEnvironmentShortCut item = mShortcuts[index.row()]; PEnvironmentShortcut item = mShortcuts[index.row()];
switch( index.column()) { switch( index.column()) {
case 0: case 0:
return item->fullPath; return item->fullPath;
@ -81,8 +86,12 @@ bool EnvironmentShortcutModel::setData(const QModelIndex &index, const QVariant
if (role == Qt::EditRole) { if (role == Qt::EditRole) {
if (index.column()!=1) if (index.column()!=1)
return false; return false;
PEnvironmentShortCut item = mShortcuts[index.row()]; PEnvironmentShortcut item = mShortcuts[index.row()];
QString s = value.toString().trimmed();
if (s!=item->shortcut) {
item->shortcut = value.toString(); item->shortcut = value.toString();
emit shortcutChanged();
}
return true; return true;
} }
return false; return false;
@ -112,20 +121,29 @@ Qt::ItemFlags EnvironmentShortcutModel::flags(const QModelIndex &index) const
return flags; return flags;
} }
const QList<PEnvironmentShortCut> &EnvironmentShortcutModel::shortcuts() const const QList<PEnvironmentShortcut> &EnvironmentShortcutModel::shortcuts() const
{ {
return mShortcuts; return mShortcuts;
} }
void EnvironmentShortcutModel::shortcutsUpdated()
{
beginResetModel();
endResetModel();
}
void EnvironmentShortcutModel::loadShortCutsOfMenu(const QMenu *menu, QList<QAction *> &globalActions) void EnvironmentShortcutModel::loadShortCutsOfMenu(const QMenu *menu, QList<QAction *> &globalActions)
{ {
QList<QAction*> actions = menu->actions(); QList<QAction*> actions = menu->actions();
foreach (QAction* action,actions) { foreach (QAction* action,actions) {
PEnvironmentShortCut item = std::make_shared<EnvironmentShortCut>(); if (!action->text().isEmpty()) {
PEnvironmentShortcut item = std::make_shared<EnvironmentShortcut>();
item->name = action->objectName(); item->name = action->objectName();
item->fullPath = QString("%1 > %2").arg(menu->title(),action->text()); item->fullPath = QString("%1 > %2").arg(menu->title(),action->text());
item->action = action; item->action = action;
item->shortcut = action->shortcut().toString().trimmed(); item->shortcut = action->shortcut().toString().trimmed();
mShortcuts.append(item);
}
globalActions.removeAll(action); globalActions.removeAll(action);
} }
} }

View File

@ -4,21 +4,15 @@
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QWidget> #include <QWidget>
#include "settingswidget.h" #include "settingswidget.h"
#include "../shortcutmanager.h"
namespace Ui { namespace Ui {
class EnvironmentShortcutWidget; class EnvironmentShortcutWidget;
} }
struct EnvironmentShortCut {
QString name;
QString fullPath;
QString shortcut;
QAction* action;
};
using PEnvironmentShortCut = std::shared_ptr<EnvironmentShortCut>;
class QMenu; class QMenu;
class EnvironmentShortcutModel: public QAbstractTableModel { class EnvironmentShortcutModel: public QAbstractTableModel {
Q_OBJECT
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
explicit EnvironmentShortcutModel(QObject* parent=nullptr); explicit EnvironmentShortcutModel(QObject* parent=nullptr);
@ -30,11 +24,15 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role) override; bool setData(const QModelIndex &index, const QVariant &value, int role) override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
const QList<PEnvironmentShortCut> &shortcuts() const; const QList<PEnvironmentShortcut> &shortcuts() const;
signals:
void shortcutChanged();
public slots:
void shortcutsUpdated();
private: private:
void loadShortCutsOfMenu(const QMenu * menu, QList<QAction*>& globalActions); void loadShortCutsOfMenu(const QMenu * menu, QList<QAction*>& globalActions);
private: private:
QList<PEnvironmentShortCut> mShortcuts; QList<PEnvironmentShortcut> mShortcuts;
}; };
class EnvironmentShortcutWidget : public SettingsWidget class EnvironmentShortcutWidget : public SettingsWidget

View File

@ -15,6 +15,7 @@
#include "editorsnippetwidget.h" #include "editorsnippetwidget.h"
#include "editormiscwidget.h" #include "editormiscwidget.h"
#include "environmentappearencewidget.h" #include "environmentappearencewidget.h"
#include "environmentshortcutwidget.h"
#include "environmentfileassociationwidget.h" #include "environmentfileassociationwidget.h"
#include "executorgeneralwidget.h" #include "executorgeneralwidget.h"
#include "debuggeneralwidget.h" #include "debuggeneralwidget.h"
@ -100,6 +101,10 @@ PSettingsDialog SettingsDialog::optionDialog()
widget->init(); widget->init();
dialog->addWidget(widget); dialog->addWidget(widget);
widget = new EnvironmentShortcutWidget(tr("Shortcuts"),tr("Environment"));
widget->init();
dialog->addWidget(widget);
widget = new CompilerSetOptionWidget(tr("Compiler Set"),tr("Compiler")); widget = new CompilerSetOptionWidget(tr("Compiler Set"),tr("Compiler"));
widget->init(); widget->init();
dialog->addWidget(widget); dialog->addWidget(widget);

View File

@ -0,0 +1,109 @@
#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();
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;
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());
if (item) {
action->setShortcut(QKeySequence::fromString(item->shortcut));
}
}

View File

@ -0,0 +1,38 @@
#ifndef SHORTCUTMANAGER_H
#define SHORTCUTMANAGER_H
#include <QMap>
#include <QObject>
#include <memory>
class QAction;
struct EnvironmentShortcut {
QString name;
QString fullPath;
QString shortcut;
QAction* action;
};
using PEnvironmentShortcut = std::shared_ptr<EnvironmentShortcut>;
class ShortcutManager : public QObject
{
Q_OBJECT
public:
explicit ShortcutManager(QObject *parent = nullptr);
void load();
void save();
void setShortcuts(QList<PEnvironmentShortcut> shortcuts);
void applyTo(QList<QAction*> actions);
void applyTo(QAction* action);
signals:
private:
private:
QMap<QString,PEnvironmentShortcut> mShortcuts;
};
using PShortcutManager = std::shared_ptr<ShortcutManager>;
#endif // SHORTCUTMANAGER_H

View File

@ -34,6 +34,9 @@
#define DEV_LASTOPENS_FILE "lastopens.ini" #define DEV_LASTOPENS_FILE "lastopens.ini"
#define DEV_SYMBOLUSAGE_FILE "symbolusage.json" #define DEV_SYMBOLUSAGE_FILE "symbolusage.json"
#define DEV_CODESNIPPET_FILE "codesnippets.json" #define DEV_CODESNIPPET_FILE "codesnippets.json"
#define DEV_AUTOLINK_FILE "autolink.json"
#define DEV_SHORTCUT_FILE "shortcuts.json"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# define PATH_SENSITIVITY Qt::CaseInsensitive # define PATH_SENSITIVITY Qt::CaseInsensitive
# define PATH_SEPARATOR ";" # define PATH_SEPARATOR ";"