NorMIT-nav  22.09
An IGT application
cxPresetWidget.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 
12 #include "cxPresetWidget.h"
13 
14 #include <QComboBox>
15 #include <QInputDialog>
16 #include "cxLogger.h"
17 #include "cxSettings.h"
18 #include "cxEnumConversion.h"
19 
20 namespace cx {
21 
22 PresetWidget::PresetWidget(QWidget* parent) :
23  BaseWidget(parent, "PresetWidget", "Presets"), mLayout(new QVBoxLayout(this))
24 {
25  this->setToolTip("Select a predefined set of options");
26  mPresetsComboBox = new QComboBox(this);
27  mPresetsComboBox->setToolTip("Select a preset to use");
28  connect(mPresetsComboBox, SIGNAL(currentIndexChanged(const QString&)), this,
29  SLOT(presetsBoxChangedSlot(const QString&)));
30 
31  mActionGroup = new QActionGroup(this);
32 
34  QIcon(":/icons/preset_reset.png"),
35  "Reset all transfer function values to the defaults", "",
36  SLOT(resetSlot()));
37 
39  QIcon(":/icons/preset_remove.png"),
40  "Delete the current preset", "",
41  SLOT(deleteSlot()));
42 
44  QIcon(":/icons/preset_save.png"),
45  "Add the current setting as a preset", "",
46  SLOT(saveSlot()));
47 
48  mLayout->addWidget(mPresetsComboBox);
49 
50  mButtonLayout = NULL;
51  this->populateButtonLayout();
52 
53  this->setLayout(mLayout);
54 }
55 
57 {
58  if(mPresetsComboBox->findText(name) == -1)
59  return false;
60 
61  mPresetsComboBox->setCurrentIndex(mPresetsComboBox->findText(name));
62  return true;
63 }
64 
66 {
67  return mPresetsComboBox->currentText();
68 }
69 
70 void PresetWidget::showDetailed(bool detailed)
71 {
72  if(!mButtonLayout)
73  return;
74 
75  for(int i=0; i < mButtonLayout->count(); ++i)
76  {
77  QWidget* widget = mButtonLayout->itemAt(i)->widget();
78  if(!widget)
79  continue;
80  if(detailed)
81  widget->show();
82  else
83  widget->hide();
84  }
85 }
86 
88 {
89  if(!presets)
90  {
91  reportError("Trying to set presets to null...");
92  return;
93  }
94  //TODO disconnect old stuff
95 
96  mPresets = presets;
97  connect(mPresets.get(), SIGNAL(changed()), this, SLOT(populatePresetListSlot()));
98 
99  this->populatePresetListSlot();
100  this->selectLastUsedPreset();
101 }
102 
104 {
105  IMAGE_MODALITY id = mPresets->getId();
106  QString preset;
107  if (!mPresets->getPresetList().isEmpty())
108  preset = mPresets->getPresetList().first();
109 
110  settings()->fillDefault(enum2string(id), preset);
111 
112  QString lastUsedPresetName = settings()->value(enum2string(id)).toString();
113  return lastUsedPresetName;
114 }
115 
117 {
118  mPresetsComboBox->setCurrentIndex(0);
119 }
120 
122 {
123  mPresets->save();
124  this->populatePresetListSlot();
125 }
126 
128 {
129  mPresets->remove();
130  this->populatePresetListSlot();
131 }
132 
134 {
135  this->populatePresetList(mPresets->getPresetList(imUNKNOWN));
136 }
137 
138 void PresetWidget::presetsBoxChangedSlot(const QString& name)
139 {
140  settings()->setValue(enum2string(mPresets->getId()), name);
141  emit presetSelected(name);
142 }
143 
145 {
146  //delete old stuff
147  if(mButtonLayout)
148  {
149  QLayoutItem *child;
150  while ((child = mButtonLayout->takeAt(0)) != 0)
151  {
152  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
153  QWidget* widget = child->widget();
154  delete child;
155  delete widget;
156  }
157  delete mButtonLayout;
158  }
159 
160  //make the new buttons
161  mButtonLayout = new QHBoxLayout;
162  mLayout->addLayout(mButtonLayout);
163 
164  QList<QAction*> actions = mActionGroup->actions();
165  for (int i=0; i<actions.size(); ++i)
166  {
167  QToolButton* button = new QToolButton(this);
168  button->setDefaultAction(actions[i]);
169  button->show();
170  mButtonLayout->addWidget(button);
171  }
172  mButtonLayout->addStretch();
173 }
174 
175 void PresetWidget::populatePresetList(QStringList list)
176 {
177  mPresetsComboBox->blockSignals(true);
178  mPresetsComboBox->clear();
179 
180  mPresetsComboBox->addItem("<Default preset>");
181 
182  mPresetsComboBox->addItems(list);
183 
184  mPresetsComboBox->blockSignals(false);
185 }
186 
187 QString PresetWidget::getNewPresetName(bool withoutSpaces = false)
188 {
189  QString retval;
190 
191  // generate a name suggestion: identical if custom, appended by index if default.
192  QString newName = PresetWidget::getCurrentPreset();
193  if (!mPresets->getPresetList(imUNKNOWN).contains(newName))
194  newName = "custom preset";
195  if (mPresets->isDefaultPreset(newName))
196  newName += "(2)";
197 
198  bool ok;
199  QString text = QInputDialog::getText(this, "Save Preset",
200  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
201  if (!ok || text.isEmpty())
202  text = newName;
203 
204  retval = text;
205  if(withoutSpaces)
206  retval = retval.replace(" ", "-");
207 
208  return retval;
209 }
210 
211 void PresetWidget::selectLastUsedPreset()
212 {
213  QString lastUsedPreset = this->getLastUsedPresetNameFromSettingsFile();
214  this->requestSetCurrentPreset(lastUsedPreset);
215 }
216 
217 
218 } /* 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
cx::PresetWidget::PresetWidget
PresetWidget(QWidget *parent)
Definition: cxPresetWidget.cpp:22
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::PresetWidget::showDetailed
void showDetailed(bool detailed)
sets the presetwidget in detailed mode or not
Definition: cxPresetWidget.cpp:70
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::BaseWidget
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
cx::PresetWidget::getNewPresetName
QString getNewPresetName(bool withoutSpaces)
Definition: cxPresetWidget.cpp:187
cx::PresetWidget::getLastUsedPresetNameFromSettingsFile
QString getLastUsedPresetNameFromSettingsFile() const
Definition: cxPresetWidget.cpp:103
imUNKNOWN
imUNKNOWN
Definition: cxDefinitions.h:151
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::PresetWidget::presetsBoxChangedSlot
virtual void presetsBoxChangedSlot(const QString &)
Definition: cxPresetWidget.cpp:138
cx::PresetWidget::deleteSlot
virtual void deleteSlot()
Definition: cxPresetWidget.cpp:127
cx::Settings::fillDefault
void fillDefault(QString name, T value)
Definition: cxSettings.h:60
cx::PresetWidget::mPresets
PresetsPtr mPresets
Definition: cxPresetWidget.h:68
cx::PresetsPtr
boost::shared_ptr< class Presets > PresetsPtr
Definition: cxForwardDeclarations.h:159
cx::PresetWidget::mActionGroup
QActionGroup * mActionGroup
contains all actions that will have buttons
Definition: cxPresetWidget.h:67
cx::PresetWidget::requestSetCurrentPreset
bool requestSetCurrentPreset(QString name)
tries to set the preset to the requested name
Definition: cxPresetWidget.cpp:56
cx::PresetWidget::presetSelected
void presetSelected(QString name)
cx::PresetWidget::setPresets
virtual void setPresets(PresetsPtr presets)
Definition: cxPresetWidget.cpp:87
cx::PresetWidget::populatePresetList
void populatePresetList(QStringList list)
populates the preset combobox
Definition: cxPresetWidget.cpp:175
cxSettings.h
cx::PresetWidget::saveSlot
virtual void saveSlot()
Definition: cxPresetWidget.cpp:121
cxPresetWidget.h
cx::Settings::setValue
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
cx::PresetWidget::populatePresetListSlot
virtual void populatePresetListSlot()
Fill the preset list with the available presets.
Definition: cxPresetWidget.cpp:133
enum2string
QString enum2string(const ENUM &val)
cxEnumConversion.h
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cx::settings
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21