Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qfocusframe.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 "qfocusframe.h" | - | ||||||||||||||||||
35 | #include "qstyle.h" | - | ||||||||||||||||||
36 | #include "qbitmap.h" | - | ||||||||||||||||||
37 | #include "qstylepainter.h" | - | ||||||||||||||||||
38 | #include "qstyleoption.h" | - | ||||||||||||||||||
39 | #include "qdebug.h" | - | ||||||||||||||||||
40 | #include <private/qwidget_p.h> | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | class QFocusFramePrivate : public QWidgetPrivate | - | ||||||||||||||||||
45 | { | - | ||||||||||||||||||
46 | Q_DECLARE_PUBLIC(QFocusFrame) | - | ||||||||||||||||||
47 | QWidget *widget; | - | ||||||||||||||||||
48 | QWidget *frameParent; | - | ||||||||||||||||||
49 | bool showFrameAboveWidget; | - | ||||||||||||||||||
50 | public: | - | ||||||||||||||||||
51 | QFocusFramePrivate() { | - | ||||||||||||||||||
52 | widget = 0; | - | ||||||||||||||||||
53 | frameParent = 0; | - | ||||||||||||||||||
54 | sendChildEvents = false; | - | ||||||||||||||||||
55 | showFrameAboveWidget = false; | - | ||||||||||||||||||
56 | } never executed: end of block | 0 | ||||||||||||||||||
57 | void updateSize(); | - | ||||||||||||||||||
58 | void update(); | - | ||||||||||||||||||
59 | }; | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | void QFocusFramePrivate::update() | - | ||||||||||||||||||
62 | { | - | ||||||||||||||||||
63 | Q_Q(QFocusFrame); | - | ||||||||||||||||||
64 | q->setParent(frameParent); | - | ||||||||||||||||||
65 | updateSize(); | - | ||||||||||||||||||
66 | if (q->parentWidget()->rect().intersects(q->geometry())) {
| 0 | ||||||||||||||||||
67 | if (showFrameAboveWidget)
| 0 | ||||||||||||||||||
68 | q->raise(); never executed: q->raise(); | 0 | ||||||||||||||||||
69 | else | - | ||||||||||||||||||
70 | q->stackUnder(widget); never executed: q->stackUnder(widget); | 0 | ||||||||||||||||||
71 | q->show(); | - | ||||||||||||||||||
72 | } else { never executed: end of block | 0 | ||||||||||||||||||
73 | q->hide(); | - | ||||||||||||||||||
74 | } never executed: end of block | 0 | ||||||||||||||||||
75 | } | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | void QFocusFramePrivate::updateSize() | - | ||||||||||||||||||
78 | { | - | ||||||||||||||||||
79 | Q_Q(QFocusFrame); | - | ||||||||||||||||||
80 | if (!widget)
| 0 | ||||||||||||||||||
81 | return; never executed: return; | 0 | ||||||||||||||||||
82 | - | |||||||||||||||||||
83 | int vmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameVMargin), | - | ||||||||||||||||||
84 | hmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameHMargin); | - | ||||||||||||||||||
85 | QPoint pos(widget->x(), widget->y()); | - | ||||||||||||||||||
86 | if (q->parentWidget() != widget->parentWidget())
| 0 | ||||||||||||||||||
87 | pos = widget->parentWidget()->mapTo(q->parentWidget(), pos); never executed: pos = widget->parentWidget()->mapTo(q->parentWidget(), pos); | 0 | ||||||||||||||||||
88 | QRect geom(pos.x()-hmargin, pos.y()-vmargin, | - | ||||||||||||||||||
89 | widget->width()+(hmargin*2), widget->height()+(vmargin*2)); | - | ||||||||||||||||||
90 | if(q->geometry() == geom)
| 0 | ||||||||||||||||||
91 | return; never executed: return; | 0 | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | q->setGeometry(geom); | - | ||||||||||||||||||
94 | QStyleHintReturnMask mask; | - | ||||||||||||||||||
95 | QStyleOption opt; | - | ||||||||||||||||||
96 | q->initStyleOption(&opt); | - | ||||||||||||||||||
97 | if (q->style()->styleHint(QStyle::SH_FocusFrame_Mask, &opt, q, &mask))
| 0 | ||||||||||||||||||
98 | q->setMask(mask.region); never executed: q->setMask(mask.region); | 0 | ||||||||||||||||||
99 | } never executed: end of block | 0 | ||||||||||||||||||
100 | - | |||||||||||||||||||
101 | /*! | - | ||||||||||||||||||
102 | Initialize \a option with the values from this QFocusFrame. This method is useful | - | ||||||||||||||||||
103 | for subclasses when they need a QStyleOption, but don't want to fill | - | ||||||||||||||||||
104 | in all the information themselves. | - | ||||||||||||||||||
105 | - | |||||||||||||||||||
106 | \sa QStyleOption::initFrom() | - | ||||||||||||||||||
107 | */ | - | ||||||||||||||||||
108 | void QFocusFrame::initStyleOption(QStyleOption *option) const | - | ||||||||||||||||||
109 | { | - | ||||||||||||||||||
110 | if (!option)
| 0 | ||||||||||||||||||
111 | return; never executed: return; | 0 | ||||||||||||||||||
112 | - | |||||||||||||||||||
113 | option->initFrom(this); | - | ||||||||||||||||||
114 | } never executed: end of block | 0 | ||||||||||||||||||
115 | - | |||||||||||||||||||
116 | /*! | - | ||||||||||||||||||
117 | \class QFocusFrame | - | ||||||||||||||||||
118 | \brief The QFocusFrame widget provides a focus frame which can be | - | ||||||||||||||||||
119 | outside of a widget's normal paintable area. | - | ||||||||||||||||||
120 | - | |||||||||||||||||||
121 | \ingroup basicwidgets | - | ||||||||||||||||||
122 | \inmodule QtWidgets | - | ||||||||||||||||||
123 | - | |||||||||||||||||||
124 | Normally an application will not need to create its own | - | ||||||||||||||||||
125 | QFocusFrame as QStyle will handle this detail for | - | ||||||||||||||||||
126 | you. A style writer can optionally use a QFocusFrame to have a | - | ||||||||||||||||||
127 | focus area outside of the widget's paintable geometry. In this way | - | ||||||||||||||||||
128 | space need not be reserved for the widget to have focus but only | - | ||||||||||||||||||
129 | set on a QWidget with QFocusFrame::setWidget. It is, however, | - | ||||||||||||||||||
130 | legal to create your own QFocusFrame on a custom widget and set | - | ||||||||||||||||||
131 | its geometry manually via QWidget::setGeometry however you will | - | ||||||||||||||||||
132 | not get auto-placement when the focused widget changes size or | - | ||||||||||||||||||
133 | placement. | - | ||||||||||||||||||
134 | */ | - | ||||||||||||||||||
135 | - | |||||||||||||||||||
136 | /*! | - | ||||||||||||||||||
137 | Constructs a QFocusFrame. | - | ||||||||||||||||||
138 | - | |||||||||||||||||||
139 | The focus frame will not monitor \a parent for updates but rather | - | ||||||||||||||||||
140 | can be placed manually or by using QFocusFrame::setWidget. A | - | ||||||||||||||||||
141 | QFocusFrame sets Qt::WA_NoChildEventsForParent attribute; as a | - | ||||||||||||||||||
142 | result the parent will not receive a QEvent::ChildAdded event, | - | ||||||||||||||||||
143 | this will make it possible to manually set the geometry of the | - | ||||||||||||||||||
144 | QFocusFrame inside of a QSplitter or other child event monitoring | - | ||||||||||||||||||
145 | widget. | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | \sa QFocusFrame::setWidget() | - | ||||||||||||||||||
148 | */ | - | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | QFocusFrame::QFocusFrame(QWidget *parent) | - | ||||||||||||||||||
151 | : QWidget(*new QFocusFramePrivate, parent, 0) | - | ||||||||||||||||||
152 | { | - | ||||||||||||||||||
153 | setAttribute(Qt::WA_TransparentForMouseEvents); | - | ||||||||||||||||||
154 | setFocusPolicy(Qt::NoFocus); | - | ||||||||||||||||||
155 | setAttribute(Qt::WA_NoChildEventsForParent, true); | - | ||||||||||||||||||
156 | setAttribute(Qt::WA_AcceptDrops, style()->styleHint(QStyle::SH_FocusFrame_AboveWidget, 0, this)); | - | ||||||||||||||||||
157 | } never executed: end of block | 0 | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | /*! | - | ||||||||||||||||||
160 | Destructor. | - | ||||||||||||||||||
161 | */ | - | ||||||||||||||||||
162 | - | |||||||||||||||||||
163 | QFocusFrame::~QFocusFrame() | - | ||||||||||||||||||
164 | { | - | ||||||||||||||||||
165 | } | - | ||||||||||||||||||
166 | - | |||||||||||||||||||
167 | /*! | - | ||||||||||||||||||
168 | QFocusFrame will track changes to \a widget and resize itself automatically. | - | ||||||||||||||||||
169 | If the monitored widget's parent changes, QFocusFrame will follow the widget | - | ||||||||||||||||||
170 | and place itself around the widget automatically. If the monitored widget is deleted, | - | ||||||||||||||||||
171 | QFocusFrame will set it to zero. | - | ||||||||||||||||||
172 | - | |||||||||||||||||||
173 | \sa QFocusFrame::widget() | - | ||||||||||||||||||
174 | */ | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | void | - | ||||||||||||||||||
177 | QFocusFrame::setWidget(QWidget *widget) | - | ||||||||||||||||||
178 | { | - | ||||||||||||||||||
179 | Q_D(QFocusFrame); | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | if (style()->styleHint(QStyle::SH_FocusFrame_AboveWidget, 0, this))
| 0 | ||||||||||||||||||
182 | d->showFrameAboveWidget = true; never executed: d->showFrameAboveWidget = true; | 0 | ||||||||||||||||||
183 | else | - | ||||||||||||||||||
184 | d->showFrameAboveWidget = false; never executed: d->showFrameAboveWidget = false; | 0 | ||||||||||||||||||
185 | - | |||||||||||||||||||
186 | if (widget == d->widget)
| 0 | ||||||||||||||||||
187 | return; never executed: return; | 0 | ||||||||||||||||||
188 | if (d->widget) {
| 0 | ||||||||||||||||||
189 | // Remove event filters from the widget hierarchy. | - | ||||||||||||||||||
190 | QWidget *p = d->widget; | - | ||||||||||||||||||
191 | do { | - | ||||||||||||||||||
192 | p->removeEventFilter(this); | - | ||||||||||||||||||
193 | if (!d->showFrameAboveWidget || p == d->frameParent)
| 0 | ||||||||||||||||||
194 | break; never executed: break; | 0 | ||||||||||||||||||
195 | p = p->parentWidget(); | - | ||||||||||||||||||
196 | }while (p); never executed: end of block
| 0 | ||||||||||||||||||
197 | } never executed: end of block | 0 | ||||||||||||||||||
198 | if (widget && !widget->isWindow() && widget->parentWidget()->windowType() != Qt::SubWindow) {
| 0 | ||||||||||||||||||
199 | d->widget = widget; | - | ||||||||||||||||||
200 | d->widget->installEventFilter(this); | - | ||||||||||||||||||
201 | QWidget *p = widget->parentWidget(); | - | ||||||||||||||||||
202 | QWidget *prev = 0; | - | ||||||||||||||||||
203 | if (d->showFrameAboveWidget) {
| 0 | ||||||||||||||||||
204 | // Find the right parent for the focus frame. | - | ||||||||||||||||||
205 | while (p) {
| 0 | ||||||||||||||||||
206 | // Traverse the hirerarchy of the 'widget' for setting event filter. | - | ||||||||||||||||||
207 | // During this if come across toolbar or a top level, use that | - | ||||||||||||||||||
208 | // as the parent for the focus frame. If we find a scroll area | - | ||||||||||||||||||
209 | // use its viewport as the parent. | - | ||||||||||||||||||
210 | bool isScrollArea = false; | - | ||||||||||||||||||
211 | if (p->isWindow() || p->inherits("QToolBar") || (isScrollArea = p->inherits("QAbstractScrollArea"))) {
| 0 | ||||||||||||||||||
212 | d->frameParent = p; | - | ||||||||||||||||||
213 | // The previous one in the hierarchy will be the viewport. | - | ||||||||||||||||||
214 | if (prev && isScrollArea)
| 0 | ||||||||||||||||||
215 | d->frameParent = prev; never executed: d->frameParent = prev; | 0 | ||||||||||||||||||
216 | break; never executed: break; | 0 | ||||||||||||||||||
217 | } else { | - | ||||||||||||||||||
218 | p->installEventFilter(this); | - | ||||||||||||||||||
219 | prev = p; | - | ||||||||||||||||||
220 | p = p->parentWidget(); | - | ||||||||||||||||||
221 | } never executed: end of block | 0 | ||||||||||||||||||
222 | } | - | ||||||||||||||||||
223 | } else { never executed: end of block | 0 | ||||||||||||||||||
224 | d->frameParent = p; | - | ||||||||||||||||||
225 | } never executed: end of block | 0 | ||||||||||||||||||
226 | d->update(); | - | ||||||||||||||||||
227 | } else { never executed: end of block | 0 | ||||||||||||||||||
228 | d->widget = 0; | - | ||||||||||||||||||
229 | hide(); | - | ||||||||||||||||||
230 | } never executed: end of block | 0 | ||||||||||||||||||
231 | } | - | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | /*! | - | ||||||||||||||||||
234 | Returns the currently monitored widget for automatically resize and | - | ||||||||||||||||||
235 | update. | - | ||||||||||||||||||
236 | - | |||||||||||||||||||
237 | \sa QFocusFrame::setWidget() | - | ||||||||||||||||||
238 | */ | - | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | QWidget * | - | ||||||||||||||||||
241 | QFocusFrame::widget() const | - | ||||||||||||||||||
242 | { | - | ||||||||||||||||||
243 | Q_D(const QFocusFrame); | - | ||||||||||||||||||
244 | return d->widget; never executed: return d->widget; | 0 | ||||||||||||||||||
245 | } | - | ||||||||||||||||||
246 | - | |||||||||||||||||||
247 | - | |||||||||||||||||||
248 | /*! \reimp */ | - | ||||||||||||||||||
249 | void | - | ||||||||||||||||||
250 | QFocusFrame::paintEvent(QPaintEvent *) | - | ||||||||||||||||||
251 | { | - | ||||||||||||||||||
252 | Q_D(QFocusFrame); | - | ||||||||||||||||||
253 | - | |||||||||||||||||||
254 | if (!d->widget)
| 0 | ||||||||||||||||||
255 | return; never executed: return; | 0 | ||||||||||||||||||
256 | - | |||||||||||||||||||
257 | QStylePainter p(this); | - | ||||||||||||||||||
258 | QStyleOption option; | - | ||||||||||||||||||
259 | initStyleOption(&option); | - | ||||||||||||||||||
260 | int vmargin = style()->pixelMetric(QStyle::PM_FocusFrameVMargin); | - | ||||||||||||||||||
261 | int hmargin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin); | - | ||||||||||||||||||
262 | QWidgetPrivate *wd = qt_widget_private(d->widget); | - | ||||||||||||||||||
263 | QRect rect = wd->clipRect().adjusted(0, 0, hmargin*2, vmargin*2); | - | ||||||||||||||||||
264 | p.setClipRect(rect); | - | ||||||||||||||||||
265 | p.drawControl(QStyle::CE_FocusFrame, option); | - | ||||||||||||||||||
266 | } never executed: end of block | 0 | ||||||||||||||||||
267 | - | |||||||||||||||||||
268 | - | |||||||||||||||||||
269 | /*! \reimp */ | - | ||||||||||||||||||
270 | bool | - | ||||||||||||||||||
271 | QFocusFrame::eventFilter(QObject *o, QEvent *e) | - | ||||||||||||||||||
272 | { | - | ||||||||||||||||||
273 | Q_D(QFocusFrame); | - | ||||||||||||||||||
274 | if(o == d->widget) {
| 0 | ||||||||||||||||||
275 | switch(e->type()) { | - | ||||||||||||||||||
276 | case QEvent::Move: never executed: case QEvent::Move: | 0 | ||||||||||||||||||
277 | case QEvent::Resize: never executed: case QEvent::Resize: | 0 | ||||||||||||||||||
278 | d->updateSize(); | - | ||||||||||||||||||
279 | break; never executed: break; | 0 | ||||||||||||||||||
280 | case QEvent::Hide: never executed: case QEvent::Hide: | 0 | ||||||||||||||||||
281 | case QEvent::StyleChange: never executed: case QEvent::StyleChange: | 0 | ||||||||||||||||||
282 | hide(); | - | ||||||||||||||||||
283 | break; never executed: break; | 0 | ||||||||||||||||||
284 | case QEvent::ParentChange: never executed: case QEvent::ParentChange: | 0 | ||||||||||||||||||
285 | if (d->showFrameAboveWidget) {
| 0 | ||||||||||||||||||
286 | QWidget *w = d->widget; | - | ||||||||||||||||||
287 | setWidget(0); | - | ||||||||||||||||||
288 | setWidget(w); | - | ||||||||||||||||||
289 | } else { never executed: end of block | 0 | ||||||||||||||||||
290 | d->update(); | - | ||||||||||||||||||
291 | } never executed: end of block | 0 | ||||||||||||||||||
292 | break; never executed: break; | 0 | ||||||||||||||||||
293 | case QEvent::Show: never executed: case QEvent::Show: | 0 | ||||||||||||||||||
294 | d->update(); | - | ||||||||||||||||||
295 | show(); | - | ||||||||||||||||||
296 | break; never executed: break; | 0 | ||||||||||||||||||
297 | case QEvent::PaletteChange: never executed: case QEvent::PaletteChange: | 0 | ||||||||||||||||||
298 | setPalette(d->widget->palette()); | - | ||||||||||||||||||
299 | break; never executed: break; | 0 | ||||||||||||||||||
300 | case QEvent::ZOrderChange: never executed: case QEvent::ZOrderChange: | 0 | ||||||||||||||||||
301 | if (style()->styleHint(QStyle::SH_FocusFrame_AboveWidget, 0, this))
| 0 | ||||||||||||||||||
302 | raise(); never executed: raise(); | 0 | ||||||||||||||||||
303 | else | - | ||||||||||||||||||
304 | stackUnder(d->widget); never executed: stackUnder(d->widget); | 0 | ||||||||||||||||||
305 | break; never executed: break; | 0 | ||||||||||||||||||
306 | case QEvent::Destroy: never executed: case QEvent::Destroy: | 0 | ||||||||||||||||||
307 | setWidget(0); | - | ||||||||||||||||||
308 | break; never executed: break; | 0 | ||||||||||||||||||
309 | default: never executed: default: | 0 | ||||||||||||||||||
310 | break; never executed: break; | 0 | ||||||||||||||||||
311 | } | - | ||||||||||||||||||
312 | } else if (d->showFrameAboveWidget) {
| 0 | ||||||||||||||||||
313 | // Handle changes in the parent widgets we are monitoring. | - | ||||||||||||||||||
314 | switch(e->type()) { | - | ||||||||||||||||||
315 | case QEvent::Move: never executed: case QEvent::Move: | 0 | ||||||||||||||||||
316 | case QEvent::Resize: never executed: case QEvent::Resize: | 0 | ||||||||||||||||||
317 | d->updateSize(); | - | ||||||||||||||||||
318 | break; never executed: break; | 0 | ||||||||||||||||||
319 | case QEvent::ZOrderChange: never executed: case QEvent::ZOrderChange: | 0 | ||||||||||||||||||
320 | raise(); | - | ||||||||||||||||||
321 | break; never executed: break; | 0 | ||||||||||||||||||
322 | default: never executed: default: | 0 | ||||||||||||||||||
323 | break; never executed: break; | 0 | ||||||||||||||||||
324 | } | - | ||||||||||||||||||
325 | } | - | ||||||||||||||||||
326 | return false; never executed: return false; | 0 | ||||||||||||||||||
327 | } | - | ||||||||||||||||||
328 | - | |||||||||||||||||||
329 | /*! \reimp */ | - | ||||||||||||||||||
330 | bool QFocusFrame::event(QEvent *e) | - | ||||||||||||||||||
331 | { | - | ||||||||||||||||||
332 | return QWidget::event(e); never executed: return QWidget::event(e); | 0 | ||||||||||||||||||
333 | } | - | ||||||||||||||||||
334 | - | |||||||||||||||||||
335 | QT_END_NAMESPACE | - | ||||||||||||||||||
336 | - | |||||||||||||||||||
337 | #include "moc_qfocusframe.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |