-
Notifications
You must be signed in to change notification settings - Fork 122
/
SliceViewer.cpp
2315 lines (2020 loc) · 79.3 KB
/
SliceViewer.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidGeometry/MDGeometry/IMDDimension.h"
#include "MantidGeometry/MDGeometry/MDBoxImplicitFunction.h"
#include "MantidGeometry/MDGeometry/MDHistoDimension.h"
#include "MantidGeometry/MDGeometry/MDTypes.h"
#include "MantidKernel/DataService.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/VMD.h"
#include "MantidQtSliceViewer/CustomTools.h"
#include "MantidQtSliceViewer/DimensionSliceWidget.h"
#include "MantidQtSliceViewer/LineOverlay.h"
#include "MantidQtSliceViewer/QwtRasterDataMD.h"
#include "MantidQtSliceViewer/SliceViewer.h"
#include "MantidQtSliceViewer/SnapToGridDialog.h"
#include "MantidQtSliceViewer/XYLimitsDialog.h"
#include "MantidQtSliceViewer/ConcretePeaksPresenter.h"
#include "MantidQtSliceViewer/CompositePeaksPresenter.h"
#include "MantidQtSliceViewer/ProxyCompositePeaksPresenter.h"
#include "MantidQtSliceViewer/PeakOverlaySphereFactory.h"
#include "MantidQtSliceViewer/PeakOverlayCrossFactory.h"
#include "MantidQtSliceViewer/PeakTransformHKL.h"
#include "MantidQtSliceViewer/PeakTransformQSample.h"
#include "MantidQtSliceViewer/PeakTransformQLab.h"
#include "MantidQtSliceViewer/FirstExperimentInfoQuery.h"
#include "MantidQtSliceViewer/PeakBoundingBox.h"
#include "MantidQtMantidWidgets/SelectWorkspacesDialog.h"
#include "qmainwindow.h"
#include "qmenubar.h"
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <limits>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/DOMWriter.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeFilter.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <qfiledialog.h>
#include <qmenu.h>
#include <QtGui/qaction.h>
#include <qwt_color_map.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_picker.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot.h>
#include <qwt_scale_engine.h>
#include <qwt_scale_map.h>
#include <sstream>
#include <vector>
#include <boost/make_shared.hpp>
#include "MantidKernel/V3D.h"
#include "MantidKernel/ReadLock.h"
#include "MantidQtMantidWidgets/SafeQwtPlot.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidQtAPI/AlgorithmRunner.h"
#include "MantidQtAPI/FileDialogHandler.h"
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;
using MantidQt::API::SyncedCheckboxes;
using Poco::XML::DOMParser;
using Poco::XML::Document;
using Poco::XML::Element;
using Poco::XML::Node;
using Poco::XML::NodeList;
using Poco::XML::NodeIterator;
using Poco::XML::NodeFilter;
using MantidQt::API::AlgorithmRunner;
namespace MantidQt
{
namespace SliceViewer
{
//------------------------------------------------------------------------------------
/** Constructor */
SliceViewer::SliceViewer(QWidget *parent)
: QWidget(parent),
m_ws(), m_firstWorkspaceOpen(false),
m_dimensions(), m_data(NULL),
m_X(), m_Y(),
m_dimX(0), m_dimY(1),
m_logColor(false),
m_fastRender(true),
m_rebinMode(false),
m_rebinLocked(true),
m_peaksPresenter(boost::make_shared<CompositePeaksPresenter>(this)),
m_peaksSliderWidget(NULL)
{
ui.setupUi(this);
m_inf = std::numeric_limits<double>::infinity();
// Point m_plot to the plot created in QtDesigner
m_plot = ui.safeQwtPlot;
// Add a spectrograph
m_spect = new QwtPlotSpectrogram();
m_spect->attach(m_plot);
// Set up the ColorBarWidget
m_colorBar = ui.colorBarWidget;
m_colorBar->setViewRange( 0, 10);
m_colorBar->setLog(true);
QObject::connect(m_colorBar, SIGNAL(changedColorRange(double,double,bool)), this, SLOT(colorRangeChanged()));
// ---- Set the color map on the data ------
m_data = new QwtRasterDataMD();
m_spect->setColorMap( m_colorBar->getColorMap() );
m_plot->autoRefresh();
// Make the splitter use the minimum size for the controls and not stretch out
ui.splitter->setStretchFactor(0, 0);
ui.splitter->setStretchFactor(1, 1);
initZoomer();
ui.btnZoom->hide();
// ----------- Toolbar button signals ----------------
QObject::connect(ui.btnResetZoom, SIGNAL(clicked()), this, SLOT(resetZoom()));
QObject::connect(ui.btnClearLine, SIGNAL(clicked()), this, SLOT(clearLine()));
QObject::connect(ui.btnRangeFull, SIGNAL(clicked()), this, SLOT(setColorScaleAutoFull()));
QObject::connect(ui.btnRangeSlice, SIGNAL(clicked()), this, SLOT(setColorScaleAutoSlice()));
QObject::connect(ui.btnRebinRefresh, SIGNAL(clicked()), this, SLOT(rebinParamsChanged()));
QObject::connect(ui.btnAutoRebin, SIGNAL(toggled(bool)), this, SLOT(autoRebin_toggled(bool)));
QObject::connect(ui.btnPeakOverlay, SIGNAL(toggled(bool)), this, SLOT(peakOverlay_toggled(bool)));
// ----------- Other signals ----------------
QObject::connect(m_colorBar, SIGNAL(colorBarDoubleClicked()), this, SLOT(loadColorMapSlot()));
m_algoRunner = new AlgorithmRunner();
QObject::connect(m_algoRunner, SIGNAL(algorithmComplete(bool)), this, SLOT(dynamicRebinComplete(bool)));
initMenus();
loadSettings();
updateDisplay();
// -------- Line Overlay ----------------
m_lineOverlay = new LineOverlay(m_plot, m_plot->canvas());
m_lineOverlay->setShown(false);
m_overlayWSOutline = new LineOverlay(m_plot, m_lineOverlay);
m_overlayWSOutline->setShowHandles(false);
m_overlayWSOutline->setShowLine(false);
m_overlayWSOutline->setShown(false);
// -------- Peak Overlay ----------------
m_peakTransformSelector.registerCandidate(boost::make_shared<PeakTransformHKLFactory>());
m_peakTransformSelector.registerCandidate(boost::make_shared<PeakTransformQSampleFactory>());
m_peakTransformSelector.registerCandidate(boost::make_shared<PeakTransformQLabFactory>());
}
//------------------------------------------------------------------------------------
/// Destructor
SliceViewer::~SliceViewer()
{
delete m_data;
saveSettings();
// Don't delete Qt objects, I think these are auto-deleted
}
//------------------------------------------------------------------------------------
/** Load QSettings from .ini-type files */
void SliceViewer::loadSettings()
{
QSettings settings;
settings.beginGroup("Mantid/SliceViewer");
bool scaleType = (bool)settings.value("LogColorScale", 0 ).toInt();
//Load Colormap. If the file is invalid the default stored colour map is used
m_currentColorMapFile = settings.value("ColormapFile", "").toString();
// Set values from settings
if (!m_currentColorMapFile.isEmpty())
loadColorMap(m_currentColorMapFile);
m_colorBar->setLog(scaleType);
// Last saved image file
m_lastSavedFile = settings.value("LastSavedImagePath", "").toString();
bool transparentZeros = settings.value("TransparentZeros", 1).toInt();
this->setTransparentZeros(transparentZeros);
int norm = settings.value("Normalization", 1).toInt();
Mantid::API::MDNormalization normaliz = static_cast<Mantid::API::MDNormalization>(norm);
this->setNormalization(normaliz);
settings.endGroup();
}
//------------------------------------------------------------------------------------
/** Save settings for next time. */
void SliceViewer::saveSettings()
{
QSettings settings;
settings.beginGroup("Mantid/SliceViewer");
settings.setValue("ColormapFile", m_currentColorMapFile);
settings.setValue("LogColorScale", (int)m_colorBar->getLog() );
settings.setValue("LastSavedImagePath", m_lastSavedFile);
settings.setValue("TransparentZeros", (m_actionTransparentZeros->isChecked() ? 1 : 0));
settings.setValue("Normalization", static_cast<int>(this->getNormalization()));
settings.endGroup();
}
//------------------------------------------------------------------------------------
/** Create the menus */
void SliceViewer::initMenus()
{
// ---------------------- Build the menu bar -------------------------
// Find the top-level parent
QWidget * widget = this;
while (widget && widget->parentWidget()) widget = widget->parentWidget() ;
QMainWindow * parentWindow = dynamic_cast<QMainWindow *>(widget);
QMenuBar * bar;
if (parentWindow)
// Use the QMainWindow menu bar
bar = parentWindow->menuBar();
else
{
// Widget is not in a QMainWindow. Make a menu bar
bar = new QMenuBar(this, "Main Menu Bar");
ui.verticalLayout->insertWidget(0, bar );
}
QAction * action;
// --------------- File Menu ----------------------------------------
m_menuFile = new QMenu("&File", this);
action = new QAction(QPixmap(), "&Close", this);
connect(action, SIGNAL(triggered()), this, SLOT(close()));
m_actionFileClose = action;
m_menuFile->addAction(action);
action = new QAction(QPixmap(), "&Save to image file", this);
action->setShortcut(Qt::Key_S + Qt::ControlModifier);
connect(action, SIGNAL(triggered()), this, SLOT(saveImage()));
m_menuFile->addAction(action);
action = new QAction(QPixmap(), "Copy image to &Clipboard", this);
action->setShortcut(Qt::Key_C + Qt::ControlModifier);
connect(action, SIGNAL(triggered()), this, SLOT(copyImageToClipboard()));
m_menuFile->addAction(action);
// --------------- View Menu ----------------------------------------
m_menuView = new QMenu("&View", this);
action = new QAction(QPixmap(), "&Reset Zoom", this);
connect(action, SIGNAL(triggered()), this, SLOT(resetZoom()));
{ QIcon icon; icon.addFile(QString::fromUtf8(":/SliceViewer/icons/view-fullscreen.png"), QSize(), QIcon::Normal, QIcon::Off); action->setIcon(icon); }
m_menuView->addAction(action);
action = new QAction(QPixmap(), "&Set X/Y View Size", this);
connect(action, SIGNAL(triggered()), this, SLOT(setXYLimitsDialog()));
m_menuView->addAction(action);
action = new QAction(QPixmap(), "Zoom &In", this);
action->setShortcut(Qt::Key_Plus + Qt::ControlModifier);
connect(action, SIGNAL(triggered()), this, SLOT(zoomInSlot()));
m_menuView->addAction(action);
action = new QAction(QPixmap(), "Zoom &Out", this);
action->setShortcut(Qt::Key_Minus + Qt::ControlModifier);
connect(action, SIGNAL(triggered()), this, SLOT(zoomOutSlot()));
m_menuView->addAction(action);
action = new QAction(QPixmap(), "&Fast Rendering Mode", this);
action->setShortcut(Qt::Key_F + Qt::ControlModifier);
action->setCheckable(true);
action->setChecked(true);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setFastRender(bool)));
m_menuView->addAction(action);
m_menuView->addSeparator();
action = new QAction(QPixmap(), "Dynamic R&ebin Mode", this);
m_syncRebinMode = new SyncedCheckboxes(action, ui.btnRebinMode, false);
connect(m_syncRebinMode, SIGNAL(toggled(bool)), this, SLOT(RebinMode_toggled(bool)));
m_menuView->addAction(action);
action = new QAction(QPixmap(), "&Lock Rebinned WS", this);
m_syncRebinLock = new SyncedCheckboxes(action, ui.btnRebinLock, true);
connect(m_syncRebinLock, SIGNAL(toggled(bool)), this, SLOT(RebinLock_toggled(bool)));
m_menuView->addAction(action);
action = new QAction(QPixmap(), "Refresh Rebin", this);
action->setShortcut(Qt::Key_R + Qt::ControlModifier);
action->setEnabled(false);
connect(action, SIGNAL(triggered()), this, SLOT(rebinParamsChanged()));
m_menuView->addAction(action);
m_actionRefreshRebin = action;
action = new QAction(QPixmap(), "Auto Rebin", this);
m_syncAutoRebin = new SyncedCheckboxes(action, ui.btnAutoRebin, false);
connect(action, SIGNAL(toggled(bool)), this, SLOT(autoRebin_toggled(bool)));
m_syncAutoRebin->setEnabled(false); // Cannot auto rebin by default.
m_menuView->addAction(action);
m_menuView->addSeparator();
action = new QAction(QPixmap(), "Peak Overlay", this);
m_syncPeakOverlay = new SyncedCheckboxes(action, ui.btnPeakOverlay, false);
connect(action, SIGNAL(toggled(bool)), this, SLOT(peakOverlay_toggled(bool)));
m_menuView->addAction(action);
m_menuView->addSeparator();
QActionGroup* group = new QActionGroup( this );
action = new QAction(QPixmap(), "No Normalization", this);
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationNone()));
m_actionNormalizeNone = action;
action = new QAction(QPixmap(), "Volume Normalization", this);
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
action->setChecked(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationVolume()));
m_actionNormalizeVolume = action;
action = new QAction(QPixmap(), "Num. Events Normalization", this);
m_menuView->addAction(action);
action->setActionGroup(group);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(changeNormalizationNumEvents()));
m_actionNormalizeNumEvents = action;
// --------------- Color options Menu ----------------------------------------
m_menuColorOptions = new QMenu("&ColorMap", this);
action = new QAction(QPixmap(), "&Load Colormap", this);
connect(action, SIGNAL(triggered()), this, SLOT(loadColorMapSlot()));
m_menuColorOptions->addAction(action);
action = new QAction(QPixmap(), "&Full range", this);
connect(action, SIGNAL(triggered()), this, SLOT(setColorScaleAutoFull()));
{ QIcon icon; icon.addFile(QString::fromUtf8(":/SliceViewer/icons/color-pallette.png"), QSize(), QIcon::Normal, QIcon::Off); action->setIcon(icon); }
m_menuColorOptions->addAction(action);
action = new QAction(QPixmap(), "&Slice range", this);
connect(action, SIGNAL(triggered()), this, SLOT(setColorScaleAutoSlice()));
action->setIconVisibleInMenu(true);
{ QIcon icon; icon.addFile(QString::fromUtf8(":/SliceViewer/icons/color-pallette-part.png"), QSize(), QIcon::Normal, QIcon::Off); action->setIcon(icon); }
m_menuColorOptions->addAction(action);
action = new QAction(QPixmap(), "Transparent &Zeros", this);
action->setCheckable(true);
action->setChecked(true);
m_actionTransparentZeros = action;
connect(action, SIGNAL(toggled(bool)), this, SLOT(setTransparentZeros(bool)));
m_menuColorOptions->addAction(action);
// --------------- Help Menu ----------------------------------------
m_menuHelp = new QMenu("&Help", this);
action = new QAction(QPixmap(), "&Slice Viewer Help (browser)", this);
action->setShortcut(Qt::Key_F1);
connect(action, SIGNAL(triggered()), this, SLOT(helpSliceViewer()));
m_menuHelp->addAction(action);
action = new QAction(QPixmap(), "&Line Viewer Help (browser)", this);
connect(action, SIGNAL(triggered()), this, SLOT(helpLineViewer()));
m_menuHelp->addAction(action);
action = new QAction(QPixmap(), "&Peaks Viewer Help (browser)", this);
connect(action, SIGNAL(triggered()), this, SLOT(helpPeaksViewer()));
m_menuHelp->addAction(action);
// --------------- Line Menu ----------------------------------------
m_menuLine = new QMenu("&Line", this);
// Line mode menu, synced to the button
action = new QAction(QPixmap(), "&Line Mode", this);
action->setShortcut(Qt::Key_L + Qt::ControlModifier);
m_syncLineMode = new SyncedCheckboxes(action, ui.btnDoLine, false);
connect(m_syncLineMode, SIGNAL(toggled(bool)), this, SLOT(LineMode_toggled(bool)));
m_menuLine->addAction(action);
// Snap-to-grid, synced to the button
action = new QAction(QPixmap(), "&Snap to Grid", this);
m_syncSnapToGrid = new SyncedCheckboxes(action, ui.btnSnapToGrid, false);
connect(m_syncSnapToGrid, SIGNAL(toggled(bool)), this, SLOT(SnapToGrid_toggled(bool)));
m_menuLine->addAction(action);
// Add all the needed menus
bar->addMenu( m_menuFile );
bar->addMenu( m_menuView );
bar->addMenu( m_menuColorOptions );
bar->addMenu( m_menuLine );
bar->addMenu( m_menuHelp );
}
//------------------------------------------------------------------------------------
/** Intialize the zooming/panning tools */
void SliceViewer::initZoomer()
{
// QwtPlotZoomer * zoomer = new CustomZoomer(m_plot->canvas());
// zoomer->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton);
// zoomer->setTrackerMode(QwtPicker::AlwaysOff);
// const QColor c(Qt::darkBlue);
// zoomer->setRubberBandPen(c);
// zoomer->setTrackerPen(c);
// QObject::connect(zoomer, SIGNAL(zoomed(const QRectF &)),
// this, SLOT(zoomRectSlot(const QRectF &)));
QwtPlotPicker * zoomer = new QwtPlotPicker(m_plot->canvas());
zoomer->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection);
zoomer->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton);
zoomer->setTrackerMode(QwtPicker::AlwaysOff);
const QColor c(Qt::darkBlue);
zoomer->setRubberBand(QwtPicker::RectRubberBand);
zoomer->setRubberBandPen(c);
QObject::connect(zoomer, SIGNAL(selected(const QwtDoubleRect &)),
this, SLOT(zoomRectSlot(const QwtDoubleRect &)));
// Zoom in/out using middle-click+drag or the mouse wheel
CustomMagnifier * magnif = new CustomMagnifier(m_plot->canvas());
magnif->setAxisEnabled(QwtPlot::yRight, false); // Don't do the colorbar axis
magnif->setWheelFactor(0.9);
magnif->setMouseButton(Qt::MidButton);
// Have to flip the keys to match our flipped mouse wheel
magnif->setZoomInKey(Qt::Key_Minus, Qt::NoModifier);
magnif->setZoomOutKey(Qt::Key_Equal, Qt::NoModifier);
// Hook-up listener to rescaled event
QObject::connect(magnif, SIGNAL(rescaled(double)), this, SLOT(magnifierRescaled(double)));
// Pan using the right mouse button + drag
QwtPlotPanner *panner = new QwtPlotPanner(m_plot->canvas());
panner->setMouseButton(Qt::RightButton);
panner->setAxisEnabled(QwtPlot::yRight, false); // Don't do the colorbar axis
QObject::connect(panner, SIGNAL(panned(int, int)), this, SLOT(panned(int, int))); // Handle panning.
// Custom picker for showing the current coordinates
CustomPicker * picker = new CustomPicker(m_spect->xAxis(), m_spect->yAxis(), m_plot->canvas());
QObject::connect(picker, SIGNAL(mouseMoved(double,double)), this, SLOT(showInfoAt(double, double)));
}
//------------------------------------------------------------------------------------
/** Programmatically show/hide the controls (sliders etc)
*
* @param visible :: true if you want to show the controls.
*/
void SliceViewer::showControls(bool visible)
{
ui.frmControls->setVisible(visible);
}
//------------------------------------------------------------------------------------
/** Add (as needed) and update DimensionSliceWidget's. */
void SliceViewer::updateDimensionSliceWidgets()
{
// Create all necessary widgets
if (m_dimWidgets.size() < m_ws->getNumDims())
{
for (size_t d=m_dimWidgets.size(); d<m_ws->getNumDims(); d++)
{
DimensionSliceWidget * widget = new DimensionSliceWidget(this);
ui.verticalLayoutControls->insertWidget(int(d), widget);
// Slots for changes on the dimension widget
QObject::connect(widget, SIGNAL(changedShownDim(int,int,int)),
this, SLOT(changedShownDim(int,int,int)));
QObject::connect(widget, SIGNAL(changedSlicePoint(int,double)),
this, SLOT(updateDisplaySlot(int,double)));
// Slots for dynamic rebinning
QObject::connect(widget, SIGNAL(changedThickness(int,double)),
this, SLOT(rebinParamsChanged()));
QObject::connect(widget, SIGNAL(changedNumBins(int,int)),
this, SLOT(rebinParamsChanged()));
// Save in this list
m_dimWidgets.push_back(widget);
}
}
// Hide unnecessary ones
for (size_t d=m_ws->getNumDims(); d<m_dimWidgets.size(); d++)
{
DimensionSliceWidget * widget = m_dimWidgets[d];
widget->hide();
}
int maxLabelWidth = 10;
int maxUnitsWidth = 10;
// Set each dimension
for (size_t d=0; d<m_dimensions.size(); d++)
{
DimensionSliceWidget * widget = m_dimWidgets[d];
widget->blockSignals(true);
widget->setDimension( int(d), m_dimensions[d] );
// Default slicing layout
if (d == m_dimX)
widget->setShownDim(0);
else if (d == m_dimY)
widget->setShownDim(1);
else
widget->setShownDim(-1);
// To harmonize the layout, find the largest label
int w;
w = widget->ui.lblName->sizeHint().width();
if (w > maxLabelWidth) maxLabelWidth = w;
w = widget->ui.lblUnits->sizeHint().width();
if (w > maxUnitsWidth) maxUnitsWidth = w;
widget->blockSignals(false);
}
// Make the labels all the same width
for (size_t d=0; d<m_ws->getNumDims(); d++)
{
DimensionSliceWidget * widget = m_dimWidgets[d];
widget->ui.lblName->setMinimumSize(QSize(maxLabelWidth, 0) );
widget->ui.lblUnits->setMinimumSize(QSize(maxUnitsWidth, 0) );
}
}
//------------------------------------------------------------------------------------
/** Set the displayed workspace. Updates UI.
*
* @param ws :: IMDWorkspace to show.
*/
void SliceViewer::setWorkspace(Mantid::API::IMDWorkspace_sptr ws)
{
m_ws = ws;
m_data->setWorkspace(ws);
m_plot->setWorkspace(ws);
// Only allow perpendicular lines if looking at a matrix workspace.
bool matrix = bool(boost::dynamic_pointer_cast<MatrixWorkspace>(m_ws));
m_lineOverlay->setAngleSnapMode(matrix);
m_lineOverlay->setAngleSnap(matrix ? 90 : 45);
// Can't use dynamic rebin mode with a MatrixWorkspace
m_syncRebinMode->setEnabled(!matrix);
m_syncRebinLock->setEnabled(!matrix);
// Go to no normalization by default for MatrixWorkspaces
if (matrix)
this->setNormalization(Mantid::API::NoNormalization, false /* without updating */ );
// Emit the signal that we changed the workspace
emit workspaceChanged();
// For MDEventWorkspace, estimate the resolution and change the # of bins accordingly
IMDEventWorkspace_sptr mdew = boost::dynamic_pointer_cast<IMDEventWorkspace>(m_ws);
std::vector<coord_t> binSizes = m_ws->estimateResolution();
// Copy the dimensions to this so they can be modified
m_dimensions.clear();
for (size_t d=0; d < m_ws->getNumDims(); d++)
{
// Choose the number of bins based on the resolution of the workspace (for MDEWs)
coord_t min = m_ws->getDimension(d)->getMinimum();
coord_t max = m_ws->getDimension(d)->getMaximum();
size_t numBins = static_cast<size_t>((max-min)/binSizes[d]);
MDHistoDimension_sptr dim(new MDHistoDimension(m_ws->getDimension(d).get()));
dim->setRange(numBins, min, max);
m_dimensions.push_back(dim);
}
// Adjust the range to that of visible data
if (mdew)
{
std::vector<Mantid::Geometry::MDDimensionExtents<coord_t> > ext = mdew->getMinimumExtents();
for (size_t d=0; d < mdew->getNumDims(); d++)
{
size_t newNumBins = size_t(ext[d].getSize()/m_dimensions[d]->getBinWidth() + 1);
m_dimensions[d]->setRange(newNumBins, ext[d].getMin(), ext[d].getMax());
}
}
// Build up the widgets
this->updateDimensionSliceWidgets();
// Find the full range. And use it
findRangeFull();
m_colorBar->setViewRange(m_colorRangeFull);
// Initial display update
this->updateDisplay(!m_firstWorkspaceOpen /*Force resetting the axes, the first time*/);
// Don't reset axes next time
m_firstWorkspaceOpen = true;
// For showing the original coordinates
ui.frmMouseInfo->setVisible(false);
if (m_ws->hasOriginalWorkspace())
{
IMDWorkspace_sptr origWS = boost::dynamic_pointer_cast<IMDWorkspace>(m_ws->getOriginalWorkspace());
CoordTransform * toOrig = m_ws->getTransformToOriginal();
if (toOrig)
{
ui.frmMouseInfo->setVisible(true);
ui.lblOriginalWorkspace->setText(QString::fromStdString("in '" + origWS->getName() + "'") );
}
}
// Enable peaks overlays according to the dimensionality and the displayed dimensions.
enablePeakOverlaysIfAppropriate();
// Send out a signal
emit changedShownDim(m_dimX, m_dimY);
}
//------------------------------------------------------------------------------------
/** Set the workspace to view using its name.
* The workspace should be a MDHistoWorkspace or a MDEventWorkspace,
* with at least 2 dimensions.
*
* @param wsName :: name of the MDWorkspace to look for
* @throw std::runtime_error if the workspace is not found or is a MatrixWorkspace
*/
void SliceViewer::setWorkspace(const QString & wsName)
{
IMDWorkspace_sptr ws = boost::dynamic_pointer_cast<IMDWorkspace>(
AnalysisDataService::Instance().retrieve(wsName.toStdString()) );
if (!ws)
throw std::runtime_error("SliceViewer can only view MDWorkspaces.");
if (boost::dynamic_pointer_cast<MatrixWorkspace>(ws))
throw std::runtime_error("SliceViewer cannot view MatrixWorkspaces. "
"Please select a MDEventWorkspace or a MDHistoWorkspace.");
this->setWorkspace(ws);
}
//------------------------------------------------------------------------------------
/** @return the workspace in the SliceViewer */
Mantid::API::IMDWorkspace_sptr SliceViewer::getWorkspace()
{
return m_ws;
}
//------------------------------------------------------------------------------------
/** Load a color map from a file
*
* @param filename :: file to open; empty to ask via a dialog box.
*/
void SliceViewer::loadColorMap(QString filename)
{
QString fileselection;
if (filename.isEmpty())
{
fileselection = MantidColorMap::loadMapDialog(m_currentColorMapFile, this);
if( fileselection.isEmpty() ) return;
}
else
fileselection = filename;
m_currentColorMapFile = fileselection;
// Load from file
m_colorBar->getColorMap().loadMap( fileselection );
m_spect->setColorMap( m_colorBar->getColorMap() );
m_colorBar->updateColorMap();
this->updateDisplay();
}
//=================================================================================================
//========================================== SLOTS ================================================
//=================================================================================================
//------------------------------------------------------------------------------------
/** Automatically sets the min/max of the color scale,
* using the limits in the entire data set of the workspace
* (every bin, even those not currently visible).
*/
void SliceViewer::setColorScaleAutoFull()
{
this->findRangeFull();
m_colorBar->setViewRange(m_colorRangeFull);
this->updateDisplay();
}
//------------------------------------------------------------------------------------
/** Automatically sets the min/max of the color scale,
* using the limits in the data that is currently visible
* in the plot (only the bins in this slice and within the
* view limits)
*/
void SliceViewer::setColorScaleAutoSlice()
{
this->findRangeSlice();
m_colorBar->setViewRange(m_colorRangeSlice);
this->updateDisplay();
}
//------------------------------------------------------------------------------------
/// Slot called when the ColorBarWidget changes the range of colors
void SliceViewer::colorRangeChanged()
{
m_spect->setColorMap( m_colorBar->getColorMap() );
this->updateDisplay();
}
//------------------------------------------------------------------------------------
/** Set whether to display 0 signal as "transparent" color.
*
* @param transparent :: true if you want zeros to be transparent.
*/
void SliceViewer::setTransparentZeros(bool transparent)
{
m_actionTransparentZeros->blockSignals(true);
m_actionTransparentZeros->setChecked(transparent);
m_actionTransparentZeros->blockSignals(false);
// Set and display
m_data->setZerosAsNan(transparent );
this->updateDisplay();
}
//------------------------------------------------------------------------------------
/// Slot called when changing the normalization menu
void SliceViewer::changeNormalizationNone()
{ this->setNormalization(Mantid::API::NoNormalization, true); }
void SliceViewer::changeNormalizationVolume()
{ this->setNormalization(Mantid::API::VolumeNormalization, true); }
void SliceViewer::changeNormalizationNumEvents()
{ this->setNormalization(Mantid::API::NumEventsNormalization, true); }
//------------------------------------------------------------------------------------
/** Set the normalization mode for viewing the data
*
* @param norm :: MDNormalization enum. 0=none; 1=volume; 2=# of events
* @param update :: update the displayed image. If false, just sets it and shows the checkboxes.
*/
void SliceViewer::setNormalization(Mantid::API::MDNormalization norm, bool update)
{
m_actionNormalizeNone->blockSignals(true);
m_actionNormalizeVolume->blockSignals(true);
m_actionNormalizeNumEvents->blockSignals(true);
m_actionNormalizeNone->setChecked(norm == Mantid::API::NoNormalization);
m_actionNormalizeVolume->setChecked(norm == Mantid::API::VolumeNormalization);
m_actionNormalizeNumEvents->setChecked(norm == Mantid::API::NumEventsNormalization);
m_actionNormalizeNone->blockSignals(false);
m_actionNormalizeVolume->blockSignals(false);
m_actionNormalizeNumEvents->blockSignals(false);
m_data->setNormalization(norm);
if (update) this->updateDisplay();
}
//------------------------------------------------------------------------------------
/** @return the current normalization */
Mantid::API::MDNormalization SliceViewer::getNormalization() const
{
return m_data->getNormalization();
}
//------------------------------------------------------------------------------------
/** Set the thickness (above and below the plane) for dynamic rebinning.
*
* @param dim :: index of the dimension to adjust
* @param thickness :: thickness to set, in units of the dimension.
* @throw runtime_error if the dimension index is invalid or the thickness is <= 0.0.
*/
void SliceViewer::setRebinThickness(int dim, double thickness)
{
if (dim < 0 || dim >= static_cast<int>(m_dimWidgets.size()))
throw std::runtime_error("SliceViewer::setRebinThickness(): Invalid dimension index");
if (thickness <= 0.0)
throw std::runtime_error("SliceViewer::setRebinThickness(): Thickness must be > 0.0");
m_dimWidgets[dim]->setThickness(thickness);
}
//------------------------------------------------------------------------------------
/** Set the number of bins for dynamic rebinning.
*
* @param xBins :: number of bins in the viewed X direction
* @param yBins :: number of bins in the viewed Y direction
* @throw runtime_error if the number of bins is < 1
*/
void SliceViewer::setRebinNumBins(int xBins, int yBins)
{
if (xBins < 1 || yBins < 1)
throw std::runtime_error("SliceViewer::setRebinNumBins(): Number of bins must be >= 1");
m_dimWidgets[m_dimX]->setNumBins(xBins);
m_dimWidgets[m_dimY]->setNumBins(yBins);
}
//------------------------------------------------------------------------------------
/** Sets the SliceViewer in dynamic rebin mode.
* In this mode, the current view area (see setXYLimits()) is used as the
* limits to rebin.
* See setRebinNumBins() to adjust the number of bins in the X/Y dimensions.
* See setRebinThickness() to adjust the thickness in other dimensions.
*
* @param mode :: true for rebinning mode
* @param locked :: if true, then the rebinned area is only refreshed manually
* or when changing rebinning parameters.
*/
void SliceViewer::setRebinMode(bool mode, bool locked)
{
// The events associated with these controls will trigger a re-draw
m_syncRebinMode->toggle(mode);
m_syncRebinLock->toggle(locked);
}
//------------------------------------------------------------------------------------
/** When in dynamic rebinning mode, this refreshes the rebinned area to be the
* currently viewed area. See setXYLimits(), setRebinNumBins(), setRebinThickness()
*/
void SliceViewer::refreshRebin()
{
this->rebinParamsChanged();
}
//------------------------------------------------------------------------------------
/// Slot called when the btnDoLine button is checked/unchecked
void SliceViewer::LineMode_toggled(bool checked)
{
m_lineOverlay->setShown(checked);
if (checked)
{
QString text;
if (m_lineOverlay->getCreationMode())
text = "Click and drag to draw an integration line.\n"
"Hold Shift key to limit to 45 degree angles.";
else
text = "Drag the existing line with its handles,\n"
"or click the red X to delete it.";
// Show a tooltip near the button
QToolTip::showText( ui.btnDoLine->mapToGlobal(ui.btnDoLine->pos() ), text, this);
}
emit showLineViewer(checked);
}
//------------------------------------------------------------------------------------
/** Toggle "line-drawing" mode (to draw 1D lines using the mouse)
*
* @param lineMode :: True to go into line mode, False to exit it.
*/
void SliceViewer::toggleLineMode(bool lineMode)
{
// This should send events to start line mode
m_syncLineMode->toggle(lineMode);
m_lineOverlay->setCreationMode(false);
}
//------------------------------------------------------------------------------------
/// Slot called to clear the line in the line overlay
void SliceViewer::clearLine()
{
m_lineOverlay->reset();
m_plot->update();
}
//------------------------------------------------------------------------------------
/// Slot called when the snap to grid is checked
void SliceViewer::SnapToGrid_toggled(bool checked)
{
if (checked)
{
SnapToGridDialog * dlg = new SnapToGridDialog(this);
dlg->setSnap( m_lineOverlay->getSnapX(), m_lineOverlay->getSnapY() );
if (dlg->exec() == QDialog::Accepted)
{
m_lineOverlay->setSnapEnabled(true);
m_lineOverlay->setSnapX( dlg->getSnapX() );
m_lineOverlay->setSnapY( dlg->getSnapY() );
}
else
{
// Uncheck - the user clicked cancel
ui.btnSnapToGrid->setChecked(false);
m_lineOverlay->setSnapEnabled(false);
}
}
else
{
m_lineOverlay->setSnapEnabled(false);
}
}
//------------------------------------------------------------------------------------
/** Slot called when going into or out of dynamic rebinning mode */
void SliceViewer::RebinMode_toggled(bool checked)
{
for (size_t d=0; d<m_dimWidgets.size(); d++)
m_dimWidgets[d]->showRebinControls(checked);
ui.btnRebinRefresh->setEnabled(checked);
ui.btnRebinLock->setEnabled(checked);
m_syncAutoRebin->setEnabled(checked);
m_actionRefreshRebin->setEnabled(checked);
m_rebinMode = checked;
if (!m_rebinMode)
{
// uncheck auto-rebin
ui.btnAutoRebin->setChecked(false);
// Remove the overlay WS
this->m_overlayWS.reset();
this->m_data->setOverlayWorkspace(m_overlayWS);
this->updateDisplay();
}
else
{
// Start the rebin
this->rebinParamsChanged();
}
}
//------------------------------------------------------------------------------------
/** Slot called when locking/unlocking the dynamically rebinned
* overlaid workspace
* @param checked :: DO lock the workspace in place
*/
void SliceViewer::RebinLock_toggled(bool checked)
{
m_rebinLocked = checked;
// Rebin immediately
if (!m_rebinLocked && m_rebinMode)
this->rebinParamsChanged();
}
//------------------------------------------------------------------------------------
/// Slot for zooming into
void SliceViewer::zoomInSlot()
{
this->zoomBy(1.1);
}
/// Slot for zooming out
void SliceViewer::zoomOutSlot()
{
this->zoomBy(1.0 / 1.1);
}
/** Slot called when zooming using QwtPlotZoomer (rubber-band method)
*
* @param rect :: rectangle to zoom to
*/
void SliceViewer::zoomRectSlot(const QwtDoubleRect & rect)
{
if ((rect.width() == 0) || (rect.height() == 0))
return;
this->setXYLimits(rect.left(), rect.right(), rect.top(), rect.bottom());
autoRebinIfRequired();
}
/// Slot for opening help page
void SliceViewer::helpSliceViewer()
{
QString helpPage = "MantidPlot:_SliceViewer";
QDesktopServices::openUrl(QUrl(QString("http://www.mantidproject.org/") + helpPage));
}
/// Slot for opening help page
void SliceViewer::helpLineViewer()
{
QString helpPage = "MantidPlot:_LineViewer";
QDesktopServices::openUrl(QUrl(QString("http://www.mantidproject.org/") + helpPage));
}
void SliceViewer::helpPeaksViewer()
{
QString helpPage = "PeaksViewer";
QDesktopServices::openUrl(QUrl(QString("http://www.mantidproject.org/") + helpPage));
}
//------------------------------------------------------------------------------------