EnergySpectrumAnalyer/src/BackgroundTaskListView/BackgroundTaskListModel.cpp

88 lines
2.0 KiB
C++

#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()
{
}