From: Rafał Długołęcki Date: Fri, 22 Jan 2016 22:28:02 +0000 (+0100) Subject: Move configuration dialog code into separate class. Extract layout code into separate... X-Git-Tag: 3.4-b1~56 X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=6d33434099abfed5a9946f1ede31be2f42e9a17b;p=vlp.git Move configuration dialog code into separate class. Extract layout code into separate ui file --- diff --git a/Makefile.am b/Makefile.am index 9120555..326f22e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,15 +66,19 @@ clean-lognet-extra: bin_logker_SOURCES = \ src/kernel/kernel.cpp \ src/kernel/kernel.moc.cpp \ - src/kernel/lock.cpp + src/kernel/lock.cpp \ + src/kernel/options.cpp \ + src/kernel/options.moc.cpp bin_logker_CPPFLAGS = $(bin_logker_CFLAGS) -bin_logker_LDADD = $(bin_logker_LIBS) +bin_logker_LDADD = $(bin_logker_LIBS) -lconfig++ bin_logkerdir = src/kernel bin_logker_HEADERS = \ src/kernel/kernel.h \ - src/kernel/lock.h -src/kernel/kernel.moc.cpp: src/kernel/kernel.h src/kernel/kernelwindow.h src/kernel/lock_dialog.h + src/kernel/lock.h \ + src/kernel/options.h +src/kernel/kernel.moc.cpp: src/kernel/kernel.h src/kernel/kernelwindow.h src/kernel/lock_dialog.h src/kernel/optionsdialog.h moc-qt4 src/kernel/kernel.h -o src/kernel/kernel.moc.cpp + moc-qt4 src/kernel/options.h -o src/kernel/options.moc.cpp # $(MOC) -i src/kernel/kernel.cpp -o src/kernel/kernel.moc src/kernel/kernelwindow.h: @@ -83,11 +87,16 @@ src/kernel/kernelwindow.h: src/kernel/lock_dialog.h: uic src/kernel/lock_dialog.ui -o src/kernel/lock_dialog.h +src/kernel/optionsdialog.h: + uic src/kernel/optionsdialog.ui -o src/kernel/optionsdialog.h + clean-logker-extra: rm -f src/kernel/*.moc + rm -f src/kernel/*.moc.cpp rm -f src/kernel/kernelwindow.h rm -f src/kernel/lock_dialog.h + rm -f src/kernel/optionsdialog.h rm -f bin/logker bin_logint_SOURCES = \ diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 094fad6..2f45322 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -66,6 +66,7 @@ #include "kernel.h" #include "kernelwindow.h" #include "lock.h" +#include "options.h" /* File resides in top directory (where are Makefiles)*/ #include "../../config.h" @@ -413,9 +414,11 @@ void QKernel::on_actionQuit_triggered() { MESSAGE msg; - QMessageBox::StandardButton response; - response = QMessageBox::question(this, "Close VLP", "Terminate VLP ?", - QMessageBox::Ok | QMessageBox::Cancel); + QMessageBox::StandardButton response = QMessageBox::question(this, + "Close VLP", + "Terminate VLP ?", + QMessageBox::Ok | QMessageBox::Cancel + ); if (response == QMessageBox::Cancel) { @@ -434,50 +437,6 @@ void QKernel::on_actionQuit_triggered() QApplication::instance()->quit(); } -/** - * Adds IP address to the configuration. - * Additional window is displayed to add address to the list - */ -void QKernel::AddAddress() -{ - QDialog d(this, Qt::Dialog); - QLabel lab("IP Address:", &d); - QLineEdit ed("", &d); - QPushButton ob("", &d); - QPushButton cb("", &d); - - if (connections) { - ob.setGeometry(30, 60, 80, 30); - ob.setText("Ok"); - ob.setDefault(TRUE); - lab.setGeometry(10, 10, 60, 30); - lab.setText("Address"); - ed.setGeometry(70, 10, 140, 30); - cb.setGeometry(130, 60, 80, 30); - cb.setText("Cancel"); - d.resize(240, 100); - connect(&ob, SIGNAL(clicked()), &d, SLOT(accept())); - connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); - if (d.exec()) - if (strcmp(ed.text().toAscii().data(), "") != 0) { - connections->addItem(ed.text()); - } - } -} - -/** - * Deletes current address from available connections. - */ -void QKernel::DelAddress() -{ - if (connections) { - if (connections->currentRow() != -1) { - /* TODO: Checki if this work correctly after porting */ - connections->removeItemWidget(connections->currentItem()); - } - } -} - /** * Sends message to node. * Additional window is displayed to set Node Number of node where send message, @@ -674,10 +633,8 @@ void QKernel::NetMessage() cnt = poms1.indexOf('='); if (cnt != -1) { poms2 = poms1.left(cnt); - poms1 = poms1.right( - poms1.length() - - cnt - 1); - sprintf(ss, "Node: %s Addr: %s", poms2.data(), poms1.data()); + poms1 = poms1.right(poms1.length() - cnt - 1); + sprintf(ss, "Node: %s Addr: %s", poms2.toStdString().c_str(), poms1.toStdString().c_str()); WriteMessage(ss); } } @@ -800,12 +757,10 @@ void QKernel::WriteMessage(char *msg) void QKernel::on_actionInfo_messages_triggered() { if (toolsMenu != NULL) { - actionInfo_messages->setChecked(!actionInfo_messages->isChecked()); -// actionInfo_messages->toggle(); - info_messages = actionInfo_messages->isChecked(); - fprintf(stderr, "Info is checked? %s\n", actionInfo_messages->isChecked() ? "yes" : "no"); + info_messages = !actionInfo_messages->isChecked(); + actionInfo_messages->setChecked(info_messages); + fprintf(stderr, "Info is checked? %s\n", info_messages ? "yes" : "no"); } -// menuBar()->repaint(); } /** @@ -815,135 +770,10 @@ void QKernel::on_actionInfo_messages_triggered() */ void QKernel::on_actionOptions_triggered() { - QDialog dlg(this, Qt::Dialog); - dlg.setWindowTitle("Options"); - ConnectEntry *e; - unsigned int i; - - QLineEdit* progs; - progs = new QLineEdit(progdir, &dlg); - progs->setGeometry(150, 20, 180, 30); - - QLabel* tmpQLabel; - tmpQLabel = new QLabel("Programs directory", &dlg); - tmpQLabel->setGeometry(30, 20, 120, 30); - - QFrame* tmpQFrame; - tmpQFrame = new QFrame(&dlg); - tmpQFrame->setGeometry(10, 60, 380, 30); - tmpQFrame->setFrameStyle(52); - - tmpQLabel = new QLabel("Virtual Processor properties (activated after " - "restarting VLP):", &dlg); - tmpQLabel->setGeometry(10, 80, 340, 30); - - QLineEdit *nn; - char nns[256]; - sprintf(nns, "%d", NodeNumber); - nn = new QLineEdit(nns, &dlg); - nn->setGeometry(110, 110, 40, 30); - - tmpQLabel = new QLabel("Node number:", &dlg); - tmpQLabel->setGeometry(20, 110, 90, 30); - - QRadioButton *exp, *reg; - exp = new QRadioButton("Explicit", &dlg); - exp->setGeometry(30, 170, 100, 30); - exp->setChecked(TRUE); - - reg = new QRadioButton("Registration", &dlg); - reg->setGeometry(30, 200, 100, 30); - reg->setEnabled(FALSE); - - connections = new QListWidget(&dlg); - connections->setGeometry(170, 140, 130, 100); - - for (int i = 0; i < ConnectList.size(); i++) { - e = ConnectList.at(i); - connections->addItem(e->addr); - } - - tmpQLabel = new QLabel("Connection list:", &dlg); - tmpQLabel->setGeometry(170, 110, 100, 30); - - QPushButton *addbtn; - QPushButton *delbtn; - QPushButton *okbtn; - QPushButton *cancelbtn; - addbtn = new QPushButton("Add", &dlg); - addbtn->setGeometry(310, 150, 60, 30); - connect(addbtn, SIGNAL(clicked()), this, SLOT(AddAddress())); - - delbtn = new QPushButton("Del", &dlg); - delbtn->setGeometry(310, 200, 60, 30); - connect(delbtn, SIGNAL(clicked()), this, SLOT(DelAddress())); - - okbtn = new QPushButton("Ok", &dlg); - okbtn->setGeometry(80, 260, 100, 30); - okbtn->setDefault(TRUE); - connect(okbtn, SIGNAL(clicked()), &dlg, SLOT(accept())); - - cancelbtn = new QPushButton("Cancel", &dlg); - cancelbtn->setGeometry(210, 260, 100, 30); - connect(cancelbtn, SIGNAL(clicked()), &dlg, SLOT(reject())); - - QGroupBox* group; - group = new QGroupBox("Connection type", &dlg); - group->setGeometry(20, 150, 120, 90); - group->setAlignment(Qt::AlignLeft); - group->lower(); - - QVBoxLayout *vbox = new QVBoxLayout(); - vbox->addWidget(exp); - vbox->addWidget(reg); - vbox->addStretch(1); - group->setLayout(vbox); - - dlg.resize(400, 310); - if (dlg.exec()) { - config_t cfg; - config_setting_t *root; - config_setting_t *setting; - config_init(&cfg); - - root = config_root_setting(&cfg); - - setting = config_setting_add(root, "progdir", - CONFIG_TYPE_STRING); - config_setting_set_string(setting, progs->text().toAscii().data()); - strcpy(progdir, progs->text().toAscii().data()); - - setting = config_setting_add(root, "node_number", - CONFIG_TYPE_INT); - config_setting_set_int(setting, atoi(nn->text().toAscii().data())); - - setting = config_setting_add(root, "homedir", - CONFIG_TYPE_STRING); - config_setting_set_string(setting, HomeDir); - - setting = config_setting_add(root, "type", - CONFIG_TYPE_STRING); - if (exp->isChecked()) { - config_setting_set_string(setting, "explicit"); - - config_setting_t *hosts = NULL; - hosts = config_setting_add(root, "host", - CONFIG_TYPE_ARRAY); - for(i = 0; i < connections->count(); i++) { - setting = config_setting_add(hosts, NULL, - CONFIG_TYPE_STRING); - config_setting_set_string(setting, - connections->item(i)->text().toAscii().data()); - } - } else { - config_setting_set_string(setting, "register"); - } - - if (!config_write_file(&cfg, "vlp.cfg")) { - fprintf(stderr, "Error while writing to file: %s.\n", - "vlp.cfg"); - } - config_destroy(&cfg); + OptionsDialog optionsDialog(this); + if (optionsDialog.exec()) { + optionsDialog.saveConfig("vlp.cfg"); + LoadConfig("vlp.cfg"); } } @@ -1081,7 +911,8 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r) { char a[256], b[255]; struct sockaddr_un svr; - int len, sock, i, on; + int len, sock, on; + unsigned int i; int newint=-1; char cmd[255]; FILE *cf; diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 13494ea..eadf975 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -107,8 +107,6 @@ public: public slots: void n_impl(); - void AddAddress(); - void DelAddress(); void NetMessage(); void IntMessage(int); @@ -118,7 +116,6 @@ protected: private: QList Interpreters; QList ConnectList; - QListWidget *connections; /** * number of working interpreters diff --git a/src/kernel/lock.cpp b/src/kernel/lock.cpp index f7aea1f..e4814e0 100644 --- a/src/kernel/lock.cpp +++ b/src/kernel/lock.cpp @@ -3,7 +3,7 @@ #include "lock.h" LockDialog::LockDialog(QWidget * parent) - : QDialog(parent) + : QDialog(parent, Qt::Dialog) { setupUi(this); } diff --git a/src/kernel/options.cpp b/src/kernel/options.cpp new file mode 100644 index 0000000..2c4732f --- /dev/null +++ b/src/kernel/options.cpp @@ -0,0 +1,199 @@ +#include +#include + +#include "options.h" + +OptionsDialog::OptionsDialog(QWidget * parent) + : QDialog(parent) +{ + setupUi(this); + + connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + loadConfig("vlp.cfg"); +} + +OptionsDialog::~OptionsDialog() +{ +} + +/** + * Adds IP address to the configuration. + * Additional window is displayed to add address to the list + */ +void OptionsDialog::on_addConnectionButton_clicked() +{ + QDialog d(this, Qt::Dialog); + QLabel lab("IP Address:", &d); + QLineEdit ed("", &d); + QPushButton ob("", &d); + QPushButton cb("", &d); + + if (connections) { + ob.setGeometry(30, 60, 80, 30); + ob.setText("Ok"); + ob.setDefault(TRUE); + lab.setGeometry(10, 10, 60, 30); + lab.setText("Address"); + ed.setGeometry(70, 10, 140, 30); + cb.setGeometry(130, 60, 80, 30); + cb.setText("Cancel"); + d.resize(240, 100); + connect(&ob, SIGNAL(clicked()), &d, SLOT(accept())); + connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); + if (d.exec()) + if (strcmp(ed.text().toAscii().data(), "") != 0) { + connections->addItem(ed.text()); + } + } +} + +/** + * Deletes current address from available connections. + */ +void OptionsDialog::on_delConnectionButton_clicked() +{ + if (connections->currentRow() != -1) { + connections->takeItem(connections->currentRow()); + } +} + +/** + * Loads configuration from the given file. + * + * @see QKernel::LoadConfig + * + * @param fname Filename of the configuration file. + */ +void OptionsDialog::loadConfig(char * fname) +{ + config_t cfg; + config_setting_t *setting; + const char *str; + + /* Hack for checking if file exists without using external libs.*/ + FILE * file = fopen(fname, "rt"); + if (!file) { + fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); + exit(3); + } + /* File exists, so file has been locked. Release it. */ + + config_init(&cfg); + + /* Read the file. If there is an error, report it and exit. */ + if (!config_read(&cfg, file)) { + fprintf(stderr, "%s! In file %s, line %d\n", + config_error_text(&cfg), + config_error_file(&cfg), + config_error_line(&cfg)); + config_destroy(&cfg); + fclose(file); + exit(3); + } + + setting = config_lookup(&cfg, "node_number"); + if (setting) { + nodeNumber->setValue(config_setting_get_int(setting)); + } else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", + "Warning", fname, "node_number"); + config_destroy(&cfg); + fclose(file); + exit(3); + } + + setting = config_lookup(&cfg, "type"); + if (setting) { + /* same as strcmp(..) == 0 */ + if (!strcmp(config_setting_get_string(setting), "explicit")) { + explicitConnectionMode->setEnabled(true); + registrationConnectionMode->setDisabled(true); + } else { + registrationConnectionMode->setEnabled(true); + explicitConnectionMode->setDisabled(true); + } + } else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "type"); + } + + setting = config_lookup(&cfg, "host"); + if (setting) { + switch(config_setting_type(setting)) { + /* TODO: Deprecated. Made for back compatibility. */ + case CONFIG_TYPE_STRING: + connections->addItem(config_setting_get_string(setting)); + break; + case CONFIG_TYPE_ARRAY: { + int size = config_setting_length(setting); + for (int i = 0; i < size; i++) { + connections->addItem(config_setting_get_string_elem(setting, i)); + } + break; + } + default: + fprintf(stderr, "%s! In file %s, bad entry type for %s." + " Will not be read.\n", + "Error", fname, "host"); + } + } else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", + "Warning", fname, "host"); + } + + setting = config_lookup(&cfg, "progdir"); + if (setting){ + programsDirectory->setText(config_setting_get_string(setting)); + } else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir"); + } + + setting = config_lookup(&cfg, "homedir"); + if (setting) { + homeDir = config_setting_get_string(setting); + } else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "homedir"); + } + + config_destroy(&cfg); + fclose(file); +} + +void OptionsDialog::saveConfig(char * fname) +{ + config_t cfg; + config_setting_t *root; + config_setting_t *setting; + config_init(&cfg); + + root = config_root_setting(&cfg); + + setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING); + config_setting_set_string(setting, programsDirectory->text().toStdString().c_str()); + + setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); + config_setting_set_int(setting, nodeNumber->value()); + + setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); + config_setting_set_string(setting, homeDir); + + setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); + if (explicitConnectionMode->isChecked()) { + config_setting_set_string(setting, "explicit"); + + config_setting_t *hosts = NULL; + hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY); + for(int i = 0; i < connections->count(); i++) { + setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING); + config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str()); + } + } else { + config_setting_set_string(setting, "register"); + } + + if (!config_write_file(&cfg, fname)) { + fprintf(stderr, "Error while writing to file: %s.\n", fname); + } + config_destroy(&cfg); +} diff --git a/src/kernel/options.h b/src/kernel/options.h new file mode 100644 index 0000000..771d02d --- /dev/null +++ b/src/kernel/options.h @@ -0,0 +1,29 @@ +#ifndef _VLP_KERNEL_OPTIONS_H +#define _VLP_KERNEL_OPTIONS_H + +#include + +#include "optionsdialog.h" + +/** + * Options Dialog class + */ +class OptionsDialog : public QDialog, private Ui::OptionsDialog { + Q_OBJECT +public: + OptionsDialog(QWidget * parent = 0); + ~OptionsDialog(); + + void saveConfig(char * fname); + +private: + QString homeDir; + + void loadConfig(char * fname); + +private slots: + void on_addConnectionButton_clicked(); + void on_delConnectionButton_clicked(); +}; + +#endif /* _VLP_KERNEL_OPTIONS_H */ diff --git a/src/kernel/optionsdialog.ui b/src/kernel/optionsdialog.ui new file mode 100644 index 0000000..87484dc --- /dev/null +++ b/src/kernel/optionsdialog.ui @@ -0,0 +1,183 @@ + + + OptionsDialog + + + + 0 + 0 + 554 + 297 + + + + Options + + + + + + + + Programs directory + + + + + + + ./ + + + + + + + + + Qt::Horizontal + + + + + + + Virtual Processor properties (activated after restarting VLP): + + + + + + + + + + + + + Node number: + + + + + + + 9999 + + + + + + + + + Connection type + + + + + + Explicit + + + true + + + + + + + false + + + Registration + + + + + + + + + + + + + + Connection list: + + + + + + + + + + + + + + Add + + + + + + + Del + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + OptionsDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + OptionsDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +