You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

platformstyle.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2015-2016 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "platformstyle.h"
  5. #include "guiconstants.h"
  6. #include <QApplication>
  7. #include <QColor>
  8. #include <QIcon>
  9. #include <QImage>
  10. #include <QPalette>
  11. #include <QPixmap>
  12. static const struct {
  13. const char *platformId;
  14. /** Show images on push buttons */
  15. const bool imagesOnButtons;
  16. /** Colorize single-color icons */
  17. const bool colorizeIcons;
  18. /** Extra padding/spacing in transactionview */
  19. const bool useExtraSpacing;
  20. } platform_styles[] = {
  21. {"macosx", false, false, true},
  22. {"windows", true, false, false},
  23. /* Other: linux, unix, ... */
  24. {"other", true, true, false}
  25. };
  26. static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
  27. namespace {
  28. /* Local functions for colorizing single-color images */
  29. void MakeSingleColorImage(QImage& img, const QColor& colorbase)
  30. {
  31. img = img.convertToFormat(QImage::Format_ARGB32);
  32. for (int x = img.width(); x--; )
  33. {
  34. for (int y = img.height(); y--; )
  35. {
  36. const QRgb rgb = img.pixel(x, y);
  37. img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
  38. }
  39. }
  40. }
  41. QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
  42. {
  43. QIcon new_ico;
  44. Q_FOREACH(const QSize sz, ico.availableSizes())
  45. {
  46. QImage img(ico.pixmap(sz).toImage());
  47. MakeSingleColorImage(img, colorbase);
  48. new_ico.addPixmap(QPixmap::fromImage(img));
  49. }
  50. return new_ico;
  51. }
  52. QImage ColorizeImage(const QString& filename, const QColor& colorbase)
  53. {
  54. QImage img(filename);
  55. MakeSingleColorImage(img, colorbase);
  56. return img;
  57. }
  58. QIcon ColorizeIcon(const QString& filename, const QColor& colorbase)
  59. {
  60. return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase)));
  61. }
  62. }
  63. PlatformStyle::PlatformStyle(const QString &_name, bool _imagesOnButtons, bool _colorizeIcons, bool _useExtraSpacing):
  64. name(_name),
  65. imagesOnButtons(_imagesOnButtons),
  66. colorizeIcons(_colorizeIcons),
  67. useExtraSpacing(_useExtraSpacing),
  68. singleColor(0,0,0),
  69. textColor(0,0,0)
  70. {
  71. // Determine icon highlighting color
  72. if (colorizeIcons) {
  73. const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
  74. const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
  75. const QColor colorText(QApplication::palette().color(QPalette::WindowText));
  76. const int colorTextLightness = colorText.lightness();
  77. QColor colorbase;
  78. if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
  79. colorbase = colorHighlightBg;
  80. else
  81. colorbase = colorHighlightFg;
  82. singleColor = colorbase;
  83. }
  84. // Determine text color
  85. textColor = QColor(QApplication::palette().color(QPalette::WindowText));
  86. }
  87. QImage PlatformStyle::SingleColorImage(const QString& filename) const
  88. {
  89. if (!colorizeIcons)
  90. return QImage(filename);
  91. return ColorizeImage(filename, SingleColor());
  92. }
  93. QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
  94. {
  95. if (!colorizeIcons)
  96. return QIcon(filename);
  97. return ColorizeIcon(filename, SingleColor());
  98. }
  99. QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
  100. {
  101. if (!colorizeIcons)
  102. return icon;
  103. return ColorizeIcon(icon, SingleColor());
  104. }
  105. QIcon PlatformStyle::TextColorIcon(const QString& filename) const
  106. {
  107. return ColorizeIcon(filename, TextColor());
  108. }
  109. QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
  110. {
  111. return ColorizeIcon(icon, TextColor());
  112. }
  113. const PlatformStyle *PlatformStyle::instantiate(const QString &platformId)
  114. {
  115. for (unsigned x=0; x<platform_styles_count; ++x)
  116. {
  117. if (platformId == platform_styles[x].platformId)
  118. {
  119. return new PlatformStyle(
  120. platform_styles[x].platformId,
  121. platform_styles[x].imagesOnButtons,
  122. platform_styles[x].colorizeIcons,
  123. platform_styles[x].useExtraSpacing);
  124. }
  125. }
  126. return 0;
  127. }