QtSlideWindow.cc 15.2 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
44
45
46
47
48
49
50
51
52
53
54
55
56

//=============================================================================
//
//  CLASS QtSlideWindow
//
//=============================================================================


//== GLOBAL DEFINITIONS=========================================================

#define BACKGROUND_RED   0xff
#define BACKGROUND_GREEN 0xff
#define BACKGROUND_BLUE  0xff
#define BACKGROUND_ALPHA 0xcf

57
#define SLIDE_DURATION 400
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78


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


#include <OpenFlipper/common/GlobalOptions.hh>

#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QTimeLine>
#include <QGraphicsItemAnimation>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDialog>
#include <QVBoxLayout>

#include "QtSlideWindow.hh"
#include "QtGraphicsButton.hh"

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

79
QtSlideWindow::QtSlideWindow(QString _name, QGraphicsItem *_parent) :
80
81
82
83
84
85
  QGraphicsProxyWidget(_parent),
  name_(_name),
  mainWidget_(0),
  autohideButton_(0),
  detachButton_(0),
  hideAnimation_(0),
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
86
87
  dialog_(0),
  tempWidget_(new QWidget)
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{
  setCacheMode (QGraphicsItem::DeviceCoordinateCache);
  setWindowFrameMargins (2, 15, 2, 2);
  setZValue (2.0);

  QImage autohide (OpenFlipper::Options::iconDirStr () + OpenFlipper::Options::dirSeparator () + "button-autohide.png");
  QImage detach (OpenFlipper::Options::iconDirStr () + OpenFlipper::Options::dirSeparator () + "button-detach.png");

  autohideButton_ = new QtGraphicsButton (autohide, this, 12, 12);
  detachButton_ = new QtGraphicsButton (detach, this, 12, 12);

  autohideButton_->setCheckable (true);
  autohideButton_->setChecked (true);
  autohideButton_->setPos (geometry().width() - 12, -13);
  detachButton_->setPos (geometry().width() - 25, -13);

  connect (detachButton_, SIGNAL(pressed ()), this, SLOT(detachPressed ()));
  connect (autohideButton_, SIGNAL(pressed ()), this, SLOT(autohidePressed ()));

  hideTimeLine_ = new QTimeLine (SLIDE_DURATION);
  hideTimeLine_->setFrameRange (0, 100);
  
  hideAnimation_ = new QGraphicsItemAnimation;
  hideAnimation_->setItem (this);
  hideAnimation_->setTimeLine (hideTimeLine_);

Jan Möbius's avatar
Jan Möbius committed
114
  hideAnimation_->setTranslationAt (0.0, 0, geometry ().height ());
115
  for (int i = 0; i < geometry ().height (); ++i)
Jan Möbius's avatar
Jan Möbius committed
116
117
    hideAnimation_->setTranslationAt (0.25 + (i / (geometry ().height () * 2.0)), 0, geometry ().height () - i);
  hideAnimation_->setTranslationAt (1.0, 0, 0);
118
119

  hide ();
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
120
  connect (hideTimeLine_, SIGNAL(finished()), this, SLOT(timelineFinished()));
121
122

  setAcceptsHoverEvents (true);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
}

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

void QtSlideWindow::attachWidget (QWidget *_m)
{
  if (!_m)
    return;

  mainWidget_ = _m;
  mainWidget_->setParent(0);

  if (autohideButton_->isChecked ())
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
137
138
139
140
141
    tempWidget_->setGeometry (mainWidget_->geometry ());
    setWidget (tempWidget_);
    setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);

    show ();
142
143
144
145
146
    hideTimeLine_->setCurrentTime (0);
    hideAnimation_->setStep (0.0);
  }
  else
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
147
148
149
150
    setWidget (mainWidget_);
    setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);

    show ();
151
152
153
154
155
156
157
    hideTimeLine_->setCurrentTime (SLIDE_DURATION);
    hideAnimation_->setStep (1.0);
  }
  updateGeometry ();
}

//-----------------------------------------------------------------------------
158
#include <iostream>
159
160
161
162
void QtSlideWindow::detachWidget ()
{
  setWidget (0);
  hide ();
163
164
165
166
167

  if(mainWidget_) {
    mainWidget_->setParent(0);
    mainWidget_ = 0;
  }
168
169
170
171
172
173
174
175
176
177
178
179
180

  if (dialog_)
  {
    disconnect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));
    dialog_->close ();
    delete dialog_;
    dialog_ = 0;
  }
}

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

void QtSlideWindow::paintWindowFrame(QPainter *_painter,
Jan Möbius's avatar
Jan Möbius committed
181
182
                                     const QStyleOptionGraphicsItem* /*_option*/,
                                     QWidget* /*_widget*/ )
