RedPanda-CPP/RedPandaIDE/settingsdialog/editorsnippetwidget.cpp

118 lines
4.3 KiB
C++
Raw Permalink Normal View History

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/>.
*/
2024-04-05 09:42:00 +08:00
#include <qsynedit/document.h>
2021-09-30 12:52:22 +08:00
#include "editorsnippetwidget.h"
#include "ui_editorsnippetwidget.h"
#include "../mainwindow.h"
#include "../codesnippetsmanager.h"
#include "../iconsmanager.h"
#include "../syntaxermanager.h"
2021-09-30 12:52:22 +08:00
2021-09-30 20:10:48 +08:00
#include <QItemSelectionModel>
2021-09-30 12:52:22 +08:00
EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& group,
QWidget *parent) :
SettingsWidget(name,group,parent),
ui(new Ui::EditorSnippetWidget)
{
mUpdatingCode = false;
ui->setupUi(this);
QItemSelectionModel* m=ui->tblSnippets->selectionModel();
2021-09-30 12:52:22 +08:00
ui->tblSnippets->setModel(&mModel);
delete m;
2021-09-30 12:52:22 +08:00
connect(ui->editCode, &Editor::changed,
[this] {
if (mUpdatingCode)
return;
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid())
return;
PCodeSnippet snippet = mModel.snippets()[index.row()];
2021-10-07 07:52:20 +08:00
snippet->code = ui->editCode->text();
2021-09-30 20:10:48 +08:00
setSettingsChanged();
});
connect(ui->tblSnippets->selectionModel(), &QItemSelectionModel::currentChanged,
[this] {
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid()) {
ui->editCode->setEnabled(false);
ui->editCode->document()->clear();
2021-09-30 20:10:48 +08:00
} else {
mUpdatingCode = true;
ui->editCode->setEnabled(true);
PCodeSnippet snippet = mModel.snippets()[index.row()];
ui->editCode->document()->setText(snippet->code);
2021-09-30 20:10:48 +08:00
mUpdatingCode = false;
}
2021-09-30 12:52:22 +08:00
});
connect(ui->editCppFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
connect(ui->editCFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
connect(ui->editGASFileTemplate,&Editor::changed,
this, &SettingsWidget::setSettingsChanged);
ui->editGASFileTemplate->setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::ATTAssembly));
2021-09-30 12:52:22 +08:00
}
EditorSnippetWidget::~EditorSnippetWidget()
{
delete ui;
}
void EditorSnippetWidget::doLoad()
{
mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets());
ui->editCppFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newCppFileTemplate());
ui->editCFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newCFileTemplate());
ui->editGASFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newGASFileTemplate());
2021-09-30 12:52:22 +08:00
}
void EditorSnippetWidget::doSave()
{
pMainWindow->codeSnippetManager()->setSnippets(mModel.snippets());
pMainWindow->codeSnippetManager()->setNewCppFileTemplate(ui->editCppFileTemplate->text());
pMainWindow->codeSnippetManager()->setNewCFileTemplate(ui->editCFileTemplate->text());
pMainWindow->codeSnippetManager()->setNewGASFileTemplate(ui->editGASFileTemplate->text());
2021-09-30 12:52:22 +08:00
pMainWindow->codeSnippetManager()->save();
}
2021-09-30 20:10:48 +08:00
void EditorSnippetWidget::on_btnAdd_clicked()
2021-09-30 12:52:22 +08:00
{
2021-09-30 20:10:48 +08:00
mModel.addSnippet(QString("").arg(getNewFileNumber()),
2021-09-30 12:52:22 +08:00
"",
"",
"",
-1);
2021-09-30 20:10:48 +08:00
ui->tblSnippets->setCurrentIndex(mModel.lastSnippetCaption());
ui->tblSnippets->edit(mModel.lastSnippetCaption());
2021-09-30 12:52:22 +08:00
}
2022-04-25 12:43:23 +08:00
void EditorSnippetWidget::updateIcons(const QSize &/*size*/)
{
pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD);
pIconsManager->setIcon(ui->btnRemove,IconsManager::ACTION_MISC_REMOVE);
}
2021-09-30 12:52:22 +08:00
2021-09-30 20:10:48 +08:00
void EditorSnippetWidget::on_btnRemove_clicked()
2021-09-30 12:52:22 +08:00
{
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid())
return;
mModel.remove(index.row());
}