NorMIT-nav  16.5
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 #include "cxActiveData.h"
43 
44 namespace cx {
45 
47  PresetWidget(parent), mIs3D(is3D),
48  mActiveData(patientModelService->getActiveData())
49 {
50  this->setToolTip("Select a predefined transfer function");
51  this->setObjectName("TransferFunctionPresetWidget");
52  this->setPresets(patientModelService->getPresetTransferFunctions3D());
53  QString toggleText = "Toggle between apply presets,\neither on %1\nor both 2D and 3D\ntransfer functions.";
54  if (is3D)
55  toggleText = toggleText.arg("3D");
56  else
57  toggleText = toggleText.arg("2D");
58 
59  mToggleAction = this->createAction(mActionGroup,
60  QIcon(":/icons/preset_2D_and_3D.png"), toggleText, "",
61  SLOT(toggleSlot()));
62  this->populateButtonLayout();
63 
64  mApplyToAll = settings()->value("applyTransferFunctionPresetsToAll").toBool();
65  this->updateToggles();
66 
67  mActiveImageProxy = ActiveImageProxy::New(mActiveData);
68  connect(mActiveImageProxy.get(), &ActiveImageProxy::activeImageChanged, this,
70  connect(mActiveImageProxy.get(), &ActiveImageProxy::propertiesChanged, this,
72 }
73 
75  mApplyToAll = !mApplyToAll;
76  settings()->setValue("applyTransferFunctionPresetsToAll", mApplyToAll);
77 
78  this->updateToggles();
79  this->populatePresetListSlot();
80 }
81 
82 void TransferFunctionPresetWidget::updateToggles()
83 {
84  if (mApplyToAll)
85  mToggleAction->setIcon(QIcon(":/icons/preset_2D_and_3D.png"));
86  else
87  {
88  if (mIs3D)
89  mToggleAction->setIcon(QIcon(":/icons/preset_3D.png"));
90  else
91  mToggleAction->setIcon(QIcon(":/icons/preset_2D.png"));
92  }
93 }
94 
96 {
97  if (mActiveData->getActive<Image>())
98  PresetWidget::populatePresetList(mPresets->getPresetList(mActiveData->getActive<Image>()->getModality()));
99  else
100  //No active image, show all available presets for debug/overview purposes
101  PresetWidget::populatePresetList(mPresets->getPresetList("UNKNOWN"));
102 }
103 
105 {
106  ImagePtr activeImage = mActiveData->getActive<Image>();
107  if (activeImage) {
108  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
109  preset->load(presetName, activeImage, this->use2D(), this->use3D());
110  }
111 }
112 
114 {
116  ImagePtr activeImage = mActiveData->getActive<Image>();
117  activeImage->resetTransferFunctions(this->use2D(), this->use3D());
118 }
119 
121 {
122  // generate a name suggestion: identical if custom, appended by index if default.
123  QString newName = PresetWidget::getCurrentPreset();
124  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
125  if (!preset->getPresetList("").contains(newName))
126  newName = "custom preset";
127  if (preset->isDefaultPreset(newName))
128  newName += "(2)";
129 
130  bool ok;
131  QString text = QInputDialog::getText(this, "Save Preset",
132  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
133  if (!ok || text.isEmpty())
134  return;
135 
136  ImagePtr activeImage = mActiveData->getActive<Image>();
137  preset->save(text, activeImage, this->use2D(), this->use3D());
138 
139  this->populatePresetListSlot();
141 }
142 
143 bool TransferFunctionPresetWidget::use2D() const
144 {
145 
146  return !mIs3D || mApplyToAll;
147 }
148 
149 bool TransferFunctionPresetWidget::use3D() const
150 {
151  return mIs3D || mApplyToAll;
152 }
153 
155 {
156  if (mPresets->isDefaultPreset(PresetWidget::getCurrentPreset())) {
157  reportWarning("It is not possible to delete one of the default presets");
158  return;
159  }
160 
161  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
162  if (QMessageBox::question(this, "Delete current preset", "Do you really want to delete the current preset?", QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
163  return;
164  preset->deletePresetData(PresetWidget::getCurrentPreset(), this->use2D(), this->use3D());
165 
166  this->populatePresetListSlot();
167  this->resetSlot();
168 }
169 
170 } //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:807
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:149
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:79
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:91
TransferFunctionPresetWidget(PatientModelServicePtr patientModelService, QWidget *parent, bool is3D)
A volumetric data set.
Definition: cxImage.h:66
void populatePresetList(QStringList list)
populates the preset combobox
virtual void save(const QString &basePath)
Definition: cxImage.cpp:930
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
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:210