183
184
185
186
187
188
189
190
191
{
  int w = geometry().width();
  int h = geometry().height();

  _painter->setRenderHint(QPainter::Antialiasing, true);
  _painter->setBrush(QBrush(QColor(BACKGROUND_RED,
                                   BACKGROUND_GREEN,
                                   BACKGROUND_BLUE,
                                   BACKGROUND_ALPHA)));
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
192
193
194
195
  _painter->setPen(QColor(BACKGROUND_RED / 4,
                          BACKGROUND_GREEN / 4,
                          BACKGROUND_BLUE / 4,
                          BACKGROUND_ALPHA));
196
197
198
199
200
201
202
203
204
205
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
  _painter->drawRoundedRect(-2, -15, w + 4, h + 40, 4, 4);

  _painter->setPen(Qt::SolidLine);

  _painter->drawText(2,-13,w - 4, 11, Qt::AlignCenter, name_);

}

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

bool QtSlideWindow::windowFrameEvent(QEvent *_e)
{
  if (_e->type() == QEvent::GraphicsSceneMousePress ||
      _e->type() == QEvent::GraphicsSceneMouseRelease)
  {
    QGraphicsSceneMouseEvent *ge = dynamic_cast<QGraphicsSceneMouseEvent*>(_e);
    if (windowFrameSectionAt(ge->pos()) != Qt::TopSection)
    {
      _e->accept();
      return false;
    }
  }
  return QGraphicsProxyWidget::windowFrameEvent(_e);
}

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

Qt::WindowFrameSection QtSlideWindow::windowFrameSectionAt(const QPointF &_pos) const
{
  if (_pos.x() >= 2 && _pos.x() < geometry().width() - 2 - (13 * 2) &&
      _pos.y() >= -15 && _pos.y() <= -10)
  {
    return Qt::TopSection;
  }
  return Qt::NoSection;
}

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

void QtSlideWindow::hoverEnterEvent (QGraphicsSceneHoverEvent *)
{
237
  if (autohideButton_->isChecked () && hideTimeLine_->currentTime() != SLIDE_DURATION)
238
239
240
241
242
  {
    hideTimeLine_->setDirection (QTimeLine::Forward);
    if (hideTimeLine_->state () == QTimeLine::NotRunning)
      hideTimeLine_->start ();
  }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
243
244
  if (!mainWidget_->isVisible ())
  {
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
245
    mainWidget_->setGeometry (tempWidget_->geometry ());
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
246
247
248
249
    setWidget (mainWidget_);
    setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    show ();
  }
250
251
252
253
254
255
}

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

void QtSlideWindow::hoverLeaveEvent (QGraphicsSceneHoverEvent *)
{
256
257
  if (autohideButton_->isChecked () &&
      (!scene ()->mouseGrabberItem () || !childItems ().contains (scene ()->mouseGrabberItem ())))
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  {
    hideTimeLine_->setDirection (QTimeLine::Backward);
    if (hideTimeLine_->state () == QTimeLine::NotRunning)
      hideTimeLine_->start ();
  }
}

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

void QtSlideWindow::resizeEvent (QGraphicsSceneResizeEvent *_event)
{
  QGraphicsProxyWidget::resizeEvent (_event);
  if (hideAnimation_)
  {
    hideAnimation_->clear ();
Jan Möbius's avatar
Jan Möbius committed
273
    hideAnimation_->setTranslationAt (0.0, 0, geometry ().height ());
274
    for (int i = 0; i < geometry ().height (); ++i)
Jan Möbius's avatar
Jan Möbius committed
275
276
      hideAnimation_->setTranslationAt (0.25 + (i / (geometry ().height () * 2.0)), 0, geometry ().height () - i);
    hideAnimation_->setTranslationAt (1.0, 0, 0);
277
278
279
280
281
282
283
284
285
286
287
  }
}

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

void QtSlideWindow::moveEvent (QGraphicsSceneMoveEvent *_event)
{
  QGraphicsProxyWidget::moveEvent (_event);
  if (hideAnimation_)
  {
    hideAnimation_->clear ();
Jan Möbius's avatar
Jan Möbius committed
288
    hideAnimation_->setTranslationAt (0.0, 0, geometry ().height ());
289
    for (int i = 0; i < geometry ().height (); ++i)
Jan Möbius's avatar
Jan Möbius committed
290
291
      hideAnimation_->setTranslationAt (0.25 + (i / (geometry ().height () * 2.0)), 0, geometry ().height () - i);
    hideAnimation_->setTranslationAt (1.0, 0, 0);
292
293
294
295
296
297
298
299
  }
}

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

