SideElement.cc 9.52 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
42
43
\*===========================================================================*/


Jan Möbius's avatar
Dennis:    
Jan Möbius committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

//== INCLUDES =================================================================

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QAction>
#include <QFrame>
#include <QMouseEvent>
#include <QFont>
#include <QDialog>

#include <OpenFlipper/common/GlobalOptions.hh>

#include "SideElement.hh"
60
#include <iostream>
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
61
62
63

//== IMPLEMENTATION ==========================================================

Dirk Wilden's avatar
Dirk Wilden committed
64
SideElement::SideElement (SideArea *_parent, QWidget *_w, QString _name, QIcon* _icon) :
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
65
66
67
  parent_ (_parent),
  widget_ (_w),
  name_ (_name),
Dirk Wilden's avatar
Dirk Wilden committed
68
  icon_ (_icon),
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
69
70
71
72
73
74
75
76
  active_ (0),
  dialog_ (0)
{
  QFont font;
  font.setBold (false);

  QHBoxLayout *hl = new QHBoxLayout;

77
  SideElement::TopArea *tra = new SideElement::TopArea (this);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
78
79
80
81

  label_ = new QLabel (_name);
  label_->setFont (font);

Dirk Wilden's avatar
Dirk Wilden committed
82
83
84
85
86
87
88
89
90
91
92
  iconHolder_ = new QLabel ();

  if (icon_ != 0)
    iconHolder_->setPixmap( icon_->pixmap(22,22) );
  else{
    QPixmap pic(QSize(22,22));
    pic.fill( QColor(0,0,0,0) );

    iconHolder_->setPixmap( pic );
  }

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
93
  detachButton_ = new QToolButton ();
94
  detachButton_->setAutoRaise(true);
Dirk Wilden's avatar
Dirk Wilden committed
95
  hl->addWidget (iconHolder_);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
96
97
98
99
  hl->addWidget (label_);
  hl->addStretch (1);
  hl->addWidget (detachButton_);

100

101
102
103
  QIcon detachIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png");
  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Active);
  detachIcon.addPixmap(QPixmap(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach_over.png"), QIcon::Selected);
104

105
106
  detachAction_ = new QAction ( detachIcon, "", this);
  detachAction_->setToolTip( tr("Show as separate window") );
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
107
108
  detachAction_->setCheckable (true);
  detachButton_->setDefaultAction (detachAction_);
109

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
110
111
  connect (detachAction_, SIGNAL (triggered (bool)), this, SLOT (detachPressed (bool)));

112
  tra->setLayout (hl);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
113
114
115
116
117
118
119

  QFrame *f = new QFrame ();
  f->setFrameShape (QFrame::HLine);

  mainLayout_ = new QVBoxLayout;

  mainLayout_->addWidget (f);
120
  mainLayout_->addWidget (tra);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  mainLayout_->addWidget (_w);
  mainLayout_->setSpacing (0);
  mainLayout_->setContentsMargins (1,1,1,1);
  setLayout (mainLayout_);

  _w->hide ();
}

//-----------------------------------------------------------------------------

SideElement::~SideElement ()
{
  if (dialog_)
    dialog_->close ();
  widget_->setParent (0);
}

//-----------------------------------------------------------------------------

void SideElement::labelPress ()
{
  if (dialog_)
  {
    dialog_->raise ();
    dialog_->activateWindow ();
  }
  else
  {
    active_ = !active_;
    if (active_)
      widget_->show ();
    else
      widget_->hide ();

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    QFont font;
    font.setBold (active_);
    label_->setFont (font);
  }
}

//-----------------------------------------------------------------------------

void SideElement::setActive(bool _active)
{
  if ( dialog_ )
  {
    dialog_->raise ();
    dialog_->activateWindow ();
  }
  else
  {
    active_ = _active;
    if (active_)
      widget_->show ();
    else
      widget_->hide ();

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
178
179
180
181
182
183
184
185
186
187
188
189
190
    QFont font;
    font.setBold (active_);
    label_->setFont (font);
  }
}

//-----------------------------------------------------------------------------

void SideElement::detachPressed (bool checked_)
{
  if (checked_)
  {
    mainLayout_->removeWidget (widget_);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
191
    dialog_ = new QDialog(0, Qt::Window);
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
192
    dialog_->setWindowTitle (name_);
193
    dialog_->setWindowIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"button-detach.png"));
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
194
195
    dialog_->setLayout (new QVBoxLayout);
    dialog_->resize (widget_->size ());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
196
197
198
199
200
201
202
203
204
205
    if (window ())
    {
      int x = (window ()->width () - widget_->width ()) / 2;
      x += window ()->x ();
      x = qMax (0, x);
      int y = (window ()->height () - widget_->height ()) / 2;
      y += window ()->y ();
      y = qMax (0, y);
      dialog_->move (x, y);
    }
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    dialog_->layout ()->addWidget (widget_);
    dialog_->show ();
    widget_->setAttribute(Qt::WA_DeleteOnClose, false);
    widget_->show ();

    connect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));

    QFont font;
    font.setBold (true);
    font.setItalic (true);
    label_->setFont (font);
  }
  else if (dialog_)
    dialog_->close ();

}

//-----------------------------------------------------------------------------

void SideElement::dialogClosed ()
{
  dialog_ = 0;
  mainLayout_->addWidget (widget_);
  widget_->setAttribute(Qt::WA_DeleteOnClose, true);

  if (active_)
    widget_->show ();
  else
    widget_->hide ();

  detachAction_->setChecked (false);
  QFont font;
  font.setBold (active_);
  label_->setFont (font);
}

//-----------------------------------------------------------------------------

void SideElement::saveState (QSettings &_settings)
{
  _settings.beginGroup (name_);
247
  _settings.setValue ("Active", active());
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  _settings.setValue ("Detached", (dialog_ != 0));
  _settings.setValue ("DialogGeometry", (dialog_) ? dialog_->saveGeometry (): QByteArray());
  _settings.endGroup ();
}

//-----------------------------------------------------------------------------
void SideElement::restoreState (QSettings &_settings)
{
  _settings.beginGroup (name_);

  active_ = _settings.value ("Active", active_).toBool ();

  if (active_)
    widget_->show ();
  else
    widget_->hide ();

  QFont font;
  font.setBold (active_);
  label_->setFont (font);

  if (_settings.value ("Detached", false).toBool () && !dialog_)
    detachPressed (true);

  if (dialog_)
    dialog_->restoreGeometry (_settings.value ("DialogGeometry").toByteArray ());

  _settings.endGroup ();
}

//-----------------------------------------------------------------------------

Dirk Wilden's avatar
Dirk Wilden committed
280
281
282
283
284
const QString& SideElement::name(){
  return name_;
}

//-----------------------------------------------------------------------------
285

286
287
288
289
290
291
bool SideElement::active(){
  return widget_->isVisible();
}

//-----------------------------------------------------------------------------

292
293
294
295
296
297
QWidget const * SideElement::widget() {
  return widget_;
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Dennis:    
Jan Möbius committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
SideElement::TopArea::TopArea (SideElement *_e) :
  e_ (_e)
{
}

//-----------------------------------------------------------------------------

void SideElement::TopArea::mousePressEvent (QMouseEvent *_event)
{
  e_->labelPress ();
  _event->accept ();
}

//=============================================================================
//=============================================================================