BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobListModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Job/JobListModel.cpp
6 //! @brief Implements class JobListModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "GUI/Model/Job/JobItem.h"
18 
19 //==================================================================================================
20 // JobListModel
21 //==================================================================================================
22 
23 //--------------------------------------------------------------------------------------------------
24 // public member functions
25 //--------------------------------------------------------------------------------------------------
26 
27 JobListModel::JobListModel(JobModel* jobs, QObject* parent)
28  : QAbstractListModel(parent)
29  , m_jobs(jobs)
30 {
31  for (JobItem* job : m_jobs->jobItems())
33 
34  connect(jobs, &QAbstractItemModel::rowsAboutToBeInserted, this,
36  connect(jobs, &QAbstractItemModel::rowsInserted, this, &JobListModel::onRowsInserted);
37 }
38 
40 {
41  for (JobItem* job : m_jobs->jobItems()) {
43  }
44 }
45 
46 int JobListModel::rowCount(const QModelIndex&) const
47 {
48  return m_jobs->jobItems().size();
49 }
50 
51 QVariant JobListModel::data(const QModelIndex& index, int role) const
52 {
53  QVector<JobItem*> jobs = m_jobs->jobItems();
54  if (!index.isValid() || index.row() >= jobs.size() || index.row() < 0)
55  return {};
56 
57  JobItem* item = jobs[index.row()];
58  if (role == Qt::DisplayRole)
59  return item->jobName();
60 
61  return {};
62 }
63 
64 JobItem* JobListModel::jobForIndex(const QModelIndex& index) const
65 {
66  QVector<JobItem*> jobs = m_jobs->jobItems();
67  if (index.row() >= 0 && index.row() < jobs.size())
68  return jobs[index.row()];
69  return nullptr;
70 }
71 
73 {
74  QVector<JobItem*> jobs = m_jobs->jobItems();
75  int idx = jobs.indexOf(job);
76  if (idx != -1)
77  return index(idx, 0);
78  return {};
79 }
80 
81 void JobListModel::runJob(const QModelIndex& index)
82 {
83  m_jobs->runJob(jobForIndex(index));
84 }
85 
86 void JobListModel::removeJob(const QModelIndex& index)
87 {
88  beginRemoveRows(QModelIndex(), index.row(), index.row());
89  JobItem* job = jobForIndex(index);
91  m_jobs->removeJob(job);
92  endRemoveRows();
93 }
94 
95 void JobListModel::cancelJob(const QModelIndex& index)
96 {
97  m_jobs->cancelJob(jobForIndex(index));
98 }
99 
101 {
102  QVector<JobItem*> jobs = m_jobs->jobItems();
103  int i = jobs.indexOf(job);
104  if (i != -1) {
105  QModelIndex idx = index(i, 0);
106  emit dataChanged(idx, idx);
107  }
108 }
109 
110 //--------------------------------------------------------------------------------------------------
111 // private slots
112 //--------------------------------------------------------------------------------------------------
113 
114 void JobListModel::onRowsAboutToBeInserted(const QModelIndex& parent, int start, int end)
115 {
116  if (!parent.isValid())
117  beginInsertRows(QModelIndex(), start, end);
118 }
119 
120 void JobListModel::onRowsInserted(const QModelIndex& parent, int start, int end)
121 {
122  if (!parent.isValid()) {
123  endInsertRows();
124  QVector<JobItem*> jobs = m_jobs->jobItems();
125  for (int i = start; i <= end; i++)
126  enableJobNotification(jobs.at(i));
127  }
128 }
129 
130 //--------------------------------------------------------------------------------------------------
131 // private member functions
132 //--------------------------------------------------------------------------------------------------
133 
135 {
136  // name
137  connect(job, &JobItem::jobNameChanged, this,
138  [=](const QString&) { emitJobListModelChanged(job); });
139 
140  // status
141  connect(job, &JobItem::jobStatusChanged, this,
142  [=](const JobStatus) { emitJobListModelChanged(job); });
143 
144  // progress
145  connect(job, &JobItem::jobProgressChanged, this, [=](int) { emitJobListModelChanged(job); });
146 }
147 
149 {
150  disconnect(job, nullptr, this, nullptr);
151 }
Defines class JobItem.
Defines class JobListModel.
Defines class JobModel.
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
void jobStatusChanged(const JobStatus status)
void jobProgressChanged(int progress)
void jobNameChanged(const QString &name)
QString jobName() const
Definition: JobItem.cpp:84
void removeJob(const QModelIndex &index)
void disableJobNotification(JobItem *job)
JobListModel(JobModel *jobs, QObject *parent=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void cancelJob(const QModelIndex &index)
QModelIndex indexForJob(JobItem *job)
void onRowsInserted(const QModelIndex &parent, int start, int end)
void emitJobListModelChanged(JobItem *job)
JobItem * jobForIndex(const QModelIndex &index) const
JobModel * m_jobs
Definition: JobListModel.h:51
void enableJobNotification(JobItem *job)
void onRowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void runJob(const QModelIndex &index)
~JobListModel() override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVector< JobItem * > jobItems() const
Definition: JobModel.cpp:77
void runJob(JobItem *jobItem)
Definition: JobModel.cpp:142
void removeJob(JobItem *jobItem)
Definition: JobModel.cpp:152
void cancelJob(JobItem *jobItem)
Definition: JobModel.cpp:147