void QtSlideWindow::detachPressed ()
{
  setWidget (0);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
300
  dialog_ = new QDialog(0, Qt::Window);
301
302
303
304
  dialog_->setWindowTitle (name_);
  dialog_->setLayout (new QVBoxLayout);
  dialog_->resize (mainWidget_->size ());
  dialog_->layout ()->addWidget (mainWidget_);
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
305
306
307
308
309
310
311
312
313
314
315
  if (scene () && scene ()->views ()[0] && scene ()->views ()[0]->window ())
  {
    QWidget *w = scene ()->views ()[0]->window ();
    int x = (w->width () - mainWidget_->width ()) / 2;
    x += w->x ();
    x = qMax (0, x);
    int y = (w->height () - mainWidget_->height ()) / 2;
    y += w->y ();
    y = qMax (0, y);
    dialog_->move (x, y);
  }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
  hide ();
  dialog_->show ();
  mainWidget_->setAttribute(Qt::WA_DeleteOnClose, false);
  connect (dialog_, SIGNAL(finished (int)), this, SLOT(dialogClosed ()));

}

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

void QtSlideWindow::dialogClosed ()
{
  dialog_ = 0;
  mainWidget_->setParent(0);
  setWidget (mainWidget_);
  setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
  mainWidget_->setAttribute(Qt::WA_DeleteOnClose, true);
  show ();
  if (autohideButton_->isChecked ())
  {
    hideTimeLine_->setCurrentTime (0);
    hideAnimation_->setStep (0.0);
  }
  else
  {
    hideTimeLine_->setCurrentTime (SLIDE_DURATION);
    hideAnimation_->setStep (1.0);
  }
  updateGeometry ();

}

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

void QtSlideWindow::autohidePressed ()
{
  if (!autohideButton_->isChecked ())
  {
    hideTimeLine_->setDirection (QTimeLine::Forward);
    if (hideTimeLine_->state () == QTimeLine::NotRunning &&
        hideTimeLine_->currentTime () != SLIDE_DURATION)
      hideTimeLine_->start ();
  }
}

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

void QtSlideWindow::updateGeometry ()
{
364
365
366
367
  if (parentWidget () && widget ())
  {
    setPos (8, parentWidget ()->geometry ().height () - geometry ().height ());
    resize (parentWidget ()->geometry ().width () - 20,
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
368
            widget ()->size ().height ());
369
    
Jan Möbius's avatar
Jan Möbius committed
370
    if (autohideButton_) {
371
      autohideButton_->setPos (geometry().width() - 12, -13);
Jan Möbius's avatar
Jan Möbius committed
372
373
    }
    if (detachButton_) {
374
      detachButton_->setPos (geometry().width() - 25, -13);
Jan Möbius's avatar
Jan Möbius committed
375
    }
376
377
378
  }
}

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//-----------------------------------------------------------------------------

void QtSlideWindow::saveState (QSettings &_settings)
{
  _settings.setValue ("AutoHide", autohideButton_->isChecked ());
  _settings.setValue ("Detached", (dialog_ != 0));
  _settings.setValue ("WidgedGeometry", (mainWidget_) ? mainWidget_->saveGeometry (): QByteArray());
  _settings.setValue ("DialogGeometry", (dialog_) ? dialog_->saveGeometry (): QByteArray());
}

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

void QtSlideWindow::restoreState (QSettings &_settings)
{
  autohideButton_->setChecked (_settings.value ("AutoHide", autohideButton_->isChecked ()).toBool ());

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

  if (mainWidget_)
  {
    mainWidget_->restoreGeometry (_settings.value ("WidgedGeometry").toByteArray ());
    if (hideAnimation_)
    {
      hideAnimation_->clear ();
      hideAnimation_->setTranslationAt (0.0, 0, geometry ().height ());
      for (int i = 0; i < geometry ().height (); ++i)
        hideAnimation_->setTranslationAt (0.25 + (i / (geometry ().height () * 2.0)), 0, geometry ().height () - i);
      hideAnimation_->setTranslationAt (1.0, 0, 0);
    }
  }

  if (autohideButton_->isChecked ())
  {
    hideTimeLine_->setCurrentTime (0);
    hideAnimation_->setStep (0.0);
  }
  else
  {
    hideTimeLine_->setCurrentTime (SLIDE_DURATION);
    hideAnimation_->setStep (1.0);
422
423
424
425
426
427
428
    if (!mainWidget_->isVisible ())
    {
      mainWidget_->setGeometry (tempWidget_->geometry ());
      setWidget (mainWidget_);
      setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
      show ();
    }
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
429
430
431
432
433
434
  }

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

Jan Möbius's avatar
Dennis:  
Jan Möbius committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//-----------------------------------------------------------------------------

void QtSlideWindow::timelineFinished ()
{
  if (hideTimeLine_->currentValue () == 0.0)
  {
    tempWidget_->setGeometry (mainWidget_->geometry ());
    setWidget (tempWidget_);
    setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
    mainWidget_->setParent (0);
    mainWidget_->hide ();
    show ();
  }
}

450
451
//=============================================================================
//=============================================================================
Jan Möbius's avatar
Jan Möbius committed
452