work save: implement shortcut configurations

This commit is contained in:
royqh1979@gmail.com 2021-10-06 12:19:46 +08:00
parent f75c9f26cf
commit d88bab1e20
7 changed files with 229 additions and 0 deletions

View File

@ -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: 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: makefile generated for static / dynamic library projects not right
- fix: editors disappeared when close/close all - fix: editors disappeared when close/close all
- implement: config shortcuts
Version 0.5.0 Version 0.5.0
- enhancement: support C++ using type alias; - enhancement: support C++ using type alias;

View File

@ -49,6 +49,7 @@ SOURCES += \
settingsdialog/editorsnippetwidget.cpp \ settingsdialog/editorsnippetwidget.cpp \
settingsdialog/editortooltipswidget.cpp \ settingsdialog/editortooltipswidget.cpp \
settingsdialog/environmentfileassociationwidget.cpp \ settingsdialog/environmentfileassociationwidget.cpp \
settingsdialog/environmentshortcutwidget.cpp \
settingsdialog/formattergeneralwidget.cpp \ settingsdialog/formattergeneralwidget.cpp \
settingsdialog/projectcompileparamaterswidget.cpp \ settingsdialog/projectcompileparamaterswidget.cpp \
settingsdialog/projectcompilerwidget.cpp \ settingsdialog/projectcompilerwidget.cpp \
@ -153,6 +154,7 @@ HEADERS += \
settingsdialog/editorsnippetwidget.h \ settingsdialog/editorsnippetwidget.h \
settingsdialog/editortooltipswidget.h \ settingsdialog/editortooltipswidget.h \
settingsdialog/environmentfileassociationwidget.h \ settingsdialog/environmentfileassociationwidget.h \
settingsdialog/environmentshortcutwidget.h \
settingsdialog/formattergeneralwidget.h \ settingsdialog/formattergeneralwidget.h \
settingsdialog/projectcompileparamaterswidget.h \ settingsdialog/projectcompileparamaterswidget.h \
settingsdialog/projectcompilerwidget.h \ settingsdialog/projectcompilerwidget.h \
@ -230,6 +232,7 @@ FORMS += \
settingsdialog/editorsnippetwidget.ui \ settingsdialog/editorsnippetwidget.ui \
settingsdialog/editortooltipswidget.ui \ settingsdialog/editortooltipswidget.ui \
settingsdialog/environmentfileassociationwidget.ui \ settingsdialog/environmentfileassociationwidget.ui \
settingsdialog/environmentshortcutwidget.ui \
settingsdialog/formattergeneralwidget.ui \ settingsdialog/formattergeneralwidget.ui \
settingsdialog/projectcompileparamaterswidget.ui \ settingsdialog/projectcompileparamaterswidget.ui \
settingsdialog/projectcompilerwidget.ui \ settingsdialog/projectcompilerwidget.ui \

View File

@ -4052,6 +4052,11 @@ void MainWindow::showSearchReplacePanel(bool show)
mSearchResultTreeModel->setSelectable(show); mSearchResultTreeModel->setSelectable(show);
} }
Ui::MainWindow *MainWindow::mainWidget() const
{
return ui;
}
void MainWindow::on_btnReplace_clicked() void MainWindow::on_btnReplace_clicked()
{ {

View File

@ -97,6 +97,8 @@ public:
QPlainTextEdit* txtLocals(); QPlainTextEdit* txtLocals();
Ui::MainWindow* mainWidget() const;
CPUDialog *cpuDialog() const; CPUDialog *cpuDialog() const;
Debugger *debugger() const; Debugger *debugger() const;

View File

@ -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<QAction*> actions = pMainWindow->findChildren<QAction*>(QString(),Qt::FindDirectChildrenOnly);
QList<QMenu*> menus = pMainWindow->mainWidget()->menubar->findChildren<QMenu*>();
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<PEnvironmentShortCut> &EnvironmentShortcutModel::shortcuts() const
{
return mShortcuts;
}
void EnvironmentShortcutModel::loadShortCutsOfMenu(const QMenu *menu, QList<QAction *> &globalActions)
{
QList<QAction*> actions = menu->actions();
foreach (QAction* action,actions) {
PEnvironmentShortCut item = std::make_shared<EnvironmentShortCut>();
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);
}
}

View File

@ -0,0 +1,59 @@
#ifndef ENVIRONMENTSHORTCUTWIDGET_H
#define ENVIRONMENTSHORTCUTWIDGET_H
#include <QAbstractTableModel>
#include <QWidget>
#include "settingswidget.h"
namespace Ui {
class EnvironmentShortcutWidget;
}
struct EnvironmentShortCut {
QString name;
QString fullPath;
QString shortcut;
QAction* action;
};
using PEnvironmentShortCut = std::shared_ptr<EnvironmentShortCut>;
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<PEnvironmentShortCut> &shortcuts() const;
private:
void loadShortCutsOfMenu(const QMenu * menu, QList<QAction*>& globalActions);
private:
QList<PEnvironmentShortCut> 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

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EnvironmentShortcutWidget</class>
<widget class="QWidget" name="EnvironmentShortcutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableView" name="tblShortcut">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>