From b6ed9b2e7c8b8444ca51cfe7dc8c62ef588f56dd Mon Sep 17 00:00:00 2001 From: Jussi Kuokkanen Date: Thu, 31 Aug 2023 20:38:22 +0300 Subject: [PATCH] return optional from model traverse callback Allows callers to stop traversing model when they are done processing --- src/tuxclocker-qt/Utils.cpp | 11 ++++++----- src/tuxclocker-qt/Utils.hpp | 2 +- src/tuxclocker-qt/data/DeviceModelDelegate.cpp | 15 ++++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/tuxclocker-qt/Utils.cpp b/src/tuxclocker-qt/Utils.cpp index 7ec5f5b..fa54e6e 100644 --- a/src/tuxclocker-qt/Utils.cpp +++ b/src/tuxclocker-qt/Utils.cpp @@ -20,16 +20,17 @@ QVariant fromAssignmentArgument(TuxClocker::Device::AssignmentArgument arg) { return QVariant{}; } -using ModelTraverseCallback = - std::function; - void traverseModel( const ModelTraverseCallback &cb, QAbstractItemModel *model, const QModelIndex &parent) { for (int i = 0; i < model->rowCount(parent); i++) { + auto nextOpt = cb(model, parent, i); + if (!nextOpt.has_value()) + // Returning nothing is used as indication we should stop traversing + return; + // We get the next index we should traverse, and the funtion does // its thing with the model and index - auto nextIndex = cb(model, parent, i); - + auto nextIndex = nextOpt.value(); if (model->hasChildren(nextIndex)) { traverseModel(cb, model, nextIndex); } diff --git a/src/tuxclocker-qt/Utils.hpp b/src/tuxclocker-qt/Utils.hpp index 789f66b..fb31ffc 100644 --- a/src/tuxclocker-qt/Utils.hpp +++ b/src/tuxclocker-qt/Utils.hpp @@ -5,7 +5,7 @@ namespace Utils { using ModelTraverseCallback = - std::function; + std::function(QAbstractItemModel *, const QModelIndex &, int)>; void traverseModel( const ModelTraverseCallback &, QAbstractItemModel *, const QModelIndex &parent = QModelIndex()); diff --git a/src/tuxclocker-qt/data/DeviceModelDelegate.cpp b/src/tuxclocker-qt/data/DeviceModelDelegate.cpp index 60e4bd5..9189607 100644 --- a/src/tuxclocker-qt/data/DeviceModelDelegate.cpp +++ b/src/tuxclocker-qt/data/DeviceModelDelegate.cpp @@ -162,28 +162,29 @@ void DeviceModelDelegate::setAssignableData( } bool DeviceModelDelegate::subtreeHasAssignableDefaults( - QAbstractItemModel *model, const QModelIndex &item) { + QAbstractItemModel *model, const QModelIndex &index) { QSettings settings{"tuxclocker"}; settings.beginGroup("assignableDefaults"); bool hasDefaults = false; - auto cb = [&settings, &hasDefaults]( - QAbstractItemModel *model, const QModelIndex &index, int row) { + auto cb = [&settings, &hasDefaults](QAbstractItemModel *model, const QModelIndex &index, + int row) -> std::optional { auto ifaceIndex = model->index(row, DeviceModel::InterfaceColumn, index); auto assProxyV = ifaceIndex.data(DeviceModel::AssignableProxyRole); - qDebug() << model->index(row, DeviceModel::NameColumn, index).data(); + auto name = model->index(row, DeviceModel::NameColumn, index).data(); if (assProxyV.isValid()) { auto nodePath = qvariant_cast(assProxyV)->dbusPath(); if (settings.contains(nodePath.replace('/', '-'))) { - // TODO: break out of the function here + // This stops traversing model hasDefaults = true; + return std::nullopt; } } - return model->index(row, DeviceModel::NameColumn, index); }; - Utils::traverseModel(cb, model); + auto nameIndex = model->index(index.row(), DeviceModel::NameColumn, index.parent()); + Utils::traverseModel(cb, model, nameIndex); return hasDefaults; }