Debian Codegraph @

avogadro/qtgui/interfacewidget.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 "interfacewidget.h"
7
8 #include <avogadro/qtgui/filebrowsewidget.h>
9 #include <avogadro/qtgui/molecule.h>
10
11 #include <QtWidgets/QCheckBox>
12 #include <QtWidgets/QComboBox>
13 #include <QtWidgets/QDoubleSpinBox>
14 #include <QtWidgets/QFileDialog>
15 #include <QtWidgets/QFormLayout>
16 #include <QtWidgets/QLabel>
17 #include <QtWidgets/QLineEdit>
18 #include <QtWidgets/QMessageBox>
19 #include <QtWidgets/QSpinBox>
20 #include <QtWidgets/QTextBrowser>
21 #include <QtWidgets/QTextEdit>
22
23 #include <QtCore/QDebug>
24 #include <QtCore/QJsonArray>
25 #include <QtCore/QJsonDocument>
26 #include <QtCore/QPointer>
27 #include <QtCore/QSettings>
28 #include <QtCore/QTimer>
29
30 namespace Avogadro::QtGui {
31
32 InterfaceWidget::InterfaceWidget(const QString& scriptFilePath,
33 QWidget* parent_)
34 : JsonWidget(parent_), m_interfaceScript(QString())
35 {
36 if (!scriptFilePath.isEmpty())
37 this->setInterfaceScript(scriptFilePath);
38 }
39
40 InterfaceWidget::~InterfaceWidget() {}
41
42 void InterfaceWidget::setInterfaceScript(const QString& scriptFile)
43 {
44 m_interfaceScript.setScriptFilePath(scriptFile);
45 m_options = m_interfaceScript.options();
46 updateOptions();
47 }
48
49 void InterfaceWidget::reloadOptions()
50 {
51 m_options = m_interfaceScript.options();
52 updateOptions();
53 }
54
55 void InterfaceWidget::defaultsClicked()
56 {
57 setOptionDefaults();
58 }
59
60 void InterfaceWidget::setWarningText(const QString& warn)
61 {
62 qWarning() << tr("Script returns warnings:\n") << warn;
63 }
64
65 QString InterfaceWidget::warningText() const
66 {
67 return QString();
68 }
69
70 void InterfaceWidget::showError(const QString& err)
71 {
72 qWarning() << err;
73
74 QWidget* theParent =
75 this->isVisible() ? this : qobject_cast<QWidget*>(parent());
76 QDialog dlg(theParent);
77 auto* vbox = new QVBoxLayout();
78 auto* label = new QLabel(tr("An error has occurred:"));
79 vbox->addWidget(label);
80 auto* textBrowser = new QTextBrowser();
81
82 // adjust the size of the text browser to ~80 char wide, ~20 lines high
83 QSize theSize = textBrowser->sizeHint();
84 QFontMetrics metrics(textBrowser->currentFont());
85 int charWidth = metrics.horizontalAdvance(QStringLiteral("i7OPlmWn9/")) / 10;
86 int charHeight = metrics.lineSpacing();
87 theSize.setWidth(80 * charWidth);
88 theSize.setHeight(20 * charHeight);
89 textBrowser->setMinimumSize(theSize);
90 textBrowser->setText(err);
91 vbox->addWidget(textBrowser);
92 dlg.setLayout(vbox);
93
94 dlg.exec();
95 }
96
97 QString InterfaceWidget::settingsKey(const QString& identifier) const
98 {
99 return QStringLiteral("scriptPlugin/%1/%2")
100 .arg(m_interfaceScript.displayName(), identifier);
101 }
102
103 } // namespace Avogadro::QtGui