Cleanup: Turn DiveListSortModel into classical singleton

To make this class available from QMLManager, the run_ui()
function would create the object and then set a pointer in
QMLManager. It works, but is inconsistent with the rest of
the code. Therefore, make it a classical singleton class,
which is generated on demand.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-09-28 23:31:40 +02:00 committed by Dirk Hohndel
parent 2a9a3dda20
commit 3db50aedea
4 changed files with 12 additions and 7 deletions

View file

@ -825,7 +825,7 @@ void QMLManager::consumeFinishedLoad(timestamp_t currentDiveTimestamp)
process_loaded_dives(); process_loaded_dives();
DiveListModel::instance()->reload(); DiveListModel::instance()->reload();
if (currentDiveTimestamp) if (currentDiveTimestamp)
setUpdateSelectedDive(dlSortModel->getIdxForId(get_dive_id_closest_to(currentDiveTimestamp))); setUpdateSelectedDive(DiveListSortModel::instance()->getIdxForId(get_dive_id_closest_to(currentDiveTimestamp)));
appendTextToLog(QStringLiteral("%1 dives loaded").arg(dive_table.nr)); appendTextToLog(QStringLiteral("%1 dives loaded").arg(dive_table.nr));
if (dive_table.nr == 0) if (dive_table.nr == 0)
setStartPageText(tr("Cloud storage open successfully. No dives in dive list.")); setStartPageText(tr("Cloud storage open successfully. No dives in dive list."));
@ -2056,7 +2056,7 @@ void QMLManager::setFilter(const QString filterText)
QMetaObject::invokeMethod(qmlWindow, "showBusy"); QMetaObject::invokeMethod(qmlWindow, "showBusy");
QtConcurrent::run(QThreadPool::globalInstance(), QtConcurrent::run(QThreadPool::globalInstance(),
[=]{ [=]{
dlSortModel->setFilter(filterText); DiveListSortModel::instance()->setFilter(filterText);
QMetaObject::invokeMethod(qmlWindow, "hideBusy"); QMetaObject::invokeMethod(qmlWindow, "hideBusy");
}); });
} }

View file

@ -6,7 +6,7 @@
#include "core/ssrf.h" // for LOG_STP #include "core/ssrf.h" // for LOG_STP
#include <QDateTime> #include <QDateTime>
DiveListSortModel::DiveListSortModel(QObject *parent) : QSortFilterProxyModel(parent) DiveListSortModel::DiveListSortModel()
{ {
setSourceModel(DiveListModel::instance()); setSourceModel(DiveListModel::instance());
setDynamicSortFilter(true); setDynamicSortFilter(true);
@ -16,6 +16,12 @@ DiveListSortModel::DiveListSortModel(QObject *parent) : QSortFilterProxyModel(pa
LOG_STP("run_ui diveListModel sorted"); LOG_STP("run_ui diveListModel sorted");
} }
DiveListSortModel *DiveListSortModel::instance()
{
static DiveListSortModel self;
return &self;
}
void DiveListSortModel::updateFilterState() void DiveListSortModel::updateFilterState()
{ {
if (filterString.isEmpty()) { if (filterString.isEmpty()) {

View file

@ -11,7 +11,8 @@ class DiveListSortModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
public: public:
DiveListSortModel(QObject *parent = 0); DiveListSortModel();
static DiveListSortModel *instance();
void setSourceModel(QAbstractItemModel *sourceModel); void setSourceModel(QAbstractItemModel *sourceModel);
Q_INVOKABLE void reload(); Q_INVOKABLE void reload();
Q_INVOKABLE QString tripTitle(const QString &trip); Q_INVOKABLE QString tripTitle(const QString &trip);

View file

@ -96,14 +96,13 @@ void run_ui()
qDebug() << "QML import path" << engine.importPathList(); qDebug() << "QML import path" << engine.importPathList();
#endif // __APPLE__ not Q_OS_IOS #endif // __APPLE__ not Q_OS_IOS
engine.addImportPath("qrc://imports"); engine.addImportPath("qrc://imports");
DiveListSortModel *sortModel = new DiveListSortModel(0);
QSortFilterProxyModel *gpsSortModel = new QSortFilterProxyModel(nullptr); QSortFilterProxyModel *gpsSortModel = new QSortFilterProxyModel(nullptr);
gpsSortModel->setSourceModel(GpsListModel::instance()); gpsSortModel->setSourceModel(GpsListModel::instance());
gpsSortModel->setDynamicSortFilter(true); gpsSortModel->setDynamicSortFilter(true);
gpsSortModel->setSortRole(GpsListModel::GpsWhenRole); gpsSortModel->setSortRole(GpsListModel::GpsWhenRole);
gpsSortModel->sort(0, Qt::DescendingOrder); gpsSortModel->sort(0, Qt::DescendingOrder);
QQmlContext *ctxt = engine.rootContext(); QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("diveModel", sortModel); ctxt->setContextProperty("diveModel", DiveListSortModel::instance());
ctxt->setContextProperty("gpsModel", gpsSortModel); ctxt->setContextProperty("gpsModel", gpsSortModel);
ctxt->setContextProperty("vendorList", vendorList); ctxt->setContextProperty("vendorList", vendorList);
set_non_bt_addresses(); set_non_bt_addresses();
@ -135,7 +134,6 @@ void run_ui()
LOG_STP("run_ui show_computer_list"); LOG_STP("run_ui show_computer_list");
manager->setDevicePixelRatio(qml_window->devicePixelRatio(), qml_window->screen()); manager->setDevicePixelRatio(qml_window->devicePixelRatio(), qml_window->screen());
manager->dlSortModel = sortModel;
manager->qmlWindow = qqWindowObject; manager->qmlWindow = qqWindowObject;
manager->screenChanged(screen); manager->screenChanged(screen);
qDebug() << "qqwindow screen has ldpi/pdpi" << screen->logicalDotsPerInch() << screen->physicalDotsPerInch(); qDebug() << "qqwindow screen has ldpi/pdpi" << screen->logicalDotsPerInch() << screen->physicalDotsPerInch();