From 318d6759bd21b10e20a0edd2a9611d2411d8a0f8 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 5 Feb 2020 21:13:59 +0500 Subject: [PATCH] UI: ZonesWindow: Checkable zones. --- src/ui/conf/confmanager.cpp | 23 ++++++++++++ src/ui/conf/confmanager.h | 1 + src/ui/form/zone/zoneswindow.cpp | 4 +-- src/ui/model/applistmodel.cpp | 20 +++++------ src/ui/model/zonelistmodel.cpp | 62 ++++++++++++++++++++++++++------ src/ui/model/zonelistmodel.h | 4 +++ 6 files changed, 89 insertions(+), 25 deletions(-) diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index 57c8eb8b..53ebc56d 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -199,6 +199,12 @@ const char * const sqlUpdateZoneName = " WHERE zone_id = ?1;" ; +const char * const sqlUpdateZoneEnabled = + "UPDATE zone" + " SET enabled = ?2" + " WHERE zone_id = ?1;" + ; + const char * const sqlUpdateZoneResult = "UPDATE zone" " SET checksum = ?2, last_run = ?3, last_success = ?4" @@ -701,6 +707,23 @@ bool ConfManager::updateZoneName(qint64 zoneId, const QString &zoneName) return ok; } +bool ConfManager::updateZoneEnabled(qint64 zoneId, bool enabled) +{ + bool ok = false; + + const auto vars = QVariantList() + << zoneId + << enabled + ; + + m_sqliteDb->executeEx(sqlUpdateZoneEnabled, vars, 0, &ok); + if (!ok) { + showErrorMessage(m_sqliteDb->errorMessage()); + } + + return ok; +} + bool ConfManager::updateZoneResult(qint64 zoneId, const QString &checksum, const QDateTime &lastRun, const QDateTime &lastSuccess) diff --git a/src/ui/conf/confmanager.h b/src/ui/conf/confmanager.h index 824135a6..50bbbd09 100644 --- a/src/ui/conf/confmanager.h +++ b/src/ui/conf/confmanager.h @@ -73,6 +73,7 @@ public: const QString &sourceCode, const QString &url, const QString &formData, bool enabled, bool customUrl); bool updateZoneName(qint64 zoneId, const QString &zoneName); + bool updateZoneEnabled(qint64 zoneId, bool enabled); bool updateZoneResult(qint64 zoneId, const QString &checksum, const QDateTime &lastRun, const QDateTime &lastSuccess); diff --git a/src/ui/form/zone/zoneswindow.cpp b/src/ui/form/zone/zoneswindow.cpp index eb47265d..412b9c71 100644 --- a/src/ui/form/zone/zoneswindow.cpp +++ b/src/ui/form/zone/zoneswindow.cpp @@ -280,8 +280,8 @@ void ZonesWindow::setupTableZonesHeader() header->setSectionResizeMode(2, QHeaderView::Stretch); header->setSectionResizeMode(3, QHeaderView::Stretch); - header->resizeSection(0, 500); - header->resizeSection(1, 140); + header->resizeSection(0, 350); + header->resizeSection(1, 290); } void ZonesWindow::setupTableZonesChanged() diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index 8737ebd0..00c02269 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -84,18 +84,14 @@ int AppListModel::columnCount(const QModelIndex &parent) const QVariant AppListModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation == Qt::Horizontal) { - switch (role) { - case Qt::DisplayRole: { - switch (section) { - case 0: return tr("Program"); - case 1: return tr("Group"); - case 2: return tr("State"); - case 3: return tr("End Time"); - case 4: return tr("Creation Time"); - } - break; - } + if (orientation == Qt::Horizontal + && role == Qt::DisplayRole) { + switch (section) { + case 0: return tr("Program"); + case 1: return tr("Group"); + case 2: return tr("State"); + case 3: return tr("End Time"); + case 4: return tr("Creation Time"); } } return QVariant(); diff --git a/src/ui/model/zonelistmodel.cpp b/src/ui/model/zonelistmodel.cpp index 31b9484c..b805d153 100644 --- a/src/ui/model/zonelistmodel.cpp +++ b/src/ui/model/zonelistmodel.cpp @@ -36,17 +36,13 @@ int ZoneListModel::columnCount(const QModelIndex &parent) const QVariant ZoneListModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation == Qt::Horizontal) { - switch (role) { - case Qt::DisplayRole: { - switch (section) { - case 0: return tr("Zone"); - case 1: return tr("Source"); - case 2: return tr("Last Run"); - case 3: return tr("Last Success"); - } - break; - } + if (orientation == Qt::Horizontal + && role == Qt::DisplayRole) { + switch (section) { + case 0: return tr("Zone"); + case 1: return tr("Source"); + case 2: return tr("Last Run"); + case 3: return tr("Last Success"); } } return QVariant(); @@ -79,11 +75,45 @@ QVariant ZoneListModel::data(const QModelIndex &index, int role) const break; } + + case Qt::CheckStateRole: + if (index.column() == 0) { + const auto zoneRow = zoneRowAt(index.row()); + return zoneRow.enabled; + } + break; } return QVariant(); } +bool ZoneListModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + Q_UNUSED(value) + + if (!index.isValid()) + return false; + + switch (role) { + case Qt::CheckStateRole: + const auto zoneRow = zoneRowAt(index.row()); + return updateZoneEnabled(zoneRow.zoneId, !zoneRow.enabled); + } + + return false; +} + +Qt::ItemFlags ZoneListModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::NoItemFlags; + + const int column = index.column(); + + return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren + | (column == 0 ? Qt::ItemIsUserCheckable : Qt::NoItemFlags); +} + const ZoneRow &ZoneListModel::zoneRowAt(int row) const { updateRowCache(row); @@ -128,6 +158,16 @@ bool ZoneListModel::updateZoneName(qint64 zoneId, const QString &zoneName) return false; } +bool ZoneListModel::updateZoneEnabled(qint64 zoneId, bool enabled) +{ + if (confManager()->updateZoneEnabled(zoneId, enabled)) { + refresh(); + return true; + } + + return false; +} + bool ZoneListModel::updateZoneResult(qint64 zoneId, const QString &checksum, const QDateTime &lastRun, const QDateTime &lastSuccess) diff --git a/src/ui/model/zonelistmodel.h b/src/ui/model/zonelistmodel.h index dc88ef35..5b4be1c5 100644 --- a/src/ui/model/zonelistmodel.h +++ b/src/ui/model/zonelistmodel.h @@ -45,6 +45,9 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + + Qt::ItemFlags flags(const QModelIndex &index) const override; const ZoneRow &zoneRowAt(int row) const; @@ -56,6 +59,7 @@ public: const QString &formData, bool enabled, bool customUrl, bool updateDriver = true); bool updateZoneName(qint64 zoneId, const QString &zoneName); + bool updateZoneEnabled(qint64 zoneId, bool enabled); bool updateZoneResult(qint64 zoneId, const QString &checksum, const QDateTime &lastRun, const QDateTime &lastSuccess);