-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
actionlabel.cpp
121 lines (88 loc) · 2.73 KB
/
actionlabel.cpp
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
/***************************************************************************
* *
* Copyright: https://code.google.com/p/qsint/ *
* License: LGPL *
* *
***************************************************************************/
#include "actionlabel.h"
#include <QStyleOptionToolButton>
#include <QApplication>
namespace QSint
{
const char* ActionLabelStyle =
"QSint--ActionLabel[class='action'] {"
"background-color: transparent;"
"border: 1px solid transparent;"
"color: #0033ff;"
"text-align: left;"
"font: 11px;"
"}"
"QSint--ActionLabel[class='action']:!enabled {"
"color: #999999;"
"}"
"QSint--ActionLabel[class='action']:hover {"
"color: #0099ff;"
"text-decoration: underline;"
"}"
"QSint--ActionLabel[class='action']:focus {"
"border: 1px dotted black;"
"}"
"QSint--ActionLabel[class='action']:on {"
"background-color: #ddeeff;"
"color: #006600;"
"}"
;
ActionLabel::ActionLabel(QWidget *parent) :
QToolButton(parent)
{
init();
}
ActionLabel::ActionLabel(QAction *action, QWidget *parent) :
QToolButton(parent)
{
init();
setDefaultAction(action);
}
void ActionLabel::init()
{
setProperty("class", "action");
setCursor(Qt::PointingHandCursor);
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
setStyleSheet(QString(ActionLabelStyle));
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setFocusPolicy(Qt::StrongFocus);
}
QSize ActionLabel::sizeHint() const
{
ensurePolished();
int w = 0, h = 0;
QStyleOptionToolButton opt;
initStyleOption(&opt);
QString s(text());
bool empty = s.isEmpty();
if (empty)
s = QString::fromLatin1("XXXX");
QFontMetrics fm = fontMetrics();
QSize sz = fm.size(Qt::TextShowMnemonic, s);
w += sz.width();
h = qMax(h, sz.height());
opt.rect.setSize(QSize(w, h)); // PM_MenuButtonIndicator depends on the height
if (!icon().isNull()) {
int ih = opt.iconSize.height();
int iw = opt.iconSize.width() + 4;
w += iw;
h = qMax(h, ih);
}
if (menu())
w += style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &opt, this);
h += 4;
w += 8;
QSize sizeHint = (style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(w, h), this).
expandedTo(QApplication::globalStrut()));
return sizeHint;
}
QSize ActionLabel::minimumSizeHint() const
{
return sizeHint();
}
} // namespace