work save: code snippet edit

This commit is contained in:
royqh1979 2021-09-30 12:52:22 +08:00
parent b518304f56
commit a0ea72071c
12 changed files with 334 additions and 22 deletions

View File

@ -46,6 +46,7 @@ SOURCES += \
settingsdialog/editorautosavewidget.cpp \
settingsdialog/editorcodecompletionwidget.cpp \
settingsdialog/editormiscwidget.cpp \
settingsdialog/editorsnippetwidget.cpp \
settingsdialog/editortooltipswidget.cpp \
settingsdialog/formattergeneralwidget.cpp \
settingsdialog/projectcompileparamaterswidget.cpp \
@ -146,6 +147,7 @@ HEADERS += \
settingsdialog/editorautosavewidget.h \
settingsdialog/editorcodecompletionwidget.h \
settingsdialog/editormiscwidget.h \
settingsdialog/editorsnippetwidget.h \
settingsdialog/editortooltipswidget.h \
settingsdialog/formattergeneralwidget.h \
settingsdialog/projectcompileparamaterswidget.h \
@ -219,6 +221,7 @@ FORMS += \
settingsdialog/editorautosavewidget.ui \
settingsdialog/editorcodecompletionwidget.ui \
settingsdialog/editormiscwidget.ui \
settingsdialog/editorsnippetwidget.ui \
settingsdialog/editortooltipswidget.ui \
settingsdialog/formattergeneralwidget.ui \
settingsdialog/projectcompileparamaterswidget.ui \

View File

