CustusX  15.4.0-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->setToolTip("Select a predefined transfer function");
50  this->setObjectName("TransferFunctionPresetWidget");
51  this->setPresets(patientModelService->getPresetTransferFunctions3D());
52  QString toggleText = "Toggle between apply presets,\neither on %1\nor both 2D and 3D\ntransfer functions.";
53  if (is3D)
54  toggleText = toggleText.arg("3D");
55  else
56  toggleText = toggleText.arg("2D");
57 
58  mToggleAction = this->createAction(mActionGroup,
59  QIcon(":/icons/preset_2D_and_3D.png"), toggleText, "",
60  SLOT(toggleSlot()));
61  this->populateButtonLayout();
62 
63  mApplyToAll = settings()->value("applyTransferFunctionPresetsToAll").toBool();
64  this->updateToggles();
65 
66  mActiveImageProxy = ActiveImageProxy::New(patientModelService);
67  connect(mActiveImageProxy.get(), &ActiveImageProxy::activeImageChanged, this,
69  connect(mActiveImageProxy.get(), &ActiveImageProxy::propertiesChanged, this,
71 }
72 
74  mApplyToAll = !mApplyToAll;
75  settings()->setValue("applyTransferFunctionPresetsToAll", mApplyToAll);
76 
77  this->updateToggles();
78 }
79 
80 void TransferFunctionPresetWidget::updateToggles()
81 {
82  if (mApplyToAll)
83  mToggleAction->setIcon(QIcon(":/icons/preset_2D_and_3D.png"));
84  else
85  {
86  if (mIs3D)
87  mToggleAction->setIcon(QIcon(":/icons/preset_3D.png"));
88  else
89  mToggleAction->setIcon(QIcon(":/icons/preset_2D.png"));
90  }
91 }
92 
94 {
95  if (mPatientModelService->getActiveImage())
96  PresetWidget::populatePresetList(mPresets->getPresetList(mPatientModelService->getActiveImage()->getModality()));
97  else
98  //No active image, show all available presets for debug/overview purposes
99  PresetWidget::populatePresetList(mPresets->getPresetList("UNKNOWN"));
100 }
101 
103 {
104  ImagePtr activeImage = mPatientModelService->getActiveImage();
105  if (activeImage) {
106  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
107  preset->load(presetName, activeImage, this->use2D(), this->use3D());
108  }
109 }
110 
112 {
114  ImagePtr activeImage = mPatientModelService->getActiveImage();
115  activeImage->resetTransferFunctions(this->use2D(), this->use3D());
116 }
117 
119 {
120  // generate a name suggestion: identical if custom, appended by index if default.
121  QString newName = PresetWidget::getCurrentPreset();
122  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
123  if (!preset->getPresetList("").contains(newName))
124  newName = "custom preset";
125  if (preset->isDefaultPreset(newName))
126  newName += "(2)";
127 
128  bool ok;
129  QString text = QInputDialog::getText(this, "Save Preset",
130  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
131  if (!ok || text.isEmpty())
132  return;
133 
134  ImagePtr activeImage = mPatientModelService->getActiveImage();
135  preset->save(text, activeImage, this->use2D(), this->use3D());
136 
137  this->populatePresetListSlot();
139 }
140 
141 bool TransferFunctionPresetWidget::use2D() const
142 {
143 
144  return !mIs3D || mApplyToAll;
145 }
146 
147 bool TransferFunctionPresetWidget::use3D() const
148 {
149  return mIs3D || mApplyToAll;
150 }
151 
153 {
154  if (mPresets->isDefaultPreset(PresetWidget::getCurrentPreset())) {
155  reportWarning("It is not possible to delete one of the default presets");
156  return;
157  }
158 
159  TransferFunctions3DPresetsPtr preset = boost::dynamic_pointer_cast<TransferFunctions3DPresets>(mPresets);
160  if (QMessageBox::question(this, "Delete current preset", "Do you really want to delete the current preset?", QMessageBox::Cancel | QMessageBox::Ok) != QMessageBox::Ok)
161  return;
162  preset->deletePresetData(PresetWidget::getCurrentPreset(), this->use2D(), this->use3D());
163 
164  this->populatePresetListSlot();
165  this->resetSlot();
166 }
167 
168 } //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)
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: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: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.