summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/divesiteimportdialog.cpp
diff options
context:
space:
mode:
authorGravatar Doug Junkins <junkins@foghead.com>2019-05-04 20:51:16 -0700
committerGravatar bstoeger <32835590+bstoeger@users.noreply.github.com>2019-05-06 10:48:44 +0200
commit00ec824129ea2662ace5a4077ba9bc865ecc70ff (patch)
treec722c55a48a6c226e879e3f2e915adb6fd3b13d6 /desktop-widgets/divesiteimportdialog.cpp
parent98b3a326bd952c616843694821a48e0c029db699 (diff)
downloadsubsurface-00ec824129ea2662ace5a4077ba9bc865ecc70ff.tar.gz
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 <junkins@foghead.com>
Diffstat (limited to 'desktop-widgets/divesiteimportdialog.cpp')
-rw-r--r--desktop-widgets/divesiteimportdialog.cpp80
1 files changed, 80 insertions, 0 deletions
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();
+}