@ -49,8 +49,19 @@ void CodeSnippetsManager::save()
}
}
void CodeSnippetsManager::addSnippet(const QString &caption, const QString &prefix, const QString &code, const QString &description, int menuSection)
const QList<PCodeSnippet> &CodeSnippetsManager::snippets() const
{
return mSnippets;
}
void CodeSnippetsManager::setSnippets(const QList<PCodeSnippet> &newSnippets)
{
mSnippets = newSnippets;
}
void CodeSnippetsModel::addSnippet(const QString &caption, const QString &prefix, const QString &code, const QString &description, int menuSection)
{
beginInsertRows(QModelIndex(),mSnippets.count(),mSnippets.count());
PCodeSnippet snippet = std::make_shared<CodeSnippet>();
snippet->caption = caption;
snippet->prefix = prefix;
@ -58,22 +69,22 @@ void CodeSnippetsManager::addSnippet(const QString &caption, const QString &pref
snippet->desc = description;
snippet->section = menuSection;
mSnippets.append(snippet);
endInsertRows();
}
void CodeSnippetsManager::remove(int index)
void CodeSnippetsModel::remove(int index)
{
Q_ASSERT(index>=0 && index<mSnippets.count());
beginRemoveRows(QModelIndex(),index,index);
mSnippets.removeAt(index);
endRemoveRows();
}
void CodeSnippetsManager::clear()
void CodeSnippetsModel::clear()
{
beginRemoveRows(QModelIndex(),0,mSnippets.count()-1);
mSnippets.clear();
}
const QList<PCodeSnippet> &CodeSnippetsManager::snippets() const
{
return mSnippets;
endRemoveRows();
}
int CodeSnippetsModel::rowCount(const QModelIndex &parent) const
@ -86,9 +97,52 @@ int CodeSnippetsModel::columnCount(const QModelIndex &parent) const
return 4;
}
QVariant CodeSnippetsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role==Qt::DisplayRole) {
int row = index.row();
PCodeSnippet snippet = mSnippets[row];
switch(index.column()) {
case 0:
return snippet->caption;
case 1:
return snippet->prefix;
case 2:
return snippet->desc;
case 3:
return snippet->section;
}
}
return QVariant();
}
bool CodeSnippetsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()) {
return false;
}
if (role==Qt::EditRole) {
int row = index.row();
PCodeSnippet snippet = mSnippets[row];
switch(index.column()) {
case 0:
snippet->caption = value.toString();
return true;
case 1:
snippet->prefix = value.toString();
return true;
case 2:
snippet->desc = value.toString();
return true;
case 3:
snippet->section = value.toInt();
return true;
}
}
return false;
}
QVariant CodeSnippetsModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -107,3 +161,20 @@ QVariant CodeSnippetsModel::headerData(int section, Qt::Orientation orientation,
}
return QVariant();
}
Qt::ItemFlags CodeSnippetsModel::flags(const QModelIndex &) const
{
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
const QList<PCodeSnippet> &CodeSnippetsModel::snippets() const
{
return mSnippets;
}
void CodeSnippetsModel::updateSnippets(const QList<PCodeSnippet> &snippets)
{
beginResetModel();
mSnippets = snippets;
endResetModel();
}

View File

@ -3,6 +3,34 @@
#include <QObject>
#include "parser/parserutils.h"
#include <QAbstractListModel>
class CodeSnippetsModel: public QAbstractListModel {
Q_OBJECT
public:
void addSnippet(
const QString& caption,
const QString& prefix,
const QString& code,
const QString& description,
int menuSection);
void remove(int index);
void clear();
// QAbstractItemModel interface
public:
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<PCodeSnippet> &snippets() const;
void updateSnippets(const QList<PCodeSnippet>& snippets);
private:
QList<PCodeSnippet> mSnippets;
};
class CodeSnippetsManager : public QObject
{
@ -12,21 +40,16 @@ public:
void load();
void save();
void addSnippet(
const QString& caption,
const QString& prefix,
const QString& code,
const QString& description,
int menuSection);
void remove(int index);
void clear();
const QList<PCodeSnippet> &snippets() const;
void setSnippets(const QList<PCodeSnippet> &newSnippets);
signals:
private:
QList<PCodeSnippet> mSnippets;
private:
};
using PCodeSnippetManager = std::shared_ptr<CodeSnippetsManager>;
#endif // CODESNIPPETSMANAGER_H

View File

@ -125,6 +125,8 @@ MainWindow::MainWindow(QWidget *parent)
mSymbolUsageManager = std::make_shared<SymbolUsageManager>();
mSymbolUsageManager->load();
mCodeSnippetManager = std::make_shared<CodeSnippetsManager>();
mCodeSnippetManager->load();
mSearchResultTreeModel = std::make_shared<SearchResultTreeModel>(&mSearchResultModel);
mSearchResultListModel = std::make_shared<SearchResultListModel>(&mSearchResultModel);
mSearchViewDelegate = std::make_shared<SearchResultTreeViewDelegate>(mSearchResultTreeModel);
@ -3748,6 +3750,11 @@ void MainWindow::on_classBrowser_doubleClicked(const QModelIndex &index)
}
}
PCodeSnippetManager &MainWindow::codeSnippetManager()
{
return mCodeSnippetManager;
}
PSymbolUsageManager &MainWindow::symbolUsageManager()
{
return mSymbolUsageManager;

View File

@ -12,6 +12,7 @@
#include "widgets/functiontooltipwidget.h"
#include "caretlist.h"
#include "symbolusagemanager.h"
#include "codesnippetsmanager.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -116,6 +117,8 @@ public:
PSymbolUsageManager &symbolUsageManager();
PCodeSnippetManager &codeSnippetManager();
public slots:
void onCompileLog(const QString& msg);
void onCompileIssue(PCompileIssue issue);
@ -383,6 +386,7 @@ private:
ClassBrowserModel mClassBrowserModel;
std::shared_ptr<QHash<StatementKind, QColor>> mStatementColors;
PSymbolUsageManager mSymbolUsageManager;
PCodeSnippetManager mCodeSnippetManager;
bool mCheckSyntaxInBack;
bool mOpenClosingBottomPanel;

View File

@ -13,7 +13,7 @@ struct CodeSnippet {
int section; //Section in the menu
};
using PCodeIns = std::shared_ptr<CodeSnippet>;
using PCodeSnippet = std::shared_ptr<CodeSnippet>;
// preprocess/ macro define
struct Define {

View File

@ -0,0 +1,70 @@
#include "editorsnippetwidget.h"
#include "ui_editorsnippetwidget.h"
#include "../mainwindow.h"
#include "../codesnippetsmanager.h"
EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& group,
QWidget *parent) :
SettingsWidget(name,group,parent),
ui(new Ui::EditorSnippetWidget)
{
mUpdatingCode = false;
ui->setupUi(this);
ui->tblSnippets->setModel(&mModel);
connect(ui->editCode, &Editor::changed,
[this] {
if (mUpdatingCode)
return;
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid())
return;
PCodeSnippet snippet = mModel.snippets()[index.row()];
snippet->code = ui->editCode->lines()->text();
});
}
EditorSnippetWidget::~EditorSnippetWidget()
{
delete ui;
}
void EditorSnippetWidget::doLoad()
{
mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets());
}
void EditorSnippetWidget::doSave()
{
pMainWindow->codeSnippetManager()->setSnippets(mModel.snippets());
pMainWindow->codeSnippetManager()->save();
}
void EditorSnippetWidget::on_tblSnippets_clicked(const QModelIndex &index)
{
if (!index.isValid())
return;
mUpdatingCode = true;
PCodeSnippet snippet = mModel.snippets()[index.row()];
ui->editCode->lines()->setText(snippet->code);
mUpdatingCode = false;
}
void EditorSnippetWidget::on_btnAdd_triggered(QAction *arg1)
{
mModel.addSnippet(QString("Code %1").arg(getNewFileNumber()),
"",
"",
"",
-1);
}
void EditorSnippetWidget::on_btnRemove_triggered(QAction *arg1)
{
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid())
return;
mModel.remove(index.row());
}

