3
* VBox frontends: Qt GUI ("VirtualBox"):
4
* VBoxMiniToolBar class declaration & implementation. This is the toolbar shown on fullscreen mode.
8
* Copyright (C) 2009 Sun Microsystems, Inc.
10
* This file is part of VirtualBox Open Source Edition (OSE), as
11
* available from http://www.virtualbox.org. This file is free software;
12
* you can redistribute it and/or modify it under the terms of the GNU
13
* General Public License (GPL) as published by the Free Software
14
* Foundation, in version 2 as it comes in the "COPYING" file of the
15
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19
* Clara, CA 95054 USA or visit http://www.sun.com if you need
20
* additional information or have any questions.
24
#include "VBoxGlobal.h"
25
#include "VBoxMiniToolBar.h"
29
#include <QDesktopWidget>
33
#include <QPaintEvent>
38
#include <QToolButton>
40
VBoxMiniToolBar::VBoxMiniToolBar (QWidget *aParent, Alignment aAlignment, bool aActive, bool aAutoHide)
41
: VBoxToolBar (aParent)
45
, mAutoHide (aAutoHide)
46
, mSlideToScreen (true)
47
, mHideAfterSlide (false)
48
, mAutoHideCounter (0)
49
, mAlignment (aAlignment)
52
, mAutoScrollDelay (100)
53
, mAutoHideTotalCounter (10)
55
AssertMsg (parentWidget(), ("Parent widget must be set!!!\n"));
58
setIconSize (QSize (16, 16));
59
setMouseTracking (mAutoHide);
62
/* Left margin of tool-bar */
63
mMargins << widgetForAction (addWidget (new QWidget (this)));
66
mAutoHideAct = new QAction (this);
67
mAutoHideAct->setIcon (VBoxGlobal::iconSet (":/pin_16px.png"));
68
mAutoHideAct->setToolTip (tr ("Always show the toolbar"));
69
mAutoHideAct->setCheckable (true);
70
mAutoHideAct->setChecked (!mAutoHide);
71
connect (mAutoHideAct, SIGNAL (toggled (bool)), this, SLOT (togglePushpin (bool)));
72
addAction (mAutoHideAct);
74
/* Left menu margin */
75
mSpacings << widgetForAction (addWidget (new QWidget (this)));
77
/* Right menu margin */
78
mInsertPosition = addWidget (new QWidget (this));
79
mSpacings << widgetForAction (mInsertPosition);
81
/* Left label margin */
82
mLabelMargins << widgetForAction (addWidget (new QWidget (this)));
84
/* Insert a label for VM Name */
85
mDisplayLabel = new QLabel (this);
86
mDisplayLabel->setAlignment (Qt::AlignCenter);
87
addWidget (mDisplayLabel);
89
/* Right label margin */
90
mLabelMargins << widgetForAction (addWidget (new QWidget (this)));
93
QAction *restoreAct = new QAction (this);
94
restoreAct->setIcon (VBoxGlobal::iconSet (":/restore_16px.png"));
95
restoreAct->setToolTip (tr ("Exit Full Screen or Seamless Mode"));
96
connect (restoreAct, SIGNAL (triggered()), this, SIGNAL (exitAction()));
97
addAction (restoreAct);
100
QAction *closeAct = new QAction (this);
101
closeAct->setIcon (VBoxGlobal::iconSet (":/close_16px.png"));
102
closeAct->setToolTip (tr ("Close VM"));
103
connect (closeAct, SIGNAL (triggered()), this, SIGNAL (closeAction()));
104
addAction (closeAct);
106
/* Right margin of tool-bar */
107
mMargins << widgetForAction (addWidget (new QWidget (this)));
110
VBoxMiniToolBar& VBoxMiniToolBar::operator<< (QList <QMenu*> aMenus)
112
for (int i = 0; i < aMenus.size(); ++ i)
114
QAction *action = aMenus [i]->menuAction();
115
insertAction (mInsertPosition, action);
116
if (QToolButton *button = qobject_cast <QToolButton*> (widgetForAction (action)))
118
button->setPopupMode (QToolButton::InstantPopup);
119
button->setAutoRaise (true);
121
if (i != aMenus.size() - 1)
122
mSpacings << widgetForAction (insertWidget (mInsertPosition, new QWidget (this)));
127
void VBoxMiniToolBar::setSeamlessMode (bool aSeamless)
129
mSeamless = aSeamless;
132
/* Update the display text, usually the VM Name */
133
void VBoxMiniToolBar::setDisplayText (const QString &aText)
135
if (mDisplayLabel->text() != aText)
137
/* Update toolbar label */
138
mDisplayLabel->setText (aText);
144
updateDisplay (!mAutoHide, false);
148
bool VBoxMiniToolBar::isAutoHide() const
153
void VBoxMiniToolBar::updateDisplay (bool aShow, bool aSetHideFlag)
155
mAutoHideCounter = 0;
157
setMouseTracking (mAutoHide);
168
mHideAfterSlide = false;
169
mSlideToScreen = true;
172
mScrollTimer.start (mScrollDelay, this);
174
else if (mActive) show();
177
mAutoScrollTimer.start (mAutoScrollDelay, this);
179
mAutoScrollTimer.stop();
187
mHideAfterSlide = true;
188
mSlideToScreen = false;
190
mScrollTimer.start (mScrollDelay, this);
196
mAutoScrollTimer.start (mAutoScrollDelay, this);
198
mAutoScrollTimer.stop();
202
void VBoxMiniToolBar::mouseMoveEvent (QMouseEvent *aEvent)
204
if (!mHideAfterSlide)
206
mSlideToScreen = true;
207
mScrollTimer.start (mScrollDelay, this);
210
QToolBar::mouseMoveEvent (aEvent);
213
/* Handles auto hide feature of the toolbar */
214
void VBoxMiniToolBar::timerEvent (QTimerEvent *aEvent)
216
if (aEvent->timerId() == mScrollTimer.timerId())
218
QRect screen = mSeamless ? QApplication::desktop()->availableGeometry (window()) :
219
QApplication::desktop()->screenGeometry (window());
224
if (((mPositionY == screen.y()) && mSlideToScreen) ||
225
((mPositionY == screen.y() - height() + 1) && !mSlideToScreen))
230
mHideAfterSlide = false;
235
mSlideToScreen ? ++ mPositionY : -- mPositionY;
240
if (((mPositionY == screen.y() + screen.height() - height()) && mSlideToScreen) ||
241
((mPositionY == screen.y() + screen.height() - 1) && !mSlideToScreen))
246
mHideAfterSlide = false;
251
mSlideToScreen ? -- mPositionY : ++ mPositionY;
257
move (mapFromScreen (QPoint (mPositionX, mPositionY)));
258
emit geometryUpdated();
260
else if (aEvent->timerId() == mAutoScrollTimer.timerId())
262
QRect rect = this->rect();
263
QPoint cursor_pos = QCursor::pos();
264
QPoint p = mapFromGlobal (cursor_pos);
266
if (!rect.contains (p))
270
if (mAutoHideCounter == mAutoHideTotalCounter)
272
mSlideToScreen = false;
273
mScrollTimer.start (mScrollDelay, this);
277
mAutoHideCounter = 0;
280
QWidget::timerEvent (aEvent);
283
void VBoxMiniToolBar::showEvent (QShowEvent *aEvent)
287
/* Tool-bar margins */
288
foreach (QWidget *margin, mMargins)
289
margin->setMinimumWidth (height());
291
/* Tool-bar spacings */
292
foreach (QWidget *spacing, mSpacings)
293
spacing->setMinimumWidth (5);
296
foreach (QWidget *lableMargin, mLabelMargins)
297
lableMargin->setMinimumWidth (15);
305
VBoxToolBar::showEvent (aEvent);
308
void VBoxMiniToolBar::paintEvent (QPaintEvent *aEvent)
311
painter.begin (this);
312
painter.fillRect (aEvent->rect(), palette().brush (QPalette::Window));
314
VBoxToolBar::paintEvent (aEvent);
317
void VBoxMiniToolBar::togglePushpin (bool aOn)
320
updateDisplay (!mAutoHide, false);
323
void VBoxMiniToolBar::initialize()
325
/* Resize to sizehint */
328
/* Update geometry */
333
void VBoxMiniToolBar::recreateMask()
335
int edgeShift = height();
344
points [2] = edgeShift;
345
points [3] = height();
347
points [4] = width() - edgeShift;
348
points [5] = height();
350
points [6] = width();
357
points [0] = edgeShift;
361
points [3] = height();
363
points [4] = width();
364
points [5] = height();
366
points [6] = width() - edgeShift;
374
/* Make sure any old mask is removed first */
376
/* Set the new mask */
378
polygon.setPoints (4, points);
382
void VBoxMiniToolBar::moveToBase()
384
QRect screen = mSeamless ? QApplication::desktop()->availableGeometry (window()) :
385
QApplication::desktop()->screenGeometry (window());
386
mPositionX = screen.x() + screen.width() / 2 - width() / 2;
391
mPositionY = screen.y() - height() + 1;
396
mPositionY = screen.y() + screen.height() - 1;
405
move (mapFromScreen (QPoint (mPositionX, mPositionY)));
408
QPoint VBoxMiniToolBar::mapFromScreen (const QPoint &aPoint)
410
QPoint globalPosition = parentWidget()->mapFromGlobal (aPoint);
411
QRect fullArea = QApplication::desktop()->screenGeometry (window());
412
QRect realArea = mSeamless ? QApplication::desktop()->availableGeometry (window()) :
413
QApplication::desktop()->screenGeometry (window());
414
QPoint shiftToReal (realArea.topLeft() - fullArea.topLeft());
415
return globalPosition + shiftToReal;