File: JobsListing.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 (281 lines) | stat: -rw-r--r-- 10,410 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Job/JobsListing.cpp
//! @brief     Implements class JobsListing.
//!
//! @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/JobsListing.h"
#include "Base/Util/Assert.h"
#include "Device/Data/Datafield.h"
#include "GUI/Model/Data/Data2DItem.h"
#include "GUI/Model/File/DatafileItem.h"
#include "GUI/Model/Job/BatchInfo.h"
#include "GUI/Model/Job/JobStatus.h"
#include "GUI/Model/Job/JobsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Util/Backup.h"
#include "GUI/View/Job/JobProgressDelegate.h"
#include "GUI/View/Job/JobsQModel.h"
#include "GUI/View/Job/Simulate.h"
#include "GUI/View/Setup/ActionFactory.h"
#include "GUI/View/Widget/StyledToolbar.h"
#include <QVBoxLayout>

namespace {

//! compare function for sorting indexes according to row descending
bool row_descending(const QModelIndex& idx1, const QModelIndex& idx2)
{
    return idx1.row() > idx2.row();
}

} // namespace

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

JobsListing::JobsListing(QWidget* parent, Qt::WindowFlags f)
    : QWidget(parent, f)
    , m_list_view(new QListView(this))
    , m_progress_delegate(new JobProgressDelegate(this))
    , m_model(new JobsQModel(this))
{
    auto* layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);

    m_run_action = new QAction("Run", this);
    m_run_action->setIcon(QIcon(":/images/play.svg"));
    m_run_action->setToolTip("Run currently selected jobs");
    connect(m_run_action, &QAction::triggered, this, &JobsListing::onRun);

    m_cancel_action = new QAction("Stop", this);
    m_cancel_action->setIcon(QIcon(":/images/stop.svg"));
    m_cancel_action->setToolTip("Stop currently selected jobs");
    connect(m_cancel_action, &QAction::triggered,
            [this] { onCancel(m_list_view->selectionModel()->selectedIndexes()); });

    m_cp_action = ActionFactory::createCopyAction("job");
    connect(m_cp_action, &QAction::triggered, this, &JobsListing::onCopy);

    m_remove_action = ActionFactory::createRemoveAction("job");
    connect(m_remove_action, &QAction::triggered, this, &JobsListing::onRemove);

    QToolBar* toolbar = new StyledToolbar(this);
    toolbar->setMinimumSize(toolbar->minimumHeight(), toolbar->minimumHeight());
    toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar->addAction(m_run_action);
    toolbar->addAction(m_cancel_action);
    // toolbar->addAction(m_cp_action); // save place and not show in toolbar
    toolbar->addAction(m_remove_action);
    layout->addWidget(toolbar);

    m_list_view->setSelectionMode(QAbstractItemView::ExtendedSelection);

    connect(m_progress_delegate, &JobProgressDelegate::cancelButtonClicked, this,
            &JobsListing::onCancel);
    m_list_view->setItemDelegate(m_progress_delegate);
    layout->addWidget(m_list_view);

    m_list_view->setModel(m_model);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QWidget::customContextMenuRequested, this, &JobsListing::showContextMenu);

    connect(m_list_view->selectionModel(), &QItemSelectionModel::selectionChanged, this,
            &JobsListing::onItemSelectionChanged);
    connect(m_model, &QAbstractListModel::dataChanged, this, &JobsListing::onJobsDataChanged);

    connect(gDoc.get(), &ProjectDocument::documentAboutToReopen, m_list_view->selectionModel(),
            &QItemSelectionModel::clearSelection);
    connect(gDoc.get(), &ProjectDocument::documentOpened, this, &JobsListing::restoreSelection);

    updateActions();

    setMinimumWidth(10);
}

QVector<JobItem*> JobsListing::selectedJobItems() const
{
    QVector<JobItem*> result;
    for (const QModelIndex& index : m_list_view->selectionModel()->selectedIndexes())
        result.push_back(m_model->jobItemForIndex(index));
    return result;
}

void JobsListing::selectNewJob(JobItem* job)
{
    QModelIndex idx = m_model->indexForJob(job);
    QModelIndexList selected = m_list_view->selectionModel()->selectedIndexes();

    // Already selected, but we still will emit the signal to notify widgets.
    // To handle the case, when the job was selected before it completed (and some stack widgets
    // were refusing to show the content for non-complete job).
    if (selected.size() == 1 && selected.front() == idx) {
        emit selectedJobsChanged({job});
        return;
    }
    selectAndSetCurrent(idx);
}

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

void JobsListing::onItemSelectionChanged()
{
    updateActions();

    QModelIndexList selected = m_list_view->selectionModel()->selectedIndexes();
    if (selected.size() == 1)
        gDoc->jobsRW()->setCurrentIndex(selected.first().row());
    else
        gDoc->jobsRW()->setCurrentIndex(-1);

    emit selectedJobsChanged(selectedJobItems());
    emit gDoc->jobs()->jobPlotContextChanged();
}

