GUI: Let user add function in the library editor

This commit is contained in:
Daniel Marjamäki 2015-08-29 18:50:08 +02:00
parent 6aeb3ff5e3
commit f5d131671c
4 changed files with 69 additions and 2 deletions

View File

@ -132,6 +132,8 @@ bool LibraryData::open(QIODevice &file)
if (!doc.setContent(&file)) if (!doc.setContent(&file))
return false; return false;
clear();
QDomElement rootElement = doc.firstChildElement("def"); QDomElement rootElement = doc.firstChildElement("def");
QStringList comments; QStringList comments;
for (QDomNode n = rootElement.firstChild(); !n.isNull(); n = n.nextSibling()) { for (QDomNode n = rootElement.firstChild(); !n.isNull(); n = n.nextSibling()) {

View File

@ -23,6 +23,7 @@
#include <QSettings> #include <QSettings>
#include <QFileDialog> #include <QFileDialog>
#include <QTextStream> #include <QTextStream>
#include <QInputDialog>
// TODO: get/compare functions from header // TODO: get/compare functions from header
@ -57,12 +58,15 @@ void LibraryDialog::openCfg()
mFileName.clear(); mFileName.clear();
QFile file(selectedFile); QFile file(selectedFile);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
ignoreChanges = true;
data.open(file); data.open(file);
mFileName = selectedFile; mFileName = selectedFile;
ui->buttonSave->setEnabled(false); ui->buttonSave->setEnabled(false);
ui->functions->clear(); ui->functions->clear();
foreach(const struct LibraryData::Function &function, data.functions) foreach(const struct LibraryData::Function &function, data.functions) {
ui->functions->addItem(function.name); ui->functions->addItem(function.name);
}
ignoreChanges = false;
} }
} }
} }
@ -79,6 +83,25 @@ void LibraryDialog::saveCfg()
} }
} }
void LibraryDialog::addFunction()
{
LibraryData::Function f;
bool ok;
f.name = QInputDialog::getText(this, tr("Add function"), tr("Function name"), QLineEdit::Normal, "doStuff", &ok);
if (!ok)
return;
int args = QInputDialog::getInt(this, tr("Add function"), tr("Number of arguments"), 0, 0, 20, 1, &ok);
if (!ok)
return;
for (int i = 1; i <= args; i++) {
LibraryData::Function::Arg arg;
arg.nr = i;
f.args.append(arg);
}
data.functions.append(f);
ui->functions->addItem(f.name);
}
void LibraryDialog::selectFunction(int row) void LibraryDialog::selectFunction(int row)
{ {
ignoreChanges = true; ignoreChanges = true;

View File

@ -40,6 +40,7 @@ public:
private slots: private slots:
void openCfg(); void openCfg();
void saveCfg(); void saveCfg();
void addFunction();
void selectFunction(int row); void selectFunction(int row);
void changeFunction(); void changeFunction();
void argumentChanged(QListWidgetItem *); void argumentChanged(QListWidgetItem *);

View File

@ -66,6 +66,30 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="addFunction">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout> </layout>
</item> </item>
<item> <item>
@ -234,6 +258,22 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>addFunction</sender>
<signal>clicked()</signal>
<receiver>LibraryDialog</receiver>
<slot>addFunction()</slot>
<hints>
<hint type="sourcelabel">
<x>48</x>
<y>282</y>
</hint>
<hint type="destinationlabel">
<x>65</x>
<y>298</y>
</hint>
</hints>
</connection>
</connections> </connections>
<slots> <slots>
<slot>selectFunction(int)</slot> <slot>selectFunction(int)</slot>
@ -241,5 +281,6 @@
<slot>changeFunction()</slot> <slot>changeFunction()</slot>
<slot>saveCfg()</slot> <slot>saveCfg()</slot>
<slot>argumentChanged(QListWidgetItem*)</slot> <slot>argumentChanged(QListWidgetItem*)</slot>
<slot>addFunction()</slot>
</slots> </slots>
</ui> </ui>