diff options
-rw-r--r-- | desktop-widgets/CMakeLists.txt | 3 | ||||
-rw-r--r-- | desktop-widgets/divesiteimportdialog.cpp | 80 | ||||
-rw-r--r-- | desktop-widgets/divesiteimportdialog.h | 40 | ||||
-rw-r--r-- | desktop-widgets/divesiteimportdialog.ui | 119 | ||||
-rw-r--r-- | qt-models/CMakeLists.txt | 2 | ||||
-rw-r--r-- | qt-models/divesiteimportmodel.cpp | 142 | ||||
-rw-r--r-- | qt-models/divesiteimportmodel.h | 35 |
7 files changed, 421 insertions, 0 deletions
diff --git a/desktop-widgets/CMakeLists.txt b/desktop-widgets/CMakeLists.txt index fbaff1d6c..53aef5dbf 100644 --- a/desktop-widgets/CMakeLists.txt +++ b/desktop-widgets/CMakeLists.txt @@ -22,6 +22,7 @@ set (SUBSURFACE_UI divecomputermanagementdialog.ui divelogexportdialog.ui divelogimportdialog.ui + divesiteimportdialog.ui diveplanner.ui diveshareexportdialog.ui downloadfromdivecomputer.ui @@ -87,6 +88,8 @@ set(SUBSURFACE_INTERFACE diveplanner.h diveshareexportdialog.cpp diveshareexportdialog.h + divesiteimportdialog.cpp + divesiteimportdialog.h downloadfromdivecomputer.cpp downloadfromdivecomputer.h filterwidget2.cpp diff --git a/desktop-widgets/divesiteimportdialog.cpp b/desktop-widgets/divesiteimportdialog.cpp new file mode 100644 index 000000000..ad45d97a5 --- /dev/null +++ b/desktop-widgets/divesiteimportdialog.cpp @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "desktop-widgets/divesiteimportdialog.h" +#include "desktop-widgets/command.h" +#include "core/display.h" +#include "core/qthelper.h" +#include "core/metrics.h" +#include "core/subsurface-string.h" +#include "desktop-widgets/mainwindow.h" +#include "qt-models/divesiteimportmodel.h" +#include "qt-models/models.h" + +#include <QShortcut> + +// Caller keeps ownership of "imported". The contents of "imported" will be consumed on execution of the dialog. +// On return, it will be empty. +DivesiteImportDialog::DivesiteImportDialog(struct dive_site_table &imported, QString source, QWidget *parent) : QDialog(parent), + importedSource(source) +{ + QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this); + QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this); + + divesiteImportedModel = new DivesiteImportedModel(this); + + int startingWidth = defaultModelFont().pointSize(); + + ui.setupUi(this); + ui.importedDivesitesView->setModel(divesiteImportedModel); + ui.importedDivesitesView->setSelectionBehavior(QAbstractItemView::SelectRows); + ui.importedDivesitesView->setSelectionMode(QAbstractItemView::SingleSelection); + ui.importedDivesitesView->horizontalHeader()->setStretchLastSection(true); + ui.importedDivesitesView->verticalHeader()->setVisible(false); + ui.importedDivesitesView->setColumnWidth(0, startingWidth * 14); + ui.importedDivesitesView->setColumnWidth(1, startingWidth * 12); + ui.importedDivesitesView->setColumnWidth(2, startingWidth * 8); + ui.importedDivesitesView->setColumnWidth(3, startingWidth * 14); + ui.selectAllButton->setEnabled(true); + ui.unselectAllButton->setEnabled(true); + + connect(ui.importedDivesitesView, &QTableView::clicked, divesiteImportedModel, &DivesiteImportedModel::changeSelected); + connect(ui.selectAllButton, &QPushButton::clicked, divesiteImportedModel, &DivesiteImportedModel::selectAll); + connect(ui.unselectAllButton, &QPushButton::clicked, divesiteImportedModel, &DivesiteImportedModel::selectNone); + connect(close, SIGNAL(activated()), this, SLOT(close())); + connect(quit, SIGNAL(activated()), parent, SLOT(close())); + + ui.ok->setEnabled(true); + + importedSites = imported; + imported.nr = imported.allocated = 0; + imported.dive_sites = nullptr; + + divesiteImportedModel->repopulate(&importedSites); +} + +DivesiteImportDialog::~DivesiteImportDialog() +{ + clear_dive_site_table(&importedSites); +} + +void DivesiteImportDialog::on_cancel_clicked() +{ + clear_dive_site_table(&importedSites); + done(-1); +} + +void DivesiteImportDialog::on_ok_clicked() +{ + // delete non-selected dive sites + struct dive_site_table selectedSites = { 0 }; + for (int i = 0; i < importedSites.nr; i++) + if (divesiteImportedModel->data(divesiteImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked) { + struct dive_site *newSite = alloc_dive_site(); + copy_dive_site(importedSites.dive_sites[i], newSite); + add_dive_site_to_table(newSite, &selectedSites); + } + + Command::importDiveSites(&selectedSites, importedSource); + clear_dive_site_table(&selectedSites); + clear_dive_site_table(&importedSites); + accept(); +} diff --git a/desktop-widgets/divesiteimportdialog.h b/desktop-widgets/divesiteimportdialog.h new file mode 100644 index 000000000..b137234e0 --- /dev/null +++ b/desktop-widgets/divesiteimportdialog.h @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef DIVESITEIMPORTDIALOG_H +#define DIVESITEIMPORTDIALOG_H + +#include <QDialog> +#include <QThread> +#include <QHash> +#include <QMap> +#include <QAbstractTableModel> +#include <memory> + +#include "ui_divesiteimportdialog.h" +#include "core/divesite.h" + +namespace Ui { + class DivesiteImportDialog; +} + +class DivesiteImportedModel; + +class DivesiteImportDialog : public QDialog { + Q_OBJECT +public: + DivesiteImportDialog(struct dive_site_table &imported, QString source, QWidget *parent = 0 ); + ~DivesiteImportDialog(); + +public +slots: + void on_ok_clicked(); + void on_cancel_clicked(); + +private: + Ui::DivesiteImportDialog ui; + struct dive_site_table importedSites; + QString importedSource; + + DivesiteImportedModel *divesiteImportedModel; +}; + +#endif // DIVESITEIMPORTDIALOG_H diff --git a/desktop-widgets/divesiteimportdialog.ui b/desktop-widgets/divesiteimportdialog.ui new file mode 100644 index 000000000..8ee80ba22 --- /dev/null +++ b/desktop-widgets/divesiteimportdialog.ui @@ -0,0 +1,119 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>DivesiteImportDialog</class> + <widget class="QDialog" name="DivesiteImportDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>747</width> + <height>535</height> + </rect> + </property> + <property name="windowTitle"> + <string>Select dive sites to import</string> + </property> + <property name="windowIcon"> + <iconset> + <normalon>:subsurface-icon</normalon> + </iconset> + </property> + <layout class="QVBoxLayout" name="verticalLayout_3"> + <property name="spacing"> + <number>5</number> + </property> + <property name="leftMargin"> + <number>5</number> + </property> + <property name="topMargin"> + <number>5</number> + </property> + <property name="rightMargin"> + <number>5</number> + </property> + <property name="bottomMargin"> + <number>5</number> + </property> + <item> + <layout class="QHBoxLayout" name="labelSelectAllNoneLayout"> + <property name="spacing"> + <number>5</number> + </property> + <item> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="selectAllButton"> + <property name="text"> + <string>Select all</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="unselectAllButton"> + <property name="text"> + <string>Unselect all</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QTableView" name="importedDivesitesView"> + <property name="sizePolicy"> + <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding"> + <horstretch>1</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="buttonBoxLayout"> + <property name="spacing"> + <number>0</number> + </property> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="ok"> + <property name="text"> + <string>OK</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="cancel"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/qt-models/CMakeLists.txt b/qt-models/CMakeLists.txt index 449b450e7..34d7ad783 100644 --- a/qt-models/CMakeLists.txt +++ b/qt-models/CMakeLists.txt @@ -29,6 +29,8 @@ set(SUBSURFACE_DESKTOP_MODELS_LIB_SRCS divepicturemodel.h diveplannermodel.cpp diveplannermodel.h + divesiteimportmodel.cpp + divesiteimportmodel.h divetripmodel.cpp divetripmodel.h filtermodels.cpp diff --git a/qt-models/divesiteimportmodel.cpp b/qt-models/divesiteimportmodel.cpp new file mode 100644 index 000000000..2bd38d6b4 --- /dev/null +++ b/qt-models/divesiteimportmodel.cpp @@ -0,0 +1,142 @@ +#include "divesiteimportmodel.h" +#include "core/qthelper.h" +#include "core/taxonomy.h" + +DivesiteImportedModel::DivesiteImportedModel(QObject *o) : QAbstractTableModel(o), + firstIndex(0), + lastIndex(-1), + importedSitesTable(nullptr) +{ +} + +int DivesiteImportedModel::columnCount(const QModelIndex &) const +{ + return 5; +} + +int DivesiteImportedModel::rowCount(const QModelIndex &) const +{ + return lastIndex - firstIndex + 1; +} + +QVariant DivesiteImportedModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Vertical) + return QVariant(); + + if (role == Qt::DisplayRole) { + switch (section) { + case NAME: + return tr("Name"); + case LOCATION: + return tr("Location"); + case COUNTRY: + return tr("Country"); + case NEAREST: + return tr("Nearest\nExisting Site"); + case DISTANCE: + return tr("Distance"); + } + } + return QVariant(); +} + +QVariant DivesiteImportedModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() + firstIndex > lastIndex) + return QVariant(); + + struct dive_site *ds = get_dive_site(index.row() + firstIndex, importedSitesTable); + if (!ds) + return QVariant(); + + // widgets access the model via index.column() + // Not supporting QML access via roles + if (role == Qt::DisplayRole) { + switch (index.column()) { + case NAME: + return ds->name; + case LOCATION: + return printGPSCoords(&ds->location); + case COUNTRY: + return taxonomy_get_country(&ds->taxonomy); + case NEAREST: { + // 40075000 is circumference of the earth in meters + struct dive_site *nearest_ds = + get_dive_site_by_gps_proximity(&ds->location, + 40075000, &dive_site_table); + if (nearest_ds) + return nearest_ds->name; + else + return QString(); + } + case DISTANCE: { + unsigned int distance = 0; + struct dive_site *nearest_ds = + get_dive_site_by_gps_proximity(&ds->location, + 40075000, &dive_site_table); + if (nearest_ds) + distance = get_distance(&ds->location, + &nearest_ds->location); + return distance_string(distance); + } + case SELECTED: + return checkStates[index.row()]; + } + } + if (role == Qt::CheckStateRole) { + if (index.column() == 0) + return checkStates[index.row()] ? Qt::Checked : Qt::Unchecked; + } + return QVariant(); +} + +void DivesiteImportedModel::changeSelected(QModelIndex clickedIndex) +{ + checkStates[clickedIndex.row()] = !checkStates[clickedIndex.row()]; + dataChanged(index(clickedIndex.row(), 0), index(clickedIndex.row(), 0), QVector<int>() << Qt::CheckStateRole << SELECTED); +} + +void DivesiteImportedModel::selectAll() +{ + std::fill(checkStates.begin(), checkStates.end(), true); + dataChanged(index(0, 0), index(lastIndex - firstIndex, 0), QVector<int>() << Qt::CheckStateRole << SELECTED); +} + +void DivesiteImportedModel::selectRow(int row) +{ + checkStates[row] = !checkStates[row]; + dataChanged(index(row, 0), index(row, 0), QVector<int>() << Qt::CheckStateRole << SELECTED); +} + +void DivesiteImportedModel::selectNone() +{ + std::fill(checkStates.begin(), checkStates.end(), false); + dataChanged(index(0, 0), index(lastIndex - firstIndex,0 ), QVector<int>() << Qt::CheckStateRole << SELECTED); +} + +Qt::ItemFlags DivesiteImportedModel::flags(const QModelIndex &index) const +{ + if (index.column() != 0) + return QAbstractTableModel::flags(index); + return QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable; +} + +void DivesiteImportedModel::repopulate(struct dive_site_table *sites) +{ + beginResetModel(); + + importedSitesTable = sites; + firstIndex = 0; + lastIndex = importedSitesTable->nr - 1; + checkStates.resize(importedSitesTable->nr); + for (int row = 0; row < importedSitesTable->nr; row++) + if (get_dive_site_by_gps(&importedSitesTable->dive_sites[row]->location, &dive_site_table)) + checkStates[row] = false; + else + checkStates[row] = true; + endResetModel(); +} diff --git a/qt-models/divesiteimportmodel.h b/qt-models/divesiteimportmodel.h new file mode 100644 index 000000000..5c7fb27fe --- /dev/null +++ b/qt-models/divesiteimportmodel.h @@ -0,0 +1,35 @@ +#ifndef DIVESITEIMPORTEDMODEL_H +#define DIVESITEIMPORTEDMODEL_H + +#include <QAbstractTableModel> +#include <vector> +#include "core/divesite.h" + +class DivesiteImportedModel : public QAbstractTableModel +{ + Q_OBJECT +public: + enum columnNames { NAME, LOCATION, COUNTRY, NEAREST, DISTANCE, SELECTED }; + + DivesiteImportedModel(QObject *parent = 0); + int columnCount(const QModelIndex& index = QModelIndex()) const; + int rowCount(const QModelIndex& index = QModelIndex()) const; + QVariant data(const QModelIndex& index, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + void repopulate(dive_site_table_t *sites); +public +slots: + void changeSelected(QModelIndex clickedIndex); + void selectRow(int row); + void selectAll(); + void selectNone(); + +private: + int firstIndex; + int lastIndex; + std::vector<char> checkStates; // char instead of bool to avoid silly pessimization of std::vector. + struct dive_site_table *importedSitesTable; +}; + +#endif |