UI: TaskListModel: Simplify *Data()

This commit is contained in:
Nodir Temirkhodjaev 2024-02-03 13:27:49 +03:00
parent a9e6308276
commit c332358f8c
3 changed files with 38 additions and 18 deletions

View File

@ -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)

View File

@ -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 });

View File

@ -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]; }