astyle.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 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. /*
  19. * Stylesheets used (c) Alexander Huszagh
  20. * https://github.com/Alexhuszagh/BreezeStyleSheets
  21. */
  22. #include "astyle.h"
  23. #include "src/opl.h"
  24. #include <QStyle>
  25. #include <QStyleFactory>
  26. #include <QApplication>
  27. #include <QFont>
  28. #include "src/classes/asettings.h"
  29. #include "src/functions/alog.h"
  30. const QString AStyle::defaultStyle = QLatin1String("Fusion");
  31. const QStringList AStyle::styles = QStyleFactory::keys();
  32. const QList<StyleSheet> AStyle::styleSheets = {
  33. {QLatin1String("Breeze"), QLatin1String(":breeze_light.qss")},
  34. {QLatin1String("Breeze-Dark"), QLatin1String(":breeze_dark.qss")},
  35. {QLatin1String("QDarkStyle"), QLatin1String(":qdarkstyle/qdarkstyle.qss")},
  36. };
  37. QString AStyle::currentStyle = defaultStyle;
  38. /*!
  39. * \brief Setup Application style by reading from openPilotLog.ini
  40. */
  41. void AStyle::setup()
  42. {
  43. // Set Font
  44. if (!ASettings::read(ASettings::Main::UseSystemFont).toBool()) {
  45. QFont font(ASettings::read(ASettings::Main::Font).toString());
  46. font.setPointSize(ASettings::read(ASettings::Main::FontSize).toUInt());
  47. qApp->setFont(font);
  48. LOG << "Application Font set: " << font.toString().splitRef(',').first();
  49. }
  50. // Set style, stylesheet or palette
  51. QString style_setting = ASettings::read(ASettings::Main::Style).toString();
  52. if (style_setting == QLatin1String("Dark-Palette")) {
  53. AStyle::setStyle(AStyle::darkPalette());
  54. ASettings::write(ASettings::Main::Style, style_setting);
  55. return;
  56. }
  57. for (const auto &style_name : styles) {
  58. if (style_setting == style_name) {
  59. setStyle(style_name);
  60. currentStyle = style_name;
  61. return;
  62. }
  63. }
  64. for (const auto &style_sheet : styleSheets) {
  65. if (style_setting == style_sheet.styleSheetName) {
  66. setStyle(style_sheet);
  67. currentStyle = style_sheet.styleSheetName;
  68. return;
  69. }
  70. }
  71. }
  72. void AStyle::resetStyle()
  73. {
  74. qApp->setStyle(QStyleFactory::create(defaultStyle));
  75. qApp->setStyleSheet(QString());
  76. qApp->setPalette(qApp->style()->standardPalette());
  77. }
  78. void AStyle::setStyle(const QString &style_key)
  79. {
  80. resetStyle();
  81. LOG << "Setting style: " << style_key;
  82. QApplication::setStyle(QStyleFactory::create(style_key));
  83. currentStyle = style_key;
  84. }
  85. void AStyle::setStyle(const StyleSheet &style_sheet)
  86. {
  87. resetStyle();
  88. LOG << "Setting stylesheet: " << style_sheet.styleSheetName;
  89. qApp->setStyleSheet(read_stylesheet(style_sheet.fileName));
  90. currentStyle = style_sheet.styleSheetName;
  91. }
  92. void AStyle::setStyle(const QPalette &palette)
  93. {
  94. resetStyle();
  95. LOG << "Setting Colour Palette...";
  96. qApp->setPalette(palette);
  97. }
  98. QPalette AStyle::darkPalette()
  99. {
  100. auto palette = QPalette();
  101. palette.setColor(QPalette::Window, QColor(53, 53, 53));
  102. palette.setColor(QPalette::WindowText, Qt::white);
  103. palette.setColor(QPalette::Base, QColor(25, 25, 25));
  104. palette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
  105. palette.setColor(QPalette::ToolTipBase, Qt::black);
  106. palette.setColor(QPalette::ToolTipText, Qt::white);
  107. palette.setColor(QPalette::Text, Qt::white);
  108. palette.setColor(QPalette::Button, QColor(53, 53, 53));
  109. palette.setColor(QPalette::ButtonText, Qt::white);
  110. palette.setColor(QPalette::BrightText, Qt::red);
  111. palette.setColor(QPalette::Link, QColor(42, 130, 218));
  112. palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
  113. palette.setColor(QPalette::HighlightedText, Qt::black);
  114. return palette;
  115. }
  116. const QString& AStyle::style()
  117. {
  118. return currentStyle;
  119. }