更新图形字体代码
parent
cfa339e16f
commit
b594e6e67c
|
@ -1,5 +1,4 @@
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
$$PWD/iconfont.h \
|
|
||||||
$$PWD/iconhelper.h \
|
$$PWD/iconhelper.h \
|
||||||
$$PWD/quiconfig.h \
|
$$PWD/quiconfig.h \
|
||||||
$$PWD/quidateselect.h \
|
$$PWD/quidateselect.h \
|
||||||
|
@ -12,7 +11,6 @@ HEADERS += \
|
||||||
$$PWD/quiwidget.h
|
$$PWD/quiwidget.h
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/iconfont.cpp \
|
|
||||||
$$PWD/iconhelper.cpp \
|
$$PWD/iconhelper.cpp \
|
||||||
$$PWD/quiconfig.cpp \
|
$$PWD/quiconfig.cpp \
|
||||||
$$PWD/quidateselect.cpp \
|
$$PWD/quidateselect.cpp \
|
||||||
|
|
|
@ -1,242 +0,0 @@
|
||||||
#include "iconfont.h"
|
|
||||||
|
|
||||||
QScopedPointer<IconFont> IconFont::self;
|
|
||||||
IconFont *IconFont::Instance()
|
|
||||||
{
|
|
||||||
if (self.isNull()) {
|
|
||||||
static QMutex mutex;
|
|
||||||
QMutexLocker locker(&mutex);
|
|
||||||
if (self.isNull()) {
|
|
||||||
self.reset(new IconFont);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return self.data();
|
|
||||||
}
|
|
||||||
|
|
||||||
IconFont::IconFont(QObject *parent) : QObject(parent)
|
|
||||||
{
|
|
||||||
//判断图形字体是否存在,不存在则加入
|
|
||||||
QFontDatabase fontDb;
|
|
||||||
if (!fontDb.families().contains("iconfont")) {
|
|
||||||
int fontId = fontDb.addApplicationFont(":/image/iconfont.ttf");
|
|
||||||
QStringList fontName = fontDb.applicationFontFamilies(fontId);
|
|
||||||
if (fontName.count() == 0) {
|
|
||||||
qDebug() << "load iconfont.ttf error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fontDb.families().contains("iconfont")) {
|
|
||||||
iconFont = QFont("iconfont");
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
|
||||||
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconFont::setIcon(QLabel *lab, int icon, quint32 size)
|
|
||||||
{
|
|
||||||
iconFont.setPixelSize(size);
|
|
||||||
lab->setFont(iconFont);
|
|
||||||
lab->setText((QChar)icon);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconFont::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
|
||||||
{
|
|
||||||
iconFont.setPixelSize(size);
|
|
||||||
btn->setFont(iconFont);
|
|
||||||
btn->setText((QChar)icon);
|
|
||||||
}
|
|
||||||
|
|
||||||
QPixmap IconFont::getPixmap(const QColor &color, int icon, quint32 size,
|
|
||||||
quint32 pixWidth, quint32 pixHeight, int flags)
|
|
||||||
{
|
|
||||||
QPixmap pix(pixWidth, pixHeight);
|
|
||||||
pix.fill(Qt::transparent);
|
|
||||||
|
|
||||||
QPainter painter;
|
|
||||||
painter.begin(&pix);
|
|
||||||
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
|
||||||
painter.setPen(color);
|
|
||||||
|
|
||||||
iconFont.setPixelSize(size);
|
|
||||||
painter.setFont(iconFont);
|
|
||||||
painter.drawText(pix.rect(), flags, (QChar)icon);
|
|
||||||
painter.end();
|
|
||||||
|
|
||||||
return pix;
|
|
||||||
}
|
|
||||||
|
|
||||||
QPixmap IconFont::getPixmap(QToolButton *btn, bool normal)
|
|
||||||
{
|
|
||||||
QPixmap pix;
|
|
||||||
int index = btns.indexOf(btn);
|
|
||||||
|
|
||||||
if (index >= 0) {
|
|
||||||
if (normal) {
|
|
||||||
pix = pixNormal.at(index);
|
|
||||||
} else {
|
|
||||||
pix = pixDark.at(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pix;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconFont::setStyle(QWidget *widget, const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding:%1px %2px %2px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding:%2px %1px %2px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding:%2px %2px %1px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding:%2px %2px %2px %1px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList qss;
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconFont::setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding:%1px %2px %2px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding:%2px %1px %2px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding:%2px %2px %1px %2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding:%2px %2px %2px %1px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
|
||||||
QStringList qss;
|
|
||||||
if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(normalBgColor).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
} else {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
}
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(normalBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget>QToolButton{border-width:0px;}"));
|
|
||||||
qss.append(QString("QWidget>QToolButton{background-color:%1;color:%2;}")
|
|
||||||
.arg(normalBgColor).arg(normalTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:hover,QWidget>QToolButton:pressed,QWidget>QToolButton:checked{background-color:%1;color:%2;}")
|
|
||||||
.arg(darkBgColor).arg(darkTextColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
btns.at(i)->setIcon(QIcon(pixNormal));
|
|
||||||
btns.at(i)->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btns.at(i)->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btns.at(i));
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconFont::setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList qss;
|
|
||||||
qss.append(QString("QFrame>QToolButton{border-style:none;border-width:0px;}"));
|
|
||||||
qss.append(QString("QFrame>QToolButton{background-color:%1;color:%2;}")
|
|
||||||
.arg(normalBgColor).arg(normalTextColor));
|
|
||||||
qss.append(QString("QFrame>QToolButton:hover,QFrame>QToolButton:pressed,QFrame>QToolButton:checked{background-color:%1;color:%2;}")
|
|
||||||
.arg(darkBgColor).arg(darkTextColor));
|
|
||||||
|
|
||||||
frame->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
btns.at(i)->setIcon(QIcon(pixNormal));
|
|
||||||
btns.at(i)->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btns.at(i)->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btns.at(i));
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IconFont::eventFilter(QObject *watched, QEvent *event)
|
|
||||||
{
|
|
||||||
if (watched->inherits("QToolButton")) {
|
|
||||||
QToolButton *btn = (QToolButton *)watched;
|
|
||||||
int index = btns.indexOf(btn);
|
|
||||||
if (index >= 0) {
|
|
||||||
if (event->type() == QEvent::Enter) {
|
|
||||||
btn->setIcon(QIcon(pixDark.at(index)));
|
|
||||||
} else if (event->type() == QEvent::Leave) {
|
|
||||||
if (btn->isChecked()) {
|
|
||||||
btn->setIcon(QIcon(pixDark.at(index)));
|
|
||||||
} else {
|
|
||||||
btn->setIcon(QIcon(pixNormal.at(index)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return QObject::eventFilter(watched, event);
|
|
||||||
}
|
|
|
@ -1,68 +0,0 @@
|
||||||
#ifndef ICONFONT_H
|
|
||||||
#define ICONFONT_H
|
|
||||||
|
|
||||||
#include <QtCore>
|
|
||||||
#include <QtGui>
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
|
||||||
#include <QtWidgets>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef quc
|
|
||||||
class Q_DECL_EXPORT IconFont : public QObject
|
|
||||||
#else
|
|
||||||
class IconFont : public QObject
|
|
||||||
#endif
|
|
||||||
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
static IconFont *Instance();
|
|
||||||
explicit IconFont(QObject *parent = 0);
|
|
||||||
|
|
||||||
void setIcon(QLabel *lab, int icon, quint32 size = 12);
|
|
||||||
void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
|
|
||||||
QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
|
|
||||||
quint32 pixWidth = 15, quint32 pixHeight = 15,
|
|
||||||
int flags = Qt::AlignCenter);
|
|
||||||
|
|
||||||
//根据按钮获取该按钮对应的图标
|
|
||||||
QPixmap getPixmap(QToolButton *btn, bool normal);
|
|
||||||
|
|
||||||
//指定导航面板样式,不带图标
|
|
||||||
static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
//指定导航面板样式,带图标和效果切换
|
|
||||||
void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
//指定导航按钮样式,带图标和效果切换
|
|
||||||
void setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &normalBgColor = "#2FC5A2",
|
|
||||||
const QString &darkBgColor = "#3EA7E9",
|
|
||||||
const QString &normalTextColor = "#EEEEEE",
|
|
||||||
const QString &darkTextColor = "#FFFFFF");
|
|
||||||
|
|
||||||
protected:
|
|
||||||
bool eventFilter(QObject *watched, QEvent *event);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static QScopedPointer<IconFont> self;
|
|
||||||
QFont iconFont; //图形字体
|
|
||||||
QList<QToolButton *> btns; //按钮队列
|
|
||||||
QList<QPixmap> pixNormal; //正常图片队列
|
|
||||||
QList<QPixmap> pixDark; //加深图片队列
|
|
||||||
};
|
|
||||||
#endif // ICONFONT_H
|
|
|
@ -1,62 +1,209 @@
|
||||||
#include "iconhelper.h"
|
#include "iconhelper.h"
|
||||||
|
|
||||||
QScopedPointer<IconHelper> IconHelper::self;
|
IconHelper *IconHelper::iconFontAliBaBa = 0;
|
||||||
IconHelper *IconHelper::Instance()
|
IconHelper *IconHelper::iconFontAwesome = 0;
|
||||||
|
void IconHelper::initFont()
|
||||||
{
|
{
|
||||||
if (self.isNull()) {
|
static bool isInit = false;
|
||||||
static QMutex mutex;
|
if (!isInit) {
|
||||||
QMutexLocker locker(&mutex);
|
isInit = true;
|
||||||
if (self.isNull()) {
|
if (iconFontAliBaBa == 0) {
|
||||||
self.reset(new IconHelper);
|
iconFontAliBaBa = new IconHelper(":/image/iconfont.ttf", "iconfont");
|
||||||
|
}
|
||||||
|
if (iconFontAwesome == 0) {
|
||||||
|
iconFontAwesome = new IconHelper(":/image/fontawesome-webfont.ttf", "FontAwesome");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.data();
|
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setIcon1(lab, icon, size);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setIcon1(lab, icon, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IconHelper::IconHelper(QObject *parent) : QObject(parent)
|
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setIcon1(btn, icon, size);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setIcon1(btn, icon, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||||
|
quint32 width, quint32 height, int flags)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setPixmap1(btn, color, icon, size, width, height, flags);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setPixmap1(btn, color, icon, size, width, height, flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
||||||
|
quint32 width, quint32 height, int flags)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
QPixmap pix;
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
pix = iconFontAliBaBa->getPixmap1(color, icon, size, width, height, flags);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
pix = iconFontAwesome->getPixmap1(color, icon, size, width, height, flags);
|
||||||
|
}
|
||||||
|
return pix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
//判断图形字体是否存在,不存在则加入
|
//判断图形字体是否存在,不存在则加入
|
||||||
QFontDatabase fontDb;
|
QFontDatabase fontDb;
|
||||||
if (!fontDb.families().contains("FontAwesome")) {
|
if (!fontDb.families().contains(fontName)) {
|
||||||
int fontId = fontDb.addApplicationFont(":/image/fontawesome-webfont.ttf");
|
int fontId = fontDb.addApplicationFont(fontFile);
|
||||||
QStringList fontName = fontDb.applicationFontFamilies(fontId);
|
QStringList listName = fontDb.applicationFontFamilies(fontId);
|
||||||
if (fontName.count() == 0) {
|
if (listName.count() == 0) {
|
||||||
qDebug() << "load fontawesome-webfont.ttf error";
|
qDebug() << QString("load %1 error").arg(fontName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fontDb.families().contains("FontAwesome")) {
|
if (fontDb.families().contains(fontName)) {
|
||||||
iconFont = QFont("FontAwesome");
|
iconFont = QFont(fontName);
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
||||||
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QFont IconHelper::getIconFont()
|
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
|
||||||
{
|
{
|
||||||
return this->iconFont;
|
//根据不同的
|
||||||
|
if (watched->inherits("QAbstractButton")) {
|
||||||
|
QAbstractButton *btn = (QAbstractButton *)watched;
|
||||||
|
int index = btns.indexOf(btn);
|
||||||
|
if (index >= 0) {
|
||||||
|
//不同的事件设置不同的图标,同时区分选中的和没有选中的
|
||||||
|
if (btn->isChecked()) {
|
||||||
|
if (event->type() == QEvent::MouseButtonPress) {
|
||||||
|
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||||
|
if (mouseEvent->button() == Qt::LeftButton) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
}
|
||||||
|
} else if (event->type() == QEvent::Enter) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
} else if (event->type() == QEvent::Leave) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (event->type() == QEvent::MouseButtonPress) {
|
||||||
|
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||||
|
if (mouseEvent->button() == Qt::LeftButton) {
|
||||||
|
btn->setIcon(QIcon(pixPressed.at(index)));
|
||||||
|
}
|
||||||
|
} else if (event->type() == QEvent::Enter) {
|
||||||
|
btn->setIcon(QIcon(pixHover.at(index)));
|
||||||
|
} else if (event->type() == QEvent::Leave) {
|
||||||
|
btn->setIcon(QIcon(pixNormal.at(index)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
|
return QObject::eventFilter(watched, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::toggled(bool checked)
|
||||||
|
{
|
||||||
|
//选中和不选中设置不同的图标
|
||||||
|
QAbstractButton *btn = (QAbstractButton *)sender();
|
||||||
|
int index = btns.indexOf(btn);
|
||||||
|
if (checked) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
} else {
|
||||||
|
btn->setIcon(QIcon(pixNormal.at(index)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
|
||||||
{
|
{
|
||||||
iconFont.setPixelSize(size);
|
iconFont.setPixelSize(size);
|
||||||
lab->setFont(iconFont);
|
lab->setFont(iconFont);
|
||||||
lab->setText((QChar)icon);
|
lab->setText((QChar)icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
void IconHelper::setIcon1(QAbstractButton *btn, int icon, quint32 size)
|
||||||
{
|
{
|
||||||
iconFont.setPixelSize(size);
|
iconFont.setPixelSize(size);
|
||||||
btn->setFont(iconFont);
|
btn->setFont(iconFont);
|
||||||
btn->setText((QChar)icon);
|
btn->setText((QChar)icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
void IconHelper::setPixmap1(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||||
quint32 pixWidth, quint32 pixHeight, int flags)
|
quint32 width, quint32 height, int flags)
|
||||||
{
|
{
|
||||||
QPixmap pix(pixWidth, pixHeight);
|
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);
|
||||||
pix.fill(Qt::transparent);
|
pix.fill(Qt::transparent);
|
||||||
|
|
||||||
QPainter painter;
|
QPainter painter;
|
||||||
|
@ -68,124 +215,100 @@ QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
||||||
painter.setFont(iconFont);
|
painter.setFont(iconFont);
|
||||||
painter.drawText(pix.rect(), flags, (QChar)icon);
|
painter.drawText(pix.rect(), flags, (QChar)icon);
|
||||||
painter.end();
|
painter.end();
|
||||||
|
|
||||||
return pix;
|
return pix;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(QToolButton *btn, bool normal)
|
void IconHelper::setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
{
|
{
|
||||||
QPixmap pix;
|
QList<QAbstractButton *> list;
|
||||||
int index = btns.indexOf(btn);
|
foreach (QPushButton *btn, btns) {
|
||||||
if (index >= 0) {
|
list << btn;
|
||||||
if (normal) {
|
|
||||||
pix = pixNormal.at(index);
|
|
||||||
} else {
|
|
||||||
pix = pixDark.at(index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pix;
|
setStyle(widget, list, icons, styleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(QToolButton *btn, int type)
|
void IconHelper::setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
{
|
{
|
||||||
QPixmap pix;
|
QList<QAbstractButton *> list;
|
||||||
int index = btns.indexOf(btn);
|
foreach (QToolButton *btn, btns) {
|
||||||
if (index >= 0) {
|
list << btn;
|
||||||
if (type == 0) {
|
|
||||||
pix = pixNormal.at(index);
|
|
||||||
} else if (type == 1) {
|
|
||||||
pix = pixHover.at(index);
|
|
||||||
} else if (type == 2) {
|
|
||||||
pix = pixPressed.at(index);
|
|
||||||
} else if (type == 3) {
|
|
||||||
pix = pixChecked.at(index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pix;
|
setStyle(widget, list, icons, styleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
{
|
||||||
int btnCount = btns.count();
|
int btnCount = btns.count();
|
||||||
int charCount = icons.count();
|
int iconCount = icons.count();
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
if (btnCount <= 0 || iconCount <= 0 || btnCount != iconCount) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList qss;
|
QString position = styleColor.position;
|
||||||
qss.append(QString("QFrame>QToolButton{border-style:none;border-width:0px;"
|
quint32 iconSize = styleColor.iconSize;
|
||||||
"background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor));
|
quint32 iconWidth = styleColor.iconWidth;
|
||||||
qss.append(QString("QFrame>QToolButton:hover,QFrame>QToolButton:pressed,QFrame>QToolButton:checked"
|
quint32 iconHeight = styleColor.iconHeight;
|
||||||
"{background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor));
|
quint32 borderWidth = styleColor.borderWidth;
|
||||||
|
|
||||||
frame->setStyleSheet(qss.join(""));
|
//根据不同的位置计算边框
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
|
||||||
btn->setIcon(QIcon(pixNormal));
|
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
this->pixHover.append(pixDark);
|
|
||||||
this->pixPressed.append(pixDark);
|
|
||||||
this->pixChecked.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
QString strBorder;
|
QString strBorder;
|
||||||
if (type == "top") {
|
if (position == "top") {
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "right") {
|
} else if (position == "right") {
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "bottom") {
|
} else if (position == "bottom") {
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "left") {
|
} else if (position == "left") {
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
||||||
QStringList qss;
|
QStringList qss;
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;"
|
if (styleColor.textBesideIcon) {
|
||||||
"color:%2;background:%3;}").arg(type).arg(normalTextColor).arg(normalBgColor));
|
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);
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
} else {
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
.arg(position).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::removeStyle(QList<QToolButton *> btns)
|
//悬停+按下+选中
|
||||||
{
|
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
||||||
for (int i = 0; i < btns.count(); i++) {
|
.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);
|
||||||
|
|
||||||
|
//设置样式表
|
||||||
|
widget->setStyleSheet(qss.join(""));
|
||||||
|
|
||||||
|
//可能会重复调用设置所以先要移除上一次的
|
||||||
|
for (int i = 0; i < btnCount; i++) {
|
||||||
for (int j = 0; j < this->btns.count(); j++) {
|
for (int j = 0; j < this->btns.count(); j++) {
|
||||||
if (this->btns.at(j) == btns.at(i)) {
|
if (this->btns.at(j) == btns.at(i)) {
|
||||||
|
disconnect(btns.at(i), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
|
||||||
this->btns.at(j)->removeEventFilter(this);
|
this->btns.at(j)->removeEventFilter(this);
|
||||||
this->btns.removeAt(j);
|
this->btns.removeAt(j);
|
||||||
this->pixNormal.removeAt(j);
|
this->pixNormal.removeAt(j);
|
||||||
this->pixDark.removeAt(j);
|
|
||||||
this->pixHover.removeAt(j);
|
this->pixHover.removeAt(j);
|
||||||
this->pixPressed.removeAt(j);
|
this->pixPressed.removeAt(j);
|
||||||
this->pixChecked.removeAt(j);
|
this->pixChecked.removeAt(j);
|
||||||
|
@ -193,174 +316,36 @@ void IconHelper::removeStyle(QList<QToolButton *> btns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
|
||||||
QStringList qss;
|
|
||||||
if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(normalBgColor).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
} else {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
}
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(normalBgColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton{border-width:0px;"
|
|
||||||
"background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:hover,QWidget>QToolButton:pressed,QWidget>QToolButton:checked{"
|
|
||||||
"background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
||||||
|
int checkedIndex = -1;
|
||||||
for (int i = 0; i < btnCount; i++) {
|
for (int i = 0; i < btnCount; i++) {
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
QPixmap pixNormal = getPixmap1(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, 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);
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
//记住最后选中的按钮
|
||||||
btn->setIcon(QIcon(pixNormal));
|
QAbstractButton *btn = btns.at(i);
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
this->pixHover.append(pixDark);
|
|
||||||
this->pixPressed.append(pixDark);
|
|
||||||
this->pixChecked.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
quint32 iconSize = styleColor.iconSize;
|
|
||||||
quint32 iconWidth = styleColor.iconWidth;
|
|
||||||
quint32 iconHeight = styleColor.iconHeight;
|
|
||||||
quint32 borderWidth = styleColor.borderWidth;
|
|
||||||
QString type = styleColor.type;
|
|
||||||
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
|
||||||
QStringList qss;
|
|
||||||
if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor));
|
|
||||||
} else {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor));
|
|
||||||
}
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor));
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor));
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(styleColor.normalBgColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton{border-width:0px;background-color:%1;color:%2;}").arg(styleColor.normalBgColor).arg(styleColor.normalTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:hover{background-color:%1;color:%2;}").arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:pressed{background-color:%1;color:%2;}").arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:checked{background-color:%1;color:%2;}").arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixHover = getPixmap(styleColor.hoverTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixPressed = getPixmap(styleColor.pressedTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixChecked = getPixmap(styleColor.checkedTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
|
||||||
btn->setIcon(QIcon(pixNormal));
|
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixHover);
|
|
||||||
this->pixHover.append(pixHover);
|
|
||||||
this->pixPressed.append(pixPressed);
|
|
||||||
this->pixChecked.append(pixChecked);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
|
|
||||||
{
|
|
||||||
if (watched->inherits("QToolButton")) {
|
|
||||||
QToolButton *btn = (QToolButton *)watched;
|
|
||||||
int index = btns.indexOf(btn);
|
|
||||||
if (index >= 0) {
|
|
||||||
if (event->type() == QEvent::Enter) {
|
|
||||||
btn->setIcon(QIcon(pixHover.at(index)));
|
|
||||||
} else if (event->type() == QEvent::MouseButtonPress) {
|
|
||||||
btn->setIcon(QIcon(pixPressed.at(index)));
|
|
||||||
} else if (event->type() == QEvent::Leave) {
|
|
||||||
if (btn->isChecked()) {
|
if (btn->isChecked()) {
|
||||||
btn->setIcon(QIcon(pixChecked.at(index)));
|
checkedIndex = i;
|
||||||
} else {
|
|
||||||
btn->setIcon(QIcon(pixNormal.at(index)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return QObject::eventFilter(watched, event);
|
btn->setIcon(QIcon(pixNormal));
|
||||||
|
btn->setIconSize(QSize(iconWidth, iconHeight));
|
||||||
|
btn->installEventFilter(this);
|
||||||
|
connect(btn, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
|
||||||
|
|
||||||
|
this->btns << btn;
|
||||||
|
this->pixNormal << pixNormal;
|
||||||
|
this->pixHover << pixHover;
|
||||||
|
this->pixPressed << pixPressed;
|
||||||
|
this->pixChecked << pixChecked;
|
||||||
|
}
|
||||||
|
|
||||||
|
//主动触发一下选中的按钮
|
||||||
|
if (checkedIndex >= 0) {
|
||||||
|
QMetaObject::invokeMethod(btns.at(checkedIndex), "toggled", Q_ARG(bool, true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
#ifndef ICONHELPER_H
|
#ifndef ICONHELPER_H
|
||||||
#define ICONHELPER_H
|
#define ICONHELPER_H
|
||||||
|
|
||||||
#include <QtCore>
|
|
||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
|
||||||
|
* 1. 可传入多种图形字体文件
|
||||||
|
* 2. 可设置 QLabel+QAbstractButton 图形字体
|
||||||
|
* 3. 可设置按钮图标
|
||||||
|
* 4. 可获取指定尺寸的图形字体图片
|
||||||
|
* 5. 超级导航栏样式设置,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
||||||
|
*/
|
||||||
|
|
||||||
#ifdef quc
|
#ifdef quc
|
||||||
class Q_DECL_EXPORT IconHelper : public QObject
|
class Q_DECL_EXPORT IconHelper : public QObject
|
||||||
#else
|
#else
|
||||||
|
@ -17,78 +25,38 @@ class IconHelper : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static IconHelper *Instance();
|
//样式颜色结构体
|
||||||
explicit IconHelper(QObject *parent = 0);
|
|
||||||
|
|
||||||
//获取图形字体
|
|
||||||
QFont getIconFont();
|
|
||||||
|
|
||||||
//设置图形字体到标签
|
|
||||||
void setIcon(QLabel *lab, int icon, quint32 size = 12);
|
|
||||||
//设置图形字体到按钮
|
|
||||||
void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
|
|
||||||
|
|
||||||
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
|
|
||||||
QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
|
|
||||||
quint32 pixWidth = 15, quint32 pixHeight = 15,
|
|
||||||
int flags = Qt::AlignCenter);
|
|
||||||
|
|
||||||
//根据按钮获取该按钮对应的图标
|
|
||||||
QPixmap getPixmap(QToolButton *btn, bool normal);
|
|
||||||
QPixmap getPixmap(QToolButton *btn, int type);
|
|
||||||
|
|
||||||
//指定QFrame导航按钮样式,带图标
|
|
||||||
void setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &normalBgColor = "#2FC5A2",
|
|
||||||
const QString &darkBgColor = "#3EA7E9",
|
|
||||||
const QString &normalTextColor = "#EEEEEE",
|
|
||||||
const QString &darkTextColor = "#FFFFFF");
|
|
||||||
|
|
||||||
//指定导航面板样式,不带图标
|
|
||||||
static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
//移除导航面板样式,防止重复
|
|
||||||
void removeStyle(QList<QToolButton *> btns);
|
|
||||||
|
|
||||||
//指定QWidget导航面板样式,带图标和效果切换
|
|
||||||
void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
struct StyleColor {
|
struct StyleColor {
|
||||||
quint32 iconSize;
|
QString position; //位置 left right top bottom
|
||||||
quint32 iconWidth;
|
bool textBesideIcon; //文字在图标下面
|
||||||
quint32 iconHeight;
|
|
||||||
quint32 borderWidth;
|
quint32 iconSize; //图标字体尺寸
|
||||||
QString type;
|
quint32 iconWidth; //图标图片宽度
|
||||||
QString borderColor;
|
quint32 iconHeight; //图标图片高度
|
||||||
QString normalBgColor;
|
|
||||||
QString normalTextColor;
|
quint32 borderWidth; //边框宽度
|
||||||
QString hoverBgColor;
|
QString borderColor; //边框颜色
|
||||||
QString hoverTextColor;
|
|
||||||
QString pressedBgColor;
|
QString normalBgColor; //正常背景颜色
|
||||||
QString pressedTextColor;
|
QString normalTextColor; //正常文字颜色
|
||||||
QString checkedBgColor;
|
QString hoverBgColor; //悬停背景颜色
|
||||||
QString checkedTextColor;
|
QString hoverTextColor; //悬停文字颜色
|
||||||
|
QString pressedBgColor; //按下背景颜色
|
||||||
|
QString pressedTextColor; //按下文字颜色
|
||||||
|
QString checkedBgColor; //选中背景颜色
|
||||||
|
QString checkedTextColor; //选中文字颜色
|
||||||
|
|
||||||
StyleColor() {
|
StyleColor() {
|
||||||
|
position = "left";
|
||||||
|
textBesideIcon = false;
|
||||||
|
|
||||||
iconSize = 12;
|
iconSize = 12;
|
||||||
iconWidth = 15;
|
iconWidth = 15;
|
||||||
iconHeight = 15;
|
iconHeight = 15;
|
||||||
|
|
||||||
borderWidth = 3;
|
borderWidth = 3;
|
||||||
type = "left";
|
|
||||||
borderColor = "#029FEA";
|
borderColor = "#029FEA";
|
||||||
|
|
||||||
normalBgColor = "#292F38";
|
normalBgColor = "#292F38";
|
||||||
normalTextColor = "#54626F";
|
normalTextColor = "#54626F";
|
||||||
hoverBgColor = "#40444D";
|
hoverBgColor = "#40444D";
|
||||||
|
@ -98,24 +66,83 @@ public:
|
||||||
checkedBgColor = "#44494F";
|
checkedBgColor = "#44494F";
|
||||||
checkedTextColor = "#FDFDFD";
|
checkedTextColor = "#FDFDFD";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//设置常规颜色 普通状态+加深状态
|
||||||
|
void setColor(const QString &normalBgColor,
|
||||||
|
const QString &normalTextColor,
|
||||||
|
const QString &darkBgColor,
|
||||||
|
const QString &darkTextColor) {
|
||||||
|
this->normalBgColor = normalBgColor;
|
||||||
|
this->normalTextColor = normalTextColor;
|
||||||
|
this->hoverBgColor = darkBgColor;
|
||||||
|
this->hoverTextColor = darkTextColor;
|
||||||
|
this->pressedBgColor = darkBgColor;
|
||||||
|
this->pressedTextColor = darkTextColor;
|
||||||
|
this->checkedBgColor = darkBgColor;
|
||||||
|
this->checkedTextColor = darkTextColor;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//指定QWidget导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
//阿里巴巴图形字体类
|
||||||
void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
static IconHelper *iconFontAliBaBa;
|
||||||
|
//FontAwesome图形字体类
|
||||||
|
static IconHelper *iconFontAwesome;
|
||||||
|
//初始化图形字体
|
||||||
|
static void initFont();
|
||||||
|
|
||||||
|
static void setIcon(QLabel *lab, int icon, quint32 size = 12);
|
||||||
|
static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
|
||||||
|
|
||||||
|
static void setPixmap(QAbstractButton *btn, const QColor &color,
|
||||||
|
int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
|
||||||
|
static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
|
||||||
|
//默认构造函数,传入字体文件+字体名称
|
||||||
|
explicit IconHelper(const QString &fontFile, const QString &fontName, QObject *parent = 0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *watched, QEvent *event);
|
bool eventFilter(QObject *watched, QEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QScopedPointer<IconHelper> self;
|
|
||||||
|
|
||||||
QFont iconFont; //图形字体
|
QFont iconFont; //图形字体
|
||||||
QList<QToolButton *> btns; //按钮队列
|
QList<QAbstractButton *> btns; //按钮队列
|
||||||
QList<QPixmap> pixNormal; //正常图片队列
|
QList<QPixmap> pixNormal; //正常图片队列
|
||||||
QList<QPixmap> pixDark; //加深图片队列
|
|
||||||
QList<QPixmap> pixHover; //悬停图片队列
|
QList<QPixmap> pixHover; //悬停图片队列
|
||||||
QList<QPixmap> pixPressed; //按下图片队列
|
QList<QPixmap> pixPressed; //按下图片队列
|
||||||
QList<QPixmap> pixChecked; //选中图片队列
|
QList<QPixmap> pixChecked; //选中图片队列
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
//按钮选中状态切换处理
|
||||||
|
void toggled(bool checked);
|
||||||
|
|
||||||
|
public:
|
||||||
|
//设置图形字体到标签
|
||||||
|
void setIcon1(QLabel *lab, int icon, quint32 size = 12);
|
||||||
|
//设置图形字体到按钮
|
||||||
|
void setIcon1(QAbstractButton *btn, int icon, quint32 size = 12);
|
||||||
|
|
||||||
|
//设置图形字体到图标
|
||||||
|
void setPixmap1(QAbstractButton *btn, const QColor &color,
|
||||||
|
int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
|
||||||
|
QPixmap getPixmap1(const QColor &color, int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
|
||||||
|
//指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
||||||
|
void setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
void setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
void setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ICONHELPER_H
|
#endif // ICONHELPER_H
|
||||||
|
|
|
@ -258,7 +258,7 @@ QString QUIDateSelect::getEndDateTime() const
|
||||||
|
|
||||||
void QUIDateSelect::setIconMain(int icon, quint32 size)
|
void QUIDateSelect::setIconMain(int icon, quint32 size)
|
||||||
{
|
{
|
||||||
IconHelper::Instance()->setIcon(this->labIco, icon, size);
|
IconHelper::setIcon(this->labIco, icon, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QUIDateSelect::setFormat(const QString &format)
|
void QUIDateSelect::setFormat(const QString &format)
|
||||||
|
|
|
@ -52,8 +52,6 @@
|
||||||
|
|
||||||
#include "head.h"
|
#include "head.h"
|
||||||
#include "iconhelper.h"
|
#include "iconhelper.h"
|
||||||
#include "iconfont.h"
|
|
||||||
|
|
||||||
#include "quihelper.h"
|
#include "quihelper.h"
|
||||||
#include "quiconfig.h"
|
#include "quiconfig.h"
|
||||||
#include "quistyle.h"
|
#include "quistyle.h"
|
||||||
|
|
|
@ -249,7 +249,7 @@ void QUIHelper::setIconBtn(QAbstractButton *btn, const QString &png, int icon)
|
||||||
int height = 18;
|
int height = 18;
|
||||||
QPixmap pix;
|
QPixmap pix;
|
||||||
if (QPixmap(png).isNull()) {
|
if (QPixmap(png).isNull()) {
|
||||||
pix = IconHelper::Instance()->getPixmap(QUIConfig::TextColor, icon, size, width, height);
|
pix = IconHelper::getPixmap(QUIConfig::TextColor, icon, size, width, height);
|
||||||
} else {
|
} else {
|
||||||
pix = QPixmap(png);
|
pix = QPixmap(png);
|
||||||
}
|
}
|
||||||
|
@ -331,8 +331,8 @@ void QUIHelper::setFramelessForm(QWidget *widgetMain, QWidget *widgetTitle,
|
||||||
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||||
}
|
}
|
||||||
|
|
||||||
IconHelper::Instance()->setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2);
|
IconHelper::setIcon(labIco, QUIConfig::IconMain, QUIConfig::FontSize + 2);
|
||||||
IconHelper::Instance()->setIcon(btnClose, QUIConfig::IconClose, QUIConfig::FontSize);
|
IconHelper::setIcon(btnClose, QUIConfig::IconClose, QUIConfig::FontSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QUIHelper::setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec)
|
void QUIHelper::setSystemDateTime(const QString &year, const QString &month, const QString &day, const QString &hour, const QString &min, const QString &sec)
|
||||||
|
|
|
@ -287,5 +287,5 @@ void QUIInputBox::on_btnMenu_Close_clicked()
|
||||||
|
|
||||||
void QUIInputBox::setIconMain(int icon, quint32 size)
|
void QUIInputBox::setIconMain(int icon, quint32 size)
|
||||||
{
|
{
|
||||||
IconHelper::Instance()->setIcon(this->labIco, icon, size);
|
IconHelper::setIcon(this->labIco, icon, size);
|
||||||
}
|
}
|
||||||
|
|
|
@ -264,7 +264,7 @@ void QUIMessageBox::on_btnMenu_Close_clicked()
|
||||||
|
|
||||||
void QUIMessageBox::setIconMain(int icon, quint32 size)
|
void QUIMessageBox::setIconMain(int icon, quint32 size)
|
||||||
{
|
{
|
||||||
IconHelper::Instance()->setIcon(this->labIco, icon, size);
|
IconHelper::setIcon(this->labIco, icon, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QUIMessageBox::setIconMsg(const QString &png, int icon)
|
void QUIMessageBox::setIconMsg(const QString &png, int icon)
|
||||||
|
@ -272,7 +272,7 @@ void QUIMessageBox::setIconMsg(const QString &png, int icon)
|
||||||
//图片存在则取图片,不存在则取图形字体
|
//图片存在则取图片,不存在则取图形字体
|
||||||
int size = this->labIcoMain->size().height();
|
int size = this->labIcoMain->size().height();
|
||||||
if (QImage(png).isNull()) {
|
if (QImage(png).isNull()) {
|
||||||
IconHelper::Instance()->setIcon(this->labIcoMain, icon, size);
|
IconHelper::setIcon(this->labIcoMain, icon, size);
|
||||||
} else {
|
} else {
|
||||||
this->labIcoMain->setStyleSheet(QString("border-image:url(%1);").arg(png));
|
this->labIcoMain->setStyleSheet(QString("border-image:url(%1);").arg(png));
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,7 +200,7 @@ void QUITipBox::on_btnMenu_Close_clicked()
|
||||||
|
|
||||||
void QUITipBox::setIconMain(int icon, quint32 size)
|
void QUITipBox::setIconMain(int icon, quint32 size)
|
||||||
{
|
{
|
||||||
IconHelper::Instance()->setIcon(this->labIco, icon, size);
|
IconHelper::setIcon(this->labIco, icon, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QUITipBox::setTip(const QString &title, const QString &tip, bool fullScreen, bool center, int closeSec)
|
void QUITipBox::setTip(const QString &title, const QString &tip, bool fullScreen, bool center, int closeSec)
|
||||||
|
|
|
@ -277,26 +277,26 @@ void QUIWidget::setIcon(QUIWidget::Widget widget, int icon, quint32 size)
|
||||||
setIconMain(icon, size);
|
setIconMain(icon, size);
|
||||||
} else if (widget == QUIWidget::BtnMenu) {
|
} else if (widget == QUIWidget::BtnMenu) {
|
||||||
QUIConfig::IconMenu = icon;
|
QUIConfig::IconMenu = icon;
|
||||||
IconHelper::Instance()->setIcon(this->btnMenu, icon, size);
|
IconHelper::setIcon(this->btnMenu, icon, size);
|
||||||
} else if (widget == QUIWidget::BtnMenu_Min) {
|
} else if (widget == QUIWidget::BtnMenu_Min) {
|
||||||
QUIConfig::IconMin = icon;
|
QUIConfig::IconMin = icon;
|
||||||
IconHelper::Instance()->setIcon(this->btnMenu_Min, icon, size);
|
IconHelper::setIcon(this->btnMenu_Min, icon, size);
|
||||||
} else if (widget == QUIWidget::BtnMenu_Max) {
|
} else if (widget == QUIWidget::BtnMenu_Max) {
|
||||||
QUIConfig::IconMax = icon;
|
QUIConfig::IconMax = icon;
|
||||||
IconHelper::Instance()->setIcon(this->btnMenu_Max, icon, size);
|
IconHelper::setIcon(this->btnMenu_Max, icon, size);
|
||||||
} else if (widget == QUIWidget::BtnMenu_Normal) {
|
} else if (widget == QUIWidget::BtnMenu_Normal) {
|
||||||
QUIConfig::IconNormal = icon;
|
QUIConfig::IconNormal = icon;
|
||||||
IconHelper::Instance()->setIcon(this->btnMenu_Max, icon, size);
|
IconHelper::setIcon(this->btnMenu_Max, icon, size);
|
||||||
} else if (widget == QUIWidget::BtnMenu_Close) {
|
} else if (widget == QUIWidget::BtnMenu_Close) {
|
||||||
QUIConfig::IconClose = icon;
|
QUIConfig::IconClose = icon;
|
||||||
IconHelper::Instance()->setIcon(this->btnMenu_Close, icon, size);
|
IconHelper::setIcon(this->btnMenu_Close, icon, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void QUIWidget::setIconMain(int icon, quint32 size)
|
void QUIWidget::setIconMain(int icon, quint32 size)
|
||||||
{
|
{
|
||||||
QUIConfig::IconMain = icon;
|
QUIConfig::IconMain = icon;
|
||||||
IconHelper::Instance()->setIcon(this->labIco, icon, size);
|
IconHelper::setIcon(this->labIco, icon, size);
|
||||||
QUIMessageBox::Instance()->setIconMain(icon, size);
|
QUIMessageBox::Instance()->setIconMain(icon, size);
|
||||||
QUIInputBox::Instance()->setIconMain(icon, size);
|
QUIInputBox::Instance()->setIconMain(icon, size);
|
||||||
QUIDateSelect::Instance()->setIconMain(icon, size);
|
QUIDateSelect::Instance()->setIconMain(icon, size);
|
||||||
|
|
|
@ -25,9 +25,9 @@ void frmNavButton::initForm()
|
||||||
|
|
||||||
//从图形字体获得图片,也可以从资源文件或者路径文件获取
|
//从图形字体获得图片,也可以从资源文件或者路径文件获取
|
||||||
int icon = 0xf061;
|
int icon = 0xf061;
|
||||||
QPixmap iconNormal = IconHelper::Instance()->getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconHover = IconHelper::Instance()->getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconCheck = IconHelper::Instance()->getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btns1 << ui->navButton11 << ui->navButton12 << ui->navButton13 << ui->navButton14;
|
btns1 << ui->navButton11 << ui->navButton12 << ui->navButton13 << ui->navButton14;
|
||||||
for (int i = 0; i < btns1.count(); i++) {
|
for (int i = 0; i < btns1.count(); i++) {
|
||||||
|
@ -73,9 +73,9 @@ void frmNavButton::initForm()
|
||||||
|
|
||||||
//分开设置图标
|
//分开设置图标
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap iconNormal = IconHelper::Instance()->getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconNormal = IconHelper::getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconHover = IconHelper::Instance()->getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconHover = IconHelper::getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconCheck = IconHelper::Instance()->getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconCheck = IconHelper::getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btn->setIconNormal(iconNormal);
|
btn->setIconNormal(iconNormal);
|
||||||
btn->setIconHover(iconHover);
|
btn->setIconHover(iconHover);
|
||||||
|
@ -115,9 +115,9 @@ void frmNavButton::initForm()
|
||||||
|
|
||||||
//分开设置图标
|
//分开设置图标
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap iconNormal = IconHelper::Instance()->getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconNormal = IconHelper::getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconHover = IconHelper::Instance()->getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconHover = IconHelper::getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconCheck = IconHelper::Instance()->getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconCheck = IconHelper::getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btn->setIconNormal(iconNormal);
|
btn->setIconNormal(iconNormal);
|
||||||
btn->setIconHover(iconHover);
|
btn->setIconHover(iconHover);
|
||||||
|
@ -138,9 +138,9 @@ void frmNavButton::initForm()
|
||||||
pixHeight = 15;
|
pixHeight = 15;
|
||||||
|
|
||||||
icon = 0xf105;
|
icon = 0xf105;
|
||||||
iconNormal = IconHelper::Instance()->getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
|
iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
|
||||||
iconHover = IconHelper::Instance()->getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
||||||
iconCheck = IconHelper::Instance()->getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btns4 << ui->navButton41 << ui->navButton42 << ui->navButton43 << ui->navButton44;
|
btns4 << ui->navButton41 << ui->navButton42 << ui->navButton43 << ui->navButton44;
|
||||||
for (int i = 0; i < btns4.count(); i++) {
|
for (int i = 0; i < btns4.count(); i++) {
|
||||||
|
@ -193,9 +193,9 @@ void frmNavButton::initForm()
|
||||||
|
|
||||||
//分开设置图标
|
//分开设置图标
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap iconNormal = IconHelper::Instance()->getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconNormal = IconHelper::getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconHover = IconHelper::Instance()->getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconHover = IconHelper::getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconCheck = IconHelper::Instance()->getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconCheck = IconHelper::getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btn->setIconNormal(iconNormal);
|
btn->setIconNormal(iconNormal);
|
||||||
btn->setIconHover(iconHover);
|
btn->setIconHover(iconHover);
|
||||||
|
@ -233,9 +233,9 @@ void frmNavButton::initForm()
|
||||||
|
|
||||||
//分开设置图标
|
//分开设置图标
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap iconNormal = IconHelper::Instance()->getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconNormal = IconHelper::getPixmap(normalTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconHover = IconHelper::Instance()->getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconHover = IconHelper::getPixmap(hoverTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
QPixmap iconCheck = IconHelper::Instance()->getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
QPixmap iconCheck = IconHelper::getPixmap(checkTextColor.name(), icon, size, pixWidth, pixHeight);
|
||||||
|
|
||||||
btn->setIconNormal(iconNormal);
|
btn->setIconNormal(iconNormal);
|
||||||
btn->setIconHover(iconHover);
|
btn->setIconHover(iconHover);
|
||||||
|
|
|
@ -1,62 +1,209 @@
|
||||||
#include "iconhelper.h"
|
#include "iconhelper.h"
|
||||||
|
|
||||||
QScopedPointer<IconHelper> IconHelper::self;
|
IconHelper *IconHelper::iconFontAliBaBa = 0;
|
||||||
IconHelper *IconHelper::Instance()
|
IconHelper *IconHelper::iconFontAwesome = 0;
|
||||||
|
void IconHelper::initFont()
|
||||||
{
|
{
|
||||||
if (self.isNull()) {
|
static bool isInit = false;
|
||||||
static QMutex mutex;
|
if (!isInit) {
|
||||||
QMutexLocker locker(&mutex);
|
isInit = true;
|
||||||
if (self.isNull()) {
|
if (iconFontAliBaBa == 0) {
|
||||||
self.reset(new IconHelper);
|
iconFontAliBaBa = new IconHelper(":/image/iconfont.ttf", "iconfont");
|
||||||
|
}
|
||||||
|
if (iconFontAwesome == 0) {
|
||||||
|
iconFontAwesome = new IconHelper(":/image/fontawesome-webfont.ttf", "FontAwesome");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.data();
|
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setIcon1(lab, icon, size);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setIcon1(lab, icon, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IconHelper::IconHelper(QObject *parent) : QObject(parent)
|
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setIcon1(btn, icon, size);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setIcon1(btn, icon, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||||
|
quint32 width, quint32 height, int flags)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setPixmap1(btn, color, icon, size, width, height, flags);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setPixmap1(btn, color, icon, size, width, height, flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
||||||
|
quint32 width, quint32 height, int flags)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
QPixmap pix;
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
pix = iconFontAliBaBa->getPixmap1(color, icon, size, width, height, flags);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
pix = iconFontAwesome->getPixmap1(color, icon, size, width, height, flags);
|
||||||
|
}
|
||||||
|
return pix;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
|
||||||
|
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
|
{
|
||||||
|
initFont();
|
||||||
|
|
||||||
|
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
|
||||||
|
int icon = icons.first();
|
||||||
|
if (icon > 0xe000 && icon < 0xf000) {
|
||||||
|
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
} else if (icon > 0xf000) {
|
||||||
|
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
//判断图形字体是否存在,不存在则加入
|
//判断图形字体是否存在,不存在则加入
|
||||||
QFontDatabase fontDb;
|
QFontDatabase fontDb;
|
||||||
if (!fontDb.families().contains("FontAwesome")) {
|
if (!fontDb.families().contains(fontName)) {
|
||||||
int fontId = fontDb.addApplicationFont(":/image/fontawesome-webfont.ttf");
|
int fontId = fontDb.addApplicationFont(fontFile);
|
||||||
QStringList fontName = fontDb.applicationFontFamilies(fontId);
|
QStringList listName = fontDb.applicationFontFamilies(fontId);
|
||||||
if (fontName.count() == 0) {
|
if (listName.count() == 0) {
|
||||||
qDebug() << "load fontawesome-webfont.ttf error";
|
qDebug() << QString("load %1 error").arg(fontName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fontDb.families().contains("FontAwesome")) {
|
if (fontDb.families().contains(fontName)) {
|
||||||
iconFont = QFont("FontAwesome");
|
iconFont = QFont(fontName);
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
||||||
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QFont IconHelper::getIconFont()
|
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
|
||||||
{
|
{
|
||||||
return this->iconFont;
|
//根据不同的
|
||||||
|
if (watched->inherits("QAbstractButton")) {
|
||||||
|
QAbstractButton *btn = (QAbstractButton *)watched;
|
||||||
|
int index = btns.indexOf(btn);
|
||||||
|
if (index >= 0) {
|
||||||
|
//不同的事件设置不同的图标,同时区分选中的和没有选中的
|
||||||
|
if (btn->isChecked()) {
|
||||||
|
if (event->type() == QEvent::MouseButtonPress) {
|
||||||
|
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||||
|
if (mouseEvent->button() == Qt::LeftButton) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
}
|
||||||
|
} else if (event->type() == QEvent::Enter) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
} else if (event->type() == QEvent::Leave) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (event->type() == QEvent::MouseButtonPress) {
|
||||||
|
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||||
|
if (mouseEvent->button() == Qt::LeftButton) {
|
||||||
|
btn->setIcon(QIcon(pixPressed.at(index)));
|
||||||
|
}
|
||||||
|
} else if (event->type() == QEvent::Enter) {
|
||||||
|
btn->setIcon(QIcon(pixHover.at(index)));
|
||||||
|
} else if (event->type() == QEvent::Leave) {
|
||||||
|
btn->setIcon(QIcon(pixNormal.at(index)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
|
return QObject::eventFilter(watched, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::toggled(bool checked)
|
||||||
|
{
|
||||||
|
//选中和不选中设置不同的图标
|
||||||
|
QAbstractButton *btn = (QAbstractButton *)sender();
|
||||||
|
int index = btns.indexOf(btn);
|
||||||
|
if (checked) {
|
||||||
|
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||||
|
} else {
|
||||||
|
btn->setIcon(QIcon(pixNormal.at(index)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
|
||||||
{
|
{
|
||||||
iconFont.setPixelSize(size);
|
iconFont.setPixelSize(size);
|
||||||
lab->setFont(iconFont);
|
lab->setFont(iconFont);
|
||||||
lab->setText((QChar)icon);
|
lab->setText((QChar)icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
void IconHelper::setIcon1(QAbstractButton *btn, int icon, quint32 size)
|
||||||
{
|
{
|
||||||
iconFont.setPixelSize(size);
|
iconFont.setPixelSize(size);
|
||||||
btn->setFont(iconFont);
|
btn->setFont(iconFont);
|
||||||
btn->setText((QChar)icon);
|
btn->setText((QChar)icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
void IconHelper::setPixmap1(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||||
quint32 pixWidth, quint32 pixHeight, int flags)
|
quint32 width, quint32 height, int flags)
|
||||||
{
|
{
|
||||||
QPixmap pix(pixWidth, pixHeight);
|
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);
|
||||||
pix.fill(Qt::transparent);
|
pix.fill(Qt::transparent);
|
||||||
|
|
||||||
QPainter painter;
|
QPainter painter;
|
||||||
|
@ -68,124 +215,100 @@ QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
||||||
painter.setFont(iconFont);
|
painter.setFont(iconFont);
|
||||||
painter.drawText(pix.rect(), flags, (QChar)icon);
|
painter.drawText(pix.rect(), flags, (QChar)icon);
|
||||||
painter.end();
|
painter.end();
|
||||||
|
|
||||||
return pix;
|
return pix;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(QToolButton *btn, bool normal)
|
void IconHelper::setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
{
|
{
|
||||||
QPixmap pix;
|
QList<QAbstractButton *> list;
|
||||||
int index = btns.indexOf(btn);
|
foreach (QPushButton *btn, btns) {
|
||||||
if (index >= 0) {
|
list << btn;
|
||||||
if (normal) {
|
|
||||||
pix = pixNormal.at(index);
|
|
||||||
} else {
|
|
||||||
pix = pixDark.at(index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pix;
|
setStyle(widget, list, icons, styleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap IconHelper::getPixmap(QToolButton *btn, int type)
|
void IconHelper::setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
{
|
{
|
||||||
QPixmap pix;
|
QList<QAbstractButton *> list;
|
||||||
int index = btns.indexOf(btn);
|
foreach (QToolButton *btn, btns) {
|
||||||
if (index >= 0) {
|
list << btn;
|
||||||
if (type == 0) {
|
|
||||||
pix = pixNormal.at(index);
|
|
||||||
} else if (type == 1) {
|
|
||||||
pix = pixHover.at(index);
|
|
||||||
} else if (type == 2) {
|
|
||||||
pix = pixPressed.at(index);
|
|
||||||
} else if (type == 3) {
|
|
||||||
pix = pixChecked.at(index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pix;
|
setStyle(widget, list, icons, styleColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
{
|
||||||
int btnCount = btns.count();
|
int btnCount = btns.count();
|
||||||
int charCount = icons.count();
|
int iconCount = icons.count();
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
if (btnCount <= 0 || iconCount <= 0 || btnCount != iconCount) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList qss;
|
QString position = styleColor.position;
|
||||||
qss.append(QString("QFrame>QToolButton{border-style:none;border-width:0px;"
|
quint32 iconSize = styleColor.iconSize;
|
||||||
"background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor));
|
quint32 iconWidth = styleColor.iconWidth;
|
||||||
qss.append(QString("QFrame>QToolButton:hover,QFrame>QToolButton:pressed,QFrame>QToolButton:checked"
|
quint32 iconHeight = styleColor.iconHeight;
|
||||||
"{background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor));
|
quint32 borderWidth = styleColor.borderWidth;
|
||||||
|
|
||||||
frame->setStyleSheet(qss.join(""));
|
//根据不同的位置计算边框
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
|
||||||
btn->setIcon(QIcon(pixNormal));
|
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
this->pixHover.append(pixDark);
|
|
||||||
this->pixPressed.append(pixDark);
|
|
||||||
this->pixChecked.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
QString strBorder;
|
QString strBorder;
|
||||||
if (type == "top") {
|
if (position == "top") {
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "right") {
|
} else if (position == "right") {
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "bottom") {
|
} else if (position == "bottom") {
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
} else if (type == "left") {
|
} else if (position == "left") {
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
.arg(borderWidth).arg(borderWidth * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
||||||
QStringList qss;
|
QStringList qss;
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;"
|
if (styleColor.textBesideIcon) {
|
||||||
"color:%2;background:%3;}").arg(type).arg(normalTextColor).arg(normalBgColor));
|
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);
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
} else {
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
.arg(position).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconHelper::removeStyle(QList<QToolButton *> btns)
|
//悬停+按下+选中
|
||||||
{
|
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
||||||
for (int i = 0; i < btns.count(); i++) {
|
.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);
|
||||||
|
|
||||||
|
//设置样式表
|
||||||
|
widget->setStyleSheet(qss.join(""));
|
||||||
|
|
||||||
|
//可能会重复调用设置所以先要移除上一次的
|
||||||
|
for (int i = 0; i < btnCount; i++) {
|
||||||
for (int j = 0; j < this->btns.count(); j++) {
|
for (int j = 0; j < this->btns.count(); j++) {
|
||||||
if (this->btns.at(j) == btns.at(i)) {
|
if (this->btns.at(j) == btns.at(i)) {
|
||||||
|
disconnect(btns.at(i), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
|
||||||
this->btns.at(j)->removeEventFilter(this);
|
this->btns.at(j)->removeEventFilter(this);
|
||||||
this->btns.removeAt(j);
|
this->btns.removeAt(j);
|
||||||
this->pixNormal.removeAt(j);
|
this->pixNormal.removeAt(j);
|
||||||
this->pixDark.removeAt(j);
|
|
||||||
this->pixHover.removeAt(j);
|
this->pixHover.removeAt(j);
|
||||||
this->pixPressed.removeAt(j);
|
this->pixPressed.removeAt(j);
|
||||||
this->pixChecked.removeAt(j);
|
this->pixChecked.removeAt(j);
|
||||||
|
@ -193,174 +316,36 @@ void IconHelper::removeStyle(QList<QToolButton *> btns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize, quint32 iconWidth, quint32 iconHeight,
|
|
||||||
const QString &type, int borderWidth, const QString &borderColor,
|
|
||||||
const QString &normalBgColor, const QString &darkBgColor,
|
|
||||||
const QString &normalTextColor, const QString &darkTextColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
|
||||||
QStringList qss;
|
|
||||||
if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(normalBgColor).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
} else {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(normalTextColor).arg(normalBgColor));
|
|
||||||
}
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:pressed,"
|
|
||||||
"QWidget[flag=\"%1\"] QAbstractButton:checked{"
|
|
||||||
"border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(borderColor).arg(darkTextColor).arg(darkBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(normalBgColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton{border-width:0px;"
|
|
||||||
"background-color:%1;color:%2;}").arg(normalBgColor).arg(normalTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:hover,QWidget>QToolButton:pressed,QWidget>QToolButton:checked{"
|
|
||||||
"background-color:%1;color:%2;}").arg(darkBgColor).arg(darkTextColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
||||||
|
int checkedIndex = -1;
|
||||||
for (int i = 0; i < btnCount; i++) {
|
for (int i = 0; i < btnCount; i++) {
|
||||||
int icon = icons.at(i);
|
int icon = icons.at(i);
|
||||||
QPixmap pixNormal = getPixmap(normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
QPixmap pixNormal = getPixmap1(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||||
QPixmap pixDark = getPixmap(darkTextColor, 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);
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
//记住最后选中的按钮
|
||||||
btn->setIcon(QIcon(pixNormal));
|
QAbstractButton *btn = btns.at(i);
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixDark);
|
|
||||||
this->pixHover.append(pixDark);
|
|
||||||
this->pixPressed.append(pixDark);
|
|
||||||
this->pixChecked.append(pixDark);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
|
||||||
{
|
|
||||||
int btnCount = btns.count();
|
|
||||||
int charCount = icons.count();
|
|
||||||
if (btnCount <= 0 || charCount <= 0 || btnCount != charCount) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
quint32 iconSize = styleColor.iconSize;
|
|
||||||
quint32 iconWidth = styleColor.iconWidth;
|
|
||||||
quint32 iconHeight = styleColor.iconHeight;
|
|
||||||
quint32 borderWidth = styleColor.borderWidth;
|
|
||||||
QString type = styleColor.type;
|
|
||||||
|
|
||||||
QString strBorder;
|
|
||||||
if (type == "top") {
|
|
||||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "right") {
|
|
||||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "bottom") {
|
|
||||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
} else if (type == "left") {
|
|
||||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
|
||||||
.arg(borderWidth).arg(borderWidth * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
|
||||||
QStringList qss;
|
|
||||||
if (btns.at(0)->toolButtonStyle() == Qt::ToolButtonTextBesideIcon) {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor));
|
|
||||||
} else {
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
|
||||||
.arg(type).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor));
|
|
||||||
}
|
|
||||||
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor));
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor));
|
|
||||||
qss.append(QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
|
||||||
.arg(type).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor));
|
|
||||||
|
|
||||||
qss.append(QString("QWidget#%1{background:%2;}").arg(widget->objectName()).arg(styleColor.normalBgColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton{border-width:0px;background-color:%1;color:%2;}").arg(styleColor.normalBgColor).arg(styleColor.normalTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:hover{background-color:%1;color:%2;}").arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:pressed{background-color:%1;color:%2;}").arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor));
|
|
||||||
qss.append(QString("QWidget>QToolButton:checked{background-color:%1;color:%2;}").arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor));
|
|
||||||
|
|
||||||
widget->setStyleSheet(qss.join(""));
|
|
||||||
|
|
||||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
|
||||||
for (int i = 0; i < btnCount; i++) {
|
|
||||||
int icon = icons.at(i);
|
|
||||||
QPixmap pixNormal = getPixmap(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixHover = getPixmap(styleColor.hoverTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixPressed = getPixmap(styleColor.pressedTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
QPixmap pixChecked = getPixmap(styleColor.checkedTextColor, icon, iconSize, iconWidth, iconHeight);
|
|
||||||
|
|
||||||
QToolButton *btn = btns.at(i);
|
|
||||||
btn->setIcon(QIcon(pixNormal));
|
|
||||||
btn->setIconSize(QSize(iconWidth, iconHeight));
|
|
||||||
btn->installEventFilter(this);
|
|
||||||
|
|
||||||
this->btns.append(btn);
|
|
||||||
this->pixNormal.append(pixNormal);
|
|
||||||
this->pixDark.append(pixHover);
|
|
||||||
this->pixHover.append(pixHover);
|
|
||||||
this->pixPressed.append(pixPressed);
|
|
||||||
this->pixChecked.append(pixChecked);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
|
|
||||||
{
|
|
||||||
if (watched->inherits("QToolButton")) {
|
|
||||||
QToolButton *btn = (QToolButton *)watched;
|
|
||||||
int index = btns.indexOf(btn);
|
|
||||||
if (index >= 0) {
|
|
||||||
if (event->type() == QEvent::Enter) {
|
|
||||||
btn->setIcon(QIcon(pixHover.at(index)));
|
|
||||||
} else if (event->type() == QEvent::MouseButtonPress) {
|
|
||||||
btn->setIcon(QIcon(pixPressed.at(index)));
|
|
||||||
} else if (event->type() == QEvent::Leave) {
|
|
||||||
if (btn->isChecked()) {
|
if (btn->isChecked()) {
|
||||||
btn->setIcon(QIcon(pixChecked.at(index)));
|
checkedIndex = i;
|
||||||
} else {
|
|
||||||
btn->setIcon(QIcon(pixNormal.at(index)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return QObject::eventFilter(watched, event);
|
btn->setIcon(QIcon(pixNormal));
|
||||||
|
btn->setIconSize(QSize(iconWidth, iconHeight));
|
||||||
|
btn->installEventFilter(this);
|
||||||
|
connect(btn, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
|
||||||
|
|
||||||
|
this->btns << btn;
|
||||||
|
this->pixNormal << pixNormal;
|
||||||
|
this->pixHover << pixHover;
|
||||||
|
this->pixPressed << pixPressed;
|
||||||
|
this->pixChecked << pixChecked;
|
||||||
|
}
|
||||||
|
|
||||||
|
//主动触发一下选中的按钮
|
||||||
|
if (checkedIndex >= 0) {
|
||||||
|
QMetaObject::invokeMethod(btns.at(checkedIndex), "toggled", Q_ARG(bool, true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
#ifndef ICONHELPER_H
|
#ifndef ICONHELPER_H
|
||||||
#define ICONHELPER_H
|
#define ICONHELPER_H
|
||||||
|
|
||||||
#include <QtCore>
|
|
||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
|
||||||
|
* 1. 可传入多种图形字体文件
|
||||||
|
* 2. 可设置 QLabel+QAbstractButton 图形字体
|
||||||
|
* 3. 可设置按钮图标
|
||||||
|
* 4. 可获取指定尺寸的图形字体图片
|
||||||
|
* 5. 超级导航栏样式设置,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
||||||
|
*/
|
||||||
|
|
||||||
#ifdef quc
|
#ifdef quc
|
||||||
class Q_DECL_EXPORT IconHelper : public QObject
|
class Q_DECL_EXPORT IconHelper : public QObject
|
||||||
#else
|
#else
|
||||||
|
@ -17,78 +25,38 @@ class IconHelper : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static IconHelper *Instance();
|
//样式颜色结构体
|
||||||
explicit IconHelper(QObject *parent = 0);
|
|
||||||
|
|
||||||
//获取图形字体
|
|
||||||
QFont getIconFont();
|
|
||||||
|
|
||||||
//设置图形字体到标签
|
|
||||||
void setIcon(QLabel *lab, int icon, quint32 size = 12);
|
|
||||||
//设置图形字体到按钮
|
|
||||||
void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
|
|
||||||
|
|
||||||
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
|
|
||||||
QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
|
|
||||||
quint32 pixWidth = 15, quint32 pixHeight = 15,
|
|
||||||
int flags = Qt::AlignCenter);
|
|
||||||
|
|
||||||
//根据按钮获取该按钮对应的图标
|
|
||||||
QPixmap getPixmap(QToolButton *btn, bool normal);
|
|
||||||
QPixmap getPixmap(QToolButton *btn, int type);
|
|
||||||
|
|
||||||
//指定QFrame导航按钮样式,带图标
|
|
||||||
void setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &normalBgColor = "#2FC5A2",
|
|
||||||
const QString &darkBgColor = "#3EA7E9",
|
|
||||||
const QString &normalTextColor = "#EEEEEE",
|
|
||||||
const QString &darkTextColor = "#FFFFFF");
|
|
||||||
|
|
||||||
//指定导航面板样式,不带图标
|
|
||||||
static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
//移除导航面板样式,防止重复
|
|
||||||
void removeStyle(QList<QToolButton *> btns);
|
|
||||||
|
|
||||||
//指定QWidget导航面板样式,带图标和效果切换
|
|
||||||
void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons,
|
|
||||||
quint32 iconSize = 12, quint32 iconWidth = 15, quint32 iconHeight = 15,
|
|
||||||
const QString &type = "left", int borderWidth = 3,
|
|
||||||
const QString &borderColor = "#029FEA",
|
|
||||||
const QString &normalBgColor = "#292F38",
|
|
||||||
const QString &darkBgColor = "#1D2025",
|
|
||||||
const QString &normalTextColor = "#54626F",
|
|
||||||
const QString &darkTextColor = "#FDFDFD");
|
|
||||||
|
|
||||||
struct StyleColor {
|
struct StyleColor {
|
||||||
quint32 iconSize;
|
QString position; //位置 left right top bottom
|
||||||
quint32 iconWidth;
|
bool textBesideIcon; //文字在图标左侧
|
||||||
quint32 iconHeight;
|
|
||||||
quint32 borderWidth;
|
quint32 iconSize; //图标字体尺寸
|
||||||
QString type;
|
quint32 iconWidth; //图标图片宽度
|
||||||
QString borderColor;
|
quint32 iconHeight; //图标图片高度
|
||||||
QString normalBgColor;
|
|
||||||
QString normalTextColor;
|
quint32 borderWidth; //边框宽度
|
||||||
QString hoverBgColor;
|
QString borderColor; //边框颜色
|
||||||
QString hoverTextColor;
|
|
||||||
QString pressedBgColor;
|
QString normalBgColor; //正常背景颜色
|
||||||
QString pressedTextColor;
|
QString normalTextColor; //正常文字颜色
|
||||||
QString checkedBgColor;
|
QString hoverBgColor; //悬停背景颜色
|
||||||
QString checkedTextColor;
|
QString hoverTextColor; //悬停文字颜色
|
||||||
|
QString pressedBgColor; //按下背景颜色
|
||||||
|
QString pressedTextColor; //按下文字颜色
|
||||||
|
QString checkedBgColor; //选中背景颜色
|
||||||
|
QString checkedTextColor; //选中文字颜色
|
||||||
|
|
||||||
StyleColor() {
|
StyleColor() {
|
||||||
|
position = "left";
|
||||||
|
textBesideIcon = false;
|
||||||
|
|
||||||
iconSize = 12;
|
iconSize = 12;
|
||||||
iconWidth = 15;
|
iconWidth = 15;
|
||||||
iconHeight = 15;
|
iconHeight = 15;
|
||||||
|
|
||||||
borderWidth = 3;
|
borderWidth = 3;
|
||||||
type = "left";
|
|
||||||
borderColor = "#029FEA";
|
borderColor = "#029FEA";
|
||||||
|
|
||||||
normalBgColor = "#292F38";
|
normalBgColor = "#292F38";
|
||||||
normalTextColor = "#54626F";
|
normalTextColor = "#54626F";
|
||||||
hoverBgColor = "#40444D";
|
hoverBgColor = "#40444D";
|
||||||
|
@ -98,24 +66,83 @@ public:
|
||||||
checkedBgColor = "#44494F";
|
checkedBgColor = "#44494F";
|
||||||
checkedTextColor = "#FDFDFD";
|
checkedTextColor = "#FDFDFD";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//设置常规颜色 普通状态+加深状态
|
||||||
|
void setColor(const QString &normalBgColor,
|
||||||
|
const QString &normalTextColor,
|
||||||
|
const QString &darkBgColor,
|
||||||
|
const QString &darkTextColor) {
|
||||||
|
this->normalBgColor = normalBgColor;
|
||||||
|
this->normalTextColor = normalTextColor;
|
||||||
|
this->hoverBgColor = darkBgColor;
|
||||||
|
this->hoverTextColor = darkTextColor;
|
||||||
|
this->pressedBgColor = darkBgColor;
|
||||||
|
this->pressedTextColor = darkTextColor;
|
||||||
|
this->checkedBgColor = darkBgColor;
|
||||||
|
this->checkedTextColor = darkTextColor;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//指定QWidget导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
//阿里巴巴图形字体类
|
||||||
void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
static IconHelper *iconFontAliBaBa;
|
||||||
|
//FontAwesome图形字体类
|
||||||
|
static IconHelper *iconFontAwesome;
|
||||||
|
//初始化图形字体
|
||||||
|
static void initFont();
|
||||||
|
|
||||||
|
static void setIcon(QLabel *lab, int icon, quint32 size = 12);
|
||||||
|
static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
|
||||||
|
|
||||||
|
static void setPixmap(QAbstractButton *btn, const QColor &color,
|
||||||
|
int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
|
||||||
|
static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
|
||||||
|
//默认构造函数,传入字体文件+字体名称
|
||||||
|
explicit IconHelper(const QString &fontFile, const QString &fontName, QObject *parent = 0);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *watched, QEvent *event);
|
bool eventFilter(QObject *watched, QEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QScopedPointer<IconHelper> self;
|
|
||||||
|
|
||||||
QFont iconFont; //图形字体
|
QFont iconFont; //图形字体
|
||||||
QList<QToolButton *> btns; //按钮队列
|
QList<QAbstractButton *> btns; //按钮队列
|
||||||
QList<QPixmap> pixNormal; //正常图片队列
|
QList<QPixmap> pixNormal; //正常图片队列
|
||||||
QList<QPixmap> pixDark; //加深图片队列
|
|
||||||
QList<QPixmap> pixHover; //悬停图片队列
|
QList<QPixmap> pixHover; //悬停图片队列
|
||||||
QList<QPixmap> pixPressed; //按下图片队列
|
QList<QPixmap> pixPressed; //按下图片队列
|
||||||
QList<QPixmap> pixChecked; //选中图片队列
|
QList<QPixmap> pixChecked; //选中图片队列
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
//按钮选中状态切换处理
|
||||||
|
void toggled(bool checked);
|
||||||
|
|
||||||
|
public:
|
||||||
|
//设置图形字体到标签
|
||||||
|
void setIcon1(QLabel *lab, int icon, quint32 size = 12);
|
||||||
|
//设置图形字体到按钮
|
||||||
|
void setIcon1(QAbstractButton *btn, int icon, quint32 size = 12);
|
||||||
|
|
||||||
|
//设置图形字体到图标
|
||||||
|
void setPixmap1(QAbstractButton *btn, const QColor &color,
|
||||||
|
int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
|
||||||
|
QPixmap getPixmap1(const QColor &color, int icon, quint32 size = 12,
|
||||||
|
quint32 width = 15, quint32 height = 15,
|
||||||
|
int flags = Qt::AlignCenter);
|
||||||
|
|
||||||
|
//指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
|
||||||
|
void setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
void setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
|
void setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ICONHELPER_H
|
#endif // ICONHELPER_H
|
||||||
|
|
Loading…
Reference in New Issue