View File

@ -0,0 +1,35 @@
#ifndef EDITORSNIPPETWIDGET_H
#define EDITORSNIPPETWIDGET_H
#include <QWidget>
#include "settingswidget.h"
#include "../codesnippetsmanager.h"
namespace Ui {
class EditorSnippetWidget;
}
class EditorSnippetWidget : public SettingsWidget
{
Q_OBJECT
public:
explicit EditorSnippetWidget(const QString& name, const QString& group, QWidget *parent = nullptr);
~EditorSnippetWidget();
private:
Ui::EditorSnippetWidget *ui;
CodeSnippetsModel mModel;
private:
bool mUpdatingCode;
// SettingsWidget interface
protected:
void doLoad() override;
void doSave() override;
private slots:
void on_tblSnippets_clicked(const QModelIndex &index);
void on_btnAdd_triggered(QAction *arg1);
void on_btnRemove_triggered(QAction *arg1);
};
#endif // EDITORSNIPPETWIDGET_H

View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EditorSnippetWidget</class>
<widget class="QWidget" name="EditorSnippetWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>932</width>
<height>574</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QTableView" name="tblSnippets"/>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QToolButton" name="btnAdd">
<property name="text">
<string>Add</string>
</property>
<property name="icon">
<iconset resource="../icons.qrc">
<normaloff>:/icons/images/newlook24/002-add.png</normaloff>:/icons/images/newlook24/002-add.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnRemove">
<property name="text">
<string>Remove</string>
</property>
<property name="icon">
<iconset resource="../icons.qrc">
<normaloff>:/icons/images/newlook24/008-close.png</normaloff>:/icons/images/newlook24/008-close.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>159</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="Editor" name="editCode">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Editor</class>
<extends>QFrame</extends>
<header location="global">editor.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../icons.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -12,6 +12,7 @@
#include "editorsymbolcompletionwidget.h"
#include "editortooltipswidget.h"
#include "editorautosavewidget.h"
#include "editorsnippetwidget.h"
#include "editormiscwidget.h"
#include "environmentappearencewidget.h"
#include "executorgeneralwidget.h"
@ -126,6 +127,10 @@ PSettingsDialog SettingsDialog::optionDialog()
widget->init();
dialog->addWidget(widget);
widget = new EditorSnippetWidget(tr("Snippet"),tr("Editor"));
widget->init();
dialog->addWidget(widget);
widget = new EditorSyntaxCheckWidget(tr("Auto Syntax Checking"),tr("Editor"));
widget->init();
dialog->addWidget(widget);

View File

@ -471,7 +471,7 @@ void CodeCompletionPopup::getCompletionFor(const QString &fileName, const QStrin
if (mShowCodeIns) {
//add custom code templates
foreach (const PCodeIns& codeIn,mCodeInsList) {
foreach (const PCodeSnippet& codeIn,mCodeInsList) {
PStatement statement = std::make_shared<Statement>();
statement->command = codeIn->prefix;
statement->kind = StatementKind::skUserCodeIn;

View File

@ -77,7 +77,7 @@ private:
private:
CodeCompletionListView * mListView;
CodeCompletionListModel* mModel;
QList<PCodeIns> mCodeInsList; //(Code template list)
QList<PCodeSnippet> mCodeInsList; //(Code template list)
//QList<PStatement> mCodeInsStatements; //temporary (user code template) statements created when show code suggestion
StatementList mFullCompletionStatementList;
StatementList mCompletionStatementList;