forked from feiyangqingyun/QWidgetDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmiconhelper.cpp
418 lines (361 loc) · 13.5 KB
/
frmiconhelper.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#pragma execution_character_set("utf-8")
#include "frmiconhelper.h"
#include "ui_frmiconhelper.h"
#include "iconhelper.h"
frmIconHelper::frmIconHelper(QWidget *parent) : QWidget(parent), ui(new Ui::frmIconHelper)
{
ui->setupUi(this);
this->initForm();
this->initWidget1();
this->initWidget2();
this->initWidget3();
this->initWidget4();
this->initWidget5();
this->initWidget6();
QTimer::singleShot(100, this, SLOT(initPanel()));
}
frmIconHelper::~frmIconHelper()
{
delete ui;
}
bool frmIconHelper::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::Enter) {
QLabel *lab = (QLabel *)watched;
if (lab != 0) {
//由于有图形字体的范围值冲突需要手动切换索引
if (ui->rbtnFontAwesome6->isChecked()) {
IconHelper::setIconFontIndex(2);
} else if (ui->rbtnFontWeather->isChecked()) {
IconHelper::setIconFontIndex(3);
} else {
IconHelper::setIconFontIndex(-1);
}
//对应图形字体的16进制值已经赋值给了 toolTip
QString value = lab->toolTip();
ui->labValue->setText(value);
int icon = value.toInt(NULL, 16);
IconHelper::setIcon(ui->labIcon, icon, iconSize);
IconHelper::setIcon(ui->btnIcon, icon, iconSize);
//万能大法直接从指定标识获取图片文件
QPixmap pix = IconHelper::getPixmap("#753775", icon, iconSize, iconSize, iconSize);
ui->labImage->setPixmap(pix);
//设置图标 以下方法二选一都可以
//ui->btnImage->setIcon(QIcon(pix));
IconHelper::setPixmap(ui->btnImage, "#FD8B28", icon, iconSize, iconSize, iconSize);
//取出对应图形字体类
QFont font = IconHelper::getIconFontAwesome();
if (ui->rbtnFontAliBaBa->isChecked()) {
font = IconHelper::getIconFontAliBaBa();
} else if (ui->rbtnFontAwesome6->isChecked()) {
font = IconHelper::getIconFontAwesome6();
} else if (ui->rbtnFontWeather->isChecked()) {
font = IconHelper::getIconFontWeather();
}
//直接设置图标+文本到按钮
font.setPixelSize(15);
ui->btnTest1->setFont(font);
ui->btnTest1->setText(QString("%1 测试图标").arg((QChar)icon));
//分别设置图标+文本到按钮
ui->btnTest2->setIcon(QIcon(pix));
}
}
return QWidget::eventFilter(watched, event);
}
void frmIconHelper::initForm()
{
iconSize = 40;
//图标对应图形字体值
icons << 0xf2ba << 0xf002 << 0xf013 << 0xf021;
//设置样式表
QStringList qss;
qss << QString("#labIcon{color:#32B9CF;}");
qss << QString("#btnIcon{color:#C13256;}");
qss << QString("#labValue,#labCount,#labInfo1,#labInfo2,#labInfo3,#labInfo4{font-weight:bold;font-size:20px;}");
qss << QString("QWidget#widget1 QAbstractButton{min-height:%1px;max-height:%1px;}").arg(35);
qss << QString("QWidget#widget2 QAbstractButton{min-height:%1px;max-height:%1px;}").arg(35);
qss << QString("QWidget#widget3 QAbstractButton{min-height:%1px;max-height:%1px;}").arg(70);
qss << QString("QWidget#widget4 QAbstractButton{min-height:%1px;max-height:%1px;}").arg(70);
qss << QString("QWidget#widget5 QAbstractButton{min-width:%1px;max-width:%1px;}").arg(90);
qss << QString("QWidget#widget6 QAbstractButton{min-width:%1px;max-width:%1px;}").arg(90);
this->setStyleSheet(qss.join(""));
//设置图形字体
IconHelper::setIcon(ui->labIcon, 0xf067, iconSize);
IconHelper::setIcon(ui->btnIcon, 0xf067, iconSize);
QPixmap pix = IconHelper::getPixmap("#753775", 0xf067, iconSize, iconSize, iconSize);
ui->labImage->setPixmap(pix);
ui->btnImage->setIconSize(QSize(iconSize, iconSize));
ui->btnImage->setIcon(QIcon(pix));
//关联单选框切换
QList<QRadioButton *> rbtns = ui->widgetRight->findChildren<QRadioButton *>();
foreach (QRadioButton *rbtn, rbtns) {
connect(rbtn, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
}
ui->tabWidget->setCurrentIndex(0);
}
void frmIconHelper::initPanel()
{
//清空原有对象
qDeleteAll(labs);
labs.clear();
//选择不同的图形字体
int start = 0xf000;
int end = 0xf2e0;
QFont iconFont = IconHelper::getIconFontAwesome();
IconHelper::setIconFontIndex(-1);
if (ui->rbtnFontAliBaBa->isChecked()) {
start = 0xe500;
end = 0xec00;
iconFont = IconHelper::getIconFontAliBaBa();
} else if (ui->rbtnFontAwesome6->isChecked()) {
start = 0xe000;
end = 0xf8ff;
iconFont = IconHelper::getIconFontAwesome6();
IconHelper::setIconFontIndex(2);
} else if (ui->rbtnFontWeather->isChecked()) {
start = 0xe900;
end = 0xe9cf;
iconFont = IconHelper::getIconFontWeather();
IconHelper::setIconFontIndex(3);
}
//设置字体大小
iconFont.setPixelSize(15);
//加载图形字体面板
QStringList list;
for (int icon = start; icon <= end; icon++) {
//中间有一段是空的,可以自行屏蔽下面这段代码查看这段空的值对应的文字
if (ui->rbtnFontAliBaBa->isChecked()) {
if (icon >= 0xe76c && icon <= 0xe8f8) {
continue;
}
} else if (ui->rbtnFontAwesome6->isChecked()) {
if (icon >= 0xe33d && icon <= 0xefff) {
continue;
}
}
QString tip = "0x" + QString::number(icon, 16);
if (!checkIcon(icon)) {
list << tip;
continue;
}
QLabel *lab = new QLabel;
lab->installEventFilter(this);
lab->setAlignment(Qt::AlignCenter);
lab->setFont(iconFont);
lab->setText((QChar)icon);
lab->setToolTip(tip);
lab->setMinimumSize(30, 30);
labs << lab;
}
//qDebug() << "no text font" << list.count() << list;
ui->widgetFontPanel->setAutoWidth(true);
ui->widgetFontPanel->setMargin(3);
ui->widgetFontPanel->setSpace(3);
ui->widgetFontPanel->setColumnCount(18);
ui->widgetFontPanel->setWidgets(labs);
//设置图形字体面板鼠标悬停时的效果
QString qss = QString("QLabel:hover,QLabel:focus{color:%1;border:1px solid %1;}").arg("#00BB9E");
ui->widgetFontPanel->setStyleSheet(qss);
int count = end - start + 1;
ui->labCount->setText(QString("%1/%2").arg(labs.count()).arg(count));
}
bool frmIconHelper::checkIcon(int icon)
{
//从图形字体对应值生成一个指定颜色的图片
QPixmap pix = IconHelper::getPixmap("#FF0000", icon, 120, 120, 120);
QImage img = pix.toImage();
int width = img.width();
int height = img.height();
//过滤不存在的图形字体
//对该图片逐个扫描像素点,都是空白则意味着当前图形字体不存在
for (int i = 0; i < height; ++i) {
uchar *lineByte = img.scanLine(i);
for (int j = 0; j < width; j++) {
uchar tp = lineByte[j];
if (tp > 0x00) {
return true;
}
}
}
//保存下图片看下
//QString fileName = QString("%1/icon/%2.jpg").arg(qApp->applicationDirPath()).arg(icon);
//pix.save(fileName, "jpg");
return false;
}
void frmIconHelper::toggled(bool checked)
{
//单选框按下后自动重新加载对应的图形字体
if (checked) {
initPanel();
}
}
void frmIconHelper::initBtn(QButtonGroup *btnGroup, bool textBesideIcon)
{
QList<QAbstractButton *> btns = btnGroup->buttons();
foreach (QAbstractButton *btn, btns) {
QToolButton *b = (QToolButton *)btn;
//关联按钮单击事件
connect(b, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
b->setCheckable(true);
b->setToolButtonStyle(textBesideIcon ? Qt::ToolButtonTextBesideIcon : Qt::ToolButtonTextUnderIcon);
}
}
void frmIconHelper::btnClicked()
{
QAbstractButton *btn = (QAbstractButton *)sender();
QString objName = btn->parent()->objectName();
if (objName == "widget1") {
ui->labInfo1->setText(btn->text());
} else if (objName == "widget2") {
ui->labInfo2->setText(btn->text());
} else if (objName == "widget3") {
ui->labInfo3->setText(btn->text());
} else if (objName == "widget4") {
ui->labInfo4->setText(btn->text());
}
}
void frmIconHelper::initWidget1()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn11);
btnGroup->addButton(ui->btn12);
btnGroup->addButton(ui->btn13);
btnGroup->addButton(ui->btn14);
//设置按钮可选中以及图标样式
initBtn(btnGroup, true);
//设置弱属性以便应用样式
ui->widget1->setProperty("flag", "left");
IconHelper::StyleColor styleColor;
styleColor.defaultBorder = true;
styleColor.position = "left";
styleColor.iconSize = 18;
styleColor.iconWidth = 30;
styleColor.iconHeight = 25;
styleColor.borderWidth = 4;
IconHelper::setStyle(ui->widget1, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn11->click();
}
void frmIconHelper::initWidget2()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn21);
btnGroup->addButton(ui->btn22);
btnGroup->addButton(ui->btn23);
btnGroup->addButton(ui->btn24);
//设置按钮可选中以及图标样式
initBtn(btnGroup, true);
//设置弱属性以便应用样式
ui->widget2->setProperty("flag", "right");
IconHelper::StyleColor styleColor;
styleColor.defaultBorder = true;
styleColor.position = "right";
styleColor.iconSize = 18;
styleColor.iconWidth = 25;
styleColor.iconHeight = 20;
styleColor.borderWidth = 4;
styleColor.borderColor = "#32B9CF";
styleColor.setColor("#187294", "#B6D7E3", "#145C75", "#F0F0F0");
IconHelper::setStyle(ui->widget2, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn22->click();
}
void frmIconHelper::initWidget3()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn31);
btnGroup->addButton(ui->btn32);
btnGroup->addButton(ui->btn33);
btnGroup->addButton(ui->btn34);
//设置按钮可选中以及图标样式
initBtn(btnGroup, false);
//设置弱属性以便应用样式
ui->widget3->setProperty("flag", "left");
IconHelper::StyleColor styleColor;
styleColor.position = "left";
styleColor.iconSize = 30;
styleColor.iconWidth = 40;
styleColor.iconHeight = 40;
styleColor.borderWidth = 3;
styleColor.borderColor = "#609EE9";
IconHelper::setStyle(ui->widget3, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn33->click();
}
void frmIconHelper::initWidget4()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn41);
btnGroup->addButton(ui->btn42);
btnGroup->addButton(ui->btn43);
btnGroup->addButton(ui->btn44);
//设置按钮可选中以及图标样式
initBtn(btnGroup, false);
//设置弱属性以便应用样式
ui->widget4->setProperty("flag", "right");
IconHelper::StyleColor styleColor;
styleColor.position = "right";
styleColor.iconSize = 30;
styleColor.iconWidth = 40;
styleColor.iconHeight = 40;
styleColor.borderWidth = 3;
styleColor.borderColor = "#F7AE13";
styleColor.setColor("#FCDC97", "#54626F", "#FFF0BC", "#54626F");
IconHelper::setStyle(ui->widget4, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn44->click();
}
void frmIconHelper::initWidget5()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn51);
btnGroup->addButton(ui->btn52);
btnGroup->addButton(ui->btn53);
btnGroup->addButton(ui->btn54);
//设置按钮可选中以及图标样式
initBtn(btnGroup, false);
//设置弱属性以便应用样式
ui->widget5->setProperty("flag", "top");
//设置整体按钮组样式
IconHelper::StyleColor styleColor;
styleColor.defaultBorder = true;
styleColor.position = "top";
styleColor.iconSize = 25;
styleColor.iconWidth = 25;
styleColor.iconHeight = 25;
styleColor.borderWidth = 3;
IconHelper::setStyle(ui->widget5, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn51->click();
}
void frmIconHelper::initWidget6()
{
//加入按钮组自动互斥
QButtonGroup *btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->btn61);
btnGroup->addButton(ui->btn62);
btnGroup->addButton(ui->btn63);
btnGroup->addButton(ui->btn64);
//设置按钮可选中以及图标样式
initBtn(btnGroup, false);
//设置弱属性以便应用样式
ui->widget6->setProperty("flag", "bottom");
//设置整体按钮组样式
IconHelper::StyleColor styleColor;
styleColor.defaultBorder = true;
styleColor.position = "bottom";
styleColor.iconSize = 25;
styleColor.iconWidth = 25;
styleColor.iconHeight = 25;
styleColor.borderWidth = 3;
styleColor.borderColor = "#A279C5";
styleColor.setColor("#292929", "#B6D7E3", "#10689A", "#F0F0F0");
IconHelper::setStyle(ui->widget6, btnGroup->buttons(), icons, styleColor);
//默认选中某个按钮
ui->btn63->click();
}