From c332358f8c55f2b88be9b3b3f1bea82c3ec06cf3 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 3 Feb 2024 13:27:49 +0300 Subject: [PATCH] UI: TaskListModel: Simplify *Data() --- src/ui/model/applistmodel.cpp | 7 +++--- src/ui/task/tasklistmodel.cpp | 47 ++++++++++++++++++++++++----------- src/ui/task/tasklistmodel.h | 2 ++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index 1f11a56d..ee8a9ff3 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -168,16 +168,15 @@ inline QVariant headerDataDisplay(int column, int role) return func(role); } -inline QVariant headerDataDecoration(int section) +inline QVariant headerDataDecoration(int column) { - switch (section) { + switch (column) { case 1: return IconCache::icon(":/icons/parking.png"); case 2: return IconCache::icon(":/icons/time.png"); - default: - return QVariant(); } + return QVariant(); } QVariant dataDisplayName(const AppRow &appRow, int role) diff --git a/src/ui/task/tasklistmodel.cpp b/src/ui/task/tasklistmodel.cpp index a58a1130..5853bab7 100644 --- a/src/ui/task/tasklistmodel.cpp +++ b/src/ui/task/tasklistmodel.cpp @@ -5,6 +5,25 @@ #include "taskinfo.h" #include "taskmanager.h" +namespace { + +inline QVariant headerDataDisplay(int column) +{ + switch (column) { + case 0: + return TaskListModel::tr("Name"); + case 1: + return TaskListModel::tr("Interval, hours"); + case 2: + return TaskListModel::tr("Last Run"); + case 3: + return TaskListModel::tr("Last Success"); + } + return QVariant(); +} + +} + TaskListModel::TaskListModel(TaskManager *taskManager, QObject *parent) : TableItemModel(parent), m_taskManager(taskManager) { @@ -38,16 +57,12 @@ int TaskListModel::columnCount(const QModelIndex &parent) const QVariant TaskListModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation == Qt::Horizontal && (role == Qt::DisplayRole || role == Qt::ToolTipRole)) { - switch (section) { - case 0: - return tr("Name"); - case 1: - return tr("Interval, hours"); - case 2: - return tr("Last Run"); - case 3: - return tr("Last Success"); + if (orientation == Qt::Horizontal) { + switch (role) { + // Label + case Qt::DisplayRole: + case Qt::ToolTipRole: + return headerDataDisplay(section); } } return QVariant(); @@ -73,10 +88,8 @@ QVariant TaskListModel::data(const QModelIndex &index, int role) const case RoleIntervalHours: return taskIntervalHours(index.row()); - case RoleRunning: { - const auto taskInfo = taskInfoAt(index.row()); - return taskInfo->running(); - } + case RoleRunning: + return taskRunning(index.row()); } return QVariant(); @@ -220,6 +233,12 @@ void TaskListModel::setTaskIntervalHours(const QModelIndex &index, int v) emitDataEdited(index, Qt::DisplayRole); } +bool TaskListModel::taskRunning(int row) const +{ + const auto taskInfo = taskInfoAt(row); + return taskInfo->running(); +} + void TaskListModel::emitDataEdited(const QModelIndex &index, int role) { emit dataChanged(index, index, { role }); diff --git a/src/ui/task/tasklistmodel.h b/src/ui/task/tasklistmodel.h index 039e03d5..3c378504 100644 --- a/src/ui/task/tasklistmodel.h +++ b/src/ui/task/tasklistmodel.h @@ -64,6 +64,8 @@ private: int taskIntervalHours(int row) const; void setTaskIntervalHours(const QModelIndex &index, int v); + bool taskRunning(int row) const; + TaskEditInfo &taskRowAt(int row) { return m_taskRows[row]; } const TaskEditInfo &taskRowAt(int row) const { return m_taskRows[row]; }