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