summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-12-10 14:49:32 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-12-17 07:37:32 -0800
commitbfe69239df6a8d2b2ae986b850246727a21e127b (patch)
tree04c5eeb37790f81861f80a93b28fa752d9a1fe9b
parenta6a5cf61e2f45eb54721c7ee2dbcc05afe8939f9 (diff)
downloadsubsurface-bfe69239df6a8d2b2ae986b850246727a21e127b.tar.gz
Import: unglobalize downloadTable
To make data flow more clear, unglobalize the downloadTable object. Make it a subobject of DownloadThread. The difficult part was making this compatible with QML, because somehow the pointer to the download-table has to be passed to the DiveImportedModel. Desktop would simply pass it to the constructor. But with objects generated in QML this is not possible. Instead, pass the table in the repopulate() function. This seems to make sense, but for this to work, we have to declare pointer-to-dive-table as a Q_METATYPE. And this only works if we use a typedef, because MOC removes the "struct" from "struct dive_table". This leads to compilation errors, because dive_table is the symbol-name of the global dive table! Sigh. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r--core/dive.h14
-rw-r--r--core/divelist.c3
-rw-r--r--core/downloadfromdcthread.cpp11
-rw-r--r--core/downloadfromdcthread.h4
-rw-r--r--core/libdivecomputer.c2
-rw-r--r--desktop-widgets/downloadfromdivecomputer.cpp21
-rw-r--r--mobile-widgets/qml/DownloadFromDiveComputer.qml2
-rw-r--r--qt-models/diveimportedmodel.cpp12
-rw-r--r--qt-models/diveimportedmodel.h4
9 files changed, 38 insertions, 35 deletions
diff --git a/core/dive.h b/core/dive.h
index d544bc9d1..6bc0f6fae 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -273,10 +273,10 @@ struct divecomputer {
#define W_IDX_PRIMARY 0
#define W_IDX_SECONDARY 1
-struct dive_table {
+typedef struct dive_table {
int nr, allocated;
struct dive **dives;
-};
+} dive_table_t;
typedef struct dive_trip
{
@@ -419,7 +419,7 @@ extern const struct units SI_units, IMPERIAL_units;
extern const struct units *get_units(void);
extern int run_survey, verbose, quit, force_root;
-extern struct dive_table dive_table, downloadTable;
+extern struct dive_table dive_table;
extern struct dive displayed_dive;
extern unsigned int dc_number;
extern struct dive *current_dive;
@@ -751,10 +751,14 @@ extern void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_de
#ifdef __cplusplus
}
-/* Make pointers to dive and dive_trip "Qt metatypes" so that they can
- * be passed through QVariants. */
+/* Make pointers to dive, dive_trip and dive_table "Qt metatypes" so that they can
+ * be passed through QVariants and through QML.
+ * Note: we have to use the typedef "dive_table_t" instead of "struct dive_table",
+ * because MOC removes the "struct", but dive_table is already the name of a global
+ * variable, leading to compilation errors. */
Q_DECLARE_METATYPE(struct dive *);
Q_DECLARE_METATYPE(struct dive_trip *);
+Q_DECLARE_METATYPE(dive_table_t *);
#endif
diff --git a/core/divelist.c b/core/divelist.c
index 4b809da37..4659261cd 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -74,9 +74,6 @@ dive_trip_t *dive_trip_list;
unsigned int amount_selected;
-// We need to stop using globals, really.
-struct dive_table downloadTable;
-
#if DEBUG_SELECTION_TRACKING
void dump_selection(void)
{
diff --git a/core/downloadfromdcthread.cpp b/core/downloadfromdcthread.cpp
index ec0705cc1..2fdebf129 100644
--- a/core/downloadfromdcthread.cpp
+++ b/core/downloadfromdcthread.cpp
@@ -59,9 +59,9 @@ static void updateRememberedDCs()
}
-DownloadThread::DownloadThread()
+DownloadThread::DownloadThread() : downloadTable({ 0 }),
+ m_data(DCDeviceData::instance())
{
- m_data = DCDeviceData::instance();
}
void DownloadThread::run()
@@ -80,7 +80,7 @@ void DownloadThread::run()
internalData->devname = "ftdi";
#endif
qDebug() << "Starting download from " << (internalData->bluetooth_mode ? "BT" : internalData->devname);
- downloadTable.nr = 0;
+ clear_table(&downloadTable);
Q_ASSERT(internalData->download_table != nullptr);
const char *errorText;
@@ -302,6 +302,11 @@ DCDeviceData *DownloadThread::data()
return m_data;
}
+struct dive_table *DownloadThread::table()
+{
+ return &downloadTable;
+}
+
QString DCDeviceData::vendor() const
{
return data.vendor;
diff --git a/core/downloadfromdcthread.h b/core/downloadfromdcthread.h
index 1ae6a4087..b380a88a1 100644
--- a/core/downloadfromdcthread.h
+++ b/core/downloadfromdcthread.h
@@ -6,6 +6,7 @@
#include <QHash>
#include <QLoggingCategory>
+#include "dive.h"
#include "libdivecomputer.h"
#include "connectionlistmodel.h"
#if BT_SUPPORT
@@ -59,15 +60,18 @@ private:
class DownloadThread : public QThread {
Q_OBJECT
+ Q_PROPERTY(dive_table_t *table READ table CONSTANT)
public:
DownloadThread();
void run() override;
DCDeviceData *data();
+ struct dive_table *table();
QString error;
private:
+ struct dive_table downloadTable;
DCDeviceData *m_data;
};
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
index a2e2890b8..4d2d29bad 100644
--- a/core/libdivecomputer.c
+++ b/core/libdivecomputer.c
@@ -1387,7 +1387,7 @@ const char *do_libdivecomputer_import(device_data_t *data)
/* TODO: Show the logfile to the user on error. */
dc_device_close(data->device);
data->device = NULL;
- if (!downloadTable.nr)
+ if (!data->download_table->nr)
dev_info(data, translate("gettextFromC", "No new dives downloaded from dive computer"));
}
dc_iostream_close(data->iostream);
diff --git a/desktop-widgets/downloadfromdivecomputer.cpp b/desktop-widgets/downloadfromdivecomputer.cpp
index e88817cc8..f92fdbb70 100644
--- a/desktop-widgets/downloadfromdivecomputer.cpp
+++ b/desktop-widgets/downloadfromdivecomputer.cpp
@@ -29,14 +29,12 @@ DownloadFromDCWidget::DownloadFromDCWidget(QWidget *parent, Qt::WindowFlags f) :
currentState(INITIAL)
{
diveImportedModel = new DiveImportedModel(this);
- diveImportedModel->setDiveTable(&downloadTable);
vendorModel.setStringList(vendorList);
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
int startingWidth = defaultModelFont().pointSize();
- clear_table(&downloadTable);
ui.setupUi(this);
ui.progressBar->hide();
ui.progressBar->setMinimum(0);
@@ -254,7 +252,7 @@ void DownloadFromDCWidget::updateState(states state)
markChildrenAsEnabled();
progress_bar_text = "";
} else {
- if (downloadTable.nr != 0)
+ if (thread.table()->nr != 0)
progress_bar_text = "";
ui.progressBar->setValue(100);
markChildrenAsEnabled();
@@ -349,7 +347,7 @@ void DownloadFromDCWidget::on_downloadCancelRetryButton_clicked()
// this means we are retrying - so we better clean out the partial
// list of downloaded dives from the last attempt
diveImportedModel->clearTable();
- clear_table(&downloadTable);
+ clear_table(thread.table());
}
updateState(DOWNLOADING);
@@ -492,7 +490,7 @@ void DownloadFromDCWidget::onDownloadThreadFinished()
}
ui.downloadCancelRetryButton->setText(tr("Retry download"));
ui.downloadCancelRetryButton->setEnabled(true);
- diveImportedModel->repopulate();
+ diveImportedModel->repopulate(thread.table());
}
void DownloadFromDCWidget::on_cancel_clicked()
@@ -501,7 +499,7 @@ void DownloadFromDCWidget::on_cancel_clicked()
return;
// now discard all the dives
- clear_table(&downloadTable);
+ clear_table(thread.table());
done(-1);
}
@@ -509,23 +507,24 @@ void DownloadFromDCWidget::on_ok_clicked()
{
if (currentState != DONE && currentState != ERROR)
return;
+ struct dive_table *table = thread.table();
// delete non-selected dives
- int total = downloadTable.nr;
+ int total = table->nr;
int j = 0;
for (int i = 0; i < total; i++) {
if (diveImportedModel->data(diveImportedModel->index(i, 0), Qt::CheckStateRole) == Qt::Checked)
j++;
else
- delete_dive_from_table(&downloadTable, j);
+ delete_dive_from_table(thread.table(), j);
}
- if (downloadTable.nr > 0) {
+ if (table->nr > 0) {
MainWindow::instance()->diveList->unselectDives();
// remember the last downloaded dive (on most dive computers this will be the chronologically
// first new dive) and select it again after processing all the dives
- int uniqId = downloadTable.dives[downloadTable.nr - 1]->id;
- process_imported_dives(&downloadTable, preferDownloaded(), true);
+ int uniqId = table->dives[table->nr - 1]->id;
+ process_imported_dives(table, preferDownloaded(), true);
autogroup_dives();
Command::clear();
// after process_imported_dives does any merging or resorting needed, we need
diff --git a/mobile-widgets/qml/DownloadFromDiveComputer.qml b/mobile-widgets/qml/DownloadFromDiveComputer.qml
index 142465f87..5f4aff985 100644
--- a/mobile-widgets/qml/DownloadFromDiveComputer.qml
+++ b/mobile-widgets/qml/DownloadFromDiveComputer.qml
@@ -28,7 +28,7 @@ Kirigami.Page {
id: downloadThread
onFinished : {
- importModel.repopulate()
+ importModel.repopulate(table)
progressBar.visible = false
if (dcImportModel.rowCount() > 0) {
console.log(dcImportModel.rowCount() + " dive downloaded")
diff --git a/qt-models/diveimportedmodel.cpp b/qt-models/diveimportedmodel.cpp
index be9b4275d..3e8386b94 100644
--- a/qt-models/diveimportedmodel.cpp
+++ b/qt-models/diveimportedmodel.cpp
@@ -6,8 +6,6 @@ DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o),
lastIndex(-1),
diveTable(nullptr)
{
- // Defaults to downloadTable, can be changed later.
- diveTable = &downloadTable;
}
int DiveImportedModel::columnCount(const QModelIndex&) const
@@ -45,11 +43,6 @@ QVariant DiveImportedModel::headerData(int section, Qt::Orientation orientation,
return QVariant();
}
-void DiveImportedModel::setDiveTable(struct dive_table* table)
-{
- diveTable = table;
-}
-
QVariant DiveImportedModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
@@ -134,10 +127,11 @@ void DiveImportedModel::clearTable()
endRemoveRows();
}
-void DiveImportedModel::repopulate()
+void DiveImportedModel::repopulate(dive_table_t *table)
{
beginResetModel();
+ diveTable = table;
firstIndex = 0;
lastIndex = diveTable->nr - 1;
checkStates.resize(diveTable->nr);
@@ -159,7 +153,7 @@ void DiveImportedModel::recordDives()
if (checkStates[i])
j++;
else
- delete_dive_from_table(&downloadTable, j);
+ delete_dive_from_table(diveTable, j);
}
process_imported_dives(diveTable, true, true);
diff --git a/qt-models/diveimportedmodel.h b/qt-models/diveimportedmodel.h
index fc4d9147b..0c5ba34cd 100644
--- a/qt-models/diveimportedmodel.h
+++ b/qt-models/diveimportedmodel.h
@@ -3,6 +3,7 @@
#include <QAbstractTableModel>
#include <vector>
+#include "core/dive.h"
class DiveImportedModel : public QAbstractTableModel
{
@@ -11,7 +12,6 @@ public:
enum roleTypes { DateTime = Qt::UserRole + 1, Duration, Depth, Selected};
DiveImportedModel(QObject *parent = 0);
- void setDiveTable(struct dive_table *table);
int columnCount(const QModelIndex& index = QModelIndex()) const;
int rowCount(const QModelIndex& index = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
@@ -20,7 +20,7 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const;
Q_INVOKABLE void clearTable();
QHash<int, QByteArray> roleNames() const;
- Q_INVOKABLE void repopulate();
+ Q_INVOKABLE void repopulate(dive_table_t *table);
Q_INVOKABLE void recordDives();
public
slots: