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 |
|
---|
61 | QT_BEGIN_NAMESPACE
|
---|
62 |
|
---|
63 | //### a rubberband window type would be a more elegant solution
|
---|
64 | #define RUBBERBAND_WINDOW_TYPE Qt::ToolTip
|
---|
65 |
|
---|
66 | class QRubberBandPrivate : public QWidgetPrivate
|
---|
67 | {
|
---|
68 | Q_DECLARE_PUBLIC(QRubberBand)
|
---|
69 | public:
|
---|
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 | */
|
---|
83 | void 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 | */
|
---|
142 | QRubberBand::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 | */
|
---|
165 | QRubberBand::~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 | */
|
---|
190 | QRubberBand::Shape QRubberBand::shape() const
|
---|
191 | {
|
---|
192 | Q_D(const QRubberBand);
|
---|
193 | return d->shape;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*!
|
---|
197 | \internal
|
---|
198 | */
|
---|
199 | void 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 | */
|
---|
215 | void 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
|
---|
225 | */
|
---|
226 | void QRubberBand::changeEvent(QEvent *e)
|
---|
227 | {
|
---|
228 | QWidget::changeEvent(e);
|
---|
229 | switch (e->type()) {
|
---|
230 | case QEvent::ParentChange:
|
---|
231 | if (parent()) {
|
---|
232 | setWindowFlags(windowFlags() & ~RUBBERBAND_WINDOW_TYPE);
|
---|
233 | } else {
|
---|
234 | setWindowFlags(windowFlags() | RUBBERBAND_WINDOW_TYPE);
|
---|
235 | }
|
---|
236 | break;
|
---|
237 | default:
|
---|
238 | break;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (e->type() == QEvent::ZOrderChange)
|
---|
242 | raise();
|
---|
243 | }
|
---|
244 |
|
---|
245 | /*!
|
---|
246 | \reimp
|
---|
247 | */
|
---|
248 | void QRubberBand::showEvent(QShowEvent *e)
|
---|
249 | {
|
---|
250 | raise();
|
---|
251 | e->ignore();
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | \reimp
|
---|
256 | */
|
---|
257 | void QRubberBand::resizeEvent(QResizeEvent *)
|
---|
258 | {
|
---|
259 | Q_D(QRubberBand);
|
---|
260 | d->updateMask();
|
---|
261 | }
|
---|
262 |
|
---|
263 | /*!
|
---|
264 | \reimp
|
---|
265 | */
|
---|
266 | void QRubberBand::moveEvent(QMoveEvent *)
|
---|
267 | {
|
---|
268 | Q_D(QRubberBand);
|
---|
269 | d->updateMask();
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*!
|
---|
273 | \fn void QRubberBand::move(const QPoint &p);
|
---|
274 |
|
---|
275 | \overload
|
---|
276 |
|
---|
277 | Moves the rubberband to point \a p.
|
---|
278 |
|
---|
279 | \sa resize()
|
---|
280 | */
|
---|
281 |
|
---|
282 | /*!
|
---|
283 | \fn void QRubberBand::move(int x, int y);
|
---|
284 |
|
---|
285 | Moves the rubberband to point (\a x, \a y).
|
---|
286 |
|
---|
287 | \sa resize()
|
---|
288 | */
|
---|
289 |
|
---|
290 | /*!
|
---|
291 | \fn void QRubberBand::resize(const QSize &size);
|
---|
292 |
|
---|
293 | \overload
|
---|
294 |
|
---|
295 | Resizes the rubberband so that its new size is \a size.
|
---|
296 |
|
---|
297 | \sa move()
|
---|
298 | */
|
---|
299 |
|
---|
300 | /*!
|
---|
301 | \fn void QRubberBand::resize(int width, int height);
|
---|
302 |
|
---|
303 | Resizes the rubberband so that its width is \a width, and its
|
---|
304 | height is \a height.
|
---|
305 |
|
---|
306 | \sa move()
|
---|
307 | */
|
---|
308 |
|
---|
309 | /*!
|
---|
310 | \fn void QRubberBand::setGeometry(const QRect &rect)
|
---|
311 |
|
---|
312 | Sets the geometry of the rubber band to \a rect, specified in the coordinate system
|
---|
313 | of its parent widget.
|
---|
314 |
|
---|
315 | \sa QWidget::geometry
|
---|
316 | */
|
---|
317 | void QRubberBand::setGeometry(const QRect &geom)
|
---|
318 | {
|
---|
319 | QWidget::setGeometry(geom);
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | \fn void QRubberBand::setGeometry(int x, int y, int width, int height)
|
---|
324 | \overload
|
---|
325 |
|
---|
326 | Sets the geometry of the rubberband to the rectangle whose top-left corner lies at
|
---|
327 | the point (\a x, \a y), and with dimensions specified by \a width and \a height.
|
---|
328 | The geometry is specified in the parent widget's coordinate system.
|
---|
329 | */
|
---|
330 |
|
---|
331 | /*! \reimp */
|
---|
332 | bool QRubberBand::event(QEvent *e)
|
---|
333 | {
|
---|
334 | return QWidget::event(e);
|
---|
335 | }
|
---|
336 |
|
---|
337 | QT_END_NAMESPACE
|
---|
338 |
|
---|
339 | #endif // QT_NO_RUBBERBAND
|
---|