From d88bab1e20cbfb3bf0b5f240bff0a6032f9ff0e9 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Wed, 6 Oct 2021 12:19:46 +0800 Subject: [PATCH] work save: implement shortcut configurations --- NEWS.md | 1 + RedPandaIDE/RedPandaIDE.pro | 3 + RedPandaIDE/mainwindow.cpp | 5 + RedPandaIDE/mainwindow.h | 2 + .../environmentshortcutwidget.cpp | 131 ++++++++++++++++++ .../environmentshortcutwidget.h | 59 ++++++++ .../environmentshortcutwidget.ui | 28 ++++ 7 files changed, 229 insertions(+) create mode 100644 RedPandaIDE/settingsdialog/environmentshortcutwidget.cpp create mode 100644 RedPandaIDE/settingsdialog/environmentshortcutwidget.h create mode 100644 RedPandaIDE/settingsdialog/environmentshortcutwidget.ui diff --git a/NEWS.md b/NEWS.md index 7df4f828..efb1fe73 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,6 +23,7 @@ Version 0.6.0 - fix: add mutex lock to prevent rare conditions when editor is modifying and the content is read - fix: makefile generated for static / dynamic library projects not right - fix: editors disappeared when close/close all + - implement: config shortcuts Version 0.5.0 - enhancement: support C++ using type alias; diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index 0e7709c9..2c0f3cdd 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -49,6 +49,7 @@ SOURCES += \ settingsdialog/editorsnippetwidget.cpp \ settingsdialog/editortooltipswidget.cpp \ settingsdialog/environmentfileassociationwidget.cpp \ + settingsdialog/environmentshortcutwidget.cpp \ settingsdialog/formattergeneralwidget.cpp \ settingsdialog/projectcompileparamaterswidget.cpp \ settingsdialog/projectcompilerwidget.cpp \ @@ -153,6 +154,7 @@ HEADERS += \ settingsdialog/editorsnippetwidget.h \ settingsdialog/editortooltipswidget.h \ settingsdialog/environmentfileassociationwidget.h \ + settingsdialog/environmentshortcutwidget.h \ settingsdialog/formattergeneralwidget.h \ settingsdialog/projectcompileparamaterswidget.h \ settingsdialog/projectcompilerwidget.h \ @@ -230,6 +232,7 @@ FORMS += \ settingsdialog/editorsnippetwidget.ui \ settingsdialog/editortooltipswidget.ui \ settingsdialog/environmentfileassociationwidget.ui \ + settingsdialog/environmentshortcutwidget.ui \ settingsdialog/formattergeneralwidget.ui \ settingsdialog/projectcompileparamaterswidget.ui \ settingsdialog/projectcompilerwidget.ui \ diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 93b46947..e5209b18 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -4052,6 +4052,11 @@ void MainWindow::showSearchReplacePanel(bool show) mSearchResultTreeModel->setSelectable(show); } +Ui::MainWindow *MainWindow::mainWidget() const +{ + return ui; +} + void MainWindow::on_btnReplace_clicked() { diff --git a/RedPandaIDE/mainwindow.h b/RedPandaIDE/mainwindow.h index b796d334..edcddcb3 100644 --- a/RedPandaIDE/mainwindow.h +++ b/RedPandaIDE/mainwindow.h @@ -97,6 +97,8 @@ public: QPlainTextEdit* txtLocals(); + Ui::MainWindow* mainWidget() const; + CPUDialog *cpuDialog() const; Debugger *debugger() const; diff --git a/RedPandaIDE/settingsdialog/environmentshortcutwidget.cpp b/RedPandaIDE/settingsdialog/environmentshortcutwidget.cpp new file mode 100644 index 00000000..12b2bb1f --- /dev/null +++ b/RedPandaIDE/settingsdialog/environmentshortcutwidget.cpp @@ -0,0 +1,131 @@ +#include "environmentshortcutwidget.h" +#include "ui_environmentshortcutwidget.h" +#include "../mainwindow.h" +#include "../ui_mainwindow.h" + +EnvironmentShortcutWidget::EnvironmentShortcutWidget(const QString& name, const QString& group, QWidget *parent) : + SettingsWidget(name,group,parent), + ui(new Ui::EnvironmentShortcutWidget) +{ + ui->setupUi(this); + ui->tblShortcut->setModel(&mModel); +} + +EnvironmentShortcutWidget::~EnvironmentShortcutWidget() +{ + delete ui; +} + +void EnvironmentShortcutWidget::doLoad() +{ + mModel.reload(); +} + +void EnvironmentShortcutWidget::doSave() +{ + foreach (const PEnvironmentShortCut& shortcut, mModel.shortcuts()) { + shortcut->action->setShortcut(QKeySequence::fromString(shortcut->shortcut)); + shortcut->shortcut = shortcut->action->shortcut().toString(); + } +} + +EnvironmentShortcutModel::EnvironmentShortcutModel(QObject *parent):QAbstractTableModel(parent) +{ + +} + +void EnvironmentShortcutModel::reload() +{ + beginResetModel(); + mShortcuts.clear(); + QList actions = pMainWindow->findChildren(QString(),Qt::FindDirectChildrenOnly); + QList menus = pMainWindow->mainWidget()->menubar->findChildren(); + foreach( const QMenu* menu, menus) { + loadShortCutsOfMenu(menu, actions); + } + endResetModel(); +} + +int EnvironmentShortcutModel::rowCount(const QModelIndex &parent) const +{ + return mShortcuts.count(); +} + +int EnvironmentShortcutModel::columnCount(const QModelIndex &parent) const +{ + return 2; +} + +QVariant EnvironmentShortcutModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + return QVariant(); + } + if (role==Qt::DisplayRole || role == Qt::EditRole) { + PEnvironmentShortCut item = mShortcuts[index.row()]; + switch( index.column()) { + case 0: + return item->fullPath; + case 1: + return item->shortcut; + } + } + return QVariant(); +} + +bool EnvironmentShortcutModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!index.isValid()) { + return false; + } + if (role == Qt::EditRole) { + if (index.column()!=1) + return false; + PEnvironmentShortCut item = mShortcuts[index.row()]; + item->shortcut = value.toString(); + return true; + } + return false; +} + +QVariant EnvironmentShortcutModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal) { + if (role == Qt::DisplayRole) { + switch(section) { + case 0: + return tr("Function"); + case 1: + return tr("Shortcut"); + } + } + } + return QVariant(); +} + +Qt::ItemFlags EnvironmentShortcutModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flags = Qt::ItemIsEnabled; + if (index.isValid() && index.column()==1) { + flags.setFlag(Qt::ItemIsEditable); + } + return flags; +} + +const QList &EnvironmentShortcutModel::shortcuts() const +{ + return mShortcuts; +} + +void EnvironmentShortcutModel::loadShortCutsOfMenu(const QMenu *menu, QList &globalActions) +{ + QList actions = menu->actions(); + foreach (QAction* action,actions) { + PEnvironmentShortCut item = std::make_shared(); + item->name = action->objectName(); + item->fullPath = QString("%1 > %2").arg(menu->title(),action->text()); + item->action = action; + item->shortcut = action->shortcut().toString().trimmed(); + globalActions.removeAll(action); + } +} diff --git a/RedPandaIDE/settingsdialog/environmentshortcutwidget.h b/RedPandaIDE/settingsdialog/environmentshortcutwidget.h new file mode 100644 index 00000000..3dcfac3e --- /dev/null +++ b/RedPandaIDE/settingsdialog/environmentshortcutwidget.h @@ -0,0 +1,59 @@ +#ifndef ENVIRONMENTSHORTCUTWIDGET_H +#define ENVIRONMENTSHORTCUTWIDGET_H + +#include +#include +#include "settingswidget.h" + +namespace Ui { +class EnvironmentShortcutWidget; +} + +struct EnvironmentShortCut { + QString name; + QString fullPath; + QString shortcut; + QAction* action; +}; + +using PEnvironmentShortCut = std::shared_ptr; +class QMenu; +class EnvironmentShortcutModel: public QAbstractTableModel { + // QAbstractItemModel interface +public: + explicit EnvironmentShortcutModel(QObject* parent=nullptr); + void reload(); + + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role) override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + const QList &shortcuts() const; +private: + void loadShortCutsOfMenu(const QMenu * menu, QList& globalActions); +private: + QList mShortcuts; +}; + +class EnvironmentShortcutWidget : public SettingsWidget +{ + Q_OBJECT + +public: + explicit EnvironmentShortcutWidget(const QString& name, const QString& group, QWidget *parent = nullptr); + ~EnvironmentShortcutWidget(); + +private: + Ui::EnvironmentShortcutWidget *ui; + + // SettingsWidget interface +protected: + void doLoad() override; + void doSave() override; +private: + EnvironmentShortcutModel mModel; +}; + +#endif // ENVIRONMENTSHORTCUTWIDGET_H diff --git a/RedPandaIDE/settingsdialog/environmentshortcutwidget.ui b/RedPandaIDE/settingsdialog/environmentshortcutwidget.ui new file mode 100644 index 00000000..f74abf2f --- /dev/null +++ b/RedPandaIDE/settingsdialog/environmentshortcutwidget.ui @@ -0,0 +1,28 @@ + + + EnvironmentShortcutWidget + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + + true + + + + + + + +