Icons: Add FilePath based API

Don't completely remove the QString based API though, since many icons
are constructed from constants and that would add a lot of noise with
FilePath.

Change-Id: I6a1b53bb1f1a79bb2b5756f94d5a2e7ca4cf0dc4
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2021-05-26 09:58:29 +02:00
parent 6e7779d31b
commit 6d394ff29e
4 changed files with 51 additions and 33 deletions

View File

@@ -63,10 +63,12 @@ static MasksAndColors masksAndColors(const Icon &icon, int dpr)
{
MasksAndColors result;
for (const IconMaskAndColor &i: icon) {
const QString &fileName = i.first;
const QString &fileName = i.first.toString();
const QColor color = creatorTheme()->color(i.second);
const QString dprFileName = StyleHelper::availableImageResolutions(i.first).contains(dpr) ?
StyleHelper::imageFileWithResolution(fileName, dpr) : fileName;
const QString dprFileName = StyleHelper::availableImageResolutions(i.first.toString())
.contains(dpr)
? StyleHelper::imageFileWithResolution(fileName, dpr)
: fileName;
QPixmap pixmap;
if (!pixmap.load(dprFileName)) {
pixmap = QPixmap(1, 1);
@@ -161,18 +163,30 @@ Icon::Icon(std::initializer_list<IconMaskAndColor> args, Icon::IconStyleOptions
{
}
Icon::Icon(const QString &imageFileName)
: m_style(None)
Icon::Icon(std::initializer_list<IconStringMaskAndColor> args, Icon::IconStyleOptions style)
: m_style(style)
{
reserve(args.size());
for (const IconStringMaskAndColor &i : args)
append({FilePath::fromString(i.first), i.second});
}
Icon::Icon(const FilePath &imageFileName)
{
append({imageFileName, Theme::Color(-1)});
}
Icon::Icon(const QString &imageFileName)
: Icon(FilePath::fromString(imageFileName))
{
}
QIcon Icon::icon() const
{
if (isEmpty()) {
return QIcon();
} else if (m_style == None) {
return QIcon(constFirst().first);
return QIcon(constFirst().first.toString());
} else {
QIcon result;
const int maxDpr = qRound(qApp->devicePixelRatio());
@@ -193,7 +207,7 @@ QPixmap Icon::pixmap(QIcon::Mode iconMode) const
if (isEmpty()) {
return QPixmap();
} else if (m_style == None) {
return QPixmap(StyleHelper::dpiSpecificImageFile(constFirst().first));
return QPixmap(StyleHelper::dpiSpecificImageFile(constFirst().first.toString()));
} else {
const MasksAndColors masks =
masksAndColors(*this, qRound(qApp->devicePixelRatio()));
@@ -204,9 +218,9 @@ QPixmap Icon::pixmap(QIcon::Mode iconMode) const
}
}
QString Icon::imageFileName() const
FilePath Icon::imageFilePath() const
{
QTC_ASSERT(length() == 1, return QString());
QTC_ASSERT(length() == 1, return {});
return first().first;
}

View File

@@ -25,8 +25,9 @@
#pragma once
#include "utils_global.h"
#include "fileutils.h"
#include "theme/theme.h"
#include "utils_global.h"
#include <QIcon>
#include <QPair>
@@ -38,7 +39,8 @@ QT_FORWARD_DECLARE_CLASS(QString)
namespace Utils {
using IconMaskAndColor = QPair<QString, Theme::Color>;
using IconMaskAndColor = QPair<FilePath, Theme::Color>;
using IconStringMaskAndColor = QPair<QString, Theme::Color>;
// Returns a recolored icon with shadow and custom disabled state for a
// series of grayscalemask|Theme::Color mask pairs
@@ -59,7 +61,10 @@ public:
Icon();
Icon(std::initializer_list<IconMaskAndColor> args, IconStyleOptions style = ToolBarStyle);
Icon(std::initializer_list<IconStringMaskAndColor> args, IconStyleOptions style = ToolBarStyle);
Icon(const FilePath &imageFileName);
Icon(const QString &imageFileName);
Icon(const Icon &other) = default;
QIcon icon() const;
@@ -68,7 +73,7 @@ public:
// Try to avoid it. it is just there for special API cases in Qt Creator
// where icons are still defined as filename.
QString imageFileName() const;
FilePath imageFilePath() const;
// Returns either the classic or a themed icon depending on
// the current Theme::FlatModeIcons flag.

View File

@@ -300,37 +300,37 @@ const Icon MACOS_TOUCHBAR_CLEAR(
QIcon CodeModelIcon::iconForType(CodeModelIcon::Type type)
{
static const IconMaskAndColor classRelationIcon {
static const IconStringMaskAndColor classRelationIcon {
QLatin1String(":/codemodel/images/classrelation.png"), Theme::IconsCodeModelOverlayForegroundColor};
static const IconMaskAndColor classRelationBackgroundIcon {
static const IconStringMaskAndColor classRelationBackgroundIcon {
QLatin1String(":/codemodel/images/classrelationbackground.png"), Theme::IconsCodeModelOverlayBackgroundColor};
static const IconMaskAndColor classMemberFunctionIcon {
static const IconStringMaskAndColor classMemberFunctionIcon {
QLatin1String(":/codemodel/images/classmemberfunction.png"), Theme::IconsCodeModelFunctionColor};
static const IconMaskAndColor classMemberVariableIcon {
static const IconStringMaskAndColor classMemberVariableIcon {
QLatin1String(":/codemodel/images/classmembervariable.png"), Theme::IconsCodeModelVariableColor};
static const IconMaskAndColor functionIcon {
static const IconStringMaskAndColor functionIcon {
QLatin1String(":/codemodel/images/member.png"), Theme::IconsCodeModelFunctionColor};
static const IconMaskAndColor variableIcon {
static const IconStringMaskAndColor variableIcon {
QLatin1String(":/codemodel/images/member.png"), Theme::IconsCodeModelVariableColor};
static const IconMaskAndColor signalIcon {
static const IconStringMaskAndColor signalIcon {
QLatin1String(":/codemodel/images/signal.png"), Theme::IconsCodeModelFunctionColor};
static const IconMaskAndColor slotIcon {
static const IconStringMaskAndColor slotIcon {
QLatin1String(":/codemodel/images/slot.png"), Theme::IconsCodeModelFunctionColor};
static const IconMaskAndColor propertyIcon {
static const IconStringMaskAndColor propertyIcon {
QLatin1String(":/codemodel/images/property.png"), Theme::IconsCodeModelOverlayForegroundColor};
static const IconMaskAndColor propertyBackgroundIcon {
static const IconStringMaskAndColor propertyBackgroundIcon {
QLatin1String(":/codemodel/images/propertybackground.png"), Theme::IconsCodeModelOverlayBackgroundColor};
static const IconMaskAndColor protectedIcon {
static const IconStringMaskAndColor protectedIcon {
QLatin1String(":/codemodel/images/protected.png"), Theme::IconsCodeModelOverlayForegroundColor};
static const IconMaskAndColor protectedBackgroundIcon {
static const IconStringMaskAndColor protectedBackgroundIcon {
QLatin1String(":/codemodel/images/protectedbackground.png"), Theme::IconsCodeModelOverlayBackgroundColor};
static const IconMaskAndColor privateIcon {
static const IconStringMaskAndColor privateIcon {
QLatin1String(":/codemodel/images/private.png"), Theme::IconsCodeModelOverlayForegroundColor};
static const IconMaskAndColor privateBackgroundIcon {
static const IconStringMaskAndColor privateBackgroundIcon {
QLatin1String(":/codemodel/images/privatebackground.png"), Theme::IconsCodeModelOverlayBackgroundColor};
static const IconMaskAndColor staticIcon {
static const IconStringMaskAndColor staticIcon {
QLatin1String(":/codemodel/images/static.png"), Theme::IconsCodeModelOverlayForegroundColor};
static const IconMaskAndColor staticBackgroundIcon {
static const IconStringMaskAndColor staticBackgroundIcon {
QLatin1String(":/codemodel/images/staticbackground.png"), Theme::IconsCodeModelOverlayBackgroundColor};
switch (type) {

View File

@@ -71,12 +71,11 @@ bool CompilationDatabaseProjectManagerPlugin::initialize(const QStringList &argu
d = new CompilationDatabaseProjectManagerPluginPrivate;
FileIconProvider::registerIconOverlayForFilename(Utils::Icons::PROJECT.imageFilePath().toString(),
COMPILE_COMMANDS_JSON);
FileIconProvider::registerIconOverlayForFilename(
Utils::Icons::PROJECT.imageFileName(),
COMPILE_COMMANDS_JSON);
FileIconProvider::registerIconOverlayForFilename(
Utils::Icons::PROJECT.imageFileName(),
QString(COMPILE_COMMANDS_JSON) + Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX);
Utils::Icons::PROJECT.imageFilePath().toString(),
QString(COMPILE_COMMANDS_JSON) + Constants::COMPILATIONDATABASEPROJECT_FILES_SUFFIX);
ProjectManager::registerProjectType<CompilationDatabaseProject>(
Constants::COMPILATIONDATABASEMIMETYPE);