| 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_QTGUI_JSONWIDGET_H |
| 7 |
#define AVOGADRO_QTGUI_JSONWIDGET_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
|
| 11 |
#include <QtCore/QJsonObject> |
| 12 |
#include <QtCore/QMap> |
| 13 |
#include <QtWidgets/QFormLayout> |
| 14 |
#include <QtWidgets/QWidget> |
| 15 |
|
| 16 |
class QJsonValue; |
| 17 |
class QTextEdit; |
| 18 |
class QWidget; |
| 19 |
|
| 20 |
namespace Avogadro { |
| 21 |
namespace QtGui { |
| 22 |
class Molecule; |
| 23 |
|
| 24 |
/** |
| 25 |
* @class JsonWidget jsonwidget.h |
| 26 |
* <avogadro/qtgui/jsonwidget.h> |
| 27 |
* @brief The JsonWidget class provides a user interface for |
| 28 |
* running external scripts |
| 29 |
* @sa InterfaceWidget |
| 30 |
* |
| 31 |
* The JsonWidget creates a GUI to represent the options given by an |
| 32 |
* script, turning JSON from the script into a form and passing the results |
| 33 |
* back to the script via command-line. |
| 34 |
* |
| 35 |
* It's used by a range of different scripts, including: |
| 36 |
* - Commands |
| 37 |
* - InterfaceGenerators |
| 38 |
*/ |
| 39 |
class AVOGADROQTGUI_EXPORT JsonWidget : public QWidget |
| 40 |
{ |
| 41 |
Q_OBJECT |
| 42 |
|
| 43 |
public: |
| 44 |
/** |
| 45 |
* Construct a widget that dynamically generates a GUI |
| 46 |
*/ |
| 47 |
explicit JsonWidget(QWidget* parent_ = nullptr); |
| 48 |
~JsonWidget() override; |
| 49 |
|
| 50 |
/** |
| 51 |
* Set the molecule used in the simulation. |
| 52 |
*/ |
| 53 |
virtual void setMolecule(QtGui::Molecule* mol); |
| 54 |
|
| 55 |
/** |
| 56 |
* Collect all of the user-specified options into a JSON object, to be sent |
| 57 |
* to the generator script. |
| 58 |
*/ |
| 59 |
QJsonObject collectOptions() const; |
| 60 |
|
| 61 |
/** |
| 62 |
* Apply the options in the passed QJsonObject to the GUI. Any widgets changed |
| 63 |
* by this method will have their signals blocked while modifying their |
| 64 |
* values. |
| 65 |
*/ |
| 66 |
void applyOptions(const QJsonObject& opts); |
| 67 |
|
| 68 |
bool isEmpty() const { return m_empty; } |
| 69 |
|
| 70 |
public slots: |
| 71 |
/** |
| 72 |
* Update the preview text in the GUI. The base implementation does nothing; |
| 73 |
* subclasses that have a preview area (e.g. InputGeneratorWidget) override |
| 74 |
* this to regenerate their preview when options change. |
| 75 |
*/ |
| 76 |
virtual void updatePreviewText() {} |
| 77 |
|
| 78 |
protected: |
| 79 |
/** |
| 80 |
* Given the name of a user-option in m_options, return the type string. |
| 81 |
* If an error occurs, an empty string will be returned. |
| 82 |
*/ |
| 83 |
QString lookupOptionType(const QString& name) const; |
| 84 |
|
| 85 |
/** |
| 86 |
* Used to construct the script-specific GUI. |
| 87 |
* @{ |
| 88 |
*/ |
| 89 |
virtual void updateOptions(); |
| 90 |
void buildOptionGui(); |
| 91 |
void combinedOptionRow(const QString& label1, const QString& label2, |
| 92 |
const QString& tr1, const QString& tr2, |
| 93 |
QJsonObject& options, bool bothLabels = false); |
| 94 |
void addOptionRow(const QString& key, const QString& label, |
| 95 |
const QJsonValue& option); |
| 96 |
|
| 97 |
QWidget* createOptionWidget(const QJsonValue& option); |
| 98 |
QWidget* createStringListWidget(const QJsonObject& obj); |
| 99 |
QWidget* createStringWidget(const QJsonObject& obj); |
| 100 |
QWidget* createFilePathWidget(const QJsonObject& obj); |
| 101 |
QWidget* createIntegerWidget(const QJsonObject& obj); |
| 102 |
QWidget* createFloatWidget(const QJsonObject& obj); |
| 103 |
QWidget* createBooleanWidget(const QJsonObject& obj); |
| 104 |
QWidget* createTextWidget(const QJsonObject& obj); |
| 105 |
QWidget* createTableWidget(const QJsonObject& obj); |
| 106 |
/**@}*/ |
| 107 |
|
| 108 |
/** |
| 109 |
* Set the simulation settings to their default values. |
| 110 |
* @{ |
| 111 |
*/ |
| 112 |
void setOptionDefaults(); |
| 113 |
void setOption(const QString& name, const QJsonValue& defaultValue); |
| 114 |
void setStringListOption(const QString& name, const QJsonValue& value); |
| 115 |
void setStringOption(const QString& name, const QJsonValue& value); |
| 116 |
void setFilePathOption(const QString& name, const QJsonValue& value); |
| 117 |
void setIntegerOption(const QString& name, const QJsonValue& value); |
| 118 |
void setFloatOption(const QString& name, const QJsonValue& value); |
| 119 |
void setBooleanOption(const QString& name, const QJsonValue& value); |
| 120 |
void setTextOption(const QString& name, const QJsonValue& value); |
| 121 |
void setTableOption(const QString& name, const QJsonValue& value); |
| 122 |
/**@}*/ |
| 123 |
|
| 124 |
/** |
| 125 |
* @brief Search for an option named @a option and convert its value to a |
| 126 |
* string. |
| 127 |
* @param option The name of the option. |
| 128 |
* @param value String to overwrite with option value. |
| 129 |
* @return True if value is overwritten, false if the option is not found or |
| 130 |
* cannot be converted to a string. |
| 131 |
*/ |
| 132 |
bool optionString(const QString& option, QString& value) const; |
| 133 |
|
| 134 |
/** |
| 135 |
* Update the autogenerated job title in the GUI. |
| 136 |
*/ |
| 137 |
QString generateJobTitle() const; |
| 138 |
|
| 139 |
QtGui::Molecule* m_molecule; |
| 140 |
bool m_isTabbed; |
| 141 |
QJsonObject m_options; |
| 142 |
QJsonObject m_optionCache; // For reverting changes |
| 143 |
QList<QTextEdit*> m_dirtyTextEdits; |
| 144 |
|
| 145 |
bool m_empty; |
| 146 |
bool m_batchMode; |
| 147 |
QFormLayout* m_currentLayout; |
| 148 |
QWidget* m_centralWidget; |
| 149 |
QMap<QString, QWidget*> m_widgets; |
| 150 |
QMap<QString, QTextEdit*> m_textEdits; |
| 151 |
}; |
| 152 |
|
| 153 |
} // namespace QtGui |
| 154 |
} // namespace Avogadro |
| 155 |
|
| 156 |
#endif // AVOGADRO_QTGUI_JSONWIDGET_H |