-
Notifications
You must be signed in to change notification settings - Fork 122
/
SlicingAlgorithmDialog.cpp
434 lines (372 loc) · 14.9 KB
/
SlicingAlgorithmDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "MantidQtCustomDialogs/SlicingAlgorithmDialog.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include <QIntValidator>
#include <QFileDialog>
#include <QDir>
#include <QSettings>
using namespace Mantid::API;
using namespace Mantid::Geometry;
namespace MantidQt
{
namespace CustomDialogs
{
DECLARE_DIALOG(SliceMDDialog);
DECLARE_DIALOG(BinMDDialog);
/**
Constructor
@param parent : parent widget
*/
SlicingAlgorithmDialog::SlicingAlgorithmDialog(QWidget* parent) : AlgorithmDialog(parent)
{
}
/// Set up the dialog layout
void SlicingAlgorithmDialog::initLayout()
{
ui.setupUi(this);
this->setWindowTitle(m_algName);
loadSettings();
//Tie core widgets to core properties.
tie(ui.workspace_selector, "InputWorkspace", ui.input_layout);
tie(ui.ck_axis_aligned, "AxisAligned");
tie(ui.txt_output, "OutputWorkspace", ui.output_layout);
tie(ui.txt_output_extents, "OutputExtents", ui.output_extents_layout);
tie(ui.txt_output_bins, "OutputBins", ui.output_bins_layout);
tie(ui.ck_normalisebasisvectors, "NormalizeBasisVectors");
tie(ui.ck_force_orthogonal, "ForceOrthogonal");
tie(ui.txt_translation, "Translation");
ui.txt_memory->setValidator(new QIntValidator(this));
ui.txt_resursion_depth->setValidator(new QIntValidator(this));
connect(ui.workspace_selector,SIGNAL(activated(int)),this ,SLOT(onWorkspaceChanged()));
connect(ui.controls, SIGNAL(accepted()), this, SLOT(accept()));
connect(ui.controls, SIGNAL(rejected()), this, SLOT(reject()));
connect(ui.ck_axis_aligned, SIGNAL(clicked(bool)), this, SLOT(onAxisAlignedChanged(bool)));
connect(ui.ck_max_from_input, SIGNAL(clicked(bool)), this, SLOT(onMaxFromInput(bool)));
connect(ui.ck_calculate, SIGNAL(clicked(bool)), this, SLOT(onCalculateChanged(bool)));
connect(ui.btn_browse, SIGNAL(clicked()), this, SLOT(onBrowse()));
connect(ui.btn_help, SIGNAL(clicked()), this, SLOT(helpClicked()));
connect(ui.btn_calculate, SIGNAL(clicked()), this, SLOT(onRebuildDimensions()));
//Configure workspace selector
ui.workspace_selector->setValidatingAlgorithm(m_algName);
connect(ui.workspace_selector, SIGNAL(clicked()), this, SLOT(createMDWorkspaceClicked()));
ui.workspace_selector->clear();
typedef std::set<std::string> WorkspaceNames;
WorkspaceNames names = AnalysisDataService::Instance().getObjectNames();
WorkspaceNames::iterator it = names.begin();
while(it != names.end())
{
IMDEventWorkspace_sptr ws = boost::dynamic_pointer_cast<IMDEventWorkspace>(AnalysisDataService::Instance().retrieve(*it));
if(ws)
{
ui.workspace_selector->addItem((*it).c_str());
}
++it;
}
QString lastUsedInputWorkspaceName = getHistoricalInputWorkspaceName();
int index = ui.workspace_selector->findText(lastUsedInputWorkspaceName);
if( index >= 0 )
{
ui.workspace_selector->setCurrentIndex(index);
}
//Derived algorithms may use this to apply any additional ties.
customiseInitLayout();
//Dynamically create the input dimensions.
buildDimensionInputs(ui.ck_calculate->isChecked());
}
/// Destructor
SlicingAlgorithmDialog::~SlicingAlgorithmDialog()
{
saveSettings();
}
/**
Create a formatted string for the dimension input based on an existing dimension.
@param dim : dimension to format to string.
*/
QString formattedAlignedDimensionInput(Mantid::Geometry::IMDDimension_const_sptr dim)
{
QString min, max, nbins, result;
QString name(dim->getName().c_str());
min.setNum(dim->getMinimum());
max.setNum(dim->getMaximum());
nbins.setNum(dim->getNBins());
result.append(name).append(",").append(min).append(",").append(max).append(",").append(nbins);
return result;
}
/**
Create a formatted string for the dimension input based on an existing dimension.
As the input it is expected the dimension to format the string.
It is expected that inherited classes will be able to implement this method, but
this class can not obviously define how the basis vectors could be automatically formed,
so, it retuns always an empty string.
@return : empty string.
*/
QString formatNonAlignedDimensionInput(Mantid::Geometry::IMDDimension_const_sptr)
{
//Deliberately return an empty string here, because it's not obveious how the basis vectors could be automatically formed.
return QString("");
}
/**
Clears the layout of any qwidget
@param layout : layout to clean
*/
void SlicingAlgorithmDialog::cleanLayoutOfDimensions(QLayout* layout)
{
int itemCount = layout->count();
for(int i = 0; i < itemCount; ++i)
{
QLayoutItem* pLayoutItem = layout->itemAt(i);
QWidget* pWidget = pLayoutItem->widget();
if (pWidget != NULL)
{
//The label text contains the property name.
QLabel* propertyLabel = dynamic_cast<QLabel*>(pWidget->layout()->itemAt(0)->widget());
untie(propertyLabel->text());
pWidget->setHidden(true);
this->layout()->removeItem(pLayoutItem);
}
}
}
/**
Find existing dimension widgets and get rid of them from the layout.
*/
void SlicingAlgorithmDialog::clearExistingDimensions()
{
QLayout* alignedLayout = this->ui.axis_aligned_layout->layout();
QLayout* nonAlignedLayout = this->ui.non_axis_aligned_layout->layout();
cleanLayoutOfDimensions(alignedLayout);
cleanLayoutOfDimensions(nonAlignedLayout);
}
/**
Determine if the inputs should be in an axis aligned form.
@return : True if axis aligned
*/
bool SlicingAlgorithmDialog::doAxisAligned() const
{
return ui.ck_axis_aligned->isChecked();
}
/**
Gets the provided input workspace name
@return name of the input workspace
*/
QString SlicingAlgorithmDialog::getCurrentInputWorkspaceName() const
{
return ui.workspace_selector->currentText();
}
/**
Gets the provided output workspace name
@return name of the output workspace
*/
QString SlicingAlgorithmDialog::getCurrentOutputWorkspaceName() const
{
return ui.txt_output->text();
}
/**
Getter for the historical input workspace name.
@return old input workspace name.
*/
QString SlicingAlgorithmDialog::getHistoricalInputWorkspaceName() const
{
return MantidQt::API::AlgorithmInputHistory::Instance().previousInput(m_algName, "InputWorkspace");
}
/**
Determine if properties relating to the dimension history have changed.
@return True if it has changed.
*/
SlicingAlgorithmDialog::HistoryChanged SlicingAlgorithmDialog::hasDimensionHistoryChanged() const
{
SlicingAlgorithmDialog::HistoryChanged result = HasNotChanged;
const QString& currentWorkspaceName = getCurrentInputWorkspaceName();
const QString previousInputWorkspaceName = MantidQt::API::AlgorithmInputHistory::Instance().previousInput(m_algName, "InputWorkspace");
if(currentWorkspaceName.isEmpty())
{
result = HasChanged; //Force a rebuild because the dialog cant find any eligable input workspaces. That's why there is an empty entry.
}
else if(AnalysisDataService::Instance().doesExist(previousInputWorkspaceName.toStdString()))
{
const auto oldWS = AnalysisDataService::Instance().retrieveWS<IMDWorkspace>(previousInputWorkspaceName.toStdString());
const auto newWS = AnalysisDataService::Instance().retrieveWS<IMDWorkspace>(currentWorkspaceName.toStdString());
if(oldWS->getNumDims() != newWS->getNumDims())
{
result = HasChanged;
}
}
return result;
}
/*
Decide and command the type of dimension inputs to provide.
@bForceForget : Force the use of inputworkspace dimensions when configuring the dialog.
*/
void SlicingAlgorithmDialog::buildDimensionInputs(const bool bForceForget)
{
clearExistingDimensions();
const bool axisAligned = doAxisAligned();
ui.non_axis_aligned_layout->setEnabled(!axisAligned);
HistoryChanged changedStatus = hasDimensionHistoryChanged();
History history;
if(changedStatus == HasChanged || bForceForget)
{
history = Forget;
}
else
{
history = Remember;
}
if(axisAligned)
{
buildDimensionInputs("AlignedDim", this->ui.axis_aligned_layout->layout(), formattedAlignedDimensionInput, history);
}
else
{
buildDimensionInputs("BasisVector", this->ui.non_axis_aligned_layout->layout(), formatNonAlignedDimensionInput, history);
}
}
/**
Build dimensions from the currently selected input workspace. Also fills
the inputs with default values.
@param propertyPrefix: The prefix for the property in the algorithm, i.e. AxisAligned.
@param owningLayout: The layout that will take ownership of the widgets once generated.
@param format: function pointer to the formatting function
@param history : Whether to remember of forget property history.
*/
void SlicingAlgorithmDialog::buildDimensionInputs(const QString& propertyPrefix,
QLayout* owningLayout, QString (*format)(IMDDimension_const_sptr), History history)
{
const QString& txt = getCurrentInputWorkspaceName();
if (!txt.isEmpty())
{
IMDWorkspace_sptr ws = boost::dynamic_pointer_cast<IMDWorkspace>(
AnalysisDataService::Instance().retrieve(txt.toStdString()));
size_t nDimensions = ws->getNumDims();
for (size_t index = 0; index < nDimensions; ++index)
{
Mantid::Geometry::IMDDimension_const_sptr dim = ws->getDimension(index);
// Configure the label
const QString propertyName = propertyPrefix.copy().append(QString().number(index));
QLabel* dimensionLabel = new QLabel(propertyName);
// Configure the default input.
const QString dimensionInfo = format(dim);
QLineEdit* txtDimension = new QLineEdit(dimensionInfo);
// Create a widget to contain the dimension components.
QHBoxLayout* layout = new QHBoxLayout;
QWidget* w = new QWidget;
w->setLayout(layout);
tie(txtDimension, propertyName, layout, (history == Remember));
// Add components to the layout.
layout->addWidget(dimensionLabel);
layout->addWidget(txtDimension);
owningLayout->addWidget(w);
}
}
}
/** Save settings for next time. */
void SlicingAlgorithmDialog::saveSettings()
{
QSettings settings;
settings.beginGroup("Mantid/SlicingAlgorithm");
settings.setValue("AlwaysCalculateExtents", (ui.ck_calculate->isChecked() ? 1 : 0));
settings.endGroup();
}
/**
* Load Settings
*/
void SlicingAlgorithmDialog::loadSettings()
{
QSettings settings;
settings.beginGroup("Mantid/SlicingAlgorithm");
const bool alwaysCalculateExtents = settings.value("AlwaysCalculateExtents", 1).toInt();
ui.ck_calculate->setChecked(alwaysCalculateExtents);
const QString alignedDimensions = settings.value("AlignedDimensions", "").toString();
const QString nonAlignedDimensions = settings.value("NonAlignedDimensions", "").toString();
settings.endGroup();
}
/// Event handler for the workspace changed event.
void SlicingAlgorithmDialog::onWorkspaceChanged()
{
buildDimensionInputs(ui.ck_calculate->isChecked());
}
/**
Event handler for the axis changed event.
This event handler allows us to continually dynamically provide inputs depending upon the dimensionality.
*/
void SlicingAlgorithmDialog::onAxisAlignedChanged(bool)
{
buildDimensionInputs(ui.ck_calculate->isChecked());
}
/**
Event handler for changes so that recursion depth for the ouput workspace is either taken
from the input workspace or from an external field.
*/
void SlicingAlgorithmDialog::onMaxFromInput(bool)
{
const bool takeFromInputWorkspace = ui.ck_max_from_input->isChecked();
ui.txt_resursion_depth->setEnabled(!takeFromInputWorkspace);
ui.lbl_resursion_depth->setEnabled(!takeFromInputWorkspace);
}
/**
Event handler for the on-forced dimension rebuild event.
*/
void SlicingAlgorithmDialog::onRebuildDimensions()
{
buildDimensionInputs(true);
}
void SlicingAlgorithmDialog::onCalculateChanged(bool checked)
{
if(checked)
buildDimensionInputs(true);
}
/**
Handler for the onbrowse event.
*/
void SlicingAlgorithmDialog::onBrowse()
{
QFileDialog dialog;
dialog.setDirectory(QDir::homePath());
dialog.setNameFilter("Nexus files (*.nxs)");
if (dialog.exec())
{
ui.txt_filename->setText(dialog.selectedFile());
}
}
/*
Perform tasks that are almost identical for derived types except for visibility switch.
@isSliceMD. Is the calling method from SliceMD.
*/
void SlicingAlgorithmDialog::commonSliceMDSetup(const bool isSliceMD)
{
ui.file_backend_layout->setVisible(isSliceMD);
ui.ck_max_from_input->setVisible(isSliceMD);
ui.lbl_resursion_depth->setVisible(isSliceMD);
ui.txt_resursion_depth->setVisible(isSliceMD);
ui.ck_parallel->setVisible(!isSliceMD);
}
/*---------------------------------------------------------------------------------------------
SliceMDDialog Methods
---------------------------------------------------------------------------------------------*/
void SliceMDDialog::customiseInitLayout()
{
commonSliceMDSetup(true);
//Tie the widgets to properties
tie(ui.ck_max_from_input, "TakeMaxRecursionDepthFromInput");
tie(ui.txt_resursion_depth, "MaxRecursionDepth");
tie(ui.txt_filename, "OutputFilename");
tie(ui.txt_memory, "Memory");
}
/*---------------------------------------------------------------------------------------------
End SliceMDDialog Methods
---------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------
BinMDDialog Methods
---------------------------------------------------------------------------------------------*/
void BinMDDialog::customiseInitLayout()
{
//Disable the stuff that doesn't relate to BinMD
commonSliceMDSetup(false);
tie(ui.ck_parallel, "Parallel");
}
/*---------------------------------------------------------------------------------------------
End BinMDDialog Methods
---------------------------------------------------------------------------------------------*/
}
}