Skip to content

Commit

Permalink
Add some helper fucntions to the abstract tasks model
Browse files Browse the repository at this point in the history
Simplify some code in the abstract tasks tree model
Work on modal task handler bug (wip)
[skip ci]
  • Loading branch information
ThomasKroes committed Sep 26, 2023
1 parent b08eb7a commit 64d0c0a
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 44 deletions.
17 changes: 17 additions & 0 deletions HDPS/src/AbstractTasksModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,21 @@ AbstractTasksModel::AbstractTasksModel(QObject* parent /*= nullptr*/) :
setHorizontalHeaderItem(static_cast<int>(column), new HeaderItem(columnInfo[column]));
}

QStandardItem* AbstractTasksModel::itemFromTask(Task* task) const
{
const auto matches = match(index(0, static_cast<int>(Column::ID)), Qt::EditRole, task->getId(), 1, Qt::MatchExactly | Qt::MatchRecursive);

if (matches.isEmpty())
throw std::runtime_error(QString("%1 not found").arg(task->getParentTask()->getName()).toStdString());

auto item = itemFromIndex(matches.first().siblingAtColumn(static_cast<int>(Column::ExpandCollapse)));

Q_ASSERT(item != nullptr);

if (item == nullptr)
throw std::runtime_error("Parent standard item may not be a nullptr");

return item;
}

}
6 changes: 6 additions & 0 deletions HDPS/src/AbstractTasksModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ class AbstractTasksModel : public QStandardItemModel
*/
AbstractTasksModel(QObject* parent = nullptr);

/**
* Get item from \p task
* @return Pointer to found item, nullptr otherwise
*/
QStandardItem* itemFromTask(Task* task) const;

friend class Item;
};

