~ubuntu-branches/ubuntu/trusty/virtualbox-ose/trusty

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/widgets/VBoxMiniToolBar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2009-12-18 16:44:29 UTC
  • mfrom: (0.3.3 upstream) (0.4.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091218164429-jd34ccexpv5na11a
Tags: 3.1.2-dfsg-1ubuntu1
* Merge from Debian unstable (LP: #498219), remaining changes:
  - Disable update action
    - debian/patches/u01-disable-update-action.dpatch
  - VirtualBox should go in Accessories, not in System tools (LP: #288590)
    - debian/virtualbox-ose-qt.files/virtualbox-ose.desktop
  - Add Apport hook
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Add Launchpad integration
    - debian/control
    - debian/lpi-bug.xpm
    - debian/patches/u02-lp-integration.dpatch
* Fixes the following bugs:
  - Kernel module fails to build with Linux >= 2.6.32 (LP: #474625)
  - X.Org drivers need to be rebuilt against X-Server 1.7 (LP: #495935)
  - The *-source packages try to build the kernel modules even though the
    kernel headers aren't available (LP: #473334)
* Replace *-source packages with transitional packages for *-dkms.
* Adapt u01-disable-update-action.dpatch and u02-lp-integration.dpatch for
  new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 *
 
3
 * VBox frontends: Qt GUI ("VirtualBox"):
 
4
 * VBoxMiniToolBar class declaration & implementation. This is the toolbar shown on fullscreen mode.
 
5
 */
 
6
 
 
7
/*
 
8
 * Copyright (C) 2009 Sun Microsystems, Inc.
 
9
 *
 
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.
 
17
 *
 
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.
 
21
 */
 
22
 
 
23
/* VBox includes */
 
24
#include "VBoxGlobal.h"
 
25
#include "VBoxMiniToolBar.h"
 
26
 
 
27
/* Qt includes */
 
28
#include <QCursor>
 
29
#include <QDesktopWidget>
 
30
#include <QLabel>
 
31
#include <QMenu>
 
32
#include <QPainter>
 
33
#include <QPaintEvent>
 
34
#include <QPolygon>
 
35
#include <QRect>
 
36
#include <QRegion>
 
37
#include <QTimer>
 
38
#include <QToolButton>
 
39
 
 
40
VBoxMiniToolBar::VBoxMiniToolBar (QWidget *aParent, Alignment aAlignment, bool aActive, bool aAutoHide)
 
41
    : VBoxToolBar (aParent)
 
42
    , mActive (aActive)
 
43
    , mPolished (false)
 
44
    , mSeamless (false)
 
45
    , mAutoHide (aAutoHide)
 
46
    , mSlideToScreen (true)
 
47
    , mHideAfterSlide (false)
 
48
    , mAutoHideCounter (0)
 
49
    , mAlignment (aAlignment)
 
50
    , mAnimated (true)
 
51
    , mScrollDelay (10)
 
52
    , mAutoScrollDelay (100)
 
53
    , mAutoHideTotalCounter (10)
 
54
{
 
55
    AssertMsg (parentWidget(), ("Parent widget must be set!!!\n"));
 
56
 
 
57
    /* Various options */
 
58
    setIconSize (QSize (16, 16));
 
59
    setMouseTracking (mAutoHide);
 
60
    setVisible (false);
 
61
 
 
62
    /* Left margin of tool-bar */
 
63
    mMargins << widgetForAction (addWidget (new QWidget (this)));
 
64
 
 
65
    /* Add pushpin */
 
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);
 
73
 
 
74
    /* Left menu margin */
 
75
    mSpacings << widgetForAction (addWidget (new QWidget (this)));
 
76
 
 
77
    /* Right menu margin */
 
78
    mInsertPosition = addWidget (new QWidget (this));
 
79
    mSpacings << widgetForAction (mInsertPosition);
 
80
 
 
81
    /* Left label margin */
 
82
    mLabelMargins << widgetForAction (addWidget (new QWidget (this)));
 
83
 
 
84
    /* Insert a label for VM Name */
 
85
    mDisplayLabel = new QLabel (this);
 
86
    mDisplayLabel->setAlignment (Qt::AlignCenter);
 
87
    addWidget (mDisplayLabel);
 
88
 
 
89
    /* Right label margin */
 
90
    mLabelMargins << widgetForAction (addWidget (new QWidget (this)));
 
91
 
 
92
    /* Exit action */
 
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);
 
98
 
 
99
    /* Close action */
 
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);
 
105
 
 
106
    /* Right margin of tool-bar */
 
107
    mMargins << widgetForAction (addWidget (new QWidget (this)));
 
108
}
 
109
 
 
110
VBoxMiniToolBar& VBoxMiniToolBar::operator<< (QList <QMenu*> aMenus)
 
111
{
 
112
    for (int i = 0; i < aMenus.size(); ++ i)
 
113
    {
 
114
        QAction *action = aMenus [i]->menuAction();
 
115
        insertAction (mInsertPosition, action);
 
116
        if (QToolButton *button = qobject_cast <QToolButton*> (widgetForAction (action)))
 
117
        {
 
118
            button->setPopupMode (QToolButton::InstantPopup);
 
119
            button->setAutoRaise (true);
 
120
        }
 
121
        if (i != aMenus.size() - 1)
 
122
            mSpacings << widgetForAction (insertWidget (mInsertPosition, new QWidget (this)));
 
123
    }
 
124
    return *this;
 
125
}
 
126
 
 
127
void VBoxMiniToolBar::setSeamlessMode (bool aSeamless)
 
128
{
 
129
    mSeamless = aSeamless;
 
130
}
 
131
 
 
132
/* Update the display text, usually the VM Name */
 
133
void VBoxMiniToolBar::setDisplayText (const QString &aText)
 
134
{
 
135
    if (mDisplayLabel->text() != aText)
 
136
    {
 
137
        /* Update toolbar label */
 
138
        mDisplayLabel->setText (aText);
 
139
 
 
140
        /* Reinitialize */
 
141
        initialize();
 
142
 
 
143
        if (!isHidden())
 
144
            updateDisplay (!mAutoHide, false);
 
145
    }
 
146
}
 
147
 
 
148
bool VBoxMiniToolBar::isAutoHide() const
 
149
{
 
150
    return mAutoHide;
 
151
}
 
152
 
 
153
void VBoxMiniToolBar::updateDisplay (bool aShow, bool aSetHideFlag)
 
154
{
 
155
    mAutoHideCounter = 0;
 
156
 
 
157
    setMouseTracking (mAutoHide);
 
158
 
 
159
    if (aShow)
 
160
    {
 
161
        if (isHidden())
 
162
            moveToBase();
 
163
 
 
164
        if (mAnimated)
 
165
        {
 
166
            if (aSetHideFlag)
 
167
            {
 
168
                mHideAfterSlide = false;
 
169
                mSlideToScreen = true;
 
170
            }
 
171
            if (mActive) show();
 
172
            mScrollTimer.start (mScrollDelay, this);
 
173
        }
 
174
        else if (mActive) show();
 
175
 
 
176
        if (mAutoHide)
 
177
            mAutoScrollTimer.start (mAutoScrollDelay, this);
 
178
        else
 
179
            mAutoScrollTimer.stop();
 
180
    }
 
181
    else
 
182
    {
 
183
        if (mAnimated)
 
184
        {
 
185
            if (aSetHideFlag)
 
186
            {
 
187
                mHideAfterSlide = true;
 
188
                mSlideToScreen = false;
 
189
            }
 
190
            mScrollTimer.start (mScrollDelay, this);
 
191
        }
 
192
        else
 
193
            hide();
 
194
 
 
195
        if (mAutoHide)
 
196
            mAutoScrollTimer.start (mAutoScrollDelay, this);
 
197
        else
 
198
            mAutoScrollTimer.stop();
 
199
    }
 
200
}
 
201
 
 
202
void VBoxMiniToolBar::mouseMoveEvent (QMouseEvent *aEvent)
 
203
{
 
204
    if (!mHideAfterSlide)
 
205
    {
 
206
        mSlideToScreen = true;
 
207
        mScrollTimer.start (mScrollDelay, this);
 
208
    }
 
209
 
 
210
    QToolBar::mouseMoveEvent (aEvent);
 
211
}
 
212
 
 
213
/* Handles auto hide feature of the toolbar */
 
214
void VBoxMiniToolBar::timerEvent (QTimerEvent *aEvent)
 
215
{
 
216
    if (aEvent->timerId() == mScrollTimer.timerId())
 
217
    {
 
218
        QRect screen = mSeamless ? QApplication::desktop()->availableGeometry (window()) :
 
219
                                   QApplication::desktop()->screenGeometry (window());
 
220
        switch (mAlignment)
 
221
        {
 
222
            case AlignTop:
 
223
            {
 
224
                if (((mPositionY == screen.y()) && mSlideToScreen) ||
 
225
                    ((mPositionY == screen.y() - height() + 1) && !mSlideToScreen))
 
226
                {
 
227
                    mScrollTimer.stop();
 
228
                    if (mHideAfterSlide)
 
229
                    {
 
230
                        mHideAfterSlide = false;
 
231
                        hide();
 
232
                    }
 
233
                    return;
 
234
                }
 
235
                mSlideToScreen ? ++ mPositionY : -- mPositionY;
 
236
                break;
 
237
            }
 
238
            case AlignBottom:
 
239
            {
 
240
                if (((mPositionY == screen.y() + screen.height() - height()) && mSlideToScreen) ||
 
241
                    ((mPositionY == screen.y() + screen.height() - 1) && !mSlideToScreen))
 
242
                {
 
243
                    mScrollTimer.stop();
 
244
                    if (mHideAfterSlide)
 
245
                    {
 
246
                        mHideAfterSlide = false;
 
247
                        hide();
 
248
                    }
 
249
                    return;
 
250
                }
 
251
                mSlideToScreen ? -- mPositionY : ++ mPositionY;
 
252
                break;
 
253
            }
 
254
            default:
 
255
                break;
 
256
        }
 
257
        move (mapFromScreen (QPoint (mPositionX, mPositionY)));
 
258
        emit geometryUpdated();
 
259
    }
 
260
    else if (aEvent->timerId() == mAutoScrollTimer.timerId())
 
261
    {
 
262
        QRect rect = this->rect();
 
263
        QPoint cursor_pos = QCursor::pos();
 
264
        QPoint p = mapFromGlobal (cursor_pos);
 
265
 
 
266
        if (!rect.contains (p))
 
267
        {
 
268
            ++ mAutoHideCounter;
 
269
 
 
270
            if (mAutoHideCounter == mAutoHideTotalCounter)
 
271
            {
 
272
                mSlideToScreen = false;
 
273
                mScrollTimer.start (mScrollDelay, this);
 
274
            }
 
275
        }
 
276
        else
 
277
            mAutoHideCounter = 0;
 
278
    }
 
279
    else
 
280
        QWidget::timerEvent (aEvent);
 
281
}
 
282
 
 
283
void VBoxMiniToolBar::showEvent (QShowEvent *aEvent)
 
284
{
 
285
    if (!mPolished)
 
286
    {
 
287
        /* Tool-bar margins */
 
288
        foreach (QWidget *margin, mMargins)
 
289
            margin->setMinimumWidth (height());
 
290
 
 
291
        /* Tool-bar spacings */
 
292
        foreach (QWidget *spacing, mSpacings)
 
293
            spacing->setMinimumWidth (5);
 
294
 
 
295
        /* Title spacings */
 
296
        foreach (QWidget *lableMargin, mLabelMargins)
 
297
            lableMargin->setMinimumWidth (15);
 
298
 
 
299
        /* Initialize */
 
300
        initialize();
 
301
 
 
302
        mPolished = true;
 
303
    }
 
304
 
 
305
    VBoxToolBar::showEvent (aEvent);
 
306
}
 
307
 
 
308
void VBoxMiniToolBar::paintEvent (QPaintEvent *aEvent)
 
309
{
 
310
    QPainter painter;
 
311
    painter.begin (this);
 
312
    painter.fillRect (aEvent->rect(), palette().brush (QPalette::Window));
 
313
    painter.end();
 
314
    VBoxToolBar::paintEvent (aEvent);
 
315
}
 
316
 
 
317
void VBoxMiniToolBar::togglePushpin (bool aOn)
 
318
{
 
319
    mAutoHide = !aOn;
 
320
    updateDisplay (!mAutoHide, false);
 
321
}
 
322
 
 
323
void VBoxMiniToolBar::initialize()
 
324
{
 
325
    /* Resize to sizehint */
 
326
    resize (sizeHint());
 
327
 
 
328
    /* Update geometry */
 
329
    recreateMask();
 
330
    moveToBase();
 
331
}
 
332
 
 
333
void VBoxMiniToolBar::recreateMask()
 
334
{
 
335
    int edgeShift = height();
 
336
    int points [8];
 
337
    switch (mAlignment)
 
338
    {
 
339
        case AlignTop:
 
340
        {
 
341
            points [0] = 0;
 
342
            points [1] = 0;
 
343
 
 
344
            points [2] = edgeShift;
 
345
            points [3] = height();
 
346
 
 
347
            points [4] = width() - edgeShift;
 
348
            points [5] = height();
 
349
 
 
350
            points [6] = width();
 
351
            points [7] = 0;
 
352
 
 
353
            break;
 
354
        }
 
355
        case AlignBottom:
 
356
        {
 
357
            points [0] = edgeShift;
 
358
            points [1] = 0;
 
359
 
 
360
            points [2] = 0;
 
361
            points [3] = height();
 
362
 
 
363
            points [4] = width();
 
364
            points [5] = height();
 
365
 
 
366
            points [6] = width() - edgeShift;
 
367
            points [7] = 0;
 
368
 
 
369
            break;
 
370
        }
 
371
        default:
 
372
            break;
 
373
    }
 
374
    /* Make sure any old mask is removed first */
 
375
    clearMask();
 
376
    /* Set the new mask */
 
377
    QPolygon polygon;
 
378
    polygon.setPoints (4, points);
 
379
    setMask (polygon);
 
380
}
 
381
 
 
382
void VBoxMiniToolBar::moveToBase()
 
383
{
 
384
    QRect screen = mSeamless ? QApplication::desktop()->availableGeometry (window()) :
 
385
                               QApplication::desktop()->screenGeometry (window());
 
386
    mPositionX = screen.x() + screen.width() / 2 - width() / 2;
 
387
    switch (mAlignment)
 
388
    {
 
389
        case AlignTop:
 
390
        {
 
391
            mPositionY = screen.y() - height() + 1;
 
392
            break;
 
393
        }
 
394
        case AlignBottom:
 
395
        {
 
396
            mPositionY = screen.y() + screen.height() - 1;
 
397
            break;
 
398
        }
 
399
        default:
 
400
        {
 
401
            mPositionY = 0;
 
402
            break;
 
403
        }
 
404
    }
 
405
    move (mapFromScreen (QPoint (mPositionX, mPositionY)));
 
406
}
 
407
 
 
408
QPoint VBoxMiniToolBar::mapFromScreen (const QPoint &aPoint)
 
409
{
 
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;
 
416
}
 
417