From 827fc19d39db8d4fb94ef710642dc655ea92e127 Mon Sep 17 00:00:00 2001 From: octaeder <102688820+octaeder@users.noreply.github.com> Date: Mon, 25 Dec 2023 18:16:45 +0100 Subject: [PATCH] remove line with macro type from macro editor macro type is now defined by radio buttons alone. todo so type is now part of user macro and exported Json. --- src/usermacro.cpp | 40 +++++++++++++++-- src/usermacro.h | 5 ++- src/usermenudialog.cpp | 100 +++++++++++++++++++---------------------- src/usermenudialog.h | 10 ++--- 4 files changed, 89 insertions(+), 66 deletions(-) diff --git a/src/usermacro.cpp b/src/usermacro.cpp index a304dd3306..8eec4158bf 100644 --- a/src/usermacro.cpp +++ b/src/usermacro.cpp @@ -200,6 +200,21 @@ void Macro::setTypedTag(const QString &m_tag) tag=parseTypedTag(m_tag,type); } +QString Macro::getTag() +{ + return tag; +} + +void Macro::setTag(const QString &ntag) +{ + tag=ntag; +} + +void Macro::setType(const Macro::Type &ntype) +{ + type=ntype; +} + QString Macro::parseTypedTag(QString typedTag, Macro::Type &retType) { if (typedTag.startsWith("%SCRIPT\n")) { @@ -250,9 +265,9 @@ bool Macro::save(const QString &fileName) const { return false; QJsonObject dd; - dd.insert("formatVersion",1); + dd.insert("formatVersion",2); dd.insert("name",name); - QString tag= typedTag(); + dd.insert("type", ( type==Script ? "Script" : ( type==Environment ? "Environment" : "Snippet" ) ) ); dd.insert("tag",QJsonArray::fromStringList(tag.split("\n"))); dd.insert("description",QJsonArray::fromStringList(description.split("\n"))); dd.insert("abbrev",abbrev); @@ -316,8 +331,25 @@ bool Macro::loadFromText(const QString &text) // distrbute data on internal structure Macro::Type typ; - QString typedTag=parseTypedTag(rawData.value("tag"),typ); - init(rawData.value("name"),typ,typedTag,rawData.value("abbrev"),rawData.value("trigger")); + QString tag; + switch(dd.value("formatVersion").toInt()) { + case 1: + tag = parseTypedTag(rawData.value("tag"),typ); + rawData.insert("formatVersion","2"); + qDebug() << QString("Macro %1 with version 1 migrated as type %2.").arg(rawData.value("name"), ( typ==Script ? "Script" : ( typ==Environment ? "Environment" : "Snippet" ) )); + break; + case 2: { + QString qtype = rawData.value("type"); + typ = ( qtype=="Script" ? Script : ( qtype=="Environment" ? Environment : Snippet ) ); + tag = rawData.value("tag"); + break; + } + default: + qDebug() << QString("Trying to import macro %1 with unknown version %2 as type Snippet. Type maybe wrong.").arg(rawData.value("name")).arg(dd.value("formatVersion").toInt()); + typ = Snippet; + tag = rawData.value("tag"); + } + init(rawData.value("name"),typ,tag,rawData.value("abbrev"),rawData.value("trigger")); m_shortcut=rawData.value("shortcut"); menu=rawData.value("menu"); description=rawData.value("description"); diff --git a/src/usermacro.h b/src/usermacro.h index 8b4921e2c6..d426439905 100644 --- a/src/usermacro.h +++ b/src/usermacro.h @@ -41,11 +41,14 @@ class Macro bool isEmpty() const; + QString getTag(); + void setTag(const QString &ntag); void setShortcut(const QString &sc); void setTrigger(const QString &newTrigger); QString typedTag() const; - void setTypedTag(const QString &m_tag); + void setTypedTag(const QString &m_tag); + void setType(const Macro::Type &ntype); static QString parseTypedTag(QString typedTag, Macro::Type &retType); void parseTriggerLanguage(QLanguageFactory *langFactory); diff --git a/src/usermenudialog.cpp b/src/usermenudialog.cpp index 1db2329c39..c7c28750b0 100644 --- a/src/usermenudialog.cpp +++ b/src/usermenudialog.cpp @@ -54,9 +54,9 @@ UserMenuDialog::UserMenuDialog(QWidget *parent, QString name, QLanguageFactory connect(ui.pbImport,SIGNAL(clicked()), SLOT(importMacro())); connect(ui.pbBrowse,SIGNAL(clicked()), SLOT(browseMacrosOnRepository())); - connect(ui.radioButtonNormal, SIGNAL(clicked()), SLOT(changeTypeToNormal())); - connect(ui.radioButtonEnvironment, SIGNAL(clicked()), SLOT(changeTypeToEnvironment())); - connect(ui.radioButtonScript, SIGNAL(clicked()), SLOT(changeTypeToScript())); + connect(ui.radioButtonNormal, SIGNAL(clicked()), SLOT(changeType())); + connect(ui.radioButtonEnvironment, SIGNAL(clicked()), SLOT(changeType())); + connect(ui.radioButtonScript, SIGNAL(clicked()), SLOT(changeType())); //editor options ui.tagEdit->setLayout(new QVBoxLayout()); @@ -127,7 +127,6 @@ UserMenuDialog::UserMenuDialog(QWidget *parent, QString name, QLanguageFactory codeedit->editor()->clearFocus(); ui.pbBrowse->setFocus(); - } UserMenuDialog::~UserMenuDialog() @@ -292,27 +291,31 @@ void UserMenuDialog::change(QTreeWidgetItem *current,QTreeWidgetItem *previous) if (m.description != ui.teDescription->toPlainText()) ui.teDescription->setPlainText(m.description); - if (codeedit->editor()->text() != m.typedTag()) { - codeedit->editor()->setText(m.typedTag(), false); - setLanguageFromText(); + if (codeedit->editor()->text() != m.getTag()) { + codeedit->editor()->setText(m.getTag(), false); + setLanguageFromType(current); codeedit->editor()->setFlag(QEditor::CursorOn,false); codeedit->editor()->repaintCursor(); } } } -void UserMenuDialog::setLanguageFromText(void) +void UserMenuDialog::setLanguageFromType(QTreeWidgetItem *current) { - QString line = codeedit->editor()->text(0); - if (line == "%SCRIPT") { - languages->setLanguage(codeedit->editor(), ".qs"); - ui.radioButtonScript->setChecked(true); - } else if (line.startsWith("%") && !line.startsWith("%%")) { - languages->setLanguage(codeedit->editor(), ""); - ui.radioButtonEnvironment->setChecked(true); - } else { - languages->setLanguage(codeedit->editor(), "(La)TeX Macro"); - ui.radioButtonNormal->setChecked(true); + if (current==nullptr) return; + QVariant v = current->data(0,Qt::UserRole); + if(v.isValid()){ + Macro m = v.value(); + if (m.type==Macro::Script) { + languages->setLanguage(codeedit->editor(), ".qs"); + ui.radioButtonScript->setChecked(true); + } else if (m.type==Macro::Environment) { + languages->setLanguage(codeedit->editor(), "Plain text"); + ui.radioButtonEnvironment->setChecked(true); + } else { + languages->setLanguage(codeedit->editor(), "(La)TeX Macro"); + ui.radioButtonNormal->setChecked(true); + } } } @@ -322,7 +325,7 @@ void UserMenuDialog::slotOk() } void UserMenuDialog::slotExecMacro() { - QTreeWidgetItem *item=ui.treeWidget->currentItem(); + QTreeWidgetItem *item = ui.treeWidget->currentItem(); if (item==nullptr) return; QVariant v = item->data(0,Qt::UserRole); if (v.isValid()){ @@ -481,52 +484,39 @@ void UserMenuDialog::browseMacrosOnRepository(){ } } -void UserMenuDialog::changeTypeToNormal() -{ - QString cur = codeedit->editor()->text(); - languages->setLanguage(codeedit->editor(), "(La)TeX Macro"); - if (cur.startsWith("%SCRIPT\n")) codeedit->editor()->setText(cur.mid(8), false); - else if (cur.startsWith("%") && !cur.startsWith("%%")) codeedit->editor()->setText(cur.mid(1), false); - codeedit->editor()->setFlag(QEditor::CursorOn,false); - codeedit->editor()->repaintCursor(); -} - -void UserMenuDialog::changeTypeToEnvironment() -{ - QString cur = codeedit->editor()->text(); - languages->setLanguage(codeedit->editor(), ""); - if (cur.startsWith("%SCRIPT\n")) { - codeedit->editor()->setText("%" + cur.mid(8), false); - } else { - if (cur.startsWith("%") && !cur.startsWith("%%")) return; - while (cur.startsWith("%%")) cur = cur.remove(0, 2); - if (!cur.startsWith("%")) cur = "%" + cur; - codeedit->editor()->setText(cur, false); - } - codeedit->editor()->setFlag(QEditor::CursorOn,false); - codeedit->editor()->repaintCursor(); -} - -void UserMenuDialog::changeTypeToScript() +void UserMenuDialog::changeType() { - QString cur = codeedit->editor()->text(); - languages->setLanguage(codeedit->editor(), ".qs"); - if (cur.startsWith("%SCRIPT\n")) return; - if (cur.startsWith("%") && !cur.startsWith("%%")) cur = cur.mid(1); - codeedit->editor()->setText("%SCRIPT\n" + cur, false); - codeedit->editor()->setFlag(QEditor::CursorOn,false); - codeedit->editor()->repaintCursor(); + QTreeWidgetItem *item = ui.treeWidget->currentItem(); + if (item==nullptr) return; + QVariant v = item->data(0,Qt::UserRole); + if (v.isValid()){ + Macro m = v.value(); + if (ui.radioButtonNormal->isChecked()) { + m.setType(Macro::Snippet); + languages->setLanguage(codeedit->editor(), "(La)TeX Macro"); + } + else if (ui.radioButtonEnvironment->isChecked()) { + m.setType(Macro::Environment); + languages->setLanguage(codeedit->editor(), "Plain text"); + } + else if (ui.radioButtonScript->isChecked()) { + m.setType(Macro::Script); + languages->setLanguage(codeedit->editor(), ".qs"); + } + item->setData(0,Qt::UserRole,QVariant::fromValue(m)); + codeedit->editor()->setFlag(QEditor::CursorOn,false); + codeedit->editor()->repaintCursor(); + } } void UserMenuDialog::textChanged() { - setLanguageFromText(); QTreeWidgetItem *item=ui.treeWidget->currentItem(); if(item==nullptr) return; QVariant v=item->data(0,Qt::UserRole); if(v.isValid()){ Macro m=v.value(); - m.setTypedTag(codeedit->editor()->text()); + m.setTag(codeedit->editor()->text()); item->setData(0,Qt::UserRole,QVariant::fromValue(m)); } } diff --git a/src/usermenudialog.h b/src/usermenudialog.h index f1fba36aac..af3f2213fe 100644 --- a/src/usermenudialog.h +++ b/src/usermenudialog.h @@ -38,7 +38,7 @@ class UserMenuDialog : public QDialog bool getLineWrap(); private: - void setLanguageFromText(void); + void setLanguageFromType(QTreeWidgetItem *current); QTreeWidgetItem* findCreateFolder(const QString &menu); QTreeWidgetItem* findCreateFolder(QTreeWidgetItem *parent, QStringList folders); @@ -53,12 +53,12 @@ class UserMenuDialog : public QDialog void execMacro(const Macro ¯o); private slots: - void change(QTreeWidgetItem *current,QTreeWidgetItem *previous); + void change(QTreeWidgetItem *current,QTreeWidgetItem *previous); void slotOk(); void slotExecMacro(); void slotAdd(); void slotRemove(); - void slotAddFolder(); + void slotAddFolder(); void slotMoveUp(); void slotMoveDown(); void importMacro(); @@ -72,9 +72,7 @@ private slots: void abbrevChanged(); void triggerChanged(); void showTooltip(); - void changeTypeToNormal(); - void changeTypeToEnvironment(); - void changeTypeToScript(); + void changeType(); }; #endif // USERMENUDIALOG_H