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