162 lines
4.8 KiB
C++
162 lines
4.8 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2015 The Bitcoin Core developers
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "platformstyle.h"
|
|
|
|
#include "guiconstants.h"
|
|
|
|
#include <QApplication>
|
|
#include <QColor>
|
|
#include <QIcon>
|
|
#include <QImage>
|
|
#include <QPalette>
|
|
#include <QPixmap>
|
|
|
|
static const struct {
|
|
const char *platformId;
|
|
/** Show images on push buttons */
|
|
const bool imagesOnButtons;
|
|
/** Colorize single-color icons */
|
|
const bool colorizeIcons;
|
|
/** Extra padding/spacing in transactionview */
|
|
const bool useExtraSpacing;
|
|
} platform_styles[] = {
|
|
{"macosx", false, false, true},
|
|
{"windows", true, false, false},
|
|
/* Other: linux, unix, ... */
|
|
{"other", true, true, false}
|
|
};
|
|
static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
|
|
|
|
namespace {
|
|
/* Local functions for colorizing single-color images */
|
|
|
|
void MakeSingleColorImage(QImage& img, const QColor& colorbase)
|
|
{
|
|
img = img.convertToFormat(QImage::Format_ARGB32);
|
|
for (int x = img.width(); x--; )
|
|
{
|
|
for (int y = img.height(); y--; )
|
|
{
|
|
const QRgb rgb = img.pixel(x, y);
|
|
img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
|
|
}
|
|
}
|
|
}
|
|
|
|
QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
|
|
{
|
|
QIcon new_ico;
|
|
QSize sz;
|
|
Q_FOREACH(sz, ico.availableSizes())
|
|
{
|
|
QImage img(ico.pixmap(sz).toImage());
|
|
MakeSingleColorImage(img, colorbase);
|
|
new_ico.addPixmap(QPixmap::fromImage(img));
|
|
}
|
|
return new_ico;
|
|
}
|
|
|
|
QImage ColorizeImage(const QString& filename, const QColor& colorbase)
|
|
{
|
|
QImage img(filename);
|
|
MakeSingleColorImage(img, colorbase);
|
|
return img;
|
|
}
|
|
|
|
QIcon ColorizeIcon(const QString& filename, const QColor& colorbase)
|
|
{
|
|
return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase)));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
PlatformStyle::PlatformStyle(const QString &name, bool imagesOnButtons, bool colorizeIcons, bool useExtraSpacing):
|
|
name(name),
|
|
imagesOnButtons(imagesOnButtons),
|
|
colorizeIcons(colorizeIcons),
|
|
useExtraSpacing(useExtraSpacing),
|
|
singleColor(0,0,0),
|
|
textColor(0,0,0)
|
|
{
|
|
// Determine icon highlighting color
|
|
if (colorizeIcons) {
|
|
const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
|
|
const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
|
|
const QColor colorText(QApplication::palette().color(QPalette::WindowText));
|
|
const int colorTextLightness = colorText.lightness();
|
|
QColor colorbase;
|
|
if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
|
|
colorbase = colorHighlightBg;
|
|
else
|
|
colorbase = colorHighlightFg;
|
|
singleColor = colorbase;
|
|
}
|
|
// Determine text color
|
|
textColor = QColor(QApplication::palette().color(QPalette::WindowText));
|
|
}
|
|
|
|
QImage PlatformStyle::SingleColorImage(const QString& filename) const
|
|
{
|
|
if (!colorizeIcons)
|
|
return QImage(filename);
|
|
return ColorizeImage(filename, SingleColor());
|
|
}
|
|
|
|
QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
|
|
{
|
|
if (!colorizeIcons)
|
|
return QIcon(filename);
|
|
return ColorizeIcon(filename, SingleColor());
|
|
}
|
|
|
|
QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
|
|
{
|
|
if (!colorizeIcons)
|
|
return icon;
|
|
return ColorizeIcon(icon, SingleColor());
|
|
}
|
|
|
|
QIcon PlatformStyle::TextColorIcon(const QString& filename) const
|
|
{
|
|
return ColorizeIcon(filename, TextColor());
|
|
}
|
|
|
|
QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
|
|
{
|
|
return ColorizeIcon(icon, TextColor());
|
|
}
|
|
|
|
const PlatformStyle *PlatformStyle::instantiate(const QString &platformId)
|
|
{
|
|
for (unsigned x=0; x<platform_styles_count; ++x)
|
|
{
|
|
if (platformId == platform_styles[x].platformId)
|
|
{
|
|
return new PlatformStyle(
|
|
platform_styles[x].platformId,
|
|
platform_styles[x].imagesOnButtons,
|
|
platform_styles[x].colorizeIcons,
|
|
platform_styles[x].useExtraSpacing);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|