CustusX  18.04
An IGT application
cxTransferFunctionPresetWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <QInputDialog>
15 #include <QMessageBox>
16 
17 #include "cxSettings.h"
18 #include "cxActiveImageProxy.h"
19 #include "cxPatientModelService.h"
20 #include "cxLogger.h"
21 #include "cxActiveData.h"
22 
23 namespace cx {
24 
26  PresetWidget(parent), mIs3D(is3D),
27  mActiveData(patientModelService->getActiveData())
28 {
29  this->setToolTip("Select a predefined transfer function");
30  this->setObjectName("transfer_function_preset_widget");
31  this->setPresets(patientModelService->getPresetTransferFunctions3D());
32  QString toggleText = "Toggle between apply presets,\neither on %1\nor both 2D and 3D\ntransfer functions.";
33  if (is3D)
34  toggleText = toggleText.arg("3D");
35  else
36  toggleText = toggleText.arg("2D");
37 
38  mToggleAction = this->createAction(mActionGroup,
39  QIcon(":/icons/preset_2D_and_3D.png"), toggleText, "",
40  SLOT(toggleSlot()));
41  this->populateButtonLayout();
42 
43  mApplyToAll = settings()->value("applyTransferFunctionPresetsToAll").toBool();
44  this->updateToggles();
45 
46  mActiveImageProxy = ActiveImageProxy::New(mActiveData);
47  connect(mActiveImageProxy.get(), &ActiveImageProxy::activeImageChanged, this,
49  connect(mActiveImageProxy.get(), &ActiveImageProxy::propertiesChanged, this,
51 }
52 
54  mApplyToAll = !mApplyToAll;
55  settings()->setValue("applyTransferFunctionPresetsToAll", mApplyToAll);
56 
57  this->updateToggles();
58  this->populatePresetListSlot();
59 }
60 
61 void TransferFunctionPresetWidget::updateToggles()
62 {
63  if (mApplyToAll)
64  mToggleAction->setIcon(QIcon(":/icons/preset_2D_and_3D.png"));
65  else
66  {
67  if (mIs3D)
68  mToggleAction->setIcon(QIcon(":/icons/preset_3D.png"));
69  else
70  mToggleAction->setIcon(QIcon(":/icons/preset_2D.png"));
71  }
72 }
73 
75 {
76  if (mActiveData->getActive<Image>())
77  PresetWidget::populatePresetList(mPresets->getPresetList(mActiveData->getActive<Image>()->getModality()));
78  else
79  //No active image, show all available presets for debug/overview purposes
80  PresetWidget::populatePresetList(mPresets->getPresetList("UNKNOWN"));
81 }
82 
84 {
85  ImagePtr activeImage = mActiveData->getActive<Image>();
86  if (activeImage) {
87  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
88  preset->load(presetName, activeImage, this->use2D(), this->use3D());
89  }
90 }
91 
93 {
95  ImagePtr activeImage = mActiveData->getActive<Image>();
96  activeImage->resetTransferFunctions(this->use2D(), this->use3D());
97 }
98 
100 {
101  // generate a name suggestion: identical if custom, appended by index if default.
102  QString newName = PresetWidget::getCurrentPreset();
103  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
104  if (!preset->getPresetList("").contains(newName))
105  newName = "custom preset";
106  if (preset->isDefaultPreset(newName))
107  newName += "(2)";
108 
109  bool ok;
110  QString text = QInputDialog::getText(this, "Save Preset",
111  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
112  if (!ok || text.isEmpty())
113  return;
114 
115  ImagePtr activeImage = mActiveData->getActive<Image>();
116  preset->save(text, activeImage, this->use2D(), this->use3D());
117 
118  this->populatePresetListSlot();
120 }
121 
122 bool TransferFunctionPresetWidget::use2D() const
123 {
124 
125  return !mIs3D || mApplyToAll;
126 }
127 
128 bool TransferFunctionPresetWidget::use3D() const
129 {
130  return mIs3D || mApplyToAll;
131 }
132 
134 {
135  if (mPresets->isDefaultPreset(PresetWidget::getCurrentPreset())) {
136  reportWarning("It is not possible to delete one of the default presets");
137  return;
138  }
139 
140  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
141  if (QMessageBox::question(this, "Delete current preset", "Do you really want to delete the current preset?", QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
142  return;
143  preset->deletePresetData(PresetWidget::getCurrentPreset(), this->use2D(), this->use3D());
144 
145  this->populatePresetListSlot();
146  this->resetSlot();
147 }
148 
149 } //namespace cx
void load(QString name, ImagePtr image, bool _2D=true, bool _3D=true)
void presetsBoxChangedSlot(const QString &presetName)
virtual void populateButtonLayout()
makes buttons based on the actions found in the actiongroup
QString getCurrentPreset()
returns the name of the currently selected preset
virtual void setPresets(PresetsPtr presets)
virtual QString getModality() const
Definition: cxImage.cpp:795
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
virtual void resetSlot()
Base class for preset handling. Takes care of making a uniform preset system. Contains a preset selec...
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
void deletePresetData(QString name, bool _2D=true, bool _3D=true)
Delete the preset data node.
static ActiveImageProxyPtr New(ActiveDataPtr activeData)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
TransferFunctionPresetWidget(PatientModelServicePtr patientModelService, QWidget *parent, bool is3D)
A volumetric data set.
Definition: cxImage.h:45
void populatePresetList(QStringList list)
populates the preset combobox
virtual void save(const QString &basePath)
Definition: cxImage.cpp:918
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
QActionGroup * mActionGroup
contains all actions that will have buttons
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
bool requestSetCurrentPreset(QString name)
tries to set the preset to the requested name
PresetsPtr mPresets
boost::shared_ptr< class TransferFunctions3DPresets > TransferFunctions3DPresetsPtr
void activeImageChanged(const QString &uid)
The original image changed signal from DataManager.
virtual void populatePresetListSlot()
Fill the preset list with the available presets (matching active images modality) ...
Handles transfer function presets.
void resetTransferFunctions(bool _2D=true, bool _3D=true)
Resets the transfer functions and creates new default values.
Definition: cxImage.cpp:189
Namespace for all CustusX production code.