From 8dd2f44dc55d293f0336feb1a0115219ab443b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B5=B7?= Date: Mon, 25 May 2026 10:40:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E6=B7=BB=E5=8A=A0=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E4=BB=BB=E5=8A=A1=E6=98=BE=E7=A4=BA=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E6=9C=AA=E6=8F=90=E4=BA=A4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BackgroundTaskListModel.cpp | 88 +++++++++++++++++++ .../BackgroundTaskListModel.h | 37 ++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/BackgroundTaskListView/BackgroundTaskListModel.cpp create mode 100644 src/BackgroundTaskListView/BackgroundTaskListModel.h diff --git a/src/BackgroundTaskListView/BackgroundTaskListModel.cpp b/src/BackgroundTaskListView/BackgroundTaskListModel.cpp new file mode 100644 index 0000000..1bc5cd2 --- /dev/null +++ b/src/BackgroundTaskListView/BackgroundTaskListModel.cpp @@ -0,0 +1,88 @@ +#include "BackgroundTaskListModel.h" +#include "DataProcessWorkPool.h" + + +BackgroundTaskListModel* BackgroundTaskListModel::_s_instance = nullptr; + +BackgroundTaskListModel *BackgroundTaskListModel::Instance() +{ + if (!_s_instance) { + _s_instance = new BackgroundTaskListModel(); + } + return _s_instance; +} + +BackgroundTaskListModel::~BackgroundTaskListModel() +{ + Clear(); +} + +BackgroundTaskListModel::BackgroundTaskListModel(QObject *parent) + : QAbstractTableModel(parent) +{ +} + +int BackgroundTaskListModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + return _task_list.size(); +} + +int BackgroundTaskListModel::columnCount(const QModelIndex &parent) const +{ + return parent.isValid() ? 0 : 3; +} + +QVariant BackgroundTaskListModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + int row = index.row(); + int col = index.column(); + if (row < 0 || row >= _task_list.size()) + return QVariant(); + + if (role == Qt::DisplayRole) { + switch(col) { + case 0: return row + 1; + case 2: return _task_list.at(row)->GetTaskName(); + default: return QVariant(); + } + } + return QVariant(); +} + +int BackgroundTaskListModel::findTaskIndexByName(const QString &task_name) +{ + for (int i = 0; i < _task_list.size(); ++i) { + if (_task_list[i]->GetTaskName() == task_name) { + return i; + } + } + return -1; +} + +void BackgroundTaskListModel::InserTask(DataProcessWorkPool::DataProcessTask* task) +{ + beginInsertRows(QModelIndex(), _task_list.size(), _task_list.size()); + _task_list.append(task); + endInsertRows(); +} + +void BackgroundTaskListModel::RemoveTask(const QString &task_name) +{ + int index = findTaskIndexByName(task_name); + if (index < 0) { + return; + } + beginRemoveRows(QModelIndex(), _task_list.size(), _task_list.size()); + _task_list.removeAt(index); + endRemoveRows(); +} + +void BackgroundTaskListModel::Clear() +{ + +} \ No newline at end of file diff --git a/src/BackgroundTaskListView/BackgroundTaskListModel.h b/src/BackgroundTaskListView/BackgroundTaskListModel.h new file mode 100644 index 0000000..8bee575 --- /dev/null +++ b/src/BackgroundTaskListView/BackgroundTaskListModel.h @@ -0,0 +1,37 @@ +#ifndef BACKGROUNDTASKLISTMODEL_H +#define BACKGROUNDTASKLISTMODEL_H + +#include +#include + +namespace DataProcessWorkPool { +class DataProcessTask; +} + +class BackgroundTaskListModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + static BackgroundTaskListModel* Instance(); + virtual ~BackgroundTaskListModel(); + + void InserTask(DataProcessWorkPool::DataProcessTask *task); + void RemoveTask(const QString& task_name); + void Clear(); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + +private: + int findTaskIndexByName(const QString& task_name); + +private: + explicit BackgroundTaskListModel(QObject *parent = nullptr); + static BackgroundTaskListModel* _s_instance; +private: + QList _task_list; +}; + +#endif // BACKGROUNDTASKLISTMODEL_H