-
Notifications
You must be signed in to change notification settings - Fork 122
/
InstrumentWindow.cpp
1366 lines (1227 loc) · 37.5 KB
/
InstrumentWindow.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 "InstrumentWindow.h"
#include "InstrumentWindowRenderTab.h"
#include "InstrumentWindowPickTab.h"
#include "InstrumentWindowMaskTab.h"
#include "InstrumentWindowTreeTab.h"
#include "XIntegrationControl.h"
#include "InstrumentActor.h"
#include "UnwrappedCylinder.h"
#include "UnwrappedSphere.h"
#include "Projection3D.h"
#include "PanelsSurface.h"
#include "SimpleWidget.h"
#include "DetXMLFile.h"
#include "../MantidUI.h"
#include "../AlgorithmMonitor.h"
#include "MantidKernel/ConfigService.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include <Poco/Path.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QString>
#include <QSplitter>
#include <QDoubleValidator>
#include <QRadioButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QComboBox>
#include <QSettings>
#include <QFileInfo>
#include <QColorDialog>
#include <QLineEdit>
#include <QCheckBox>
#include <QImageWriter>
#include <QApplication>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QStackedLayout>
#include <QKeyEvent>
#include <QUrl>
#include <QTemporaryFile>
#include "MantidQtAPI/FileDialogHandler.h"
#include <numeric>
#include <stdexcept>
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace MantidQt::API;
// Name of the QSettings group to store the InstrumentWindw settings
const char* InstrumentWindowSettingsGroup = "Mantid/InstrumentWindow";
/**
* Constructor.
*/
InstrumentWindow::InstrumentWindow(const QString& wsName, const QString& label, ApplicationWindow *app , const QString& name , Qt::WFlags f ):
MdiSubWindow(app, label, name, f), WorkspaceObserver(),
m_InstrumentDisplay(NULL),
m_simpleDisplay(NULL),
m_workspaceName(wsName),
m_instrumentActor(NULL),
m_surfaceType(FULL3D),
m_savedialog_dir(QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory"))),
mViewChanged(false),
m_blocked(false),
m_instrumentDisplayContextMenuOn(false)
{
setFocusPolicy(Qt::StrongFocus);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QSplitter* controlPanelLayout = new QSplitter(Qt::Horizontal);
//Add Tab control panel
mControlsTab = new QTabWidget(this,0);
controlPanelLayout->addWidget(mControlsTab);
controlPanelLayout->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
// Create the display widget
m_InstrumentDisplay = new MantidGLWidget(this);
m_InstrumentDisplay->installEventFilter(this);
connect(this,SIGNAL(enableLighting(bool)),m_InstrumentDisplay,SLOT(enableLighting(bool)));
// Create simple display widget
m_simpleDisplay = new SimpleWidget(this);
m_simpleDisplay->installEventFilter(this);
QWidget* aWidget = new QWidget(this);
m_instrumentDisplayLayout = new QStackedLayout(aWidget);
m_instrumentDisplayLayout->addWidget(m_InstrumentDisplay);
m_instrumentDisplayLayout->addWidget(m_simpleDisplay);
controlPanelLayout->addWidget(aWidget);
mainLayout->addWidget(controlPanelLayout);
m_xIntegration = new XIntegrationControl(this);
mainLayout->addWidget(m_xIntegration);
connect(m_xIntegration,SIGNAL(changed(double,double)),this,SLOT(setIntegrationRange(double,double)));
//Set the mouse/keyboard operation info and help button
QHBoxLayout* infoLayout = new QHBoxLayout();
mInteractionInfo = new QLabel();
infoLayout->addWidget(mInteractionInfo);
QPushButton* helpButton = new QPushButton("?");
helpButton->setMaximumWidth(25);
connect(helpButton,SIGNAL(clicked()),this,SLOT(helpClicked()));
infoLayout->addWidget(helpButton);
infoLayout->setStretchFactor(mInteractionInfo,1);
infoLayout->setStretchFactor(helpButton,0);
mainLayout->addLayout(infoLayout);
QSettings settings;
settings.beginGroup("Mantid/InstrumentWindow");
// Background colour
setBackgroundColor(settings.value("BackgroundColor",QColor(0,0,0,1.0)).value<QColor>());
// Create the b=tabs
createTabs(settings);
settings.endGroup();
// Init actions
mInfoAction = new QAction(tr("&Details"), this);
connect(mInfoAction,SIGNAL(triggered()),this,SLOT(spectraInfoDialog()));
mPlotAction = new QAction(tr("&Plot Spectra"), this);
connect(mPlotAction,SIGNAL(triggered()),this,SLOT(plotSelectedSpectra()));
mDetTableAction = new QAction(tr("&Extract Data"), this);
connect(mDetTableAction, SIGNAL(triggered()), this, SLOT(showDetectorTable()));
m_clearPeakOverlays = new QAction("Clear peaks",this);
connect(m_clearPeakOverlays,SIGNAL(activated()),this,SLOT(clearPeakOverlays()));
confirmClose(app->confirmCloseInstrWindow);
setAttribute(Qt::WA_DeleteOnClose);
// Watch for the deletion of the associated workspace
observePreDelete();
observeAfterReplace();
observeADSClear();
connect(app->mantidUI->getAlgMonitor(),SIGNAL(algorithmStarted(void*)),this,SLOT(block()));
connect(app->mantidUI->getAlgMonitor(),SIGNAL(allAlgorithmsStopped()),this,SLOT(unblock()));
const int windowWidth = 800;
const int tabsSize = windowWidth / 4;
QList<int> sizes;
sizes << tabsSize << windowWidth - tabsSize;
controlPanelLayout->setSizes(sizes);
controlPanelLayout->setStretchFactor(0,0);
controlPanelLayout->setStretchFactor(1,1);
resize(windowWidth,650);
tabChanged(0);
connect(this,SIGNAL(needSetIntegrationRange(double,double)),this,SLOT(setIntegrationRange(double,double)),Qt::QueuedConnection);
setAcceptDrops(true);
setWindowTitle(QString("Instrument - ") + m_workspaceName);
}
/**
* Destructor
*/
InstrumentWindow::~InstrumentWindow()
{
if (m_instrumentActor)
{
saveSettings();
delete m_instrumentActor;
}
}
/**
* Init the geometry and colour map outside constructor to prevent creating a broken MdiSubwindow.
* Must be called straight after constructor.
* @param resetGeometry :: Set true for resetting the view's geometry: the bounding box and rotation. Default is true.
* @param autoscaling :: True to start with autoscaling option on.
* @param scaleMin :: Minimum value of the colormap scale. Ignored if autoscaling == true.
* @param scaleMax :: Maximum value of the colormap scale. Ignored if autoscaling == true.
* @param setDefaultView :: Set the default surface type
*/
void InstrumentWindow::init(bool resetGeometry, bool autoscaling, double scaleMin, double scaleMax, bool setDefaultView)
{
// Previously in (now removed) setWorkspaceName method
m_instrumentActor = new InstrumentActor(m_workspaceName, autoscaling, scaleMin, scaleMax);
m_xIntegration->setTotalRange(m_instrumentActor->minBinValue(),m_instrumentActor->maxBinValue());
m_xIntegration->setUnits(QString::fromStdString(m_instrumentActor->getWorkspace()->getAxis(0)->unit()->caption()));
auto surface = getSurface();
if ( resetGeometry || !surface )
{
if ( setDefaultView )
{
// set the view type to the instrument's default view
QString defaultView = QString::fromStdString(m_instrumentActor->getInstrument()->getDefaultView());
if ( defaultView == "3D" && Mantid::Kernel::ConfigService::Instance().getString("MantidOptions.InstrumentView.UseOpenGL") != "On" )
{
// if OpenGL is switched off don't open the 3D view at start up
defaultView = "CYLINDRICAL_Y";
}
setSurfaceType( defaultView );
}
else
{
setSurfaceType(m_surfaceType); // This call must come after the InstrumentActor is created
}
setupColorMap();
}
else
{
surface->resetInstrumentActor( m_instrumentActor );
updateInfoText();
}
}
/**
* Select the tab to be displayed
*/
void InstrumentWindow::selectTab(int tab)
{
mControlsTab->setCurrentIndex(tab);
}
/**
* Returns the named tab or the current tab if none supplied
* @param title Optional title of a tab (default="")
*/
InstrumentWindowTab *InstrumentWindow::getTab(const QString & title)const
{
QWidget *tab(NULL);
if(title.isEmpty()) tab = mControlsTab->currentWidget();
else
{
for(int i = 0; i < mControlsTab->count(); ++i)
{
if(mControlsTab->tabText(i) == title)
{
tab = mControlsTab->widget(i);
break;
}
}
}
if(tab) return qobject_cast<InstrumentWindowTab*>(tab);
else return NULL;
}
/**
* @param tab An enumeration for the tab to select
* @returns A pointer to the requested tab
*/
InstrumentWindowTab * InstrumentWindow::getTab(const Tab tab) const
{
QWidget *widget = mControlsTab->widget(static_cast<int>(tab));
if(widget) return qobject_cast<InstrumentWindowTab*>(widget);
else return NULL;
}
/**
* Opens Qt file dialog to select the filename.
* The dialog opens in the directory used last for saving or the default user directory.
*
* @param title :: The title of the dialog.
* @param filters :: The filters
* @param selectedFilter :: The selected filter.
*/
QString InstrumentWindow::getSaveFileName(const QString& title, const QString& filters, QString *selectedFilter)
{
QString filename = MantidQt::API::FileDialogHandler::getSaveFileName(this, title, m_savedialog_dir, filters, selectedFilter);
// If its empty, they cancelled the dialog
if( !filename.isEmpty() )
{
//Save the directory used
QFileInfo finfo(filename);
m_savedialog_dir = finfo.dir().path();
}
return filename;
}
/**
* Update the info text displayed at the bottom of the window.
*/
void InstrumentWindow::updateInfoText()
{
setInfoText( getSurfaceInfoText() );
}
void InstrumentWindow::setSurfaceType(int type)
{
// we cannot do 3D without OpenGL
if ( type == FULL3D && !isGLEnabled() )
{
QMessageBox::warning(this,"Mantid - Warning","OpenGL must be enabled to render the instrument in 3D.");
return;
}
if (type < RENDERMODE_SIZE)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
m_surfaceType = SurfaceType(type);
if (!m_instrumentActor) return;
ProjectionSurface* surface = getSurface().get();
int peakLabelPrecision = 6;
bool showPeakRow = true;
bool showPeakLabels = true;
if ( surface )
{
peakLabelPrecision = surface->getPeakLabelPrecision();
showPeakRow = surface->getShowPeakRowsFlag();
showPeakLabels = surface->getShowPeakLabelsFlag();
}
else
{
QSettings settings;
peakLabelPrecision = settings.value("Mantid/InstrumentWindow/PeakLabelPrecision",2).toInt();
showPeakRow = settings.value("Mantid/InstrumentWindow/ShowPeakRows",true).toBool();
showPeakLabels = settings.value("Mantid/InstrumentWindow/ShowPeakLabels",true).toBool();
}
// Surface factory
{
Mantid::Geometry::Instrument_const_sptr instr = m_instrumentActor->getInstrument();
Mantid::Geometry::IObjComponent_const_sptr sample = instr->getSample();
Mantid::Kernel::V3D sample_pos = sample->getPos();
Mantid::Kernel::V3D axis;
// define the axis
if (m_surfaceType == SPHERICAL_Y || m_surfaceType == CYLINDRICAL_Y)
{
axis = Mantid::Kernel::V3D(0,1,0);
}
else if (m_surfaceType == SPHERICAL_Z || m_surfaceType == CYLINDRICAL_Z)
{
axis = Mantid::Kernel::V3D(0,0,1);
}
else if (m_surfaceType == SPHERICAL_X || m_surfaceType == CYLINDRICAL_X)
{
axis = Mantid::Kernel::V3D(1,0,0);
}
else // SIDE_BY_SIDE
{
axis = Mantid::Kernel::V3D(0,0,1);
}
// create the surface
if (m_surfaceType == FULL3D)
{
surface = new Projection3D(m_instrumentActor,getInstrumentDisplayWidth(),getInstrumentDisplayHeight());
}
else if (m_surfaceType <= CYLINDRICAL_Z)
{
surface = new UnwrappedCylinder(m_instrumentActor,sample_pos,axis);
}
else if (m_surfaceType <= SPHERICAL_Z)
{
surface = new UnwrappedSphere(m_instrumentActor,sample_pos,axis);
}
else // SIDE_BY_SIDE
{
surface = new PanelsSurface(m_instrumentActor,sample_pos,axis);
}
}
// end Surface factory
surface->setPeakLabelPrecision(peakLabelPrecision);
surface->setShowPeakRowsFlag(showPeakRow);
surface->setShowPeakLabelsFlag(showPeakLabels);
// set new surface
setSurface(surface);
// init tabs with new surface
foreach (InstrumentWindowTab* tab, m_tabs)
{
tab->initSurface();
}
//connect(surface,SIGNAL(multipleDetectorsSelected(QList<int>&)),this,SLOT(multipleDetectorsSelected(QList<int>&)));
connect(surface,SIGNAL(executeAlgorithm(Mantid::API::IAlgorithm_sptr)),this,SIGNAL(execMantidAlgorithm(Mantid::API::IAlgorithm_sptr)));
connect(surface,SIGNAL(updateInfoText()),this,SLOT(updateInfoText()),Qt::QueuedConnection);
QApplication::restoreOverrideCursor();
}
emit surfaceTypeChanged( type );
updateInfoText();
update();
}
/**
* Set the surface type from a string.
* @param typeStr :: Symbolic name of the surface type: same as the names in SurfaceType enum. Caseless.
*/
void InstrumentWindow::setSurfaceType(const QString& typeStr)
{
int typeIndex = 0;
QString upperCaseStr = typeStr.toUpper();
if ( upperCaseStr == "FULL3D" || upperCaseStr == "3D" )
{
typeIndex = 0;
}
else if ( upperCaseStr == "CYLINDRICAL_X" )
{
typeIndex = 1;
}
else if ( upperCaseStr == "CYLINDRICAL_Y" )
{
typeIndex = 2;
}
else if ( upperCaseStr == "CYLINDRICAL_Z" )
{
typeIndex = 3;
}
else if ( upperCaseStr == "SPHERICAL_X" )
{
typeIndex = 4;
}
else if ( upperCaseStr == "SPHERICAL_Y" )
{
typeIndex = 5;
}
else if ( upperCaseStr == "SPHERICAL_Z" )
{
typeIndex = 6;
}
else if ( upperCaseStr == "SIDE_BY_SIDE" )
{
typeIndex = 7;
}
setSurfaceType( typeIndex );
}
/**
* Update the colormap on the render tab.
*/
void InstrumentWindow::setupColorMap()
{
emit colorMapChanged();
}
/**
* Connected to QTabWidget::currentChanged signal
*/
void InstrumentWindow::tabChanged(int)
{
updateInfoText();
}
/**
* Change color map button slot. This provides the file dialog box to select colormap or sets it directly a string is provided
*/
void InstrumentWindow::changeColormap(const QString &filename)
{
if (!m_instrumentActor) return;
QString fileselection;
//Use a file dialog if no parameter is passed
if( filename.isEmpty() )
{
fileselection = MantidColorMap::loadMapDialog(m_instrumentActor->getCurrentColorMap(), this);
if( fileselection.isEmpty() ) return;
}
else
{
fileselection = QFileInfo(filename).absoluteFilePath();
if( !QFileInfo(fileselection).exists() ) return;
}
if( !m_instrumentActor->getCurrentColorMap().isEmpty() && (fileselection == m_instrumentActor->getCurrentColorMap())) return;
m_instrumentActor->loadColorMap(fileselection);
if( this->isVisible() )
{
setupColorMap();
updateInstrumentView();
}
}
/**
* This is slot for the dialog to appear when a detector is picked and the info menu is selected
*/
void InstrumentWindow::spectraInfoDialog()
{
QString info;
const int ndets = static_cast<int>(m_selectedDetectors.size());
if( ndets == 1 )
{
QString wsIndex;
try {
wsIndex = QString::number(m_instrumentActor->getWorkspaceIndex(m_selectedDetectors.front()));
} catch (Mantid::Kernel::Exception::NotFoundError &) {
// Detector doesn't have a workspace index relating to it
wsIndex = "None";
}
info = QString("Workspace index: %1\nDetector ID: %2").arg(wsIndex,
QString::number(m_selectedDetectors.front()));
}
else
{
std::vector<size_t> wksp_indices;
for(int i = 0; i < m_selectedDetectors.size(); ++i)
{
try {
wksp_indices.push_back(m_instrumentActor->getWorkspaceIndex(m_selectedDetectors[i]));
} catch (Mantid::Kernel::Exception::NotFoundError &) {
continue; // Detector doesn't have a workspace index relating to it
}
}
info = QString("Index list size: %1\nDetector list size: %2").arg(QString::number(wksp_indices.size()), QString::number(ndets));
}
QMessageBox::information(this,tr("Detector/Spectrum Information"), info,
QMessageBox::Ok|QMessageBox::Default, QMessageBox::NoButton, QMessageBox::NoButton);
}
/**
* Sends a signal to plot the selected spectrum.
*/
void InstrumentWindow::plotSelectedSpectra()
{
if (m_selectedDetectors.empty()) return;
std::set<int> indices;
for(int i = 0; i < m_selectedDetectors.size(); ++i)
{
try {
indices.insert(int(m_instrumentActor->getWorkspaceIndex(m_selectedDetectors[i])));
} catch (Mantid::Kernel::Exception::NotFoundError &) {
continue; // Detector doesn't have a workspace index relating to it
}
}
emit plotSpectra(m_workspaceName, indices);
}
/**
* Show detector table
*/
void InstrumentWindow::showDetectorTable()
{
if (m_selectedDetectors.empty()) return;
std::vector<int> indexes;
for(int i = 0; i < m_selectedDetectors.size(); ++i)
{
try {
indexes.push_back(int(m_instrumentActor->getWorkspaceIndex(m_selectedDetectors[i])));
} catch (Mantid::Kernel::Exception::NotFoundError &) {
continue; // Detector doesn't have a workspace index relating to it
}
}
emit createDetectorTable(m_workspaceName, indexes, true);
}
QString InstrumentWindow::confirmDetectorOperation(const QString & opName, const QString & inputWS, int ndets)
{
QString message("This operation will affect %1 detectors.\nSelect output workspace option:");
QMessageBox prompt(this);
prompt.setWindowTitle("MantidPlot");
prompt.setText(message.arg(QString::number(ndets)));
QPushButton *replace = prompt.addButton("Replace", QMessageBox::ActionRole);
QPushButton *create = prompt.addButton("New", QMessageBox::ActionRole);
prompt.addButton("Cancel", QMessageBox::ActionRole);
prompt.exec();
QString outputWS;
if( prompt.clickedButton() == replace )
{
outputWS = inputWS;
}
else if( prompt.clickedButton() == create )
{
outputWS = inputWS + "_" + opName;
}
else
{
outputWS = "";
}
return outputWS;
}
/**
* Convert a list of integers to a comma separated string of numbers
*/
QString InstrumentWindow::asString(const std::vector<int>& numbers) const
{
QString num_str;
std::vector<int>::const_iterator iend = numbers.end();
for( std::vector<int>::const_iterator itr = numbers.begin(); itr < iend; ++itr )
{
num_str += QString::number(*itr) + ",";
}
//Remove trailing comma
num_str.chop(1);
return num_str;
}
/// Set a maximum and minimum for the colour map range
void InstrumentWindow::setColorMapRange(double minValue, double maxValue)
{
emit colorMapRangeChanged(minValue, maxValue);
update();
}
/// Set the minimum value of the colour map
void InstrumentWindow::setColorMapMinValue(double minValue)
{
emit colorMapMinValueChanged(minValue);
update();
}
/// Set the maximumu value of the colour map
void InstrumentWindow::setColorMapMaxValue(double maxValue)
{
emit colorMapMaxValueChanged(maxValue);
update();
}
/**
* This is the callback for the combo box that selects the view direction
*/
void InstrumentWindow::setViewDirection(const QString& input)
{
auto p3d = boost::dynamic_pointer_cast<Projection3D>( getSurface() );
if (p3d)
{
p3d->setViewDirection(input);
}
updateInstrumentView();
repaint();
}
/** For the scripting API. Selects a component in the tree and zooms to it.
* @param name The name of the component
* @throw std::invalid_argument If the component name given does not exist in the tree
*/
void InstrumentWindow::selectComponent(const QString & name)
{
emit requestSelectComponent(name);
}
/**
* Set the scale type programmatically
* @param type :: The scale choice
*/
void InstrumentWindow::setScaleType(GraphOptions::ScaleType type)
{
emit scaleTypeChanged(type);
}
/**
* This method opens a color dialog to pick the background color,
* and then sets it.
*/
void InstrumentWindow::pickBackgroundColor()
{
QColor color = QColorDialog::getColor(Qt::green,this);
setBackgroundColor(color);
}
/**
* Saves the current image buffer as a png file.
* @param filename Optional filename. Empty string raises a save dialog
*/
void InstrumentWindow::saveImage(QString filename)
{
QString defaultExt = ".png";
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
if(filename.isEmpty())
{
QListIterator<QByteArray> itr(formats);
QString filter("");
while( itr.hasNext() )
{
filter += "*." + itr.next();
if( itr.hasNext() )
{
filter += ";;";
}
}
QString selectedFilter = "*" + defaultExt;
filename = getSaveFileName("Save image ...", filter, &selectedFilter);
// If its empty, they cancelled the dialog
if( filename.isEmpty() ) return;
}
QFileInfo finfo(filename);
QString ext = finfo.completeSuffix();
if( ext.isEmpty() )
{
filename += defaultExt;
ext = QFileInfo(filename).completeSuffix();
}
else
{
if( !formats.contains(ext.toAscii()) )
{
QString msg("Unsupported file extension. Choose one of the following: ");
QListIterator<QByteArray> itr(formats);
while( itr.hasNext() )
{
msg += itr.next() + ", ";
}
msg.chop(2);// Remove last space and comma
QMessageBox::warning(this, "MantidPlot", msg);
return;
}
}
if ( isGLEnabled() )
{
m_InstrumentDisplay->saveToFile(filename);
}
else
{
m_simpleDisplay->saveToFile(filename);
}
}
/**
* Use the file dialog to select a filename to save grouping.
*/
QString InstrumentWindow::getSaveGroupingFilename()
{
QString filename = MantidQt::API::FileDialogHandler::getSaveFileName(this, "Save grouping file", m_savedialog_dir, "Grouping (*.xml);;All files (*.*)");
// If its empty, they cancelled the dialog
if( !filename.isEmpty() )
{
//Save the directory used
QFileInfo finfo(filename);
m_savedialog_dir = finfo.dir().path();
}
return filename;
}
///**
// * Update the text display that informs the user of the current mode and details about it
// */
void InstrumentWindow::setInfoText(const QString& text)
{
mInteractionInfo->setText(text);
}
/**
* Save properties of the window a persistent store
*/
void InstrumentWindow::saveSettings()
{
QSettings settings;
settings.beginGroup("Mantid/InstrumentWindow");
if ( m_InstrumentDisplay )
settings.setValue("BackgroundColor", m_InstrumentDisplay->currentBackgroundColor());
settings.setValue("PeakLabelPrecision",getSurface()->getPeakLabelPrecision());
settings.setValue("ShowPeakRows",getSurface()->getShowPeakRowsFlag());
settings.setValue("ShowPeakLabels",getSurface()->getShowPeakLabelsFlag());
foreach(InstrumentWindowTab* tab, m_tabs)
{
tab->saveSettings(settings);
}
settings.endGroup();
}
/**
* Closes the window if the associated workspace is deleted.
* @param ws_name :: Name of the deleted workspace.
* @param workspace_ptr :: Pointer to the workspace to be deleted
*/
void InstrumentWindow::preDeleteHandle(const std::string & ws_name, const boost::shared_ptr<Workspace> workspace_ptr)
{
if (ws_name == m_workspaceName.toStdString())
{
confirmClose(false);
close();
return;
}
Mantid::API::IPeaksWorkspace_sptr pws = boost::dynamic_pointer_cast<Mantid::API::IPeaksWorkspace>(workspace_ptr);
if (pws)
{
getSurface()->deletePeaksWorkspace(pws);
updateInstrumentView();
return;
}
}
void InstrumentWindow::afterReplaceHandle(const std::string& wsName,
const boost::shared_ptr<Workspace> workspace)
{
//Replace current workspace
if (wsName == m_workspaceName.toStdString())
{
bool resetGeometry = true;
bool autoscaling = true;
double scaleMin = 0.0;
double scaleMax = 0.0;
if (m_instrumentActor)
{
// try to detect if the instrument changes with the workspace
auto matrixWS = boost::dynamic_pointer_cast<const MatrixWorkspace>( workspace );
resetGeometry = matrixWS->getInstrument()->getNumberDetectors() != m_instrumentActor->ndetectors();
// if instrument doesn't change keep the scaling
if ( !resetGeometry )
{
autoscaling = m_instrumentActor->autoscaling();
scaleMin = m_instrumentActor->minValue();
scaleMax = m_instrumentActor->maxValue();
}
delete m_instrumentActor;
m_instrumentActor = NULL;
}
init( resetGeometry, autoscaling, scaleMin, scaleMax, false );
updateInstrumentDetectors();
}
}
void InstrumentWindow::clearADSHandle()
{
confirmClose(false);
close();
}
/**
* This method saves the workspace name associated with the instrument window
* and geometry to a string.This is useful for loading/saving the project.
*/
QString InstrumentWindow::saveToString(const QString& geometry, bool saveAsTemplate)
{
(void) saveAsTemplate;
QString s="<instrumentwindow>\n";
s+="WorkspaceName\t"+m_workspaceName+"\n";
s+=geometry;
s+="</instrumentwindow>\n";
return s;
}
/**
* Called just before a show event
*/
void InstrumentWindow::showEvent(QShowEvent* e)
{
MdiSubWindow::showEvent(e);
//updateWindow();
}
void InstrumentWindow::block()
{
m_blocked = true;
}
void InstrumentWindow::unblock()
{
m_blocked = false;
}
void InstrumentWindow::helpClicked()
{
QDesktopServices::openUrl(QUrl("http://www.mantidproject.org/MantidPlot:_Instrument_View"));
}
void InstrumentWindow::set3DAxesState(bool on)
{
auto p3d = boost::dynamic_pointer_cast<Projection3D>( getSurface() );
if (p3d)
{
p3d->set3DAxesState(on);
updateInstrumentView();
}
}
void InstrumentWindow::finishHandle(const Mantid::API::IAlgorithm* alg)
{
UNUSED_ARG(alg);
emit needSetIntegrationRange(m_instrumentActor->minBinValue(),m_instrumentActor->maxBinValue());
//m_instrumentActor->update();
//m_InstrumentDisplay->refreshView();
}
void InstrumentWindow::changeScaleType(int type)
{
m_instrumentActor->changeScaleType(type);
setupColorMap();
updateInstrumentView();
}
void InstrumentWindow::changeColorMapMinValue(double minValue)
{
m_instrumentActor->setAutoscaling(false);
m_instrumentActor->setMinValue(minValue);
setupColorMap();
updateInstrumentView();
}
/// Set the maximumu value of the colour map
void InstrumentWindow::changeColorMapMaxValue(double maxValue)
{
m_instrumentActor->setAutoscaling(false);
m_instrumentActor->setMaxValue(maxValue);
setupColorMap();
updateInstrumentView();
}
void InstrumentWindow::changeColorMapRange(double minValue, double maxValue)
{
m_instrumentActor->setMinMaxRange(minValue,maxValue);
setupColorMap();
updateInstrumentView();
}
void InstrumentWindow::setWireframe(bool on)
{
auto p3d = boost::dynamic_pointer_cast<Projection3D>( getSurface() );
if (p3d)
{
p3d->setWireframe(on);
}
updateInstrumentView();
}
/**
* Set new integration range but don't update XIntegrationControl (because the control calls this slot)
*/
void InstrumentWindow::setIntegrationRange(double xmin,double xmax)
{
m_instrumentActor->setIntegrationRange(xmin,xmax);
setupColorMap();
updateInstrumentDetectors();
emit integrationRangeChanged(xmin,xmax);
}
/**
* Set new integration range and update XIntegrationControl. To be called from python.
*/
void InstrumentWindow::setBinRange(double xmin,double xmax)
{
m_xIntegration->setRange(xmin,xmax);
}
/**
* Update the display to view a selected component. The selected component
* is visible the rest of the instrument is hidden.
* @param id :: The component id.
*/
void InstrumentWindow::componentSelected(ComponentID id)
{
auto surface = getSurface();
if (surface)
{
surface->componentSelected(id);
//surface->updateView();
updateInstrumentView();
}
}
void InstrumentWindow::executeAlgorithm(const QString& alg_name, const QString& param_list)
{
emit execMantidAlgorithm(alg_name,param_list,this);
}
void InstrumentWindow::executeAlgorithm(Mantid::API::IAlgorithm_sptr alg)
{
emit execMantidAlgorithm( alg );
}
/**
* Set the type of the view (SurfaceType).
* @param type :: String code for the type. One of:
* FULL3D, CYLINDRICAL_X, CYLINDRICAL_Y, CYLINDRICAL_Z, SPHERICAL_X, SPHERICAL_Y, SPHERICAL_Z
*/
void InstrumentWindow::setViewType(const QString& type)
{
QString type_upper = type.toUpper();
SurfaceType itype = FULL3D;
if (type_upper == "FULL3D")
{
itype = FULL3D;
}
else if (type_upper == "CYLINDRICAL_X")
{
itype = CYLINDRICAL_X;
}
else if (type_upper == "CYLINDRICAL_Y")
{
itype = CYLINDRICAL_Y;
}
else if (type_upper == "CYLINDRICAL_Z")