StyleSheet.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "StyleSheet.h"
  2. #include "QFluentWidgets.h"
  3. #include <QFile>
  4. #include <QMap>
  5. #include <QTextCodec>
  6. #include <QTextStream>
  7. #include <QtMath>
  8. StyleSheetManager::StyleSheetManager() { }
  9. void StyleSheetManager::Register(const QString &file, QWidget *widget)
  10. {
  11. if (!m_widgets.contains(widget)) {
  12. connect(widget, &QWidget::destroyed, [=](QObject *obj) {
  13. QWidget *w = static_cast<QWidget *>(obj);
  14. StyleSheetManager::instance().DeRegister(w);
  15. });
  16. }
  17. m_widgets.insert(widget, file);
  18. }
  19. void StyleSheetManager::DeRegister(QWidget *widget)
  20. {
  21. if (!m_widgets.contains(widget)) {
  22. return;
  23. }
  24. m_widgets.remove(widget);
  25. }
  26. QHash<QWidget *, QString> StyleSheetManager::widgets() const
  27. {
  28. return m_widgets;
  29. }
  30. QString StyleSheetBase::getStyleSheet(const QString &file, Qfw::Theme theme)
  31. {
  32. QFile f(file);
  33. QString qss;
  34. if (f.open(QIODevice::ReadOnly)) {
  35. QTextCodec *codec = QTextCodec::codecForName("UTF8");
  36. qss = codec->toUnicode(f.readAll());
  37. f.close();
  38. }
  39. return qss;
  40. }
  41. void StyleSheetBase::setStyleSheet(QWidget *widget, const QString &file, Qfw::Theme theme, bool reg)
  42. {
  43. if (reg) {
  44. StyleSheetManager::instance().Register(file, widget);
  45. }
  46. widget->setStyleSheet(getStyleSheet(file, theme));
  47. }
  48. QString StyleSheetBase::applyThemeColor(const QString &qss)
  49. {
  50. // 模板替换
  51. const static QList<ThemeColor> mappings = { ThemeColor(ThemeColor::PRIMARY), ThemeColor(ThemeColor::DARK_1),
  52. ThemeColor(ThemeColor::DARK_2), ThemeColor(ThemeColor::DARK_3),
  53. ThemeColor(ThemeColor::LIGHT_1), ThemeColor(ThemeColor::LIGHT_2),
  54. ThemeColor(ThemeColor::LIGHT_3) };
  55. QStringList lines = qss.split("\r\n");
  56. QString result = "";
  57. for (auto line : lines) {
  58. int start = line.indexOf("--");
  59. if (start >= 0) {
  60. int end = -1;
  61. for (int i = start + 2; i < line.count(); ++i) {
  62. if (!line.at(i).isLetterOrNumber()) {
  63. break;
  64. }
  65. end = i;
  66. }
  67. if (end > start) {
  68. QString mask = line.mid(start + 2, end - start - 1);
  69. bool found = false;
  70. for (auto m : mappings) {
  71. if (m.type() == mask) {
  72. result += line.left(start) + m.name() + line.mid(end + 1) + "\r\n";
  73. found = true;
  74. break;
  75. }
  76. }
  77. if (!found) {
  78. result += line + "\r\n";
  79. }
  80. } else {
  81. result += line + "\r\n";
  82. }
  83. } else {
  84. result += line + "\r\n";
  85. }
  86. }
  87. return result;
  88. }
  89. QString FluentStyleSheet::typeName(FluentStyleSheet::Type t)
  90. {
  91. switch (t) {
  92. case MENU:
  93. return "menu";
  94. case BUTTON:
  95. return "button";
  96. case DIALOG:
  97. return "dialog";
  98. case SLIDER:
  99. return "slider";
  100. case INFO_BAR:
  101. return "info_bar";
  102. case SPIN_BOX:
  103. return "spin_box";
  104. case TOOL_TIP:
  105. return "tool_tip";
  106. case CHECK_BOX:
  107. return "check_box";
  108. case COMBO_BOX:
  109. return "combo_box";
  110. case LINE_EDIT:
  111. return "line_edit";
  112. case TREE_VIEW:
  113. return "tree_view";
  114. case TIME_PICKER:
  115. return "time_picker";
  116. case SETTING_CARD:
  117. return "setting_card";
  118. case COLOR_DIALOG:
  119. return "color_dialog";
  120. case SWITCH_BUTTON:
  121. return "switch_button";
  122. case MESSAGE_DIALOG:
  123. return "message_dialog";
  124. case STATE_TOOL_TIP:
  125. return "state_tool_tip";
  126. case FOLDER_LIST_DIALOG:
  127. return "folder_list_dialog";
  128. case SETTING_CARD_GROUP:
  129. return "setting_card_group";
  130. case EXPAND_SETTING_CARD:
  131. return "expand_setting_card";
  132. case NAVIGATION_INTERFACE:
  133. return "navigation_interface";
  134. }
  135. return "";
  136. }
  137. QString FluentStyleSheet::path(Qfw::Theme theme)
  138. {
  139. return "";
  140. }
  141. QString FluentStyleSheet::content(const QString &name)
  142. {
  143. QString fullName;
  144. if (QFWIns.isDarkTheme()) {
  145. fullName = QString(":/qfluentwidgets/qss/dark/%2.qss").arg(name.toLower());
  146. } else {
  147. fullName = QString(":/qfluentwidgets/qss/light/%2.qss").arg(name.toLower());
  148. }
  149. QFile f(fullName);
  150. QString qss = "";
  151. if (f.open(QIODevice::ReadOnly)) {
  152. // 用QTextStream读取样式文件不用区分文件编码 带bom也行
  153. QTextStream in(&f);
  154. in.setCodec("utf-8");
  155. while (!in.atEnd()) {
  156. QString line = in.readLine();
  157. qss += line + "\r\n";
  158. }
  159. f.close();
  160. }
  161. return qss;
  162. }
  163. QString FluentStyleSheet::operator[](FluentStyleSheet::Type t)
  164. {
  165. return typeName(t);
  166. }
  167. void FluentStyleSheet::apply(FluentStyleSheet::Type t, QWidget *widget)
  168. {
  169. QString name = typeName(t);
  170. if (!name.isEmpty()) {
  171. apply(name, widget);
  172. }
  173. }
  174. void FluentStyleSheet::apply(const QString &name, QWidget *widget)
  175. {
  176. QString fullName;
  177. if (QFWIns.isDarkTheme()) {
  178. fullName = QString(":/qfluentwidgets/qss/dark/%2.qss").arg(name.toLower());
  179. } else {
  180. fullName = QString(":/qfluentwidgets/qss/light/%2.qss").arg(name.toLower());
  181. }
  182. QFile f(fullName);
  183. QString qss = "";
  184. if (f.open(QIODevice::ReadOnly)) {
  185. // 用QTextStream读取样式文件不用区分文件编码 带bom也行
  186. QTextStream in(&f);
  187. in.setCodec("utf-8");
  188. while (!in.atEnd()) {
  189. QString line = in.readLine();
  190. qss += line + "\r\n";
  191. }
  192. f.close();
  193. }
  194. widget->setStyleSheet(applyThemeColor(qss));
  195. }
  196. ThemeColor::ThemeColor(ThemeColor::Type t) : m_type(t) { }
  197. QString ThemeColor::type()
  198. {
  199. switch (m_type) {
  200. case PRIMARY:
  201. return "ThemeColorPrimary";
  202. case DARK_1:
  203. return "ThemeColorDark1";
  204. case DARK_2:
  205. return "ThemeColorDark2";
  206. case DARK_3:
  207. return "ThemeColorDark3";
  208. case LIGHT_1:
  209. return "ThemeColorLight1";
  210. case LIGHT_2:
  211. return "ThemeColorLight2";
  212. case LIGHT_3:
  213. return "ThemeColorLight3";
  214. }
  215. return "";
  216. }
  217. QString ThemeColor::operator[](ThemeColor::Type t)
  218. {
  219. switch (t) {
  220. case PRIMARY:
  221. return "ThemeColorPrimary";
  222. case DARK_1:
  223. return "ThemeColorDark1";
  224. case DARK_2:
  225. return "ThemeColorDark2";
  226. case DARK_3:
  227. return "ThemeColorDark3";
  228. case LIGHT_1:
  229. return "ThemeColorLight1";
  230. case LIGHT_2:
  231. return "ThemeColorLight2";
  232. case LIGHT_3:
  233. return "ThemeColorLight3";
  234. }
  235. return "";
  236. }
  237. QString ThemeColor::name() const
  238. {
  239. return color().name();
  240. }
  241. QColor ThemeColor::color() const
  242. {
  243. QColor themeColor = QFWIns.themeColor();
  244. // transform color into hsv space
  245. qreal h, s, v;
  246. themeColor.getHsvF(&h, &s, &v);
  247. if (QFWIns.isDarkTheme()) {
  248. s *= 0.84;
  249. v = 1;
  250. if (m_type == DARK_1) {
  251. v *= 0.9;
  252. } else if (m_type == DARK_2) {
  253. s *= 0.977;
  254. v *= 0.82;
  255. } else if (m_type == DARK_3) {
  256. s *= 0.95;
  257. v *= 0.7;
  258. } else if (m_type == LIGHT_1) {
  259. s *= 0.92;
  260. } else if (m_type == LIGHT_2) {
  261. s *= 0.78;
  262. } else if (m_type == LIGHT_3) {
  263. s *= 0.65;
  264. }
  265. } else {
  266. if (m_type == DARK_1) {
  267. v *= 0.75;
  268. } else if (m_type == DARK_2) {
  269. s *= 1.05;
  270. v *= 0.5;
  271. } else if (m_type == DARK_3) {
  272. s *= 1.1;
  273. v *= 0.4;
  274. } else if (m_type == LIGHT_1) {
  275. v *= 1.05;
  276. } else if (m_type == LIGHT_2) {
  277. s *= 0.75;
  278. v *= 1.05;
  279. } else if (m_type == LIGHT_3) {
  280. s *= 0.65;
  281. v *= 1.05;
  282. }
  283. }
  284. return QColor::fromHsvF(h, qMin(s, 1.), qMin(v, 1.));
  285. }
  286. QColor themeColor()
  287. {
  288. return ThemeColor(ThemeColor::PRIMARY).color();
  289. }
  290. void setThemeColor(const QColor &color, bool save) { }