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