qt_demoe/control/navbutton/iconhelper.cpp

391 lines
14 KiB
C++
Raw Normal View History

2021-05-15 11:10:30 +00:00
#include "iconhelper.h"
2021-06-08 07:46:18 +00:00
IconHelper *IconHelper::iconFontAliBaBa = 0;
IconHelper *IconHelper::iconFontAwesome = 0;
2022-01-22 12:01:44 +00:00
IconHelper *IconHelper::iconFontAwesome6 = 0;
2021-09-19 03:44:22 +00:00
IconHelper *IconHelper::iconFontWeather = 0;
int IconHelper::iconFontIndex = -1;
2021-06-08 07:46:18 +00:00
void IconHelper::initFont()
2021-05-15 11:10:30 +00:00
{
2021-06-08 07:46:18 +00:00
static bool isInit = false;
if (!isInit) {
isInit = true;
if (iconFontAliBaBa == 0) {
2021-11-03 06:13:56 +00:00
iconFontAliBaBa = new IconHelper(":/font/iconfont.ttf", "iconfont");
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
if (iconFontAwesome == 0) {
2021-11-03 06:13:56 +00:00
iconFontAwesome = new IconHelper(":/font/fontawesome-webfont.ttf", "FontAwesome");
2021-06-08 07:46:18 +00:00
}
2022-01-22 12:01:44 +00:00
if (iconFontAwesome6 == 0) {
iconFontAwesome6 = new IconHelper(":/font/fa-regular-400.ttf", "Font Awesome 6 Pro Regular");
}
2021-09-19 03:44:22 +00:00
if (iconFontWeather == 0) {
2021-11-03 06:13:56 +00:00
iconFontWeather = new IconHelper(":/font/pe-icon-set-weather.ttf", "pe-icon-set-weather");
2021-09-19 03:44:22 +00:00
}
2021-06-08 07:46:18 +00:00
}
}
2022-01-22 12:01:44 +00:00
void IconHelper::setIconFontIndex(int index)
{
iconFontIndex = index;
}
2021-09-19 03:44:22 +00:00
QFont IconHelper::getIconFontAliBaBa()
2021-06-08 07:46:18 +00:00
{
initFont();
2021-09-19 03:44:22 +00:00
return iconFontAliBaBa->getIconFont();
}
2021-06-08 07:46:18 +00:00
2021-09-19 03:44:22 +00:00
QFont IconHelper::getIconFontAwesome()
{
initFont();
return iconFontAwesome->getIconFont();
2021-06-08 07:46:18 +00:00
}
2022-01-22 12:01:44 +00:00
QFont IconHelper::getIconFontAwesome6()
{
initFont();
return iconFontAwesome6->getIconFont();
}
2021-09-19 03:44:22 +00:00
QFont IconHelper::getIconFontWeather()
2021-06-08 07:46:18 +00:00
{
initFont();
2021-09-19 03:44:22 +00:00
return iconFontWeather->getIconFont();
}
2021-06-08 07:46:18 +00:00
2021-09-19 03:44:22 +00:00
IconHelper *IconHelper::getIconHelper(int icon)
{
initFont();
//指定了字体索引则取对应索引的字体类
//没指定则自动根据不同的字体的值选择对应的类
//由于部分值范围冲突所以可以指定索引来取
//fontawesome 0xf000-0xf2e0
2022-01-22 12:01:44 +00:00
//fontawesome6 0xe000-0xe33d 0xf000-0xf8ff
2022-11-24 12:14:44 +00:00
//iconfont 0xe501-0xe793 0xe8d5-0xea5d 0xeb00-0xec00
2021-09-19 03:44:22 +00:00
//weather 0xe900-0xe9cf
IconHelper *iconHelper = iconFontAwesome;
if (iconFontIndex < 0) {
2022-11-24 12:14:44 +00:00
if ((icon >= 0xe501 && icon <= 0xe793) || (icon >= 0xe8d5 && icon <= 0xea5d) || (icon >= 0xeb00 && icon <= 0xec00)) {
2021-09-19 03:44:22 +00:00
iconHelper = iconFontAliBaBa;
}
} else if (iconFontIndex == 0) {
iconHelper = iconFontAliBaBa;
} else if (iconFontIndex == 1) {
iconHelper = iconFontAwesome;
} else if (iconFontIndex == 2) {
2022-01-22 12:01:44 +00:00
iconHelper = iconFontAwesome6;
} else if (iconFontIndex == 3) {
2021-09-19 03:44:22 +00:00
iconHelper = iconFontWeather;
2021-06-08 07:46:18 +00:00
}
2021-09-19 03:44:22 +00:00
return iconHelper;
}
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
{
getIconHelper(icon)->setIcon1(lab, icon, size);
}
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
{
getIconHelper(icon)->setIcon1(btn, icon, size);
2021-06-08 07:46:18 +00:00
}
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
2021-09-19 03:44:22 +00:00
getIconHelper(icon)->setPixmap1(btn, color, icon, size, width, height, flags);
2021-06-08 07:46:18 +00:00
}
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
2021-09-19 03:44:22 +00:00
return getIconHelper(icon)->getPixmap1(color, icon, size, width, height, flags);
2021-06-08 07:46:18 +00:00
}
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
2021-09-19 03:44:22 +00:00
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
2021-06-08 07:46:18 +00:00
}
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
2021-09-19 03:44:22 +00:00
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
2021-06-08 07:46:18 +00:00
}
2021-05-15 11:10:30 +00:00
2021-06-08 07:46:18 +00:00
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
2021-09-19 03:44:22 +00:00
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
2021-05-15 11:10:30 +00:00
}
2021-09-19 03:44:22 +00:00
2021-06-08 07:46:18 +00:00
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
2021-05-15 11:10:30 +00:00
{
//判断图形字体是否存在,不存在则加入
2024-02-26 02:34:39 +00:00
//这里暂时限制在同一个项目中只加载一次字体文件
2021-05-15 11:10:30 +00:00
QFontDatabase fontDb;
2024-02-26 02:34:39 +00:00
bool exist = false;//fontDb.families().contains(fontName);
if (!exist && QFile(fontFile).exists()) {
2021-06-08 07:46:18 +00:00
int fontId = fontDb.addApplicationFont(fontFile);
QStringList listName = fontDb.applicationFontFamilies(fontId);
2023-03-20 07:04:23 +00:00
if (listName.count() == 0) {
2021-06-08 07:46:18 +00:00
qDebug() << QString("load %1 error").arg(fontName);
2021-05-15 11:10:30 +00:00
}
}
2021-09-19 03:44:22 +00:00
//再次判断是否包含字体名称防止加载失败
2021-06-08 07:46:18 +00:00
if (fontDb.families().contains(fontName)) {
iconFont = QFont(fontName);
2021-05-15 11:10:30 +00:00
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
iconFont.setHintingPreference(QFont::PreferNoHinting);
#endif
}
}
2021-06-08 07:46:18 +00:00
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
{
//根据不同的
if (watched->inherits("QAbstractButton")) {
QAbstractButton *btn = (QAbstractButton *)watched;
int index = btns.indexOf(btn);
if (index >= 0) {
//不同的事件设置不同的图标,同时区分选中的和没有选中的
2023-12-15 04:55:21 +00:00
int type = event->type();
2021-06-08 07:46:18 +00:00
if (btn->isChecked()) {
2023-12-15 04:55:21 +00:00
if (type == QEvent::MouseButtonPress) {
2021-06-08 07:46:18 +00:00
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (mouseEvent->button() == Qt::LeftButton) {
btn->setIcon(QIcon(pixChecked.at(index)));
}
2023-12-15 04:55:21 +00:00
} else if (type == QEvent::Enter) {
2021-06-08 07:46:18 +00:00
btn->setIcon(QIcon(pixChecked.at(index)));
2023-12-15 04:55:21 +00:00
} else if (type == QEvent::Leave) {
2021-06-08 07:46:18 +00:00
btn->setIcon(QIcon(pixChecked.at(index)));
}
} else {
2023-12-15 04:55:21 +00:00
if (type == QEvent::MouseButtonPress) {
2021-06-08 07:46:18 +00:00
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (mouseEvent->button() == Qt::LeftButton) {
btn->setIcon(QIcon(pixPressed.at(index)));
}
2023-12-15 04:55:21 +00:00
} else if (type == QEvent::Enter) {
2021-06-08 07:46:18 +00:00
btn->setIcon(QIcon(pixHover.at(index)));
2023-12-15 04:55:21 +00:00
} else if (type == QEvent::Leave) {
2021-06-08 07:46:18 +00:00
btn->setIcon(QIcon(pixNormal.at(index)));
}
}
}
}
return QObject::eventFilter(watched, event);
}
void IconHelper::toggled(bool checked)
2021-05-15 11:10:30 +00:00
{
2021-06-08 07:46:18 +00:00
//选中和不选中设置不同的图标
QAbstractButton *btn = (QAbstractButton *)sender();
int index = btns.indexOf(btn);
if (checked) {
btn->setIcon(QIcon(pixChecked.at(index)));
} else {
btn->setIcon(QIcon(pixNormal.at(index)));
}
2021-05-15 11:10:30 +00:00
}
2021-09-19 03:44:22 +00:00
QFont IconHelper::getIconFont()
{
return this->iconFont;
}
2021-06-08 07:46:18 +00:00
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
2021-05-15 11:10:30 +00:00
{
iconFont.setPixelSize(size);
lab->setFont(iconFont);
2021-05-30 07:59:42 +00:00
lab->setText((QChar)icon);
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
void IconHelper::setIcon1(QAbstractButton *btn, int icon, quint32 size)
2021-05-15 11:10:30 +00:00
{
iconFont.setPixelSize(size);
btn->setFont(iconFont);
2021-05-30 07:59:42 +00:00
btn->setText((QChar)icon);
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
void IconHelper::setPixmap1(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
2021-05-15 11:10:30 +00:00
{
2021-06-08 07:46:18 +00:00
btn->setIcon(getPixmap1(color, icon, size, width, height, flags));
}
QPixmap IconHelper::getPixmap1(const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
//主动绘制图形字体到图片
QPixmap pix(width, height);
2021-05-15 11:10:30 +00:00
pix.fill(Qt::transparent);
QPainter painter;
painter.begin(&pix);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.setPen(color);
iconFont.setPixelSize(size);
painter.setFont(iconFont);
2021-05-30 07:59:42 +00:00
painter.drawText(pix.rect(), flags, (QChar)icon);
2021-05-15 11:10:30 +00:00
painter.end();
return pix;
}
2021-06-08 07:46:18 +00:00
void IconHelper::setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
2021-05-15 11:10:30 +00:00
{
2021-06-08 07:46:18 +00:00
QList<QAbstractButton *> list;
foreach (QPushButton *btn, btns) {
list << btn;
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
setStyle(widget, list, icons, styleColor);
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
void IconHelper::setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
2021-05-15 11:10:30 +00:00
{
2021-06-08 07:46:18 +00:00
QList<QAbstractButton *> list;
foreach (QToolButton *btn, btns) {
list << btn;
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
setStyle(widget, list, icons, styleColor);
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
2021-05-15 11:10:30 +00:00
{
2023-03-20 07:04:23 +00:00
int btnCount = btns.count();
int iconCount = icons.count();
2021-06-08 07:46:18 +00:00
if (btnCount <= 0 || iconCount <= 0 || btnCount != iconCount) {
2021-05-15 11:10:30 +00:00
return;
}
2021-06-08 07:46:18 +00:00
QString position = styleColor.position;
2023-08-16 07:41:00 +00:00
quint32 btnWidth = styleColor.btnWidth;
quint32 btnHeight = styleColor.btnHeight;
2021-06-08 07:46:18 +00:00
quint32 iconSize = styleColor.iconSize;
quint32 iconWidth = styleColor.iconWidth;
quint32 iconHeight = styleColor.iconHeight;
quint32 borderWidth = styleColor.borderWidth;
2021-05-15 11:10:30 +00:00
2021-06-08 07:46:18 +00:00
//根据不同的位置计算边框
2021-05-15 11:10:30 +00:00
QString strBorder;
2021-06-08 07:46:18 +00:00
if (position == "top") {
2021-05-15 11:10:30 +00:00
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
2021-06-08 07:46:18 +00:00
} else if (position == "right") {
2021-05-15 11:10:30 +00:00
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
2021-06-08 07:46:18 +00:00
} else if (position == "bottom") {
2021-05-15 11:10:30 +00:00
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
2021-06-08 07:46:18 +00:00
} else if (position == "left") {
2021-05-15 11:10:30 +00:00
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
}
2021-06-08 07:46:18 +00:00
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
2021-09-19 03:44:22 +00:00
//如果图标在文字上面而设置的边框是 top bottom 也需要启用加深边框
2021-05-15 11:10:30 +00:00
QStringList qss;
2021-09-19 03:44:22 +00:00
if (styleColor.defaultBorder) {
2021-06-08 07:46:18 +00:00
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
} else {
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
.arg(position).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
}
2021-05-15 11:10:30 +00:00
2021-06-08 07:46:18 +00:00
//悬停+按下+选中
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor);
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor);
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor);
//窗体背景颜色+按钮背景颜色
qss << QString("QWidget#%1{background:%2;}")
.arg(widget->objectName()).arg(styleColor.normalBgColor);
qss << QString("QWidget>QAbstractButton{border-width:0px;background-color:%1;color:%2;}")
.arg(styleColor.normalBgColor).arg(styleColor.normalTextColor);
qss << QString("QWidget>QAbstractButton:hover{background-color:%1;color:%2;}")
.arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor);
qss << QString("QWidget>QAbstractButton:pressed{background-color:%1;color:%2;}")
.arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor);
qss << QString("QWidget>QAbstractButton:checked{background-color:%1;color:%2;}")
.arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor);
2023-08-16 07:41:00 +00:00
//按钮宽度高度
if (btnWidth > 0) {
qss << QString("QWidget>QAbstractButton{min-width:%1px;}").arg(btnWidth);
}
if (btnHeight > 0) {
qss << QString("QWidget>QAbstractButton{min-height:%1px;}").arg(btnHeight);
}
2021-06-08 07:46:18 +00:00
//设置样式表
2021-05-15 11:10:30 +00:00
widget->setStyleSheet(qss.join(""));
2021-06-08 07:46:18 +00:00
//可能会重复调用设置所以先要移除上一次的
for (int i = 0; i < btnCount; ++i) {
2023-03-20 07:04:23 +00:00
for (int j = 0; j < this->btns.count(); j++) {
2021-05-15 11:10:30 +00:00
if (this->btns.at(j) == btns.at(i)) {
2021-06-08 07:46:18 +00:00
disconnect(btns.at(i), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
2021-05-15 11:10:30 +00:00
this->btns.at(j)->removeEventFilter(this);
this->btns.removeAt(j);
this->pixNormal.removeAt(j);
this->pixHover.removeAt(j);
this->pixPressed.removeAt(j);
this->pixChecked.removeAt(j);
break;
}
}
}
//存储对应按钮对象,方便鼠标移上去的时候切换图片
2021-06-08 07:46:18 +00:00
int checkedIndex = -1;
for (int i = 0; i < btnCount; ++i) {
2021-05-30 07:59:42 +00:00
int icon = icons.at(i);
2021-06-08 07:46:18 +00:00
QPixmap pixNormal = getPixmap1(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixHover = getPixmap1(styleColor.hoverTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixPressed = getPixmap1(styleColor.pressedTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixChecked = getPixmap1(styleColor.checkedTextColor, icon, iconSize, iconWidth, iconHeight);
//记住最后选中的按钮
QAbstractButton *btn = btns.at(i);
if (btn->isChecked()) {
checkedIndex = i;
}
2021-05-15 11:10:30 +00:00
btn->setIcon(QIcon(pixNormal));
btn->setIconSize(QSize(iconWidth, iconHeight));
btn->installEventFilter(this);
2021-06-08 07:46:18 +00:00
connect(btn, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
2021-05-15 11:10:30 +00:00
2021-06-08 07:46:18 +00:00
this->btns << btn;
this->pixNormal << pixNormal;
this->pixHover << pixHover;
this->pixPressed << pixPressed;
this->pixChecked << pixChecked;
2021-05-15 11:10:30 +00:00
}
2021-06-08 07:46:18 +00:00
//主动触发一下选中的按钮
if (checkedIndex >= 0) {
QMetaObject::invokeMethod(btns.at(checkedIndex), "toggled", Q_ARG(bool, true));
2021-05-15 11:10:30 +00:00
}
}