-
Notifications
You must be signed in to change notification settings - Fork 122
/
MantidWSIndexDialog.cpp
806 lines (680 loc) · 19.8 KB
/
MantidWSIndexDialog.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//----------------------------------
// Includes
//----------------------------------
#include "MantidWSIndexDialog.h"
#include "MantidUI.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/SpectraDetectorTypes.h"
#include <stdlib.h>
#include <QRegExp>
#include <QtAlgorithms>
#include <boost/lexical_cast.hpp>
#include <exception>
#include <QPalette>
//----------------------------------
// MantidWSIndexDialog public methods
//----------------------------------
/**
* Construct an object of this type
* @param mui :: The MantidUI area
* @param flags :: Window flags that are passed the the QDialog constructor
* @param wsNames :: the names of the workspaces to be plotted
*/
MantidWSIndexDialog::MantidWSIndexDialog(MantidUI* mui, Qt::WFlags flags, QList<QString> wsNames, const bool showWaterfallOption)
: QDialog(mui->appWindow(), flags),
m_mantidUI(mui),
m_spectra(false),
m_waterfall(showWaterfallOption),
m_wsNames(wsNames),
m_wsIndexIntervals(),
m_spectraIdIntervals(),
m_wsIndexChoice(),
m_spectraIdChoice()
{
checkForSpectraAxes();
// Generate the intervals allowed to be plotted by the user.
generateWsIndexIntervals();
if(m_spectra)
{
generateSpectraIdIntervals();
}
// Set up UI.
init();
}
MantidWSIndexDialog::UserInput MantidWSIndexDialog::getSelections() const
{
UserInput options;
options.plots = getPlots();
options.waterfall = waterfallPlotRequested();
return options;
}
QMultiMap<QString,std::set<int> > MantidWSIndexDialog::getPlots() const
{
// Map of workspace names to set of indices to be plotted.
QMultiMap<QString,std::set<int> > plots;
// If the user typed in the wsField ...
if(m_wsIndexChoice.getList().size() > 0)
{
for(int i = 0; i < m_wsNames.size(); i++)
{
std::set<int> intSet = m_wsIndexChoice.getIntSet();
plots.insert(m_wsNames[i],intSet);
}
}
// Else if the user typed in the spectraField ...
else if(m_spectraIdChoice.getList().size() > 0)
{
for(int i = 0; i < m_wsNames.size(); i++)
{
// Convert the spectra choices of the user into workspace indices for us to use.
Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve(m_wsNames[i].toStdString()));
if ( NULL == ws ) continue;
const Mantid::spec2index_map spec2index = ws->getSpectrumToWorkspaceIndexMap();
std::set<int> origSet = m_spectraIdChoice.getIntSet();
std::set<int>::iterator it = origSet.begin();
std::set<int> convertedSet;
for( ; it != origSet.end(); ++it)
{
int origInt = (*it);
int convertedInt = static_cast<int>(spec2index.find(origInt)->second);
convertedSet.insert(convertedInt);
}
plots.insert(m_wsNames[i],convertedSet);
}
}
return plots;
}
bool MantidWSIndexDialog::waterfallPlotRequested() const
{
return m_waterfallOpt->isChecked();
}
//----------------------------------
// MantidWSIndexDialog private slots
//----------------------------------
void MantidWSIndexDialog::plot()
{
int npos = 0;
QString wsText = m_wsField->lineEdit()->text();
QString spectraTest = m_spectraField->lineEdit()->text();
QValidator::State wsState = m_wsField->lineEdit()->validator()->validate(wsText, npos);
QValidator::State spectraState = m_spectraField->lineEdit()->validator()->validate(spectraTest, npos);
// If the user typed in the wsField ...
if(wsState == QValidator::Acceptable)
{
m_wsIndexChoice.addIntervals(m_wsField->lineEdit()->text());
accept();
}
// Else if the user typed in the spectraField ...
else if(spectraState == QValidator::Acceptable)
{
m_spectraIdChoice.addIntervals(m_spectraField->lineEdit()->text());
accept();
}else{
QString error_message("Invalid input. It is not in the range available");
if (!wsText.isEmpty())
m_wsField->setError(error_message);
if (!spectraTest.isEmpty())
m_spectraField->setError(error_message);
}
}
void MantidWSIndexDialog::plotAll()
{
m_wsIndexChoice = m_wsIndexIntervals;
accept();
}
void MantidWSIndexDialog::editedWsField()
{
if(usingSpectraIDs()) {
m_spectraField->lineEdit()->clear();
m_spectraField->setError("");
}
}
void MantidWSIndexDialog::editedSpectraField()
{
m_wsField->lineEdit()->clear();
m_wsField->setError("");
}
//----------------------------------
// MantidWSIndexDialog private methods
//----------------------------------
void MantidWSIndexDialog::init()
{
m_outer = new QVBoxLayout;
setWindowTitle(tr("MantidPlot"));
initSpectraBox();
initWorkspaceBox();
initOptionsBoxes();
initButtons();
setLayout(m_outer);
}
void MantidWSIndexDialog::initWorkspaceBox()
{
m_wsBox = new QVBoxLayout;
m_wsMessage = new QLabel(tr("Enter Workspace Indices: " + m_wsIndexIntervals.toQString()));
m_wsField = new QLineEditWithErrorMark();
m_wsField->lineEdit()->setValidator(new IntervalListValidator(this, m_wsIndexIntervals));
m_wsBox->add(m_wsMessage);
m_wsBox->add(m_wsField);
m_outer->addItem(m_wsBox);
connect(m_wsField->lineEdit(), SIGNAL(textEdited(const QString &)), this, SLOT(editedWsField()));
}
void MantidWSIndexDialog::initSpectraBox()
{
m_spectraBox = new QVBoxLayout;
m_spectraMessage = new QLabel(tr("Enter Spectra IDs: " + m_spectraIdIntervals.toQString()));
m_spectraField = new QLineEditWithErrorMark();
m_orMessage = new QLabel(tr("<br>Or"));
m_spectraField->lineEdit()->setValidator(new IntervalListValidator(this, m_spectraIdIntervals));
m_spectraBox->add(m_spectraMessage);
m_spectraBox->add(m_spectraField);
m_spectraBox->add(m_orMessage);
if( usingSpectraIDs() )
m_outer->addItem(m_spectraBox);
connect(m_spectraField->lineEdit(), SIGNAL(textEdited(const QString &)), this, SLOT(editedSpectraField()));
}
void MantidWSIndexDialog::initOptionsBoxes()
{
m_optionsBox = new QHBoxLayout;
m_waterfallOpt = new QCheckBox("Waterfall Plot");
if(m_waterfall)
m_optionsBox->add(m_waterfallOpt);
else
m_waterfallOpt->setChecked(true);
m_outer->addItem(m_optionsBox);
}
void MantidWSIndexDialog::initButtons()
{
m_buttonBox = new QHBoxLayout;
m_okButton = new QPushButton("OK");
m_cancelButton = new QPushButton("Cancel");
m_plotAllButton = new QPushButton("Plot All");
m_buttonBox->addWidget(m_okButton);
m_buttonBox->addWidget(m_cancelButton);
m_buttonBox->addWidget(m_plotAllButton);
m_outer->addItem(m_buttonBox);
connect(m_okButton, SIGNAL(clicked()), this, SLOT(plot()));
connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(m_plotAllButton, SIGNAL(clicked()), this, SLOT(plotAll()));
}
void MantidWSIndexDialog::checkForSpectraAxes()
{
// Check to see if *all* workspaces have a spectrum axis.
// If even one does not have a spectra axis, then we wont
// ask the user to enter spectra IDs - only workspace indices.
QList<QString>::const_iterator it = m_wsNames.constBegin();
m_spectra = true;
for ( ; it != m_wsNames.constEnd(); ++it )
{
Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve((*it).toStdString()));
if ( NULL == ws ) continue;
bool hasSpectra = false;
for(int i = 0; i < ws->axes(); i++)
{
if(ws->getAxis(i)->isSpectra())
hasSpectra = true;
}
if(hasSpectra == false)
{
m_spectra = false;
break;
}
}
}
void MantidWSIndexDialog::generateWsIndexIntervals()
{
// Get the available interval for each of the workspaces, and then
// present the user with interval which is the INTERSECTION of each of
// those intervals.
QList<QString>::const_iterator it = m_wsNames.constBegin();
// Cycle through the workspaces ...
for ( ; it != m_wsNames.constEnd(); ++it )
{
Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve((*it).toStdString()));
if ( NULL == ws ) continue;
const int endWs = static_cast<int>(ws->getNumberHistograms() - 1);//= static_cast<int> (end->first);
Interval interval(0,endWs);
// If no interval has been added yet, just add it ...
if(it == m_wsNames.constBegin())
m_wsIndexIntervals.addInterval(interval);
// ... else set the list as the intersection of what's already there
// and what has just been added.
else
m_wsIndexIntervals.setIntervalList(IntervalList::intersect(m_wsIndexIntervals,interval));
}
}
void MantidWSIndexDialog::generateSpectraIdIntervals()
{
bool firstWs = true;
foreach( const QString wsName, m_wsNames )
{
Mantid::API::MatrixWorkspace_const_sptr ws = boost::dynamic_pointer_cast<const Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve(wsName.toStdString()));
if( !ws ) continue; // Belt and braces.
const Mantid::spec2index_map spec2index = ws->getSpectrumToWorkspaceIndexMap();
IntervalList spectraIntervalList;
for( auto pair = spec2index.begin(); pair != spec2index.end(); ++pair )
{
spectraIntervalList.addInterval(static_cast<int>(pair->first));
}
if( firstWs )
{
m_spectraIdIntervals = spectraIntervalList;
firstWs = false;
}
else
{
m_spectraIdIntervals.setIntervalList(IntervalList::intersect(m_spectraIdIntervals, spectraIntervalList));
}
}
}
bool MantidWSIndexDialog::usingSpectraIDs() const
{
return m_spectra && m_spectraIdIntervals.getList().size() > 0;
}
//----------------------------------
// Interval public methods
//----------------------------------
Interval::Interval(int single)
{
init(single, single);
}
Interval::Interval(int start, int end)
{
init(start, end);
}
Interval::Interval(QString intervalString)
{
// Check to see if string is of the correct format, and then parse.
// An interval can either be "n" or "n-m" where n and m are integers
const QString patternSingle("^\\d+$"); // E.g. "2" or "712"
const QString patternRange("^\\d+-\\d+$"); // E.g. "2-4" or "214-200"
const QRegExp regExpSingle(patternSingle);
const QRegExp regExpRange(patternRange);
if(regExpSingle.exactMatch(intervalString))
{
int single = intervalString.toInt();
init(single, single);
}
else if(regExpRange.exactMatch(intervalString))
{
QStringList range = intervalString.split("-");
int start = range[0].toInt();
int end = range[1].toInt();
init(start, end);
}
else
{
throw std::exception();
}
}
Interval::Interval(const Interval& copy)
{
init(copy.m_start, copy.m_end);
}
bool Interval::merge(const Interval& other)
{
// If cant merge, return false
if(!canMerge(other))
return false;
// Else, merge - e.g "2" into "3-5" to create "2-5":
if(other.start() < m_start)
m_start = other.start();
if(other.end() > m_end)
m_end = other.end();
return true;
}
bool Interval::canMerge(const Interval& other) const
{
if(other.start() > m_end + 1 || other.end() + 1 < m_start)
return false;
else
return true;
}
int Interval::start() const
{
return m_start;
}
int Interval::end() const
{
return m_end;
}
// Note that the length of an interval with only one number is 1.
// Ergo, "length" is defined as (1 + (end - start))
int Interval::length() const
{
return 1 + m_end - m_start;
}
std::set<int> Interval::getIntSet() const
{
std::set<int> intSet;
for(int i = m_start; i <= m_end; i++)
{
intSet.insert(i);
}
return intSet;
}
bool Interval::contains(const Interval& other) const
{
if(other.m_start >= m_start && other.m_end <= m_end)
return true;
return false;
}
std::string Interval::toStdString() const
{
std::string output;
if(m_start == m_end)
{
output += boost::lexical_cast<std::string>(m_start);
}
else
{
output += boost::lexical_cast<std::string>(m_start) + "-";
output += boost::lexical_cast<std::string>(m_end);
}
return output;
}
QString Interval::toQString() const
{
QString output;
if(m_start == m_end)
{
output.append(QString("%1").arg(m_start));
}
else
{
output.append(QString("%1").arg(m_start));
output += "-";
output.append(QString("%1").arg(m_end));
}
return output;
}
//----------------------------------
// Interval private methods
//----------------------------------
void Interval::init(int start, int end)
{
if(start <= end)
{
m_start = start;
m_end = end;
}
// Here we cater for the case where a user sets start to be at say 4 but
// end at 2. We redefine the interval to be "2-4".
else
{
m_start = end;
m_end = start;
}
}
//----------------------------------
// IntervalList public methods
//----------------------------------
IntervalList::IntervalList(void)
{
}
IntervalList::IntervalList(QString intervals)
{
addIntervals(intervals);
}
IntervalList::IntervalList(Interval interval)
{
m_list.append(interval);
}
IntervalList::IntervalList(const IntervalList& copy)
{
m_list = copy.m_list;
}
const QList<Interval>& IntervalList::getList() const
{
return m_list;
}
int IntervalList::totalIntervalLength() const
{
// Total up all the individual Interval lengths in the list:
int total = 0;
for(int i = 0; i < m_list.size(); i++)
{
total += (m_list.at(i).length());
}
return total;
}
std::string IntervalList::toStdString(int numOfIntervals) const
{
std::string output;
if(m_list.size() <= numOfIntervals)
{
for(int i = 0; i < m_list.size(); i++)
{
if(i > 0) output += ", ";
output += m_list.at(i).toStdString();
}
}
// If the number of Intervals is over the numOfIntervals, then
// we only print out the first (numOfIntervals - 1) Intervals,
// followed by a ", ...", followed by the final Interval.
// E.g. "0,2,4,6,8,10,12,14,16,18" becomes "0,2,4,6,8,...,18"
else
{
for(int i = 0; i < numOfIntervals - 1; i++)
{
if(i > 0) output += ", ";
output += m_list[i].toStdString();
}
output += ", ..., ";
output += m_list[m_list.size() - 1].toStdString();
}
return output;
}
QString IntervalList::toQString(int numOfIntervals) const
{
QString output(toStdString(numOfIntervals).c_str());
return output;
}
void IntervalList::addInterval(int single)
{
Interval interval(single, single);
IntervalList::addInterval(interval);
}
// Note: this is considerably more efficient in the case where intervals are added
// smallest first.
void IntervalList::addInterval(Interval interval)
{
if(m_list.size() == 0)
{
m_list.append(interval);
return;
}
bool added = false;
QList<int> deleteList;
for(int i = m_list.size() - 1; i >= 0 ; i--)
{
// if new interval is completely higher than this interval
if(interval.start() > m_list.at(i).end() + 1)
{
// add new interval as a seperate interval
m_list.append(interval);
added = true;
break;
}
// else if the new interval can be merged with this interval
else if(m_list.at(i).canMerge(interval))
{
// for each interval in the list before and including this one
for(int j = i; j >= 0; j--)
{
// if it can be merged into the new interval
if(m_list.at(j).canMerge(interval))
{
// do it
interval.merge(m_list.at(j));
// then add its index to the list of intervals to be deleted
deleteList.append(j);
}
// else if it cant, there is no need to continue checking whether
// any other intervals can alse be merged
else
{
break;
}
}
// insert the new large interval in the correct place
m_list.insert(i+1, interval);
added = true;
break;
}
}
// if deleteList has any elements, delete intervals at those indices
if(deleteList.size() > 0)
{
qSort(deleteList);
for(int i = deleteList.size() - 1; i >=0 ; i--)
{
m_list.removeAt(deleteList[i]);
}
}
// if still not assigned, add to the beginning
if(!added)
{
m_list.insert(0, interval);
}
}
void IntervalList::addInterval(int start, int end)
{
Interval interval(start, end);
IntervalList::addInterval(interval);
}
void IntervalList::addIntervals(QString intervals)
{
// Remove whitespace
intervals = intervals.simplified();
intervals = intervals.replace(" ", "");
// Split the string, and add the intervals to the list.
QStringList intervalList = intervals.split(",");
for(int i = 0; i < intervalList.size(); i++)
{
Interval interval(intervalList[i]);
addInterval(interval);
}
}
void IntervalList::addIntervalList(const IntervalList& intervals)
{
const QList<Interval> list = intervals.getList();
QList<Interval>::const_iterator it = list.constBegin();
for( ; it != list.constEnd(); ++it)
{
addInterval((*it));
}
}
void IntervalList::setIntervalList(const IntervalList& intervals)
{
m_list = QList<Interval>(intervals.getList());
}
std::set<int> IntervalList::getIntSet() const
{
std::set<int> intSet;
for(int i = 0; i < m_list.size(); i++)
{
std::set<int> intervalSet = m_list.at(i).getIntSet();
intSet.insert(intervalSet.begin(), intervalSet.end());
}
return intSet;
}
bool IntervalList::contains(const Interval& other) const
{
for(int i = 0; i < m_list.size(); i++)
{
if(m_list.at(i).contains(other))
return true;
}
return false;
}
bool IntervalList::contains(const IntervalList& other) const
{
for(int i = 0; i < other.m_list.size(); i++)
{
if(!IntervalList::contains(other.m_list.at(i)))
return false;
}
return true;
}
bool IntervalList::isParsable(const QString &input, const IntervalList &container)
{
try
{
const IntervalList test(input);
return container.contains(test);
}
catch (std::exception&)
{
return false;
}
}
bool IntervalList::isParsable(const QString &input)
{
try
{
IntervalList interval(input);
return true;
}
catch (std::exception&)
{
return false;
}
}
IntervalList IntervalList::intersect(const IntervalList& aList, const Interval& bInterval)
{
const IntervalList bList(bInterval);
return IntervalList::intersect(aList, bList);
}
IntervalList IntervalList::intersect(const IntervalList& a, const IntervalList& b)
{
IntervalList output;
const std::set<int> aInts = a.getIntSet();
const std::set<int> bInts = b.getIntSet();
for( auto aInt = aInts.begin(); aInt != aInts.end(); ++aInt )
{
const bool inIntervalListB = bInts.find(*aInt) != bInts.end();
if( inIntervalListB )
output.addInterval(*aInt);
}
return output;
}
//----------------------------------
// IntervalListValidator public methods
//----------------------------------
IntervalListValidator::IntervalListValidator(QObject * parent, const IntervalList &intervalList) : QValidator(parent), m_intervalList(intervalList)
{
}
QValidator::State IntervalListValidator::validate(QString &input, int &pos) const
{
UNUSED_ARG(pos)
if(IntervalList::isParsable(input, m_intervalList))
return QValidator::Acceptable;
const QString pattern("^(\\d|-|,)*$");
const QRegExp regExp(pattern);
if(regExp.exactMatch(input))
return QValidator::Intermediate;
return QValidator::Invalid;
}
//////////////////////////////////////
// QLineEditWithErrorMark
//////////////////////////////////////
MantidWSIndexDialog::QLineEditWithErrorMark::QLineEditWithErrorMark(QWidget * parent): QWidget(parent){
QGridLayout * layout = new QGridLayout();
_lineEdit = new QLineEdit();
m_validLbl = new QLabel("*"); // make it red
QPalette pal = m_validLbl->palette();
pal.setColor(QPalette::WindowText, Qt::darkRed);
m_validLbl->setPalette(pal);
layout->addWidget(_lineEdit,0,0);
layout->addWidget(m_validLbl,0,1);
m_validLbl->setVisible(false);
setLayout(layout);
}
void MantidWSIndexDialog::QLineEditWithErrorMark::setError(QString error){
if(error.isEmpty()){
m_validLbl->setVisible(false);
}else{
m_validLbl->setVisible(true);
m_validLbl->setToolTip(error.trimmed());
}
}