Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/sublime/idealtoolbutton.cpp
Show All 29 Lines | |||||
30 | { | 30 | { | ||
31 | setFocusPolicy(Qt::NoFocus); | 31 | setFocusPolicy(Qt::NoFocus); | ||
32 | KAcceleratorManager::setNoAccel(this); | 32 | KAcceleratorManager::setNoAccel(this); | ||
33 | setCheckable(true); | 33 | setCheckable(true); | ||
34 | setAutoRaise(true); | 34 | setAutoRaise(true); | ||
35 | setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | 35 | setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | ||
36 | 36 | | |||
37 | setContextMenuPolicy(Qt::CustomContextMenu); | 37 | setContextMenuPolicy(Qt::CustomContextMenu); | ||
38 | | ||||
39 | QSizePolicy sizePolicy = this->sizePolicy(); | ||||
40 | if (orientation() == Qt::Horizontal) { | ||||
41 | sizePolicy.setHorizontalPolicy(QSizePolicy::Preferred); | ||||
42 | } else { | ||||
43 | sizePolicy.setVerticalPolicy(QSizePolicy::Preferred); | ||||
44 | } | ||||
45 | setSizePolicy(sizePolicy); | ||||
38 | } | 46 | } | ||
39 | 47 | | |||
40 | Qt::Orientation IdealToolButton::orientation() const | 48 | Qt::Orientation IdealToolButton::orientation() const | ||
41 | { | 49 | { | ||
42 | if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) | 50 | if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) | ||
43 | return Qt::Vertical; | 51 | return Qt::Vertical; | ||
44 | 52 | | |||
45 | return Qt::Horizontal; | 53 | return Qt::Horizontal; | ||
Show All 35 Lines | 57 | { | |||
81 | 89 | | |||
82 | if (orientation() == Qt::Vertical) { | 90 | if (orientation() == Qt::Vertical) { | ||
83 | return QSize(size.height(), size.width()); // transposed | 91 | return QSize(size.height(), size.width()); // transposed | ||
84 | } else { | 92 | } else { | ||
85 | return size; | 93 | return size; | ||
86 | } | 94 | } | ||
87 | } | 95 | } | ||
88 | 96 | | |||
97 | QSize IdealToolButton::minimumSizeHint() const | ||||
98 | { | ||||
99 | QStyleOptionToolButton opt; | ||||
kossebau: We might better be safe and call `ensurePolished();` as well here at the begin. | |||||
100 | initStyleOption(&opt); | ||||
101 | | ||||
102 | // if style has icons, minimumSize is the size of the icon | ||||
103 | if (toolButtonStyle() != Qt::ToolButtonTextOnly && !opt.icon.isNull()) { | ||||
104 | int iconWidth = 0; | ||||
105 | int iconHeight = 0; | ||||
106 | if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) { | ||||
107 | iconWidth = opt.iconSize.width(); | ||||
108 | iconHeight = opt.iconSize.height(); | ||||
109 | } else { | ||||
110 | iconWidth = opt.iconSize.height(); | ||||
111 | iconHeight = opt.iconSize.width(); | ||||
112 | } | ||||
113 | | ||||
114 | return {iconWidth + 1, iconHeight + 1}; | ||||
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… | |||||
115 | } else { | ||||
kossebau: No `else` after a branch ending in `return`, please. | |||||
116 | // if no icon, set an arbitrary minimum size | ||||
117 | QFontMetrics fm = fontMetrics(); | ||||
118 | const QSize minimumSize = fm.size(Qt::TextShowMnemonic, opt.text.left(3) + QStringLiteral("...")); | ||||
119 | if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) { | ||||
120 | return minimumSize; | ||||
121 | } else { | ||||
kossebau: No `else` after branch ending in `return`. | |||||
122 | return {minimumSize.height(), minimumSize.width()}; | ||||
kossebau: Please `return minimumSize.transposed();` | |||||
123 | } | ||||
124 | } | ||||
125 | } | ||||
126 | | ||||
127 | | ||||
89 | void IdealToolButton::paintEvent(QPaintEvent *event) | 128 | void IdealToolButton::paintEvent(QPaintEvent *event) | ||
90 | { | 129 | { | ||
91 | Q_UNUSED(event); | 130 | Q_UNUSED(event); | ||
92 | 131 | | |||
93 | QStylePainter painter(this); | 132 | QStylePainter painter(this); | ||
94 | QStyleOptionToolButton option; | 133 | QStyleOptionToolButton option; | ||
95 | initStyleOption(&option); | 134 | initStyleOption(&option); | ||
96 | 135 | | |||
▲ 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.