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(currentTextChanged(const QString &)), this, SLOT(workspaceChanged(const QString &)));
44 connect(m_form.workspace, SIGNAL(emptied()), this, SLOT(clearGUI()));
45 connect(m_form.cbColumnName, SIGNAL(currentIndexChanged(int)), this, SLOT(changedColumnName(int)));
46 connect(m_form.btnAddColumn, SIGNAL(clicked()), this, SLOT(addColumn()));
47 connect(m_form.btnRemoveColumn, SIGNAL(clicked()), this, SLOT(removeColumn()));
48
49 tieStaticWidgets(true);
50}
51
56 QStringList columns;
57 QStringList ascending;
58
59 // extract column names to sort by from the controls, sort order too
60 auto n = m_sortColumns.size();
61 for (int i = 0; i < n; ++i) {
62 auto itemColumn = m_form.columnsLayout->itemAtPosition(i, 1);
63 auto itemAscending = m_form.columnsLayout->itemAtPosition(i, 2);
64 if (!itemColumn || !itemColumn->widget() || !itemAscending || !itemAscending->widget()) {
65 throw std::logic_error("Logic error in SortTableWorkspaceDialog: internal inconsistency.");
66 }
67
68 auto name = dynamic_cast<QComboBox *>(itemColumn->widget())->currentText();
69 auto ia = dynamic_cast<QComboBox *>(itemAscending->widget())->currentIndex();
70 columns << name;
71 ascending << QString::number(ia == 0 ? 1 : 0);
72 }
73
74 // pass the properties to the algorithm
75 storePropertyValue("Columns", columns.join(","));
76 storePropertyValue("Ascending", ascending.join(","));
77}
78
83void SortTableWorkspaceDialog::tieStaticWidgets(const bool /*unused*/) {
84 QStringList allowedTypes;
85 allowedTypes << "TableWorkspace";
86 m_form.workspace->setWorkspaceTypes(allowedTypes);
87 tie(m_form.workspace, "InputWorkspace");
88 tie(m_form.output, "OutputWorkspace");
89 if (!m_form.workspace->currentText().isEmpty()) {
90 // start with output == input
91 m_form.output->setText(m_form.workspace->currentText());
92 }
93}
94
99void SortTableWorkspaceDialog::workspaceChanged(const QString &wsName) {
100 // start with output == input
101 m_form.output->setText(wsName);
102 // prepare the controls for new values
103 clearGUI();
104 if (wsName.isEmpty())
105 return;
106 try {
107 auto ws =
108 Mantid::API::AnalysisDataService::Instance().retrieveWS<Mantid::API::ITableWorkspace>(wsName.toStdString());
109 if (!ws)
110 return;
111 m_columnNames.clear();
112 // get and cache the column names from the workspace
113 auto columnNames = ws->getColumnNames();
114 for (const auto &columnName : columnNames) {
115 m_columnNames << QString::fromStdString(columnName);
116 }
117 m_form.cbColumnName->addItems(m_columnNames);
118 // the GUI already has the controls to set the first column
119 // if there are no other columns in the table no more names can be added
120 if (m_columnNames.size() <= 1) {
121 m_form.btnAddColumn->setEnabled(false);
122 } else {
123 m_form.btnAddColumn->setEnabled(true);
124 }
125 // cache the selected column name
126 m_sortColumns[0] = m_form.cbColumnName->currentText();
128 return;
129 }
130}
131
136 m_columnNames.clear();
137 m_form.lblColumnName->setText("Column");
138 m_form.cbColumnName->clear();
139 m_form.cbAscending->setCurrentIndex(0);
140 m_form.btnAddColumn->setEnabled(false);
141 m_form.btnRemoveColumn->setEnabled(false);
142 // remove controls for any additional columns
143 auto nRows = m_form.columnsLayout->rowCount();
144 for (auto row = nRows - 1; row > 0; --row) {
145 for (int col = 0; col < 3; ++col) {
146 auto item = m_form.columnsLayout->itemAtPosition(row, col);
147 if (item) {
148 auto index = m_form.columnsLayout->indexOf(item->widget());
149 m_form.columnsLayout->takeAt(index);
150 item->widget()->deleteLater();
151 }
152 }
153 }
154 // leave a space for one column name
155 // size of m_sortColumns is used to get the number of selected columns
156 m_sortColumns.clear();
157 m_sortColumns << "";
158}
159
164 m_form.lblColumnName->setText("Column 1");
165 // create controls for setting new column
166 int newRow = static_cast<int>(m_sortColumns.size());
167 assert(newRow <= m_columnNames.size());
168 QLabel *label = new QLabel(QString("Column %1").arg(newRow + 1));
169 auto *columnName = new QComboBox();
170 columnName->addItems(m_columnNames);
171 columnName->setToolTip(m_form.cbColumnName->toolTip());
172 connect(columnName, SIGNAL(currentIndexChanged(int)), this, SLOT(changedColumnName(int)));
173 auto *ascending = new QComboBox();
174 ascending->addItem("Ascending");
175 ascending->addItem("Descending");
176 ascending->setToolTip(m_form.cbAscending->toolTip());
177 // add them to the layout
178 m_form.columnsLayout->addWidget(label, newRow, 0);
179 m_form.columnsLayout->addWidget(columnName, newRow, 1);
180 m_form.columnsLayout->addWidget(ascending, newRow, 2);
181 // correct the tab order
182 QWidget::setTabOrder(m_form.columnsLayout->itemAtPosition(newRow - 1, 2)->widget(), columnName);
183 QWidget::setTabOrder(columnName, ascending);
184
185 // suggest a name for the new column: one that hasn't been used in
186 // other sort columns
187 const auto it = std::find_if(m_columnNames.cbegin(), m_columnNames.cend(),
188 [&](const auto &name) { return !m_sortColumns.contains(name); });
189 if (it != m_columnNames.cend()) {
190 columnName->setItemText(-1, *it);
191 }
192 // cache the column name
193 m_sortColumns << columnName->currentText();
194 // set the Add/Remove buttons into a correct state
195 if (m_sortColumns.size() == m_columnNames.size()) {
196 m_form.btnAddColumn->setEnabled(false);
197 }
198 m_form.btnRemoveColumn->setEnabled(true);
199}
200
205 // don't try to figure out which column changed - just reset all cached names
206 auto n = m_sortColumns.size();
207 for (int i = 0; i < n; ++i) {
208 auto item = m_form.columnsLayout->itemAtPosition(i, 1);
209 if (!item || !item->widget() || !dynamic_cast<QComboBox *>(item->widget())) {
210 throw std::logic_error("Logic error in SortTableWorkspaceDialog: internal inconsistency.");
211 }
212
213 auto name = dynamic_cast<QComboBox *>(item->widget())->currentText();
214 m_sortColumns[i] = name;
215 }
216}
217
222 assert(m_columnNames.size() > 1);
223 // remove the last column
224 m_sortColumns.removeLast();
225 int row = static_cast<int>(m_sortColumns.size());
226 for (int col = 0; col < 3; ++col) {
227 auto item = m_form.columnsLayout->itemAtPosition(row, col);
228 if (item) {
229 auto index = m_form.columnsLayout->indexOf(item->widget());
230 m_form.columnsLayout->takeAt(index);
231 item->widget()->deleteLater();
232 }
233 }
234 // leave the Add/Remove buttons in a correct state
235 if (m_sortColumns.size() == 1) {
236 m_form.btnRemoveColumn->setEnabled(false);
237 m_form.lblColumnName->setText("Column");
238 }
239 m_form.btnAddColumn->setEnabled(true);
240}
241
242} // 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