source: trunk/src/gui/widgets/qrubberband.cpp@ 147

Last change on this file since 147 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 9.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qbitmap.h"
43#include "qevent.h"
44#include "qstylepainter.h"
45#include "qrubberband.h"
46#include "qtimer.h"
47
48#ifndef QT_NO_RUBBERBAND
49
50#include "qstyle.h"
51#include "qstyleoption.h"
52#ifdef Q_WS_MAC
53# include <private/qt_mac_p.h>
54# include <private/qt_cocoa_helpers_mac_p.h>
55#endif
56
57#include <qdebug.h>
58
59#include <private/qwidget_p.h>
60
61QT_BEGIN_NAMESPACE
62
63//### a rubberband window type would be a more elegant solution
64#define RUBBERBAND_WINDOW_TYPE Qt::ToolTip
65
66class QRubberBandPrivate : public QWidgetPrivate
67{
68 Q_DECLARE_PUBLIC(QRubberBand)
69public:
70 QRect rect;
71 QRubberBand::Shape shape;
72 QRegion clipping;
73 void updateMask();
74};
75
76/*!
77 Initialize \a option with the values from this QRubberBand. This method
78 is useful for subclasses when they need a QStyleOptionRubberBand, but don't want
79 to fill in all the information themselves.
80
81 \sa QStyleOption::initFrom()
82*/
83void QRubberBand::initStyleOption(QStyleOptionRubberBand *option) const
84{
85 if (!option)
86 return;
87 option->initFrom(this);
88 option->shape = d_func()->shape;
89#ifndef Q_WS_MAC
90 option->opaque = true;
91#else
92 option->opaque = windowFlags() & RUBBERBAND_WINDOW_TYPE;
93#endif
94}
95
96/*!
97 \class QRubberBand
98 \brief The QRubberBand class provides a rectangle or line that can
99 indicate a selection or a boundary.
100
101 \ingroup misc
102 \mainclass
103
104 A rubber band is often used to show a new bounding area (as in a
105 QSplitter or a QDockWidget that is undocking). Historically this has
106 been implemented using a QPainter and XOR, but this approach
107 doesn't always work properly since rendering can happen in the
108 window below the rubber band, but before the rubber band has been
109 "erased".
110
111 You can create a QRubberBand whenever you need to render a rubber band
112 around a given area (or to represent a single line), then call
113 setGeometry(), move() or resize() to position and size it. A common
114 pattern is to do this in conjunction with mouse events. For example:
115
116 \snippet doc/src/snippets/code/src_gui_widgets_qrubberband.cpp 0
117
118 If you pass a parent to QRubberBand's constructor, the rubber band will
119 display only inside its parent, but stays on top of other child widgets.
120 If no parent is passed, QRubberBand will act as a top-level widget.
121
122 Call show() to make the rubber band visible; also when the
123 rubber band is not a top-level. Hiding or destroying
124 the widget will make the rubber band disappear. The rubber band
125 can be a \l Rectangle or a \l Line (vertical or horizontal),
126 depending on the shape() it was given when constructed.
127*/
128
129// ### DOC: How about some nice convenience constructors?
130//QRubberBand::QRubberBand(QRubberBand::Type t, const QRect &rect, QWidget *p)
131//QRubberBand::QRubberBand(QRubberBand::Type t, int x, int y, int w, int h, QWidget *p)
132
133/*!
134 Constructs a rubber band of shape \a s, with parent \a p.
135
136 By default a rectangular rubber band (\a s is \c Rectangle) will
137 use a mask, so that a small border of the rectangle is all
138 that is visible. Some styles (e.g., native Mac OS X) will
139 change this and call QWidget::setWindowOpacity() to make a
140 semi-transparent filled selection rectangle.
141*/
142QRubberBand::QRubberBand(Shape s, QWidget *p)
143 : QWidget(*new QRubberBandPrivate, p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE)
144{
145 Q_D(QRubberBand);
146 d->shape = s;
147 setAttribute(Qt::WA_TransparentForMouseEvents);
148#ifndef Q_WS_WIN
149 setAttribute(Qt::WA_NoSystemBackground);
150#endif //Q_WS_WIN
151 setAttribute(Qt::WA_WState_ExplicitShowHide);
152 setVisible(false);
153#ifdef Q_WS_MAC
154 if (isWindow()) {
155 createWinId();
156 extern OSWindowRef qt_mac_window_for(const QWidget *); //qwidget_mac.cpp
157 macWindowSetHasShadow(qt_mac_window_for(this), false);
158 }
159#endif
160}
161
162/*!
163 Destructor.
164*/
165QRubberBand::~QRubberBand()
166{
167}
168
169/*!
170 \enum QRubberBand::Shape
171
172 This enum specifies what shape a QRubberBand should have. This is
173 a drawing hint that is passed down to the style system, and can be
174 interpreted by each QStyle.
175
176 \value Line A QRubberBand can represent a vertical or horizontal
177 line. Geometry is still given in rect() and the line
178 will fill the given geometry on most styles.
179
180 \value Rectangle A QRubberBand can represent a rectangle. Some
181 styles will interpret this as a filled (often
182 semi-transparent) rectangle, or a rectangular
183 outline.
184*/
185
186/*!
187 Returns the shape of this rubber band. The shape can only be set
188 upon construction.
189*/
190QRubberBand::Shape QRubberBand::shape() const
191{
192 Q_D(const QRubberBand);
193 return d->shape;
194}
195
196/*!
197 \internal
198*/
199void QRubberBandPrivate::updateMask()
200{
201 Q_Q(QRubberBand);
202 QStyleHintReturnMask mask;
203 QStyleOptionRubberBand opt;
204 q->initStyleOption(&opt);
205 if (q->style()->styleHint(QStyle::SH_RubberBand_Mask, &opt, q, &mask)) {
206 q->setMask(mask.region);
207 } else {
208 q->clearMask();
209 }
210}
211
212/*!
213 \reimp
214*/
215void QRubberBand::paintEvent(QPaintEvent *)
216{
217 QStylePainter painter(this);
218 QStyleOptionRubberBand option;
219 initStyleOption(&option);
220 painter.drawControl(QStyle::CE_RubberBand, option);
221}
222
223/*!
224 \reimp