From 89f02c42aa2fb49a811cb6e31dd40cd56dee0cf9 Mon Sep 17 00:00:00 2001 From: Amit Chaudhuri Date: Sat, 27 Apr 2013 10:09:57 +0100 Subject: [PATCH 1/9] Add support for remembering settings Use QSettings to provide persistent storage of settings. For example, we store and restore the size of the MainWindow. We use the organisation name hohndel.org and keep subsurface as the application name. A section is specified for things to do with the MainWindow; other sections could be added e.g. for preferred units? Signed-off-by: Amit Chaudhuri Signed-off-by: Dirk Hohndel --- qt-ui/mainwindow.cpp | 37 +++++++++++++++++++++++++++++++++++++ qt-ui/mainwindow.h | 5 +++++ 2 files changed, 42 insertions(+) diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 8cdc60193..0c6691a06 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include "divelistview.h" #include "starwidget.h" @@ -31,6 +33,7 @@ MainWindow::MainWindow() : ui(new Ui::MainWindow()), ui->ListWidget->setModel(sortModel); setWindowIcon(QIcon(":subsurface-icon")); + readSettings(); } void MainWindow::on_actionNew_triggered() @@ -138,6 +141,11 @@ void MainWindow::on_actionPreferences_triggered() void MainWindow::on_actionQuit_triggered() { qDebug("actionQuit"); + if (unsaved_changes() && (askSaveChanges() == FALSE)) + { + return; + } + } void MainWindow::on_actionDownloadDC_triggered() @@ -284,3 +292,32 @@ bool MainWindow::askSaveChanges() } return false; } + +void MainWindow::readSettings() +{ + QSettings settings("hohndel.org","subsurface"); + + /* note: section/key i.e. forward slash to separate */ + QSize sz = settings.value("MainWindow/size").value(); + resize(sz); +} + +void MainWindow::writeSettings() +{ + QSettings settings("hohndel.org","subsurface"); + settings.beginGroup("MainWindow"); + settings.setValue("size",size()); + settings.endGroup(); + /* other groups here; avoid '/' and '\' in keys with setValue(...) please */ +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (unsaved_changes() && (askSaveChanges() == FALSE)) + { + event->ignore(); + return; + } + event->accept(); + writeSettings(); +} \ No newline at end of file diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h index fdb100c7a..eece91ade 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -66,12 +66,17 @@ private Q_SLOTS: void on_actionAboutSubsurface_triggered(); void on_actionUserManual_triggered(); +protected: + void closeEvent(QCloseEvent *); + private: Ui::MainWindow *ui; DiveTripModel *model; QSortFilterProxyModel *sortModel; QString filter(); bool askSaveChanges(); + void readSettings(); + void writeSettings(); }; From 4bb0cd8cef2bbbbe3df6fc3daa9ccca95f065038 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 27 Apr 2013 20:47:47 -0700 Subject: [PATCH 2/9] Minor coding style cleanups - and use core logic function Signed-off-by: Dirk Hohndel --- qt-ui/mainwindow.cpp | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 0c6691a06..d20f8cfb6 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -44,9 +44,8 @@ void MainWindow::on_actionNew_triggered() void MainWindow::on_actionOpen_triggered() { QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), filter()); - if (filename.isEmpty()) { + if (filename.isEmpty()) return; - } // Needed to convert to char* QByteArray fileNamePtr = filename.toLocal8Bit(); @@ -57,15 +56,13 @@ void MainWindow::on_actionOpen_triggered() parse_file(fileNamePtr.data(), &error); set_filename(fileNamePtr.data(), TRUE); - if (error != NULL) - { + if (error != NULL) { QMessageBox::warning(this, "Error", error->message); g_error_free(error); error = NULL; } - //WARNING: Port This method to Qt - report_dives(FALSE, FALSE); + process_dives(FALSE, FALSE); ui->InfoWidget->reload(); @@ -86,15 +83,12 @@ void MainWindow::on_actionSaveAs_triggered() void MainWindow::on_actionClose_triggered() { if (unsaved_changes() && (askSaveChanges() == FALSE)) - { return; - } /* free the dives and trips */ while (dive_table.nr) - { delete_single_dive(0); - } + mark_divelist_changed(FALSE); /* clear the selection and the statistics */ @@ -142,10 +136,7 @@ void MainWindow::on_actionQuit_triggered() { qDebug("actionQuit"); if (unsaved_changes() && (askSaveChanges() == FALSE)) - { return; - } - } void MainWindow::on_actionDownloadDC_triggered() @@ -313,11 +304,10 @@ void MainWindow::writeSettings() void MainWindow::closeEvent(QCloseEvent *event) { - if (unsaved_changes() && (askSaveChanges() == FALSE)) - { + if (unsaved_changes() && (askSaveChanges() == FALSE)) { event->ignore(); return; } event->accept(); writeSettings(); -} \ No newline at end of file +} From 4c665be05f7128a5a259fb3b71f3d65019639b1d Mon Sep 17 00:00:00 2001 From: Amit Chaudhuri Date: Sun, 28 Apr 2013 10:05:37 +0100 Subject: [PATCH 3/9] Save and retore splitter dimensions. Rename splitters and remove seemingly redundant empty splitter. Use save/restoreState to save splitter sizes using QSettings. Signed-off-by: Amit Chaudhuri Signed-off-by: Dirk Hohndel --- qt-ui/mainwindow.cpp | 9 +++++++-- qt-ui/mainwindow.ui | 13 +++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index c1e15e30c..71ba70525 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -289,9 +289,12 @@ void MainWindow::readSettings() { QSettings settings("hohndel.org","subsurface"); - /* note: section/key i.e. forward slash to separate */ - QSize sz = settings.value("MainWindow/size").value(); + settings.beginGroup("MainWindow"); + QSize sz = settings.value("size").value(); resize(sz); + ui->mainSplitter->restoreState(settings.value("mainSplitter").toByteArray()); + ui->infoProfileSplitter->restoreState(settings.value("infoProfileSplitter").toByteArray()); + settings.endGroup(); } void MainWindow::writeSettings() @@ -299,6 +302,8 @@ void MainWindow::writeSettings() QSettings settings("hohndel.org","subsurface"); settings.beginGroup("MainWindow"); settings.setValue("size",size()); + settings.setValue("mainSplitter", ui->mainSplitter->saveState()); + settings.setValue("infoProfileSplitter", ui->infoProfileSplitter->saveState()); settings.endGroup(); /* other groups here; avoid '/' and '\' in keys with setValue(...) please */ } diff --git a/qt-ui/mainwindow.ui b/qt-ui/mainwindow.ui index 3d3f0ec35..073476918 100644 --- a/qt-ui/mainwindow.ui +++ b/qt-ui/mainwindow.ui @@ -16,11 +16,11 @@ - + Qt::Vertical - + Qt::Horizontal @@ -34,13 +34,6 @@ - - - - Qt::Horizontal - - - @@ -49,7 +42,7 @@ 0 0 763 - 25 + 20 From 730e055e5aacd2f917be9d86140773853670e850 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 09:04:14 -0700 Subject: [PATCH 4/9] Use the existing current_dive macro in Qt code Replicating this with the currentDive member seems to make no sense. Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 39 ++++++++++++++++++++------------------- qt-ui/models.h | 7 +++---- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 23eea3e48..d26b52ec9 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -59,9 +59,7 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const if (!index.isValid() || index.row() >= MAX_CYLINDERS) { return ret; } - - struct dive *d = get_dive(selected_dive); - cylinder_t& cyl = d->cylinder[index.row()]; + cylinder_t& cyl = current_dive->cylinder[index.row()]; if (role == Qt::DisplayRole) { switch(index.column()) { @@ -93,57 +91,60 @@ QVariant CylindersModel::data(const QModelIndex& index, int role) const int CylindersModel::rowCount(const QModelIndex& parent) const { - return usedRows[currentDive]; + return usedRows[current_dive]; } void CylindersModel::add(cylinder_t* cyl) { - if (usedRows[currentDive] >= MAX_CYLINDERS) { + if (usedRows[current_dive] >= MAX_CYLINDERS) { free(cyl); } - int row = usedRows[currentDive]; + int row = usedRows[current_dive]; - cylinder_t& cylinder = currentDive->cylinder[row]; + cylinder_t& cylinder = current_dive->cylinder[row]; cylinder.end.mbar = cyl->end.mbar; cylinder.start.mbar = cyl->start.mbar; beginInsertRows(QModelIndex(), row, row); - usedRows[currentDive]++; + usedRows[current_dive]++; endInsertRows(); } void CylindersModel::update() { - if (usedRows[currentDive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); endRemoveRows(); } - - currentDive = get_dive(selected_dive); - if (usedRows[currentDive] > 0) { - beginInsertRows(QModelIndex(), 0, usedRows[currentDive]-1); + if (usedRows[current_dive] > 0) { + beginInsertRows(QModelIndex(), 0, usedRows[current_dive]-1); endInsertRows(); } } void CylindersModel::clear() { - if (usedRows[currentDive] > 0) { - beginRemoveRows(QModelIndex(), 0, usedRows[currentDive]-1); - usedRows[currentDive] = 0; + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); + usedRows[current_dive] = 0; endRemoveRows(); } } void WeightModel::clear() { + if (usedRows[current_dive] > 0) { + beginRemoveRows(QModelIndex(), 0, usedRows[current_dive]-1); + usedRows[current_dive] = 0; + endRemoveRows(); + } } int WeightModel::columnCount(const QModelIndex& parent) const { - return 0; + return 2; } QVariant WeightModel::data(const QModelIndex& index, int role) const @@ -153,7 +154,7 @@ QVariant WeightModel::data(const QModelIndex& index, int role) const int WeightModel::rowCount(const QModelIndex& parent) const { - return rows; + return usedRows[current_dive]; } QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int role) const diff --git a/qt-ui/models.h b/qt-ui/models.h index 9e4666dc7..b6bcdec78 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -47,12 +47,10 @@ public: void clear(); void update(); private: - dive *currentDive; - /* Since the dive doesn`t stores the number of cylinders that * it has ( max 8 ) and since I don`t want to make a * model-for-each-dive, let`s hack this here instead. */ - QMap usedRows; + QMap usedRows; }; /* Encapsulation of the Weight Model, that represents @@ -68,7 +66,8 @@ class WeightModel : public QAbstractTableModel { void clear(); void update(); private: - int rows; + /* Remember the number of rows in a dive */ + QMap usedRows; }; /*! An AbstractItemModel for recording dive trip information such as a list of dives. From 5c2ce0ac200cbf4b37a9765148b78d3091b5cd9f Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 10:11:46 -0700 Subject: [PATCH 5/9] Add data and add functions for WeightModel Signed-off-by: Dirk Hohndel --- qt-ui/models.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- qt-ui/models.h | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index d26b52ec9..4583c36cd 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -149,7 +149,29 @@ int WeightModel::columnCount(const QModelIndex& parent) const QVariant WeightModel::data(const QModelIndex& index, int role) const { - return QVariant(); + QVariant ret; + if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) { + return ret; + } + weightsystem_t *ws = ¤t_dive->weightsystem[index.row()]; + + if (role == Qt::DisplayRole) { + switch(index.column()) { + case TYPE: + ret = QString(ws->description); + break; + case WEIGHT: + if (get_units()->weight == units::KG) { + int gr = ws->weight.grams % 1000; + int kg = ws->weight.grams / 1000; + ret = QString("%1.%2").arg(kg).arg((unsigned)(gr + 500) / 100); + } else { + ret = QString("%1").arg((unsigned)(grams_to_lbs(ws->weight.grams) + 0.5)); + } + break; + } + } + return ret; } int WeightModel::rowCount(const QModelIndex& parent) const @@ -175,8 +197,22 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r return ret; } -void WeightModel::add(weight_t* weight) +void WeightModel::add(weightsystem_t* weight) { + if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) { + free(weight); + } + + int row = usedRows[current_dive]; + + weightsystem_t *ws = ¤t_dive->weightsystem[row]; + + ws->description = weight->description; + ws->weight.grams = weight->weight.grams; + + beginInsertRows(QModelIndex(), row, row); + usedRows[current_dive]++; + endInsertRows(); } void WeightModel::update() diff --git a/qt-ui/models.h b/qt-ui/models.h index b6bcdec78..46da9e51b 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -62,7 +62,7 @@ class WeightModel : public QAbstractTableModel { /*reimp*/ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; /*reimp*/ int rowCount(const QModelIndex& parent = QModelIndex()) const; - void add(weight_t *weight); + void add(weightsystem_t *weight); void clear(); void update(); private: From 04e59a0e1cdb32c5091fc4bc0d692f00821ab849 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 14:30:34 -0700 Subject: [PATCH 6/9] Hook up adding a weightsystem Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 17 +++++++++++++++++ qt-ui/maintab.h | 3 +++ qt-ui/maintab.ui | 6 +++--- qt-ui/models.cpp | 16 +++++++++------- qt-ui/models.h | 2 ++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 7d60db5c2..f8c69ed9c 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -83,6 +83,23 @@ void MainTab::on_delCylinder_clicked() { } +void MainTab::on_addWeight_clicked() +{ + /* this needs a dialog - right now we just fill in a dummy */ + weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); + newWeightsystem->description = "Just testing"; + newWeightsystem->weight.grams = 15000; + weightModel->add(newWeightsystem); +} + +void MainTab::on_editWeight_clicked() +{ +} + +void MainTab::on_delWeight_clicked() +{ +} + void MainTab::reload() { cylindersModel->update(); diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h index 44815fafc..cf83e0dfe 100644 --- a/qt-ui/maintab.h +++ b/qt-ui/maintab.h @@ -31,6 +31,9 @@ public Q_SLOTS: void on_addCylinder_clicked(); void on_editCylinder_clicked(); void on_delCylinder_clicked(); + void on_addWeight_clicked(); + void on_editWeight_clicked(); + void on_delWeight_clicked(); private: Ui::MainTab *ui; diff --git a/qt-ui/maintab.ui b/qt-ui/maintab.ui index 7edbf5837..a99b0aed7 100644 --- a/qt-ui/maintab.ui +++ b/qt-ui/maintab.ui @@ -152,14 +152,14 @@ - + Edit - + Add @@ -179,7 +179,7 @@ - + Delete diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 4583c36cd..6756002e2 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -186,13 +186,15 @@ QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int r return ret; } - switch(section) { - case TYPE: - ret = tr("Type"); - break; - case WEIGHT: - ret = tr("Weight"); - break; + if (role == Qt::DisplayRole) { + switch(section) { + case TYPE: + ret = tr("Type"); + break; + case WEIGHT: + ret = tr("Weight"); + break; + } } return ret; } diff --git a/qt-ui/models.h b/qt-ui/models.h index 46da9e51b..9a4602f18 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -56,6 +56,8 @@ private: /* Encapsulation of the Weight Model, that represents * the current weights on a dive. */ class WeightModel : public QAbstractTableModel { +Q_OBJECT +public: enum Column {TYPE, WEIGHT}; /*reimp*/ QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; /*reimp*/ int columnCount(const QModelIndex& parent = QModelIndex()) const; From 482bea84c2f218f515b3b16556197379623a8028 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 14:49:17 -0700 Subject: [PATCH 7/9] Don't add cylinders and weightsystems past the MAX We actually should disable the 'Add' button, I guess. Signed-off-by: Dirk Hohndel --- qt-ui/maintab.cpp | 6 ++++++ qt-ui/models.cpp | 2 ++ 2 files changed, 8 insertions(+) diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index f8c69ed9c..4174ce5dd 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -61,6 +61,9 @@ void MainTab::clearStats() void MainTab::on_addCylinder_clicked() { + if (cylindersModel->rowCount() >= MAX_CYLINDERS) + return; + AddCylinderDialog dialog(this); cylinder_t *newCylinder = (cylinder_t*) malloc(sizeof(cylinder_t)); newCylinder->type.description = ""; @@ -85,6 +88,9 @@ void MainTab::on_delCylinder_clicked() void MainTab::on_addWeight_clicked() { + if (weightModel->rowCount() >= MAX_WEIGHTSYSTEMS) + return; + /* this needs a dialog - right now we just fill in a dummy */ weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); newWeightsystem->description = "Just testing"; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 6756002e2..f1bb8d137 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -98,6 +98,7 @@ void CylindersModel::add(cylinder_t* cyl) { if (usedRows[current_dive] >= MAX_CYLINDERS) { free(cyl); + return; } int row = usedRows[current_dive]; @@ -203,6 +204,7 @@ void WeightModel::add(weightsystem_t* weight) { if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) { free(weight); + return; } int row = usedRows[current_dive]; From f45618f0c7ce5af85e0f69aab49ef20a1663bca1 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 15:37:41 -0700 Subject: [PATCH 8/9] Create Add Weightsystem dialog My first attempt to create a Qt dialog and to hook it up with the program. Unsurprisingly this doesn't quite work as expected (i.e., the values I enter aren't populated in the model), but it's a start... Signed-off-by: Dirk Hohndel --- Makefile | 2 + qt-ui/addweightsystemdialog.cpp | 38 +++++++++++ qt-ui/addweightsystemdialog.h | 30 +++++++++ qt-ui/addweightsystemdialog.ui | 109 ++++++++++++++++++++++++++++++++ qt-ui/maintab.cpp | 14 +++- 5 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 qt-ui/addweightsystemdialog.cpp create mode 100644 qt-ui/addweightsystemdialog.h create mode 100644 qt-ui/addweightsystemdialog.ui diff --git a/Makefile b/Makefile index aac1759bc..1bb080363 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ EXTRA_FLAGS = $(QTCXXFLAGS) $(GTKCFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) \ HEADERS = \ qt-ui/addcylinderdialog.h \ + qt-ui/addweightsystemdialog.h \ qt-ui/divelistview.h \ qt-ui/maintab.h \ qt-ui/mainwindow.h \ @@ -70,6 +71,7 @@ SOURCES = \ webservice.c \ qt-gui.cpp \ qt-ui/addcylinderdialog.cpp \ + qt-ui/addweightsystemdialog.cpp \ qt-ui/divelistview.cpp \ qt-ui/maintab.cpp \ qt-ui/mainwindow.cpp \ diff --git a/qt-ui/addweightsystemdialog.cpp b/qt-ui/addweightsystemdialog.cpp new file mode 100644 index 000000000..6ac1c02f9 --- /dev/null +++ b/qt-ui/addweightsystemdialog.cpp @@ -0,0 +1,38 @@ +/* + * addweightsystemdialog.cpp + * + * classes for the add weightsystem dialog of Subsurface + * + */ +#include "addweightsystemdialog.h" +#include "ui_addweightsystemdialog.h" +#include +#include +#include "../conversions.h" +#include "models.h" + +AddWeightsystemDialog::AddWeightsystemDialog(QWidget *parent) : ui(new Ui::AddWeightsystemDialog()) +{ + ui->setupUi(this); +} + +void AddWeightsystemDialog::setWeightsystem(weightsystem_t *ws) +{ + currentWeightsystem = ws; + + ui->description->insert(QString(ws->description)); + if (get_units()->weight == units::KG) + ui->weight->setValue(ws->weight.grams / 1000); + else + ui->weight->setValue(grams_to_lbs(ws->weight.grams)); +} + +void AddWeightsystemDialog::updateWeightsystem() +{ + currentWeightsystem->description = strdup(ui->description->text().toUtf8().data()); + if (get_units()->weight == units::KG) + currentWeightsystem->weight.grams = ui->weight->value() * 1000; + else + currentWeightsystem->weight.grams = lbs_to_grams(ui->weight->value()); +} + diff --git a/qt-ui/addweightsystemdialog.h b/qt-ui/addweightsystemdialog.h new file mode 100644 index 000000000..e99dc08d8 --- /dev/null +++ b/qt-ui/addweightsystemdialog.h @@ -0,0 +1,30 @@ +/* + * addweightsystemdialog.h + * + * header file for the add weightsystem dialog of Subsurface + * + */ +#ifndef ADDWEIGHTSYSTEMDIALOG_H +#define ADDWEIGHTSYSTEMDIALOG_H + +#include +#include "../dive.h" + +namespace Ui{ + class AddWeightsystemDialog; +} + +class AddWeightsystemDialog : public QDialog{ + Q_OBJECT +public: + explicit AddWeightsystemDialog(QWidget* parent = 0); + void setWeightsystem(weightsystem_t *ws); + void updateWeightsystem(); + +private: + Ui::AddWeightsystemDialog *ui; + weightsystem_t *currentWeightsystem; +}; + + +#endif diff --git a/qt-ui/addweightsystemdialog.ui b/qt-ui/addweightsystemdialog.ui new file mode 100644 index 000000000..11e60d562 --- /dev/null +++ b/qt-ui/addweightsystemdialog.ui @@ -0,0 +1,109 @@ + + + AddWeightsystemDialog + + + + 0 + 0 + 408 + 186 + + + + Dialog + + + + + + Weightsystem + + + + QFormLayout::ExpandingFieldsGrow + + + + + Description + + + + + + + Weight + + + + + + + + 0 + 0 + + + + Qt::ImhFormattedNumbersOnly + + + true + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + AddWeightsystemDialog + accept() + + + 248 + 269 + + + 157 + 260 + + + + + buttonBox + rejected() + AddWeightsystemDialog + reject() + + + 290 + 269 + + + 286 + 260 + + + + + diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index 4174ce5dd..f26a72a8a 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -7,6 +7,7 @@ #include "maintab.h" #include "ui_maintab.h" #include "addcylinderdialog.h" +#include "addweightsystemdialog.h" #include @@ -91,10 +92,17 @@ void MainTab::on_addWeight_clicked() if (weightModel->rowCount() >= MAX_WEIGHTSYSTEMS) return; - /* this needs a dialog - right now we just fill in a dummy */ + AddWeightsystemDialog dialog(this); weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); - newWeightsystem->description = "Just testing"; - newWeightsystem->weight.grams = 15000; + newWeightsystem->description = ""; + newWeightsystem->weight.grams = 0; + + dialog.setWeightsystem(newWeightsystem); + int result = dialog.exec(); + if (result == QDialog::Rejected) + return; + + dialog.updateWeightsystem(); weightModel->add(newWeightsystem); } From 00d85313827af88ae5f35b2391ffa6964e81da49 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 1 May 2013 16:23:20 -0700 Subject: [PATCH 9/9] Fix a couple of small problems in add weightsystem The constructor letf the currentWeightsytem variable uninitialized. Instead of creating the memory leak by malloc-ing the newWeightsystem in the on_addWeight_clicked() function use a local variable instead and pass its address around. Signed-off-by: Dirk Hohndel --- qt-ui/addweightsystemdialog.cpp | 1 + qt-ui/maintab.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/qt-ui/addweightsystemdialog.cpp b/qt-ui/addweightsystemdialog.cpp index 6ac1c02f9..48a399de9 100644 --- a/qt-ui/addweightsystemdialog.cpp +++ b/qt-ui/addweightsystemdialog.cpp @@ -14,6 +14,7 @@ AddWeightsystemDialog::AddWeightsystemDialog(QWidget *parent) : ui(new Ui::AddWeightsystemDialog()) { ui->setupUi(this); + currentWeightsystem = NULL; } void AddWeightsystemDialog::setWeightsystem(weightsystem_t *ws) diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp index f26a72a8a..2467dd0d7 100644 --- a/qt-ui/maintab.cpp +++ b/qt-ui/maintab.cpp @@ -93,17 +93,17 @@ void MainTab::on_addWeight_clicked() return; AddWeightsystemDialog dialog(this); - weightsystem_t *newWeightsystem = (weightsystem_t *) malloc(sizeof(weightsystem_t)); - newWeightsystem->description = ""; - newWeightsystem->weight.grams = 0; + weightsystem_t newWeightsystem; + newWeightsystem.description = ""; + newWeightsystem.weight.grams = 0; - dialog.setWeightsystem(newWeightsystem); + dialog.setWeightsystem(&newWeightsystem); int result = dialog.exec(); if (result == QDialog::Rejected) return; dialog.updateWeightsystem(); - weightModel->add(newWeightsystem); + weightModel->add(&newWeightsystem); } void MainTab::on_editWeight_clicked()