forked from AmuUncle/MusicPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconhelper.cpp
184 lines (146 loc) · 5.73 KB
/
iconhelper.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
#include "iconhelper.h"
#include <QApplication>
#include <QFontDatabase>
#include <QPainter>
QFont IconHelper::m_iconFont;
bool IconHelper::Load()
{
int nFontId = QFontDatabase::addApplicationFont(":/font/ttf/iconfont.ttf");
QStringList strlistFontName = QFontDatabase::applicationFontFamilies(nFontId);
if (strlistFontName.count() > 0)
{
m_iconFont = QFont(strlistFontName.at(0));
m_iconFont.setStyleStrategy(QFont::PreferAntialias);
return true;
}
return false;
}
void IconHelper::SetIcon( QLabel *label, QChar iconCode, uint dwSize )
{
if (NULL == label)
return;
m_iconFont.setPixelSize(dwSize); // 图标字体的大小不会随DPI的改变而发生变化。
label->setFont(m_iconFont);
label->setText(iconCode);
label->setAlignment(Qt::AlignCenter);
}
void IconHelper::SetIcon( QPushButton *btn, QChar iconCode, uint dwSize /*= 26*/)
{
if (NULL == btn)
return;
m_iconFont.setPixelSize(dwSize); // 图标字体的大小不会随DPI的改变而发生变化。
btn->setFont(m_iconFont);
btn->setText(iconCode);
btn->setProperty("class", "IconfontBtn"); // iconfont 单独为一类别
}
void IconHelper::SetIcon(QPushButton *btn, QChar iconCode, const QString &strColor, uint dwSize)
{
if (NULL == btn)
return;
m_iconFont.setPixelSize(dwSize); // 图标字体的大小不会随DPI的改变而发生变化。
btn->setFont(m_iconFont);
btn->setText(iconCode);
btn->setStyleSheet(QString("background:none;border-style:none;color:%1").arg(strColor));
}
void IconHelper::SetIcon( QToolButton *btn, QChar iconCode, const QString &strColor /*= "#FFFFFF"*/,
uint dwIconSize /*= 26*/)
{
if (NULL == btn)
return;
QPixmap pixNormal = GetPixmap(strColor, iconCode, dwIconSize, dwIconSize, dwIconSize);
btn->setIcon(QIcon(pixNormal));
btn->setIconSize(QSize(dwIconSize, dwIconSize));
}
void IconHelper::SetIcon( QAction *action, QChar iconCode, const QString &strColor /*= "#000000"*/,
uint dwIconSize /*= 40*/)
{
if (NULL == action)
return;
QPixmap pixNormal = GetPixmap(strColor, iconCode, dwIconSize, 30, 30); // TODO: 待研究
action->setIcon(QIcon(pixNormal));
action->setIconVisibleInMenu(true);
}
void IconHelper::SetIcon( QLabel *label, const QString &strMainIcon /*= ""*/, const QString &strSecIcon /*= ""*/,
const QString &strThirdIcon /*= ""*/, uint dwIconSize /*= 24*/)
{
if (NULL == label)
return;
QPixmap pixImage = GetPixmap(strMainIcon, strSecIcon, strThirdIcon, dwIconSize, dwIconSize);
label->setPixmap(pixImage);
}
void IconHelper::SetIcon(QStandardItem *item, QChar iconCode, uint dwSize)
{
if (NULL == item)
return;
m_iconFont.setPixelSize(dwSize); // 图标字体的大小不会随DPI的改变而发生变化。
item->setFont(m_iconFont);
item->setText(iconCode);
}
QPixmap IconHelper::GetPixmap( const QString &strColor, QChar iconCode, uint dwSize , uint dwPixWidth, uint dwPixHeight )
{
QPixmap pix(dwPixWidth, dwPixHeight);
pix.fill(Qt::transparent);
QPainter painter;
painter.begin(&pix);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); // 抗锯齿和使用平滑转换算法
painter.setPen(QColor(strColor));
painter.setBrush(QColor(strColor));
m_iconFont.setPixelSize(dwSize);
painter.setFont(m_iconFont);
painter.drawText(pix.rect(), Qt::AlignCenter, iconCode);
painter.end();
return pix;
}
QPixmap IconHelper::GetPixmap( const QString &strMainIcon, const QString &strSecIcon, const QString &strThirdIcon, uint dwPixWidth, uint dwPixHeight )
{
QPixmap pix(dwPixWidth, dwPixHeight);
pix.fill(Qt::transparent);
QRect rcClient = pix.rect();
QPainter painter;
painter.begin(&pix);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); // 抗锯齿和使用平滑转换算法
painter.drawImage(rcClient, QImage(strMainIcon));
// 右下角
if (strSecIcon.length() > 0)
{
QRect rcSecIcon = rcClient;
QImage imgSecIcon = QImage(strSecIcon);
rcSecIcon.setTopLeft(QPoint(rcSecIcon.right() - imgSecIcon.width(), rcSecIcon.bottom() - imgSecIcon.height()));
painter.drawImage(rcSecIcon, imgSecIcon);
}
// 左下角
if (strThirdIcon.length() > 0)
{
QRect rcThirdIcon = rcClient;
QImage imgThirdIcon = QImage(strThirdIcon);
rcThirdIcon.setTopRight(QPoint(rcThirdIcon.left() + imgThirdIcon.width(), rcThirdIcon.bottom() - imgThirdIcon.height()));
painter.drawImage(rcThirdIcon, imgThirdIcon);
}
painter.end();
return pix;
}
QPixmap IconHelper::GetDragChnPixmap( QString strText )
{
const int nSpace = 5;
QFontMetrics cs(QApplication::font());
int nW = cs.width(strText) + nSpace * 2;
int nH = cs.height()+ nSpace * 2;
QPixmap pix(nW, nH);
QRect rcClient = pix.rect();
pix.fill(QColor(65, 70, 98, 255));
QPainter painter;
painter.begin(&pix);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); //抗锯齿和使用平滑转换算法
painter.save();
painter.setPen(QPen(QColor(0x3d, 0x77, 0xda), 1, Qt::SolidLine, Qt::RoundCap));
rcClient.setTopLeft(QPoint(rcClient.top() + 1, rcClient.left() + 1));
rcClient.setBottomRight(QPoint(rcClient.bottom() - 1, rcClient.right() - 1));
painter.drawRect(pix.rect());
painter.restore();
painter.save();
painter.setPen(Qt::white);
painter.drawText(pix.rect(), Qt::AlignCenter, strText);
painter.restore();
painter.end();
return pix;
}