CustusX  2023.01.05-dev+develop.0da12
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 */
PresetWidget(QWidget *parent)
void reportError(QString msg)
Definition: cxLogger.cpp:71
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 void saveSlot()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
QString getNewPresetName(bool withoutSpaces)
QString getLastUsedPresetNameFromSettingsFile() const
virtual void populatePresetListSlot()
Fill the preset list with the available presets.
void showDetailed(bool detailed)
sets the presetwidget in detailed mode or not
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()
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
void populatePresetList(QStringList list)
populates the preset combobox
QActionGroup * mActionGroup
contains all actions that will have buttons
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
virtual void deleteSlot()
boost::shared_ptr< class Presets > PresetsPtr
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
imUNKNOWN
bool requestSetCurrentPreset(QString name)
tries to set the preset to the requested name
PresetsPtr mPresets
void fillDefault(QString name, T value)
Definition: cxSettings.h:60
virtual void presetsBoxChangedSlot(const QString &)
void presetSelected(QString name)
QString enum2string(const ENUM &val)
Namespace for all CustusX production code.