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