From ea7209e6692122f3145785dd2e59772eef4001d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Aug 2015 12:49:51 +0200 Subject: [PATCH] GUI: make it possible to save changes in library editor --- gui/librarydialog.cpp | 105 +++++++++++++++++++++++++++++++++++++----- gui/librarydialog.h | 5 +- gui/librarydialog.ui | 102 ++++++++++++++++++++++++++++++++-------- 3 files changed, 182 insertions(+), 30 deletions(-) diff --git a/gui/librarydialog.cpp b/gui/librarydialog.cpp index 1866c27c4..50bdacc1b 100644 --- a/gui/librarydialog.cpp +++ b/gui/librarydialog.cpp @@ -25,6 +25,7 @@ #include #include #include +#include const unsigned int LibraryDialog::Function::Arg::ANY = ~0U; @@ -35,6 +36,7 @@ LibraryDialog::LibraryDialog(QWidget *parent) : ui(new Ui::LibraryDialog) { ui->setupUi(this); + ui->buttonSave->setEnabled(false); } LibraryDialog::~LibraryDialog() @@ -42,14 +44,6 @@ LibraryDialog::~LibraryDialog() delete ui; } -void LibraryDialog::updateui() -{ - ui->functions->clear(); - for (const struct Function &function : functions) { - ui->functions->addItem(function.name); - } -} - static LibraryDialog::Function::Arg loadFunctionArg(const QDomElement &functionArgElement) { LibraryDialog::Function::Arg arg; @@ -141,10 +135,86 @@ void LibraryDialog::openCfg() QFile file(selectedFile); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { loadFile(file); - updateui(); + + mFileName = selectedFile; + + ui->buttonSave->setEnabled(false); + ui->functions->clear(); + foreach (const struct Function &function, functions) + ui->functions->addItem(function.name); + } + } +} + +static QDomElement FunctionElement(QDomDocument &doc, const LibraryDialog::Function &function) +{ + QDomElement functionElement = doc.createElement("function"); + functionElement.setAttribute("name", function.name); + if (!function.noreturn) { + QDomElement e = doc.createElement("noreturn"); + e.appendChild(doc.createTextNode("false")); + functionElement.appendChild(e); + } + if (function.useretval) + functionElement.appendChild(doc.createElement("useretval")); + if (function.leakignore) + functionElement.appendChild(doc.createElement("leak-ignore")); + + // Argument info.. + foreach (const LibraryDialog::Function::Arg &arg, function.args) { + QDomElement argElement = doc.createElement("arg"); + functionElement.appendChild(argElement); + if (arg.nr == LibraryDialog::Function::Arg::ANY) + argElement.setAttribute("nr", "any"); + else + argElement.setAttribute("nr", arg.nr); + if (arg.notbool) + argElement.appendChild(doc.createElement("not-bool")); + if (arg.notnull) + argElement.appendChild(doc.createElement("not-null")); + if (arg.notuninit) + argElement.appendChild(doc.createElement("not-uninit")); + if (arg.strz) + argElement.appendChild(doc.createElement("strz")); + if (arg.formatstr) + argElement.appendChild(doc.createElement("formatstr")); + + if (!arg.valid.isEmpty()) { + QDomElement e = doc.createElement("valid"); + e.appendChild(doc.createTextNode(arg.valid)); + argElement.appendChild(e); + } + + if (!arg.minsize.type.isEmpty()) { + QDomElement e = doc.createElement("minsize"); + e.setAttribute("type", arg.minsize.type); + e.setAttribute("arg", arg.minsize.arg); + if (!arg.minsize.arg2.isEmpty()) + e.setAttribute("arg2", arg.minsize.arg2); + argElement.appendChild(e); } } + return functionElement; +} + +void LibraryDialog::saveCfg() +{ + QDomDocument doc; + QDomElement root = doc.createElement("def"); + doc.appendChild(root); + root.setAttribute("format","2"); + + foreach (const Function &function, functions) { + root.appendChild(FunctionElement(doc, function)); + } + + QFile file(mFileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream ts( &file ); + ts << doc.toString(); + ui->buttonSave->setEnabled(false); + } } void LibraryDialog::selectFunction(int row) @@ -154,16 +224,29 @@ void LibraryDialog::selectFunction(int row) ui->useretval->setChecked(function.useretval); ui->leakignore->setChecked(function.leakignore); ui->arguments->clear(); - for (const Function::Arg &arg : function.args) { + foreach (const Function::Arg &arg, function.args) { QString s("arg"); if (arg.nr != Function::Arg::ANY) s += QString::number(arg.nr); if (arg.formatstr) s += " formatstr"; - else if (!arg.strz) + else if (arg.strz) s += " strz"; else if (!arg.valid.isNull()) s += " " + arg.valid; ui->arguments->addItem(s); } } + +void LibraryDialog::changeFunction() +{ + foreach (const QListWidgetItem *item, ui->functions->selectedItems()) + { + Function &function = functions[ui->functions->row(item)]; + function.noreturn = !ui->functionreturn->isChecked(); + function.useretval = ui->useretval->isChecked(); + function.leakignore = ui->leakignore->isChecked(); + } + ui->buttonSave->setEnabled(true); +} + diff --git a/gui/librarydialog.h b/gui/librarydialog.h index d447f53bd..5c60d0e50 100644 --- a/gui/librarydialog.h +++ b/gui/librarydialog.h @@ -34,7 +34,7 @@ public: ~LibraryDialog(); struct Function { - Function() : noreturn(false), gccPure(false), gccConst(false), + Function() : noreturn(true), gccPure(false), gccConst(false), leakignore(false), useretval(false) { } @@ -73,7 +73,9 @@ public: private slots: void openCfg(); + void saveCfg(); void selectFunction(int row); + void changeFunction(); private: Ui::LibraryDialog *ui; @@ -81,6 +83,7 @@ private: void updateui(); bool loadFile(QFile &file); QList functions; + QString mFileName; }; #endif // LIBRARYDIALOG_H diff --git a/gui/librarydialog.ui b/gui/librarydialog.ui index 3566d0cc4..3c71f3cea 100644 --- a/gui/librarydialog.ui +++ b/gui/librarydialog.ui @@ -14,28 +14,22 @@ Library Editor - - - - false - - - QLabel { background-color : red; color : blue; } - - - EXPERIMENTAL - - - - + Open + + + + Save + + + @@ -64,8 +58,14 @@ + + QAbstractItemView::NoEditTriggers + - QAbstractItemView::SingleSelection + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::SelectRows @@ -142,14 +142,14 @@ - pushButton + buttonOpen clicked() LibraryDialog openCfg() - 56 - 36 + 61 + 27 72 @@ -157,9 +157,75 @@ + + functionreturn + clicked() + LibraryDialog + changeFunction() + + + 327 + 45 + + + 353 + 2 + + + + + useretval + clicked() + LibraryDialog + changeFunction() + + + 372 + 69 + + + 475 + 5 + + + + + leakignore + clicked() + LibraryDialog + changeFunction() + + + 419 + 99 + + + 319 + 3 + + + + + buttonSave + clicked() + LibraryDialog + saveCfg() + + + 102 + 9 + + + 115 + 1 + + + selectFunction(int) openCfg() + changeFunction() + saveCfg()