fw4spl
QfwToolbox.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/widget/QfwToolbox.hpp"
8 
9 #include <QApplication>
10 #include <QFrame>
11 #include <QIcon>
12 #include <QPixmap>
13 #include <QStyleOption>
14 #include <QVBoxLayout>
15 
16 namespace fwGuiQt
17 {
18 namespace widget
19 {
20 
21 static const char* branch_closed[] = {
22  "8 17 2 1",
23 /* colors */
24  "- c #000000",
25  "a c None",
26 /* pixels */
27  "aaaaaaaa",
28  "-aaaaaaa",
29  "--aaaaaa",
30  "---aaaaa",
31  "----aaaa",
32  "-----aaa",
33  "------aa",
34  "-------a",
35  "--------",
36  "-------a",
37  "------aa",
38  "-----aaa",
39  "----aaaa",
40  "---aaaaa",
41  "--aaaaaa",
42  "-aaaaaaa",
43  "aaaaaaaa"
44 };
45 
46 static const char* branch_open[] = {
47  "11 6 2 1",
48 /* colors */
49  "- c #000000",
50  "a c None",
51 /* pixels */
52  "-----------",
53  "a---------a",
54  "aa-------aa",
55  "aaa-----aaa",
56  "aaaa---aaaa",
57  "aaaaa-aaaaa"
58 };
59 
60 QfwToolBox::~QfwToolBox()
61 {
62 }
63 
64 //-----------------------------------------------------------------------------
65 
66 QfwToolBox::QfwToolBox(QWidget* parent, Qt::WindowFlags f) : QFrame(parent, f)
67 {
68  this->layout = new QFormLayout(this);
69  this->layout->setMargin(0);
70  layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
71  layout->setHorizontalSpacing(0);
72  layout->setVerticalSpacing(1);
73  setBackgroundRole(QPalette::Button);
74 }
75 
76 //-----------------------------------------------------------------------------
77 
78 ::fwGuiQt::widget::Page* QfwToolBox::page(QWidget *widget) const
79 {
80  if (!widget)
81  {
82  return 0;
83  }
84 
85  for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)
86  {
87  if ((*i).widget == widget)
88  {
89  return (Page*) &(*i);
90  }
91  }
92  return 0;
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 ::fwGuiQt::widget::Page* QfwToolBox::page(int index)
98 {
99  if (index >= 0 && index < pageList.size())
100  {
101  return &pageList[index];
102  }
103  return 0;
104 }
105 
106 //-----------------------------------------------------------------------------
107 
108 const ::fwGuiQt::widget::Page* QfwToolBox::page(int index) const
109 {
110  if (index >= 0 && index < pageList.size())
111  {
112  return &pageList.at(index);
113  }
114  return 0;
115 }
116 
117 //-----------------------------------------------------------------------------
118 
119 void QfwToolBox::collapseItem(int index)
120 {
121  Page* page = this->page(index);
122  if(page)
123  {
124  page->sv->setVisible(false);
125  page->button->setChecked(false);
126  }
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 void QfwToolBox::expandItem(int index)
132 {
133  Page* page = this->page(index);
134  if(page)
135  {
136  page->sv->setVisible(true);
137  page->button->setChecked(true);
138  }
139 }
140 
141 //-----------------------------------------------------------------------------
142 
143 int QfwToolBox::addItem(QWidget* item, const QString& text)
144 {
145  return insertItem(-1, item, text);
146 }
147 
148 //-----------------------------------------------------------------------------
149 
150 int QfwToolBox::insertItem(int index, QWidget* widget, const QString& text)
151 {
152  if (!widget)
153  {
154  return -1;
155  }
156 
157  connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)));
158 
159  Page c;
160  c.widget = widget;
161  c.button = new QPushButton(this);
162  c.button->setObjectName("QfwToolBoxButton");
163  c.button->setBackgroundRole(QPalette::Window);
164  c.button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
165  c.button->setFocusPolicy(Qt::NoFocus);
166  c.button->setCheckable(true);
167  QPixmap pixOpen(branch_open);
168  QPixmap pixClose(branch_closed);
169  QIcon bIcon;
170  bIcon.addPixmap( pixClose, QIcon::Normal, QIcon::Off );
171  bIcon.addPixmap( pixOpen, QIcon::Active, QIcon::On );
172  c.setIcon(bIcon);
173  if(qApp->styleSheet().isEmpty())
174  {
175  QString style(
176  "text-align: left;"
177  "background-color: lightgray;"
178  "border-style: solid;"
179  "border-width: 1px;"
180  "border-color: darkgray;"
181  "height: 20px;"
182  );
183  c.button->setStyleSheet(style);
184  }
185  connect(c.button, SIGNAL(toggled(bool)), this, SLOT(buttonToggled(bool)));
186 
187  c.sv = new QFrame(this);
188  QVBoxLayout* verticalLayout = new QVBoxLayout(c.sv);
189  verticalLayout->setMargin(0);
190  verticalLayout->addWidget(widget);
191  c.sv->hide();
192  c.sv->setFrameStyle(QFrame::NoFrame);
193 
194  c.setText(text);
195 
196  if (index < 0 || index >= (int)this->pageList.count())
197  {
198  index = this->pageList.count();
199  this->pageList.append(c);
200  this->layout->addWidget(c.button);
201  this->layout->addWidget(c.sv);
202  }
203  else
204  {
205  this->pageList.insert(index, c);
206  this->relayout();
207  }
208  c.button->show();
209  return index;
210 }
211 
212 //-----------------------------------------------------------------------------
213 
214 void QfwToolBox::buttonToggled(bool checked)
215 {
216  QPushButton* tb = qobject_cast<QPushButton*>(this->sender());
217  QWidget* item = 0;
218  for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)
219  {
220  if ((*i).button == tb)
221  {
222  item = (*i).widget;
223  break;
224  }
225  }
226  int index = this->indexOf(item);
227  Page* page = this->page(index);
228  page->sv->setVisible(checked);
229 }
230 
231 //-----------------------------------------------------------------------------
232 
233 int QfwToolBox::count() const
234 {
235  return this->pageList.count();
236 }
237 
238 //-----------------------------------------------------------------------------
239 
240 void QfwToolBox::relayout()
241 {
242  delete layout;
243  layout = new QFormLayout(this);
244  layout->setMargin(0);
245  layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
246  layout->setHorizontalSpacing(0);
247  layout->setVerticalSpacing(1);
248  for (PageList::ConstIterator i = pageList.constBegin(); i != pageList.constEnd(); ++i)
249  {
250  layout->addWidget((*i).button);
251  layout->addWidget((*i).sv);
252  }
253 }
254 
255 //-----------------------------------------------------------------------------
256 
257 void QfwToolBox::widgetDestroyed(QObject* object)
258 {
259  QWidget* p = (QWidget*)object;
260  Page* c = page(p);
261  if (!p || !c)
262  {
263  return;
264  }
265 
266  layout->removeWidget(c->sv);
267  layout->removeWidget(c->button);
268  c->sv->deleteLater();
269  delete c->button;
270  pageList.removeAll(*c);
271 }
272 
273 //-----------------------------------------------------------------------------
274 
275 void QfwToolBox::removeItem(int index)
276 {
277  if (QWidget* w = widget(index))
278  {
279  disconnect(w, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)));
280  w->setParent(this);
281  this->widgetDestroyed(w);
282  }
283 }
284 
285 //-----------------------------------------------------------------------------
286 
287 QWidget* QfwToolBox::widget(int index) const
288 {
289  if (index < 0 || index >= (int) this->pageList.size())
290  {
291  return 0;
292  }
293  return this->pageList.at(index).widget;
294 }
295 
296 //-----------------------------------------------------------------------------
297 
298 int QfwToolBox::indexOf(QWidget* widget) const
299 {
300  Page* c = (widget ? this->page(widget) : 0);
301  return c ? this->pageList.indexOf(*c) : -1;
302 }
303 
304 //-----------------------------------------------------------------------------
305 
306 void QfwToolBox::setItemEnabled(int index, bool enabled)
307 {
308  Page* c = this->page(index);
309  if (!c)
310  {
311  return;
312  }
313 
314  c->button->setEnabled(enabled);
315 }
316 
317 //-----------------------------------------------------------------------------
318 
319 void QfwToolBox::setItemText(int index, const QString& text)
320 {
321  Page* c = this->page(index);
322  if (c)
323  {
324  c->setText(text);
325  }
326 }
327 
328 //-----------------------------------------------------------------------------
329 
330 void QfwToolBox::setItemToolTip(int index, const QString& toolTip)
331 {
332  Page* c = this->page(index);
333  if (c)
334  {
335  c->setToolTip(toolTip);
336  }
337 }
338 
339 //-----------------------------------------------------------------------------
340 
341 bool QfwToolBox::isItemEnabled(int index) const
342 {
343  const Page* c = this->page(index);
344  return c && c->button->isEnabled();
345 }
346 
347 //-----------------------------------------------------------------------------
348 
349 QString QfwToolBox::itemText(int index) const
350 {
351  const Page* c = this->page(index);
352  return (c ? c->text() : QString());
353 }
354 
355 //-----------------------------------------------------------------------------
356 
357 QString QfwToolBox::itemToolTip(int index) const
358 {
359  const Page* c = this->page(index);
360  return (c ? c->toolTip() : QString());
361 }
362 
363 //-----------------------------------------------------------------------------
364 
365 } // namespace widget
366 } // namespace fwGuiQt
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32