更新代码
This commit is contained in:
20
ui/core_helper/appdata.cpp
Normal file
20
ui/core_helper/appdata.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "appdata.h"
|
||||
#include "qthelper.h"
|
||||
|
||||
QString AppData::TitleFlag = "(QQ: 517216493 WX: feiyangqingyun)";
|
||||
int AppData::RowHeight = 25;
|
||||
int AppData::RightWidth = 180;
|
||||
int AppData::FormWidth = 1200;
|
||||
int AppData::FormHeight = 750;
|
||||
|
||||
void AppData::checkRatio()
|
||||
{
|
||||
//根据分辨率设定宽高
|
||||
int width = QtHelper::deskWidth();
|
||||
if (width >= 1440) {
|
||||
RowHeight = RowHeight < 25 ? 25 : RowHeight;
|
||||
RightWidth = RightWidth < 220 ? 220 : RightWidth;
|
||||
FormWidth = FormWidth < 1200 ? 1200 : FormWidth;
|
||||
FormHeight = FormHeight < 800 ? 800 : FormHeight;
|
||||
}
|
||||
}
|
||||
18
ui/core_helper/appdata.h
Normal file
18
ui/core_helper/appdata.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef APPDATA_H
|
||||
#define APPDATA_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class AppData
|
||||
{
|
||||
public:
|
||||
static QString TitleFlag; //标题标识
|
||||
static int RowHeight; //行高
|
||||
static int RightWidth; //右侧宽度
|
||||
static int FormWidth; //窗体宽度
|
||||
static int FormHeight; //窗体高度
|
||||
|
||||
static void checkRatio(); //校验分辨率
|
||||
};
|
||||
|
||||
#endif // APPDATA_H
|
||||
67
ui/core_helper/appinit.cpp
Normal file
67
ui/core_helper/appinit.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "appinit.h"
|
||||
#include "qmutex.h"
|
||||
#include "qapplication.h"
|
||||
#include "qevent.h"
|
||||
#include "qwidget.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
QScopedPointer<AppInit> AppInit::self;
|
||||
AppInit *AppInit::Instance()
|
||||
{
|
||||
if (self.isNull()) {
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
if (self.isNull()) {
|
||||
self.reset(new AppInit);
|
||||
}
|
||||
}
|
||||
|
||||
return self.data();
|
||||
}
|
||||
|
||||
AppInit::AppInit(QObject *parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
bool AppInit::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
QWidget *w = (QWidget *)watched;
|
||||
if (!w->property("canMove").toBool()) {
|
||||
return QObject::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
static QPoint mousePoint;
|
||||
static bool mousePressed = false;
|
||||
|
||||
int type = event->type();
|
||||
QPoint p;
|
||||
QMouseEvent *mouseEvent = (QMouseEvent *)(event);
|
||||
if (mouseEvent) {
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
|
||||
p = mouseEvent->globalPos();
|
||||
#else
|
||||
p = mouseEvent->globalPosition().toPoint();
|
||||
#endif
|
||||
}
|
||||
|
||||
if (type == QEvent::MouseButtonPress) {
|
||||
if (mouseEvent->button() == Qt::LeftButton) {
|
||||
mousePressed = true;
|
||||
mousePoint = p - w->pos();
|
||||
}
|
||||
} else if (type == QEvent::MouseButtonRelease) {
|
||||
mousePressed = false;
|
||||
} else if (type == QEvent::MouseMove) {
|
||||
if (mousePressed) {
|
||||
w->move(p - mousePoint);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return QObject::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void AppInit::start()
|
||||
{
|
||||
qApp->installEventFilter(this);
|
||||
}
|
||||
23
ui/core_helper/appinit.h
Normal file
23
ui/core_helper/appinit.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef APPINIT_H
|
||||
#define APPINIT_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class AppInit : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static AppInit *Instance();
|
||||
explicit AppInit(QObject *parent = 0);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
static QScopedPointer<AppInit> self;
|
||||
|
||||
public slots:
|
||||
void start();
|
||||
};
|
||||
|
||||
#endif // APPINIT_H
|
||||
41
ui/core_helper/base64helper.cpp
Normal file
41
ui/core_helper/base64helper.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "base64helper.h"
|
||||
#include "qbuffer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
QString Base64Helper::imageToBase64(const QImage &image)
|
||||
{
|
||||
return QString(imageToBase64x(image));
|
||||
}
|
||||
|
||||
QByteArray Base64Helper::imageToBase64x(const QImage &image)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QByteArray data;
|
||||
QBuffer buffer(&data);
|
||||
image.save(&buffer, "JPG");
|
||||
data = data.toBase64();
|
||||
return data;
|
||||
}
|
||||
|
||||
QImage Base64Helper::base64ToImage(const QString &data)
|
||||
{
|
||||
return base64ToImagex(data.toUtf8());
|
||||
}
|
||||
|
||||
QImage Base64Helper::base64ToImagex(const QByteArray &data)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QImage image;
|
||||
image.loadFromData(QByteArray::fromBase64(data));
|
||||
return image;
|
||||
}
|
||||
|
||||
QString Base64Helper::textToBase64(const QString &text)
|
||||
{
|
||||
return QString(text.toUtf8().toBase64());
|
||||
}
|
||||
|
||||
QString Base64Helper::base64ToText(const QString &text)
|
||||
{
|
||||
return QString(QByteArray::fromBase64(text.toUtf8()));
|
||||
}
|
||||
37
ui/core_helper/base64helper.h
Normal file
37
ui/core_helper/base64helper.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef BASE64HELPER_H
|
||||
#define BASE64HELPER_H
|
||||
|
||||
/**
|
||||
* base64编码转换类 作者:feiyangqingyun(QQ:517216493) 2016-12-16
|
||||
* 1. 图片转base64字符串。
|
||||
* 2. base64字符串转图片。
|
||||
* 3. 字符转base64字符串。
|
||||
* 4. base64字符串转字符。
|
||||
* 5. 后期增加数据压缩。
|
||||
* 6. Qt6对base64编码转换进行了重写效率提升至少200%。
|
||||
*/
|
||||
|
||||
#include <QImage>
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT Base64Helper
|
||||
#else
|
||||
class Base64Helper
|
||||
#endif
|
||||
|
||||
{
|
||||
public:
|
||||
//图片转base64字符串
|
||||
static QString imageToBase64(const QImage &image);
|
||||
static QByteArray imageToBase64x(const QImage &image);
|
||||
|
||||
//base64字符串转图片
|
||||
static QImage base64ToImage(const QString &data);
|
||||
static QImage base64ToImagex(const QByteArray &data);
|
||||
|
||||
//字符串与base64互转
|
||||
static QString textToBase64(const QString &text);
|
||||
static QString base64ToText(const QString &text);
|
||||
};
|
||||
|
||||
#endif // BASE64HELPER_H
|
||||
72
ui/core_helper/core_helper.pri
Normal file
72
ui/core_helper/core_helper.pri
Normal file
@@ -0,0 +1,72 @@
|
||||
QT *= network
|
||||
greaterThan(QT_MAJOR_VERSION, 4) {
|
||||
lessThan(QT_MAJOR_VERSION, 6) {
|
||||
android {QT *= androidextras}
|
||||
} else {
|
||||
QT *= core-private
|
||||
}}
|
||||
|
||||
#指定编译产生的文件分门别类放到对应目录
|
||||
MOC_DIR = temp/moc
|
||||
RCC_DIR = temp/rcc
|
||||
UI_DIR = temp/ui
|
||||
OBJECTS_DIR = temp/obj
|
||||
|
||||
#指定编译生成的可执行文件放到源码上一级目录下的bin目录
|
||||
!android:!ios {
|
||||
DESTDIR = $$PWD/../bin
|
||||
}
|
||||
|
||||
#把所有警告都关掉眼不见为净
|
||||
CONFIG += warn_off
|
||||
#QMAKE_CXXFLAGS_WARN_ON -= -w34100
|
||||
#QMAKE_CXXFLAGS *= -Wno-unused-parameter
|
||||
|
||||
#开启大资源支持
|
||||
CONFIG += resources_big
|
||||
#开启后会将打印信息用控制台输出
|
||||
#CONFIG += console
|
||||
#开启后不会生成空的 debug release 目录
|
||||
#CONFIG -= debug_and_release
|
||||
|
||||
#引入全志H3芯片依赖(不需要的用户可以删除)
|
||||
include ($$PWD/h3.pri)
|
||||
#将当前目录加入到头文件路径
|
||||
INCLUDEPATH += $$PWD
|
||||
|
||||
HEADERS += $$PWD/appdata.h
|
||||
SOURCES += $$PWD/appdata.cpp
|
||||
|
||||
HEADERS += $$PWD/appinit.h
|
||||
SOURCES += $$PWD/appinit.cpp
|
||||
|
||||
HEADERS += $$PWD/base64helper.h
|
||||
SOURCES += $$PWD/base64helper.cpp
|
||||
|
||||
HEADERS += $$PWD/customstyle.h
|
||||
SOURCES += $$PWD/customstyle.cpp
|
||||
|
||||
HEADERS += $$PWD/iconhelper.h
|
||||
SOURCES += $$PWD/iconhelper.cpp
|
||||
|
||||
HEADERS += $$PWD/qthelper.h
|
||||
SOURCES += $$PWD/qthelper.cpp
|
||||
|
||||
#可以指定不加载对应的资源文件
|
||||
!contains(DEFINES, no_qrc_image) {
|
||||
RESOURCES += $$PWD/qrc/image.qrc
|
||||
}
|
||||
|
||||
!contains(DEFINES, no_qrc_qm) {
|
||||
RESOURCES += $$PWD/qrc/qm.qrc
|
||||
}
|
||||
|
||||
!contains(DEFINES, no_qrc_font) {
|
||||
RESOURCES += $$PWD/qrc/font.qrc
|
||||
}
|
||||
|
||||
wasm {
|
||||
HEADERS += $$PWD/wasmhelper.h
|
||||
SOURCES += $$PWD/wasmhelper.cpp
|
||||
RESOURCES += $$PWD/qrc/wasm.qrc
|
||||
}
|
||||
66
ui/core_helper/customstyle.cpp
Normal file
66
ui/core_helper/customstyle.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "customstyle.h"
|
||||
#include "qapplication.h"
|
||||
#include "qpalette.h"
|
||||
|
||||
void CustomStyle::initStyle(int fontSize, int radioButtonSize, int checkBoxSize, int sliderHeight)
|
||||
{
|
||||
if (fontSize <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList list;
|
||||
//全局字体
|
||||
list << QString("*{font-size:%1px;}").arg(fontSize);
|
||||
//单选框
|
||||
list << QString("QRadioButton::indicator{width:%1px;height:%1px;}").arg(radioButtonSize);
|
||||
//复选框
|
||||
list << QString("QCheckBox::indicator,QGroupBox::indicator,QTreeWidget::indicator,QListWidget::indicator{width:%1px;height:%1px;}").arg(checkBoxSize);
|
||||
|
||||
//滑块颜色
|
||||
#if 0
|
||||
QString normalColor = "#e3e3e3";
|
||||
QString grooveColor = "#0078d7";
|
||||
QString handleColor = "#FFFFFF";
|
||||
QString borderColor = "#9B9B9B";
|
||||
#else
|
||||
QPalette palette;
|
||||
for (int i = 0; i < 21; ++i) {
|
||||
//qDebug() << i << palette.color((QPalette::ColorRole)i).name();
|
||||
}
|
||||
|
||||
QString normalColor = palette.color(QPalette::Midlight).name();
|
||||
QString grooveColor = palette.color(QPalette::Highlight).name();
|
||||
QString handleColor = palette.color(QPalette::Light).name();
|
||||
QString borderColor = palette.color(QPalette::Shadow).name();
|
||||
#endif
|
||||
int sliderRadius = sliderHeight / 2;
|
||||
int handleWidth = (sliderHeight * 3) / 2 + (sliderHeight / 5);
|
||||
int handleRadius = handleWidth / 2 + 1;
|
||||
int handleOffset = handleRadius / 2;
|
||||
|
||||
//横向滑块
|
||||
list << QString("QSlider::horizontal{min-height:%1px;}").arg(sliderHeight * 2);
|
||||
list << QString("QSlider::groove:horizontal{background:%1;height:%2px;border-radius:%3px;}")
|
||||
.arg(normalColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::add-page:horizontal{background:%1;height:%2px;border-radius:%3px;}")
|
||||
.arg(normalColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::sub-page:horizontal{background:%1;height:%2px;border-radius:%3px;}")
|
||||
.arg(grooveColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::handle:horizontal{border:1px solid %5;width:%2px;margin-top:-%3px;margin-bottom:-%3px;border-radius:%4px;"
|
||||
"background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #FFFFFF,stop:0.8 %1);}")
|
||||
.arg(handleColor).arg(handleWidth).arg(handleOffset).arg(handleRadius).arg(borderColor);
|
||||
|
||||
//垂直滑块
|
||||
list << QString("QSlider::vertical{min-width:%1px;}").arg(sliderHeight * 2);
|
||||
list << QString("QSlider::groove:vertical{background:%1;width:%2px;border-radius:%3px;}")
|
||||
.arg(normalColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::add-page:vertical{background:%1;width:%2px;border-radius:%3px;}")
|
||||
.arg(grooveColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::sub-page:vertical{background:%1;width:%2px;border-radius:%3px;}")
|
||||
.arg(normalColor).arg(sliderHeight).arg(sliderRadius);
|
||||
list << QString("QSlider::handle:vertical{border:1px solid %5;height:%2px;margin-left:-%3px;margin-right:-%3px;border-radius:%4px;"
|
||||
"background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #FFFFFF,stop:0.8 %1);}")
|
||||
.arg(handleColor).arg(handleWidth).arg(handleOffset).arg(handleRadius).arg(borderColor);
|
||||
|
||||
qApp->setStyleSheet(list.join(""));
|
||||
}
|
||||
13
ui/core_helper/customstyle.h
Normal file
13
ui/core_helper/customstyle.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef CUSTOMSTYLE_H
|
||||
#define CUSTOMSTYLE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class CustomStyle
|
||||
{
|
||||
public:
|
||||
//全局样式比如放大选择器
|
||||
static void initStyle(int fontSize = 15, int radioButtonSize = 18, int checkBoxSize = 16, int sliderHeight = 13);
|
||||
};
|
||||
|
||||
#endif // CUSTOMSTYLE_H
|
||||
7
ui/core_helper/h3.pri
Normal file
7
ui/core_helper/h3.pri
Normal file
@@ -0,0 +1,7 @@
|
||||
unix:!macx {
|
||||
contains(QT_ARCH, arm) {
|
||||
contains(DEFINES, arma7) {
|
||||
INCLUDEPATH += /usr/local/openssl-1.0.2m-h3-gcc-4.9.2/include
|
||||
LIBS += -L/usr/local/openssl-1.0.2m-h3-gcc-4.9.2/lib -lssl -lcrypto
|
||||
LIBS += -L/usr/local/h3_rootfsv -lXdmcp
|
||||
}}}
|
||||
390
ui/core_helper/iconhelper.cpp
Normal file
390
ui/core_helper/iconhelper.cpp
Normal file
@@ -0,0 +1,390 @@
|
||||
#include "iconhelper.h"
|
||||
|
||||
IconHelper *IconHelper::iconFontAliBaBa = 0;
|
||||
IconHelper *IconHelper::iconFontAwesome = 0;
|
||||
IconHelper *IconHelper::iconFontAwesome6 = 0;
|
||||
IconHelper *IconHelper::iconFontWeather = 0;
|
||||
int IconHelper::iconFontIndex = -1;
|
||||
|
||||
void IconHelper::initFont()
|
||||
{
|
||||
static bool isInit = false;
|
||||
if (!isInit) {
|
||||
isInit = true;
|
||||
if (iconFontAliBaBa == 0) {
|
||||
iconFontAliBaBa = new IconHelper(":/font/iconfont.ttf", "iconfont");
|
||||
}
|
||||
if (iconFontAwesome == 0) {
|
||||
iconFontAwesome = new IconHelper(":/font/fontawesome-webfont.ttf", "FontAwesome");
|
||||
}
|
||||
if (iconFontAwesome6 == 0) {
|
||||
iconFontAwesome6 = new IconHelper(":/font/fa-regular-400.ttf", "Font Awesome 6 Pro Regular");
|
||||
}
|
||||
if (iconFontWeather == 0) {
|
||||
iconFontWeather = new IconHelper(":/font/pe-icon-set-weather.ttf", "pe-icon-set-weather");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IconHelper::setIconFontIndex(int index)
|
||||
{
|
||||
iconFontIndex = index;
|
||||
}
|
||||
|
||||
QFont IconHelper::getIconFontAliBaBa()
|
||||
{
|
||||
initFont();
|
||||
return iconFontAliBaBa->getIconFont();
|
||||
}
|
||||
|
||||
QFont IconHelper::getIconFontAwesome()
|
||||
{
|
||||
initFont();
|
||||
return iconFontAwesome->getIconFont();
|
||||
}
|
||||
|
||||
QFont IconHelper::getIconFontAwesome6()
|
||||
{
|
||||
initFont();
|
||||
return iconFontAwesome6->getIconFont();
|
||||
}
|
||||
|
||||
QFont IconHelper::getIconFontWeather()
|
||||
{
|
||||
initFont();
|
||||
return iconFontWeather->getIconFont();
|
||||
}
|
||||
|
||||
IconHelper *IconHelper::getIconHelper(int icon)
|
||||
{
|
||||
initFont();
|
||||
|
||||
//指定了字体索引则取对应索引的字体类
|
||||
//没指定则自动根据不同的字体的值选择对应的类
|
||||
//由于部分值范围冲突所以可以指定索引来取
|
||||
//fontawesome 0xf000-0xf2e0
|
||||
//fontawesome6 0xe000-0xe33d 0xf000-0xf8ff
|
||||
//iconfont 0xe501-0xe793 0xe8d5-0xea5d 0xeb00-0xec00
|
||||
//weather 0xe900-0xe9cf
|
||||
|
||||
IconHelper *iconHelper = iconFontAwesome;
|
||||
if (iconFontIndex < 0) {
|
||||
if ((icon >= 0xe501 && icon <= 0xe793) || (icon >= 0xe8d5 && icon <= 0xea5d) || (icon >= 0xeb00 && icon <= 0xec00)) {
|
||||
iconHelper = iconFontAliBaBa;
|
||||
}
|
||||
} else if (iconFontIndex == 0) {
|
||||
iconHelper = iconFontAliBaBa;
|
||||
} else if (iconFontIndex == 1) {
|
||||
iconHelper = iconFontAwesome;
|
||||
} else if (iconFontIndex == 2) {
|
||||
iconHelper = iconFontAwesome6;
|
||||
} else if (iconFontIndex == 3) {
|
||||
iconHelper = iconFontWeather;
|
||||
}
|
||||
|
||||
return iconHelper;
|
||||
}
|
||||
|
||||
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
|
||||
{
|
||||
getIconHelper(icon)->setIcon1(lab, icon, size);
|
||||
}
|
||||
|
||||
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
|
||||
{
|
||||
getIconHelper(icon)->setIcon1(btn, icon, size);
|
||||
}
|
||||
|
||||
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||
quint32 width, quint32 height, int flags)
|
||||
{
|
||||
getIconHelper(icon)->setPixmap1(btn, color, icon, size, width, height, flags);
|
||||
}
|
||||
|
||||
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
|
||||
quint32 width, quint32 height, int flags)
|
||||
{
|
||||
return getIconHelper(icon)->getPixmap1(color, icon, size, width, height, flags);
|
||||
}
|
||||
|
||||
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
|
||||
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
int icon = icons.first();
|
||||
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
|
||||
}
|
||||
|
||||
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
|
||||
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
int icon = icons.first();
|
||||
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
|
||||
}
|
||||
|
||||
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
|
||||
QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
int icon = icons.first();
|
||||
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
|
||||
}
|
||||
|
||||
|
||||
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
|
||||
{
|
||||
//判断图形字体是否存在,不存在则加入
|
||||
//这里暂时限制在同一个项目中只加载一次字体文件
|
||||
QFontDatabase fontDb;
|
||||
bool exist = false;//fontDb.families().contains(fontName);
|
||||
if (!exist && QFile(fontFile).exists()) {
|
||||
int fontId = fontDb.addApplicationFont(fontFile);
|
||||
QStringList listName = fontDb.applicationFontFamilies(fontId);
|
||||
if (listName.count() == 0) {
|
||||
qDebug() << QString("load %1 error").arg(fontName);
|
||||
}
|
||||
}
|
||||
|
||||
//再次判断是否包含字体名称防止加载失败
|
||||
if (fontDb.families().contains(fontName)) {
|
||||
iconFont = QFont(fontName);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
||||
iconFont.setHintingPreference(QFont::PreferNoHinting);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//根据不同的
|
||||
if (watched->inherits("QAbstractButton")) {
|
||||
QAbstractButton *btn = (QAbstractButton *)watched;
|
||||
int index = btns.indexOf(btn);
|
||||
if (index >= 0) {
|
||||
//不同的事件设置不同的图标,同时区分选中的和没有选中的
|
||||
int type = event->type();
|
||||
if (btn->isChecked()) {
|
||||
if (type == QEvent::MouseButtonPress) {
|
||||
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||
if (mouseEvent->button() == Qt::LeftButton) {
|
||||
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||
}
|
||||
} else if (type == QEvent::Enter) {
|
||||
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||
} else if (type == QEvent::Leave) {
|
||||
btn->setIcon(QIcon(pixChecked.at(index)));
|
||||
}
|
||||
} else {
|
||||
if (type == QEvent::MouseButtonPress) {
|
||||
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||
if (mouseEvent->button() == Qt::LeftButton) {
|
||||
btn->setIcon(QIcon(pixPressed.at(index)));
|
||||
}
|
||||
} else if (type == QEvent::Enter) {
|
||||
btn->setIcon(QIcon(pixHover.at(index)));
|
||||
} else if (type == QEvent::Leave) {
|
||||
btn->setIcon(QIcon(pixNormal.at(index)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
QFont IconHelper::getIconFont()
|
||||
{
|
||||
return this->iconFont;
|
||||
}
|
||||
|
||||
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
|
||||
{
|
||||
iconFont.setPixelSize(size);
|
||||
lab->setFont(iconFont);
|
||||
lab->setText((QChar)icon);
|
||||
}
|
||||
|
||||
void IconHelper::setIcon1(QAbstractButton *btn, int icon, quint32 size)
|
||||
{
|
||||
iconFont.setPixelSize(size);
|
||||
btn->setFont(iconFont);
|
||||
btn->setText((QChar)icon);
|
||||
}
|
||||
|
||||
void IconHelper::setPixmap1(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
|
||||
quint32 width, quint32 height, int flags)
|
||||
{
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void IconHelper::setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
QList<QAbstractButton *> list;
|
||||
foreach (QPushButton *btn, btns) {
|
||||
list << btn;
|
||||
}
|
||||
|
||||
setStyle(widget, list, icons, styleColor);
|
||||
}
|
||||
|
||||
void IconHelper::setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
QList<QAbstractButton *> list;
|
||||
foreach (QToolButton *btn, btns) {
|
||||
list << btn;
|
||||
}
|
||||
|
||||
setStyle(widget, list, icons, styleColor);
|
||||
}
|
||||
|
||||
void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
|
||||
{
|
||||
int btnCount = btns.count();
|
||||
int iconCount = icons.count();
|
||||
if (btnCount <= 0 || iconCount <= 0 || btnCount != iconCount) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString position = styleColor.position;
|
||||
quint32 btnWidth = styleColor.btnWidth;
|
||||
quint32 btnHeight = styleColor.btnHeight;
|
||||
quint32 iconSize = styleColor.iconSize;
|
||||
quint32 iconWidth = styleColor.iconWidth;
|
||||
quint32 iconHeight = styleColor.iconHeight;
|
||||
quint32 borderWidth = styleColor.borderWidth;
|
||||
|
||||
//根据不同的位置计算边框
|
||||
QString strBorder;
|
||||
if (position == "top") {
|
||||
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
|
||||
.arg(borderWidth).arg(borderWidth * 2);
|
||||
} else if (position == "right") {
|
||||
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
|
||||
.arg(borderWidth).arg(borderWidth * 2);
|
||||
} else if (position == "bottom") {
|
||||
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
|
||||
.arg(borderWidth).arg(borderWidth * 2);
|
||||
} else if (position == "left") {
|
||||
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
|
||||
.arg(borderWidth).arg(borderWidth * 2);
|
||||
}
|
||||
|
||||
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
|
||||
//如果图标在文字上面而设置的边框是 top bottom 也需要启用加深边框
|
||||
QStringList qss;
|
||||
if (styleColor.defaultBorder) {
|
||||
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
|
||||
.arg(position).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
|
||||
} else {
|
||||
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
|
||||
.arg(position).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
|
||||
}
|
||||
|
||||
//悬停+按下+选中
|
||||
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
||||
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor);
|
||||
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
||||
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor);
|
||||
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
|
||||
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor);
|
||||
|
||||
//窗体背景颜色+按钮背景颜色
|
||||
qss << QString("QWidget#%1{background:%2;}")
|
||||
.arg(widget->objectName()).arg(styleColor.normalBgColor);
|
||||
qss << QString("QWidget>QAbstractButton{border-width:0px;background-color:%1;color:%2;}")
|
||||
.arg(styleColor.normalBgColor).arg(styleColor.normalTextColor);
|
||||
qss << QString("QWidget>QAbstractButton:hover{background-color:%1;color:%2;}")
|
||||
.arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor);
|
||||
qss << QString("QWidget>QAbstractButton:pressed{background-color:%1;color:%2;}")
|
||||
.arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor);
|
||||
qss << QString("QWidget>QAbstractButton:checked{background-color:%1;color:%2;}")
|
||||
.arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor);
|
||||
|
||||
//按钮宽度高度
|
||||
if (btnWidth > 0) {
|
||||
qss << QString("QWidget>QAbstractButton{min-width:%1px;}").arg(btnWidth);
|
||||
}
|
||||
if (btnHeight > 0) {
|
||||
qss << QString("QWidget>QAbstractButton{min-height:%1px;}").arg(btnHeight);
|
||||
}
|
||||
|
||||
//设置样式表
|
||||
widget->setStyleSheet(qss.join(""));
|
||||
|
||||
//可能会重复调用设置所以先要移除上一次的
|
||||
for (int i = 0; i < btnCount; ++i) {
|
||||
for (int j = 0; j < this->btns.count(); j++) {
|
||||
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.removeAt(j);
|
||||
this->pixNormal.removeAt(j);
|
||||
this->pixHover.removeAt(j);
|
||||
this->pixPressed.removeAt(j);
|
||||
this->pixChecked.removeAt(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//存储对应按钮对象,方便鼠标移上去的时候切换图片
|
||||
int checkedIndex = -1;
|
||||
for (int i = 0; i < btnCount; ++i) {
|
||||
int icon = icons.at(i);
|
||||
QPixmap pixNormal = getPixmap1(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||
QPixmap pixHover = getPixmap1(styleColor.hoverTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||
QPixmap pixPressed = getPixmap1(styleColor.pressedTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||
QPixmap pixChecked = getPixmap1(styleColor.checkedTextColor, icon, iconSize, iconWidth, iconHeight);
|
||||
|
||||
//记住最后选中的按钮
|
||||
QAbstractButton *btn = btns.at(i);
|
||||
if (btn->isChecked()) {
|
||||
checkedIndex = i;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
189
ui/core_helper/iconhelper.h
Normal file
189
ui/core_helper/iconhelper.h
Normal file
@@ -0,0 +1,189 @@
|
||||
#ifndef ICONHELPER_H
|
||||
#define ICONHELPER_H
|
||||
|
||||
/**
|
||||
* 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
|
||||
* 1. 可传入多种图形字体文件,一个类通用所有图形字体。
|
||||
* 2. 默认已经内置了阿里巴巴图形字体FontAliBaBa、国际知名图形字体FontAwesome、天气图形字体FontWeather。
|
||||
* 3. 可设置 QLabel、QAbstractButton 文本为图形字体。
|
||||
* 4. 可设置图形字体作为 QAbstractButton 按钮图标。
|
||||
* 5. 内置万能的方法 getPixmap 将图形字体值转换为图片。
|
||||
* 6. 无论是设置文本、图标、图片等都可以设置图标的大小、尺寸、颜色等参数。
|
||||
* 7. 内置超级导航栏样式设置,将图形字体作为图标设置到按钮。
|
||||
* 8. 支持各种颜色设置比如正常颜色、悬停颜色、按下颜色、选中颜色。
|
||||
* 9. 可设置导航的位置为 left、right、top、bottom 四种。
|
||||
* 10. 可设置导航加深边框颜色和粗细大小。
|
||||
* 11. 导航面板的各种切换效果比如鼠标悬停、按下、选中等都自动处理掉样式设置。
|
||||
* 12. 全局静态方法,接口丰富,使用极其简单方便。
|
||||
*/
|
||||
|
||||
#include <QtGui>
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
#include <QtWidgets>
|
||||
#endif
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT IconHelper : public QObject
|
||||
#else
|
||||
class IconHelper : public QObject
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
//阿里巴巴图形字体类
|
||||
static IconHelper *iconFontAliBaBa;
|
||||
//FontAwesome图形字体类
|
||||
static IconHelper *iconFontAwesome;
|
||||
//FontAwesome6图形字体类
|
||||
static IconHelper *iconFontAwesome6;
|
||||
//天气图形字体类
|
||||
static IconHelper *iconFontWeather;
|
||||
//图形字体索引
|
||||
static int iconFontIndex;
|
||||
|
||||
public:
|
||||
//样式颜色结构体
|
||||
struct StyleColor {
|
||||
QString position; //位置 left right top bottom
|
||||
bool defaultBorder; //默认有边框
|
||||
|
||||
quint32 btnWidth; //按钮宽度
|
||||
quint32 btnHeight; //按钮高度
|
||||
|
||||
quint32 iconSize; //图标字体尺寸
|
||||
quint32 iconWidth; //图标图片宽度
|
||||
quint32 iconHeight; //图标图片高度
|
||||
|
||||
quint32 borderWidth; //边框宽度
|
||||
QString borderColor; //边框颜色
|
||||
|
||||
QString normalBgColor; //正常背景颜色
|
||||
QString normalTextColor; //正常文字颜色
|
||||
QString hoverBgColor; //悬停背景颜色
|
||||
QString hoverTextColor; //悬停文字颜色
|
||||
QString pressedBgColor; //按下背景颜色
|
||||
QString pressedTextColor; //按下文字颜色
|
||||
QString checkedBgColor; //选中背景颜色
|
||||
QString checkedTextColor; //选中文字颜色
|
||||
|
||||
StyleColor() {
|
||||
position = "left";
|
||||
defaultBorder = false;
|
||||
|
||||
btnWidth = 0;
|
||||
btnHeight = 0;
|
||||
|
||||
iconSize = 12;
|
||||
iconWidth = 15;
|
||||
iconHeight = 15;
|
||||
|
||||
borderWidth = 3;
|
||||
borderColor = "#029FEA";
|
||||
|
||||
normalBgColor = "#292F38";
|
||||
normalTextColor = "#54626F";
|
||||
hoverBgColor = "#40444D";
|
||||
hoverTextColor = "#FDFDFD";
|
||||
pressedBgColor = "#404244";
|
||||
pressedTextColor = "#FDFDFD";
|
||||
checkedBgColor = "#44494F";
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//初始化图形字体
|
||||
static void initFont();
|
||||
//设置引用图形字体文件索引
|
||||
static void setIconFontIndex(int index);
|
||||
|
||||
//获取图形字体
|
||||
static QFont getIconFontAliBaBa();
|
||||
static QFont getIconFontAwesome();
|
||||
static QFont getIconFontAwesome6();
|
||||
static QFont getIconFontWeather();
|
||||
|
||||
//根据值获取图形字体类
|
||||
static IconHelper *getIconHelper(int icon);
|
||||
|
||||
//设置图形字体到标签
|
||||
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:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
private:
|
||||
QFont iconFont; //图形字体
|
||||
QList<QAbstractButton *> btns; //按钮队列
|
||||
QList<QPixmap> pixNormal; //正常图片队列
|
||||
QList<QPixmap> pixHover; //悬停图片队列
|
||||
QList<QPixmap> pixPressed; //按下图片队列
|
||||
QList<QPixmap> pixChecked; //选中图片队列
|
||||
|
||||
private slots:
|
||||
//按钮选中状态切换处理
|
||||
void toggled(bool checked);
|
||||
|
||||
public:
|
||||
//获取图形字体
|
||||
QFont getIconFont();
|
||||
|
||||
//设置图形字体到标签
|
||||
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
|
||||
7
ui/core_helper/qrc/font.qrc
Normal file
7
ui/core_helper/qrc/font.qrc
Normal file
@@ -0,0 +1,7 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>font/fontawesome-webfont.ttf</file>
|
||||
<file>font/iconfont.ttf</file>
|
||||
<file>font/pe-icon-set-weather.ttf</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
BIN
ui/core_helper/qrc/font/DroidSansFallback.ttf
Normal file
BIN
ui/core_helper/qrc/font/DroidSansFallback.ttf
Normal file
Binary file not shown.
BIN
ui/core_helper/qrc/font/fontawesome-webfont.ttf
Normal file
BIN
ui/core_helper/qrc/font/fontawesome-webfont.ttf
Normal file
Binary file not shown.
BIN
ui/core_helper/qrc/font/iconfont.ttf
Normal file
BIN
ui/core_helper/qrc/font/iconfont.ttf
Normal file
Binary file not shown.
BIN
ui/core_helper/qrc/font/pe-icon-set-weather.ttf
Normal file
BIN
ui/core_helper/qrc/font/pe-icon-set-weather.ttf
Normal file
Binary file not shown.
5
ui/core_helper/qrc/image.qrc
Normal file
5
ui/core_helper/qrc/image.qrc
Normal file
@@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>image/bg_novideo.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
BIN
ui/core_helper/qrc/image/bg_novideo.png
Normal file
BIN
ui/core_helper/qrc/image/bg_novideo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
6
ui/core_helper/qrc/qm.qrc
Normal file
6
ui/core_helper/qrc/qm.qrc
Normal file
@@ -0,0 +1,6 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>qm/qt_zh_CN.qm</file>
|
||||
<file>qm/widgets.qm</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
BIN
ui/core_helper/qrc/qm/qt_zh_CN.qm
Normal file
BIN
ui/core_helper/qrc/qm/qt_zh_CN.qm
Normal file
Binary file not shown.
BIN
ui/core_helper/qrc/qm/widgets.qm
Normal file
BIN
ui/core_helper/qrc/qm/widgets.qm
Normal file
Binary file not shown.
5
ui/core_helper/qrc/wasm.qrc
Normal file
5
ui/core_helper/qrc/wasm.qrc
Normal file
@@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>font/DroidSansFallback.ttf</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
1310
ui/core_helper/qthelper.cpp
Normal file
1310
ui/core_helper/qthelper.cpp
Normal file
File diff suppressed because it is too large
Load Diff
188
ui/core_helper/qthelper.h
Normal file
188
ui/core_helper/qthelper.h
Normal file
@@ -0,0 +1,188 @@
|
||||
#ifndef QTHELPER_H
|
||||
#define QTHELPER_H
|
||||
|
||||
#include "head.h"
|
||||
|
||||
class QtHelper
|
||||
{
|
||||
public:
|
||||
//获取所有屏幕区域/当前鼠标所在屏幕索引/区域尺寸/缩放系数
|
||||
static QList<QRect> getScreenRects(bool available = true);
|
||||
static int getScreenIndex();
|
||||
static QRect getScreenRect(bool available = true);
|
||||
static qreal getScreenRatio(int index = -1, bool devicePixel = false);
|
||||
|
||||
//矫正当前鼠标所在屏幕居中尺寸
|
||||
static QRect checkCenterRect(QRect &rect, bool available = true);
|
||||
|
||||
//获取桌面宽度高度+居中显示
|
||||
static int deskWidth();
|
||||
static int deskHeight();
|
||||
static QSize deskSize();
|
||||
|
||||
//居中显示窗体
|
||||
//定义标志位指定是以桌面为参照还是主程序界面为参照
|
||||
static QWidget *centerBaseForm;
|
||||
static void setFormInCenter(QWidget *form);
|
||||
static void showForm(QWidget *form);
|
||||
|
||||
//程序文件名称和当前所在路径
|
||||
static QString appName();
|
||||
static QString appPath();
|
||||
|
||||
//程序最前面获取应用程序路径和名称
|
||||
static void getCurrentInfo(char *argv[], QString &path, QString &name);
|
||||
//程序最前面读取配置文件节点的值
|
||||
static QString getIniValue(const QString &fileName, const QString &key);
|
||||
static QString getIniValue(char *argv[], const QString &key, const QString &dir = QString(), const QString &file = QString());
|
||||
|
||||
//获取本地网卡IP集合
|
||||
static QStringList getLocalIPs();
|
||||
//添加网卡集合并根据默认值设置当前项
|
||||
static void initLocalIPs(QComboBox *cbox, const QString &defaultIP, bool local127 = true);
|
||||
|
||||
//获取内置颜色集合
|
||||
static QList<QColor> colors;
|
||||
static QList<QColor> getColorList();
|
||||
static QStringList getColorNames();
|
||||
//随机获取颜色集合中的颜色
|
||||
static QColor getRandColor();
|
||||
|
||||
//初始化随机数种子
|
||||
static void initRand();
|
||||
//获取随机小数
|
||||
static float getRandFloat(float min, float max);
|
||||
//获取随机数,指定最小值和最大值
|
||||
static double getRandValue(int min, int max, bool contansMin = false, bool contansMax = false);
|
||||
//获取范围值随机经纬度集合
|
||||
static QStringList getRandPoint(int count, float mainLng, float mainLat, float dotLng, float dotLat);
|
||||
//根据旧的范围值和值计算新的范围值对应的值
|
||||
static int getRangeValue(int oldMin, int oldMax, int oldValue, int newMin, int newMax);
|
||||
|
||||
//获取uuid
|
||||
static QString getUuid();
|
||||
//校验目录
|
||||
static void checkPath(const QString &dirName);
|
||||
//通用延时函数(支持Qt4 Qt5 Qt6)
|
||||
static void sleep(int msec, bool exec = true);
|
||||
//检查程序是否已经运行
|
||||
static void checkRun();
|
||||
|
||||
//设置Qt自带样式
|
||||
static void setStyle();
|
||||
//设置字体
|
||||
static QFont addFont(const QString &fontFile, const QString &fontName);
|
||||
static void setFont(int fontSize = 12);
|
||||
//设置编码
|
||||
static void setCode(bool utf8 = true);
|
||||
//设置翻译文件
|
||||
static void setTranslator(const QString &qmFile);
|
||||
|
||||
//动态设置权限
|
||||
static bool checkPermission(const QString &permission);
|
||||
//申请安卓权限
|
||||
static void initAndroidPermission();
|
||||
|
||||
//一次性设置所有包括编码样式字体等
|
||||
static void initAll(bool utf8 = true, bool style = true, int fontSize = 13);
|
||||
//初始化main函数最前面执行的一段代码
|
||||
static void initMain(bool desktopSettingsAware = false, bool use96Dpi = true, bool logCritical = true);
|
||||
//初始化opengl类型(1=AA_UseDesktopOpenGL 2=AA_UseOpenGLES 3=AA_UseSoftwareOpenGL)
|
||||
static void initOpenGL(quint8 type = 0, bool checkCardEnable = false, bool checkVirtualSystem = false);
|
||||
|
||||
//读取qss文件获取样式表内容
|
||||
static QString getStyle(const QString &qssFile);
|
||||
//设置qss文件到全局样式
|
||||
static void setStyle(const QString &qssFile);
|
||||
|
||||
//执行命令行返回执行结果
|
||||
static QString doCmd(const QString &program, const QStringList &arguments, int timeout = 1000);
|
||||
//获取显卡是否被禁用
|
||||
static bool isVideoCardEnable();
|
||||
//获取是否在虚拟机环境
|
||||
static bool isVirtualSystem();
|
||||
|
||||
//插入消息
|
||||
static bool replaceCRLF;
|
||||
static QVector<int> msgTypes;
|
||||
static QVector<QString> msgKeys;
|
||||
static QVector<QColor> msgColors;
|
||||
static QString appendMsg(QTextEdit *textEdit, int type, const QString &data,
|
||||
int maxCount, int ¤tCount,
|
||||
bool clear = false, bool pause = false);
|
||||
|
||||
//设置无边框
|
||||
static void setFramelessForm(QWidget *widgetMain, bool tool = false, bool top = false, bool menu = true);
|
||||
|
||||
//弹出框
|
||||
static int showMessageBox(const QString &text, int type = 0, int closeSec = 0, bool exec = false);
|
||||
//弹出消息框
|
||||
static void showMessageBoxInfo(const QString &text, int closeSec = 0, bool exec = false);
|
||||
//弹出错误框
|
||||
static void showMessageBoxError(const QString &text, int closeSec = 0, bool exec = false);
|
||||
//弹出询问框
|
||||
static int showMessageBoxQuestion(const QString &text);
|
||||
|
||||
//为什么还要自定义对话框因为可控宽高和汉化对应文本等
|
||||
//初始化对话框文本
|
||||
static void initDialog(QFileDialog *dialog, const QString &title, const QString &acceptName,
|
||||
const QString &dirName, bool native, int width, int height);
|
||||
//拿到对话框结果
|
||||
static QString getDialogResult(QFileDialog *dialog);
|
||||
//选择文件对话框
|
||||
static QString getOpenFileName(const QString &filter = QString(),
|
||||
const QString &dirName = QString(),
|
||||
const QString &fileName = QString(),
|
||||
bool native = false, int width = 900, int height = 600);
|
||||
//保存文件对话框
|
||||
static QString getSaveFileName(const QString &filter = QString(),
|
||||
const QString &dirName = QString(),
|
||||
const QString &fileName = QString(),
|
||||
bool native = false, int width = 900, int height = 600);
|
||||
//选择目录对话框
|
||||
static QString getExistingDirectory(const QString &dirName = QString(),
|
||||
bool native = false, int width = 900, int height = 600);
|
||||
|
||||
//异或加密-只支持字符,如果是中文需要将其转换base64编码
|
||||
static QString getXorEncryptDecrypt(const QString &value, char key);
|
||||
//异或校验
|
||||
static quint8 getOrCode(const QByteArray &data);
|
||||
//计算校验码
|
||||
static quint8 getCheckCode(const QByteArray &data);
|
||||
|
||||
//初始化表格
|
||||
static void initTableView(QTableView *tableView, int rowHeight = 25,
|
||||
bool headVisible = false, bool edit = false,
|
||||
bool stretchLast = true);
|
||||
//打开文件带提示框
|
||||
static void openFile(const QString &fileName, const QString &msg);
|
||||
|
||||
//检查ini配置文件
|
||||
static bool checkIniFile(const QString &iniFile);
|
||||
|
||||
//首尾截断字符串显示
|
||||
static QString cutString(const QString &text, int len, int left, int right, bool file, const QString &mid = "...");
|
||||
|
||||
//传入图片尺寸和窗体区域及边框大小返回居中区域(scaleMode: 0-自动调整 1-等比缩放 2-拉伸填充)
|
||||
static QRect getCenterRect(const QSize &imageSize, const QRect &widgetRect, int borderWidth = 2, int scaleMode = 0);
|
||||
//传入图片尺寸和窗体尺寸及缩放策略返回合适尺寸(scaleMode: 0-自动调整 1-等比缩放 2-拉伸填充)
|
||||
static void getScaledImage(QImage &image, const QSize &widgetSize, int scaleMode = 0, bool fast = true);
|
||||
|
||||
//毫秒数转时间 00:00
|
||||
static QString getTimeString(qint64 time);
|
||||
//用时时间转秒数
|
||||
static QString getTimeString(QElapsedTimer timer);
|
||||
//文件大小转 KB MB GB TB
|
||||
static QString getSizeString(quint64 size);
|
||||
|
||||
//设置系统时间
|
||||
static void setSystemDateTime(const QString &year, const QString &month, const QString &day,
|
||||
const QString &hour, const QString &min, const QString &sec);
|
||||
//设置开机自启动
|
||||
static void runWithSystem(bool autoRun = true);
|
||||
static void runWithSystem(const QString &fileName, const QString &filePath, bool autoRun = true);
|
||||
//启动运行程序(已经在运行则不启动)
|
||||
static void runBin(const QString &path, const QString &name);
|
||||
};
|
||||
|
||||
#endif // QTHELPER_H
|
||||
107
ui/core_helper/wasmhelper.cpp
Normal file
107
ui/core_helper/wasmhelper.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "wasmhelper.h"
|
||||
#include "qrect.h"
|
||||
#include "emscripten.h"
|
||||
#include "emscripten/html5.h"
|
||||
|
||||
//弹出js信息框
|
||||
EM_JS(void, showMessageJs, (const char *text), {
|
||||
alert(UTF8ToString(text));
|
||||
})
|
||||
|
||||
//弹出js输入框
|
||||
EM_JS(const char *, getInputJs, (const char *title, const char *defaultText), {
|
||||
var result = prompt(UTF8ToString(title), UTF8ToString(defaultText));
|
||||
if (!result)
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
return stringToNewUTF8(result);
|
||||
})
|
||||
|
||||
//打开iframe窗体
|
||||
EM_JS(void, openIframeJs, (const char *flag, const char *url, const char *style), {
|
||||
//如果存在则只移动位置
|
||||
var id = UTF8ToString(flag);
|
||||
var iframe = document.getElementById(id);
|
||||
if (iframe)
|
||||
{
|
||||
iframe.style = UTF8ToString(style);
|
||||
return;
|
||||
}
|
||||
|
||||
iframe = document.createElement('iframe');
|
||||
iframe.id = id;
|
||||
iframe.src = UTF8ToString(url);
|
||||
iframe.style = UTF8ToString(style);
|
||||
iframe.scrolling = 'no';
|
||||
document.body.appendChild(iframe);
|
||||
})
|
||||
|
||||
//加载iframe窗体
|
||||
EM_JS(void, reloadIframeJs, (const char *flag, const char *url), {
|
||||
var iframe = document.getElementById(UTF8ToString(flag));
|
||||
if (iframe)
|
||||
{
|
||||
iframe.src = UTF8ToString(url);
|
||||
}
|
||||
})
|
||||
|
||||
//移动iframe窗体
|
||||
EM_JS(void, moveIframeJs, (const char *flag, const char *style), {
|
||||
var iframe = document.getElementById(UTF8ToString(flag));
|
||||
if (iframe)
|
||||
{
|
||||
iframe.style = UTF8ToString(style);
|
||||
}
|
||||
})
|
||||
|
||||
//隐藏iframe窗体
|
||||
EM_JS(void, hideIframeJs, (const char *flag), {
|
||||
var iframe = document.getElementById(UTF8ToString(flag));
|
||||
if (iframe)
|
||||
{
|
||||
iframe.style = "display:none";
|
||||
}
|
||||
})
|
||||
|
||||
void WasmHelper::showMessage(const QString &text)
|
||||
{
|
||||
showMessageJs(text.toUtf8().constData());
|
||||
}
|
||||
|
||||
QString WasmHelper::getInput(const QString &title, const QString &text)
|
||||
{
|
||||
return getInputJs(title.toUtf8().constData(), text.toUtf8().constData());
|
||||
}
|
||||
|
||||
QString WasmHelper::getIframeStyle(const QRect &rect)
|
||||
{
|
||||
QString style = QString("border:0px;position:absolute;margin:0px;padding:0px;z-index:10000;opacity:1.0;");
|
||||
style += QString("left:%1px;").arg(rect.x());
|
||||
style += QString("top:%1px;").arg(rect.y());
|
||||
style += QString("width:%1px;").arg(rect.width());
|
||||
style += QString("height:%1px;").arg(rect.height());
|
||||
return style;
|
||||
}
|
||||
|
||||
void WasmHelper::openIframe(const QString &flag, const QString &url, const QRect &rect)
|
||||
{
|
||||
QString style = getIframeStyle(rect);
|
||||
openIframeJs(flag.toUtf8().constData(), url.toUtf8().constData(), style.toUtf8().constData());
|
||||
}
|
||||
|
||||
void WasmHelper::reloadIframe(const QString &flag, const QString &url)
|
||||
{
|
||||
reloadIframeJs(flag.toUtf8().constData(), url.toUtf8().constData());
|
||||
}
|
||||
|
||||
void WasmHelper::moveIframe(const QString &flag, const QRect &rect)
|
||||
{
|
||||
QString style = getIframeStyle(rect);
|
||||
moveIframeJs(flag.toUtf8().constData(), style.toUtf8().constData());
|
||||
}
|
||||
|
||||
void WasmHelper::hideIframe(const QString &flag)
|
||||
{
|
||||
hideIframeJs(flag.toUtf8().constData());
|
||||
}
|
||||
26
ui/core_helper/wasmhelper.h
Normal file
26
ui/core_helper/wasmhelper.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WASMHELPER_H
|
||||
#define WASMHELPER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class WasmHelper
|
||||
{
|
||||
public:
|
||||
//弹出js信息框
|
||||
static void showMessage(const QString &text);
|
||||
//弹出js输入框
|
||||
static QString getInput(const QString &title, const QString &text);
|
||||
|
||||
//获取iframe样式
|
||||
static QString getIframeStyle(const QRect &rect);
|
||||
//打开iframe窗体
|
||||
static void openIframe(const QString &flag, const QString &url, const QRect &rect);
|
||||
//重新加载iframe窗体
|
||||
static void reloadIframe(const QString &flag, const QString &url);
|
||||
//移动iframe窗体
|
||||
static void moveIframe(const QString &flag, const QRect &rect);
|
||||
//隐藏iframe窗体
|
||||
static void hideIframe(const QString &flag);
|
||||
};
|
||||
|
||||
#endif // WASMHELPER_H
|
||||
Reference in New Issue
Block a user