From ebed836ee5d10659ae3d830704508f9c575181d3 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jun 2013 11:43:45 -0300 Subject: Created a new dialog - Edit DiveComputer Created a new dialog, Edit Divecomputer, it will currently only lists the divecomputers that are used on the xml file. I used the same method that the gtk version used, but only 2 divecomputers got visualized in the dirk dive data. I'll assume that it's correct and will fix it in the next couple of commits. Signed-off-by: Tomaz Canabrava --- Makefile | 2 + qt-ui/divecomputermanagementdialog.cpp | 22 +++++++++ qt-ui/divecomputermanagementdialog.h | 22 +++++++++ qt-ui/divecomputermanagementdialog.ui | 67 +++++++++++++++++++++++++++ qt-ui/mainwindow.cpp | 5 +- qt-ui/models.cpp | 83 ++++++++++++++++++++++++++++++++++ qt-ui/models.h | 16 +++++++ 7 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 qt-ui/divecomputermanagementdialog.cpp create mode 100644 qt-ui/divecomputermanagementdialog.h create mode 100644 qt-ui/divecomputermanagementdialog.ui diff --git a/Makefile b/Makefile index ab4c8b2ef..870c832bb 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,7 @@ HEADERS = \ qt-ui/preferences.h \ qt-ui/simplewidgets.h \ qt-ui/subsurfacewebservices.h \ + qt-ui/divecomputermanagementdialog.h \ SOURCES = \ @@ -82,6 +83,7 @@ SOURCES = \ qt-ui/preferences.cpp \ qt-ui/simplewidgets.cpp \ qt-ui/subsurfacewebservices.cpp \ + qt-ui/divecomputermanagementdialog.cpp \ $(RESFILE) diff --git a/qt-ui/divecomputermanagementdialog.cpp b/qt-ui/divecomputermanagementdialog.cpp new file mode 100644 index 000000000..867ca2d49 --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.cpp @@ -0,0 +1,22 @@ +#include "divecomputermanagementdialog.h" +#include "models.h" +#include "ui_divecomputermanagementdialog.h" + +DiveComputerManagementDialog::DiveComputerManagementDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f) +, ui( new Ui::DiveComputerManagementDialog()) +{ + ui->setupUi(this); + model = new DiveComputerModel(); + ui->tableView->setModel(model); +} + +DiveComputerManagementDialog* DiveComputerManagementDialog::instance() +{ + static DiveComputerManagementDialog *self = new DiveComputerManagementDialog(); + return self; +} + +void DiveComputerManagementDialog::update() +{ + model->update(); +} diff --git a/qt-ui/divecomputermanagementdialog.h b/qt-ui/divecomputermanagementdialog.h new file mode 100644 index 000000000..27a1408ab --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.h @@ -0,0 +1,22 @@ +#ifndef DIVECOMPUTERMANAGEMENTDIALOG_H +#define DIVECOMPUTERMANAGEMENTDIALOG_H +#include + +class DiveComputerModel; +namespace Ui{ + class DiveComputerManagementDialog; +}; + +class DiveComputerManagementDialog : public QDialog{ +Q_OBJECT + +public: + static DiveComputerManagementDialog *instance(); + void update(); +private: + explicit DiveComputerManagementDialog(QWidget* parent = 0, Qt::WindowFlags f = 0); + Ui::DiveComputerManagementDialog *ui; + DiveComputerModel *model; +}; + +#endif \ No newline at end of file diff --git a/qt-ui/divecomputermanagementdialog.ui b/qt-ui/divecomputermanagementdialog.ui new file mode 100644 index 000000000..abda9f5ce --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.ui @@ -0,0 +1,67 @@ + + + DiveComputerManagementDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + DiveComputerManagementDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + DiveComputerManagementDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 286aba43e..a927c0231 100644 --- a/qt-ui/mainwindow.cpp +++ b/qt-ui/mainwindow.cpp @@ -29,6 +29,7 @@ #include "downloadfromdivecomputer.h" #include "preferences.h" #include "subsurfacewebservices.h" +#include "divecomputermanagementdialog.h" static MainWindow* instance = 0; @@ -187,7 +188,9 @@ void MainWindow::on_actionDownloadWeb_triggered() void MainWindow::on_actionEditDeviceNames_triggered() { - qDebug("actionEditDeviceNames");} + DiveComputerManagementDialog::instance()->update(); + DiveComputerManagementDialog::instance()->show(); +} void MainWindow::on_actionAddDive_triggered() { diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 045592deb..1fc2515c6 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,7 @@ #include "models.h" #include "../helpers.h" #include "../dive.h" +#include "../device.h" #include #include #include @@ -1105,3 +1106,85 @@ void DiveTripModel::setLayout(DiveTripModel::Layout layout) currentLayout = layout; setupModelData(); } + +/*#################################################################### + * + * Dive Computer Model + * + *#################################################################### + */ + +DiveComputerModel::DiveComputerModel(QObject* parent): QAbstractTableModel(parent) +{ + +} + +int DiveComputerModel::columnCount(const QModelIndex& parent) const +{ + return COLUMNS; +} + +QVariant DiveComputerModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + QVariant ret; + if (role != Qt::DisplayRole || orientation != Qt::Horizontal){ + return ret; + } + switch(section){ + case ID : ret = tr("Device ID"); break; + case MODEL : ret = tr("Model"); break; + case NICKNAME : ret = tr("Nickname"); break; + } + return ret; +} + +QVariant DiveComputerModel::data(const QModelIndex& index, int role) const +{ + struct device_info *device = head_of_device_info_list(); + for(int i = 0; i < index.row(); i++){ + device = device->next; + } + + QVariant ret; + if (role == Qt::DisplayRole || role == Qt::EditRole){ + switch(index.column()){ + case ID : ret = device->deviceid; break; + case MODEL : ret = device->model; break; + case NICKNAME : ret = device->nickname; break; + } + } + + if (role == Qt::DecorationRole && index.column() == REMOVE){ + ret = QIcon(":trash"); + } + return ret; +} + +int DiveComputerModel::rowCount(const QModelIndex& parent) const +{ + return numRows; +} + +void DiveComputerModel::update() +{ + int count = 0; + struct device_info *nnl = head_of_device_info_list(); + while (nnl) { + nnl = nnl->next; + count++; + } + qDebug() << "Numero de Devices" << count; + + if(numRows){ + beginRemoveRows(QModelIndex(), 0, numRows-1); + numRows = 0; + endRemoveRows(); + } + + if (count){ + beginInsertRows(QModelIndex(), 0, count-1); + numRows = count; + endInsertRows(); + } + +} diff --git a/qt-ui/models.h b/qt-ui/models.h index a012ec6bd..c0625325f 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -167,4 +167,20 @@ private: Layout currentLayout; }; +class DiveComputerModel : public QAbstractTableModel +{ + Q_OBJECT +public: + enum {REMOVE, MODEL, ID, NICKNAME, COLUMNS}; + explicit DiveComputerModel(QObject* parent = 0); + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + virtual int columnCount(const QModelIndex& parent = QModelIndex()) const; + virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; + virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; + void update(); +private: + int numRows; + +}; + #endif -- cgit v1.2.3-70-g09d2 From 24446f9c3ce3b48c59b2ceb212b7ac49eb6dbff7 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jun 2013 12:57:35 -0300 Subject: Edit the name of the Dive Computer via dialog. The GTK version seems to be bugged on this, since the dialog doesn't save the dive computer nickname that I setted, but the Qt version shows less dive-computers than the GTK one on the same dive. I want somebody to do a quick review of my code too. :) I also plan to remove the 'OK' and 'Cancel' buttom, they seem to be overrated on this dialog - fairly uneeded. Signed-off-by: Tomaz Canabrava --- qt-ui/models.cpp | 23 ++++++++++++++++++++--- qt-ui/models.h | 2 ++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 1fc2515c6..787dbd8cf 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -1173,18 +1173,35 @@ void DiveComputerModel::update() nnl = nnl->next; count++; } - qDebug() << "Numero de Devices" << count; - + if(numRows){ beginRemoveRows(QModelIndex(), 0, numRows-1); numRows = 0; endRemoveRows(); } - + if (count){ beginInsertRows(QModelIndex(), 0, count-1); numRows = count; endInsertRows(); } +} + +Qt::ItemFlags DiveComputerModel::flags(const QModelIndex& index) const +{ + Qt::ItemFlags flags = QAbstractItemModel::flags(index); + if (index.column() == NICKNAME) + flags |= Qt::ItemIsEditable; + return flags; +} + +bool DiveComputerModel::setData(const QModelIndex& index, const QVariant& value, int role) +{ + struct device_info *nnl = head_of_device_info_list(); + for(int i = 0; i < index.row(); i++){ + nnl = nnl->next; + } + QByteArray v = value.toByteArray(); + nnl->nickname = strdup(v.data()); // how should I free this before setting a new one? } diff --git a/qt-ui/models.h b/qt-ui/models.h index c0625325f..7a32998ce 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -177,6 +177,8 @@ public: virtual int columnCount(const QModelIndex& parent = QModelIndex()) const; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; + virtual Qt::ItemFlags flags(const QModelIndex& index) const; + virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); void update(); private: int numRows; -- cgit v1.2.3-70-g09d2 From 478c24d79795b34efb42c1ac29abd8fa777b9c90 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jun 2013 14:59:06 -0300 Subject: Removed the buttons on the Edit Dive computers. This edit dive computers thing doesn't really need buttons anyway. Signed-off-by: Tomaz Canabrava --- qt-gui.cpp | 1 + qt-ui/divecomputermanagementdialog.ui | 45 +---------------------------------- qt-ui/models.cpp | 2 ++ 3 files changed, 4 insertions(+), 44 deletions(-) diff --git a/qt-gui.cpp b/qt-gui.cpp index bab27a318..d85198d68 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -157,6 +157,7 @@ const char *get_dc_nickname(const char *model, uint32_t deviceid) void set_dc_nickname(struct dive *dive) { /* needs Qt implementation */ + /*well, I don't know how to do this here... = ( */ } QString get_depth_string(depth_t depth, bool showunit) diff --git a/qt-ui/divecomputermanagementdialog.ui b/qt-ui/divecomputermanagementdialog.ui index abda9f5ce..6685e842e 100644 --- a/qt-ui/divecomputermanagementdialog.ui +++ b/qt-ui/divecomputermanagementdialog.ui @@ -17,51 +17,8 @@ - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - buttonBox - accepted() - DiveComputerManagementDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - DiveComputerManagementDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - + diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 787dbd8cf..6cb1fa079 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -1198,10 +1198,12 @@ Qt::ItemFlags DiveComputerModel::flags(const QModelIndex& index) const bool DiveComputerModel::setData(const QModelIndex& index, const QVariant& value, int role) { struct device_info *nnl = head_of_device_info_list(); + for(int i = 0; i < index.row(); i++){ nnl = nnl->next; } QByteArray v = value.toByteArray(); nnl->nickname = strdup(v.data()); // how should I free this before setting a new one? + // set_dc_nickname(dive); -> should this be used instead? } -- cgit v1.2.3-70-g09d2 From 54128aa52fe5c765ce6ced8bd05611e202195d6b Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jun 2013 15:25:29 -0300 Subject: Added the code to remove a dive computer. Added the code to remove a dive computer, plus a few fixes Signed-off-by: Tomaz Canabrava --- device.c | 5 +++++ device.h | 1 + gtk-gui.c | 5 ----- qt-ui/models.cpp | 12 ++++++++++++ qt-ui/models.h | 3 +++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/device.c b/device.c index 660cfc1ab..52c49a5bd 100644 --- a/device.c +++ b/device.c @@ -9,6 +9,11 @@ struct device_info *head_of_device_info_list(void) return device_info_list; } +void remove_dive_computer(const char *model, uint32_t deviceid) +{ + free(remove_device_info(model, deviceid)); +} + static int match_device_info(struct device_info *entry, const char *model, uint32_t deviceid) { return !strcmp(entry->model, model) && entry->deviceid == deviceid; diff --git a/device.h b/device.h index 636eb73c6..3efa42900 100644 --- a/device.h +++ b/device.h @@ -20,6 +20,7 @@ extern struct device_info *get_different_device_info(const char *model, uint32_t extern struct device_info *create_device_info(const char *model, uint32_t deviceid); extern struct device_info *remove_device_info(const char *model, uint32_t deviceid); extern struct device_info *head_of_device_info_list(void); +extern void remove_dive_computer(const char *model, uint32_t deviceid); #ifdef __cplusplus } diff --git a/gtk-gui.c b/gtk-gui.c index 666438c33..e873fc622 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -101,11 +101,6 @@ static void remember_dc(const char *model, uint32_t deviceid, const char *nickna nn_entry->nickname = strdup(nickname); } -static void remove_dc(const char *model, uint32_t deviceid) -{ - free(remove_device_info(model, deviceid)); -} - static GtkWidget *dive_profile; GtkActionGroup *action_group; diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 6cb1fa079..868cba952 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -8,6 +8,7 @@ #include "../helpers.h" #include "../dive.h" #include "../device.h" + #include #include #include @@ -1203,7 +1204,18 @@ bool DiveComputerModel::setData(const QModelIndex& index, const QVariant& value, nnl = nnl->next; } + QByteArray v = value.toByteArray(); nnl->nickname = strdup(v.data()); // how should I free this before setting a new one? // set_dc_nickname(dive); -> should this be used instead? + + return true; +} + +void DiveComputerModel::remove(const QModelIndex& i) +{ + QByteArray model = data(index(i.row(), (int)MODEL)).toByteArray(); + uint32_t deviceid = data(index(i.row(), (int) ID)).toUInt(); + remove_dive_computer(model.data(), deviceid); + update(); } diff --git a/qt-ui/models.h b/qt-ui/models.h index 7a32998ce..5a7d2b214 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -180,6 +180,9 @@ public: virtual Qt::ItemFlags flags(const QModelIndex& index) const; virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); void update(); + +public slots: + void remove(const QModelIndex& index); private: int numRows; -- cgit v1.2.3-70-g09d2 From 7681895fe09aa81c83143c6a58459e354a4234b1 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Fri, 7 Jun 2013 15:34:27 -0300 Subject: Remove the dive computer clicking on the trash icon. This code removes the dive computer clicking on the trash icon, the result is not saved on the XML, this will need a bit of hacking from some of the older guys. :) Signed-off-by: Tomaz Canabrava --- qt-ui/divecomputermanagementdialog.cpp | 22 ++++++++++++++++++++++ qt-ui/divecomputermanagementdialog.h | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/qt-ui/divecomputermanagementdialog.cpp b/qt-ui/divecomputermanagementdialog.cpp index 867ca2d49..7a96b4c63 100644 --- a/qt-ui/divecomputermanagementdialog.cpp +++ b/qt-ui/divecomputermanagementdialog.cpp @@ -1,6 +1,8 @@ #include "divecomputermanagementdialog.h" #include "models.h" #include "ui_divecomputermanagementdialog.h" +#include "mainwindow.h" +#include DiveComputerManagementDialog::DiveComputerManagementDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f) , ui( new Ui::DiveComputerManagementDialog()) @@ -8,6 +10,8 @@ DiveComputerManagementDialog::DiveComputerManagementDialog(QWidget* parent, Qt:: ui->setupUi(this); model = new DiveComputerModel(); ui->tableView->setModel(model); + connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(tryRemove(QModelIndex))); + ui->tableView->setColumnWidth(DiveComputerModel::REMOVE, 22); } DiveComputerManagementDialog* DiveComputerManagementDialog::instance() @@ -20,3 +24,21 @@ void DiveComputerManagementDialog::update() { model->update(); } + +void DiveComputerManagementDialog::tryRemove(const QModelIndex& index) +{ + if (index.column() != DiveComputerModel::REMOVE){ + return; + } + + QMessageBox::StandardButton response = QMessageBox::question( + this, + tr("Remove the selected Dive Computer?"), + tr("Are you sure that you want to \n remove the selected dive computer?"), + QMessageBox::Ok | QMessageBox::Cancel + ); + + if (response == QMessageBox::Ok){ + model->remove(index); + } +} diff --git a/qt-ui/divecomputermanagementdialog.h b/qt-ui/divecomputermanagementdialog.h index 27a1408ab..e10a96db2 100644 --- a/qt-ui/divecomputermanagementdialog.h +++ b/qt-ui/divecomputermanagementdialog.h @@ -2,6 +2,7 @@ #define DIVECOMPUTERMANAGEMENTDIALOG_H #include +class QModelIndex; class DiveComputerModel; namespace Ui{ class DiveComputerManagementDialog; @@ -13,6 +14,10 @@ Q_OBJECT public: static DiveComputerManagementDialog *instance(); void update(); + +public slots: + void tryRemove(const QModelIndex& index); + private: explicit DiveComputerManagementDialog(QWidget* parent = 0, Qt::WindowFlags f = 0); Ui::DiveComputerManagementDialog *ui; -- cgit v1.2.3-70-g09d2