iconhelper.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef ICONHELPER_H
  2. #define ICONHELPER_H
  3. /**
  4. * 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
  5. * 1. 可传入多种图形字体文件,一个类通用所有图形字体。
  6. * 2. 默认已经内置了阿里巴巴图形字体FontAliBaBa、国际知名图形字体FontAwesome、天气图形字体FontWeather。
  7. * 3. 可设置 QLabel、QAbstractButton 文本为图形字体。
  8. * 4. 可设置图形字体作为 QAbstractButton 按钮图标。
  9. * 5. 内置万能的方法 getPixmap 将图形字体值转换为图片。
  10. * 6. 无论是设置文本、图标、图片等都可以设置图标的大小、尺寸、颜色等参数。
  11. * 7. 内置超级导航栏样式设置,将图形字体作为图标设置到按钮。
  12. * 8. 支持各种颜色设置比如正常颜色、悬停颜色、按下颜色、选中颜色。
  13. * 9. 可设置导航的位置为 left、right、top、bottom 四种。
  14. * 10. 可设置导航加深边框颜色和粗细大小。
  15. * 11. 导航面板的各种切换效果比如鼠标悬停、按下、选中等都自动处理掉样式设置。
  16. * 12. 全局静态方法,接口丰富,使用极其简单方便。
  17. */
  18. #include <QtGui>
  19. #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
  20. // #include <QtWidgets>
  21. #include <QtWidgets/QtWidgets>
  22. #endif
  23. #ifdef quc
  24. class Q_DECL_EXPORT IconHelper : public QObject
  25. #else
  26. class IconHelper : public QObject
  27. #endif
  28. {
  29. Q_OBJECT
  30. private:
  31. //阿里巴巴图形字体类
  32. static IconHelper *iconFontAliBaBa;
  33. //FontAwesome图形字体类
  34. static IconHelper *iconFontAwesome;
  35. //FontAwesome6图形字体类
  36. static IconHelper *iconFontAwesome6;
  37. //天气图形字体类
  38. static IconHelper *iconFontWeather;
  39. //图形字体索引
  40. static int iconFontIndex;
  41. public:
  42. //样式颜色结构体
  43. struct StyleColor {
  44. QString position; //位置 left right top bottom
  45. bool defaultBorder; //默认有边框
  46. quint32 iconSize; //图标字体尺寸
  47. quint32 iconWidth; //图标图片宽度
  48. quint32 iconHeight; //图标图片高度
  49. quint32 borderWidth; //边框宽度
  50. QString borderColor; //边框颜色
  51. QString normalBgColor; //正常背景颜色
  52. QString normalTextColor; //正常文字颜色
  53. QString hoverBgColor; //悬停背景颜色
  54. QString hoverTextColor; //悬停文字颜色
  55. QString pressedBgColor; //按下背景颜色
  56. QString pressedTextColor; //按下文字颜色
  57. QString checkedBgColor; //选中背景颜色
  58. QString checkedTextColor; //选中文字颜色
  59. StyleColor() {
  60. position = "left";
  61. defaultBorder = false;
  62. iconSize = 12;
  63. iconWidth = 15;
  64. iconHeight = 15;
  65. borderWidth = 3;
  66. borderColor = "#029FEA";
  67. normalBgColor = "#292F38";
  68. normalTextColor = "#54626F";
  69. hoverBgColor = "#40444D";
  70. hoverTextColor = "#FDFDFD";
  71. pressedBgColor = "#404244";
  72. pressedTextColor = "#FDFDFD";
  73. checkedBgColor = "#44494F";
  74. checkedTextColor = "#FDFDFD";
  75. }
  76. //设置常规颜色 普通状态+加深状态
  77. void setColor(const QString &normalBgColor,
  78. const QString &normalTextColor,
  79. const QString &darkBgColor,
  80. const QString &darkTextColor) {
  81. this->normalBgColor = normalBgColor;
  82. this->normalTextColor = normalTextColor;
  83. this->hoverBgColor = darkBgColor;
  84. this->hoverTextColor = darkTextColor;
  85. this->pressedBgColor = darkBgColor;
  86. this->pressedTextColor = darkTextColor;
  87. this->checkedBgColor = darkBgColor;
  88. this->checkedTextColor = darkTextColor;
  89. }
  90. };
  91. //初始化图形字体
  92. static void initFont();
  93. //设置引用图形字体文件索引
  94. static void setIconFontIndex(int index);
  95. //获取图形字体
  96. static QFont getIconFontAliBaBa();
  97. static QFont getIconFontAwesome();
  98. static QFont getIconFontAwesome6();
  99. static QFont getIconFontWeather();
  100. //根据值获取图形字体类
  101. static IconHelper *getIconHelper(int icon);
  102. //设置图形字体到标签
  103. static void setIcon(QLabel *lab, int icon, quint32 size = 12);
  104. //设置图形字体到按钮
  105. static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
  106. //设置图形字体到图标
  107. static void setPixmap(QAbstractButton *btn, const QColor &color,
  108. int icon, quint32 size = 12,
  109. quint32 width = 15, quint32 height = 15,
  110. int flags = Qt::AlignCenter);
  111. //获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
  112. static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
  113. quint32 width = 15, quint32 height = 15,
  114. int flags = Qt::AlignCenter);
  115. //指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
  116. static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
  117. static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
  118. static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
  119. //默认构造函数,传入字体文件+字体名称
  120. explicit IconHelper(const QString &fontFile, const QString &fontName, QObject *parent = 0);
  121. protected:
  122. bool eventFilter(QObject *watched, QEvent *event);
  123. private:
  124. QFont iconFont; //图形字体
  125. QList<QAbstractButton *> btns; //按钮队列
  126. QList<QPixmap> pixNormal; //正常图片队列
  127. QList<QPixmap> pixHover; //悬停图片队列
  128. QList<QPixmap> pixPressed; //按下图片队列
  129. QList<QPixmap> pixChecked; //选中图片队列
  130. private slots:
  131. //按钮选中状态切换处理
  132. void toggled(bool checked);
  133. public:
  134. //获取图形字体
  135. QFont getIconFont();
  136. //设置图形字体到标签
  137. void setIcon1(QLabel *lab, int icon, quint32 size = 12);
  138. //设置图形字体到按钮
  139. void setIcon1(QAbstractButton *btn, int icon, quint32 size = 12);
  140. //设置图形字体到图标
  141. void setPixmap1(QAbstractButton *btn, const QColor &color,
  142. int icon, quint32 size = 12,
  143. quint32 width = 15, quint32 height = 15,
  144. int flags = Qt::AlignCenter);
  145. //获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
  146. QPixmap getPixmap1(const QColor &color, int icon, quint32 size = 12,
  147. quint32 width = 15, quint32 height = 15,
  148. int flags = Qt::AlignCenter);
  149. //指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
  150. void setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
  151. void setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
  152. void setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
  153. };
  154. #endif // ICONHELPER_H