Fraxinus  2023.01.05-dev+develop.0da12
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
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 IMAGE_MODALITY getModality() const
Definition: cxImage.cpp:805
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
virtual void save(const QString &basePath, FileManagerServicePtr filemanager)
Definition: cxImage.cpp:930
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
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
imUNKNOWN
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.