Skip to content

Commit

Permalink
remove line with macro type from macro editor
Browse files Browse the repository at this point in the history
macro type is now defined by radio buttons alone. todo so type is now part of user macro and exported Json.
  • Loading branch information
octaeder committed Dec 29, 2023
1 parent 5d171e5 commit 827fc19
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 66 deletions.
40 changes: 36 additions & 4 deletions src/usermacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/usermacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
100 changes: 45 additions & 55 deletions src/usermenudialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -127,7 +127,6 @@ UserMenuDialog::UserMenuDialog(QWidget *parent, QString name, QLanguageFactory

codeedit->editor()->clearFocus();
ui.pbBrowse->setFocus();

}

UserMenuDialog::~UserMenuDialog()
Expand Down Expand Up @@ -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<Macro>();
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);
}
}
}

Expand All @@ -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()){
Expand Down Expand Up @@ -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<Macro>();
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<Macro>();
m.setTypedTag(codeedit->editor()->text());
m.setTag(codeedit->editor()->text());
item->setData(0,Qt::UserRole,QVariant::fromValue(m));
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/usermenudialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -53,12 +53,12 @@ class UserMenuDialog : public QDialog
void execMacro(const Macro &macro);

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();
Expand All @@ -72,9 +72,7 @@ private slots:
void abbrevChanged();
void triggerChanged();
void showTooltip();
void changeTypeToNormal();
void changeTypeToEnvironment();
void changeTypeToScript();
void changeType();
};

#endif // USERMENUDIALOG_H

0 comments on commit 827fc19

Please sign in to comment.