2019-04-24 23:43:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
**
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "stylehelper.h"
|
|
|
|
|
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QCommonStyle>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QWindow>
|
|
|
|
#include <qmath.h>
|
|
|
|
|
|
|
|
// Clamps float color values within (0, 255)
|
|
|
|
static int clamp(float x) {
|
|
|
|
const int val = x > 255 ? 255 : static_cast<int>(x);
|
|
|
|
return val < 0 ? 0 : val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamps float color values within (0, 255)
|
|
|
|
/*
|
|
|
|
static int range(float x, int min, int max) {
|
|
|
|
int val = x > max ? max : x;
|
|
|
|
return val < min ? min : val;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
QColor StyleHelper::m_baseColor;
|
|
|
|
QColor StyleHelper::m_requestedBaseColor;
|
2021-07-11 09:49:38 +02:00
|
|
|
QColor StyleHelper::m_IconsBaseColor = 0xffdcdcdc;
|
|
|
|
QColor StyleHelper::m_IconsDisabledColor = 0x88a0a0a0;
|
2019-04-24 23:43:09 +02:00
|
|
|
QColor StyleHelper::m_ProgressBarTitleColor = 0xffffffff;
|
|
|
|
|
|
|
|
QColor StyleHelper::mergedColors(const QColor &colorA, const QColor &colorB, int factor) {
|
|
|
|
|
|
|
|
const int maxFactor = 100;
|
|
|
|
QColor tmp = colorA;
|
|
|
|
tmp.setRed((tmp.red() * factor) / maxFactor + (colorB.red() * (maxFactor - factor)) / maxFactor);
|
|
|
|
tmp.setGreen((tmp.green() * factor) / maxFactor + (colorB.green() * (maxFactor - factor)) / maxFactor);
|
|
|
|
tmp.setBlue((tmp.blue() * factor) / maxFactor + (colorB.blue() * (maxFactor - factor)) / maxFactor);
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::alphaBlendedColors(const QColor &colorA, const QColor &colorB) {
|
|
|
|
|
|
|
|
const int alpha = colorB.alpha();
|
|
|
|
const int antiAlpha = 255 - alpha;
|
|
|
|
|
|
|
|
return QColor(
|
|
|
|
(colorA.red() * antiAlpha + colorB.red() * alpha) / 255,
|
|
|
|
(colorA.green() * antiAlpha + colorB.green() * alpha) / 255,
|
|
|
|
(colorA.blue() * antiAlpha + colorB.blue() * alpha) / 255
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal StyleHelper::sidebarFontSize() {
|
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
return 10;
|
|
|
|
#else
|
|
|
|
return 7.5;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::notTooBrightHighlightColor() {
|
|
|
|
|
|
|
|
QColor highlightColor = QApplication::palette().highlight().color();
|
2021-08-23 21:21:08 +02:00
|
|
|
if (0.5 * highlightColor.saturationF() + 0.75 - highlightColor.valueF() < 0) {
|
2019-04-24 23:43:09 +02:00
|
|
|
highlightColor.setHsvF(highlightColor.hsvHueF(), 0.1 + highlightColor.saturationF() * 2.0, highlightColor.valueF());
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
return highlightColor;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QPalette StyleHelper::sidebarFontPalette(const QPalette &original) {
|
|
|
|
|
|
|
|
QPalette palette = original;
|
|
|
|
const QColor textColor = m_ProgressBarTitleColor;
|
|
|
|
palette.setColor(QPalette::WindowText, textColor);
|
|
|
|
palette.setColor(QPalette::Text, textColor);
|
|
|
|
return palette;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::panelTextColor(bool lightColored) {
|
|
|
|
|
2021-08-23 21:21:08 +02:00
|
|
|
if (lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
return Qt::black;
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qt::white;
|
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::baseColor(bool lightColored) {
|
|
|
|
|
2021-08-23 21:21:08 +02:00
|
|
|
if (lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
return m_baseColor.lighter(230);
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_baseColor;
|
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::highlightColor(bool lightColored) {
|
|
|
|
|
|
|
|
QColor result = baseColor(lightColored);
|
2021-08-23 21:21:08 +02:00
|
|
|
if (lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
result.setHsv(result.hue(), clamp(result.saturation()), clamp(result.value() * 1.06));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.setHsv(result.hue(), clamp(result.saturation()), clamp(result.value() * 1.16));
|
|
|
|
}
|
|
|
|
|
2019-04-24 23:43:09 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::shadowColor(bool lightColored) {
|
|
|
|
|
|
|
|
QColor result = baseColor(lightColored);
|
|
|
|
result.setHsv(result.hue(), clamp(result.saturation() * 1.1), clamp(result.value() * 0.70));
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::borderColor(bool lightColored) {
|
|
|
|
|
|
|
|
QColor result = baseColor(lightColored);
|
|
|
|
result.setHsv(result.hue(), result.saturation(), result.value() / 2);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StyleHelper::toolBarBorderColor() {
|
|
|
|
|
|
|
|
const QColor base = baseColor();
|
2021-06-20 23:49:04 +02:00
|
|
|
return QColor::fromHsv(base.hue(), base.saturation(), clamp(base.value() * 0.80F));
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// We try to ensure that the actual color used are within reasonalbe bounds while generating the actual baseColor from the users request.
|
|
|
|
void StyleHelper::setBaseColor(const QColor &newcolor) {
|
|
|
|
|
|
|
|
m_requestedBaseColor = newcolor;
|
|
|
|
|
|
|
|
QColor color;
|
|
|
|
color.setHsv(newcolor.hue(), newcolor.saturation() * 0.7, 64 + newcolor.value() / 3);
|
|
|
|
|
|
|
|
if (color.isValid() && color != m_baseColor) {
|
|
|
|
m_baseColor = color;
|
2021-07-11 09:49:38 +02:00
|
|
|
for (QWidget *w : QApplication::topLevelWidgets()) {
|
2019-04-24 23:43:09 +02:00
|
|
|
w->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
static void verticalGradientHelper(QPainter *p, const QRect spanRect, const QRect rect, bool lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QColor highlight = StyleHelper::highlightColor(lightColored);
|
|
|
|
QColor shadow = StyleHelper::shadowColor(lightColored);
|
|
|
|
QLinearGradient grad(spanRect.topRight(), spanRect.topLeft());
|
|
|
|
grad.setColorAt(0, highlight.lighter(117));
|
|
|
|
grad.setColorAt(1, shadow.darker(109));
|
|
|
|
p->fillRect(rect, grad);
|
|
|
|
|
|
|
|
QColor light(255, 255, 255, 80);
|
|
|
|
p->setPen(light);
|
|
|
|
p->drawLine(rect.topRight() - QPoint(1, 0), rect.bottomRight() - QPoint(1, 0));
|
|
|
|
QColor dark(0, 0, 0, 90);
|
|
|
|
p->setPen(dark);
|
|
|
|
p->drawLine(rect.topLeft(), rect.bottomLeft());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void StyleHelper::verticalGradient(QPainter *painter, const QRect spanRect, const QRect clipRect, bool lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
if (StyleHelper::usePixmapCache()) {
|
|
|
|
QColor keyColor = baseColor(lightColored);
|
2019-12-21 18:15:45 +01:00
|
|
|
QString key = QString::asprintf("mh_vertical %d %d %d %d %d", spanRect.width(), spanRect.height(), clipRect.width(), clipRect.height(), keyColor.rgb());
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
|
|
|
pixmap = QPixmap(clipRect.size());
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
QRect rect(0, 0, clipRect.width(), clipRect.height());
|
|
|
|
verticalGradientHelper(&p, spanRect, rect, lightColored);
|
|
|
|
p.end();
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
}
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
verticalGradientHelper(painter, spanRect, clipRect, lightColored);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
static void horizontalGradientHelper(QPainter *p, const QRect spanRect, const QRect rect, bool lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
if (lightColored) {
|
|
|
|
QLinearGradient shadowGradient(rect.topLeft(), rect.bottomLeft());
|
|
|
|
shadowGradient.setColorAt(0, 0xf0f0f0);
|
|
|
|
shadowGradient.setColorAt(1, 0xcfcfcf);
|
|
|
|
p->fillRect(rect, shadowGradient);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor base = StyleHelper::baseColor(lightColored);
|
|
|
|
QColor highlight = StyleHelper::highlightColor(lightColored);
|
|
|
|
QColor shadow = StyleHelper::shadowColor(lightColored);
|
|
|
|
QLinearGradient grad(rect.topLeft(), rect.bottomLeft());
|
|
|
|
grad.setColorAt(0, highlight.lighter(120));
|
|
|
|
if (rect.height() == StyleHelper::navigationWidgetHeight()) {
|
|
|
|
grad.setColorAt(0.4, highlight);
|
|
|
|
grad.setColorAt(0.401, base);
|
|
|
|
}
|
|
|
|
grad.setColorAt(1, shadow);
|
|
|
|
p->fillRect(rect, grad);
|
|
|
|
|
|
|
|
QLinearGradient shadowGradient(spanRect.topLeft(), spanRect.topRight());
|
|
|
|
shadowGradient.setColorAt(0, QColor(0, 0, 0, 30));
|
|
|
|
QColor lighterHighlight;
|
|
|
|
lighterHighlight = highlight.lighter(130);
|
|
|
|
lighterHighlight.setAlpha(100);
|
|
|
|
shadowGradient.setColorAt(0.7, lighterHighlight);
|
|
|
|
shadowGradient.setColorAt(1, QColor(0, 0, 0, 40));
|
|
|
|
p->fillRect(rect, shadowGradient);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void StyleHelper::horizontalGradient(QPainter *painter, const QRect spanRect, const QRect clipRect, bool lightColored) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
if (StyleHelper::usePixmapCache()) {
|
|
|
|
QColor keyColor = baseColor(lightColored);
|
2019-12-21 18:15:45 +01:00
|
|
|
QString key = QString::asprintf("mh_horizontal %d %d %d %d %d %d", spanRect.width(), spanRect.height(), clipRect.width(), clipRect.height(), keyColor.rgb(), spanRect.x());
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
|
|
|
pixmap = QPixmap(clipRect.size());
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
QRect rect = QRect(0, 0, clipRect.width(), clipRect.height());
|
|
|
|
horizontalGradientHelper(&p, spanRect, rect, lightColored);
|
|
|
|
p.end();
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
}
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
horizontalGradientHelper(painter, spanRect, clipRect, lightColored);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
static void menuGradientHelper(QPainter *p, const QRect spanRect, const QRect rect) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QLinearGradient grad(spanRect.topLeft(), spanRect.bottomLeft());
|
|
|
|
QColor menuColor = StyleHelper::mergedColors(StyleHelper::baseColor(), QColor(244, 244, 244), 25);
|
|
|
|
grad.setColorAt(0, menuColor.lighter(112));
|
|
|
|
grad.setColorAt(1, menuColor);
|
|
|
|
p->fillRect(rect, grad);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleHelper::drawArrow(QStyle::PrimitiveElement element, QPainter *painter, const QStyleOption *option) {
|
|
|
|
|
2021-08-23 21:21:08 +02:00
|
|
|
if (option->rect.width() <= 1 || option->rect.height() <= 1) {
|
2019-04-24 23:43:09 +02:00
|
|
|
return;
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
const qreal devicePixelRatio = painter->device()->devicePixelRatio();
|
|
|
|
const bool enabled = option->state & QStyle::State_Enabled;
|
|
|
|
QRect r = option->rect;
|
|
|
|
int size = qMin(r.height(), r.width());
|
|
|
|
QPixmap pixmap;
|
2021-06-22 13:54:58 +02:00
|
|
|
QString pixmapName = QString::asprintf("StyleHelper::drawArrow-%d-%d-%d-%f", element, size, (enabled ? 1 : 0), devicePixelRatio);
|
2019-04-24 23:43:09 +02:00
|
|
|
if (!QPixmapCache::find(pixmapName, &pixmap)) {
|
|
|
|
QImage image(size * devicePixelRatio, size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.fill(Qt::transparent);
|
2020-04-23 21:08:28 +02:00
|
|
|
QPainter p(&image);
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QStyleOption tweakedOption(*option);
|
|
|
|
tweakedOption.state = QStyle::State_Enabled;
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
auto drawCommonStyleArrow = [&tweakedOption, element, &p](const QRect rect, const QColor &color) -> void
|
2019-04-24 23:43:09 +02:00
|
|
|
{
|
|
|
|
static const QCommonStyle* const style = qobject_cast<QCommonStyle*>(QApplication::style());
|
|
|
|
if (!style)
|
|
|
|
return;
|
|
|
|
tweakedOption.palette.setColor(QPalette::ButtonText, color.rgb());
|
|
|
|
tweakedOption.rect = rect;
|
2020-04-23 21:08:28 +02:00
|
|
|
p.setOpacity(color.alphaF());
|
|
|
|
style->QCommonStyle::drawPrimitive(element, &tweakedOption, &p);
|
2019-04-24 23:43:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
drawCommonStyleArrow(image.rect(), m_IconsDisabledColor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
drawCommonStyleArrow(image.rect().translated(0, devicePixelRatio), toolBarDropShadowColor());
|
|
|
|
drawCommonStyleArrow(image.rect(), m_IconsBaseColor);
|
|
|
|
}
|
2020-04-23 21:08:28 +02:00
|
|
|
p.end();
|
2019-04-24 23:43:09 +02:00
|
|
|
pixmap = QPixmap::fromImage(image);
|
|
|
|
pixmap.setDevicePixelRatio(devicePixelRatio);
|
|
|
|
QPixmapCache::insert(pixmapName, pixmap);
|
|
|
|
}
|
2021-07-11 09:49:38 +02:00
|
|
|
int xOffset = r.x() + (r.width() - size) / 2;
|
|
|
|
int yOffset = r.y() + (r.height() - size) / 2;
|
2019-04-24 23:43:09 +02:00
|
|
|
painter->drawPixmap(xOffset, yOffset, pixmap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void StyleHelper::menuGradient(QPainter *painter, const QRect spanRect, const QRect clipRect) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
if (StyleHelper::usePixmapCache()) {
|
2019-12-21 18:15:45 +01:00
|
|
|
QString key = QString::asprintf("mh_menu %d %d %d %d %d", spanRect.width(), spanRect.height(), clipRect.width(), clipRect.height(), StyleHelper::baseColor().rgb());
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
|
|
|
pixmap = QPixmap(clipRect.size());
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
QRect rect = QRect(0, 0, clipRect.width(), clipRect.height());
|
|
|
|
menuGradientHelper(&p, spanRect, rect);
|
|
|
|
p.end();
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
}
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
menuGradientHelper(painter, spanRect, clipRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap StyleHelper::disabledSideBarIcon(const QPixmap &enabledicon) {
|
|
|
|
|
|
|
|
QImage im = enabledicon.toImage().convertToFormat(QImage::Format_ARGB32);
|
|
|
|
|
2021-07-11 09:49:38 +02:00
|
|
|
for (int y = 0; y < im.height(); ++y) {
|
2019-04-24 23:43:09 +02:00
|
|
|
auto scanLine = reinterpret_cast<QRgb*>(im.scanLine(y));
|
|
|
|
for (int x=0; x<im.width(); ++x) {
|
|
|
|
QRgb pixel = *scanLine;
|
2021-10-11 22:28:28 +02:00
|
|
|
char intensity = static_cast<char>(qGray(pixel));
|
2019-04-24 23:43:09 +02:00
|
|
|
*scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel));
|
|
|
|
++scanLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QPixmap::fromImage(im);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draws a CSS-like border image where the defined borders are not stretched
|
|
|
|
// Unit for rect, left, top, right and bottom is user pixels
|
2021-06-20 19:04:08 +02:00
|
|
|
void StyleHelper::drawCornerImage(const QImage &img, QPainter *painter, const QRect rect, int left, int top, int right, int bottom) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
// source rect for drawImage() calls needs to be specified in DIP unit of the image
|
|
|
|
const qreal imagePixelRatio = img.devicePixelRatio();
|
|
|
|
const qreal leftDIP = left * imagePixelRatio;
|
|
|
|
const qreal topDIP = top * imagePixelRatio;
|
|
|
|
const qreal rightDIP = right * imagePixelRatio;
|
|
|
|
const qreal bottomDIP = bottom * imagePixelRatio;
|
|
|
|
|
|
|
|
const QSize size = img.size();
|
|
|
|
if (top > 0) { //top
|
|
|
|
painter->drawImage(QRectF(rect.left() + left, rect.top(), rect.width() -right - left, top), img, QRectF(leftDIP, 0, size.width() - rightDIP - leftDIP, topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
if (left > 0) { //top-left
|
2019-04-24 23:43:09 +02:00
|
|
|
painter->drawImage(QRectF(rect.left(), rect.top(), left, top), img, QRectF(0, 0, leftDIP, topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
if (right > 0) { //top-right
|
2019-04-24 23:43:09 +02:00
|
|
|
painter->drawImage(QRectF(rect.left() + rect.width() - right, rect.top(), right, top), img, QRectF(size.width() - rightDIP, 0, rightDIP, topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
}
|
|
|
|
//left
|
2021-08-23 21:21:08 +02:00
|
|
|
if (left > 0) {
|
2019-04-24 23:43:09 +02:00
|
|
|
painter->drawImage(QRectF(rect.left(), rect.top()+top, left, rect.height() - top - bottom), img, QRectF(0, topDIP, leftDIP, size.height() - bottomDIP - topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
//center
|
|
|
|
painter->drawImage(QRectF(rect.left() + left, rect.top()+top, rect.width() -right - left, rect.height() - bottom - top), img, QRectF(leftDIP, topDIP, size.width() - rightDIP - leftDIP, size.height() - bottomDIP - topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
if (right > 0) { //right
|
2019-04-24 23:43:09 +02:00
|
|
|
painter->drawImage(QRectF(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), img, QRectF(size.width() - rightDIP, topDIP, rightDIP, size.height() - bottomDIP - topDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
if (bottom > 0) { //bottom
|
|
|
|
painter->drawImage(QRectF(rect.left() +left, rect.top() + rect.height() - bottom, rect.width() - right - left, bottom), img, QRectF(leftDIP, size.height() - bottomDIP, size.width() - rightDIP - leftDIP, bottomDIP));
|
2021-08-23 21:21:08 +02:00
|
|
|
if (left > 0) { //bottom-left
|
|
|
|
painter->drawImage(QRectF(rect.left(), rect.top() + rect.height() - bottom, left, bottom), img, QRectF(0, size.height() - bottomDIP, leftDIP, bottomDIP));
|
|
|
|
}
|
|
|
|
if (right > 0) { //bottom-right
|
|
|
|
painter->drawImage(QRectF(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), img, QRectF(size.width() - rightDIP, size.height() - bottomDIP, rightDIP, bottomDIP));
|
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tints an image with tintColor, while preserving alpha and lightness
|
|
|
|
void StyleHelper::tintImage(QImage &img, const QColor &tintColor) {
|
|
|
|
|
|
|
|
QPainter p(&img);
|
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Screen);
|
|
|
|
|
|
|
|
for (int x = 0; x < img.width(); ++x) {
|
|
|
|
for (int y = 0; y < img.height(); ++y) {
|
|
|
|
QRgb rgbColor = img.pixel(x, y);
|
|
|
|
int alpha = qAlpha(rgbColor);
|
|
|
|
QColor c = QColor(rgbColor);
|
|
|
|
|
|
|
|
if (alpha > 0) {
|
|
|
|
c.toHsl();
|
|
|
|
qreal l = c.lightnessF();
|
|
|
|
QColor newColor = QColor::fromHslF(tintColor.hslHueF(), tintColor.hslSaturationF(), l);
|
|
|
|
newColor.setAlpha(alpha);
|
|
|
|
img.setPixel(x, y, newColor.rgba());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
QLinearGradient StyleHelper::statusBarGradient(const QRect statusBarRect) {
|
2019-04-24 23:43:09 +02:00
|
|
|
|
|
|
|
QLinearGradient grad(statusBarRect.topLeft(), QPoint(statusBarRect.center().x(), statusBarRect.bottom()));
|
|
|
|
QColor startColor = shadowColor().darker(164);
|
|
|
|
QColor endColor = baseColor().darker(130);
|
|
|
|
grad.setColorAt(0, startColor);
|
|
|
|
grad.setColorAt(1, endColor);
|
|
|
|
return grad;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString StyleHelper::dpiSpecificImageFile(const QString &fileName) {
|
|
|
|
|
|
|
|
// See QIcon::addFile()
|
|
|
|
if (qApp->devicePixelRatio() > 1.0) {
|
|
|
|
const QString atDprfileName = imageFileWithResolution(fileName, qRound(qApp->devicePixelRatio()));
|
2021-08-23 21:21:08 +02:00
|
|
|
if (QFile::exists(atDprfileName)) {
|
2019-04-24 23:43:09 +02:00
|
|
|
return atDprfileName;
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
}
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString StyleHelper::imageFileWithResolution(const QString &fileName, int dpr) {
|
|
|
|
|
|
|
|
const QFileInfo fi(fileName);
|
|
|
|
return dpr == 1 ? fileName : fi.path() + QLatin1Char('/') + fi.completeBaseName() + QLatin1Char('@') + QString::number(dpr) + QLatin1String("x.") + fi.suffix();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<int> StyleHelper::availableImageResolutions(const QString &fileName) {
|
|
|
|
|
|
|
|
QList<int> result;
|
|
|
|
const int maxResolutions = qApp->devicePixelRatio();
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 1; i <= maxResolutions; ++i) {
|
|
|
|
if (QFile::exists(imageFileWithResolution(fileName, i))) {
|
2019-04-24 23:43:09 +02:00
|
|
|
result.append(i);
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 23:43:09 +02:00
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Utils
|