Mantid
Loading...
Searching...
No Matches
GenericDialog.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//----------------------------------
12
17
18#include <QApplication>
19#include <QCheckBox>
20#include <QComboBox>
21#include <QDir>
22#include <QFileInfo>
23#include <QGridLayout>
24#include <QGroupBox>
25#include <QGuiApplication>
26#include <QHBoxLayout>
27#include <QLabel>
28#include <QLineEdit>
29#include <QMessageBox>
30#include <QPalette>
31#include <QPushButton>
32#include <QScreen>
33#include <QScrollArea>
34#include <QSignalMapper>
35#include <QVBoxLayout>
36
40#include <climits>
41
42// Dialog stuff is defined here
43using namespace MantidQt::API;
44using namespace Mantid::Kernel;
45using namespace Mantid::API;
46
47//----------------------------------
48// Public member functions
49//----------------------------------
53GenericDialog::GenericDialog(QWidget *parent) : AlgorithmDialog(parent), m_algoPropertiesWidget(nullptr) {}
54
59
60//----------------------------------
61// Protected member functions
62//----------------------------------
67
68 // Add a layout for QDialog
69 auto *dialog_layout = new QVBoxLayout();
70 setLayout(dialog_layout);
71 // Add the helpful summary message
73 this->addOptionalMessage(dialog_layout);
74
75 // Make the widget with all the properties
77 dialog_layout->addWidget(m_algoPropertiesWidget, 1);
79
80 // Create and add the OK/Cancel/Help. buttons
81 dialog_layout->addLayout(this->createDefaultButtonLayout(), 0);
82
83 // Mark the properties that will be forced enabled or disabled
84 QStringList enabled = m_enabled;
85 QStringList disabled = m_disabled;
86 // Disable any python arguments
87 disabled += m_python_arguments;
89
90 // Share the errors map with the parent dialog
92
93 // At this point, all the widgets have been added and are visible.
94 // This makes sure the viewport does not get scaled smaller, even if some
95 // controls are hidden.
96 QWidget *viewport = m_algoPropertiesWidget->m_viewport;
97 // QScrollArea * scroll = m_algoPropertiesWidget->m_scroll;
98 viewport->layout()->update();
99 // This makes the layout minimum size = that of the widgets inside
100 viewport->layout()->setSizeConstraint(QLayout::SetMinimumSize);
101
102 QCoreApplication::processEvents();
103
104 int screenHeight = QGuiApplication::primaryScreen()->geometry().height();
105 int dialogHeight = viewport->sizeHint().height();
106
107 // If the thing won't end up too big compared to the screen height,
108 // resize the scroll area so we don't get a scroll bar
109 if ((dialogHeight + 100) < 0.8 * screenHeight) {
110 m_algoPropertiesWidget->m_scroll->setMinimumHeight(dialogHeight + 10);
111
112 // Find the size that the dialog WANTS to be.
113 dialogHeight = this->sizeHint().height();
114
115 // Choose a width given the desired size, but limit it
116 int dialogWidth = this->sizeHint().width() + 25;
117 if (dialogWidth > 640)
118 dialogWidth = 640;
119
120 // But allow the scroll area to resize smaller again
121 m_algoPropertiesWidget->m_scroll->setMinimumHeight(60);
122 // But resize the dialog again to its preferred size.
123 this->resize(dialogWidth, dialogHeight);
124 }
125
126 // Set all previous values (from history, etc.)
127 for (auto it = m_algoPropertiesWidget->m_propWidgets.begin(); it != m_algoPropertiesWidget->m_propWidgets.end();
128 it++) {
129 this->setPreviousValue(it.value(), it.key());
130 }
131
132 // Using the default values, hide or disable the dynamically shown properties
134}
135
136//-----------------------------------------------------------------------------
140 auto itr = m_algoPropertiesWidget->m_propWidgets.begin();
141 for (; itr != m_algoPropertiesWidget->m_propWidgets.end(); itr++) {
142 // Get the value from each widget and store it
143 storePropertyValue(itr.key(), itr.value()->getValue());
144 }
145}
146
147//-----------------------------------------------------------------------------
153 // Get property values
154 parse();
155
156 // Try and set and validate the properties and
157 if (setPropertyValues()) {
158 // Store input for next time
159 saveInput();
160 if (!this->m_keepOpen) {
161 QDialog::accept();
162 } else {
164 }
165 } else {
166 // Highlight the validators that are in error (combined from them + whole
167 // algorithm)
168 // If we get to this clause, there are errors
169 for (auto it = m_errors.begin(); it != m_errors.end(); it++) {
170 // if this assert is encountered, the property and validator keys may not
171 // match (check case)
172 assert(m_algoPropertiesWidget->m_propWidgets[it.key()]);
173 m_algoPropertiesWidget->m_propWidgets[it.key()]->updateIconVisibility(it.value());
174 }
175 QMessageBox::critical(this, "",
176 "One or more properties are invalid. The invalid properties are\n"
177 "marked with a *, hold your mouse over the * for more information.");
178 }
179}
This class should be the basis for all customised algorithm dialogs.
bool isMessageAvailable() const
Is there a message string available.
QStringList m_enabled
A list of property names that should have their widgets enabled.
virtual void executeAlgorithmAsync()
Executes the algorithm in a separate thread.
bool setPropertyValues(const QStringList &skipList=QStringList())
Set properties on this algorithm by pulling values from the tied widgets.
QStringList m_python_arguments
A list of property names that have been passed from Python.
QStringList m_disabled
A list of property names that the user has requested to be disabled (overrides those in enabled)
QHash< QString, QString > m_errors
A map where key = property name; value = the error for this property (i.e.
bool m_keepOpen
Whether to keep the dialog box open after alg execution.
QLayout * createDefaultButtonLayout(const QString &helpText=QString("?"), const QString &loadText=QString("Run"), const QString &cancelText=QString("Close"), const QString &keepOpenText=QString("Keep Open"))
Create a row layout of buttons with specified text.
void storePropertyValue(const QString &name, const QString &value)
Adds a property (name,value) pair to the stored map.
void parse()
Parse out the input from the dialog.
Mantid::API::IAlgorithm_sptr getAlgorithm() const
Get the algorithm pointer.
void setPreviousValue(QWidget *widget, const QString &property)
Set a value based on any old input that we have.
void addOptionalMessage(QVBoxLayout *mainLay)
Add the optional message to the given layout.
virtual void saveInput()
Save the input history of an accepted dialog.
Widget that contains dynamically generated PropertyWidget's for each property of an algorithm,...
QScrollArea * m_scroll
Scroll area containing the viewport.
QWidget * m_viewport
Viewport containing the grid of property widgets.
void shareErrorsMap(const QHash< QString, QString > &errors)
Share the errors map with the parent dialog.
void setAlgorithm(const Mantid::API::IAlgorithm_sptr &algo)
Directly set the algorithm to view.
void hideOrDisableProperties(const QString &changedPropName="")
Go through all the properties, and check their settings in order to implement any changes dependent o...
void addEnabledAndDisableLists(const QStringList &enabled, const QStringList &disabled)
Sets the properties to force as enabled/disabled.
QHash< QString, PropertyWidget * > m_propWidgets
Each dynamically created PropertyWidget.
void accept() override
A slot that can be used to connect a button that accepts the dialog if all of the properties are vali...
void parseInput() override
Parse out information from the dialog.
GenericDialog(QWidget *parent=nullptr)
Default Constructor.
AlgorithmPropertiesWidget * m_algoPropertiesWidget
Widget containing all the PropertyWidgets.
void initLayout() override
Create the layout for this dialog.
~GenericDialog() override
Destructor.