astyle.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * Breeze Stylesheets (c) Alexander Huszagh
  19. * https://github.com/Alexhuszagh/BreezeStyleSheets
  20. *
  21. * QDarksStyle Stylesheet (c) Colin Duquesnoy
  22. * https://github.com/ColinDuquesnoy/QDarkStyleSheet
  23. *
  24. * Dark Palette (c) 2017 by Juergen Skrotzky
  25. * https://github.com/Jorgen-VikingGod/Qt-Frameless-Window-DarkStyle
  26. *
  27. */
  28. #include "astyle.h"
  29. #include "src/opl.h"
  30. #include <QStyle>
  31. #include <QStyleFactory>
  32. #include <QApplication>
  33. #include <QFont>
  34. #include "src/classes/asettings.h"
  35. #include "src/functions/alog.h"
  36. const QString AStyle::defaultStyle = QLatin1String("Fusion");
  37. const QStringList AStyle::styles = QStyleFactory::keys();
  38. const QList<StyleSheet> AStyle::styleSheets = {
  39. {QLatin1String("Breeze"), QLatin1String(":breeze_light.qss")},
  40. {QLatin1String("Breeze-Dark"), QLatin1String(":breeze_dark.qss")},
  41. {QLatin1String("QDarkStyle"), QLatin1String(":qdarkstyle/qdarkstyle.qss")},
  42. };
  43. QString AStyle::currentStyle = defaultStyle;
  44. QLatin1String AStyle::DARK_PALETTE = QLatin1String("Dark-Palette");
  45. /*!
  46. * \brief Setup Application style by reading from openPilotLog.ini
  47. */
  48. void AStyle::setup()
  49. {
  50. if (!ASettings::read(ASettings::Main::SetupComplete).toBool()) // Use system default for first run
  51. return;
  52. // Set Font
  53. if (!ASettings::read(ASettings::Main::UseSystemFont).toBool()) {
  54. QFont font(ASettings::read(ASettings::Main::Font).toString());
  55. font.setPointSize(ASettings::read(ASettings::Main::FontSize).toUInt());
  56. qApp->setFont(font);
  57. LOG << "Application Font set: " << font.toString().split(',').first();
  58. }
  59. // Set style, stylesheet or palette
  60. QString style_setting = ASettings::read(ASettings::Main::Style).toString();
  61. if (style_setting == DARK_PALETTE) {
  62. AStyle::setStyle(AStyle::darkPalette());
  63. ASettings::write(ASettings::Main::Style, style_setting);
  64. return;
  65. }
  66. for (const auto &style_name : styles) {
  67. if (style_setting == style_name) {
  68. setStyle(style_name);
  69. currentStyle = style_name;
  70. return;
  71. }
  72. }
  73. for (const auto &style_sheet : styleSheets) {
  74. if (style_setting == style_sheet.styleSheetName) {
  75. setStyle(style_sheet);
  76. currentStyle = style_sheet.styleSheetName;
  77. return;
  78. }
  79. }
  80. }
  81. void AStyle::resetStyle()
  82. {
  83. qApp->setStyle(QStyleFactory::create(defaultStyle));
  84. qApp->setStyleSheet(QString());
  85. qApp->setPalette(qApp->style()->standardPalette());
  86. }
  87. void AStyle::setStyle(const QString &style_key)
  88. {
  89. resetStyle();
  90. LOG << "Setting style: " << style_key;
  91. QApplication::setStyle(QStyleFactory::create(style_key));
  92. currentStyle = style_key;
  93. }
  94. void AStyle::setStyle(const StyleSheet &style_sheet)
  95. {
  96. resetStyle();
  97. LOG << "Setting stylesheet: " << style_sheet.styleSheetName;
  98. qApp->setStyleSheet(read_stylesheet(style_sheet.fileName));
  99. currentStyle = style_sheet.styleSheetName;
  100. }
  101. void AStyle::setStyle(const QPalette &palette)
  102. {
  103. resetStyle();
  104. LOG << "Setting Colour Palette...";
  105. currentStyle = DARK_PALETTE;
  106. qApp->setPalette(palette);
  107. }
  108. StyleType AStyle::getStyleType()
  109. {
  110. const QStringList darkStyles = {
  111. QStringLiteral("Breeze-Dark"),
  112. QStringLiteral("QDarkStyle"),
  113. DARK_PALETTE,
  114. };
  115. if (darkStyles.contains(currentStyle))
  116. return StyleType::Dark;
  117. else
  118. return StyleType::Light;
  119. }
  120. QPalette AStyle::darkPalette()
  121. {
  122. auto palette = QPalette();
  123. //palette.setColor(QPalette::Window, QColor(53, 53, 53));
  124. //palette.setColor(QPalette::WindowText, Qt::white);
  125. //palette.setColor(QPalette::Base, QColor(25, 25, 25));
  126. //palette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
  127. //palette.setColor(QPalette::ToolTipBase, Qt::black);
  128. //palette.setColor(QPalette::ToolTipText, Qt::white);
  129. //palette.setColor(QPalette::Text, Qt::white);
  130. //palette.setColor(QPalette::Button, QColor(53, 53, 53));
  131. //palette.setColor(QPalette::ButtonText, Qt::white);
  132. //palette.setColor(QPalette::BrightText, Qt::red);
  133. //palette.setColor(QPalette::Link, QColor(42, 130, 218));
  134. //palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
  135. //palette.setColor(QPalette::HighlightedText, Qt::black);
  136. palette.setColor(QPalette::Window, QColor(53, 53, 53));
  137. palette.setColor(QPalette::WindowText, Qt::white);
  138. palette.setColor(QPalette::Disabled, QPalette::WindowText,
  139. QColor(127, 127, 127));
  140. palette.setColor(QPalette::Base, QColor(42, 42, 42));
  141. palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66));
  142. palette.setColor(QPalette::ToolTipBase, Qt::white);
  143. palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53));
  144. palette.setColor(QPalette::Text, Qt::white);
  145. palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127));
  146. palette.setColor(QPalette::Dark, QColor(35, 35, 35));
  147. palette.setColor(QPalette::Shadow, QColor(20, 20, 20));
  148. palette.setColor(QPalette::Button, QColor(53, 53, 53));
  149. palette.setColor(QPalette::ButtonText, Qt::white);
  150. palette.setColor(QPalette::Disabled, QPalette::ButtonText,
  151. QColor(127, 127, 127));
  152. palette.setColor(QPalette::BrightText, Qt::red);
  153. palette.setColor(QPalette::Link, QColor(42, 130, 218));
  154. palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
  155. palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80));
  156. palette.setColor(QPalette::HighlightedText, Qt::white);
  157. palette.setColor(QPalette::Disabled, QPalette::HighlightedText,
  158. QColor(127, 127, 127));
  159. return palette;
  160. }
  161. const QString& AStyle::style()
  162. {
  163. return currentStyle;
  164. }