Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SideElement.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 //== INCLUDES =================================================================
53 
54 #include <QVBoxLayout>
55 #include <QLabel>
56 #include <QToolButton>
57 #include <QAction>
58 #include <QMouseEvent>
59 #include <QDialog>
60 
62 
63 #include "SideElement.hh"
64 
65 //== IMPLEMENTATION ==========================================================
66 
67 SideElement::SideElement (SideArea *_parent, QWidget *_w, QString _name, QIcon* _icon,
68  QWidget *_headerAreaWidget) :
69  parent_ (_parent),
70  widget_ (_w),
71  headerAreaWidget_(_headerAreaWidget),
72  name_ (_name),
73  icon_ (_icon),
74  active_ (0),
75  dialog_ (0)
76 {
77  QFont font;
78  font.setBold (false);
79 
80  QHBoxLayout *hl = new QHBoxLayout;
81  hl->setContentsMargins(2, 2, 2, 2);
82 
84 
85  label_ = new QLabel (_name);
86  label_->setFont (font);
87 
88  iconHolder_ = new QLabel ();
89 
90  if (icon_ != 0)
91  iconHolder_->setPixmap( icon_->pixmap(22,22) );
92  else{
93  QPixmap pic(QSize(22,22));
94  pic.fill( QColor(0,0,0,0) );
95 
96  iconHolder_->setPixmap( pic );
97  }
98 
99  detachButton_ = new QToolButton ();
100  detachButton_->setAutoRaise(true);
101  hl->addWidget (iconHolder_);
102  hl->addWidget (label_);
103  if (headerAreaWidget_) {
104  headerAreaWidget_->setVisible(false);
105  connect(this, SIGNAL(toggleActive(bool)), headerAreaWidget_, SLOT(setVisible(bool)));
106  hl->addWidget (headerAreaWidget_);
107  }
108  hl->addStretch(1);
109  hl->addWidget (detachButton_);
110 
111 
112  QIcon detachIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png");
113  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Active);
114  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Selected);
115 
116  detachAction_ = new QAction ( detachIcon, "", this);
117  detachAction_->setToolTip( tr("Show as separate window") );
118  detachAction_->setCheckable (true);
119  detachButton_->setDefaultAction (detachAction_);
120 
121  connect (detachAction_, SIGNAL (triggered (bool)), this, SLOT (detachPressed (bool)));
122 
123  tra->setLayout (hl);
124 
125  QFrame *f = new QFrame ();
126  f->setFrameShape (QFrame::HLine);
127 
128  mainLayout_ = new QVBoxLayout;
129 
130  mainLayout_->addWidget (f);
131  mainLayout_->addWidget (tra);
132  mainLayout_->addWidget (_w);
133  mainLayout_->setSpacing (0);
134  mainLayout_->setContentsMargins(0, 0, 0, 0);
135  setLayout (mainLayout_);
136 
137  _w->hide ();
138 }
139 
140 //-----------------------------------------------------------------------------
141 
143 {
144  if (dialog_) {
145  disconnect(dialog_,SIGNAL(finished(int)),this,SLOT(dialogClosed()));
146  dialog_->close ();
147  }
148  widget_->setParent (0);
149  if (headerAreaWidget_)
150  headerAreaWidget_->setParent(0);
151 }
152 
153 //-----------------------------------------------------------------------------
154 
156 {
157  if (dialog_)
158  {
159  dialog_->raise ();
160  dialog_->activateWindow ();
161  }
162  else
163  {
164  active_ = !active_;
165  if (active_)
166  widget_->show ();
167  else
168  widget_->hide ();
169 
170  QFont font;
171  font.setBold (active_);
172  label_->setFont (font);
173 
174  emit toggleActive(active_);
175  }
176 }
177 
178 //-----------------------------------------------------------------------------
179 
180 void SideElement::setActive(bool _active)
181 {
182  if ( dialog_ )
183  {
184  dialog_->raise ();
185  dialog_->activateWindow ();
186  }
187  else
188  {
189  const bool doEmit = (active_ != _active);
190  active_ = _active;
191  if (active_)
192  widget_->show ();
193  else
194  widget_->hide ();
195 
196  QFont font;
197  font.setBold (active_);
198  label_->setFont (font);
199 
200  if (doEmit) emit toggleActive(active_);
201  }
202 }
203 
204 //-----------------------------------------------------------------------------
205 
206 void SideElement::detachPressed (bool checked_)
207 {
208  if (checked_)
209  {
210  mainLayout_->removeWidget (widget_);
211  dialog_ = new QDialog(0, Qt::Window);
212  dialog_->setWindowTitle (name_);
213  dialog_->setWindowIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png"));
214  dialog_->setLayout (new QVBoxLayout);
215  dialog_->resize (widget_->size ());
216  if (window ())
217  {
218  int x = (window ()->width () - widget_->width ()) / 2;
219  x += window ()->x ();
220  x = qMax (0, x);
221  int y = (window ()->height () - widget_->height ()) / 2;
222  y += window ()->y ();
223  y = qMax (0, y);
224  dialog_->move (x, y);
225  }
226  dialog_->layout ()->addWidget (widget_);
227  dialog_->show ();
228  widget_->setAttribute(Qt::WA_DeleteOnClose, false);
229  widget_->show ();
230 
231  connect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));
232 
233  QFont font;
234  font.setBold (true);
235  font.setItalic (true);
236  label_->setFont (font);
237  }
238  else if (dialog_)
239  dialog_->close ();
240 
241 }
242 
243 //-----------------------------------------------------------------------------
244 
246 {
247  dialog_ = 0;
248  mainLayout_->addWidget (widget_);
249  widget_->setAttribute(Qt::WA_DeleteOnClose, true);
250 
251  if (active_)
252  widget_->show ();
253  else
254  widget_->hide ();
255 
256  detachAction_->setChecked (false);
257  QFont font;
258  font.setBold (active_);
259  label_->setFont (font);
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 void SideElement::saveState (QSettings &_settings)
265 {
266  _settings.beginGroup (name_);
267  _settings.setValue ("Active", active());
268  _settings.setValue ("Detached", (dialog_ != 0));
269  _settings.setValue ("DialogGeometry", (dialog_) ? dialog_->saveGeometry (): QByteArray());
270  _settings.endGroup ();
271 }
272 
273 //-----------------------------------------------------------------------------
274 void SideElement::restoreState (QSettings &_settings)
275 {
276  _settings.beginGroup (name_);
277 
278  bool active = _settings.value ("Active", active_).toBool ();
279  const bool doEmit = (active_ != active);
280  active_ = active;
281 
282  if (active_)
283  widget_->show ();
284  else
285  widget_->hide ();
286 
287  QFont font;
288  font.setBold (active_);
289  label_->setFont (font);
290 
291  if (doEmit) emit toggleActive(active_);
292 
293  if (_settings.value ("Detached", false).toBool () && !dialog_)
294  detachPressed (true);
295 
296  if (dialog_)
297  dialog_->restoreGeometry (_settings.value ("DialogGeometry").toByteArray ());
298 
299  _settings.endGroup ();
300 }
301 
302 //-----------------------------------------------------------------------------
303 
304 const QString& SideElement::name(){
305  return name_;
306 }
307 
308 //-----------------------------------------------------------------------------
309 
311  return widget_->isVisible();
312 }
313 
314 //-----------------------------------------------------------------------------
315 
316 QWidget const * SideElement::widget() {
317  return widget_;
318 }
319 
320 //-----------------------------------------------------------------------------
321 
322 SideElement::TopArea::TopArea (SideElement *_e) :
323  e_ (_e)
324 {
325 }
326 
327 //-----------------------------------------------------------------------------
328 
329 void SideElement::TopArea::mousePressEvent (QMouseEvent *_event)
330 {
331  e_->labelPress ();
332  _event->accept ();
333 }
334 
335 //=============================================================================
336 //=============================================================================
337 
void labelPress()
Called on mouse press.
Definition: SideElement.cc:155
void restoreState(QSettings &_settings)
restores the state
Definition: SideElement.cc:274
const QString & name()
return the name
Definition: SideElement.cc:304
void setActive(bool _active)
Set the element as active.
Definition: SideElement.cc:180
QWidget const * widget()
returns the pointer to the plugin tool widget
Definition: SideElement.cc:316
void saveState(QSettings &_settings)
saves the current state
Definition: SideElement.cc:264
bool active()
returns if the SideElement is active
Definition: SideElement.cc:310
void dialogClosed()
Called if a detached dialog was closed.
Definition: SideElement.cc:245
~SideElement()
Destructor.
Definition: SideElement.cc:142
Clickable area inside of the side element.
Definition: SideElement.hh:127
SideElement(SideArea *_parent, QWidget *_w, QString _name, QIcon *_icon, QWidget *_headerAreaWidget)
Definition: SideElement.cc:67
void detachPressed(bool _checked)
Called if the detach button was pressed.
Definition: SideElement.cc:206