2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "stylehelper.h"
|
|
|
|
|
|
2016-03-21 12:48:06 +01:00
|
|
|
#include "theme/theme.h"
|
2012-08-23 15:53:58 +02:00
|
|
|
#include "hostosinfo.h"
|
2022-05-25 06:30:04 +02:00
|
|
|
#include "qtcassert.h"
|
2020-04-20 09:34:38 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
2014-07-14 11:22:52 +02:00
|
|
|
#include <QCommonStyle>
|
2022-05-17 22:25:02 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QFontDatabase>
|
|
|
|
|
#include <QPainter>
|
2023-04-21 14:49:26 +02:00
|
|
|
#include <QPainterPath>
|
2022-05-17 22:25:02 +02:00
|
|
|
#include <QPixmapCache>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QStyleOption>
|
2017-09-12 18:55:14 +02:00
|
|
|
#include <QWindow>
|
2022-05-17 22:25:02 +02:00
|
|
|
|
2013-01-16 22:06:59 +01:00
|
|
|
#include <qmath.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Clamps float color values within (0, 255)
|
2008-12-02 15:08:31 +01:00
|
|
|
static int clamp(float x)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
const int val = x > 255 ? 255 : static_cast<int>(x);
|
|
|
|
|
return val < 0 ? 0 : val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clamps float color values within (0, 255)
|
|
|
|
|
/*
|
2008-12-02 15:08:31 +01:00
|
|
|
static int range(float x, int min, int max)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
int val = x > max ? max : x;
|
|
|
|
|
return val < min ? min : val;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2009-10-05 11:06:05 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2023-05-11 13:15:55 +02:00
|
|
|
static StyleHelper::ToolbarStyle m_toolbarStyle = StyleHelper::defaultToolbarStyle;
|
|
|
|
|
// Invalid by default, setBaseColor needs to be called at least once
|
|
|
|
|
static QColor m_baseColor;
|
|
|
|
|
static QColor m_requestedBaseColor;
|
|
|
|
|
|
2009-06-05 13:59:10 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-17 19:00:59 +02:00
|
|
|
QColor StyleHelper::alphaBlendedColors(const QColor &colorA, const QColor &colorB)
|
|
|
|
|
{
|
2016-07-28 10:25:51 +02:00
|
|
|
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
|
|
|
|
|
);
|
2016-06-17 19:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-11 13:15:55 +02:00
|
|
|
QColor StyleHelper::sidebarHighlight()
|
|
|
|
|
{
|
|
|
|
|
return QColor(255, 255, 255, 40);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QColor StyleHelper::sidebarShadow()
|
|
|
|
|
{
|
|
|
|
|
return QColor(0, 0, 0, 40);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QColor StyleHelper::toolBarDropShadowColor()
|
|
|
|
|
{
|
|
|
|
|
return QColor(0, 0, 0, 70);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 15:43:31 +02:00
|
|
|
int StyleHelper::navigationWidgetHeight()
|
|
|
|
|
{
|
|
|
|
|
return m_toolbarStyle == ToolbarStyleCompact ? 24 : 30;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StyleHelper::setToolbarStyle(ToolbarStyle style)
|
|
|
|
|
{
|
|
|
|
|
m_toolbarStyle = style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StyleHelper::ToolbarStyle StyleHelper::toolbarStyle()
|
|
|
|
|
{
|
|
|
|
|
return m_toolbarStyle;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
qreal StyleHelper::sidebarFontSize()
|
|
|
|
|
{
|
2012-08-23 15:53:58 +02:00
|
|
|
return HostOsInfo::isMacHost() ? 10 : 7.5;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-06 16:06:32 +02:00
|
|
|
QColor StyleHelper::notTooBrightHighlightColor()
|
|
|
|
|
{
|
2017-04-24 17:01:10 +02:00
|
|
|
QColor highlightColor = QApplication::palette().highlight().color();
|
2014-06-06 16:06:32 +02:00
|
|
|
if (0.5 * highlightColor.saturationF() + 0.75 - highlightColor.valueF() < 0)
|
|
|
|
|
highlightColor.setHsvF(highlightColor.hsvHueF(), 0.1 + highlightColor.saturationF() * 2.0, highlightColor.valueF());
|
|
|
|
|
return highlightColor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QPalette StyleHelper::sidebarFontPalette(const QPalette &original)
|
|
|
|
|
{
|
|
|
|
|
QPalette palette = original;
|
2016-07-21 17:14:52 +02:00
|
|
|
const QColor textColor = creatorTheme()->color(Theme::ProgressBarTitleColor);
|
|
|
|
|
palette.setColor(QPalette::WindowText, textColor);
|
|
|
|
|
palette.setColor(QPalette::Text, textColor);
|
2008-12-02 12:01:29 +01:00
|
|
|
return palette;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor StyleHelper::panelTextColor(bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-02-19 09:54:29 +01:00
|
|
|
if (!lightColored)
|
|
|
|
|
return Qt::white;
|
|
|
|
|
else
|
|
|
|
|
return Qt::black;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor StyleHelper::baseColor(bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2018-01-07 21:38:17 +01:00
|
|
|
static const QColor windowColor = QApplication::palette().color(QPalette::Window);
|
2016-07-27 18:25:06 +02:00
|
|
|
static const bool windowColorAsBase = creatorTheme()->flag(Theme::WindowColorAsBase);
|
|
|
|
|
|
2018-01-07 21:38:17 +01:00
|
|
|
return (lightColored || windowColorAsBase) ? windowColor : m_baseColor;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-11 13:15:55 +02:00
|
|
|
QColor StyleHelper::requestedBaseColor()
|
|
|
|
|
{
|
|
|
|
|
return m_requestedBaseColor;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 17:25:10 +01:00
|
|
|
QColor StyleHelper::toolbarBaseColor(bool lightColored)
|
|
|
|
|
{
|
|
|
|
|
if (creatorTheme()->flag(Theme::QDSTheme))
|
|
|
|
|
return creatorTheme()->color(Utils::Theme::DStoolbarBackground);
|
|
|
|
|
else
|
|
|
|
|
return StyleHelper::baseColor(lightColored);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor StyleHelper::highlightColor(bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor result = baseColor(lightColored);
|
|
|
|
|
if (!lightColored)
|
|
|
|
|
result.setHsv(result.hue(),
|
2008-12-02 12:01:29 +01:00
|
|
|
clamp(result.saturation()),
|
|
|
|
|
clamp(result.value() * 1.16));
|
2010-02-19 09:54:29 +01:00
|
|
|
else
|
|
|
|
|
result.setHsv(result.hue(),
|
|
|
|
|
clamp(result.saturation()),
|
|
|
|
|
clamp(result.value() * 1.06));
|
2008-12-02 12:01:29 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor StyleHelper::shadowColor(bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor result = baseColor(lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
result.setHsv(result.hue(),
|
|
|
|
|
clamp(result.saturation() * 1.1),
|
|
|
|
|
clamp(result.value() * 0.70));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor StyleHelper::borderColor(bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor result = baseColor(lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
result.setHsv(result.hue(),
|
|
|
|
|
result.saturation(),
|
|
|
|
|
result.value() / 2);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 18:24:25 +02:00
|
|
|
QColor StyleHelper::toolBarBorderColor()
|
|
|
|
|
{
|
|
|
|
|
const QColor base = baseColor();
|
|
|
|
|
return QColor::fromHsv(base.hue(),
|
|
|
|
|
base.saturation() ,
|
|
|
|
|
clamp(base.value() * 0.80f));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 13:15:55 +02:00
|
|
|
QColor StyleHelper::buttonTextColor()
|
|
|
|
|
{
|
|
|
|
|
return QColor(0x4c4c4c);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-03 11:02:13 +01: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)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-03-03 11:02:13 +01:00
|
|
|
m_requestedBaseColor = newcolor;
|
|
|
|
|
|
2016-03-21 12:48:06 +01:00
|
|
|
const QColor themeBaseColor = creatorTheme()->color(Theme::PanelStatusBarBackgroundColor);
|
|
|
|
|
const QColor defaultBaseColor = QColor(DEFAULT_BASE_COLOR);
|
2010-03-03 11:02:13 +01:00
|
|
|
QColor color;
|
2016-03-21 12:48:06 +01:00
|
|
|
|
|
|
|
|
if (defaultBaseColor == newcolor) {
|
|
|
|
|
color = themeBaseColor;
|
|
|
|
|
} else {
|
|
|
|
|
const int valueDelta = (newcolor.value() - defaultBaseColor.value()) / 3;
|
|
|
|
|
const int value = qBound(0, themeBaseColor.value() + valueDelta, 255);
|
|
|
|
|
|
|
|
|
|
color.setHsv(newcolor.hue(),
|
|
|
|
|
newcolor.saturation() * 0.7,
|
|
|
|
|
value);
|
|
|
|
|
}
|
2010-03-03 11:02:13 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (color.isValid() && color != m_baseColor) {
|
|
|
|
|
m_baseColor = color;
|
2023-05-09 17:38:23 +02:00
|
|
|
const QWidgetList widgets = QApplication::allWidgets();
|
2020-06-13 23:39:57 +03:00
|
|
|
for (QWidget *w : widgets)
|
2008-12-02 12:01:29 +01:00
|
|
|
w->update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
static void verticalGradientHelper(QPainter *p, const QRect &spanRect, const QRect &rect, bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor highlight = StyleHelper::highlightColor(lightColored);
|
|
|
|
|
QColor shadow = StyleHelper::shadowColor(lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
QLinearGradient grad(spanRect.topRight(), spanRect.topLeft());
|
2010-03-23 15:47:43 +01:00
|
|
|
grad.setColorAt(0, highlight.lighter(117));
|
2010-02-25 18:53:42 +01:00
|
|
|
grad.setColorAt(1, shadow.darker(109));
|
2008-12-02 12:01:29 +01:00
|
|
|
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));
|
2010-02-19 18:05:42 +01:00
|
|
|
QColor dark(0, 0, 0, 90);
|
|
|
|
|
p->setPen(dark);
|
|
|
|
|
p->drawLine(rect.topLeft(), rect.bottomLeft());
|
2009-11-09 11:47:05 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
void StyleHelper::verticalGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect, bool lightColored)
|
2009-11-09 11:47:05 +01:00
|
|
|
{
|
|
|
|
|
if (StyleHelper::usePixmapCache()) {
|
2019-05-27 17:23:59 +02:00
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor keyColor = baseColor(lightColored);
|
2019-05-27 17:23:59 +02:00
|
|
|
const QString key = QString::asprintf("mh_vertical %d %d %d %d %d",
|
2009-11-09 11:47:05 +01:00
|
|
|
spanRect.width(), spanRect.height(), clipRect.width(),
|
2013-09-03 18:48:02 +02:00
|
|
|
clipRect.height(), keyColor.rgb());
|
2009-11-09 11:47:05 +01:00
|
|
|
|
|
|
|
|
QPixmap pixmap;
|
2019-02-11 10:17:53 +01:00
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
2009-11-09 11:47:05 +01:00
|
|
|
pixmap = QPixmap(clipRect.size());
|
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
|
QRect rect(0, 0, clipRect.width(), clipRect.height());
|
2010-02-19 09:54:29 +01:00
|
|
|
verticalGradientHelper(&p, spanRect, rect, lightColored);
|
2009-11-09 11:47:05 +01:00
|
|
|
p.end();
|
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
2009-11-09 11:47:05 +01:00
|
|
|
} else {
|
2010-02-19 09:54:29 +01:00
|
|
|
verticalGradientHelper(painter, spanRect, clipRect, lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-09 11:47:05 +01:00
|
|
|
static void horizontalGradientHelper(QPainter *p, const QRect &spanRect, const
|
2010-02-19 09:54:29 +01:00
|
|
|
QRect &rect, bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-28 16:09:48 +02:00
|
|
|
if (lightColored) {
|
|
|
|
|
QLinearGradient shadowGradient(rect.topLeft(), rect.bottomLeft());
|
|
|
|
|
shadowGradient.setColorAt(0, 0xf0f0f0);
|
|
|
|
|
shadowGradient.setColorAt(1, 0xcfcfcf);
|
|
|
|
|
p->fillRect(rect, shadowGradient);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor base = StyleHelper::baseColor(lightColored);
|
|
|
|
|
QColor highlight = StyleHelper::highlightColor(lightColored);
|
|
|
|
|
QColor shadow = StyleHelper::shadowColor(lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
QLinearGradient grad(rect.topLeft(), rect.bottomLeft());
|
2010-02-19 09:54:29 +01:00
|
|
|
grad.setColorAt(0, highlight.lighter(120));
|
2009-11-09 11:47:05 +01:00
|
|
|
if (rect.height() == StyleHelper::navigationWidgetHeight()) {
|
2010-02-19 09:54:29 +01:00
|
|
|
grad.setColorAt(0.4, highlight);
|
2009-09-21 10:57:07 +02:00
|
|
|
grad.setColorAt(0.401, base);
|
|
|
|
|
}
|
2010-02-19 09:54:29 +01:00
|
|
|
grad.setColorAt(1, shadow);
|
2008-12-02 12:01:29 +01:00
|
|
|
p->fillRect(rect, grad);
|
|
|
|
|
|
|
|
|
|
QLinearGradient shadowGradient(spanRect.topLeft(), spanRect.topRight());
|
2010-02-19 09:54:29 +01:00
|
|
|
shadowGradient.setColorAt(0, QColor(0, 0, 0, 30));
|
|
|
|
|
QColor lighterHighlight;
|
2010-06-02 14:35:42 +02:00
|
|
|
lighterHighlight = highlight.lighter(130);
|
2010-02-19 09:54:29 +01:00
|
|
|
lighterHighlight.setAlpha(100);
|
|
|
|
|
shadowGradient.setColorAt(0.7, lighterHighlight);
|
|
|
|
|
shadowGradient.setColorAt(1, QColor(0, 0, 0, 40));
|
2008-12-02 12:01:29 +01:00
|
|
|
p->fillRect(rect, shadowGradient);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
void StyleHelper::horizontalGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect, bool lightColored)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-11-09 11:47:05 +01:00
|
|
|
if (StyleHelper::usePixmapCache()) {
|
2019-05-27 17:23:59 +02:00
|
|
|
|
2010-02-19 09:54:29 +01:00
|
|
|
QColor keyColor = baseColor(lightColored);
|
2019-05-27 17:23:59 +02:00
|
|
|
const QString key = QString::asprintf("mh_horizontal %d %d %d %d %d %d",
|
2009-11-09 11:47:05 +01:00
|
|
|
spanRect.width(), spanRect.height(), clipRect.width(),
|
2010-03-19 17:47:45 +01:00
|
|
|
clipRect.height(), keyColor.rgb(), spanRect.x());
|
2009-11-09 11:47:05 +01:00
|
|
|
|
|
|
|
|
QPixmap pixmap;
|
2019-02-11 10:17:53 +01:00
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
2009-11-09 11:47:05 +01:00
|
|
|
pixmap = QPixmap(clipRect.size());
|
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
|
QRect rect = QRect(0, 0, clipRect.width(), clipRect.height());
|
2010-02-19 09:54:29 +01:00
|
|
|
horizontalGradientHelper(&p, spanRect, rect, lightColored);
|
2009-11-09 11:47:05 +01:00
|
|
|
p.end();
|
|
|
|
|
QPixmapCache::insert(key, pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
|
|
|
|
|
|
|
|
|
} else {
|
2010-02-19 09:54:29 +01:00
|
|
|
horizontalGradientHelper(painter, spanRect, clipRect, lightColored);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-11-09 11:47:05 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-11-09 11:47:05 +01:00
|
|
|
static void menuGradientHelper(QPainter *p, const QRect &spanRect, const QRect &rect)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
QLinearGradient grad(spanRect.topLeft(), spanRect.bottomLeft());
|
2009-11-09 11:47:05 +01:00
|
|
|
QColor menuColor = StyleHelper::mergedColors(StyleHelper::baseColor(), QColor(244, 244, 244), 25);
|
2008-12-02 12:01:29 +01:00
|
|
|
grad.setColorAt(0, menuColor.lighter(112));
|
|
|
|
|
grad.setColorAt(1, menuColor);
|
|
|
|
|
p->fillRect(rect, grad);
|
2009-11-09 11:47:05 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-17 17:09:00 +01:00
|
|
|
void StyleHelper::drawArrow(QStyle::PrimitiveElement element, QPainter *painter, const QStyleOption *option)
|
|
|
|
|
{
|
|
|
|
|
if (option->rect.width() <= 1 || option->rect.height() <= 1)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-07-14 11:22:52 +02:00
|
|
|
const qreal devicePixelRatio = painter->device()->devicePixelRatio();
|
2016-03-31 13:56:02 +02:00
|
|
|
const bool enabled = option->state & QStyle::State_Enabled;
|
2010-02-17 17:09:00 +01:00
|
|
|
QRect r = option->rect;
|
|
|
|
|
int size = qMin(r.height(), r.width());
|
|
|
|
|
QPixmap pixmap;
|
2019-05-27 17:23:59 +02:00
|
|
|
const QString pixmapName = QString::asprintf("StyleHelper::drawArrow-%d-%d-%d-%f",
|
2016-03-31 13:56:02 +02:00
|
|
|
element, size, enabled, devicePixelRatio);
|
2019-02-11 10:17:53 +01:00
|
|
|
if (!QPixmapCache::find(pixmapName, &pixmap)) {
|
2014-07-14 11:22:52 +02:00
|
|
|
QImage image(size * devicePixelRatio, size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
image.fill(Qt::transparent);
|
|
|
|
|
QPainter painter(&image);
|
2010-02-17 17:09:00 +01:00
|
|
|
|
2014-07-14 11:22:52 +02:00
|
|
|
QStyleOption tweakedOption(*option);
|
|
|
|
|
tweakedOption.state = QStyle::State_Enabled;
|
2010-02-17 17:09:00 +01:00
|
|
|
|
2016-03-29 17:36:49 +02:00
|
|
|
auto drawCommonStyleArrow = [&tweakedOption, element, &painter](const QRect &rect, const QColor &color) -> void
|
|
|
|
|
{
|
|
|
|
|
static const QCommonStyle* const style = qobject_cast<QCommonStyle*>(QApplication::style());
|
|
|
|
|
if (!style)
|
|
|
|
|
return;
|
2022-11-21 15:02:57 +01:00
|
|
|
|
|
|
|
|
// Workaround for QTCREATORBUG-28470
|
2022-11-18 14:27:51 +01:00
|
|
|
QPalette pal = tweakedOption.palette;
|
2022-11-21 15:02:57 +01:00
|
|
|
pal.setBrush(QPalette::Base, pal.text()); // Base and Text differ, causing a detachment.
|
|
|
|
|
// Inspired by tst_QPalette::cacheKey()
|
2022-11-18 14:27:51 +01:00
|
|
|
pal.setColor(QPalette::ButtonText, color.rgb());
|
2022-11-21 15:02:57 +01:00
|
|
|
|
|
|
|
|
tweakedOption.palette = pal;
|
2016-03-29 17:36:49 +02:00
|
|
|
tweakedOption.rect = rect;
|
|
|
|
|
painter.setOpacity(color.alphaF());
|
2014-07-14 11:22:52 +02:00
|
|
|
style->QCommonStyle::drawPrimitive(element, &tweakedOption, &painter);
|
2016-03-29 17:36:49 +02:00
|
|
|
};
|
2014-07-14 11:22:52 +02:00
|
|
|
|
2016-03-31 13:56:02 +02:00
|
|
|
if (!enabled) {
|
2016-03-29 17:36:49 +02:00
|
|
|
drawCommonStyleArrow(image.rect(), creatorTheme()->color(Theme::IconsDisabledColor));
|
|
|
|
|
} else {
|
2016-06-17 18:52:24 +02:00
|
|
|
if (creatorTheme()->flag(Theme::ToolBarIconShadow))
|
|
|
|
|
drawCommonStyleArrow(image.rect().translated(0, devicePixelRatio), toolBarDropShadowColor());
|
2016-03-29 17:36:49 +02:00
|
|
|
drawCommonStyleArrow(image.rect(), creatorTheme()->color(Theme::IconsBaseColor));
|
2010-02-17 17:09:00 +01:00
|
|
|
}
|
2014-07-14 11:22:52 +02:00
|
|
|
painter.end();
|
2010-02-17 17:09:00 +01:00
|
|
|
pixmap = QPixmap::fromImage(image);
|
2014-07-14 11:22:52 +02:00
|
|
|
pixmap.setDevicePixelRatio(devicePixelRatio);
|
2010-02-17 17:09:00 +01:00
|
|
|
QPixmapCache::insert(pixmapName, pixmap);
|
2023-01-20 14:06:19 +02:00
|
|
|
}
|
|
|
|
|
int xOffset = r.x() + (r.width() - size)/2;
|
|
|
|
|
int yOffset = r.y() + (r.height() - size)/2;
|
|
|
|
|
painter->drawPixmap(xOffset, yOffset, pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StyleHelper::drawMinimalArrow(QStyle::PrimitiveElement element, QPainter *painter, const QStyleOption *option)
|
|
|
|
|
{
|
|
|
|
|
if (option->rect.width() <= 1 || option->rect.height() <= 1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
const QString pixmapName = QString::asprintf("StyleHelper::drawMinimalArrow-%d-%d-%d-%f",
|
|
|
|
|
element, size, enabled, devicePixelRatio);
|
|
|
|
|
if (!QPixmapCache::find(pixmapName, &pixmap)) {
|
|
|
|
|
QImage image(size * devicePixelRatio, size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
image.fill(Qt::transparent);
|
|
|
|
|
QPainter painter(&image);
|
|
|
|
|
QStyleOption tweakedOption(*option);
|
|
|
|
|
|
|
|
|
|
double rotation = 0;
|
|
|
|
|
switch (element) {
|
|
|
|
|
case QStyle::PE_IndicatorArrowLeft:
|
|
|
|
|
rotation = 45;
|
|
|
|
|
break;
|
|
|
|
|
case QStyle::PE_IndicatorArrowUp:
|
|
|
|
|
rotation = 135;
|
|
|
|
|
break;
|
|
|
|
|
case QStyle::PE_IndicatorArrowRight:
|
|
|
|
|
rotation = 225;
|
|
|
|
|
break;
|
|
|
|
|
case QStyle::PE_IndicatorArrowDown:
|
|
|
|
|
rotation = 315;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto drawArrow = [&tweakedOption, rotation, &painter](const QRect &rect, const QColor &color) -> void
|
|
|
|
|
{
|
|
|
|
|
static const QCommonStyle* const style = qobject_cast<QCommonStyle*>(QApplication::style());
|
|
|
|
|
if (!style)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Workaround for QTCREATORBUG-28470
|
|
|
|
|
QPalette pal = tweakedOption.palette;
|
|
|
|
|
pal.setBrush(QPalette::Base, pal.text()); // Base and Text differ, causing a detachment.
|
|
|
|
|
// Inspired by tst_QPalette::cacheKey()
|
|
|
|
|
pal.setColor(QPalette::ButtonText, color.rgb());
|
|
|
|
|
|
|
|
|
|
tweakedOption.palette = pal;
|
|
|
|
|
tweakedOption.rect = rect;
|
|
|
|
|
|
|
|
|
|
painter.save();
|
|
|
|
|
painter.setOpacity(color.alphaF());
|
|
|
|
|
|
|
|
|
|
double minDim = std::min(rect.width(), rect.height());
|
|
|
|
|
double innerWidth = minDim/M_SQRT2;
|
|
|
|
|
int penWidth = std::max(innerWidth/4, 1.0);
|
|
|
|
|
innerWidth -= penWidth;
|
|
|
|
|
|
|
|
|
|
QPen pPen(pal.color(QPalette::ButtonText), penWidth);
|
|
|
|
|
pPen.setJoinStyle(Qt::MiterJoin);
|
|
|
|
|
painter.setBrush(pal.text());
|
|
|
|
|
painter.setPen(pPen);
|
|
|
|
|
|
|
|
|
|
painter.translate(rect.center());
|
|
|
|
|
painter.rotate(rotation);
|
|
|
|
|
painter.translate(-innerWidth/2, -innerWidth/2);
|
|
|
|
|
|
|
|
|
|
const QPointF points[3] = {
|
|
|
|
|
{0, 0},
|
|
|
|
|
{0, innerWidth},
|
|
|
|
|
{innerWidth, innerWidth}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
painter.drawPolyline(points, 3);
|
|
|
|
|
painter.restore();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
if (creatorTheme()->flag(Theme::ToolBarIconShadow))
|
|
|
|
|
drawArrow(image.rect().translated(0, devicePixelRatio), toolBarDropShadowColor());
|
|
|
|
|
drawArrow(image.rect(), creatorTheme()->color(Theme::IconsBaseColor));
|
|
|
|
|
} else {
|
|
|
|
|
drawArrow(image.rect(), creatorTheme()->color(Theme::IconsDisabledColor));
|
|
|
|
|
}
|
|
|
|
|
painter.end();
|
|
|
|
|
pixmap = QPixmap::fromImage(image);
|
|
|
|
|
pixmap.setDevicePixelRatio(devicePixelRatio);
|
|
|
|
|
QPixmapCache::insert(pixmapName, pixmap);
|
2010-02-17 17:09:00 +01:00
|
|
|
}
|
|
|
|
|
int xOffset = r.x() + (r.width() - size)/2;
|
|
|
|
|
int yOffset = r.y() + (r.height() - size)/2;
|
|
|
|
|
painter->drawPixmap(xOffset, yOffset, pixmap);
|
|
|
|
|
}
|
2023-04-21 14:49:26 +02:00
|
|
|
|
|
|
|
|
void StyleHelper::drawPanelBgRect(QPainter *painter, const QRectF &rect, const QBrush &brush)
|
|
|
|
|
{
|
|
|
|
|
if (toolbarStyle() == ToolbarStyleCompact) {
|
|
|
|
|
painter->fillRect(rect.toRect(), brush);
|
|
|
|
|
} else {
|
|
|
|
|
constexpr int margin = 2;
|
|
|
|
|
constexpr int radius = 5;
|
|
|
|
|
QPainterPath path;
|
|
|
|
|
path.addRoundedRect(rect.adjusted(margin, margin, -margin, -margin), radius, radius);
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
painter->fillPath(path, brush);
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-17 17:09:00 +01:00
|
|
|
|
2009-11-09 11:47:05 +01:00
|
|
|
void StyleHelper::menuGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect)
|
|
|
|
|
{
|
|
|
|
|
if (StyleHelper::usePixmapCache()) {
|
2019-05-27 17:23:59 +02:00
|
|
|
const QString key = QString::asprintf("mh_menu %d %d %d %d %d",
|
2009-11-09 11:47:05 +01:00
|
|
|
spanRect.width(), spanRect.height(), clipRect.width(),
|
|
|
|
|
clipRect.height(), StyleHelper::baseColor().rgb());
|
|
|
|
|
|
|
|
|
|
QPixmap pixmap;
|
2019-02-11 10:17:53 +01:00
|
|
|
if (!QPixmapCache::find(key, &pixmap)) {
|
2009-11-09 11:47:05 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
painter->drawPixmap(clipRect.topLeft(), pixmap);
|
2009-11-09 11:47:05 +01:00
|
|
|
} else {
|
|
|
|
|
menuGradientHelper(painter, spanRect, clipRect);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-05 11:06:05 +02:00
|
|
|
|
2023-05-11 13:15:55 +02:00
|
|
|
bool StyleHelper::usePixmapCache()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 19:26:58 +01:00
|
|
|
QPixmap StyleHelper::disabledSideBarIcon(const QPixmap &enabledicon)
|
|
|
|
|
{
|
|
|
|
|
QImage im = enabledicon.toImage().convertToFormat(QImage::Format_ARGB32);
|
|
|
|
|
for (int y=0; y<im.height(); ++y) {
|
2018-07-19 16:39:41 +02:00
|
|
|
auto scanLine = reinterpret_cast<QRgb*>(im.scanLine(y));
|
2015-11-11 19:26:58 +01:00
|
|
|
for (int x=0; x<im.width(); ++x) {
|
|
|
|
|
QRgb pixel = *scanLine;
|
|
|
|
|
char intensity = char(qGray(pixel));
|
|
|
|
|
*scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel));
|
|
|
|
|
++scanLine;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QPixmap::fromImage(im);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-15 17:09:57 +01:00
|
|
|
// Draws a cached pixmap with shadow
|
2010-03-23 15:47:43 +01:00
|
|
|
void StyleHelper::drawIconWithShadow(const QIcon &icon, const QRect &rect,
|
2013-01-16 22:06:59 +01:00
|
|
|
QPainter *p, QIcon::Mode iconMode, int dipRadius, const QColor &color, const QPoint &dipOffset)
|
2010-03-15 17:09:57 +01:00
|
|
|
{
|
|
|
|
|
QPixmap cache;
|
2017-09-12 18:55:14 +02:00
|
|
|
const int devicePixelRatio = p->device()->devicePixelRatio();
|
|
|
|
|
QString pixmapName = QString::fromLatin1("icon %0 %1 %2 %3")
|
|
|
|
|
.arg(icon.cacheKey()).arg(iconMode).arg(rect.height()).arg(devicePixelRatio);
|
2010-03-23 15:47:43 +01:00
|
|
|
|
2019-02-11 10:17:53 +01:00
|
|
|
if (!QPixmapCache::find(pixmapName, &cache)) {
|
2013-01-16 22:06:59 +01:00
|
|
|
// High-dpi support: The in parameters (rect, radius, offset) are in
|
|
|
|
|
// device-independent pixels. The call to QIcon::pixmap() below might
|
|
|
|
|
// return a high-dpi pixmap, which will in that case have a devicePixelRatio
|
|
|
|
|
// different than 1. The shadow drawing caluculations are done in device
|
|
|
|
|
// pixels.
|
2017-09-12 18:55:14 +02:00
|
|
|
QWindow *window = dynamic_cast<QWidget*>(p->device())->window()->windowHandle();
|
2015-11-11 19:26:58 +01:00
|
|
|
QPixmap px = icon.pixmap(window, rect.size(), iconMode);
|
2013-01-16 22:06:59 +01:00
|
|
|
int radius = dipRadius * devicePixelRatio;
|
|
|
|
|
QPoint offset = dipOffset * devicePixelRatio;
|
2010-03-15 17:09:57 +01:00
|
|
|
cache = QPixmap(px.size() + QSize(radius * 2, radius * 2));
|
|
|
|
|
cache.fill(Qt::transparent);
|
2010-03-23 15:47:43 +01:00
|
|
|
|
2010-03-15 17:09:57 +01:00
|
|
|
QPainter cachePainter(&cache);
|
2010-03-23 15:47:43 +01:00
|
|
|
if (iconMode == QIcon::Disabled) {
|
2017-09-12 18:55:14 +02:00
|
|
|
const bool hasDisabledState =
|
|
|
|
|
icon.availableSizes().count() == icon.availableSizes(QIcon::Disabled).count();
|
2015-11-11 19:26:58 +01:00
|
|
|
if (!hasDisabledState)
|
|
|
|
|
px = disabledSideBarIcon(icon.pixmap(window, rect.size()));
|
2016-06-17 18:52:24 +02:00
|
|
|
} else if (creatorTheme()->flag(Theme::ToolBarIconShadow)) {
|
2015-11-11 19:26:58 +01:00
|
|
|
// Draw shadow
|
|
|
|
|
QImage tmp(px.size() + QSize(radius * 2, radius * 2 + 1), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
tmp.fill(Qt::transparent);
|
|
|
|
|
|
|
|
|
|
QPainter tmpPainter(&tmp);
|
|
|
|
|
tmpPainter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
|
tmpPainter.drawPixmap(QRect(radius, radius, px.width(), px.height()), px);
|
|
|
|
|
tmpPainter.end();
|
|
|
|
|
|
|
|
|
|
// blur the alpha channel
|
|
|
|
|
QImage blurred(tmp.size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
|
blurred.fill(Qt::transparent);
|
|
|
|
|
QPainter blurPainter(&blurred);
|
|
|
|
|
qt_blurImage(&blurPainter, tmp, radius, false, true);
|
|
|
|
|
blurPainter.end();
|
|
|
|
|
|
|
|
|
|
tmp = blurred;
|
|
|
|
|
|
|
|
|
|
// blacken the image...
|
|
|
|
|
tmpPainter.begin(&tmp);
|
|
|
|
|
tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
|
tmpPainter.fillRect(tmp.rect(), color);
|
|
|
|
|
tmpPainter.end();
|
|
|
|
|
|
|
|
|
|
tmpPainter.begin(&tmp);
|
|
|
|
|
tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
|
|
|
|
tmpPainter.fillRect(tmp.rect(), color);
|
|
|
|
|
tmpPainter.end();
|
|
|
|
|
|
|
|
|
|
// draw the blurred drop shadow...
|
|
|
|
|
cachePainter.drawImage(QRect(0, 0, cache.rect().width(), cache.rect().height()), tmp);
|
2010-03-23 15:47:43 +01:00
|
|
|
}
|
2010-03-15 17:09:57 +01:00
|
|
|
|
|
|
|
|
// Draw the actual pixmap...
|
2013-01-16 22:06:59 +01:00
|
|
|
cachePainter.drawPixmap(QRect(QPoint(radius, radius) + offset, QSize(px.width(), px.height())), px);
|
2017-02-06 12:25:15 +01:00
|
|
|
cachePainter.end();
|
2013-01-16 22:06:59 +01:00
|
|
|
cache.setDevicePixelRatio(devicePixelRatio);
|
2010-03-15 17:09:57 +01:00
|
|
|
QPixmapCache::insert(pixmapName, cache);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-23 15:47:43 +01:00
|
|
|
QRect targetRect = cache.rect();
|
2014-08-29 13:18:41 +02:00
|
|
|
targetRect.setSize(targetRect.size() / cache.devicePixelRatio());
|
2013-01-16 22:06:59 +01:00
|
|
|
targetRect.moveCenter(rect.center() - dipOffset);
|
|
|
|
|
p->drawPixmap(targetRect, cache);
|
2010-03-15 17:09:57 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-24 19:23:40 +01:00
|
|
|
// Draws a CSS-like border image where the defined borders are not stretched
|
2014-07-10 13:45:35 +02:00
|
|
|
// Unit for rect, left, top, right and bottom is user pixels
|
2014-05-19 18:26:28 +03:00
|
|
|
void StyleHelper::drawCornerImage(const QImage &img, QPainter *painter, const QRect &rect,
|
2010-02-24 19:23:40 +01:00
|
|
|
int left, int top, int right, int bottom)
|
|
|
|
|
{
|
2014-07-10 13:45:35 +02:00
|
|
|
// source rect for drawImage() calls needs to be specified in DIP unit of the image
|
2014-07-14 22:15:17 -07:00
|
|
|
const qreal imagePixelRatio = img.devicePixelRatio();
|
2014-07-10 13:45:35 +02:00
|
|
|
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();
|
2010-02-24 19:23:40 +01:00
|
|
|
if (top > 0) { //top
|
2014-07-10 13:45:35 +02:00
|
|
|
painter->drawImage(QRectF(rect.left() + left, rect.top(), rect.width() -right - left, top), img,
|
|
|
|
|
QRectF(leftDIP, 0, size.width() - rightDIP - leftDIP, topDIP));
|
2010-02-24 19:23:40 +01:00
|
|
|
if (left > 0) //top-left
|
2014-07-10 13:45:35 +02:00
|
|
|
painter->drawImage(QRectF(rect.left(), rect.top(), left, top), img,
|
|
|
|
|
QRectF(0, 0, leftDIP, topDIP));
|
2010-02-24 19:23:40 +01:00
|
|
|
if (right > 0) //top-right
|
2014-07-10 13:45:35 +02:00
|
|
|
painter->drawImage(QRectF(rect.left() + rect.width() - right, rect.top(), right, top), img,
|
|
|
|
|
QRectF(size.width() - rightDIP, 0, rightDIP, topDIP));
|
2010-02-24 19:23:40 +01:00
|
|
|
}
|
|
|
|
|
//left
|
|
|
|
|
if (left > 0)
|
2014-07-10 13:45:35 +02:00
|
|
|
painter->drawImage(QRectF(rect.left(), rect.top()+top, left, rect.height() - top - bottom), img,
|
|
|
|
|
QRectF(0, topDIP, leftDIP, size.height() - bottomDIP - topDIP));
|
2010-02-24 19:23:40 +01:00
|
|
|
//center
|
2014-07-10 13:45:35 +02:00
|
|
|
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));
|
2010-02-24 19:23:40 +01:00
|
|
|
if (right > 0) //right
|
2014-07-10 13:45:35 +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));
|
2010-02-24 19:23:40 +01:00
|
|
|
if (bottom > 0) { //bottom
|
2014-07-10 13:45:35 +02:00
|
|
|
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));
|
|
|
|
|
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));
|
2010-02-24 19:23:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-02 17:02:53 +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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 14:06:42 +01:00
|
|
|
QLinearGradient StyleHelper::statusBarGradient(const QRect &statusBarRect)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2010-08-02 17:02:53 +02:00
|
|
|
|
2023-04-21 22:10:24 +02:00
|
|
|
void StyleHelper::setPanelWidget(QWidget *widget, bool value)
|
|
|
|
|
{
|
2023-05-09 13:20:04 +02:00
|
|
|
widget->setProperty(C_PANEL_WIDGET, value);
|
2023-04-21 22:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StyleHelper::setPanelWidgetSingleRow(QWidget *widget, bool value)
|
|
|
|
|
{
|
2023-05-09 13:20:04 +02:00
|
|
|
widget->setProperty(C_PANEL_WIDGET_SINGLE_ROW, value);
|
2023-04-21 22:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 17:25:10 +01:00
|
|
|
bool StyleHelper::isQDSTheme()
|
|
|
|
|
{
|
2023-02-01 11:06:22 +01:00
|
|
|
return creatorTheme() ? creatorTheme()->flag(Theme::QDSTheme) : false;
|
2023-01-30 17:25:10 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 17:17:39 +02:00
|
|
|
QIcon StyleHelper::getIconFromIconFont(const QString &fontName, const QList<IconFontHelper> ¶meters)
|
|
|
|
|
{
|
|
|
|
|
QFontDatabase a;
|
|
|
|
|
|
|
|
|
|
QTC_ASSERT(a.hasFamily(fontName), {});
|
|
|
|
|
|
|
|
|
|
if (!a.hasFamily(fontName))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
|
|
|
|
|
for (const IconFontHelper &p : parameters) {
|
|
|
|
|
const int maxDpr = qRound(qApp->devicePixelRatio());
|
|
|
|
|
for (int dpr = 1; dpr <= maxDpr; dpr++) {
|
|
|
|
|
QPixmap pixmap(p.size() * dpr);
|
|
|
|
|
pixmap.setDevicePixelRatio(dpr);
|
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
|
|
QFont font(fontName);
|
|
|
|
|
font.setPixelSize(p.size().height());
|
|
|
|
|
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
painter.save();
|
|
|
|
|
painter.setPen(p.color());
|
|
|
|
|
painter.setFont(font);
|
|
|
|
|
painter.drawText(QRectF(QPoint(0, 0), p.size()), p.iconSymbol());
|
|
|
|
|
painter.restore();
|
|
|
|
|
|
|
|
|
|
icon.addPixmap(pixmap, p.mode(), p.state());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 09:34:38 +02:00
|
|
|
QIcon StyleHelper::getIconFromIconFont(const QString &fontName, const QString &iconSymbol, int fontSize, int iconSize, QColor color)
|
2020-04-07 19:17:33 +02:00
|
|
|
{
|
|
|
|
|
QFontDatabase a;
|
|
|
|
|
|
2020-04-20 09:34:38 +02:00
|
|
|
QTC_ASSERT(a.hasFamily(fontName), {});
|
2020-04-07 19:17:33 +02:00
|
|
|
|
|
|
|
|
if (a.hasFamily(fontName)) {
|
|
|
|
|
|
2020-04-20 09:34:38 +02:00
|
|
|
QIcon icon;
|
|
|
|
|
QSize size(iconSize, iconSize);
|
|
|
|
|
|
|
|
|
|
const int maxDpr = qRound(qApp->devicePixelRatio());
|
|
|
|
|
for (int dpr = 1; dpr <= maxDpr; dpr++) {
|
|
|
|
|
QPixmap pixmap(size * dpr);
|
|
|
|
|
pixmap.setDevicePixelRatio(dpr);
|
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
|
|
QFont font(fontName);
|
2020-05-29 10:42:02 +02:00
|
|
|
font.setPixelSize(fontSize);
|
2020-04-20 09:34:38 +02:00
|
|
|
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
painter.save();
|
|
|
|
|
painter.setPen(color);
|
|
|
|
|
painter.setFont(font);
|
2023-02-03 19:39:10 +01:00
|
|
|
painter.drawText(QRectF(QPoint(0, 0), size), Qt::AlignCenter, iconSymbol);
|
2020-04-20 09:34:38 +02:00
|
|
|
painter.restore();
|
2020-04-07 19:17:33 +02:00
|
|
|
|
2020-04-20 09:34:38 +02:00
|
|
|
icon.addPixmap(pixmap);
|
|
|
|
|
}
|
2020-04-07 19:17:33 +02:00
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 09:34:38 +02:00
|
|
|
QIcon StyleHelper::getIconFromIconFont(const QString &fontName, const QString &iconSymbol, int fontSize, int iconSize)
|
|
|
|
|
{
|
|
|
|
|
QColor penColor = QApplication::palette("QWidget").color(QPalette::Normal, QPalette::ButtonText);
|
|
|
|
|
return getIconFromIconFont(fontName, iconSymbol, fontSize, iconSize, penColor);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 16:28:16 +01:00
|
|
|
QIcon StyleHelper::getCursorFromIconFont(const QString &fontName, const QString &cursorFill, const QString &cursorOutline,
|
|
|
|
|
int fontSize, int iconSize)
|
|
|
|
|
{
|
|
|
|
|
QFontDatabase a;
|
|
|
|
|
|
|
|
|
|
QTC_ASSERT(a.hasFamily(fontName), {});
|
|
|
|
|
|
|
|
|
|
const QColor outlineColor = Qt::black;
|
|
|
|
|
const QColor fillColor = Qt::white;
|
|
|
|
|
|
|
|
|
|
if (a.hasFamily(fontName)) {
|
|
|
|
|
|
|
|
|
|
QIcon icon;
|
|
|
|
|
QSize size(iconSize, iconSize);
|
|
|
|
|
|
|
|
|
|
const int maxDpr = qRound(qApp->devicePixelRatio());
|
|
|
|
|
for (int dpr = 1; dpr <= maxDpr; dpr++) {
|
|
|
|
|
QPixmap pixmap(size * dpr);
|
|
|
|
|
pixmap.setDevicePixelRatio(dpr);
|
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
|
|
QFont font(fontName);
|
|
|
|
|
font.setPixelSize(fontSize);
|
|
|
|
|
|
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
|
painter.save();
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
painter.setRenderHint(QPainter::TextAntialiasing, true);
|
|
|
|
|
painter.setRenderHint(QPainter::LosslessImageRendering, true);
|
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
|
|
|
|
|
|
|
|
|
|
painter.setFont(font);
|
|
|
|
|
painter.setPen(outlineColor);
|
|
|
|
|
painter.drawText(QRectF(QPointF(0.0, 0.0), size),
|
|
|
|
|
Qt::AlignCenter, cursorOutline);
|
|
|
|
|
|
|
|
|
|
painter.setPen(fillColor);
|
|
|
|
|
painter.drawText(QRectF(QPointF(0.0, 0.0), size),
|
|
|
|
|
Qt::AlignCenter, cursorFill);
|
|
|
|
|
|
|
|
|
|
painter.restore();
|
|
|
|
|
|
|
|
|
|
icon.addPixmap(pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-07-24 15:24:58 +02:00
|
|
|
QString StyleHelper::dpiSpecificImageFile(const QString &fileName)
|
2014-07-09 13:20:53 +02:00
|
|
|
{
|
|
|
|
|
// See QIcon::addFile()
|
|
|
|
|
if (qApp->devicePixelRatio() > 1.0) {
|
2015-10-15 16:34:54 +02:00
|
|
|
const QString atDprfileName =
|
|
|
|
|
imageFileWithResolution(fileName, qRound(qApp->devicePixelRatio()));
|
2023-09-01 11:23:56 +02:00
|
|
|
if (QFileInfo::exists(atDprfileName))
|
2015-10-15 16:34:54 +02:00
|
|
|
return atDprfileName;
|
2014-07-09 13:20:53 +02:00
|
|
|
}
|
2014-07-24 15:24:58 +02:00
|
|
|
return fileName;
|
2014-07-09 13:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
2015-10-15 16:34:54 +02:00
|
|
|
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();
|
|
|
|
|
for (int i = 1; i <= maxResolutions; ++i)
|
2023-09-01 11:23:56 +02:00
|
|
|
if (QFileInfo::exists(imageFileWithResolution(fileName, i)))
|
2015-10-15 16:34:54 +02:00
|
|
|
result.append(i);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 14:11:34 +01:00
|
|
|
double StyleHelper::luminance(const QColor &color)
|
2019-11-14 14:12:59 +01:00
|
|
|
{
|
|
|
|
|
// calculate the luminance based on
|
|
|
|
|
// https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
|
|
|
|
|
auto val = [](const double &colorVal) {
|
|
|
|
|
return colorVal < 0.03928 ? colorVal / 12.92 : std::pow((colorVal + 0.055) / 1.055, 2.4);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static QHash<QRgb, double> cache;
|
|
|
|
|
QHash<QRgb, double>::iterator it = cache.find(color.rgb());
|
|
|
|
|
if (it == cache.end()) {
|
|
|
|
|
it = cache.insert(color.rgb(), 0.2126 * val(color.redF())
|
|
|
|
|
+ 0.7152 * val(color.greenF())
|
|
|
|
|
+ 0.0722 * val(color.blueF()));
|
|
|
|
|
}
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double contrastRatio(const QColor &color1, const QColor &color2)
|
|
|
|
|
{
|
|
|
|
|
// calculate the contrast ratio based on
|
|
|
|
|
// https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
|
2019-11-14 14:11:34 +01:00
|
|
|
auto contrast = (StyleHelper::luminance(color1) + .05) / (StyleHelper::luminance(color2) + .05);
|
2019-11-14 14:12:59 +01:00
|
|
|
if (contrast < 1)
|
|
|
|
|
return 1 / contrast;
|
|
|
|
|
return contrast;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StyleHelper::isReadableOn(const QColor &background, const QColor &foreground)
|
|
|
|
|
{
|
|
|
|
|
// following the W3C Recommendation on contrast for large Text
|
|
|
|
|
// https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
|
|
|
|
|
return contrastRatio(background, foreground) > 3;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 11:20:53 +01:00
|
|
|
QColor StyleHelper::ensureReadableOn(const QColor &background, const QColor &desiredForeground)
|
|
|
|
|
{
|
|
|
|
|
if (isReadableOn(background, desiredForeground))
|
|
|
|
|
return desiredForeground;
|
|
|
|
|
|
|
|
|
|
int h, s, v;
|
|
|
|
|
QColor foreground = desiredForeground;
|
|
|
|
|
foreground.getHsv(&h, &s, &v);
|
|
|
|
|
// adjust the color value to ensure better readability
|
|
|
|
|
if (luminance(background) < .5)
|
|
|
|
|
v = v + 64;
|
|
|
|
|
else if (v >= 64)
|
|
|
|
|
v = v - 64;
|
|
|
|
|
v %= 256;
|
|
|
|
|
|
|
|
|
|
foreground.setHsv(h, s, v);
|
|
|
|
|
if (!isReadableOn(background, foreground)) {
|
|
|
|
|
s = (s + 128) % 256; // adjust the saturation to ensure better readability
|
|
|
|
|
foreground.setHsv(h, s, v);
|
|
|
|
|
if (!isReadableOn(background, foreground)) // we failed to create some better foreground
|
|
|
|
|
return desiredForeground;
|
|
|
|
|
}
|
|
|
|
|
return foreground;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-05 11:06:05 +02:00
|
|
|
} // namespace Utils
|