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 =
109 Mantid::API::AnalysisDataService::Instance().retrieveWS<Mantid::API::ITableWorkspace>(wsName.toStdString());
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 (const 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 const auto it = std::find_if(m_columnNames.cbegin(), m_columnNames.cend(),
189 [&](const auto &name) { return !m_sortColumns.contains(name); });
190 if (it != m_columnNames.cend()) {
191 columnName->setItemText(-1, *it);
192 }
193 // cache the column name
194 m_sortColumns << columnName->currentText();
195 // set the Add/Remove buttons into a correct state
196 if (m_sortColumns.size() == m_columnNames.size()) {
197 m_form.btnAddColumn->setEnabled(false);
198 }
199 m_form.btnRemoveColumn->setEnabled(true);
200}
201
206 // don't try to figure out which column changed - just reset all cached names
207 auto n = m_sortColumns.size();
208 for (int i = 0; i < n; ++i) {
209 auto item = m_form.columnsLayout->itemAtPosition(i, 1);
210 if (!item || !item->widget() || !dynamic_cast<QComboBox *>(item->widget())) {
211 throw std::logic_error("Logic error in SortTableWorkspaceDialog: internal inconsistency.");
212 }
213
214 auto name = dynamic_cast<QComboBox *>(item->widget())->currentText();
215 m_sortColumns[i] = name;
216 }
217}
218
223 assert(m_columnNames.size() > 1);
224 // remove the last column
225 m_sortColumns.removeLast();
226 auto row = m_sortColumns.size();
227 for (int col = 0; col < 3; ++col) {
228 auto item = m_form.columnsLayout->itemAtPosition(row, col);
229 if (item) {
230 auto index = m_form.columnsLayout->indexOf(item->widget());
231 m_form.columnsLayout->takeAt(index);
232 item->widget()->deleteLater();
233 }
234 }
235 // leave the Add/Remove buttons in a correct state
236 if (m_sortColumns.size() == 1) {
237 m_form.btnRemoveColumn->setEnabled(false);
238 m_form.lblColumnName->setText("Column");
239 }
240 m_form.btnAddColumn->setEnabled(true);
241}
242
243} // namespace MantidQt::CustomDialogs
std::string name
Definition Run.cpp:60
#define DECLARE_DIALOG(classname)
std::map< DeltaEMode::Type, std::string > index
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 ...
virtual std::vector< std::string > getColumnNames() const =0
Returns a vector of all column names.
Exception for when an item is not found in a collection.
Definition Exception.h:145