From 00ec824129ea2662ace5a4077ba9bc865ecc70ff Mon Sep 17 00:00:00 2001 From: Doug Junkins Date: Sat, 4 May 2019 20:51:16 -0700 Subject: Create DivesiteImportDialog to select sites to import Creates the dialog box to select which sites to import from the file selected in mainwindow.cpp. The DivesiteImportModel is created as a table to display and select which sites are to be imported. Once the sites are selected, the Command::importDiveSites command is called to add the sites to the core dive site table with undo/redo functions. Signed-off-by: Doug Junkins --- desktop-widgets/CMakeLists.txt | 3 + desktop-widgets/divesiteimportdialog.cpp | 80 +++++++++++++++++++++ desktop-widgets/divesiteimportdialog.h | 40 +++++++++++ desktop-widgets/divesiteimportdialog.ui | 119 +++++++++++++++++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 desktop-widgets/divesiteimportdialog.cpp create mode 100644 desktop-widgets/divesiteimportdialog.h create mode 100644 desktop-widgets/divesiteimportdialog.ui (limited to 'desktop-widgets') 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 + +// 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 +#include +#include +#include +#include +#include + +#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 @@ + + + DivesiteImportDialog + + + + 0 + 0 + 747 + 535 + + + + Select dive sites to import + + + + :subsurface-icon + + + + + 5 + + + 5 + + + 5 + + + 5 + + + 5 + + + + + 5 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Select all + + + + + + + Unselect all + + + + + + + + + + 1 + 0 + + + + + + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + OK + + + + + + + Cancel + + + + + + + + + + -- cgit v1.2.3-70-g09d2