| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "editorsnippetwidget.h" |
| 18 | #include "ui_editorsnippetwidget.h" |
| 19 | #include "../mainwindow.h" |
| 20 | #include "../codesnippetsmanager.h" |
| 21 | #include "../iconsmanager.h" |
| 22 | |
| 23 | #include <QItemSelectionModel> |
| 24 | |
| 25 | EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& group, |
| 26 | QWidget *parent) : |
| 27 | SettingsWidget(name,group,parent), |
| 28 | ui(new Ui::EditorSnippetWidget) |
| 29 | { |
| 30 | mUpdatingCode = false; |
| 31 | ui->setupUi(this); |
| 32 | ui->tblSnippets->setModel(&mModel); |
| 33 | connect(ui->editCode, &Editor::changed, |
| 34 | [this] { |
| 35 | if (mUpdatingCode) |
| 36 | return; |
| 37 | QModelIndex index = ui->tblSnippets->currentIndex(); |
| 38 | if (!index.isValid()) |
| 39 | return; |
| 40 | PCodeSnippet snippet = mModel.snippets()[index.row()]; |
| 41 | snippet->code = ui->editCode->text(); |
| 42 | setSettingsChanged(); |
| 43 | }); |
| 44 | connect(ui->tblSnippets->selectionModel(), &QItemSelectionModel::currentChanged, |
| 45 | [this] { |
| 46 | QModelIndex index = ui->tblSnippets->currentIndex(); |
| 47 | if (!index.isValid()) { |
| 48 | ui->editCode->setEnabled(false); |
| 49 | ui->editCode->document()->clear(); |
| 50 | } else { |
| 51 | mUpdatingCode = true; |
| 52 | ui->editCode->setEnabled(true); |
| 53 | PCodeSnippet snippet = mModel.snippets()[index.row()]; |
| 54 | ui->editCode->document()->setText(snippet->code); |
| 55 | mUpdatingCode = false; |
| 56 | } |
| 57 | }); |
| 58 | connect(ui->editFileTemplate,&Editor::changed, |
| 59 | this, &SettingsWidget::setSettingsChanged); |
| 60 | } |
| 61 | |
| 62 | EditorSnippetWidget::~EditorSnippetWidget() |
| 63 | { |
| 64 | delete ui; |
| 65 | } |
| 66 | |
| 67 | void EditorSnippetWidget::doLoad() |
| 68 | { |
| 69 | mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets()); |
| 70 | ui->editFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newFileTemplate()); |
| 71 | } |
| 72 | |
| 73 | void EditorSnippetWidget::doSave() |
| 74 | { |
| 75 | pMainWindow->codeSnippetManager()->setSnippets(mModel.snippets()); |
| 76 | pMainWindow->codeSnippetManager()->setNewFileTemplate(ui->editFileTemplate->text()); |
| 77 | pMainWindow->codeSnippetManager()->save(); |
| 78 | } |
| 79 | |
| 80 | void EditorSnippetWidget::on_btnAdd_clicked() |
| 81 | { |
| 82 | mModel.addSnippet(QString("" ).arg(getNewFileNumber()), |
| 83 | "" , |
| 84 | "" , |
| 85 | "" , |
| 86 | -1); |
| 87 | ui->tblSnippets->setCurrentIndex(mModel.lastSnippetCaption()); |
| 88 | ui->tblSnippets->edit(mModel.lastSnippetCaption()); |
| 89 | } |
| 90 | |
| 91 | void EditorSnippetWidget::updateIcons(const QSize &/*size*/) |
| 92 | { |
| 93 | pIconsManager->setIcon(ui->btnAdd,IconsManager::ACTION_MISC_ADD); |
| 94 | pIconsManager->setIcon(ui->btnRemove,IconsManager::ACTION_MISC_REMOVE); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void EditorSnippetWidget::on_btnRemove_clicked() |
| 99 | { |
| 100 | QModelIndex index = ui->tblSnippets->currentIndex(); |
| 101 | if (!index.isValid()) |
| 102 | return; |
| 103 | mModel.remove(index.row()); |
| 104 | } |
| 105 | |