Developer Documentation
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 
45 
46 //== INCLUDES =================================================================
47 
48 #include <QVBoxLayout>
49 #include <QLabel>
50 #include <QToolButton>
51 #include <QAction>
52 #include <QMouseEvent>
53 #include <QDialog>
54 
56 
57 #include "SideElement.hh"
58 
59 //== IMPLEMENTATION ==========================================================
60 
61 SideElement::SideElement (SideArea *_parent, QWidget *_w, QString _name, QIcon* _icon,
62  QWidget *_headerAreaWidget) :
63  parent_ (_parent),
64  widget_ (_w),
65  headerAreaWidget_(_headerAreaWidget),
66  name_ (_name),
67  icon_ (_icon),
68  active_ (0),
69  dialog_ (0)
70 {
71  QFont font;
72  font.setBold (false);
73 
74  QHBoxLayout *hl = new QHBoxLayout;
75  hl->setContentsMargins(2, 2, 2, 2);
76 
78 
79  label_ = new QLabel (_name);
80  label_->setFont (font);
81 
82  iconHolder_ = new QLabel ();
83 
84  if (icon_ != 0)
85  iconHolder_->setPixmap( icon_->pixmap(22,22) );
86  else{
87  QPixmap pic(QSize(22,22));
88  pic.fill( QColor(0,0,0,0) );
89 
90  iconHolder_->setPixmap( pic );
91  }
92 
93  detachButton_ = new QToolButton ();
94  detachButton_->setAutoRaise(true);
95  hl->addWidget (iconHolder_);
96  hl->addWidget (label_);
97  if (headerAreaWidget_) {
98  headerAreaWidget_->setVisible(false);
99  connect(this, SIGNAL(toggleActive(bool)), headerAreaWidget_, SLOT(setVisible(bool)));
100  hl->addWidget (headerAreaWidget_);
101  }
102  hl->addStretch(1);
103  hl->addWidget (detachButton_);
104 
105 
106  QIcon detachIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png");
107  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Active);
108  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Selected);
109 
110  detachAction_ = new QAction ( detachIcon, "", this);
111  detachAction_->setToolTip( tr("Show as separate window") );
112  detachAction_->setCheckable (true);
113  detachButton_->setDefaultAction (detachAction_);
114 
115  connect (detachAction_, SIGNAL (triggered (bool)), this, SLOT (detachPressed (bool)));
116 
117  tra->setLayout (hl);
118 
119  QFrame *f = new QFrame ();
120  f->setFrameShape (QFrame::HLine);
121 
122  mainLayout_ = new QVBoxLayout;
123 
124  mainLayout_->addWidget (f);
125  mainLayout_->addWidget (tra);
126  mainLayout_->addWidget (_w);
127  mainLayout_->setSpacing (0);
128  mainLayout_->setContentsMargins(0, 0, 0, 0);
129  setLayout (mainLayout_);
130 
131  _w->hide ();
132 }
133 
134 //-----------------------------------------------------------------------------
135 
137 {
138  if (dialog_) {
139  disconnect(dialog_,SIGNAL(finished(int)),this,SLOT(dialogClosed()));
140  dialog_->close ();
141  }
142  widget_->setParent (0);
143  if (headerAreaWidget_)
144  headerAreaWidget_->setParent(0);
145 }
146 
147 //-----------------------------------------------------------------------------
148 
150 {
151  if (dialog_)
152  {
153  dialog_->raise ();
154  dialog_->activateWindow ();
155  }
156  else
157  {
158  active_ = !active_;
159  if (active_)
160  widget_->show ();
161  else
162  widget_->hide ();
163 
164  QFont font;
165  font.setBold (active_);
166  label_->setFont (font);
167 
168  emit toggleActive(active_);
169  }
170 }
171 
172 //-----------------------------------------------------------------------------
173 
174 void SideElement::setActive(bool _active)
175 {
176  if ( dialog_ )
177  {
178  dialog_->raise ();
179  dialog_->activateWindow ();
180  }
181  else
182  {
183  const bool doEmit = (active_ != _active);
184  active_ = _active;
185  if (active_)
186  widget_->show ();
187  else
188  widget_->hide ();
189 
190  QFont font;
191  font.setBold (active_);
192  label_->setFont (font);
193 
194  if (doEmit) emit toggleActive(active_);
195  }
196 }
197 
198 //-----------------------------------------------------------------------------
199 
200 void SideElement::detachPressed (bool checked_)
201 {
202  if (checked_)
203  {
204  mainLayout_->removeWidget (widget_);
205  dialog_ = new QDialog(0, Qt::Window);
206  dialog_->setWindowTitle (name_);
207  dialog_->setWindowIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png"));
208  dialog_->setLayout (new QVBoxLayout);
209  dialog_->resize (widget_->size ());
210  if (window ())
211  {
212  int x = (window ()->width () - widget_->width ()) / 2;
213  x += window ()->x ();
214  x = qMax (0, x);
215  int y = (window ()->height () - widget_->height ()) / 2;
216  y += window ()->y ();
217  y = qMax (0, y);
218  dialog_->move (x, y);
219  }
220  dialog_->layout ()->addWidget (widget_);
221  dialog_->show ();
222  widget_->setAttribute(Qt::WA_DeleteOnClose, false);
223  widget_->show ();
224 
225  connect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));
226 
227  QFont font;
228  font.setBold (true);
229  font.setItalic (true);
230  label_->setFont (font);
231  }
232  else if (dialog_)
233  dialog_->close ();
234 
235 }
236 
237 //-----------------------------------------------------------------------------
238 
240 {
241  dialog_ = 0;
242  mainLayout_->addWidget (widget_);
243  widget_->setAttribute(Qt::WA_DeleteOnClose, true);
244 
245  if (active_)
246  widget_->show ();
247  else
248  widget_->hide ();
249 
250  detachAction_->setChecked (false);
251  QFont font;
252  font.setBold (active_);
253  label_->setFont (font);
254 }
255 
256 //-----------------------------------------------------------------------------
257 
258 void SideElement::saveState (QSettings &_settings)
259 {
260  _settings.beginGroup (name_);
261  _settings.setValue ("Active", active());
262  _settings.setValue ("Detached", (dialog_ != 0));
263  _settings.setValue ("DialogGeometry", (dialog_) ? dialog_->saveGeometry (): QByteArray());
264  _settings.endGroup ();
265 }
266 
267 //-----------------------------------------------------------------------------
268 void SideElement::restoreState (QSettings &_settings)
269 {
270  _settings.beginGroup (name_);
271 
272  bool active = _settings.value ("Active", active_).toBool ();
273  const bool doEmit = (active_ != active);
274  active_ = active;
275 
276  if (active_)
277  widget_->show ();
278  else
279  widget_->hide ();
280 
281  QFont font;
282  font.setBold (active_);
283  label_->setFont (font);
284 
285  if (doEmit) emit toggleActive(active_);
286 
287  if (_settings.value ("Detached", false).toBool () && !dialog_)
288  detachPressed (true);
289 
290  if (dialog_)
291  dialog_->restoreGeometry (_settings.value ("DialogGeometry").toByteArray ());
292 
293  _settings.endGroup ();
294 }
295 
296 //-----------------------------------------------------------------------------
297 
298 const QString& SideElement::name(){
299  return name_;
300 }
301 
302 //-----------------------------------------------------------------------------
303 
305  return widget_->isVisible();
306 }
307 
308 //-----------------------------------------------------------------------------
309 
310 QWidget const * SideElement::widget() {
311  return widget_;
312 }
313 
314 //-----------------------------------------------------------------------------
315 
316 SideElement::TopArea::TopArea (SideElement *_e) :
317  e_ (_e)
318 {
319 }
320 
321 //-----------------------------------------------------------------------------
322 
323 void SideElement::TopArea::mousePressEvent (QMouseEvent *_event)
324 {
325  e_->labelPress ();
326  _event->accept ();
327 }
328 
329 //=============================================================================
330 //=============================================================================
331 
bool active()
returns if the SideElement is active
Definition: SideElement.cc:304
SideElement(SideArea *_parent, QWidget *_w, QString _name, QIcon *_icon, QWidget *_headerAreaWidget)
Definition: SideElement.cc:61
void detachPressed(bool _checked)
Called if the detach button was pressed.
Definition: SideElement.cc:200
void saveState(QSettings &_settings)
saves the current state
Definition: SideElement.cc:258
void restoreState(QSettings &_settings)
restores the state
Definition: SideElement.cc:268
const QString & name()
return the name
Definition: SideElement.cc:298
QWidget const * widget()
returns the pointer to the plugin tool widget
Definition: SideElement.cc:310
void setActive(bool _active)
Set the element as active.
Definition: SideElement.cc:174
void dialogClosed()
Called if a detached dialog was closed.
Definition: SideElement.cc:239
~SideElement()
Destructor.
Definition: SideElement.cc:136
void labelPress()
Called on mouse press.
Definition: SideElement.cc:149
Clickable area inside of the side element.
Definition: SideElement.hh:121