Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qrubberband.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
25 | ** | - | ||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
29 | ** | - | ||||||
30 | ** $QT_END_LICENSE$ | - | ||||||
31 | ** | - | ||||||
32 | ****************************************************************************/ | - | ||||||
33 | - | |||||||
34 | #include "qbitmap.h" | - | ||||||
35 | #include "qevent.h" | - | ||||||
36 | #include "qstylepainter.h" | - | ||||||
37 | #include "qrubberband.h" | - | ||||||
38 | #include "qtimer.h" | - | ||||||
39 | - | |||||||
40 | #ifndef QT_NO_RUBBERBAND | - | ||||||
41 | - | |||||||
42 | #include "qstyle.h" | - | ||||||
43 | #include "qstyleoption.h" | - | ||||||
44 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||
45 | # include <private/qt_mac_p.h> | - | ||||||
46 | # include <private/qt_cocoa_helpers_mac_p.h> | - | ||||||
47 | #endif | - | ||||||
48 | - | |||||||
49 | #include <qdebug.h> | - | ||||||
50 | - | |||||||
51 | #include <private/qwidget_p.h> | - | ||||||
52 | - | |||||||
53 | QT_BEGIN_NAMESPACE | - | ||||||
54 | - | |||||||
55 | //### a rubberband window type would be a more elegant solution | - | ||||||
56 | #define RUBBERBAND_WINDOW_TYPE Qt::ToolTip | - | ||||||
57 | - | |||||||
58 | class QRubberBandPrivate : public QWidgetPrivate | - | ||||||
59 | { | - | ||||||
60 | Q_DECLARE_PUBLIC(QRubberBand) | - | ||||||
61 | public: | - | ||||||
62 | QRect rect; | - | ||||||
63 | QRubberBand::Shape shape; | - | ||||||
64 | QRegion clipping; | - | ||||||
65 | void updateMask(); | - | ||||||
66 | }; | - | ||||||
67 | - | |||||||
68 | /*! | - | ||||||
69 | Initialize \a option with the values from this QRubberBand. This method | - | ||||||
70 | is useful for subclasses when they need a QStyleOptionRubberBand, but don't want | - | ||||||
71 | to fill in all the information themselves. | - | ||||||
72 | - | |||||||
73 | \sa QStyleOption::initFrom() | - | ||||||
74 | */ | - | ||||||
75 | void QRubberBand::initStyleOption(QStyleOptionRubberBand *option) const | - | ||||||
76 | { | - | ||||||
77 | if (!option)
| 0 | ||||||
78 | return; never executed: return; | 0 | ||||||
79 | option->initFrom(this); | - | ||||||
80 | option->shape = d_func()->shape; | - | ||||||
81 | #ifndef Q_OS_MAC | - | ||||||
82 | option->opaque = true; | - | ||||||
83 | #else | - | ||||||
84 | option->opaque = windowFlags() & RUBBERBAND_WINDOW_TYPE; | - | ||||||
85 | #endif | - | ||||||
86 | } never executed: end of block | 0 | ||||||
87 | - | |||||||
88 | /*! | - | ||||||
89 | \class QRubberBand | - | ||||||
90 | \brief The QRubberBand class provides a rectangle or line that can | - | ||||||
91 | indicate a selection or a boundary. | - | ||||||
92 | - | |||||||
93 | \inmodule QtWidgets | - | ||||||
94 | - | |||||||
95 | A rubber band is often used to show a new bounding area (as in a | - | ||||||
96 | QSplitter or a QDockWidget that is undocking). Historically this has | - | ||||||
97 | been implemented using a QPainter and XOR, but this approach | - | ||||||
98 | doesn't always work properly since rendering can happen in the | - | ||||||
99 | window below the rubber band, but before the rubber band has been | - | ||||||
100 | "erased". | - | ||||||
101 | - | |||||||
102 | You can create a QRubberBand whenever you need to render a rubber band | - | ||||||
103 | around a given area (or to represent a single line), then call | - | ||||||
104 | setGeometry(), move() or resize() to position and size it. A common | - | ||||||
105 | pattern is to do this in conjunction with mouse events. For example: | - | ||||||
106 | - | |||||||
107 | \snippet code/src_gui_widgets_qrubberband.cpp 0 | - | ||||||
108 | - | |||||||
109 | If you pass a parent to QRubberBand's constructor, the rubber band will | - | ||||||
110 | display only inside its parent, but stays on top of other child widgets. | - | ||||||
111 | If no parent is passed, QRubberBand will act as a top-level widget. | - | ||||||
112 | - | |||||||
113 | Call show() to make the rubber band visible; also when the | - | ||||||
114 | rubber band is not a top-level. Hiding or destroying | - | ||||||
115 | the widget will make the rubber band disappear. The rubber band | - | ||||||
116 | can be a \l Rectangle or a \l Line (vertical or horizontal), | - | ||||||
117 | depending on the shape() it was given when constructed. | - | ||||||
118 | */ | - | ||||||
119 | - | |||||||
120 | // ### DOC: How about some nice convenience constructors? | - | ||||||
121 | //QRubberBand::QRubberBand(QRubberBand::Type t, const QRect &rect, QWidget *p) | - | ||||||
122 | //QRubberBand::QRubberBand(QRubberBand::Type t, int x, int y, int w, int h, QWidget *p) | - | ||||||
123 | - | |||||||
124 | /*! | - | ||||||
125 | Constructs a rubber band of shape \a s, with parent \a p. | - | ||||||
126 | - | |||||||
127 | By default a rectangular rubber band (\a s is \c Rectangle) will | - | ||||||
128 | use a mask, so that a small border of the rectangle is all | - | ||||||
129 | that is visible. Some styles (e.g., native \macos) will | - | ||||||
130 | change this and call QWidget::setWindowOpacity() to make a | - | ||||||
131 | semi-transparent filled selection rectangle. | - | ||||||
132 | */ | - | ||||||
133 | QRubberBand::QRubberBand(Shape s, QWidget *p) | - | ||||||
134 | : QWidget(*new QRubberBandPrivate, p, (p && p->windowType() != Qt::Desktop) ? Qt::Widget : RUBBERBAND_WINDOW_TYPE) | - | ||||||
135 | { | - | ||||||
136 | Q_D(QRubberBand); | - | ||||||
137 | d->shape = s; | - | ||||||
138 | setAttribute(Qt::WA_TransparentForMouseEvents); | - | ||||||
139 | #ifndef Q_DEAD_CODE_FROM_QT4_WIN | - | ||||||
140 | setAttribute(Qt::WA_NoSystemBackground); | - | ||||||
141 | #endif //Q_DEAD_CODE_FROM_QT4_WIN | - | ||||||
142 | setAttribute(Qt::WA_WState_ExplicitShowHide); | - | ||||||
143 | setVisible(false); | - | ||||||
144 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||
145 | if (isWindow()) { | - | ||||||
146 | createWinId(); | - | ||||||
147 | extern OSWindowRef qt_mac_window_for(const QWidget *); //qwidget_mac.cpp | - | ||||||
148 | macWindowSetHasShadow(qt_mac_window_for(this), false); | - | ||||||
149 | } | - | ||||||
150 | #endif | - | ||||||
151 | } never executed: end of block | 0 | ||||||
152 | - | |||||||
153 | /*! | - | ||||||
154 | Destructor. | - | ||||||
155 | */ | - | ||||||
156 | QRubberBand::~QRubberBand() | - | ||||||
157 | { | - | ||||||
158 | } | - | ||||||
159 | - | |||||||
160 | /*! | - | ||||||
161 | \enum QRubberBand::Shape | - | ||||||
162 | - | |||||||
163 | This enum specifies what shape a QRubberBand should have. This is | - | ||||||
164 | a drawing hint that is passed down to the style system, and can be | - | ||||||
165 | interpreted by each QStyle. | - | ||||||
166 | - | |||||||
167 | \value Line A QRubberBand can represent a vertical or horizontal | - | ||||||
168 | line. Geometry is still given in rect() and the line | - | ||||||
169 | will fill the given geometry on most styles. | - | ||||||
170 | - | |||||||
171 | \value Rectangle A QRubberBand can represent a rectangle. Some | - | ||||||
172 | styles will interpret this as a filled (often | - | ||||||
173 | semi-transparent) rectangle, or a rectangular | - | ||||||
174 | outline. | - | ||||||
175 | */ | - | ||||||
176 | - | |||||||
177 | /*! | - | ||||||
178 | Returns the shape of this rubber band. The shape can only be set | - | ||||||
179 | upon construction. | - | ||||||
180 | */ | - | ||||||
181 | QRubberBand::Shape QRubberBand::shape() const | - | ||||||
182 | { | - | ||||||
183 | Q_D(const QRubberBand); | - | ||||||
184 | return d->shape; never executed: return d->shape; | 0 | ||||||
185 | } | - | ||||||
186 | - | |||||||
187 | /*! | - | ||||||
188 | \internal | - | ||||||
189 | */ | - | ||||||
190 | void QRubberBandPrivate::updateMask() | - | ||||||
191 | { | - | ||||||
192 | Q_Q(QRubberBand); | - | ||||||
193 | QStyleHintReturnMask mask; | - | ||||||
194 | QStyleOptionRubberBand opt; | - | ||||||
195 | q->initStyleOption(&opt); | - | ||||||
196 | if (q->style()->styleHint(QStyle::SH_RubberBand_Mask, &opt, q, &mask)) {
| 0 | ||||||
197 | q->setMask(mask.region); | - | ||||||
198 | } else { never executed: end of block | 0 | ||||||
199 | q->clearMask(); | - | ||||||
200 | } never executed: end of block | 0 | ||||||
201 | } | - | ||||||
202 | - | |||||||
203 | /*! | - | ||||||
204 | \reimp | - | ||||||
205 | */ | - | ||||||
206 | void QRubberBand::paintEvent(QPaintEvent *) | - | ||||||
207 | { | - | ||||||
208 | QStylePainter painter(this); | - | ||||||
209 | QStyleOptionRubberBand option; | - | ||||||
210 | initStyleOption(&option); | - | ||||||
211 | painter.drawControl(QStyle::CE_RubberBand, option); | - | ||||||
212 | } never executed: end of block | 0 | ||||||
213 | - | |||||||
214 | /*! | - | ||||||
215 | \reimp | - | ||||||
216 | */ | - | ||||||
217 | void QRubberBand::changeEvent(QEvent *e) | - | ||||||
218 | { | - | ||||||
219 | QWidget::changeEvent(e); | - | ||||||
220 | switch (e->type()) { | - | ||||||
221 | case QEvent::ParentChange: never executed: case QEvent::ParentChange: | 0 | ||||||
222 | if (parent()) {
| 0 | ||||||
223 | setWindowFlags(windowFlags() & ~RUBBERBAND_WINDOW_TYPE); | - | ||||||
224 | } else { never executed: end of block | 0 | ||||||
225 | setWindowFlags(windowFlags() | RUBBERBAND_WINDOW_TYPE); | - | ||||||
226 | } never executed: end of block | 0 | ||||||
227 | break; never executed: break; | 0 | ||||||
228 | default: never executed: default: | 0 | ||||||
229 | break; never executed: break; | 0 | ||||||
230 | } | - | ||||||
231 | - | |||||||
232 | if (e->type() == QEvent::ZOrderChange)
| 0 | ||||||
233 | raise(); never executed: raise(); | 0 | ||||||
234 | } never executed: end of block | 0 | ||||||
235 | - | |||||||
236 | /*! | - | ||||||
237 | \reimp | - | ||||||
238 | */ | - | ||||||
239 | void QRubberBand::showEvent(QShowEvent *e) | - | ||||||
240 | { | - | ||||||
241 | raise(); | - | ||||||
242 | e->ignore(); | - | ||||||
243 | } never executed: end of block | 0 | ||||||
244 | - | |||||||
245 | /*! | - | ||||||
246 | \reimp | - | ||||||
247 | */ | - | ||||||
248 | void QRubberBand::resizeEvent(QResizeEvent *) | - | ||||||
249 | { | - | ||||||
250 | Q_D(QRubberBand); | - | ||||||
251 | d->updateMask(); | - | ||||||
252 | } never executed: end of block | 0 | ||||||
253 | - | |||||||
254 | /*! | - | ||||||
255 | \reimp | - | ||||||
256 | */ | - | ||||||
257 | void QRubberBand::moveEvent(QMoveEvent *) | - | ||||||
258 | { | - | ||||||
259 | Q_D(QRubberBand); | - | ||||||
260 | d->updateMask(); | - | ||||||
261 | } never executed: end of block | 0 | ||||||
262 | - | |||||||
263 | /*! | - | ||||||
264 | \fn void QRubberBand::move(const QPoint &p); | - | ||||||
265 | - | |||||||
266 | \overload | - | ||||||
267 | - | |||||||
268 | Moves the rubberband to point \a p. | - | ||||||
269 | - | |||||||
270 | \sa resize() | - | ||||||
271 | */ | - | ||||||
272 | - | |||||||
273 | /*! | - | ||||||
274 | \fn void QRubberBand::move(int x, int y); | - | ||||||
275 | - | |||||||
276 | Moves the rubberband to point (\a x, \a y). | - | ||||||
277 | - | |||||||
278 | \sa resize() | - | ||||||
279 | */ | - | ||||||
280 | - | |||||||
281 | /*! | - | ||||||
282 | \fn void QRubberBand::resize(const QSize &size); | - | ||||||
283 | - | |||||||
284 | \overload | - | ||||||
285 | - | |||||||
286 | Resizes the rubberband so that its new size is \a size. | - | ||||||
287 | - | |||||||
288 | \sa move() | - | ||||||
289 | */ | - | ||||||
290 | - | |||||||
291 | /*! | - | ||||||
292 | \fn void QRubberBand::resize(int width, int height); | - | ||||||
293 | - | |||||||
294 | Resizes the rubberband so that its width is \a width, and its | - | ||||||
295 | height is \a height. | - | ||||||
296 | - | |||||||
297 | \sa move() | - | ||||||
298 | */ | - | ||||||
299 | - | |||||||
300 | /*! | - | ||||||
301 | \fn void QRubberBand::setGeometry(const QRect &rect) | - | ||||||
302 | - | |||||||
303 | Sets the geometry of the rubber band to \a rect, specified in the coordinate system | - | ||||||
304 | of its parent widget. | - | ||||||
305 | - | |||||||
306 | \sa QWidget::geometry | - | ||||||
307 | */ | - | ||||||
308 | void QRubberBand::setGeometry(const QRect &geom) | - | ||||||
309 | { | - | ||||||
310 | QWidget::setGeometry(geom); | - | ||||||
311 | } never executed: end of block | 0 | ||||||
312 | - | |||||||
313 | /*! | - | ||||||
314 | \fn void QRubberBand::setGeometry(int x, int y, int width, int height) | - | ||||||
315 | \overload | - | ||||||
316 | - | |||||||
317 | Sets the geometry of the rubberband to the rectangle whose top-left corner lies at | - | ||||||
318 | the point (\a x, \a y), and with dimensions specified by \a width and \a height. | - | ||||||
319 | The geometry is specified in the parent widget's coordinate system. | - | ||||||
320 | */ | - | ||||||
321 | - | |||||||
322 | /*! \reimp */ | - | ||||||
323 | bool QRubberBand::event(QEvent *e) | - | ||||||
324 | { | - | ||||||
325 | return QWidget::event(e); never executed: return QWidget::event(e); | 0 | ||||||
326 | } | - | ||||||
327 | - | |||||||
328 | QT_END_NAMESPACE | - | ||||||
329 | - | |||||||
330 | #include "moc_qrubberband.cpp" | - | ||||||
331 | - | |||||||
332 | #endif // QT_NO_RUBBERBAND | - | ||||||
Source code | Switch to Preprocessed file |