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..48a399de9 --- /dev/null +++ b/qt-ui/addweightsystemdialog.cpp @@ -0,0 +1,39 @@ +/* + * 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); + currentWeightsystem = NULL; +} + +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 7d60db5c2..2467dd0d7 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 @@ -61,6 +62,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 = ""; @@ -83,6 +87,33 @@ void MainTab::on_delCylinder_clicked() { } +void MainTab::on_addWeight_clicked() +{ + if (weightModel->rowCount() >= MAX_WEIGHTSYSTEMS) + return; + + AddWeightsystemDialog dialog(this); + weightsystem_t newWeightsystem; + newWeightsystem.description = ""; + newWeightsystem.weight.grams = 0; + + dialog.setWeightsystem(&newWeightsystem); + int result = dialog.exec(); + if (result == QDialog::Rejected) + return; + + dialog.updateWeightsystem(); + 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/mainwindow.cpp b/qt-ui/mainwindow.cpp index f4538db22..9901e4186 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() @@ -41,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(); @@ -54,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(); @@ -84,15 +84,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 */ @@ -139,6 +136,8 @@ void MainWindow::on_actionPreferences_triggered() void MainWindow::on_actionQuit_triggered() { qDebug("actionQuit"); + if (unsaved_changes() && (askSaveChanges() == FALSE)) + return; } void MainWindow::on_actionDownloadDC_triggered() @@ -285,3 +284,36 @@ bool MainWindow::askSaveChanges() } return false; } + +void MainWindow::readSettings() +{ + QSettings settings("hohndel.org","subsurface"); + + 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() +{ + 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 */ +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (unsaved_changes() && (askSaveChanges() == FALSE)) { + event->ignore(); + return; + } + event->accept(); + writeSettings(); +} 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(); }; 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 diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 7b9d8dc16..6d9bbad39 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -60,9 +60,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()) { @@ -94,67 +92,93 @@ 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); + return; } - 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 { - 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 { - return rows; + return usedRows[current_dive]; } QVariant WeightModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -164,19 +188,36 @@ 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; } -void WeightModel::add(weight_t* weight) +void WeightModel::add(weightsystem_t* weight) { + if (usedRows[current_dive] >= MAX_WEIGHTSYSTEMS) { + free(weight); + return; + } + + 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 7bc7578b8..52a3498df 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -50,28 +50,29 @@ 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 * 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; /*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: - 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.