Qt 4.8
qfocusframe.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qfocusframe.h"
43 #include "qstyle.h"
44 #include "qbitmap.h"
45 #include "qstylepainter.h"
46 #include "qstyleoption.h"
47 #include "qdebug.h"
48 #include <private/qwidget_p.h>
49 
51 
53 {
58 public:
60  widget = 0;
61  frameParent = 0;
62  sendChildEvents = false;
63  showFrameAboveWidget = false;
64  }
65  void updateSize();
66  void update();
67 };
68 
70 {
72  q->setParent(frameParent);
73  updateSize();
74  if (q->parentWidget()->rect().intersects(q->geometry())) {
76  q->raise();
77  else
78  q->stackUnder(widget);
79  q->show();
80  } else {
81  q->hide();
82  }
83 }
84 
86 {
88  if (!widget)
89  return;
90 
91  int vmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameVMargin),
92  hmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
93  QPoint pos(widget->x(), widget->y());
94  if (q->parentWidget() != widget->parentWidget())
95  pos = widget->parentWidget()->mapTo(q->parentWidget(), pos);
96  QRect geom(pos.x()-hmargin, pos.y()-vmargin,
97  widget->width()+(hmargin*2), widget->height()+(vmargin*2));
98  if(q->geometry() == geom)
99  return;
100 
101  q->setGeometry(geom);
103  QStyleOption opt;
104  q->initStyleOption(&opt);
105  if (q->style()->styleHint(QStyle::SH_FocusFrame_Mask, &opt, q, &mask))
106  q->setMask(mask.region);
107 }
108 
117 {
118  if (!option)
119  return;
120 
121  option->initFrom(this);
122 }
123 
162  : QWidget(*new QFocusFramePrivate, parent, 0)
163 {
168 }
169 
175 {
176 }
177 
187 void
189 {
190  Q_D(QFocusFrame);
191 
193  d->showFrameAboveWidget = true;
194  else
195  d->showFrameAboveWidget = false;
196 
197  if (widget == d->widget)
198  return;
199  if (d->widget) {
200  // Remove event filters from the widget hierarchy.
201  QWidget *p = d->widget;
202  do {
203  p->removeEventFilter(this);
204  if (!d->showFrameAboveWidget || p == d->frameParent)
205  break;
206  p = p->parentWidget();
207  }while (p);
208  }
209  if (widget && !widget->isWindow() && widget->parentWidget()->windowType() != Qt::SubWindow) {
210  d->widget = widget;
211  d->widget->installEventFilter(this);
212  QWidget *p = widget->parentWidget();
213  QWidget *prev = 0;
214  if (d->showFrameAboveWidget) {
215  // Find the right parent for the focus frame.
216  while (p) {
217  // Traverse the hirerarchy of the 'widget' for setting event filter.
218  // During this if come across toolbar or a top level, use that
219  // as the parent for the focus frame. If we find a scroll area
220  // use its viewport as the parent.
221  bool isScrollArea = false;
222  if (p->isWindow() || p->inherits("QToolBar") || (isScrollArea = p->inherits("QAbstractScrollArea"))) {
223  d->frameParent = p;
224  // The previous one in the hierarchy will be the viewport.
225  if (prev && isScrollArea)
226  d->frameParent = prev;
227  break;
228  } else {
229  p->installEventFilter(this);
230  prev = p;
231  p = p->parentWidget();
232  }
233  }
234  } else {
235  d->frameParent = p;
236  }
237  d->update();
238  } else {
239  d->widget = 0;
240  hide();
241  }
242 }
243 
251 QWidget *
253 {
254  Q_D(const QFocusFrame);
255  return d->widget;
256 }
257 
258 
260 void
262 {
263  Q_D(QFocusFrame);
264  QStylePainter p(this);
265  QStyleOption option;
266  initStyleOption(&option);
269  QWidgetPrivate *wd = qt_widget_private(d->widget);
270  QRect rect = wd->clipRect().adjusted(0, 0, hmargin*2, vmargin*2);
271  p.setClipRect(rect);
273 }
274 
275 
277 bool
279 {
280  Q_D(QFocusFrame);
281  if(o == d->widget) {
282  switch(e->type()) {
283  case QEvent::Move:
284  case QEvent::Resize:
285  d->updateSize();
286  break;
287  case QEvent::Hide:
288  case QEvent::StyleChange:
289  hide();
290  break;
292  if (d->showFrameAboveWidget) {
293  QWidget *w = d->widget;
294  setWidget(0);
295  setWidget(w);
296  } else {
297  d->update();
298  }
299  break;
300  case QEvent::Show:
301  d->update();
302  show();
303  break;
305  setPalette(d->widget->palette());
306  break;
309  raise();
310  else
311  stackUnder(d->widget);
312  break;
313  case QEvent::Destroy:
314  setWidget(0);
315  break;
316  default:
317  break;
318  }
319  } else if (d->showFrameAboveWidget) {
320  // Handle changes in the parent widgets we are monitoring.
321  switch(e->type()) {
322  case QEvent::Move:
323  case QEvent::Resize:
324  d->updateSize();
325  break;
327  raise();
328  break;
329  default:
330  break;
331  }
332  }
333  return false;
334 }
335 
338 {
339  return QWidget::event(e);
340 }
341 
double d
Definition: qnumeric_p.h:62
void stackUnder(QWidget *)
Places the widget under w in the parent widget&#39;s stack.
Definition: qwidget.cpp:11972
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
QRect adjusted(int x1, int y1, int x2, int y2) const
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition: qrect.h:431
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void initStyleOption(QStyleOption *option) const
Initialize option with the values from this QFocusFrame.
void drawControl(QStyle::ControlElement ce, const QStyleOption &opt)
Use the widget&#39;s style to draw a control element ce specified by QStyleOption option.
Definition: qstylepainter.h:87
int width
the width of the widget excluding any window frame
Definition: qwidget.h:166
QPoint mapTo(QWidget *, const QPoint &) const
Translates the widget coordinate pos to the coordinate system of parent.
Definition: qwidget.cpp:4409
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
QRect clipRect() const
Definition: qwidget.cpp:1997
void removeEventFilter(QObject *)
Removes an event filter object obj from this object.
Definition: qobject.cpp:2099
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void paintEvent(QPaintEvent *)
Reimplemented Function
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=0, const QWidget *widget=0, QStyleHintReturn *returnData=0) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
QWidget * widget() const
Returns the currently monitored widget for automatically resize and update.
void setWidget(QWidget *widget)
QFocusFrame will track changes to widget and resize itself automatically.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
int x
the x coordinate of the widget relative to its parent including any window frame
Definition: qwidget.h:161
QStyle * style() const
Definition: qwidget.cpp:2742
#define Q_Q(Class)
Definition: qglobal.h:2483
bool eventFilter(QObject *, QEvent *)
Reimplemented Function
QWidget * frameParent
Definition: qfocusframe.cpp:56
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QStyleOption class stores the parameters used by QStyle functions.
Definition: qstyleoption.h:67
int height
the height of the widget excluding any window frame
Definition: qwidget.h:167
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
const char * styleHint(const QFontDef &request)
void show()
Shows the widget and its child widgets.
bool inherits(const char *classname) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass t...
Definition: qobject.h:275
QRect rect() const
void hide()
Hides the widget.
Definition: qwidget.h:501
~QFocusFrame()
Destructor.
The QStyleHintReturnMask class provides style hints that return a QRegion.
Definition: qstyleoption.h:923
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
bool event(QEvent *e)
Reimplemented Function
int y
the y coordinate of the widget relative to its parent and including any window frame ...
Definition: qwidget.h:162
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QObject * parent
Definition: qobject.h:92
The QFocusFrame widget provides a focus frame which can be outside of a widget&#39;s normal paintable are...
Definition: qfocusframe.h:56
friend Q_GUI_EXPORT QWidgetPrivate * qt_widget_private(QWidget *widget)
Definition: qwidget.cpp:12920
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget...
Definition: qstylepainter.h:55
Qt::WindowType windowType() const
Returns the window type of this widget.
Definition: qwidget.h:937
uint sendChildEvents
Definition: qobject.h:100
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
QRegion region
the region for style hints that return a QRegion
Definition: qstyleoption.h:930
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
QFocusFrame(QWidget *parent=0)
Constructs a QFocusFrame.
void setPalette(const QPalette &)
Use the single-argument overload instead.
Definition: qwidget.cpp:4858
void setFocusPolicy(Qt::FocusPolicy policy)
Definition: qwidget.cpp:7631