Expand Down
2 changes: 2 additions & 0 deletions HDPS/src/BackgroundTaskTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ BackgroundTaskTester::BackgroundTaskTester(QObject* parent, const QString& name)
const auto addChildTask = [this, &timers](const QString& name, QStringList tasks, int interval) -> Task* {
auto childTask = new BackgroundTask(nullptr, name);

//childTask->setParentTask(Application::current()->getTask(Application::TaskType::OverallBackground));

if (!tasks.isEmpty()) {
childTask->setSubtasks(tasks);
childTask->setRunning();
Expand Down
32 changes: 17 additions & 15 deletions HDPS/src/ModalTaskHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ ModalTaskHandler::ModalTaskHandler(QObject* parent) :

connect(&_minimumDurationTimer, &QTimer::timeout, this, &ModalTaskHandler::updateDialogVisibility);

connect(&_tasksFilterModel, &QSortFilterProxyModel::layoutChanged, this, updateVisibilityDeferred);
connect(&_tasksFilterModel, &QSortFilterProxyModel::rowsInserted, this, updateVisibilityDeferred);
connect(&_tasksFilterModel, &QSortFilterProxyModel::rowsRemoved, this, updateVisibilityDeferred);
//connect(&_tasksFilterModel, &QSortFilterProxyModel::layoutChanged, this, [updateVisibilityDeferred]() { qDebug() << " QSortFilterProxyModel::layoutChanged"; });
//connect(tasks().getTreeModel(), &QStandardItemModel::rowsInserted, this, [this, updateVisibilityDeferred]() { qDebug() << " QStandardItemModel::rowsInserted"; });
connect(&_tasksFilterModel, &QSortFilterProxyModel::rowsInserted, this, [this, updateVisibilityDeferred]() { qDebug() << " QSortFilterProxyModel::rowsInserted"; updateDialogVisibility(); });
connect(&_tasksFilterModel, &QSortFilterProxyModel::rowsRemoved, this, [this, updateVisibilityDeferred]() { qDebug() << " QSortFilterProxyModel::rowsRemoved"; updateDialogVisibility(); });
}

void ModalTaskHandler::updateDialogVisibility()
{
const auto numberOfModalTasks = getTasksFilterModel().rowCount();

qDebug() << " " << __FUNCTION__ << numberOfModalTasks;

for (int rowIndex = 0; rowIndex < numberOfModalTasks; ++rowIndex) {
const auto sourceModelIndex = getTasksFilterModel().mapToSource(getTasksFilterModel().index(rowIndex, static_cast<int>(AbstractTasksModel::Column::Progress)));

Expand All @@ -68,7 +71,7 @@ void ModalTaskHandler::updateDialogVisibility()
if (progressItem == nullptr)
continue;

qDebug() << " " << __FUNCTION__ << progressItem->getTask()->getName();
qDebug() << " " << __FUNCTION__ << progressItem->getTask()->getName();
}

if (numberOfModalTasks == 0 && _modalTasksDialog.isVisible())
Expand Down Expand Up @@ -114,8 +117,7 @@ void ModalTaskHandler::ModalTasksDialog::numberOfModalTasksChanged()

const auto numberOfModalTasks = tasksFilterModel.rowCount();

if (numberOfModalTasks == 0)
return;
qDebug() << __FUNCTION__ << numberOfModalTasks;

cleanLayout();

Expand Down Expand Up @@ -165,15 +167,15 @@ void ModalTaskHandler::ModalTasksDialog::numberOfModalTasksChanged()
const auto clockIcon = Application::getIconFont("FontAwesome").getIcon("clock");

if (numberOfModalTasks == 1) {
const auto sourceModelIndex = tasksFilterModel.mapToSource(tasksFilterModel.index(0, 0));
const auto item = dynamic_cast<AbstractTasksModel::Item*>(tasks().getTreeModel()->itemFromIndex(sourceModelIndex));
const auto task = item->getTask();
const auto taskName = task->getName();
const auto taskDescription = task->getDescription();
const auto taskIcon = task->getIcon();

setWindowTitle(taskDescription.isEmpty() ? QString("Waiting for %1 to complete...").arg(taskName) : task->getDescription());
setWindowIcon(taskIcon.isNull() ? clockIcon : taskIcon);
//const auto sourceModelIndex = tasksFilterModel.mapToSource(tasksFilterModel.index(0, 0));
//const auto item = dynamic_cast<AbstractTasksModel::Item*>(tasks().getTreeModel()->itemFromIndex(sourceModelIndex));
//const auto task = item->getTask();
//const auto taskName = task->getName();
//const auto taskDescription = task->getDescription();
//const auto taskIcon = task->getIcon();

//setWindowTitle(taskDescription.isEmpty() ? QString("Waiting for %1 to complete...").arg(taskName) : task->getDescription());
//setWindowIcon(taskIcon.isNull() ? clockIcon : taskIcon);
}
else {
setWindowTitle(QString("Waiting for %1 tasks to complete...").arg(QString::number(numberOfModalTasks)));
Expand Down
18 changes: 16 additions & 2 deletions HDPS/src/ModalTaskTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ModalTaskTester.h"
#include "TaskTesterRunner.h"
#include "ModalTask.h"
#include "BackgroundTask.h"

#include <QEventLoop>

Expand All @@ -14,9 +15,22 @@ namespace hdps
ModalTaskTester::ModalTaskTester(QObject* parent, const QString& name) :
AbstractTaskTester(parent, name)
{
testRunningIndeterminate();
auto modalTask = new ModalTask(this, "New modal task");


auto childModalTask = new ModalTask(this, "Child modal task");

childModalTask->setParentTask(modalTask);

childModalTask->setRunning();
childModalTask->setRunning();
childModalTask->setIdle();
childModalTask->setRunning();
childModalTask->setFinished();

//testRunningIndeterminate();
//testAggregation();
testPerformance();
//testPerformance();
}

void ModalTaskTester::testRunningIndeterminate()
Expand Down
6 changes: 3 additions & 3 deletions HDPS/src/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ class Task : public QObject, public util::Serializable
/** Couples scope enum value to scope name string */
static QMap<Scope, QString> scopeNames;

using TasksPtrs = QVector<Task*>;
using TasksPtrs = QVector<Task*>;
using ProgressTextFormatter = std::function<QString(Task&)>;
using Scopes = QVector<Scope>;
using Statuses = QVector<Status>;
using Scopes = QVector<Scope>;
using Statuses = QVector<Status>;

private:

Expand Down
55 changes: 34 additions & 21 deletions HDPS/src/TasksTreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <util/Exception.h>

#ifdef _DEBUG
#define TASKS_MODEL_VERBOSE
#define TASKS_TREE_MODEL_VERBOSE
#endif

namespace hdps
Expand Down Expand Up @@ -41,24 +41,38 @@ void TasksTreeModel::taskAddedToTaskManager(Task* task)
if (task == nullptr)
throw std::runtime_error("Task may not be a nullptr");

if (task->hasParentTask()) {
const auto matches = match(index(0, static_cast<int>(Column::ID)), Qt::EditRole, task->getParentTask()->getId(), 1, Qt::MatchExactly | Qt::MatchRecursive);

if (matches.isEmpty())
throw std::runtime_error(QString("%1 not found").arg(task->getParentTask()->getName()).toStdString());

auto parentItem = itemFromIndex(matches.first().siblingAtColumn(static_cast<int>(Column::ExpandCollapse)));
if (task->hasParentTask())
itemFromTask(task->getParentTask())->appendRow(Row(task));
else
appendRow(Row(task));

Q_ASSERT(parentItem != nullptr);
connect(task, &Task::parentTaskChanged, this, [this, task](Task* previousParentTask, Task* currentParentTask) -> void {
try {
auto taskItem = itemFromTask(task);

if (parentItem == nullptr)
throw std::runtime_error("Parent standard item may not be a nullptr");
#ifdef TASKS_TREE_MODEL_VERBOSE
qDebug() << __FUNCTION__ << task->getName() << currentParentTask->getName();
#endif

parentItem->appendRow(Row(task));
}
else {
appendRow(Row(task));
}
if (previousParentTask)
removeRow(taskItem->row(), itemFromTask(previousParentTask)->index());
else
removeRow(taskItem->row(), QModelIndex());

if (currentParentTask)
itemFromTask(task->getParentTask())->appendRow(Row(task));
else
appendRow(Row(task));
}
catch (std::exception& e)
{
exceptionMessageBox("Unable to re-parent task", e);
}
catch (...)
{
exceptionMessageBox("Unable to re-parent task");
}
});
}
catch (std::exception& e)
{
Expand All @@ -81,13 +95,12 @@ void TasksTreeModel::taskAboutToBeRemovedFromTaskManager(Task* task)
if (task == nullptr)
throw std::runtime_error("Task may not be a nullptr");

const auto matches = match(index(0, static_cast<int>(Column::ID)), Qt::EditRole, task->getId(), -1, Qt::MatchExactly | Qt::MatchRecursive);

if (matches.empty())
throw std::runtime_error(QString("%1 not found").arg(task->getName()).toStdString());
auto taskItem = itemFromTask(task);

if (!removeRow(matches.first().row()))
if (!removeRow(taskItem->row(), taskItem->parent() ? taskItem->parent()->index() : QModelIndex()))
throw std::runtime_error("Remove row failed");

disconnect(task, &Task::parentTaskChanged, this, nullptr);
}
catch (std::exception& e)
{
Expand Down
7 changes: 4 additions & 3 deletions HDPS/src/actions/TaskAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void TaskAction::setTask(Task* task)

_task = task;

const auto updateCancelTaskAction = [this]() -> void {
const auto updateKillTaskAction = [this]() -> void {
Q_ASSERT(_task != nullptr);

if (_task == nullptr)
Expand All @@ -69,15 +69,15 @@ void TaskAction::setTask(Task* task)
_killTaskAction.setToolTip(QString("Cancel %1").arg(_task->getName()));
};

updateCancelTaskAction();
updateKillTaskAction();

const auto updateProgressAction = [this]() -> void {
_progressAction.setProgress(static_cast<int>(_task->getProgress() * 100.f));
};

updateProgressAction();

connect(_task, &Task::nameChanged, this, updateCancelTaskAction);
connect(_task, &Task::nameChanged, this, updateKillTaskAction);
connect(_task, &Task::progressChanged, this, updateProgressAction);
connect(_task, &Task::progressTextChanged, this, &TaskAction::updateProgressActionTextFormat);
connect(_task, &Task::statusChanged, this, &TaskAction::updateActionsReadOnly);
Expand Down Expand Up @@ -112,6 +112,7 @@ void TaskAction::updateProgressActionRange()
switch (_task->getStatus())
{
case Task::Status::Idle:
_progressAction.setRange(0, 100);
break;

case Task::Status::Running:
Expand Down
4 changes: 4 additions & 0 deletions HDPS/src/private/ProjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ void ProjectManager::openProject(QString filePath /*= ""*/, bool importDataOnly

task.setSubtasks(QStringList() << decompressionTaskNames << "Create data hierarchy" << viewPluginsTaskNames);

connect(&archiver, &Archiver::taskStarted, this, [this](const QString& taskName) -> void {
_project->getTask().setSubtaskStarted(taskName, QString("extracting %1").arg(taskName));
});

connect(&archiver, &Archiver::taskFinished, this, [this](const QString& taskName) -> void {
_project->getTask().setSubtaskFinished(taskName, QString("%1 extracted").arg(taskName));
});
Expand Down

0 comments on commit 64d0c0a

Please sign in to comment.