Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/sublime/idealtoolbutton.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | Copyright 2007 Roberto Raggi <roberto@kdevelop.org> | 2 | Copyright 2007 Roberto Raggi <roberto@kdevelop.org> | ||
3 | Copyright 2007 Hamish Rodda <rodda@kde.org> | 3 | Copyright 2007 Hamish Rodda <rodda@kde.org> | ||
4 | Copyright 2011 Alexander Dymo <adymo@kdevelop.org> | 4 | Copyright 2011 Alexander Dymo <adymo@kdevelop.org> | ||
5 | Copyright 2018 Amish Naidu <amhndu@gmail.com> | ||||
5 | 6 | | |||
6 | Permission to use, copy, modify, distribute, and sell this software and its | 7 | Permission to use, copy, modify, distribute, and sell this software and its | ||
7 | documentation for any purpose is hereby granted without fee, provided that | 8 | documentation for any purpose is hereby granted without fee, provided that | ||
8 | the above copyright notice appear in all copies and that both that | 9 | the above copyright notice appear in all copies and that both that | ||
9 | copyright notice and this permission notice appear in supporting | 10 | copyright notice and this permission notice appear in supporting | ||
10 | documentation. | 11 | documentation. | ||
11 | 12 | | |||
12 | The above copyright notice and this permission notice shall be included in | 13 | The above copyright notice and this permission notice shall be included in | ||
13 | all copies or substantial portions of the Software. | 14 | all copies or substantial portions of the Software. | ||
14 | 15 | | |||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
18 | KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | 19 | KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
19 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 20 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
21 | */ | 22 | */ | ||
22 | 23 | | |||
23 | #include "idealtoolbutton.h" | 24 | #include "idealtoolbutton.h" | ||
24 | #include <KAcceleratorManager> | 25 | #include <KAcceleratorManager> | ||
25 | #include <QStyleOption> | 26 | #include <QStyleOption> | ||
26 | #include <QStylePainter> | 27 | #include <QStylePainter> | ||
28 | #include <QApplication> | ||||
27 | 29 | | |||
28 | IdealToolButton::IdealToolButton(Qt::DockWidgetArea area, QWidget *parent) | 30 | IdealToolButton::IdealToolButton(Qt::DockWidgetArea area, QWidget *parent) | ||
29 | : QToolButton(parent), _area(area) | 31 | : QToolButton(parent), _area(area) | ||
30 | { | 32 | { | ||
31 | setFocusPolicy(Qt::NoFocus); | 33 | setFocusPolicy(Qt::NoFocus); | ||
32 | KAcceleratorManager::setNoAccel(this); | 34 | KAcceleratorManager::setNoAccel(this); | ||
33 | setCheckable(true); | 35 | setCheckable(true); | ||
34 | setAutoRaise(true); | 36 | setAutoRaise(true); | ||
35 | setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | 37 | setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | ||
36 | 38 | | |||
37 | setContextMenuPolicy(Qt::CustomContextMenu); | 39 | setContextMenuPolicy(Qt::CustomContextMenu); | ||
40 | | ||||
41 | QSizePolicy sizePolicy = this->sizePolicy(); | ||||
42 | if (orientation() == Qt::Horizontal) { | ||||
43 | sizePolicy.setHorizontalPolicy(QSizePolicy::Maximum); | ||||
44 | } else { | ||||
45 | sizePolicy.setVerticalPolicy(QSizePolicy::Maximum); | ||||
46 | } | ||||
47 | setSizePolicy(sizePolicy); | ||||
38 | } | 48 | } | ||
39 | 49 | | |||
40 | Qt::Orientation IdealToolButton::orientation() const | 50 | Qt::Orientation IdealToolButton::orientation() const | ||
41 | { | 51 | { | ||
42 | if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) | 52 | if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) | ||
43 | return Qt::Vertical; | 53 | return Qt::Vertical; | ||
44 | 54 | | |||
45 | return Qt::Horizontal; | 55 | return Qt::Horizontal; | ||
Show All 35 Lines | 59 | { | |||
81 | 91 | | |||
82 | if (orientation() == Qt::Vertical) { | 92 | if (orientation() == Qt::Vertical) { | ||
83 | return QSize(size.height(), size.width()); // transposed | 93 | return QSize(size.height(), size.width()); // transposed | ||
84 | } else { | 94 | } else { | ||
85 | return size; | 95 | return size; | ||
86 | } | 96 | } | ||
87 | } | 97 | } | ||
88 | 98 | | |||
99 | QSize IdealToolButton::minimumSizeHint() const | ||||
100 | { | ||||
101 | ensurePolished(); | ||||
kossebau: We might better be safe and call `ensurePolished();` as well here at the begin. | |||||
102 | | ||||
103 | QStyleOptionToolButton opt; | ||||
104 | initStyleOption(&opt); | ||||
105 | | ||||
106 | QSize minimumSize; | ||||
107 | // if style has icons, minimumSize is the size of the icon | ||||
108 | if (toolButtonStyle() != Qt::ToolButtonTextOnly && !opt.icon.isNull()) { | ||||
109 | minimumSize = opt.iconSize; | ||||
110 | if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) { | ||||
111 | minimumSize.transpose(); | ||||
112 | } | ||||
113 | } else { | ||||
114 | // if no icon, set an arbitrary minimum size | ||||
115 | QFontMetrics fm = fontMetrics(); | ||||
116 | minimumSize = fm.size(Qt::TextShowMnemonic, opt.text.left(4)); | ||||
Why the +1 ? To make up for any stuff style adds? For that it might be better in any case to do at the end of the method some call like sizeHint = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(w, h), this). expandedTo(QApplication::globalStrut()); Cmp. also implemenation of QToolButton::sizeHint() e.g. to be seen at https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qtoolbutton.cpp.html#_ZNK11QToolButton8sizeHintEv And yes, in a follow-up commit we should also update the sizeHint() implementation to Qt5 style API. kossebau: Why the +1 ? To make up for any stuff style adds?
For that it might be better in any case to… | |||||
117 | } | ||||
kossebau: No `else` after a branch ending in `return`, please. | |||||
118 | | ||||
119 | minimumSize = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, minimumSize, this). | ||||
120 | expandedTo(QApplication::globalStrut()); | ||||
121 | | ||||
122 | if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) { | ||||
123 | return minimumSize; | ||||
kossebau: No `else` after branch ending in `return`. | |||||
124 | } | ||||
kossebau: Please `return minimumSize.transposed();` | |||||
125 | return minimumSize.transposed(); | ||||
126 | } | ||||
127 | | ||||
128 | | ||||
89 | void IdealToolButton::paintEvent(QPaintEvent *event) | 129 | void IdealToolButton::paintEvent(QPaintEvent *event) | ||
90 | { | 130 | { | ||
91 | Q_UNUSED(event); | 131 | Q_UNUSED(event); | ||
92 | 132 | | |||
93 | QStylePainter painter(this); | 133 | QStylePainter painter(this); | ||
94 | QStyleOptionToolButton option; | 134 | QStyleOptionToolButton option; | ||
95 | initStyleOption(&option); | 135 | initStyleOption(&option); | ||
96 | 136 | | |||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |
We might better be safe and call ensurePolished(); as well here at the begin.