File: JobsQModel.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (102 lines) | stat: -rw-r--r-- 3,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Job/JobsQModel.cpp
//! @brief     Implements class JobsQModel.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Job/JobsQModel.h"
#include "GUI/Model/Job/BatchInfo.h"
#include "GUI/Model/Job/JobItem.h"
#include "GUI/Model/Job/JobsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"

//--------------------------------------------------------------------------------------------------
// public member functions
//--------------------------------------------------------------------------------------------------

JobsQModel::JobsQModel(QObject* parent)
    : QAbstractListModel(parent)
{
    connect(gDoc->jobsRW(), &JobsSet::jobAdded, this, &JobsQModel::onJobAdded);
}

JobsQModel::~JobsQModel() = default;

int JobsQModel::rowCount(const QModelIndex&) const
{
    return gDoc->jobs()->size();
}

QVariant JobsQModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid() || (size_t)index.row() >= gDoc->jobs()->size())
        return {};

    const JobItem* item = gDoc->jobs()->at(index.row());
    if (role == Qt::DisplayRole)
        return item->batchInfo()->jobName();

    return {};
}

JobItem* JobsQModel::jobItemForIndex(const QModelIndex& index) const
{
    if (index.row() >= 0 && index.row() < (int)gDoc->jobs()->size())
        return gDoc->jobsRW()->at(index.row());
    return nullptr;
}

QModelIndex JobsQModel::indexForJob(JobItem* job)
{
    int idx = gDoc->jobs()->index_of(job);
    if (idx != -1)
        return index(idx, 0);
    return {};
}

void JobsQModel::removeJob(const QModelIndex& index)
{
    beginRemoveRows(QModelIndex(), index.row(), index.row());
    JobItem* job = jobItemForIndex(index);
    job->batchInfo()->disconnect();
    gDoc->jobsRW()->removeJob(job);
    endRemoveRows();
}

void JobsQModel::cancelJob(const QModelIndex& index)
{
    gDoc->jobsRW()->cancelJob(jobItemForIndex(index));
}

//--------------------------------------------------------------------------------------------------
// private slots
//--------------------------------------------------------------------------------------------------

void JobsQModel::emitJobsQModelChanged(JobItem* job)
{
    const int i = gDoc->jobs()->index_of(job);
    if (i != -1) {
        const QModelIndex idx = index(i, 0);
        emit dataChanged(idx, idx);
    }
    gDoc->setModified();
}

void JobsQModel::onJobAdded(JobItem* job)
{
    connect(job->batchInfo(), &BatchInfo::jobStatusChanged,
            [this, job] { emitJobsQModelChanged(job); });
    connect(job->batchInfo(), &BatchInfo::jobNameChanged,
            [this, job] { emitJobsQModelChanged(job); });
    connect(job->batchInfo(), &BatchInfo::jobProgressChanged,
            [this, job] { emitJobsQModelChanged(job); });
    emitJobsQModelChanged(job);
}