Debian Codegraph @

avogadro/molequeue/molequeuedialog.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "molequeuedialog.h"
7 #include "molequeuewidget.h"
8 #include "ui_molequeuedialog.h"
9
10 #include <QtWidgets/QMessageBox>
11 #include <QtWidgets/QProgressDialog>
12
13 #include <QtCore/QEventLoop>
14 #include <QtCore/QTimer>
15
16 namespace Avogadro::MoleQueue {
17
18 MoleQueueDialog::MoleQueueDialog(QWidget* parent_)
19 : QDialog(parent_), m_ui(new Ui::MoleQueueDialog)
20 {
21 m_ui->setupUi(this);
22 }
23
24 MoleQueueDialog::~MoleQueueDialog()
25 {
26 delete m_ui;
27 }
28
29 MoleQueueDialog::SubmitStatus MoleQueueDialog::submitJob(
30 QWidget* parent_, const QString& caption, JobObject& jobTemplate,
31 SubmitOptions options, unsigned int* moleQueueId, int* submissionRequestId)
32 {
33 // initialize return args
34 if (moleQueueId)
35 *moleQueueId = MoleQueueWidget::InvalidMoleQueueId;
36 if (submissionRequestId)
37 *submissionRequestId = -1;
38
39 MoleQueueDialog dlg(parent_);
40 dlg.setWindowTitle(caption);
41 dlg.widget().setJobTemplate(jobTemplate);
42
43 if (options & SelectProgramFromTemplate)
44 dlg.widget().showAndSelectProgram(jobTemplate.program());
45
46 for (;;) {
47 int dlgResult = dlg.exec();
48
49 if (dlgResult != QDialog::Accepted)
50 return SubmissionAborted;
51
52 int requestId = dlg.widget().submitJobRequest();
53
54 if (options & WaitForSubmissionResponse || dlg.widget().openOutput()) {
55 QProgressDialog progress;
56 progress.setCancelButton(nullptr);
57 progress.setLabelText(tr("Submitting job to MoleQueue…"));
58 progress.setRange(0, 0);
59 progress.setValue(0);
60 progress.show();
61
62 QList<MetaMethod> submittedSignal;
63 submittedSignal << MetaMethod(&dlg.widget(), SIGNAL(jobSubmitted(bool)));
64 if (!dlg.waitForSignal(submittedSignal)) {
65 progress.hide();
66 QMessageBox::information(&dlg, tr("Job Submission Timeout"),
67 tr("Avogadro timed out waiting for a reply "
68 "from MoleQueue."));
69 continue;
70 }
71
72 if (dlg.widget().submissionSuccess()) {
73 if (submissionRequestId != nullptr)
74 *submissionRequestId = dlg.widget().requestId();
75 if (moleQueueId != nullptr)
76 *moleQueueId = dlg.widget().moleQueueId();
77
78 // Do we need to wait for the job to finish?
79 if (!dlg.widget().openOutput())
80 return SubmissionSuccessful;
81
82 // Update progress dialog
83 progress.setLabelText(tr("Waiting for job %1 “%2” to finish…",
84 "%1 = job ID, %2 = description")
85 .arg(dlg.widget().moleQueueId())
86 .arg(jobTemplate.description()));
87 progress.setCancelButtonText(tr("Stop waiting"));
88
89 // Wait for job completion or progress bar cancellation.
90 QList<MetaMethod> completionSignals;
91 completionSignals << MetaMethod(&dlg.widget(),
92 SIGNAL(jobFinished(bool)))
93 << MetaMethod(&progress, SIGNAL(canceled()));
94
95 dlg.waitForSignal(completionSignals, -1);
96
97 // Did the user cancel?
98 if (progress.wasCanceled())
99 return SubmissionSuccessful;
100
101 // Error occurred:
102 if (!dlg.widget().jobSuccess())
103 return JobFailed;
104
105 // Update progress bar:
106 progress.setLabelText(tr("Fetching completed job information…"));
107 progress.setCancelButton(nullptr);
108
109 // Job completed -- overwrite job template with updated job details.
110 connect(&dlg.widget(), SIGNAL(jobUpdated(MoleQueue::JobObject)),
111 &dlg.widget(), SLOT(setJobTemplate(MoleQueue::JobObject)));
112 QList<MetaMethod> lookupSignal;
113 lookupSignal << MetaMethod(&dlg.widget(),
114 SIGNAL(jobUpdated(MoleQueue::JobObject)));
115 dlg.widget().requestJobLookup();
116 if (!dlg.waitForSignal(lookupSignal)) {
117 progress.hide();
118 QMessageBox::information(&dlg, tr("Job Retrieval Timeout"),
119 tr("Avogadro timed out waiting for the "
120 "finished job details from MoleQueue."));
121 return JobFailed;
122 }
123
124 jobTemplate = dlg.widget().jobTemplate();
125 return JobFinished;
126 } else {
127 progress.hide();
128 QMessageBox::warning(
129 &dlg, tr("Error Submitting Job"),
130 tr("The job has been rejected by MoleQueue: %1", "%1 = error")
131 .arg(dlg.widget().submissionError()));
132 continue;
133 }
134 } else {
135 if (requestId >= 0) {
136 if (submissionRequestId != nullptr)
137 *submissionRequestId = requestId;
138 return SubmissionAttempted;
139 } else {
140 return SubmissionFailed;
141 }
142 }
143 }
144 }
145
146 bool MoleQueueDialog::promptForJobOptions(QWidget* windowParent,
147 const QString& caption,
148 MoleQueue::JobObject& jobTemplate)
149 {
150 MoleQueueDialog dlg(windowParent);
151 dlg.setWindowTitle(caption);
152 dlg.widget().setBatchMode(true);
153 dlg.widget().setJobTemplate(jobTemplate);
154 if (!jobTemplate.program().isEmpty())
155 dlg.widget().showAndSelectProgram(jobTemplate.program());
156
157 if (static_cast<DialogCode>(dlg.exec()) != Accepted)
158 return false;
159
160 jobTemplate = dlg.widget().configuredJob();
161 return true;
162 }
163
164 MoleQueueWidget& MoleQueueDialog::widget()
165 {
166 return *m_ui->widget;
167 }
168
169 const MoleQueueWidget& MoleQueueDialog::widget() const
170 {
171 return *m_ui->widget;
172 }
173
174 bool MoleQueueDialog::waitForSignal(const QList<MetaMethod>& signalList,
175 int msTimeout) const
176 {
177 QEventLoop waiter;
178
179 foreach (const MetaMethod& sig, signalList)
180 connect(sig.first, sig.second, &waiter, SLOT(quit()));
181
182 QTimer limiter;
183 if (msTimeout >= 0) {
184 limiter.setSingleShot(true);
185 connect(&limiter, SIGNAL(timeout()), &waiter, SLOT(quit()));
186 limiter.start(msTimeout);
187 }
188
189 waiter.exec();
190
191 return limiter.isActive();
192 }
193
194 void MoleQueueDialog::done(int r)
195 {
196 if (r == QDialog::Accepted && !widget().programSelected()) {
197 QMessageBox::information(parentWidget(), tr("No program selected."),
198 tr("Please select the target program from the "
199 "\"Queue and Program\" list."));
200 } else {
201 QDialog::done(r);
202 }
203 }
204
205 } // namespace Avogadro::MoleQueue