-
Notifications
You must be signed in to change notification settings - Fork 122
/
PeaksTableControllerVsi.cpp
614 lines (541 loc) · 20.6 KB
/
PeaksTableControllerVsi.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
#include "MantidVatesSimpleGuiViewWidgets/PeaksTableControllerVsi.h"
#include "MantidVatesSimpleGuiViewWidgets/PeaksTabWidget.h"
#include "MantidVatesSimpleGuiViewWidgets/CameraManager.h"
#include "MantidAPI/IPeaksWorkspace.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/PeakTransformHKL.h"
#include "MantidAPI/PeakTransformQSample.h"
#include "MantidAPI/PeakTransformQLab.h"
#include "MantidKernel/V3D.h"
#include "MantidKernel/SpecialCoordinateSystem.h"
#include "MantidKernel/Logger.h"
#include "MantidVatesAPI/PeaksPresenterVsi.h"
#include "MantidVatesAPI/NullPeaksPresenterVsi.h"
#include "MantidVatesAPI/ConcretePeaksPresenterVsi.h"
#include "MantidVatesAPI/CompositePeaksPresenterVsi.h"
#include "MantidQtSliceViewer/PeakPalette.h"
#include "MantidQtAPI/PlotAxis.h"
#include "MantidGeometry/MDGeometry/IMDDimension.h"
// Have to deal with ParaView warnings and Intel compiler the hard way.
#if defined(__INTEL_COMPILER)
#pragma warning disable 1170
#endif
#include <pqActiveObjects.h>
#include <pqApplicationCore.h>
#include <pqObjectBuilder.h>
#include <pqPipelineSource.h>
#include <pqPipelineFilter.h>
#include <pqPipelineRepresentation.h>
#include <pqRenderView.h>
#include <pqServer.h>
#include <pqServerManagerModel.h>
#include <vtkSMSourceProxy.h>
#include <vtkSMPropertyHelper.h>
#include <vtkSMProxy.h>
#include <vtkSMDoubleVectorProperty.h>
#if defined(__INTEL_COMPILER)
#pragma warning enable 1170
#endif
#include <QString>
#include <QPointer>
#include <QVBoxLayout>
#include <QLayout>
#include <QLayoutItem>
#include <QColor>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <stdexcept>
#include <algorithm>
#include <map>
#include <sstream>
namespace Mantid {
namespace Vates {
namespace SimpleGui {
namespace {
Mantid::Kernel::Logger g_log("PeakViewerVsi");
}
/**
* Constructor
* @param cameraManager A cameraManager pointer.
* @param parent A pointer to a QWidget parent.
*/
PeaksTableControllerVsi::PeaksTableControllerVsi(
boost::shared_ptr<CameraManager> cameraManager, QWidget *parent)
: QWidget(parent), m_cameraManager(cameraManager),
m_presenter(new Mantid::VATES::CompositePeaksPresenterVsi()),
m_peaksTabWidget(NULL), m_peakMarker(NULL), m_coordinateSystem(Mantid::Kernel::SpecialCoordinateSystem::QLab) {
m_peakTransformSelector.registerCandidate(
boost::make_shared<Mantid::API::PeakTransformHKLFactory>());
m_peakTransformSelector.registerCandidate(
boost::make_shared<Mantid::API::PeakTransformQSampleFactory>());
m_peakTransformSelector.registerCandidate(
boost::make_shared<Mantid::API::PeakTransformQLabFactory>());
}
PeaksTableControllerVsi::~PeaksTableControllerVsi() {
destroySinglePeakSource();
}
/**
* Check for viewable peaks.
* @returns A vector of the peak indices which are visible and which are not
* visible.
*/
std::vector<bool> PeaksTableControllerVsi::getViewablePeaks() {
std::vector<bool> viewablePeaks;
if (m_presenter) {
// Get the up to date area
updateViewableArea();
// Get a list with viewable peak coordinates
try {
viewablePeaks = m_presenter->getViewablePeaks();
} catch (...) {
g_log.warning() << "The viewable peaks could not be retrieved. \n";
}
}
return viewablePeaks;
}
/**
* Add a new workspace
* @param source A new peaks source
* @param splatSource A pointer to the splatter source
*/
void PeaksTableControllerVsi::addWorkspace(
pqPipelineSource *source, QPointer<pqPipelineSource> splatSource) {
try {
if (!source || !splatSource) {
throw std::invalid_argument(
"The pqPipelineSource of the peaks workspace does not exist.");
}
// Get the pointer to the peaks workspace
std::string wsName(vtkSMPropertyHelper(source->getProxy(), "WorkspaceName",
true).GetAsString());
std::string peaksFrame(vtkSMPropertyHelper(source->getProxy(),
"Peak Dimensions",
true).GetAsString());
// Get dimensions from splattersource
std::vector<std::string> dimInfo = extractFrameFromSource(splatSource);
if (dimInfo.size() < 2) {
throw std::invalid_argument(
"The workspace needs to have at least two dimensions");
}
std::string dimCompare = dimInfo[0];
std::transform(dimCompare.begin(), dimCompare.end(), dimCompare.begin(),
::toupper);
std::transform(peaksFrame.begin(), peaksFrame.end(), peaksFrame.begin(),
::toupper);
// Check if frames match
if (dimCompare.find(peaksFrame) == std::string::npos) {
throw std::runtime_error("The workspaces do not match.");
}
Mantid::API::IPeaksWorkspace_sptr peaksWorkspace =
Mantid::API::AnalysisDataService::Instance()
.retrieveWS<Mantid::API::IPeaksWorkspace>(wsName);
Mantid::API::PeakTransformFactory_sptr transformFactory =
m_peakTransformSelector.makeChoice(dimInfo[0], dimInfo[1]);
Mantid::API::PeakTransform_sptr transform =
transformFactory->createTransform(dimInfo[0], dimInfo[1]);
std::string frame = transform->getFriendlyName();
m_presenter->addPresenter(
boost::make_shared<Mantid::VATES::ConcretePeaksPresenterVsi>(
peaksWorkspace, m_cameraManager->getCurrentViewFrustum(), frame));
// If the tab widget is visible, then update it
if (m_peaksTabWidget) {
std::map<std::string, std::vector<bool>> viewablePeaks =
m_presenter->getInitializedViewablePeaks();
m_peaksTabWidget->addNewPeaksWorkspace(
peaksWorkspace, viewablePeaks[peaksWorkspace->getName()]);
m_peaksTabWidget->updateTabs(viewablePeaks, getColors());
updatePeakWorkspaceColor();
}
} catch (Mantid::Kernel::Exception::NotFoundError &) {
g_log.warning() << "Could not retrieve the peaks workspace.\n";
throw;
} catch (std::invalid_argument &ex) {
g_log.warning() << ex.what();
throw;
} catch (std::runtime_error &ex) {
g_log.warning() << ex.what();
throw;
}
}
/**
* Get the colors for the tabs
* @returns A map of peak workspace names and colors
*/
std::map<std::string, QColor> PeaksTableControllerVsi::getColors() {
std::map<std::string, QColor> colors;
std::vector<Mantid::API::IPeaksWorkspace_sptr> peakWs =
m_presenter->getPeaksWorkspaces();
for (std::vector<Mantid::API::IPeaksWorkspace_sptr>::iterator it =
peakWs.begin();
it != peakWs.end(); ++it) {
const int pos = static_cast<int>(std::distance(peakWs.begin(), it));
QColor color = m_peakPalette.foregroundIndexToColour(pos);
const std::string name = (*it)->getName();
colors.insert(std::pair<std::string, QColor>(name, color));
}
return colors;
}
/**
* Update the color of the peak workspace representation.
*/
void PeaksTableControllerVsi::updatePeakWorkspaceColor() {
std::vector<Mantid::API::IPeaksWorkspace_sptr> peakWs =
m_presenter->getPeaksWorkspaces();
for (std::vector<Mantid::API::IPeaksWorkspace_sptr>::iterator it =
peakWs.begin();
it != peakWs.end(); ++it) {
const int pos = static_cast<int>(std::distance(peakWs.begin(), it));
QColor color = m_peakPalette.foregroundIndexToColour(pos);
const std::string name = (*it)->getName();
// Find the source whcih is associated with the peak workspace and change
// its representation
pqServer *server = pqActiveObjects::instance().activeServer();
pqServerManagerModel *smModel =
pqApplicationCore::instance()->getServerManagerModel();
QList<pqPipelineSource *> sources =
smModel->findItems<pqPipelineSource *>(server);
for (QList<pqPipelineSource *>::iterator src = sources.begin();
src != sources.end(); ++src) {
// Make sure that the source is a peak workspace
std::string xmlName((*src)->getProxy()->GetXMLName());
if ((xmlName.find("Peaks Source") != std::string::npos)) {
std::string workspaceName(
vtkSMPropertyHelper((*src)->getProxy(), "WorkspaceName")
.GetAsString());
if (workspaceName == name) {
int r = color.red();
int g = color.green();
int b = color.blue();
double red = static_cast<double>(r) / 255.0;
double green = static_cast<double>(g) / 255.0;
double blue = static_cast<double>(b) / 255.0;
pqDataRepresentation *rep =
(*src)
->getRepresentation(pqActiveObjects::instance().activeView());
pqPipelineRepresentation *pipelineRepresentation =
qobject_cast<pqPipelineRepresentation *>(rep);
pipelineRepresentation->getProxy()->UpdatePropertyInformation();
vtkSMDoubleVectorProperty *prop =
vtkSMDoubleVectorProperty::SafeDownCast(
pipelineRepresentation->getProxy()->GetProperty(
"AmbientColor"));
prop->SetElement(0, red);
prop->SetElement(1, green);
prop->SetElement(2, blue);
pipelineRepresentation->getProxy()->UpdateVTKObjects();
pipelineRepresentation->updateHelperProxies();
pqActiveObjects::instance().activeView()->forceRender();
break;
}
}
}
}
}
/**
* Update the view region for the presenters
*/
void PeaksTableControllerVsi::updateViewableArea() {
Mantid::VATES::ViewFrustum frustum = m_cameraManager->getCurrentViewFrustum();
m_presenter->updateViewFrustum(frustum);
}
/**
* Extract the frame from the source
* @param splatSource A pointer to a splatter plot source.
*/
std::vector<std::string> PeaksTableControllerVsi::extractFrameFromSource(
QPointer<pqPipelineSource> splatSource) {
pqPipelineFilter *filter = qobject_cast<pqPipelineFilter *>(splatSource);
if (!filter) {
throw std::invalid_argument("The splatter source is not a filter.");
}
// Check the original source
pqPipelineSource *originalSource = filter->getInput(0);
if (!originalSource) {
throw std::invalid_argument("The original source cannot be found.");
}
std::string xmlName(originalSource->getProxy()->GetXMLName());
if (!(xmlName.find("MDEW") != std::string::npos)) {
throw std::invalid_argument("The original source cannot be found.");
}
std::string wsName(vtkSMPropertyHelper(originalSource->getProxy(),
"WorkspaceName", true).GetAsString());
Mantid::API::IMDEventWorkspace_sptr eventWorkspace =
Mantid::API::AnalysisDataService::Instance()
.retrieveWS<Mantid::API::IMDEventWorkspace>(wsName);
std::vector<std::string> dimensionInfo;
for (size_t i = 0; i < eventWorkspace->getNumDims(); i++) {
dimensionInfo.push_back(
MantidQt::API::PlotAxis(*(eventWorkspace->getDimension(i)))
.title()
.toStdString());
}
// Update the current coordiate system
m_coordinateSystem = eventWorkspace->getSpecialCoordinateSystem();
return dimensionInfo;
}
/**
* Check if the peaks viewer has a peaks workspace loaded.
* @returns If the a peaks workspace is loaded.
*/
bool PeaksTableControllerVsi::hasPeaks() {
if (!m_presenter || !m_presenter->hasPeaks()) {
return false;
} else {
return true;
}
}
/**
* Show all peaks in the table.
*/
void PeaksTableControllerVsi::showFullTable() { createTable(); }
/**
* Create the table
*/
void PeaksTableControllerVsi::createTable() {
// Create the table if it does not exist
if (hasPeaks()) {
if (layout()) {
removeLayout(this);
}
// Create new widget
try {
// Set the layout of the table
this->setLayout(new QVBoxLayout);
PeaksTabWidget *widget = new PeaksTabWidget(
m_presenter->getPeaksWorkspaces(), m_presenter->getFrame(), this);
QObject::connect(
widget, SIGNAL(zoomToPeak(Mantid::API::IPeaksWorkspace_sptr, int)),
this, SLOT(onZoomToPeak(Mantid::API::IPeaksWorkspace_sptr, int)));
QObject::connect(
widget, SIGNAL(sortPeaks(const std::string &, const bool,
Mantid::API::IPeaksWorkspace_sptr)),
this, SLOT(onPeaksSorted(const std::string &, const bool,
Mantid::API::IPeaksWorkspace_sptr)));
// Initialize the viewablePeaks to be true
std::map<std::string, std::vector<bool>> viewablePeaks =
m_presenter->getInitializedViewablePeaks();
widget->setupMvc(viewablePeaks);
layout()->addWidget(widget);
m_peaksTabWidget = widget;
// Set the color
m_peaksTabWidget->updateTabs(viewablePeaks, getColors());
updatePeakWorkspaceColor();
} catch (std::runtime_error &ex) {
g_log.warning()
<< "Could not setup the the peaks widget for the splatterplot: "
<< ex.what() << "\n";
} catch (...) {
g_log.warning()
<< "Could not setup the the peaks widget for the splatterplot.\n";
}
}
}
/**
* Remove the layout
* @param widget
*/
void PeaksTableControllerVsi::removeLayout(QWidget *widget) {
QLayout *layout = widget->layout();
if (layout != 0) {
QLayoutItem *item;
while ((item = layout->takeAt(0)) != 0) {
layout->removeItem(item);
delete item->widget();
}
delete layout;
}
}
/**
* Remove the table.
*/
void PeaksTableControllerVsi::removeTable() {
destroySinglePeakSource();
if (m_peaksTabWidget) {
m_peaksTabWidget->deleteLater();
}
m_peaksTabWidget = NULL;
}
/**
* Zoom to a specific peak
* @param peaksWorkspace The peaksworkspace which is currently being displayed.
* @param row The selected row.
*/
void PeaksTableControllerVsi::onZoomToPeak(
Mantid::API::IPeaksWorkspace_sptr peaksWorkspace, int row) {
try {
double radius;
Mantid::Kernel::V3D position;
m_presenter->getPeaksInfo(peaksWorkspace, row, position, radius, m_coordinateSystem);
// Reset camera
m_cameraManager->setCameraToPeak(position[0], position[1], position[2],
radius);
// Place a marker glyph at the position
if (!m_peakMarker) {
generateSinglePeaksSource(position[0], position[1], position[2], radius);
} else {
resetSinglePeaksSource(position[0], position[1], position[2], radius);
}
emit setRotationToPoint(position[0], position[1], position[2]);
} catch (std::invalid_argument &ex) {
g_log.warning() << ex.what();
emit setRotationToPoint(0.0, 0.0, 0.0);
}
}
/**
* Generate a single peak glyph
* @param position1 Position 1 of the glyph.
* @param position2 Position 2 of the glyph.
* @param position3 Position 3 of the glyph.
* @param radius The radius of the peak.
*/
void PeaksTableControllerVsi::generateSinglePeaksSource(double position1,
double position2,
double position3,
double radius) {
// Create the source from the plugin
pqObjectBuilder *builder = pqApplicationCore::instance()->getObjectBuilder();
pqServer *server = pqActiveObjects::instance().activeServer();
pqPipelineSource *src =
builder->createSource("sources", "SinglePeakMarkerSource", server);
vtkSMPropertyHelper(src->getProxy(), "Position1").Set(position1);
vtkSMPropertyHelper(src->getProxy(), "Position2").Set(position2);
vtkSMPropertyHelper(src->getProxy(), "Position3").Set(position3);
vtkSMPropertyHelper(src->getProxy(), "RadiusMarker").Set(radius);
vtkSMSourceProxy *srcProxy = vtkSMSourceProxy::SafeDownCast(src->getProxy());
srcProxy->UpdateVTKObjects();
srcProxy->Modified();
srcProxy->UpdatePipelineInformation();
src->updatePipeline();
pqDataRepresentation *drep = builder->createDataRepresentation(
src->getOutputPort(0), pqActiveObjects::instance().activeView());
vtkSMPropertyHelper(drep->getProxy(), "Representation").Set("Surface");
srcProxy->UpdateVTKObjects();
srcProxy->Modified();
srcProxy->UpdatePipelineInformation();
src->updatePipeline();
pqActiveObjects::instance().activeView()->forceRender();
m_peakMarker = src;
// We need to make sure we detect when the source is destroyed, as the user
// can delete it in the pipeline browser
QObject::connect(m_peakMarker, SIGNAL(destroyed()), this,
SLOT(onPeakMarkerDestroyed()));
}
/**
* Destroy a single peaks source.
*/
void PeaksTableControllerVsi::destroySinglePeakSource() {
if (m_peakMarker) {
pqObjectBuilder *builder =
pqApplicationCore::instance()->getObjectBuilder();
builder->destroy(m_peakMarker);
m_peakMarker = NULL;
}
}
/**
* On Single Peak Marker destroyed
*/
void PeaksTableControllerVsi::onPeakMarkerDestroyed() { m_peakMarker = NULL; }
/**
* Reset the single peak source
* @param position1 Position 1 of the glyph.
* @param position2 Position 2 of the glyph.
* @param position3 Position 3 of the glyph.
* @param radius The radius of the peak.
*/
void PeaksTableControllerVsi::resetSinglePeaksSource(double position1,
double position2,
double position3,
double radius) {
vtkSMPropertyHelper(m_peakMarker->getProxy(), "Position1").Set(position1);
vtkSMPropertyHelper(m_peakMarker->getProxy(), "Position2").Set(position2);
vtkSMPropertyHelper(m_peakMarker->getProxy(), "Position3").Set(position3);
vtkSMPropertyHelper(m_peakMarker->getProxy(), "RadiusMarker").Set(radius);
vtkSMSourceProxy *srcProxy =
vtkSMSourceProxy::SafeDownCast(m_peakMarker->getProxy());
srcProxy->UpdateVTKObjects();
srcProxy->Modified();
srcProxy->UpdatePipelineInformation();
m_peakMarker->updatePipeline();
pqActiveObjects::instance().activeView()->forceRender();
}
/**
* Get the workspace names as a concatenated string
* @param delimiter The delimiter to concatenate workspace names.
* @returns The concatenated workspace names.
*/
std::string
PeaksTableControllerVsi::getConcatenatedWorkspaceNames(std::string delimiter) {
std::vector<std::string> peaksWorkspaceNames =
m_presenter->getPeaksWorkspaceNames();
std::stringstream stream;
for (size_t i = 0; i < peaksWorkspaceNames.size(); i++) {
stream << peaksWorkspaceNames[i];
// Don't add a delimiter after the last element
if (i != (peaksWorkspaceNames.size() - 1)) {
stream << delimiter;
}
}
return stream.str();
}
/**
* Update the presenters with the available peak workspaces
* @param peakSources A list with available peak sources
* @param splatSource The splatterplot source
*/
void PeaksTableControllerVsi::updatePeaksWorkspaces(
QList<QPointer<pqPipelineSource>> peakSources,
pqPipelineSource *splatSource) {
// Check if the presenters exist and which need to be added
std::vector<std::string> peaksWorkspaceNames;
std::vector<pqPipelineSource *> nonTrackedWorkspaces;
std::vector<std::string> trackedWorkspaceNames =
m_presenter->getPeaksWorkspaceNames();
for (QList<QPointer<pqPipelineSource>>::Iterator it = peakSources.begin();
it != peakSources.end(); ++it) {
std::string workspaceName(
vtkSMPropertyHelper((*it)->getProxy(), "WorkspaceName").GetAsString());
peaksWorkspaceNames.push_back(workspaceName);
int count = static_cast<int>(std::count(trackedWorkspaceNames.begin(),
trackedWorkspaceNames.end(),
workspaceName));
if (count == 0) {
nonTrackedWorkspaces.push_back(*it);
}
}
if (splatSource) {
// Add the workspaces which are missing in the presenter
for (std::vector<pqPipelineSource *>::iterator it =
nonTrackedWorkspaces.begin();
it != nonTrackedWorkspaces.end(); ++it) {
addWorkspace(*it, splatSource);
}
}
// Now update all the presenter
m_presenter->updateWorkspaces(peaksWorkspaceNames);
if (!peakSources.empty() && m_peaksTabWidget) {
m_peaksTabWidget->updateTabs(m_presenter->getInitializedViewablePeaks(),
getColors());
updatePeakWorkspaceColor();
}
// If there are no presenters left, we want to destroy the table
if (!hasPeaks()) {
removeTable();
}
}
/**
* Sort the peaks according to a selected column.
* @param columnToSortBy The column by which to sort.
* @param sortAscending If sort ascending or descending.
* @param ws The workspace which requires sorting.
*/
void PeaksTableControllerVsi::onPeaksSorted(
const std::string &columnToSortBy, const bool sortAscending,
Mantid::API::IPeaksWorkspace_sptr ws) {
// Invoke the ording command on the presenters
m_presenter->sortPeaksWorkspace(columnToSortBy, sortAscending, ws);
// Update the tabs
}
}
}
}