Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
33 #include "cxPresetWidget.h"
34 
35 #include <QComboBox>
36 #include <QInputDialog>
37 #include "cxLogger.h"
38 #include "cxSettings.h"
39 
40 namespace cx {
41 
42 PresetWidget::PresetWidget(QWidget* parent) :
43  BaseWidget(parent, "PresetWidget", "Presets"), mLayout(new QVBoxLayout(this))
44 {
45  this->setToolTip("Select a predefined set of options");
46  mPresetsComboBox = new QComboBox(this);
47  mPresetsComboBox->setToolTip("Select a preset to use");
48  connect(mPresetsComboBox, SIGNAL(currentIndexChanged(const QString&)), this,
49  SLOT(presetsBoxChangedSlot(const QString&)));
50 
51  mActionGroup = new QActionGroup(this);
52 
54  QIcon(":/icons/preset_reset.png"),
55  "Reset all transfer function values to the defaults", "",
56  SLOT(resetSlot()));
57 
59  QIcon(":/icons/preset_remove.png"),
60  "Delete the current preset", "",
61  SLOT(deleteSlot()));
62 
64  QIcon(":/icons/preset_save.png"),
65  "Add the current setting as a preset", "",
66  SLOT(saveSlot()));
67 
68  mLayout->addWidget(mPresetsComboBox);
69 
70  mButtonLayout = NULL;
71  this->populateButtonLayout();
72 
73  this->setLayout(mLayout);
74 }
75 
77 {
78  if(mPresetsComboBox->findText(name) == -1)
79  return false;
80 
81  mPresetsComboBox->setCurrentIndex(mPresetsComboBox->findText(name));
82  return true;
83 }
84 
86 {
87  return mPresetsComboBox->currentText();
88 }
89 
90 void PresetWidget::showDetailed(bool detailed)
91 {
92  if(!mButtonLayout)
93  return;
94 
95  for(int i=0; i < mButtonLayout->count(); ++i)
96  {
97  QWidget* widget = mButtonLayout->itemAt(i)->widget();
98  if(!widget)
99  continue;
100  if(detailed)
101  widget->show();
102  else
103  widget->hide();
104  }
105 }
106 
108 {
109  if(!presets)
110  {
111  reportError("Trying to set presets to null...");
112  return;
113  }
114  //TODO disconnect old stuff
115 
116  mPresets = presets;
117  connect(mPresets.get(), SIGNAL(changed()), this, SLOT(populatePresetListSlot()));
118 
119  this->populatePresetListSlot();
120  this->selectLastUsedPreset();
121 }
122 
124 {
125  QString id = mPresets->getId();
126  settings()->fillDefault(id, mPresets->getPresetList().first());
127 
128  QString lastUsedPresetName = settings()->value(id).toString();
129  return lastUsedPresetName;
130 }
131 
133 {
134  mPresetsComboBox->setCurrentIndex(0);
135 }
136 
138 {
139  mPresets->save();
140  this->populatePresetListSlot();
141 }
142 
144 {
145  mPresets->remove();
146  this->populatePresetListSlot();
147 }
148 
150 {
151  this->populatePresetList(mPresets->getPresetList(""));
152 }
153 
154 void PresetWidget::presetsBoxChangedSlot(const QString& name)
155 {
156  settings()->setValue(mPresets->getId(), name);
157  emit presetSelected(name);
158 }
159 
161 {
162  //delete old stuff
163  if(mButtonLayout)
164  {
165  QLayoutItem *child;
166  while ((child = mButtonLayout->takeAt(0)) != 0)
167  {
168  // delete both the layoutitem AND the widget. Not auto done because layoutitem is no QObject.
169  QWidget* widget = child->widget();
170  delete child;
171  delete widget;
172  }
173  delete mButtonLayout;
174  }
175 
176  //make the new buttons
177  mButtonLayout = new QHBoxLayout;
178  mLayout->addLayout(mButtonLayout);
179 
180  QList<QAction*> actions = mActionGroup->actions();
181  for (int i=0; i<actions.size(); ++i)
182  {
183  QToolButton* button = new QToolButton(this);
184  button->setDefaultAction(actions[i]);
185  button->show();
186  mButtonLayout->addWidget(button);
187  }
188  mButtonLayout->addStretch();
189 }
190 
191 void PresetWidget::populatePresetList(QStringList list)
192 {
193  mPresetsComboBox->blockSignals(true);
194  mPresetsComboBox->clear();
195 
196  mPresetsComboBox->addItem("<Default preset>");
197 
198  mPresetsComboBox->addItems(list);
199 
200  mPresetsComboBox->blockSignals(false);
201 }
202 
203 QString PresetWidget::getNewPresetName(bool withoutSpaces = false)
204 {
205  QString retval;
206 
207  // generate a name suggestion: identical if custom, appended by index if default.
208  QString newName = PresetWidget::getCurrentPreset();
209  if (!mPresets->getPresetList("").contains(newName))
210  newName = "custom preset";
211  if (mPresets->isDefaultPreset(newName))
212  newName += "(2)";
213 
214  bool ok;
215  QString text = QInputDialog::getText(this, "Save Preset",
216  "Custom Preset Name", QLineEdit::Normal, newName, &ok);
217  if (!ok || text.isEmpty())
218  text = newName;
219 
220  retval = text;
221  if(withoutSpaces)
222  retval = retval.replace(" ", "-");
223 
224  return retval;
225 }
226 
227 void PresetWidget::selectLastUsedPreset()
228 {
229  QString lastUsedPreset = this->getLastUsedPresetNameFromSettingsFile();
230  this->requestSetCurrentPreset(lastUsedPreset);
231 }
232 
233 
234 } /* namespace cx */
PresetWidget(QWidget *parent)
void reportError(QString msg)
Definition: cxLogger.cpp:92
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:87
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:149
virtual void resetSlot()
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:79
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:42
virtual void deleteSlot()
boost::shared_ptr< class Presets > PresetsPtr
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
bool requestSetCurrentPreset(QString name)
tries to set the preset to the requested name
PresetsPtr mPresets
void fillDefault(QString name, T value)
Definition: cxSettings.h:81
virtual void presetsBoxChangedSlot(const QString &)
void presetSelected(QString name)