diff --git a/RedPandaIDE/RedPandaIDE.pro b/RedPandaIDE/RedPandaIDE.pro index 2d9ca9b2..d1b7446e 100644 --- a/RedPandaIDE/RedPandaIDE.pro +++ b/RedPandaIDE/RedPandaIDE.pro @@ -33,6 +33,7 @@ SOURCES += \ qsynedit/Search.cpp \ qsynedit/SearchBase.cpp \ qsynedit/SearchRegex.cpp \ + settingsdialog/compilerautolinkwidget.cpp \ settingsdialog/debuggeneralwidget.cpp \ settingsdialog/editorautosavewidget.cpp \ settingsdialog/editorcodecompletionwidget.cpp \ @@ -109,6 +110,7 @@ HEADERS += \ qsynedit/Search.h \ qsynedit/SearchBase.h \ qsynedit/SearchRegex.h \ + settingsdialog/compilerautolinkwidget.h \ settingsdialog/debuggeneralwidget.h \ settingsdialog/editorautosavewidget.h \ settingsdialog/editorcodecompletionwidget.h \ @@ -166,6 +168,7 @@ HEADERS += \ widgets/searchresultview.h FORMS += \ + settingsdialog/compilerautolinkwidget.ui \ settingsdialog/debuggeneralwidget.ui \ settingsdialog/editorautosavewidget.ui \ settingsdialog/editorcodecompletionwidget.ui \ diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 6e32cd98..873ebb60 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -420,6 +420,16 @@ void Settings::Editor::setAutoSaveStrategy(AutoSaveStrategy newAutoSaveStrategy) mAutoSaveStrategy = newAutoSaveStrategy; } +bool Settings::Editor::enableAutolink() const +{ + return mEnableAutolink; +} + +void Settings::Editor::setEnableAutolink(bool newEnableAutolink) +{ + mEnableAutolink = newEnableAutolink; +} + AutoSaveTarget Settings::Editor::autoSaveTarget() const { return mAutoSaveTarget; @@ -891,6 +901,9 @@ void Settings::Editor::doSave() saveValue("auto_save_target",mAutoSaveTarget); saveValue("auto_save_strategy",mAutoSaveStrategy); + //auto link + saveValue("enable_autolink",mEnableAutolink); + //misc saveValue("default_encoding",mDefaultEncoding); saveValue("readonly_system_header",mReadOnlySytemHeader); @@ -983,6 +996,9 @@ void Settings::Editor::doLoad() mAutoSaveStrategy = static_cast( intValue("auto_save_strategy",AutoSaveStrategy::assOverwrite)); + //auto link + mEnableAutolink = boolValue("enable_autolink",true); + //misc mDefaultEncoding = value("default_encoding", ENCODING_SYSTEM_DEFAULT).toByteArray(); mReadOnlySytemHeader = boolValue("readonly_system_header",true); diff --git a/RedPandaIDE/settings.h b/RedPandaIDE/settings.h index 3c54fe2e..3a5e8472 100644 --- a/RedPandaIDE/settings.h +++ b/RedPandaIDE/settings.h @@ -285,6 +285,9 @@ public: AutoSaveStrategy autoSaveStrategy() const; void setAutoSaveStrategy(AutoSaveStrategy newAutoSaveStrategy); + bool enableAutolink() const; + void setEnableAutolink(bool newEnableAutolink); + private: //General // indents @@ -366,6 +369,9 @@ public: enum AutoSaveTarget mAutoSaveTarget; enum AutoSaveStrategy mAutoSaveStrategy; + //auto link + bool mEnableAutolink; + //Misc QByteArray mDefaultEncoding; bool mReadOnlySytemHeader; diff --git a/RedPandaIDE/settingsdialog/compilerautolinkwidget.cpp b/RedPandaIDE/settingsdialog/compilerautolinkwidget.cpp new file mode 100644 index 00000000..36c596bf --- /dev/null +++ b/RedPandaIDE/settingsdialog/compilerautolinkwidget.cpp @@ -0,0 +1,148 @@ +#include "compilerautolinkwidget.h" +#include "ui_compilerautolinkwidget.h" +#include "../mainwindow.h" +#include "../settings.h" + +#include + +CompilerAutolinkWidget::CompilerAutolinkWidget(const QString& name, const QString& group, QWidget* parent) : + SettingsWidget(name,group,parent), + ui(new Ui::CompilerAutolinkWidget) +{ + ui->setupUi(this); + ui->tblAutolinks->setModel(&mModel); +} + +CompilerAutolinkWidget::~CompilerAutolinkWidget() +{ + delete ui; +} + +void CompilerAutolinkWidget::doLoad() +{ + ui->grpAutolink->setChecked(pSettings->editor().enableAutolink()); + mModel.setLinks(pAutolinkManager->links()); +} + +void CompilerAutolinkWidget::doSave() +{ + pSettings->editor().setEnableAutolink(ui->grpAutolink->isChecked()); + pSettings->editor().save(); + pAutolinkManager->clear(); + auto iter = mModel.links().cbegin(); + while (iter!=mModel.links().cend()) { + PAutolink link = iter.value(); + pAutolinkManager->setLink( + link->header, + link->linkOption + ); + iter++; + } + pAutolinkManager->save(); +} + +AutolinkModel::AutolinkModel(QObject *parent):QAbstractTableModel(parent) +{ + +} + +int AutolinkModel::rowCount(const QModelIndex &) const +{ + return mLinks.count(); +} + +int AutolinkModel::columnCount(const QModelIndex &) const +{ + return 2; +} + +QVariant AutolinkModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch(section) { + case 0: + return tr("Header"); + case 1: + return tr("Link options"); + } + } + return QVariant(); +} + +QVariant AutolinkModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + if (role == Qt::DisplayRole || role == Qt::EditRole) { + int row = index.row(); + QList links = mLinks.values(); + if (row<0 || row>=links.count()) + return QVariant(); + PAutolink link = links[row]; + switch(index.column()) { + case 0: + return link->header; + case 1: + return link->linkOption; + } + } + return QVariant(); +} + +bool AutolinkModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (!index.isValid()) + return false; + if (role == Qt::EditRole) { + int row = index.row(); + QList links = mLinks.values(); + if (row<0 || row>=links.count()) + return false; + PAutolink link = links[row]; + QString s=value.toString(); + if (index.column() == 0) { + if (s.isEmpty()) + return false; + PAutolink oldLink = mLinks.value(s,PAutolink()); + if (oldLink) { + QMessageBox::warning(pMainWindow, + tr("Header exists"), + tr("Header already exists."), + QMessageBox::Yes); + return false; + } + mLinks.remove(link->header); + PAutolink newLink = std::make_shared(); + newLink->header = s; + newLink->linkOption = link->linkOption; + mLinks.insert(newLink->header,newLink); + return true; + } else if (index.column() == 1) { + PAutolink newLink = std::make_shared(); + newLink->header = link->header; + newLink->linkOption = s; + mLinks.insert(newLink->header,newLink); + return true; + } + } + return false; +} + +Qt::ItemFlags AutolinkModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flags = Qt::NoItemFlags; + if (index.isValid()) { + flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable ; + } + return flags; +} + +const QMap &AutolinkModel::links() const +{ + return mLinks; +} + +void AutolinkModel::setLinks(const QMap &newLinks) +{ + mLinks = newLinks; +} diff --git a/RedPandaIDE/settingsdialog/compilerautolinkwidget.h b/RedPandaIDE/settingsdialog/compilerautolinkwidget.h new file mode 100644 index 00000000..c6a3fc8c --- /dev/null +++ b/RedPandaIDE/settingsdialog/compilerautolinkwidget.h @@ -0,0 +1,52 @@ +#ifndef COMPILERAUTOLINKWIDGET_H +#define COMPILERAUTOLINKWIDGET_H + +#include +#include +#include "settingswidget.h" +#include "../autolinkmanager.h" + +namespace Ui { +class CompilerAutolinkWidget; +} + +class AutolinkModel: public QAbstractTableModel { + Q_OBJECT +public: + explicit AutolinkModel(QObject* parent=nullptr); + + // QAbstractItemModel interface +public: + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + QVariant data(const QModelIndex &index, int role) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role) override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + const QMap &links() const; + void setLinks(const QMap &newLinks); + +private: + QMap mLinks; +}; + +class CompilerAutolinkWidget : public SettingsWidget +{ + Q_OBJECT + +public: + explicit CompilerAutolinkWidget(const QString& name, const QString& group, QWidget *parent = nullptr); + ~CompilerAutolinkWidget(); + +private: + AutolinkModel mModel; +private: + Ui::CompilerAutolinkWidget *ui; + + // SettingsWidget interface +protected: + void doLoad() override; + void doSave() override; +}; + +#endif // COMPILERAUTOLINKWIDGET_H diff --git a/RedPandaIDE/settingsdialog/compilerautolinkwidget.ui b/RedPandaIDE/settingsdialog/compilerautolinkwidget.ui new file mode 100644 index 00000000..cfede6ce --- /dev/null +++ b/RedPandaIDE/settingsdialog/compilerautolinkwidget.ui @@ -0,0 +1,46 @@ + + + CompilerAutolinkWidget + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + + Enable auto link + + + true + + + + + + Qt::ElideNone + + + true + + + true + + + + + + + + + + + diff --git a/RedPandaIDE/settingsdialog/settingsdialog.cpp b/RedPandaIDE/settingsdialog/settingsdialog.cpp index 72c9bffa..16a417a5 100644 --- a/RedPandaIDE/settingsdialog/settingsdialog.cpp +++ b/RedPandaIDE/settingsdialog/settingsdialog.cpp @@ -2,6 +2,7 @@ #include "ui_settingsdialog.h" #include "settingswidget.h" #include "compilersetoptionwidget.h" +#include "compilerautolinkwidget.h" #include "editorgeneralwidget.h" #include "editorfontwidget.h" #include "editorclipboardwidget.h" @@ -39,6 +40,10 @@ SettingsDialog::SettingsDialog(QWidget *parent) : pCompilerSetOptionWidget->init(); addWidget(pCompilerSetOptionWidget); + pCompilerAutolinkWidget = new CompilerAutolinkWidget(tr("Auto Link"),tr("Compiler")); + pCompilerAutolinkWidget->init(); + addWidget(pCompilerAutolinkWidget); + pEditorGeneralWidget = new EditorGeneralWidget(tr("General"),tr("Editor")); pEditorGeneralWidget->init(); addWidget(pEditorGeneralWidget); diff --git a/RedPandaIDE/settingsdialog/settingsdialog.h b/RedPandaIDE/settingsdialog/settingsdialog.h index 974ac775..a86e54e8 100644 --- a/RedPandaIDE/settingsdialog/settingsdialog.h +++ b/RedPandaIDE/settingsdialog/settingsdialog.h @@ -11,6 +11,7 @@ class SettingsDialog; } class CompilerSetOptionWidget; +class CompilerAutolinkWidget; class EditorGeneralWidget; class EditorFontWidget; class EditorClipboardWidget; @@ -54,6 +55,7 @@ private: QStandardItemModel model; CompilerSetOptionWidget *pCompilerSetOptionWidget; + CompilerAutolinkWidget *pCompilerAutolinkWidget; EditorGeneralWidget *pEditorGeneralWidget; EditorFontWidget *pEditorFontWidget; EditorClipboardWidget *pEditorClipboardWidget; diff --git a/RedPandaIDE/settingsdialog/settingswidget.cpp b/RedPandaIDE/settingsdialog/settingswidget.cpp index c81583d0..7b14a2db 100644 --- a/RedPandaIDE/settingsdialog/settingswidget.cpp +++ b/RedPandaIDE/settingsdialog/settingswidget.cpp @@ -43,6 +43,7 @@ void SettingsWidget::connectAbstractItemView(QAbstractItemView *pView) connect(pView->model(),&QAbstractItemModel::rowsRemoved,this,&SettingsWidget::setSettingsChanged); connect(pView->model(),&QAbstractItemModel::dataChanged,this,&SettingsWidget::setSettingsChanged); connect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged); + connect(pView->model(),&QAbstractItemModel::modelReset,this,&SettingsWidget::setSettingsChanged); } void SettingsWidget::disconnectAbstractItemView(QAbstractItemView *pView)