1 | /* $Id: QIStateIndicator.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * VirtualBox Qt extensions: QIStateIndicator class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "QIStateIndicator.h"
|
---|
21 |
|
---|
22 | /* Qt includes */
|
---|
23 | #include <QPainter>
|
---|
24 | #ifdef Q_WS_MAC
|
---|
25 | # include <QContextMenuEvent>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | /** @clas QIStateIndicator
|
---|
29 | *
|
---|
30 | * The QIStateIndicator class is a simple class that can visually indicate
|
---|
31 | * the state of some thing, as described by the state property.
|
---|
32 | */
|
---|
33 |
|
---|
34 | QIStateIndicator::QIStateIndicator(QWidget *pParent /* = 0 */)
|
---|
35 | : QFrame(pParent)
|
---|
36 | , mState(0)
|
---|
37 | , mSize(0, 0)
|
---|
38 | {
|
---|
39 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new QIStateIndicator instance. This instance is useless
|
---|
44 | * until icons are specified for necessary states.
|
---|
45 | *
|
---|
46 | * @param aState
|
---|
47 | * the initial indicator state
|
---|
48 | */
|
---|
49 | QIStateIndicator::QIStateIndicator (int aState)
|
---|
50 | // : QFrame (aParent, aName, aFlags | Qt::WStaticContents | Qt::WMouseNoMask)
|
---|
51 | {
|
---|
52 | mState = aState;
|
---|
53 | mSize = QSize (0, 0);
|
---|
54 |
|
---|
55 | setSizePolicy (QSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed));
|
---|
56 |
|
---|
57 | /* we will precompose the pixmap background using the widget background in
|
---|
58 | * drawContents(), so try to set the correct background origin for the
|
---|
59 | * case when a pixmap is used as a widget background. */
|
---|
60 | // if (aParent)
|
---|
61 | // setBackgroundOrigin (aParent->backgroundOrigin());
|
---|
62 | }
|
---|
63 |
|
---|
64 | QIStateIndicator::~QIStateIndicator()
|
---|
65 | {
|
---|
66 | qDeleteAll (mStateIcons);
|
---|
67 | }
|
---|
68 |
|
---|
69 | QSize QIStateIndicator::sizeHint() const
|
---|
70 | {
|
---|
71 | return mSize;
|
---|
72 | }
|
---|
73 |
|
---|
74 | QPixmap QIStateIndicator::stateIcon (int aState) const
|
---|
75 | {
|
---|
76 | Icon *icon = mStateIcons [aState];
|
---|
77 | return icon ? icon->pixmap : QPixmap();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Sets an icon for the specified state. The first icon set by this method
|
---|
82 | * defines the preferred size of this indicator. All other icons will be
|
---|
83 | * scaled to fit this size.
|
---|
84 | *
|
---|
85 | * @note If this widget is constructed with the WNoAutoErase flag, then all
|
---|
86 | * transparent areas of the new state icon are filled with the widget
|
---|
87 | * background color or pixmap (as taken from the widget palette), to provide
|
---|
88 | * flicker free state redraws in one single operation (which is useful for
|
---|
89 | * indicators that frequently change their state).
|
---|
90 | */
|
---|
91 | void QIStateIndicator::setStateIcon (int aState, const QPixmap &aPixmap)
|
---|
92 | {
|
---|
93 | /* Here we just set the original pixmap. All actual work from the @note
|
---|
94 | * above takes place in #drawContents(). */
|
---|
95 | mStateIcons.insert (aState, new Icon (aPixmap));
|
---|
96 |
|
---|
97 | if (mSize.isNull())
|
---|
98 | mSize = aPixmap.size();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void QIStateIndicator::setState (int aState)
|
---|
102 | {
|
---|
103 | mState = aState;
|
---|
104 | repaint();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void QIStateIndicator::paintEvent (QPaintEvent * /* aEv */)
|
---|
108 | {
|
---|
109 | QPainter painter (this);
|
---|
110 | drawContents (&painter);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void QIStateIndicator::drawContents (QPainter *aPainter)
|
---|
114 | {
|
---|
115 | Icon *icon = mStateIcons [mState];
|
---|
116 | if (icon)
|
---|
117 | aPainter->drawPixmap (contentsRect(), icon->pixmap);
|
---|
118 | }
|
---|
119 |
|
---|
120 | #ifdef Q_WS_MAC
|
---|
121 | /**
|
---|
122 | * Make the left button also show the context menu to make things
|
---|
123 | * simpler for users with single mouse button mice (laptops++).
|
---|
124 | */
|
---|
125 | void QIStateIndicator::mousePressEvent (QMouseEvent *aEv)
|
---|
126 | {
|
---|
127 | /* Do this for the left mouse button event only, cause in the case of the
|
---|
128 | * right mouse button it could happen that the context menu event is
|
---|
129 | * triggered twice. Also this isn't necessary for the middle mouse button
|
---|
130 | * which would be some kind of overstated. */
|
---|
131 | if (aEv->button() == Qt::LeftButton)
|
---|
132 | {
|
---|
133 | QContextMenuEvent qme (QContextMenuEvent::Mouse, aEv->pos(), aEv->globalPos());
|
---|
134 | emit contextMenuRequested (this, &qme);
|
---|
135 | if (qme.isAccepted())
|
---|
136 | aEv->accept();
|
---|
137 | else
|
---|
138 | QFrame::mousePressEvent (aEv);
|
---|
139 | }else
|
---|
140 | QFrame::mousePressEvent (aEv);
|
---|
141 | }
|
---|
142 | #endif /* Q_WS_MAC */
|
---|
143 |
|
---|
144 | void QIStateIndicator::mouseDoubleClickEvent (QMouseEvent * e)
|
---|
145 | {
|
---|
146 | emit mouseDoubleClicked (this, e);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void QIStateIndicator::contextMenuEvent (QContextMenuEvent * e)
|
---|
150 | {
|
---|
151 | emit contextMenuRequested (this, e);
|
---|
152 | }
|
---|
153 |
|
---|