diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | device.c | 5 | ||||
-rw-r--r-- | device.h | 1 | ||||
-rw-r--r-- | gtk-gui.c | 5 | ||||
-rw-r--r-- | qt-gui.cpp | 1 | ||||
-rw-r--r-- | qt-ui/divecomputermanagementdialog.cpp | 44 | ||||
-rw-r--r-- | qt-ui/divecomputermanagementdialog.h | 27 | ||||
-rw-r--r-- | qt-ui/divecomputermanagementdialog.ui | 24 | ||||
-rw-r--r-- | qt-ui/mainwindow.cpp | 4 | ||||
-rw-r--r-- | qt-ui/models.cpp | 114 | ||||
-rw-r--r-- | qt-ui/models.h | 21 |
11 files changed, 242 insertions, 6 deletions
@@ -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) @@ -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; @@ -22,6 +22,7 @@ extern struct device_info *create_device_info(const char *model, uint32_t device extern struct device_info *remove_device_info(const char *model, uint32_t deviceid); extern struct device_info *head_of_device_info_list(void); extern struct divecomputer *fake_dc(struct divecomputer* dc); +extern void remove_dive_computer(const char *model, uint32_t deviceid); #ifdef __cplusplus } @@ -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-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.cpp b/qt-ui/divecomputermanagementdialog.cpp new file mode 100644 index 000000000..7a96b4c63 --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.cpp @@ -0,0 +1,44 @@ +#include "divecomputermanagementdialog.h" +#include "models.h" +#include "ui_divecomputermanagementdialog.h" +#include "mainwindow.h" +#include <QMessageBox> + +DiveComputerManagementDialog::DiveComputerManagementDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f) +, ui( new Ui::DiveComputerManagementDialog()) +{ + 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() +{ + static DiveComputerManagementDialog *self = new DiveComputerManagementDialog(); + return self; +} + +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 new file mode 100644 index 000000000..e10a96db2 --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.h @@ -0,0 +1,27 @@ +#ifndef DIVECOMPUTERMANAGEMENTDIALOG_H +#define DIVECOMPUTERMANAGEMENTDIALOG_H +#include <QDialog> + +class QModelIndex; +class DiveComputerModel; +namespace Ui{ + class DiveComputerManagementDialog; +}; + +class DiveComputerManagementDialog : public QDialog{ +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; + 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..6685e842e --- /dev/null +++ b/qt-ui/divecomputermanagementdialog.ui @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>DiveComputerManagementDialog</class> + <widget class="QDialog" name="DiveComputerManagementDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QTableView" name="tableView"/> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp index 8b216bd8c..53546fa67 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; @@ -204,7 +205,8 @@ 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 6d6459983..03300a8d6 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -7,6 +7,8 @@ #include "models.h" #include "../helpers.h" #include "../dive.h" +#include "../device.h" + #include <QCoreApplication> #include <QDebug> #include <QColor> @@ -1152,3 +1154,115 @@ 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++; + } + + 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? + // 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 79af13bed..b61c79cd6 100644 --- a/qt-ui/models.h +++ b/qt-ui/models.h @@ -172,4 +172,25 @@ 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; + 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; + +}; + #endif |