File: idealtoolbutton.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (193 lines) | stat: -rw-r--r-- 6,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
  Copyright 2007 Roberto Raggi <roberto@kdevelop.org>
  Copyright 2007 Hamish Rodda <rodda@kde.org>
  Copyright 2011 Alexander Dymo <adymo@kdevelop.org>
  Copyright 2018 Amish Naidu <amhndu@gmail.com>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "idealtoolbutton.h"
#include <KAcceleratorManager>
#include <QStyleOption>
#include <QStylePainter>
#include <QApplication>

IdealToolButton::IdealToolButton(Qt::DockWidgetArea area, QWidget *parent)
    : QToolButton(parent), _area(area)
{
    setFocusPolicy(Qt::NoFocus);
    KAcceleratorManager::setNoAccel(this);
    setCheckable(true);
    setAutoRaise(true);
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    setContextMenuPolicy(Qt::CustomContextMenu);

    QSizePolicy sizePolicy = this->sizePolicy();
    if (orientation() == Qt::Horizontal) {
        sizePolicy.setHorizontalPolicy(QSizePolicy::Maximum);
    } else {
        sizePolicy.setVerticalPolicy(QSizePolicy::Maximum);
    }
    setSizePolicy(sizePolicy);
}

Qt::Orientation IdealToolButton::orientation() const
{
    if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea)
        return Qt::Vertical;

    return Qt::Horizontal;
}

QSize IdealToolButton::sizeHint() const
{
    ensurePolished();

    QStyleOptionToolButton opt;
    initStyleOption(&opt);

    QFontMetrics fm = fontMetrics();

#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
    const int charWidth = fm.horizontalAdvance(QLatin1Char('x'));
#else
    const int charWidth = fm.width(QLatin1Char('x'));
#endif
    QSize textSize;
    // Use text size only if we request text
    if (toolButtonStyle() != Qt::ToolButtonIconOnly || opt.icon.isNull()) {
        textSize = fm.size(Qt::TextShowMnemonic, opt.text);
        textSize.rwidth() += 2 * charWidth;
    }

    int iconwidth = 0, iconheight = 0;
    // Use icon size only if it's requested and the icon is valid
    if (toolButtonStyle() != Qt::ToolButtonTextOnly && !opt.icon.isNull()) {
        if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) {
            iconwidth = opt.iconSize.width();
            iconheight = opt.iconSize.height();
        } else {
            iconwidth = opt.iconSize.height();
            iconheight = opt.iconSize.width();
        }
    }
    // adding +4 to be consistent with qtoolbutton
    int width = textSize.width() + iconwidth + 4;
    int height = qMax(textSize.height(), iconheight);
    QSize size = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(width, height), this);

    if (orientation() == Qt::Vertical) {
        return QSize(size.height(), size.width()); // transposed
    } else {
        return size;
    }
}

QSize IdealToolButton::minimumSizeHint() const
{
    ensurePolished();

    QStyleOptionToolButton opt;
    initStyleOption(&opt);

    QSize minimumSize;
    // if style has icons, minimumSize is the size of the icon
    if (toolButtonStyle() != Qt::ToolButtonTextOnly && !opt.icon.isNull()) {
        minimumSize = opt.iconSize;
        if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) {
            minimumSize.transpose();
        }
    } else {
        // if no icon, set an arbitrary minimum size
        QFontMetrics fm = fontMetrics();
        minimumSize = fm.size(Qt::TextShowMnemonic, opt.text.left(4));
    }

    minimumSize = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, minimumSize, this);

    if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) {
        return minimumSize;
    }
    return minimumSize.transposed();
}


void IdealToolButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QStylePainter painter(this);
    QStyleOptionToolButton option;
    initStyleOption(&option);

    if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) {
        // elide text
        int iconWidth = 0;
        if (toolButtonStyle() != Qt::ToolButtonTextOnly && !option.icon.isNull()) {
            iconWidth = option.iconSize.width();
        }

        // subtract 4 to be consistent with the size calculated by sizeHint, which adds 4,
        // again to be consistent with QToolButton
        option.text = fontMetrics().elidedText(text(), Qt::ElideRight, contentsRect().width() - iconWidth - 4);
        painter.drawComplexControl(QStyle::CC_ToolButton, option);
    } else {
        // rotated paint
        // elide text
        int iconHeight = 0;
        if (toolButtonStyle() != Qt::ToolButtonTextOnly && !option.icon.isNull()) {
            iconHeight = option.iconSize.height();
        }
        QString textToDraw = fontMetrics().elidedText(text(), Qt::ElideRight, contentsRect().height() - iconHeight - 4);
        // first draw normal frame and not text/icon
        option.text = QString();
        option.icon = QIcon();
        painter.drawComplexControl(QStyle::CC_ToolButton, option);

        // rotate the options
        QSize size( option.rect.size() );
        size.transpose();
        option.rect.setSize( size );

        // rotate the painter
        if(_area == Qt::LeftDockWidgetArea) {
            painter.translate( 0, height() );
            painter.rotate( -90 );
        } else {
            painter.translate( width(), 0 );
            painter.rotate( 90 );
        }

        // paint text and icon
        option.text = textToDraw;

        QIcon::Mode iconMode = (option.state & QStyle::State_MouseOver) ? QIcon::Active : QIcon::Normal;
        QPixmap ic = icon().pixmap(option.iconSize, iconMode, QIcon::On);
        QTransform tf;
        if(_area == Qt::LeftDockWidgetArea) {
            tf.rotate(90);
        } else {
            tf.rotate(-90);
        }
        option.icon = ic.transformed( tf, Qt::SmoothTransformation );
        painter.drawControl(QStyle::CE_ToolButtonLabel, option);
        painter.end();
    }
}