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