void JobsListing::onJobsDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
    // currently only single items change, not ranges; thus ranges are not supported
    ASSERT(topLeft == bottomRight);

    if (m_list_view->selectionModel()->isSelected(topLeft))
        updateActions();
}

void JobsListing::onRun()
{
    for (const QModelIndex& index : m_list_view->selectionModel()->selectedIndexes())
        if (!GUI::Sim::simulate(m_model->jobItemForIndex(index), gDoc->jobsRW()))
            break;
    gDoc->setModified();
}

void JobsListing::onCancel(const QModelIndexList indexes)
{
    for (const QModelIndex& index : indexes)
        m_model->cancelJob(index);
    gDoc->setModified();
}

void JobsListing::onCopy()
{
    QModelIndexList indexes = m_list_view->selectionModel()->selectedIndexes();
    ASSERT(!indexes.isEmpty());
    std::sort(indexes.begin(), indexes.end());
    for (const QModelIndex& index : indexes) {
        JobItem* old_job = m_model->jobItemForIndex(index);
        JobItem* new_job = gDoc->jobsRW()->createJobItem();
        GUI::Util::copyContents(old_job, new_job);
        new_job->batchInfo()->setJobName(old_job->batchInfo()->jobName() + " (copy)");

        // copy simulated datafield
        if (old_job->simulatedDataItem())
            if (auto* field = old_job->simulatedDataItem()->c_field())
                new_job->simulatedDataItem()->setDatafield(*field->clone());

        // copy real datafield
        if (old_job->dfileItem())
            if (auto* field = old_job->dfileItem()->dataItem()->c_field())
                new_job->dfileItem()->dataItem()->setDatafield(*field->clone());

        // there is no need to copy diff datafield, because if will be automatically updated by
        // signal from setDatafield

        emit gDoc->jobsRW()->jobAdded(new_job);
    }
    QModelIndex last_index = m_model->indexForJob(gDoc->jobsRW()->back());
    selectAndSetCurrent(last_index);
}

void JobsListing::onRemove()
{
    QModelIndexList indexes = m_list_view->selectionModel()->selectedIndexes();
    ASSERT(!indexes.isEmpty());
    std::sort(indexes.begin(), indexes.end(), row_descending);
    for (const QModelIndex& index : indexes)
        m_model->removeJob(index);

    int rowToSelect = indexes.front().row();
    selectAlternativeItem(rowToSelect);

    gDoc->setModified();
}

void JobsListing::showContextMenu(const QPoint&)
{
    QMenu menu(this);
    menu.addAction(m_run_action);
    menu.addAction(m_cancel_action);
    menu.addAction(m_cp_action);
    menu.addAction(m_remove_action);
    menu.exec(QCursor::pos());
}

//  ------------------------------------------------------------------------------------------------
//  private member functions
//  ------------------------------------------------------------------------------------------------

void JobsListing::updateActions()
{
    QModelIndexList indexes = m_list_view->selectionModel()->selectedIndexes();

    struct IsRunningOrFitting {
        JobsQModel* m_model;
        IsRunningOrFitting(JobsQModel* model)
            : m_model(model)
        {
        }
        bool operator()(const QModelIndex& i) const
        {
            JobItem* job = m_model->jobItemForIndex(i);
            ASSERT(job);
            return isActive(job->batchInfo()->status());
        }
    };

    bool none_running = std::none_of(indexes.begin(), indexes.end(), IsRunningOrFitting(m_model));
    bool all_running = std::all_of(indexes.begin(), indexes.end(), IsRunningOrFitting(m_model));
    bool nonempty = !indexes.empty();
    m_run_action->setEnabled(nonempty && none_running);
    m_cancel_action->setEnabled(nonempty && all_running);
    m_cp_action->setEnabled(nonempty && none_running);
    m_remove_action->setEnabled(nonempty && none_running);
}

void JobsListing::restoreSelection()
{
    int lastUsed = gDoc->jobs()->currentIndex();
    if (lastUsed < 0 || lastUsed >= m_model->rowCount())
        return;

    QModelIndex lastUsedIndex = m_model->index(lastUsed, 0, QModelIndex());
    selectAndSetCurrent(lastUsedIndex);
}

void JobsListing::selectAlternativeItem(int lastSelectedRow)
{
    if (m_list_view->selectionModel()->hasSelection() || !m_model->rowCount())
        return;

    QModelIndex rowToSelect = m_model->index(m_model->rowCount() - 1, 0, QModelIndex());
    if (lastSelectedRow >= 0 && lastSelectedRow < m_model->rowCount())
        rowToSelect = m_model->index(lastSelectedRow, 0, QModelIndex());
    selectAndSetCurrent(rowToSelect);
}

void JobsListing::selectAndSetCurrent(const QModelIndex& index)
{
    m_list_view->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
    m_list_view->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
}