Mantid
Loading...
Searching...
No Matches
UserSubWindow.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------
8// Includes
9//----------------------------------
13
14#include <QDir>
15#include <QFileDialog>
16#include <QIcon>
17#include <QMessageBox>
18#include <QTemporaryFile>
19#include <QTextStream>
20
21using namespace MantidQt::API;
22
23//------------------------------------------------------
24// Public member functions
25//------------------------------------------------------
30 : QMainWindow(parent), m_bIsInitialized(false), m_isPyInitialized(false), m_ifacename(""), m_pythonRunner() {
31 setAttribute(Qt::WA_DeleteOnClose, false);
32
33 // re-emit the run Python code from m_pyRunner, to work this signal must reach
34 // the slot in QtiPlot
35 connect(&m_pythonRunner, SIGNAL(runAsPythonScript(const QString &, bool)), this,
36 SIGNAL(runAsPythonScript(const QString &, bool)));
37}
38
43 if (isInitialized())
44 return;
45
46 // Calls the derived class function
47 this->initLayout();
48
49 // Se the object name to the interface name
50 setObjectName(m_ifacename);
51
52 // Set the icon
53 setWindowIcon(QIcon(":/mantidplot.png"));
54
55 m_bIsInitialized = true;
56
58 m_ifacename.toStdString(), false);
59}
60
66
72
79 if (isPyInitialized())
80 return;
81
82 // Call overridable function
83 this->initLocalPython();
84 m_isPyInitialized = true;
85}
86
87//--------------------------------------
88// Protected member functions
89//-------------------------------------
95void UserSubWindow::showInformationBox(const QString &message) const {
96 if (!message.isEmpty()) {
97 QMessageBox::information(const_cast<UserSubWindow *>(this), this->windowTitle(), message);
98 }
99}
100
110QString UserSubWindow::runPythonCode(const QString &code, bool no_output) {
111 return m_pythonRunner.runPythonCode(code, no_output);
112}
113
121QString UserSubWindow::openFileDialog(const bool save, const QStringList &exts) {
122 QString filter;
123 if (!exts.empty()) {
124 filter = "";
125 for (int i = 0; i < exts.size(); i++) {
126 filter.append("*." + exts[i] + " ");
127 }
128 filter = filter.trimmed();
129 }
130 filter.append(";;All Files (*)");
131
132 QString filename;
133 if (save) {
134 filename = QFileDialog::getSaveFileName(this, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(),
135 filter);
136 } else {
137 filename = QFileDialog::getOpenFileName(this, "Open file", AlgorithmInputHistory::Instance().getPreviousDirectory(),
138 filter);
139 }
140
141 if (!filename.isEmpty()) {
142 AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(filename).absoluteDir().path());
143 }
144 return filename;
145}
150QLabel *UserSubWindow::newValidator(QWidget *parent) {
151 auto *validLbl = new QLabel("*", parent);
152 QPalette pal = validLbl->palette();
153 pal.setColor(QPalette::WindowText, Qt::darkRed);
154 validLbl->setPalette(pal);
155 return validLbl;
156}
157
158//--------------------------------------
159// Private member functions
160//-------------------------------------
165void UserSubWindow::setInterfaceName(const QString &iface_name) { m_ifacename = iface_name; }
QString runPythonCode(const QString &code, bool no_output=false)
Run python code.
This is the base class all customised user interfaces that do not wish to be tied to a specific Manti...
Definition: UserSubWindow.h:70
QString runPythonCode(const QString &code, bool no_output=false)
Run a piece of python code and return any output that was written to stdout.
void showInformationBox(const QString &message) const
Raise a dialog box giving some information.
QLabel * newValidator(QWidget *parent)
Returns a pointer to a new validator QLabel.
QString openFileDialog(const bool save, const QStringList &exts)
Open a file selection box.
void runAsPythonScript(const QString &code, bool)
Emitted to start a (generally small) script running.
void setInterfaceName(const QString &iface_name)
Set the interface name, made public so possible from Python.
QString m_ifacename
Store the name of the interface.
bool m_bIsInitialized
Has this already been initialized.
UserSubWindow(QWidget *parent=nullptr)
DefaultConstructor.
virtual void initLocalPython()
Run local Python setup code.
virtual void initLayout()=0
To be overridden to set the appropriate layout.
bool isPyInitialized() const
Has the Python initialization function been run.
void initializeLocalPython()
Run local Python init code.
void initializeLayout()
Create the layout of the widget. Can only be called once.
PythonRunner m_pythonRunner
Python executor.
bool isInitialized() const
Is this dialog initialized.
bool m_isPyInitialized
Has the python initialization been run.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...