/
xymenustyle.cpp
191 lines (175 loc) · 5.37 KB
/
xymenustyle.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
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
#include "xymenustyle.h"
#include "xytooltips.h"
#include <QPainter>
#include <QMouseEvent>
#include <QAction>
XYMenuStyle::XYMenuStyle(QAction *action, QWidget *parent)
:QWidget(parent)
{
mopAction = action;
mopMenu = NULL;
mbContainsMouse = false;
mbIsMenu = false;
}
XYMenuStyle::XYMenuStyle(XYMenu *menu, QWidget *parent)
:QWidget(parent)
{
mopAction = NULL;
mopMenu = menu;
mbContainsMouse = false;
mbIsMenu = true;
}
XYMenuStyle::~XYMenuStyle()
{
}
bool XYMenuStyle::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip)
{
if (!mbIsMenu && mopAction)
{
XYToolTips::showToolTips(mopAction->shortcut().toString());
}
return true;
}
return QWidget::event(event);
}
void XYMenuStyle::paintEvent(QPaintEvent *event)
{
int width = this->width();
int height = this->height();
XYMenu *parent = (XYMenu *)this->parentWidget();
QPainter painter(this);
QPen pen = painter.pen();
painter.setPen(QPen(QColor(0, 0, 0, 0)));
// 画底色
if (parent->mopCurrentChecked == this)
{
painter.setBrush(QColor(85, 12, 41, 90));
}
else
{
painter.setBrush(QColor(28, 71, 0xd5, 90));
}
painter.drawRect(this->rect());
painter.setPen(pen);
if (!mbIsMenu)
{
QFontMetrics metrics(mopAction->font());
// 画图标
QPixmap pixmap(mopAction->icon().pixmap(25, 25));
painter.drawPixmap(QRect(10,
(height - pixmap.height()) / 2,
pixmap.width(),
pixmap.height()),
pixmap,
pixmap.rect());
// 画字体
painter.setFont(mopAction->font());
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.drawText(QRect(25 + 20,
(height - metrics.height()) / 2,
width,
height),
mopAction->text());
// 画是否勾选
if (mopAction->isCheckable())
{
QString s_ico;
if (mopAction->isChecked())
{
s_ico = ":/sourceImage/checked";
}
else
{
s_ico = ":/sourceImage/unchecked";
}
QPixmap checkico(s_ico);
checkico = checkico.scaled(QSize(15, 15),
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
painter.drawPixmap(QRect(25 + 20 + ((XYMenu *)parentWidget())->miActionMaxWidth + 10,
(height - checkico.height()) / 2,
checkico.width(),
checkico.height()),
checkico,
checkico.rect());
}
}
else
{
QFontMetrics metrics(mopMenu->font());
// 画图标
QPixmap pixmap(mopMenu->icon().pixmap(25, 25));
painter.drawPixmap(QRect(10,
(height - pixmap.height()) / 2,
pixmap.width(),
pixmap.height()),
pixmap,
pixmap.rect());
// 画字体
painter.setFont(mopMenu->font());
painter.drawText(QRect(25 + 20,
(height - metrics.height()) / 2,
width,
height),
mopMenu->title());
// 画下级菜单图标
QString s_ico = ":/sourceImage/menu2";
QPixmap menuico(s_ico);
menuico = menuico.scaled(QSize(15, 15),
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
painter.drawPixmap(QRect(25 + 20 + ((XYMenu *)parentWidget())->miActionMaxWidth + 15,
(height - menuico.height()) / 2,
menuico.width(),
menuico.height()),
menuico,
menuico.rect());
}
}
void XYMenuStyle::enterEvent(QEvent *event)
{
XYMenu *parent = (XYMenu *)this->parentWidget();
parent->mopCurrentChecked = this;
parent->updateCheckedMenu();
parent->execMenu2(this);
}
void XYMenuStyle::mouseReleaseEvent(QMouseEvent *event)
{
// 如果释放鼠标的位置不在窗口内,直接退出
if (!this->rect().contains(event->pos()))
{
return;
}
if (!mbIsMenu)
{
if (!mopAction->isCheckable())
{
((XYMenu *)parentWidget())->close(true);
}
else
{
mopAction->setChecked(!mopAction->isChecked());
if (event->pos().x() < width() - 25)
{
((XYMenu *)parentWidget())->close(true);
}
else
{
update();
((XYMenu *)parentWidget())->raise();
}
}
#if QT_VERSION >= 0x050000
emit mopAction->triggered(mopAction->isChecked());
#else
mopAction->trigger();
#endif
}
else
{
mopMenu->exec(((XYMenu *)parentWidget()));
}
event->accept();
}