Mantid
Loading...
Searching...
No Matches
SortTableWorkspaceDialog.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//------------------------------------------------------------------------------
15
16using namespace MantidQt::API;
17
19
20// Declare the dialog. Name must match the class name
21DECLARE_DIALOG(SortTableWorkspaceDialog)
22
23
25
28 // set up the GUI elements
29 m_form.setupUi(this);
30
31 // add the Run/Cancel buttons
32 m_form.dialogLayout->addLayout(this->createDefaultButtonLayout());
33
34 // correct the tab order
35 QWidget::setTabOrder(m_form.groupBox, m_form.cbColumnName);
36 QWidget::setTabOrder(m_form.cbColumnName, m_form.cbAscending);
37
38 // disable Add/Remove buttons in case there are no table workspaces at all
39 m_form.btnRemoveColumn->setEnabled(false);
40 m_form.btnAddColumn->setEnabled(false);
41
42 // connect the slots
43 connect(m_form.workspace, SIGNAL(currentIndexChanged(const QString &)), this,
44 SLOT(workspaceChanged(const QString &)));
45 connect(m_form.workspace, SIGNAL(emptied()), this, SLOT(clearGUI()));
46 connect(m_form.cbColumnName, SIGNAL(currentIndexChanged(int)), this, SLOT(changedColumnName(int)));
47 connect(m_form.btnAddColumn, SIGNAL(clicked()), this, SLOT(addColumn()));
48 connect(m_form.btnRemoveColumn, SIGNAL(clicked()), this, SLOT(removeColumn()));
49
50 tieStaticWidgets(true);
51}
52
57 QStringList columns;
58 QStringList ascending;
59
60 // extract column names to sort by from the controls, sort order too
61 auto n = m_sortColumns.size();
62 for (int i = 0; i < n; ++i) {
63 auto itemColumn = m_form.columnsLayout->itemAtPosition(i, 1);
64 auto itemAscending = m_form.columnsLayout->itemAtPosition(i, 2);
65 if (!itemColumn || !itemColumn->widget() || !itemAscending || !itemAscending->widget()) {
66 throw std::logic_error("Logic error in SortTableWorkspaceDialog: internal inconsistency.");
67 }
68
69 auto name = dynamic_cast<QComboBox *>(itemColumn->widget())->currentText();
70 auto ia = dynamic_cast<QComboBox *>(itemAscending->widget())->currentIndex();
71 columns << name;
72 ascending << QString::number(ia == 0 ? 1 : 0);
73 }
74
75 // pass the properties to the algorithm
76 storePropertyValue("Columns", columns.join(","));
77 storePropertyValue("Ascending", ascending.join(","));
78}
79
84void SortTableWorkspaceDialog::tieStaticWidgets(const bool /*unused*/) {
85 QStringList allowedTypes;
86 allowedTypes << "TableWorkspace";
87 m_form.workspace->setWorkspaceTypes(allowedTypes);
88 tie(m_form.workspace, "InputWorkspace");
89 tie(m_form.output, "OutputWorkspace");
90 if (!m_form.workspace->currentText().isEmpty()) {
91 // start with output == input
92 m_form.output->setText(m_form.workspace->currentText());
93 }
94}
95
101 // start with output == input
102 m_form.output->setText(wsName);
103 // prepare the controls for new values
104 clearGUI();
105 if (wsName.isEmpty())
106 return;
107 try {
108 auto ws =
110 if (!ws)
111 return;
112 m_columnNames.clear();
113 // get and cache the column names from the workspace
114 auto columnNames = ws->getColumnNames();
115 for (auto &columnName : columnNames) {
116 m_columnNames << QString::fromStdString(columnName);
117 }
118 m_form.cbColumnName->addItems(m_columnNames);
119 // the GUI already has the controls to set the first column
120 // if there are no other columns in the table no more names can be added
121 if (m_columnNames.size() <= 1) {
122 m_form.btnAddColumn->setEnabled(false);
123 } else {
124 m_form.btnAddColumn->setEnabled(true);
125 }
126 // cache the selected column name
127 m_sortColumns[0] = m_form.cbColumnName->currentText();
129 return;
130 }
131}
132
137 m_columnNames.clear();
138 m_form.lblColumnName->setText("Column");
139 m_form.cbColumnName->clear();
140 m_form.cbAscending->setCurrentIndex(0);
141 m_form.btnAddColumn->setEnabled(false);
142 m_form.btnRemoveColumn->setEnabled(false);
143 // remove controls for any additional columns
144 auto nRows = m_form.columnsLayout->rowCount();
145 for (auto row = nRows - 1; row > 0; --row) {
146 for (int col = 0; col < 3; ++col) {
147 auto item = m_form.columnsLayout->itemAtPosition(row, col);
148 if (item) {
149 auto index = m_form.columnsLayout->indexOf(item->widget());
150 m_form.columnsLayout->takeAt(index);
151 item->widget()->deleteLater();
152 }
153 }
154 }
155 // leave a space for one column name
156 // size of m_sortColumns is used to get the number of selected columns
157 m_sortColumns.clear();
158 m_sortColumns << "";
159}
160
165 m_form.lblColumnName->setText("Column 1");
166 // create controls for setting new column
167 auto newRow = m_sortColumns.size();
168 assert(newRow <= m_columnNames.size());
169 QLabel *label = new QLabel(QString("Column %1").arg(newRow + 1));
170 auto *columnName = new QComboBox();
171 columnName->addItems(m_columnNames);
172 columnName->setToolTip(m_form.cbColumnName->toolTip());
173 connect(columnName, SIGNAL(currentIndexChanged(int)), this, SLOT(changedColumnName(int)));
174 auto *ascending = new QComboBox();
175 ascending->addItem("Ascending");
176 ascending->addItem("Descending");
177 ascending->setToolTip(m_form.cbAscending->toolTip());
178 // add them to the layout
179 m_form.columnsLayout->addWidget(label, newRow, 0);
180 m_form.columnsLayout->addWidget(columnName, newRow, 1);
181 m_form.columnsLayout->addWidget(ascending, newRow, 2);
182 // correct the tab order
183 QWidget::setTabOrder(m_form.columnsLayout->itemAtPosition(newRow - 1, 2)->widget(), columnName);
184 QWidget::setTabOrder(columnName, ascending);
185
186 // suggest a name for the new column: one that hasn't been used in
187 // other sort columns
188 QString newColumnName;
189 foreach (QString name, m_columnNames) {
190 if (!m_sortColumns.contains(name)) {
191 columnName->setItemText(-1, name);
192 break;
193 }
194 }
195 // cache the column name
196 m_sortColumns << columnName->currentText();
197 // set the Add/Remove buttons into a correct state
198 if (m_sortColumns.size() == m_columnNames.size()) {
199 m_form.btnAddColumn->setEnabled(false);
200 }
201 m_form.btnRemoveColumn->setEnabled(true);
202}
203
208 // don't try to figure out which column changed - just reset all cached names
209 auto n = m_sortColumns.size();
210 for (int i = 0; i < n; ++i) {
211 auto item = m_form.columnsLayout->itemAtPosition(i, 1);
212 if (!item || !item->widget() || !dynamic_cast<QComboBox *>(item->widget())) {
213 throw std::logic_error("Logic error in SortTableWorkspaceDialog: internal inconsistency.");
214 }
215
216 auto name = dynamic_cast<QComboBox *>(item->widget())->currentText();
217 m_sortColumns[i] = name;
218 }
219}
220
225 assert(m_columnNames.size() > 1);
226 // remove the last column
227 m_sortColumns.removeLast();
228 auto row = m_sortColumns.size();
229 for (int col = 0; col < 3; ++col) {
230 auto item = m_form.columnsLayout->itemAtPosition(row, col);
231 if (item) {
232 auto index = m_form.columnsLayout->indexOf(item->widget());
233 m_form.columnsLayout->takeAt(index);
234 item->widget()->deleteLater();
235 }
236 }
237 // leave the Add/Remove buttons in a correct state
238 if (m_sortColumns.size() == 1) {
239 m_form.btnRemoveColumn->setEnabled(false);
240 m_form.lblColumnName->setText("Column");
241 }
242 m_form.btnAddColumn->setEnabled(true);
243}
244
245} // namespace MantidQt::CustomDialogs
#define DECLARE_DIALOG(classname)
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
This class should be the basis for all customised algorithm dialogs.
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.
QWidget * tie(QWidget *widget, const QString &property, QLayout *parent_layout=nullptr, bool readHistory=true)
Tie a widget to a property.
This class gives specialised dialog for the SortTableWorkspace algorithm.
void tieStaticWidgets(const bool readHistory)
Tie static widgets to their properties.
void addColumn()
Add GUI elements to set a new column as a sorting key.
QStringList m_columnNames
Names of the columns in the workspace.
void changedColumnName(int)
Sync the GUI after a sorting column name changes.
QStringList m_sortColumns
Names of columns used to sort the table.
void clearGUI()
Clear the GUI form the workspace specific data/elements.
void parseInput() override
Pass input from non-standard GUI elements to the algorithm.
void workspaceChanged(const QString &wsName)
Update GUI after workspace changes.
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Exception for when an item is not found in a collection.
Definition: Exception.h:145
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...