Utils: Turn StyleHelper into a namespace

It was in fact just a bag of static functions. Most importantly, it
fixes a previouls introduced build error.

Amends 5975657e77

Change-Id: Ia7de8bdcf91e1763f9d3b435316a5df9993717de
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Alessandro Portale
2023-05-11 13:15:55 +02:00
parent 1acd2499e2
commit fb406a26f4
3 changed files with 172 additions and 135 deletions

View File

@@ -37,6 +37,11 @@ static int range(float x, int min, int max)
namespace Utils { namespace Utils {
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;
QColor StyleHelper::mergedColors(const QColor &colorA, const QColor &colorB, int factor) QColor StyleHelper::mergedColors(const QColor &colorA, const QColor &colorB, int factor)
{ {
const int maxFactor = 100; const int maxFactor = 100;
@@ -59,6 +64,21 @@ QColor StyleHelper::alphaBlendedColors(const QColor &colorA, const QColor &color
); );
} }
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);
}
int StyleHelper::navigationWidgetHeight() int StyleHelper::navigationWidgetHeight()
{ {
return m_toolbarStyle == ToolbarStyleCompact ? 24 : 30; return m_toolbarStyle == ToolbarStyleCompact ? 24 : 30;
@@ -105,11 +125,6 @@ QColor StyleHelper::panelTextColor(bool lightColored)
return Qt::black; return Qt::black;
} }
StyleHelper::ToolbarStyle StyleHelper::m_toolbarStyle = StyleHelper::defaultToolbarStyle;
// Invalid by default, setBaseColor needs to be called at least once
QColor StyleHelper::m_baseColor;
QColor StyleHelper::m_requestedBaseColor;
QColor StyleHelper::baseColor(bool lightColored) QColor StyleHelper::baseColor(bool lightColored)
{ {
static const QColor windowColor = QApplication::palette().color(QPalette::Window); static const QColor windowColor = QApplication::palette().color(QPalette::Window);
@@ -118,6 +133,11 @@ QColor StyleHelper::baseColor(bool lightColored)
return (lightColored || windowColorAsBase) ? windowColor : m_baseColor; return (lightColored || windowColorAsBase) ? windowColor : m_baseColor;
} }
QColor StyleHelper::requestedBaseColor()
{
return m_requestedBaseColor;
}
QColor StyleHelper::toolbarBaseColor(bool lightColored) QColor StyleHelper::toolbarBaseColor(bool lightColored)
{ {
if (creatorTheme()->flag(Theme::QDSTheme)) if (creatorTheme()->flag(Theme::QDSTheme))
@@ -166,6 +186,11 @@ QColor StyleHelper::toolBarBorderColor()
clamp(base.value() * 0.80f)); clamp(base.value() * 0.80f));
} }
QColor StyleHelper::buttonTextColor()
{
return QColor(0x4c4c4c);
}
// We try to ensure that the actual color used are within // We try to ensure that the actual color used are within
// reasonalbe bounds while generating the actual baseColor // reasonalbe bounds while generating the actual baseColor
// from the users request. // from the users request.
@@ -495,6 +520,11 @@ void StyleHelper::menuGradient(QPainter *painter, const QRect &spanRect, const Q
} }
} }
bool StyleHelper::usePixmapCache()
{
return true;
}
QPixmap StyleHelper::disabledSideBarIcon(const QPixmap &enabledicon) QPixmap StyleHelper::disabledSideBarIcon(const QPixmap &enabledicon)
{ {
QImage im = enabledicon.toImage().convertToFormat(QImage::Format_ARGB32); QImage im = enabledicon.toImage().convertToFormat(QImage::Format_ARGB32);

View File

@@ -13,143 +13,150 @@ class QPainter;
class QRect; class QRect;
// Note, this is exported but in a private header as qtopengl depends on it. // Note, this is exported but in a private header as qtopengl depends on it.
// We should consider adding this as a public helper function. // We should consider adding this as a public helper function.
void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0); void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly,
int transposed = 0);
QT_END_NAMESPACE QT_END_NAMESPACE
// Helper class holding all custom color values // Helper class holding all custom color values
namespace Utils::StyleHelper {
namespace Utils { const unsigned int DEFAULT_BASE_COLOR = 0x666666;
class QTCREATOR_UTILS_EXPORT StyleHelper const int progressFadeAnimationDuration = 600;
constexpr char C_ALIGN_ARROW[] = "alignarrow";
constexpr char C_DRAW_LEFT_BORDER[] = "drawleftborder";
constexpr char C_ELIDE_MODE[] = "elidemode";
constexpr char C_HIDE_BORDER[] = "hideborder";
constexpr char C_HIDE_ICON[] = "hideicon";
constexpr char C_HIGHLIGHT_WIDGET[] = "highlightWidget";
constexpr char C_LIGHT_COLORED[] = "lightColored";
constexpr char C_MINI_SPLITTER[] = "minisplitter";
constexpr char C_NOT_ELIDE_ASTERISK[] = "notelideasterisk";
constexpr char C_NO_ARROW[] = "noArrow";
constexpr char C_PANEL_WIDGET[] = "panelwidget";
constexpr char C_PANEL_WIDGET_SINGLE_ROW[] = "panelwidget_singlerow";
constexpr char C_SHOW_BORDER[] = "showborder";
constexpr char C_TOP_BORDER[] = "topBorder";
enum ToolbarStyle {
ToolbarStyleCompact,
ToolbarStyleRelaxed,
};
constexpr ToolbarStyle defaultToolbarStyle = ToolbarStyleCompact;
// Height of the project explorer navigation bar
QTCREATOR_UTILS_EXPORT int navigationWidgetHeight();
QTCREATOR_UTILS_EXPORT void setToolbarStyle(ToolbarStyle style);
QTCREATOR_UTILS_EXPORT ToolbarStyle toolbarStyle();
QTCREATOR_UTILS_EXPORT qreal sidebarFontSize();
QTCREATOR_UTILS_EXPORT QPalette sidebarFontPalette(const QPalette &original);
// This is our color table, all colors derive from baseColor
QTCREATOR_UTILS_EXPORT QColor requestedBaseColor();
QTCREATOR_UTILS_EXPORT QColor baseColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor toolbarBaseColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor panelTextColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor highlightColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor shadowColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor borderColor(bool lightColored = false);
QTCREATOR_UTILS_EXPORT QColor toolBarBorderColor();
QTCREATOR_UTILS_EXPORT QColor buttonTextColor();
QTCREATOR_UTILS_EXPORT QColor mergedColors(const QColor &colorA, const QColor &colorB,
int factor = 50);
QTCREATOR_UTILS_EXPORT QColor alphaBlendedColors(const QColor &colorA, const QColor &colorB);
QTCREATOR_UTILS_EXPORT QColor sidebarHighlight();
QTCREATOR_UTILS_EXPORT QColor sidebarShadow();
QTCREATOR_UTILS_EXPORT QColor toolBarDropShadowColor();
QTCREATOR_UTILS_EXPORT QColor notTooBrightHighlightColor();
// Sets the base color and makes sure all top level widgets are updated
QTCREATOR_UTILS_EXPORT void setBaseColor(const QColor &color);
// Draws a shaded anti-aliased arrow
QTCREATOR_UTILS_EXPORT void drawArrow(QStyle::PrimitiveElement element, QPainter *painter,
const QStyleOption *option);
QTCREATOR_UTILS_EXPORT void drawMinimalArrow(QStyle::PrimitiveElement element, QPainter *painter,
const QStyleOption *option);
QTCREATOR_UTILS_EXPORT void drawPanelBgRect(QPainter *painter, const QRectF &rect,
const QBrush &brush);
// Gradients used for panels
QTCREATOR_UTILS_EXPORT void horizontalGradient(QPainter *painter, const QRect &spanRect,
const QRect &clipRect, bool lightColored = false);
QTCREATOR_UTILS_EXPORT void verticalGradient(QPainter *painter, const QRect &spanRect,
const QRect &clipRect, bool lightColored = false);
QTCREATOR_UTILS_EXPORT void menuGradient(QPainter *painter, const QRect &spanRect,
const QRect &clipRect);
QTCREATOR_UTILS_EXPORT bool usePixmapCache();
QTCREATOR_UTILS_EXPORT QPixmap disabledSideBarIcon(const QPixmap &enabledicon);
QTCREATOR_UTILS_EXPORT void drawIconWithShadow(const QIcon &icon, const QRect &rect, QPainter *p,
QIcon::Mode iconMode, int dipRadius = 3,
const QColor &color = QColor(0, 0, 0, 130),
const QPoint &dipOffset = QPoint(1, -2));
QTCREATOR_UTILS_EXPORT void drawCornerImage(const QImage &img, QPainter *painter, const QRect &rect,
int left = 0, int top = 0, int right = 0,
int bottom = 0);
QTCREATOR_UTILS_EXPORT void tintImage(QImage &img, const QColor &tintColor);
QTCREATOR_UTILS_EXPORT QLinearGradient statusBarGradient(const QRect &statusBarRect);
QTCREATOR_UTILS_EXPORT void setPanelWidget(QWidget *widget, bool value = true);
QTCREATOR_UTILS_EXPORT void setPanelWidgetSingleRow(QWidget *widget, bool value = true);
QTCREATOR_UTILS_EXPORT bool isQDSTheme();
class IconFontHelper
{ {
public: public:
static const unsigned int DEFAULT_BASE_COLOR = 0x666666; IconFontHelper(const QString &iconSymbol,
static const int progressFadeAnimationDuration = 600; const QColor &color,
const QSize &size,
QIcon::Mode mode = QIcon::Normal,
QIcon::State state = QIcon::Off)
: m_iconSymbol(iconSymbol)
, m_color(color)
, m_size(size)
, m_mode(mode)
, m_state(state)
{}
constexpr static char C_ALIGN_ARROW[] = "alignarrow"; QString iconSymbol() const { return m_iconSymbol; }
constexpr static char C_DRAW_LEFT_BORDER[] = "drawleftborder"; QColor color() const { return m_color; }
constexpr static char C_ELIDE_MODE[] = "elidemode"; QSize size() const { return m_size; }
constexpr static char C_HIDE_BORDER[] = "hideborder"; QIcon::Mode mode() const { return m_mode; }
constexpr static char C_HIDE_ICON[] = "hideicon"; QIcon::State state() const { return m_state; }
constexpr static char C_HIGHLIGHT_WIDGET[] = "highlightWidget";
constexpr static char C_LIGHT_COLORED[] = "lightColored";
constexpr static char C_MINI_SPLITTER[] = "minisplitter";
constexpr static char C_NOT_ELIDE_ASTERISK[] = "notelideasterisk";
constexpr static char C_NO_ARROW[] = "noArrow";
constexpr static char C_PANEL_WIDGET[] = "panelwidget";
constexpr static char C_PANEL_WIDGET_SINGLE_ROW[] = "panelwidget_singlerow";
constexpr static char C_SHOW_BORDER[] = "showborder";
constexpr static char C_TOP_BORDER[] = "topBorder";
enum ToolbarStyle {
ToolbarStyleCompact,
ToolbarStyleRelaxed,
};
// Height of the project explorer navigation bar
static int navigationWidgetHeight();
static void setToolbarStyle(ToolbarStyle style);
static ToolbarStyle toolbarStyle();
static constexpr ToolbarStyle defaultToolbarStyle = ToolbarStyleCompact;
static qreal sidebarFontSize();
static QPalette sidebarFontPalette(const QPalette &original);
// This is our color table, all colors derive from baseColor
static QColor requestedBaseColor() { return m_requestedBaseColor; }
static QColor baseColor(bool lightColored = false);
static QColor toolbarBaseColor(bool lightColored = false);
static QColor panelTextColor(bool lightColored = false);
static QColor highlightColor(bool lightColored = false);
static QColor shadowColor(bool lightColored = false);
static QColor borderColor(bool lightColored = false);
static QColor toolBarBorderColor();
static QColor buttonTextColor() { return QColor(0x4c4c4c); }
static QColor mergedColors(const QColor &colorA, const QColor &colorB, int factor = 50);
static QColor alphaBlendedColors(const QColor &colorA, const QColor &colorB);
static QColor sidebarHighlight() { return QColor(255, 255, 255, 40); }
static QColor sidebarShadow() { return QColor(0, 0, 0, 40); }
static QColor toolBarDropShadowColor() { return QColor(0, 0, 0, 70); }
static QColor notTooBrightHighlightColor();
// Sets the base color and makes sure all top level widgets are updated
static void setBaseColor(const QColor &color);
// Draws a shaded anti-aliased arrow
static void drawArrow(QStyle::PrimitiveElement element, QPainter *painter, const QStyleOption *option);
static void drawMinimalArrow(QStyle::PrimitiveElement element, QPainter *painter, const QStyleOption *option);
static void drawPanelBgRect(QPainter *painter, const QRectF &rect, const QBrush &brush);
// Gradients used for panels
static void horizontalGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect, bool lightColored = false);
static void verticalGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect, bool lightColored = false);
static void menuGradient(QPainter *painter, const QRect &spanRect, const QRect &clipRect);
static bool usePixmapCache() { return true; }
static QPixmap disabledSideBarIcon(const QPixmap &enabledicon);
static void drawIconWithShadow(const QIcon &icon, const QRect &rect, QPainter *p, QIcon::Mode iconMode,
int dipRadius = 3, const QColor &color = QColor(0, 0, 0, 130),
const QPoint &dipOffset = QPoint(1, -2));
static void drawCornerImage(const QImage &img, QPainter *painter, const QRect &rect,
int left = 0, int top = 0, int right = 0, int bottom = 0);
static void tintImage(QImage &img, const QColor &tintColor);
static QLinearGradient statusBarGradient(const QRect &statusBarRect);
static void setPanelWidget(QWidget *widget, bool value = true);
static void setPanelWidgetSingleRow(QWidget *widget, bool value = true);
static bool isQDSTheme();
class IconFontHelper
{
public:
IconFontHelper(const QString &iconSymbol,
const QColor &color,
const QSize &size,
QIcon::Mode mode = QIcon::Normal,
QIcon::State state = QIcon::Off)
: m_iconSymbol(iconSymbol)
, m_color(color)
, m_size(size)
, m_mode(mode)
, m_state(state)
{}
QString iconSymbol() const { return m_iconSymbol; }
QColor color() const { return m_color; }
QSize size() const { return m_size; }
QIcon::Mode mode() const { return m_mode; }
QIcon::State state() const { return m_state; }
private:
QString m_iconSymbol;
QColor m_color;
QSize m_size;
QIcon::Mode m_mode;
QIcon::State m_state;
};
static QIcon getIconFromIconFont(const QString &fontName, const QList<IconFontHelper> &parameters);
static QIcon getIconFromIconFont(const QString &fontName, const QString &iconSymbol, int fontSize, int iconSize, QColor color);
static QIcon getIconFromIconFont(const QString &fontName, const QString &iconSymbol, int fontSize, int iconSize);
static QIcon getCursorFromIconFont(const QString &fontname, const QString &cursorFill, const QString &cursorOutline,
int fontSize, int iconSize);
static QString dpiSpecificImageFile(const QString &fileName);
static QString imageFileWithResolution(const QString &fileName, int dpr);
static QList<int> availableImageResolutions(const QString &fileName);
static double luminance(const QColor &color);
static bool isReadableOn(const QColor &background, const QColor &foreground);
// returns a foreground color readable on background (desiredForeground if already readable or adaption fails)
static QColor ensureReadableOn(const QColor &background, const QColor &desiredForeground);
private: private:
static ToolbarStyle m_toolbarStyle; QString m_iconSymbol;
static QColor m_baseColor; QColor m_color;
static QColor m_requestedBaseColor; QSize m_size;
QIcon::Mode m_mode;
QIcon::State m_state;
}; };
} // namespace Utils QTCREATOR_UTILS_EXPORT QIcon getIconFromIconFont(const QString &fontName,
const QList<IconFontHelper> &parameters);
QTCREATOR_UTILS_EXPORT QIcon getIconFromIconFont(const QString &fontName,
const QString &iconSymbol, int fontSize,
int iconSize, QColor color);
QTCREATOR_UTILS_EXPORT QIcon getIconFromIconFont(const QString &fontName,
const QString &iconSymbol, int fontSize,
int iconSize);
QTCREATOR_UTILS_EXPORT QIcon getCursorFromIconFont(const QString &fontname,
const QString &cursorFill,
const QString &cursorOutline,
int fontSize, int iconSize);
QTCREATOR_UTILS_EXPORT QString dpiSpecificImageFile(const QString &fileName);
QTCREATOR_UTILS_EXPORT QString imageFileWithResolution(const QString &fileName, int dpr);
QTCREATOR_UTILS_EXPORT QList<int> availableImageResolutions(const QString &fileName);
QTCREATOR_UTILS_EXPORT double luminance(const QColor &color);
QTCREATOR_UTILS_EXPORT bool isReadableOn(const QColor &background, const QColor &foreground);
// returns a foreground color readable on background (desiredForeground if already readable or adaption fails)
QTCREATOR_UTILS_EXPORT QColor ensureReadableOn(const QColor &background,
const QColor &desiredForeground);
} // namespace Utils::StyleHelper

View File

@@ -154,7 +154,7 @@ QPixmap getDeletePixmap(bool enabled,
{ {
using Utils::Theme; using Utils::Theme;
using Utils::creatorTheme; using Utils::creatorTheme;
using Utils::StyleHelper; using namespace Utils::StyleHelper;
const double xRatio = 19; const double xRatio = 19;
const double yRatio = 9; const double yRatio = 9;