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>
This commit is contained in:
Doug Junkins 2019-05-04 20:51:16 -07:00 committed by bstoeger
parent 98b3a326bd
commit 00ec824129
7 changed files with 421 additions and 0 deletions

View file

@ -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

View file

@ -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();
}

View file

@ -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

View file

@ -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>

View file

@ -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

View file

@ -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();
}

View file

@ -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