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 // Disabled the python arguments
86 disabled += m_python_arguments;
88
89 // At this point, all the widgets have been added and are visible.
90 // This makes sure the viewport does not get scaled smaller, even if some
91 // controls are hidden.
92 QWidget *viewport = m_algoPropertiesWidget->m_viewport;
93 // QScrollArea * scroll = m_algoPropertiesWidget->m_scroll;
94 viewport->layout()->update();
95 // This makes the layout minimum size = that of the widgets inside
96 viewport->layout()->setSizeConstraint(QLayout::SetMinimumSize);
97
98 QCoreApplication::processEvents();
99
100 int screenHeight = QApplication::desktop()->height();
101 int dialogHeight = viewport->sizeHint().height();
102
103 // If the thing won't end up too big compared to the screen height,
104 // resize the scroll area so we don't get a scroll bar
105 if ((dialogHeight + 100) < 0.8 * screenHeight) {
106 m_algoPropertiesWidget->m_scroll->setMinimumHeight(dialogHeight + 10);
107
108 // Find the size that the dialog WANTS to be.
109 dialogHeight = this->sizeHint().height();
110
111 // Choose a width given the desired size, but limit it
112 int dialogWidth = this->sizeHint().width() + 25;
113 if (dialogWidth > 640)
114 dialogWidth = 640;
115
116 // But allow the scroll area to resize smaller again
117 m_algoPropertiesWidget->m_scroll->setMinimumHeight(60);
118 // But resize the dialog again to its preferred size.
119 this->resize(dialogWidth, dialogHeight);
120 }
121
122 // Set all previous values (from history, etc.)
123 for (auto it = m_algoPropertiesWidget->m_propWidgets.begin(); it != m_algoPropertiesWidget->m_propWidgets.end();
124 it++) {
125 this->setPreviousValue(it.value(), it.key());
126 }
127
128 // Using the default values, hide or disable the dynamically shown properties
130}
131
132//-----------------------------------------------------------------------------
136 auto itr = m_algoPropertiesWidget->m_propWidgets.begin();
137 for (; itr != m_algoPropertiesWidget->m_propWidgets.end(); itr++) {
138 // Get the value from each widget and store it
139 storePropertyValue(itr.key(), itr.value()->getValue());
140 }
141}
142
143//-----------------------------------------------------------------------------
149 // Get property values
150 parse();
151
152 // Try and set and validate the properties and
153 if (setPropertyValues()) {
154 // Store input for next time
155 saveInput();
156 if (!this->m_keepOpen) {
157 QDialog::accept();
158 } else {
160 }
161 } else {
162 // Highlight the validators that are in error (combined from them + whole
163 // algorithm)
164 // If got there, there were errors
165 for (auto it = m_errors.begin(); it != m_errors.end(); it++) {
166 // if these assert is encounted, the property and validate keys may not
167 // match (check case)
168 assert(m_algoPropertiesWidget->m_propWidgets[it.key()]);
169 m_algoPropertiesWidget->m_propWidgets[it.key()]->updateIconVisibility(it.value());
170 }
171 QMessageBox::critical(this, "",
172 "One or more properties are invalid. The invalid properties are\n"
173 "marked with a *, hold your mouse over the * for more information.");
174 }
175}
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 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 validators to determine whether they should be made di...
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.
Definition: GenericDialog.h:56
void initLayout() override
Create the layout for this dialog.
~GenericDialog() override
Destructor.