/
MantidUI.cpp
3535 lines (3116 loc) · 111 KB
/
MantidUI.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 "PythonSystemHeader.h"
#include "MantidUI.h"
#include "MantidMatrix.h"
#include "MantidDock.h"
#include "ImportWorkspaceDlg.h"
#include "AlgorithmMonitor.h"
#include "MantidSampleLogDialog.h"
#include "MantidSampleMaterialDialog.h"
#include "AlgorithmHistoryWindow.h"
#include "MantidMatrixCurve.h"
#include "MantidMDCurve.h"
#include "MantidMDCurveDialog.h"
#include "MantidQtMantidWidgets/FitPropertyBrowser.h"
#include "MantidTable.h"
#include "../../MantidQt/MantidWidgets/ui_SequentialFitDialog.h"
#include "../Spectrogram.h"
#include "../pixmaps.h"
#include "../ScriptingWindow.h"
#include "../Folder.h"
#include "../TiledWindow.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/LogFilter.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/UnitConversion.h"
#include "InstrumentWidget/InstrumentWindow.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include "MantidQtAPI/InterfaceManager.h"
#include "MantidQtAPI/PlotAxis.h"
#include "MantidQtAPI/VatesViewerInterface.h"
#include "MantidKernel/EnvironmentHistory.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include <QMessageBox>
#include <QTextEdit>
#include <QListWidget>
#include <QMdiArea>
#include <QMenuBar>
#include <QApplication>
#include <QToolBar>
#include <QMenu>
#include <QInputDialog>
#include <qwt_plot_curve.h>
#include <algorithm>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <algorithm>
#include <locale>
#include <set>
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/tokenizer.hpp>
#include <Poco/ActiveResult.h>
#include "MantidAPI/IMDWorkspace.h"
#include "MantidQtSliceViewer/SliceViewerWindow.h"
#include "MantidQtFactory/WidgetFactory.h"
#include "MantidAPI/MemoryManager.h"
#include "MantidQtSpectrumViewer/SpectrumView.h"
#include <typeinfo>
using namespace std;
using namespace Mantid::API;
using Mantid::Kernel::DateAndTime;
using MantidQt::SliceViewer::SliceViewerWindow;
namespace MantidException = Mantid::Kernel::Exception;
namespace
{
/// The number of detectors to show within a group before eliding
size_t DET_TABLE_NDETS_GROUP = 10;
// Initialize logger
Mantid::Kernel::Logger g_log("MantidUI");
}
MantidUI::MantidUI(ApplicationWindow *aw):
m_finishedLoadDAEObserver(*this, &MantidUI::handleLoadDAEFinishedNotification),
m_addObserver(*this,&MantidUI::handleAddWorkspace),
m_replaceObserver(*this,&MantidUI::handleReplaceWorkspace),
m_deleteObserver(*this,&MantidUI::handleDeleteWorkspace),
m_clearADSObserver(*this,&MantidUI::handleClearADS),
m_renameObserver(*this,&MantidUI::handleRenameWorkspace),
m_groupworkspacesObserver(*this,&MantidUI::handleGroupWorkspaces),
m_ungroupworkspaceObserver(*this,&MantidUI::handleUnGroupWorkspace),
m_workspaceGroupUpdateObserver(*this,&MantidUI::handleWorkspaceGroupUpdate),
m_configServiceObserver(*this,&MantidUI::handleConfigServiceUpdate),
m_appWindow(aw), m_vatesSubWindow(NULL)//, m_spectrumViewWindow(NULL)
{
// To be able to use them in queued signals they need to be registered
static bool registered_addtional_types = false;
if( !registered_addtional_types )
{
registered_addtional_types = true;
qRegisterMetaType<Mantid::API::Workspace_sptr>();
qRegisterMetaType<Mantid::API::MatrixWorkspace_sptr>();
qRegisterMetaType<Mantid::API::MatrixWorkspace_const_sptr>();
//Register std::string as well as we use it alot
qRegisterMetaType<std::string>();
}
m_exploreMantid = new MantidDockWidget(this,aw);
m_exploreAlgorithms = new AlgorithmDockWidget(this,aw);
actionCopyRowToTable = new QAction(this);
actionCopyRowToTable->setIcon(QIcon(getQPixmap("table_xpm")));
connect(actionCopyRowToTable, SIGNAL(activated()), this, SLOT(copyRowToTable()));
actionCopyRowToGraph = new QAction(this);
actionCopyRowToGraph->setIcon(QIcon(getQPixmap("graph_xpm")));
connect(actionCopyRowToGraph, SIGNAL(activated()), this, SLOT(copyRowToGraph()));
actionCopyRowToGraphErr = new QAction(this);
actionCopyRowToGraphErr->setIcon(QIcon(getQPixmap("graph_xpm")));
connect(actionCopyRowToGraphErr, SIGNAL(activated()), this, SLOT(copyRowToGraphErr()));
actionWaterfallPlot = new QAction(QIcon(":/waterfall_plot.png"), tr("Plot spectra as waterfall"), this);
connect(actionWaterfallPlot, SIGNAL(activated()), this, SLOT(copyRowsToWaterfall()));
actionCopyDetectorsToTable = new QAction(tr("View detectors table"), this);
actionCopyDetectorsToTable->setIcon(QIcon(getQPixmap("table_xpm")));
connect(actionCopyDetectorsToTable, SIGNAL(activated()), this, SLOT(copyDetectorsToTable()));
actionCopyValues = new QAction(tr("Copy"), this);
actionCopyValues->setIcon(QIcon(getQPixmap("copy_xpm")));
connect(actionCopyValues, SIGNAL(activated()), this, SLOT(copyValues()));
actionCopyColumnToTable = new QAction(this);
actionCopyColumnToTable->setIcon(QIcon(getQPixmap("table_xpm")));
connect(actionCopyColumnToTable, SIGNAL(activated()), this, SLOT(copyColumnToTable()));
actionCopyColumnToGraph = new QAction(this);
actionCopyColumnToGraph->setIcon(QIcon(getQPixmap("graph_xpm")));
connect(actionCopyColumnToGraph, SIGNAL(activated()), this, SLOT(copyColumnToGraph()));
actionCopyColumnToGraphErr = new QAction(this);
actionCopyColumnToGraphErr->setIcon(QIcon(getQPixmap("graph_xpm")));
connect(actionCopyColumnToGraphErr, SIGNAL(activated()), this, SLOT(copyColumnToGraphErr()));
connect(this,SIGNAL(needToCreateLoadDAEMantidMatrix(const QString&)),this,SLOT(createLoadDAEMantidMatrix(const QString&)));
connect(this,SIGNAL(needToShowCritical(const QString&)),this,SLOT(showCritical(const QString&)));
m_algMonitor = new AlgorithmMonitor(this);
connect(m_algMonitor,SIGNAL(algorithmStarted(void*)),m_exploreAlgorithms,SLOT(algorithmStarted(void*)), Qt::QueuedConnection);
connect(m_algMonitor,SIGNAL(algorithmFinished(void*)),m_exploreAlgorithms,SLOT(algorithmFinished(void*)), Qt::QueuedConnection);
connect(m_algMonitor,SIGNAL(needUpdateProgress(void*,double, const QString&, double, int)),
m_exploreAlgorithms,SLOT(updateProgress(void*,double, const QString&, double, int)), Qt::QueuedConnection);
m_algMonitor->start();
mantidMenu = new QMenu(m_appWindow);
mantidMenu->setObjectName("mantidMenu");
// for activating the keyboard shortcut for Clear All Memory even if no clciking on Mantid Menu
// Ticket #672
//connect(mantidMenu, SIGNAL(aboutToShow()), this, SLOT(mantidMenuAboutToShow()));
mantidMenuAboutToShow();
menuMantidMatrix = new QMenu(m_appWindow);
connect(menuMantidMatrix, SIGNAL(aboutToShow()), this, SLOT(menuMantidMatrixAboutToShow()));
init();
}
// Should it be moved to the constructor?
void MantidUI::init()
{
AnalysisDataServiceImpl &dataStore = AnalysisDataService::Instance();
dataStore.notificationCenter.addObserver(m_addObserver);
dataStore.notificationCenter.addObserver(m_replaceObserver);
dataStore.notificationCenter.addObserver(m_deleteObserver);
dataStore.notificationCenter.addObserver(m_clearADSObserver);
dataStore.notificationCenter.addObserver(m_renameObserver);
dataStore.notificationCenter.addObserver(m_groupworkspacesObserver);
dataStore.notificationCenter.addObserver(m_ungroupworkspaceObserver);
dataStore.notificationCenter.addObserver(m_workspaceGroupUpdateObserver);
Mantid::Kernel::ConfigService::Instance().addObserver(m_configServiceObserver);
m_exploreAlgorithms->update();
try
{
m_defaultFitFunction = new MantidQt::MantidWidgets::FitPropertyBrowser(m_appWindow, this);
m_defaultFitFunction->init();
// this make the progress bar work with Fit algorithm running form the fit browser
connect(m_defaultFitFunction,SIGNAL(executeFit(QString,QHash<QString,QString>,Mantid::API::AlgorithmObserver*)),
this,SLOT(showAlgorithmDialog(QString,QHash<QString,QString>,Mantid::API::AlgorithmObserver*)));
m_defaultFitFunction->hide();
m_appWindow->addDockWidget( Qt::LeftDockWidgetArea, m_defaultFitFunction );
m_fitFunction = m_defaultFitFunction;
}
catch(...)
{
m_defaultFitFunction = NULL;
m_fitFunction = NULL;
showCritical("The curve fitting plugin is missing");
}
}
/// Slot: Receives a new X range from a FitPropertyBrowser and re-emits it.
void MantidUI::x_range_from_picker(double xmin, double xmax)
{
emit x_range_update(xmin, xmax);
}
/// Updates the algorithms tree as this may have changed
void MantidUI::updateAlgorithms()
{
m_exploreAlgorithms->update();
}
/// Show / hide the AlgorithmDockWidget
void MantidUI::showAlgWidget(bool on)
{
if (on)
{
m_exploreAlgorithms->show();
}
else
{
m_exploreAlgorithms->hide();
}
}
void MantidUI::addMenuItems(QMenu *menu)
{
actionToggleMantid = m_exploreMantid->toggleViewAction();
actionToggleMantid->setIcon(getQPixmap("mantid_matrix_xpm"));
actionToggleMantid->setShortcut( tr("Ctrl+Shift+M") );
menu->addAction(actionToggleMantid);
actionToggleAlgorithms = m_exploreAlgorithms->toggleViewAction();
actionToggleAlgorithms->setShortcut( tr("Ctrl+Shift+A") );
menu->addAction(actionToggleAlgorithms);
if (m_fitFunction)
{
actionToggleFitFunction = m_fitFunction->toggleViewAction();
menu->addAction(actionToggleFitFunction);
}
}
// Show / hide the FitPropertyBrowser
void MantidUI::showFitPropertyBrowser(bool on)
{
if (!m_fitFunction) return;
if (on)
{
m_fitFunction->show();
}
else
{
m_fitFunction->hide();
}
}
/**
* Be careful where this is called, if it is a called too late in the Qt shutdown the application
* crashes
*/
void MantidUI::shutdown()
{
g_log.notice("MantidPlot is shutting down...");
// First we need to cancel any running algorithms otherwise bad things can happen if they call
// the logging framework after it's been shutdown. The cancel calls within cancelAll are not
// blocking, hence the loop to make sure they're all done before moving on. (N.B. Tried copying
// the wait/exit/wait business from the AlgorithmMonitor dtor, but that gave occasional crashes.)
if ( m_algMonitor )
{
m_algMonitor->cancelAll();
while ( m_algMonitor->count() > 0 )
{
Poco::Thread::sleep(100);
}
}
Mantid::API::FrameworkManager::Instance().clear();
}
MantidUI::~MantidUI()
{
delete m_algMonitor;
Mantid::Kernel::ConfigService::Instance().removeObserver(m_configServiceObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_groupworkspacesObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_ungroupworkspaceObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_workspaceGroupUpdateObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_addObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_deleteObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_clearADSObserver);
delete m_fitFunction;
}
void MantidUI::saveSettings() const
{
// Save algorithm dialog input
MantidQt::API::AlgorithmInputHistory::Instance().save();
}
QStringList MantidUI::getWorkspaceNames()
{
QStringList sl;
std::set<std::string> sv = Mantid::API::AnalysisDataService::Instance().getObjectNames();
for (std::set<std::string>::const_iterator it = sv.begin(); it != sv.end(); ++it)
sl<<QString::fromStdString(*it);
return sl;
}
QStringList MantidUI::getAlgorithmNames()
{
QStringList sl;
std::vector<std::string> sv;
sv = Mantid::API::AlgorithmFactory::Instance().getKeys();
for(size_t i=0;i<sv.size();i++)
sl<<QString::fromStdString(sv[i]);
return sl;
}
/**
* Returns the number of algorithms currently executing
*/
int MantidUI::runningAlgCount() const
{
return m_algMonitor->count();
}
/**
* Ticket #678
*/
void MantidUI::saveNexusWorkspace()
{
executeSaveNexus();
}
/**
* DeleteWorkspace
@param workspaceName :: Name of the workspace to delete
*/
void MantidUI::deleteWorkspace(const QString& workspaceName)
{
auto alg = createAlgorithm("DeleteWorkspace");
alg->setLogging(false);
alg->setPropertyValue("Workspace",workspaceName.toStdString());
executeAlgorithmAsync(alg);
}
/**
getSelectedWorkspaceName
*/
QString MantidUI::getSelectedWorkspaceName()
{
QString str = m_exploreMantid->getSelectedWorkspaceName();
if ( str.isEmpty() )
{
//Check if a mantid matrix is selected
MantidMatrix *m = qobject_cast<MantidMatrix*>(appWindow()->activeWindow());
if( !m ) return "";
str = m->workspaceName();
}
return str;
}
Mantid::API::Workspace_const_sptr MantidUI::getSelectedWorkspace()
{
return m_exploreMantid->getSelectedWorkspace();
}
Mantid::API::Workspace_const_sptr MantidUI::getWorkspace(const QString& workspaceName)
{
if (AnalysisDataService::Instance().doesExist(workspaceName.toStdString()))
{
return AnalysisDataService::Instance().retrieve(workspaceName.toStdString());
}
Workspace_sptr empty;
return empty;//??
}
/** Extension to ApplicationWindow::menuAboutToShow() to deal with Mantid.
*/
bool MantidUI::menuAboutToShow(MdiSubWindow *w)
{
if (w && w->isA("MantidMatrix"))
{
appWindow()->myMenuBar()->insertItem(tr("3D &Plot"), appWindow()->plot3DMenu);
appWindow()->actionCopySelection->setEnabled(true);
appWindow()->actionPasteSelection->setEnabled(false);
appWindow()->actionClearSelection->setEnabled(false);
appWindow()->myMenuBar()->insertItem(tr("&Workspace"),menuMantidMatrix);
return true;
}
return false;
}
Graph3D *MantidUI::plot3DMatrix(int style)
{
MdiSubWindow *w = appWindow()->activeWindow();
if (w->isA("MantidMatrix"))
{
return static_cast<MantidMatrix*>(w)->plotGraph3D(style);
}
return 0;
}
MultiLayer* MantidUI::plotSpectrogram(Graph::CurveType type)
{
MantidMatrix *m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (m)
{
return drawSingleColorFillPlot(QString::fromStdString(m->getWorkspaceName()), type);
}
return 0;
}
/** Import a MatrixWorkspace into a MantidMatrix.
@param wsName :: Workspace name
@param lower :: An optional lower boundary
@param upper :: An optional upper boundary
@param showDlg :: If true show a dialog box to set some import parameters
@param makeVisible :: If true show the created MantidMatrix, hide otherwise.
@return A pointer to the new MantidMatrix.
*/
MantidMatrix* MantidUI::importMatrixWorkspace(const QString& wsName, int lower, int upper, bool showDlg, bool makeVisible)
{
MatrixWorkspace_sptr ws;
if (AnalysisDataService::Instance().doesExist(wsName.toStdString()))
{
ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName.toStdString());
}
if (!ws.get()) return 0;
MantidMatrix* w = 0;
if (showDlg)
{
ImportWorkspaceDlg dlg(appWindow(), ws->getNumberHistograms());
if (dlg.exec() == QDialog::Accepted)
{
int start = dlg.getLowerLimit();
int end = dlg.getUpperLimit();
w = new MantidMatrix(ws, appWindow(), "Mantid",wsName, start, end );
if (dlg.isFiltered())
w->setRange(0,dlg.getMaxValue());
}
}
else
{
w = new MantidMatrix(ws, appWindow(), "Mantid",wsName, lower, upper);
}
if ( !w ) return 0;
appWindow()->addMdiSubWindow(w,makeVisible);
return w;
}
/** Import a Workspace into MantidPlot.
@param wsName :: Workspace name
@param showDlg :: If true show a dialog box to set some import parameters
@param makeVisible :: If true show the created widget, hide otherwise.
*/
void MantidUI::importWorkspace(const QString& wsName, bool showDlg, bool makeVisible)
{
MantidMatrix* mm = importMatrixWorkspace(wsName,-1, -1, showDlg,makeVisible);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (!mm)
{
importTableWorkspace(wsName,showDlg,makeVisible);
}
QApplication::restoreOverrideCursor();
}
/** Import the selected workspace, if any. Displays the import dialog.
*/
void MantidUI::importWorkspace()
{
QString wsName = getSelectedWorkspaceName();
importWorkspace(wsName,true,true);
}
/** Import the selected table workspace transposed.
*/
void MantidUI::importTransposed()
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QString wsName = getSelectedWorkspaceName();
ITableWorkspace_sptr ws;
if (AnalysisDataService::Instance().doesExist(wsName.toStdString()))
{
ws = AnalysisDataService::Instance().retrieveWS<ITableWorkspace>(wsName.toStdString());
importTableWorkspace(wsName,true,true,true);
}
QApplication::restoreOverrideCursor();
}
/** Create a TableWorkspace of box data from the MDEventWorkspace
*/
void MantidUI::importBoxDataTable()
{
std::cout << "MantidUI::importBoxDataTable()" << std::endl;
QString wsName = getSelectedWorkspaceName();
try
{
// Get the MD event table
IMDEventWorkspace_sptr ws = boost::dynamic_pointer_cast<IMDEventWorkspace>(
AnalysisDataService::Instance().retrieve( wsName.toStdString()) );
if (!ws) return;
ITableWorkspace_sptr tabWs = ws->makeBoxTable(0,0);
if (!tabWs) return;
std::string tableName = wsName.toStdString() + std::string("_boxdata");
AnalysisDataService::Instance().addOrReplace(tableName, tabWs);
// Now show that table
importWorkspace(QString::fromStdString(tableName), true, true);
}
catch (...)
{
}
}
/** Plots a Curve showing intensities for a MDWorkspace.
* But only if the workspace meets certain criteria, such as
* having only one non-integrated dimension. Should exit gracefully otherwise.
*/
void MantidUI::showMDPlot()
{
QString wsName = getSelectedWorkspaceName();
// Create a dialog to ask for options
MantidMDCurveDialog dlg(appWindow(), wsName);
if ( dlg.exec() == QDialog::Rejected )
return;
// Extract the settings from the dialog opened earlier
bool showErrors = dlg.showErrorBars();
LinePlotOptions * opts = dlg.getLineOptionsWidget();
QStringList all;
all << wsName;
plotMDList(all, opts->getPlotAxis(), opts->getNormalization(), showErrors);
}
/**
* Plots a curve showing intensities for MDWorkspaces
* @param wsNames : Names of the workspaces to plot
* @param plotAxis : Axis number to plot
* @param normalization: Normalization option to use
* @param showErrors: True if errors are to be show
* @param plotWindow :: Window to use for plotting. If NULL a new one will be created
* @param clearWindow :: Whether to clean the plotWindow before plotting.Ignored if plotWindow == NULL
* @return NULL if failure. Otherwise, if plotWindow == NULL - created window, if not NULL - plotWindow
*/
MultiLayer* MantidUI::plotMDList(const QStringList& wsNames, const int plotAxis,
const Mantid::API::MDNormalization normalization, const bool showErrors, MultiLayer* plotWindow,
bool clearWindow)
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
auto firstName = wsNames.at(0);
bool isGraphNew = false;
MultiLayer* ml = appWindow()->prepareMultiLayer(isGraphNew, plotWindow, firstName, clearWindow);
Graph *g = ml->activeGraph();
try
{
for (int i = 0; i < wsNames.size(); ++i)
{
// Create the curve with defaults
auto wsName = wsNames.at(i);
MantidMDCurve* curve = new MantidMDCurve(wsName, g, showErrors);
MantidQwtIMDWorkspaceData * data = curve->mantidData();
// Apply the settings
data->setPreviewMode(false);
data->setPlotAxisChoice(plotAxis);
data->setNormalization(normalization);
// Using information from the first graph
if( i == 0 && isGraphNew )
{
g->setXAxisTitle(data->getXAxisLabel());
g->setYAxisTitle(data->getYAxisLabel());
g->setAutoScale();
}
}
}
catch (std::invalid_argument &e)
{
g_log.warning() << e.what() << std::endl;
}
catch (std::runtime_error &e)
{
g_log.warning() << e.what() << std::endl;
}
catch (...)
{}
if(!isGraphNew)
// Replot graph is we've added curves to existing one
g->replot();
// Check if window does not contain any curves and should be closed
ml->maybeNeedToClose();
QApplication::restoreOverrideCursor();
return ml;
}
/*
Generates a table workspace from a md workspace and pulls up
a grid to display the results.
*/
void MantidUI::showListData()
{
QString wsName = getSelectedWorkspaceName();
QString tableWsName = wsName + "_data_list_table";
Mantid::API::IAlgorithm_sptr queryWorkspace = this->createAlgorithm("QueryMDWorkspace");
queryWorkspace->initialize();
queryWorkspace->setPropertyValue("InputWorkspace", wsName.toStdString());
std::string sTableWorkspaceName=tableWsName.toStdString();
queryWorkspace->setPropertyValue("OutputWorkspace", sTableWorkspaceName);
queryWorkspace->setProperty("LimitRows", false);
queryWorkspace->execute();
importWorkspace(tableWsName);
}
void MantidUI::showVatesSimpleInterface()
{
QString wsName = getSelectedWorkspaceName();
try
{
IMDEventWorkspace_sptr mdews = boost::dynamic_pointer_cast<IMDEventWorkspace>(
AnalysisDataService::Instance().retrieve( wsName.toStdString()) );
IPeaksWorkspace_sptr pws = boost::dynamic_pointer_cast<IPeaksWorkspace>(
AnalysisDataService::Instance().retrieve(wsName.toStdString()));
IMDHistoWorkspace_sptr mdhist = boost::dynamic_pointer_cast<IMDHistoWorkspace>(
AnalysisDataService::Instance().retrieve( wsName.toStdString()) );
if (!mdews && !pws && !mdhist)
{
return;
}
// Set the type of workspace, the GUI needs it
int wsType = MantidQt::API::VatesViewerInterface::MDEW;
if (pws)
{
wsType = MantidQt::API::VatesViewerInterface::PEAKS;
}
if (mdhist)
{
wsType = MantidQt::API::VatesViewerInterface::MDHW;
}
if (m_vatesSubWindow)
{
QWidget *vwidget = m_vatesSubWindow->widget();
vwidget->show();
qobject_cast<MantidQt::API::VatesViewerInterface *>(vwidget)->renderWorkspace(wsName, wsType);
return;
}
else
{
m_vatesSubWindow = new QMdiSubWindow;
m_vatesSubWindow->setAttribute(Qt::WA_DeleteOnClose, false);
QIcon icon; icon.addFile(QString::fromUtf8(":/VatesSimpleGuiViewWidgets/icons/pvIcon.png"), QSize(), QIcon::Normal, QIcon::Off);
m_vatesSubWindow->setWindowIcon(icon);
connect(m_appWindow, SIGNAL(shutting_down()), m_vatesSubWindow, SLOT(close()));
MantidQt::API::InterfaceManager interfaceManager;
MantidQt::API::VatesViewerInterface *vsui = interfaceManager.createVatesSimpleGui();
if (vsui)
{
connect(m_appWindow, SIGNAL(shutting_down()),
vsui, SLOT(shutdown()));
connect(vsui, SIGNAL(requestClose()), m_vatesSubWindow, SLOT(close()));
vsui->setParent(m_vatesSubWindow);
m_vatesSubWindow->setWindowTitle("Vates Simple Interface");
vsui->setupPluginMode();
//m_appWindow->setGeometry(m_vatesSubWindow, vsui);
m_vatesSubWindow->setWidget(vsui);
m_vatesSubWindow->widget()->show();
vsui->renderWorkspace(wsName, wsType);
}
else
{
delete m_vatesSubWindow;
m_vatesSubWindow = NULL;
return;
}
}
}
catch (std::runtime_error &e)
{
throw std::runtime_error(e);
}
catch (...)
{
}
}
void MantidUI::showSpectrumViewer()
{
QString wsName = getSelectedWorkspaceName();
try
{
MatrixWorkspace_sptr wksp = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve( wsName.toStdString()) );
if ( wksp )
{
MantidQt::SpectrumView::SpectrumView* viewer = new MantidQt::SpectrumView::SpectrumView(m_appWindow);
viewer->setAttribute(Qt::WA_DeleteOnClose, false);
viewer->resize( 1050, 800 );
connect(m_appWindow, SIGNAL(shutting_down()), viewer, SLOT(close()));
viewer->show();
viewer->renderWorkspace(wksp);
}
else
{
g_log.information() << "Only event or matrix workspaces are currently supported.\n"
<< "Please convert to one of these before using the ImageView.\n";
}
}
catch (std::runtime_error &e)
{
g_log.error() << e.what() << "\n";
throw std::runtime_error(e);
}
catch (...)
{
g_log.error() << "Image View: Exception getting workspace\n";
}
}
/** Create a window with a SliceViewer widget to show
* the selected workspace
*/
void MantidUI::showSliceViewer()
{
// Retrieve the MDWorkspace
QString wsName = getSelectedWorkspaceName();
IMDWorkspace_sptr mdws = boost::dynamic_pointer_cast<IMDWorkspace>(
AnalysisDataService::Instance().retrieve( wsName.toStdString()) );
MatrixWorkspace_sptr mw = boost::dynamic_pointer_cast<MatrixWorkspace>(mdws);
if (mdws)
{
// Create the slice viewer window
SliceViewerWindow * w = MantidQt::Factory::WidgetFactory::Instance()->
createSliceViewerWindow(wsName, "");
// Special options for viewing MatrixWorkspaces
if (mw)
{
w->getSlicer()->setTransparentZeros(false);
}
// Connect the MantidPlot close() event with the the window's close().
QObject::connect(appWindow(), SIGNAL(destroyed()), w, SLOT(close()));
// Pop up the window
w->show();
// And add it
//appWindow()->d_workspace->addSubWindow(w);
}
}
/** #539: For adding Workspace History display to MantidPlot
Show Algorithm History Details in a window .
*/
void MantidUI::showAlgorithmHistory()
{
QString wsName=getSelectedWorkspaceName();
Mantid::API::Workspace_const_sptr wsptr=getWorkspace(wsName);
if(NULL != wsptr)
{
// If the workspace has any AlgorithHistory ...
if(!wsptr->getHistory().empty())
{
// ... create and display the window.
AlgorithmHistoryWindow *palgHist = new AlgorithmHistoryWindow(m_appWindow,wsptr);
if(NULL != palgHist)
{
palgHist->show();
}
}
}
else
{
QMessageBox::information(appWindow(),"Mantid","Invalid WorkSpace");
return;
}
}
/** Create a new Table and fill it with the data from a Tableworkspace
@param wsName :: Workspace name
@param showDlg :: If true show a dialog box to set some import parameters
@param makeVisible :: If true show the created Table, hide otherwise.
@param transpose :: Transpose the table
@return A pointer to the new Table.
*/
Table* MantidUI::importTableWorkspace(const QString& wsName, bool, bool makeVisible, bool transpose)
{
ITableWorkspace_sptr ws;
if (AnalysisDataService::Instance().doesExist(wsName.toStdString()))
{
ws = AnalysisDataService::Instance().retrieveWS<ITableWorkspace>(wsName.toStdString());
}
if (!ws.get()) return 0;
if (ws->rowCount() == 0 || ws->columnCount() == 0)
{
showCritical("Cannot create an empty table");
return 0;
}
Table* t = new MantidTable(appWindow()->scriptingEnv(), ws, wsName, appWindow(), transpose);
if (makeVisible) t->showNormal();
else t->showMinimized();
return t;
}
void MantidUI::showContextMenu(QMenu& cm, MdiSubWindow* w)
{
if (w->isA("MantidMatrix"))
{
MantidMatrix * mm = dynamic_cast<MantidMatrix*>(w);
bool areSpectraSelected = mm->setSelectedRows();
bool areColumnsSelected = mm->setSelectedColumns();
cm.addAction(actionCopyValues);
if (areSpectraSelected) cm.addAction(actionCopyRowToTable);
if (areColumnsSelected) cm.addAction(actionCopyColumnToTable);
cm.addSeparator();
cm.addAction(actionCopyDetectorsToTable);
cm.addSeparator();
if (areSpectraSelected && mm->numCols() > 1)
{
// Enable the appropriate options
cm.addAction(actionCopyRowToGraph);
cm.addAction(actionCopyRowToGraphErr);
if (mm->getSelectedRows().size() > 1)
{
cm.addAction(actionWaterfallPlot);
}
}
if (areColumnsSelected && mm->numRows() > 1)
{
cm.addAction(actionCopyColumnToGraph);
cm.addAction(actionCopyColumnToGraphErr);
}
// Set the option texts to the correct plurality
if (mm->getSelectedRows().size() > 1)
{
actionCopyRowToTable->setText("Copy spectra to table");
actionCopyRowToGraph->setText("Plot spectra (values only)");
actionCopyRowToGraphErr->setText("Plot spectra (values + errors)");
}
else
{
actionCopyRowToTable->setText("Copy spectrum to table");
actionCopyRowToGraph->setText("Plot spectrum (values only)");
actionCopyRowToGraphErr->setText("Plot spectrum (values + errors)");
}
if (mm->getSelectedColumns().size() > 1)
{
actionCopyColumnToTable->setText("Copy bins to table");
actionCopyColumnToGraph->setText("Plot bins (values only)");
actionCopyColumnToGraphErr->setText("Plot bins (values + errors)");
}
else
{
actionCopyColumnToTable->setText("Copy bin to table");
actionCopyColumnToGraph->setText("Plot bin (values only)");
actionCopyColumnToGraphErr->setText("Plot bin (values + errors)");
}
}
}
void MantidUI::copyRowToTable()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
Table* t = createTableFromSelectedRows(m,true,true);
t->showNormal();
}
void MantidUI::copyColumnToTable()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
Table* t = createTableFromSelectedColumns(m,true);
t->showNormal();
}
void MantidUI::copyRowToGraph()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
plotSelectedRows(m,false);
}
void MantidUI::copyColumnToGraph()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
plotSelectedColumns(m,false);
}
void MantidUI::copyColumnToGraphErr()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
plotSelectedColumns(m,true);
}
void MantidUI::copyRowToGraphErr()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
plotSelectedRows(m,true);
}
void MantidUI::copyRowsToWaterfall()
{
const MantidMatrix* const m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
MultiLayer* ml = plotSelectedRows(m,false);
if (ml) convertToWaterfall(ml);
}
void MantidUI::plotWholeAsWaterfall()
{
const MantidMatrix* const m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());
if (!m || !m->isA("MantidMatrix")) return;
MultiLayer* ml = plotSpectraRange(m->workspaceName(),0,m->numRows()-1,false);
if (ml) convertToWaterfall(ml);
}
void MantidUI::convertToWaterfall(MultiLayer* ml)
{
ml->hide();
ml->activeGraph()->setWaterfallOffset(10,20);
ml->setWaterfallLayout();
// Next two lines replace the legend so that it works on reversing the curve order
ml->activeGraph()->removeLegend();
ml->activeGraph()->newLegend();
ml->show();
}
void MantidUI::copyDetectorsToTable()
{
MantidMatrix* m = dynamic_cast<MantidMatrix*>(appWindow()->activeWindow());