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 <QDesktopWidget>
22#include <QDir>
23#include <QFileInfo>
24#include <QGridLayout>
25#include <QGroupBox>
26#include <QHBoxLayout>
27#include <QLabel>
28#include <QLineEdit>
29#include <QMessageBox>
30#include <QPalette>
31#include <QPushButton>
32#include <QScrollArea>
33#include <QSignalMapper>
34#include <QVBoxLayout>
35
39#include <climits>
40
41// Dialog stuff is defined here
42using namespace MantidQt::API;
43using namespace Mantid::Kernel;
44using namespace Mantid::API;
45
46//----------------------------------
47// Public member functions
48//----------------------------------
52GenericDialog::GenericDialog(QWidget *parent) : AlgorithmDialog(parent), m_algoPropertiesWidget(nullptr) {}
53
58
59//----------------------------------
60// Protected member functions
61//----------------------------------
66
67 // Add a layout for QDialog
68 auto *dialog_layout = new QVBoxLayout();
69 setLayout(dialog_layout);
70 // Add the helpful summary message
72 this->addOptionalMessage(dialog_layout);
73
74 // Make the widget with all the properties
76 dialog_layout->addWidget(m_algoPropertiesWidget, 1);
78
79 // Create and add the OK/Cancel/Help. buttons
80 dialog_layout->addLayout(this->createDefaultButtonLayout(), 0);
81
82 // Mark the properties that will be forced enabled or disabled
83 QStringList enabled = m_enabled;
84 QStringList disabled = m_disabled;
85 // Disable any python arguments
86 disabled += m_python_arguments;
88
89 // Share the errors map with the parent dialog
91
92 // At this point, all the widgets have been added and are visible.
93 // This makes sure the viewport does not get scaled smaller, even if some
94 // controls are hidden.
95 QWidget *viewport = m_algoPropertiesWidget->m_viewport;
96 // QScrollArea * scroll = m_algoPropertiesWidget->m_scroll;
97 viewport->layout()->update();
98 // This makes the layout minimum size = that of the widgets inside
99 viewport->layout()->setSizeConstraint(QLayout::SetMinimumSize);
100
101 QCoreApplication::processEvents();
102
103 int screenHeight = QApplication::desktop()->height();
104 int dialogHeight = viewport->sizeHint().height();
105
106 // If the thing won't end up too big compared to the screen height,
107 // resize the scroll area so we don't get a scroll bar
108 if ((dialogHeight + 100) < 0.8 * screenHeight) {
109 m_algoPropertiesWidget->m_scroll->setMinimumHeight(dialogHeight + 10);
110
111 // Find the size that the dialog WANTS to be.
112 dialogHeight = this->sizeHint().height();
113
114 // Choose a width given the desired size, but limit it
115 int dialogWidth = this->sizeHint().width() + 25;
116 if (dialogWidth > 640)
117 dialogWidth = 640;
118
119 // But allow the scroll area to resize smaller again
120 m_algoPropertiesWidget->m_scroll->setMinimumHeight(60);
121 // But resize the dialog again to its preferred size.
122 this->resize(dialogWidth, dialogHeight);
123 }
124
125 // Set all previous values (from history, etc.)
126 for (auto it = m_algoPropertiesWidget->m_propWidgets.begin(); it != m_algoPropertiesWidget->m_propWidgets.end();
127 it++) {
128 this->setPreviousValue(it.value(), it.key());
129 }
130
131 // Using the default values, hide or disable the dynamically shown properties
133}
134
135//-----------------------------------------------------------------------------
139 auto itr = m_algoPropertiesWidget->m_propWidgets.begin();
140 for (; itr != m_algoPropertiesWidget->m_propWidgets.end(); itr++) {
141 // Get the value from each widget and store it
142 storePropertyValue(itr.key(), itr.value()->getValue());
143 }
144}
145
146//-----------------------------------------------------------------------------
152 // Get property values
153 parse();
154
155 // Try and set and validate the properties and
156 if (setPropertyValues()) {
157 // Store input for next time
158 saveInput();
159 if (!this->m_keepOpen) {
160 QDialog::accept();
161 } else {
163 }
164 } else {
165 // Highlight the validators that are in error (combined from them + whole
166 // algorithm)
167 // If we get to this clause, there are errors
168 for (auto it = m_errors.begin(); it != m_errors.end(); it++) {
169 // if this assert is encountered, the property and validator keys may not
170 // match (check case)
171 assert(m_algoPropertiesWidget->m_propWidgets[it.key()]);
172 m_algoPropertiesWidget->m_propWidgets[it.key()]->updateIconVisibility(it.value());
173 }
174 QMessageBox::critical(this, "",
175 "One or more properties are invalid. The invalid properties are\n"
176 "marked with a *, hold your mouse over the * for more information.");
177 }
178}
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.