CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include <QInputDialog>
36 #include <QMessageBox>
37 
38 #include "cxSettings.h"
39 #include "cxActiveImageProxy.h"
40 #include "cxPatientModelService.h"
41 #include "cxLogger.h"
42 
43 namespace cx {
44 
46  PresetWidget(parent), mIs3D(is3D),
47  mPatientModelService(patientModelService)
48 {
49  this->setObjectName("TransferFunctionPresetWidget");
50  this->setPresets(patientModelService->getPresetTransferFunctions3D());
51  QString toggleText = "Toggle between apply presets,\neither on %1\nor both 2D and 3D\ntransfer functions.";
52  if (is3D)
53  toggleText = toggleText.arg("3D");
54  else
55  toggleText = toggleText.arg("2D");
56 
57  mToggleAction = this->createAction(mActionGroup,
58  QIcon(":/icons/preset_2D_and_3D.png"), toggleText, "",
59  SLOT(toggleSlot()));
60  this->populateButtonLayout();
61 
62  mApplyToAll = settings()->value("applyTransferFunctionPresetsToAll").toBool();
63  this->updateToggles();
64 
65  mActiveImageProxy = ActiveImageProxy::New(patientModelService);
66  connect(mActiveImageProxy.get(), &ActiveImageProxy::activeImageChanged, this,
68  connect(mActiveImageProxy.get(), &ActiveImageProxy::propertiesChanged, this,
70 }
71 
73  mApplyToAll = !mApplyToAll;
74  settings()->setValue("applyTransferFunctionPresetsToAll", mApplyToAll);
75 
76  this->updateToggles();
77 }
78 
79 void TransferFunctionPresetWidget::updateToggles()
80 {
81  if (mApplyToAll)
82  mToggleAction->setIcon(QIcon(":/icons/preset_2D_and_3D.png"));
83  else
84  {
85  if (mIs3D)
86  mToggleAction->setIcon(QIcon(":/icons/preset_3D.png"));
87  else
88  mToggleAction->setIcon(QIcon(":/icons/preset_2D.png"));
89  }
90 }
91 
93 {
94  return "<html>"
95  "<h3>Transfer Function Presets</h3>"
96  "<p>Lets you select a predefined transfer function.</p>"
97  "<p><i></i></p>"
98  "</html>";
99 }
100 
102 {
103  if (mPatientModelService->getActiveImage())
104  PresetWidget::populatePresetList(mPresets->getPresetList(mPatientModelService->getActiveImage()->getModality()));
105  else
106  //No active image, show all available presets for debug/overview purposes
107  PresetWidget::populatePresetList(mPresets->getPresetList("UNKNOWN"));
108 }
109 
111 {
112  ImagePtr activeImage = mPatientModelService->getActiveImage();
113  if (activeImage) {
114  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
115  preset->load(presetName, activeImage, this->use2D(), this->use3D());
116  }
117 }
118 
120 {
122  ImagePtr activeImage = mPatientModelService->getActiveImage();
123  activeImage->resetTransferFunctions(this->use2D(), this->use3D());
124 }
125 
127 {
128  // generate a name suggestion: identical if custom, appended by index if default.
129  QString newName = PresetWidget::getCurrentPreset();
130  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
131  if (!preset->getPresetList("").contains(newName))
132  newName = "custom preset";
133  if (preset->isDefaultPreset(newName))
134  newName += "(2)";
135 
136  bool ok;
137  QString text = QInputDialog::getText(this, "Save Preset",
138  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
139  if (!ok || text.isEmpty())
140  return;
141 
142  ImagePtr activeImage = mPatientModelService->getActiveImage();
143  preset->save(text, activeImage, this->use2D(), this->use3D());
144 
145  this->populatePresetListSlot();
147 }
148 
149 bool TransferFunctionPresetWidget::use2D() const
150 {
151 
152  return !mIs3D || mApplyToAll;
153 }
154 
155 bool TransferFunctionPresetWidget::use3D() const
156 {
157  return mIs3D || mApplyToAll;
158 }
159 
161 {
162  if (mPresets->isDefaultPreset(PresetWidget::getCurrentPreset())) {
163  reportWarning("It is not possible to delete one of the default presets");
164  return;
165  }
166 
167  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
168  if (QMessageBox::question(this, "Delete current preset", "Do you really want to delete the current preset?", QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
169  return;
170  preset->deletePresetData(PresetWidget::getCurrentPreset(), this->use2D(), this->use3D());
171 
172  this->populatePresetListSlot();
173  this->resetSlot();
174 }
175 
176 } //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 defaultWhatsThis() const
Returns a short description of what this widget will do for you.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:99
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:130
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:91
void deletePresetData(QString name, bool _2D=true, bool _3D=true)
Delete the preset data node.
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
TransferFunctionPresetWidget(PatientModelServicePtr patientModelService, QWidget *parent, bool is3D)
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:42
bool requestSetCurrentPreset(QString name)
tries to set the preset to the requested name
static ActiveImageProxyPtr New(PatientModelServicePtr patientModelService)
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.