Fraxinus  18.10
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  settings()->fillDefault(id, mPresets->getPresetList().first());
106 
107  QString lastUsedPresetName = settings()->value(id).toString();
108  return lastUsedPresetName;
109 }
110 
112 {
113  mPresetsComboBox->setCurrentIndex(0);
114 }
115 
117 {
118  mPresets->save();
119  this->populatePresetListSlot();
120 }
121 
123 {
124  mPresets->remove();
125  this->populatePresetListSlot();
126 }
127 
129 {
130  this->populatePresetList(mPresets->getPresetList(""));
131 }
132 
133 void PresetWidget::presetsBoxChangedSlot(const QString& name)
134 {
135  settings()->setValue(mPresets->getId(), name);
136  emit presetSelected(name);
137 }
138 
140 {
141  //delete old stuff
142  if(mButtonLayout)
143  {
144  QLayoutItem *child;
145  while ((child = mButtonLayout->takeAt(0)) != 0)
146  {
147  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
148  QWidget* widget = child->widget();
149  delete child;
150  delete widget;
151  }
152  delete mButtonLayout;
153  }
154 
155  //make the new buttons
156  mButtonLayout = new QHBoxLayout;
157  mLayout->addLayout(mButtonLayout);
158 
159  QList<QAction*> actions = mActionGroup->actions();
160  for (int i=0; i<actions.size(); ++i)
161  {
162  QToolButton* button = new QToolButton(this);
163  button->setDefaultAction(actions[i]);
164  button->show();
165  mButtonLayout->addWidget(button);
166  }
167  mButtonLayout->addStretch();
168 }
169 
170 void PresetWidget::populatePresetList(QStringList list)
171 {
172  mPresetsComboBox->blockSignals(true);
173  mPresetsComboBox->clear();
174 
175  mPresetsComboBox->addItem("<Default preset>");
176 
177  mPresetsComboBox->addItems(list);
178 
179  mPresetsComboBox->blockSignals(false);
180 }
181 
182 QString PresetWidget::getNewPresetName(bool withoutSpaces = false)
183 {
184  QString retval;
185 
186  // generate a name suggestion: identical if custom, appended by index if default.
187  QString newName = PresetWidget::getCurrentPreset();
188  if (!mPresets->getPresetList("").contains(newName))
189  newName = "custom preset";
190  if (mPresets->isDefaultPreset(newName))
191  newName += "(2)";
192 
193  bool ok;
194  QString text = QInputDialog::getText(this, "Save Preset",
195  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
196  if (!ok || text.isEmpty())
197  text = newName;
198 
199  retval = text;
200  if(withoutSpaces)
201  retval = retval.replace(" ", "-");
202 
203  return retval;
204 }
205 
206 void PresetWidget::selectLastUsedPreset()
207 {
208  QString lastUsedPreset = this->getLastUsedPresetNameFromSettingsFile();
209  this->requestSetCurrentPreset(lastUsedPreset);
210 }
211 
212 
213 } /* 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.