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 "qtoolbarextension_p.h" |
5 | #include <qevent.h> |
6 | #include <qstyle.h> |
7 | #include <qstylepainter.h> |
8 | #include <qstyleoption.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | using namespace Qt::StringLiterals; |
13 | |
14 | QToolBarExtension::QToolBarExtension(QWidget *parent) |
15 | : QToolButton(parent) |
16 | , m_orientation(Qt::Horizontal) |
17 | { |
18 | setObjectName("qt_toolbar_ext_button"_L1); |
19 | setAutoRaise(true); |
20 | setOrientation(m_orientation); |
21 | setSizePolicy(hor: QSizePolicy::Minimum, ver: QSizePolicy::Minimum); |
22 | setCheckable(true); |
23 | } |
24 | |
25 | void QToolBarExtension::setOrientation(Qt::Orientation o) |
26 | { |
27 | QStyleOption opt; |
28 | opt.initFrom(w: this); |
29 | if (o == Qt::Horizontal) { |
30 | setIcon(style()->standardIcon(standardIcon: QStyle::SP_ToolBarHorizontalExtensionButton, option: &opt)); |
31 | } else { |
32 | setIcon(style()->standardIcon(standardIcon: QStyle::SP_ToolBarVerticalExtensionButton, option: &opt)); |
33 | } |
34 | m_orientation = o; |
35 | } |
36 | |
37 | void QToolBarExtension::paintEvent(QPaintEvent *) |
38 | { |
39 | QStylePainter p(this); |
40 | QStyleOptionToolButton opt; |
41 | initStyleOption(option: &opt); |
42 | // We do not need to draw both extension arrows |
43 | opt.features &= ~QStyleOptionToolButton::HasMenu; |
44 | p.drawComplexControl(cc: QStyle::CC_ToolButton, opt); |
45 | } |
46 | |
47 | |
48 | QSize QToolBarExtension::sizeHint() const |
49 | { |
50 | QStyleOption opt; |
51 | opt.initFrom(w: this); |
52 | const int ext = style()->pixelMetric(metric: QStyle::PM_ToolBarExtensionExtent, option: &opt); |
53 | return QSize(ext, ext); |
54 | } |
55 | |
56 | bool QToolBarExtension::event(QEvent *event) |
57 | { |
58 | switch (event->type()) { |
59 | case QEvent::LayoutDirectionChange: |
60 | setOrientation(m_orientation); |
61 | break; |
62 | default: |
63 | break; |
64 | } |
65 | return QToolButton::event(e: event); |
66 | } |
67 | |
68 | QT_END_NAMESPACE |
69 | |
70 | #include "moc_qtoolbarextension_p.cpp" |
71 |