| 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 |
#ifndef AVOGADRO_MOLEQUEUE_MOLEQUEUEWIDGET_H |
| 7 |
#define AVOGADRO_MOLEQUEUE_MOLEQUEUEWIDGET_H |
| 8 |
|
| 9 |
#include "avogadromolequeueexport.h" |
| 10 |
#include <QtWidgets/QWidget> |
| 11 |
|
| 12 |
#include <avogadro/core/avogadrocore.h> |
| 13 |
|
| 14 |
#include "client/jobobject.h" |
| 15 |
|
| 16 |
namespace Avogadro::MoleQueue { |
| 17 |
|
| 18 |
namespace Ui { |
| 19 |
class MoleQueueWidget; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @class MoleQueueWidget molequeuewidget.h |
| 24 |
* <avogadro/molequeue/molequeuewidget.h> |
| 25 |
* @brief The MoleQueueWidget class provides a widget for configuring and |
| 26 |
* submitting a MoleQueue::JobObject. |
| 27 |
*/ |
| 28 |
class AVOGADROMOLEQUEUE_EXPORT MoleQueueWidget : public QWidget |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
public: |
| 32 |
explicit MoleQueueWidget(QWidget* parent_ = nullptr); |
| 33 |
~MoleQueueWidget() override; |
| 34 |
|
| 35 |
/** |
| 36 |
* A "template" MoleQueue::JobObject that is used to initialize the GUI. |
| 37 |
* Should be fully configured to submit, as this is used to initialize job |
| 38 |
* that will be submitted by submitJobRequest. |
| 39 |
* @{ |
| 40 |
*/ |
| 41 |
JobObject& jobTemplate(); |
| 42 |
const JobObject& jobTemplate() const; |
| 43 |
public slots: |
| 44 |
void setJobTemplate(const JobObject& job); |
| 45 |
|
| 46 |
public: |
| 47 |
/** @} */ |
| 48 |
|
| 49 |
/** |
| 50 |
* Expand all queue nodes that contain a program that matches @a programName |
| 51 |
* and select the first matching program node. |
| 52 |
* Matches are case insensitive. |
| 53 |
*/ |
| 54 |
void showAndSelectProgram(const QString& programName); |
| 55 |
|
| 56 |
/** |
| 57 |
* @return True if the last submission was successful. Only valid after |
| 58 |
* jobSubmitted has been emitted. |
| 59 |
*/ |
| 60 |
bool submissionSuccess() const { return m_moleQueueId != InvalidMoleQueueId; } |
| 61 |
|
| 62 |
/** |
| 63 |
* @return True if the last submission was successful. Only valid after |
| 64 |
* jobSubmitted has been emitted. |
| 65 |
*/ |
| 66 |
QString jobState() const { return m_jobState; } |
| 67 |
|
| 68 |
/** |
| 69 |
* @return True if the job has finished running. |
| 70 |
*/ |
| 71 |
bool jobCompleted() const |
| 72 |
{ |
| 73 |
return (m_jobState == QLatin1String("Finished") || |
| 74 |
m_jobState == QLatin1String("Error") || |
| 75 |
m_jobState == QLatin1String("Canceled")); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @return true if the job completed without error. |
| 80 |
*/ |
| 81 |
bool jobSuccess() const { return m_jobState == QLatin1String("Finished"); } |
| 82 |
|
| 83 |
/** |
| 84 |
* @return The request id associated with the last call to submitJobRequest. |
| 85 |
* -1 if there was a submission error. |
| 86 |
*/ |
| 87 |
int requestId() const { return m_requestId; } |
| 88 |
|
| 89 |
/** |
| 90 |
* Indicates an invalid MoleQueue ID in the moleQueueId() result. |
| 91 |
*/ |
| 92 |
static const unsigned int InvalidMoleQueueId; |
| 93 |
|
| 94 |
/** |
| 95 |
* @return The MoleQueue ID associated with the last submitJobRequest() call. |
| 96 |
* Only valid after jobSubmitted has been emitted. |
| 97 |
* @note If an error occurs, InvalidMoleQueueId will be returned. |
| 98 |
*/ |
| 99 |
unsigned int moleQueueId() const { return m_moleQueueId; } |
| 100 |
|
| 101 |
/** |
| 102 |
* @return A string describing the submission error when submissionSuccess() |
| 103 |
* return false. |
| 104 |
*/ |
| 105 |
QString submissionError() const { return m_submissionError; } |
| 106 |
|
| 107 |
/** |
| 108 |
* @return True if the user has requested that the output file be opened when |
| 109 |
* the calculation completes. |
| 110 |
*/ |
| 111 |
bool openOutput() const; |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief Request the current state of the job identified by moleQueueId() |
| 115 |
* from the server. The result will be emitted in the jobUpdated() signal. |
| 116 |
* @return True if moleQueueId() is valid and the server is connected, false |
| 117 |
* if the request cannot be sent. |
| 118 |
*/ |
| 119 |
bool requestJobLookup(); |
| 120 |
|
| 121 |
/** |
| 122 |
* If the widget is in 'batch mode', options that don't make sense are hidden |
| 123 |
* (such as 'open output when finished'). |
| 124 |
*/ |
| 125 |
void setBatchMode(bool batch); |
| 126 |
bool batchMode() const; |
| 127 |
/**@}*/ |
| 128 |
|
| 129 |
/** |
| 130 |
* @return True if the user has selected a program, false otherwise. |
| 131 |
*/ |
| 132 |
bool programSelected(); |
| 133 |
|
| 134 |
/** |
| 135 |
* @return A JobObject with the GUI options. Any settings in jobTemplate that |
| 136 |
* are not handled by the GUI are passed through untouched to the new object. |
| 137 |
*/ |
| 138 |
JobObject configuredJob() const; |
| 139 |
|
| 140 |
public slots: |
| 141 |
/** |
| 142 |
* Query the MoleQueue server (if available) for the list of available queues |
| 143 |
* and programs. |
| 144 |
*/ |
| 145 |
void refreshPrograms(); |
| 146 |
|
| 147 |
/** |
| 148 |
* Submit the job returned by configuredJob() to MoleQueue. |
| 149 |
* @return The request id associated with the submission, or -1 on error. |
| 150 |
* @note The result of the submission request can be checked by monitoring |
| 151 |
* jobSubmitted, which will always be emitted after this slot is called. |
| 152 |
*/ |
| 153 |
int submitJobRequest(); |
| 154 |
|
| 155 |
signals: |
| 156 |
/** |
| 157 |
* Emitted after a call to submitJobRequest |
| 158 |
* @param success True if the job has been accepted by MoleQueue. |
| 159 |
*/ |
| 160 |
void jobSubmitted(bool success); |
| 161 |
|
| 162 |
/** |
| 163 |
* Emitted after jobSubmitted is emitted and the job completes. |
| 164 |
* @param success True if the job enters the "Finished" state. False if the |
| 165 |
* job enters the "Canceled" or "Error" states. |
| 166 |
*/ |
| 167 |
void jobFinished(bool success); |
| 168 |
|
| 169 |
/** |
| 170 |
* Emitted after a successful call to requestJobLookup(). |
| 171 |
* @param job The result of the lookupJob() RPC query. |
| 172 |
*/ |
| 173 |
void jobUpdated(const JobObject& job); |
| 174 |
|
| 175 |
private slots: |
| 176 |
void showAndSelectProgramHandler(); |
| 177 |
|
| 178 |
void onLookupJobReply(int reqId, const QJsonObject& result); |
| 179 |
|
| 180 |
void onSubmissionSuccess(int localId, unsigned int moleQueueId); |
| 181 |
void onSubmissionFailure(int localId, unsigned int, const QString& error); |
| 182 |
|
| 183 |
void onJobStateChange(unsigned int mqId, const QString& oldState, |
| 184 |
const QString& newState); |
| 185 |
|
| 186 |
private: |
| 187 |
void listenForLookupJobReply(bool listen = true); |
| 188 |
void listenForJobSubmitReply(bool listen = true); |
| 189 |
void listenForJobStateChange(bool listen = true); |
| 190 |
|
| 191 |
Ui::MoleQueueWidget* m_ui; |
| 192 |
JobObject m_jobTemplate; |
| 193 |
QString m_jobState; |
| 194 |
QString m_submissionError; |
| 195 |
int m_requestId; |
| 196 |
unsigned int m_moleQueueId; |
| 197 |
}; |
| 198 |
|
| 199 |
} // namespace Avogadro::MoleQueue |
| 200 |
|
| 201 |
#endif // AVOGADRO_MOLEQUEUE_MOLEQUEUEWIDGET_H |