Kaydet (Commit) 6fd7c0c7 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl Kaydeden (comit) Tomaž Vajngerl

ImplImageTree extract parameters to own struct

Change-Id: I56e68ab19e63637d21dea7929f093bb1aceb982a
Reviewed-on: https://gerrit.libreoffice.org/32135Reviewed-by: 's avatarTomaž Vajngerl <quikee@gmail.com>
Tested-by: 's avatarTomaž Vajngerl <quikee@gmail.com>
üst bf5f6df9
...@@ -38,6 +38,23 @@ namespace com { namespace sun { namespace star { namespace container { ...@@ -38,6 +38,23 @@ namespace com { namespace sun { namespace star { namespace container {
class XNameAccess; class XNameAccess;
}}}} }}}}
struct ImageRequestParameters
{
OUString msName;
OUString msStyle;
BitmapEx& mrBitmap;
bool mbLocalized;
ImageLoadFlags meFlags;
ImageRequestParameters(const OUString & rName, const OUString & rStyle, BitmapEx& rBitmap, bool bLocalized, ImageLoadFlags eFlags)
: msName(rName)
, msStyle(rStyle)
, mrBitmap(rBitmap)
, mbLocalized(bLocalized)
, meFlags(eFlags)
{}
};
class ImplImageTree class ImplImageTree
{ {
public: public:
...@@ -100,9 +117,7 @@ private: ...@@ -100,9 +117,7 @@ private:
return maIconSets[maCurrentStyle]; return maIconSets[maCurrentStyle];
} }
bool doLoadImage( bool doLoadImage(ImageRequestParameters& rParameters);
OUString const & name, OUString const & style,
BitmapEx & bitmap, bool localized, const ImageLoadFlags eFlags);
std::vector<OUString> getPaths(OUString const & name, LanguageTag& rLanguageTag); std::vector<OUString> getPaths(OUString const & name, LanguageTag& rLanguageTag);
...@@ -112,9 +127,9 @@ private: ...@@ -112,9 +127,9 @@ private:
void createStyle(); void createStyle();
bool iconCacheLookup(OUString const & rName, bool bLocalized, const ImageLoadFlags eFlags, BitmapEx & rBitmap); bool iconCacheLookup(ImageRequestParameters& rParameters);
bool findImage(std::vector<OUString> const & rPaths, BitmapEx & rBitmap, const ImageLoadFlags eFlags); bool findImage(std::vector<OUString> const & rPaths, ImageRequestParameters& rParameters);
void loadImageLinks(); void loadImageLinks();
......
...@@ -132,44 +132,44 @@ std::shared_ptr<SvStream> wrapStream(css::uno::Reference< css::io::XInputStream ...@@ -132,44 +132,44 @@ std::shared_ptr<SvStream> wrapStream(css::uno::Reference< css::io::XInputStream
return s; return s;
} }
void loadImageFromStream(std::shared_ptr<SvStream> const & xStream, OUString const & rPath, BitmapEx & rBitmap, const ImageLoadFlags eFlags) void loadImageFromStream(std::shared_ptr<SvStream> const & xStream, OUString const & rPath, ImageRequestParameters& rParameters)
{ {
static bool bIconsForDarkTheme = !!getenv("VCL_ICONS_FOR_DARK_THEME"); static bool bIconsForDarkTheme = !!getenv("VCL_ICONS_FOR_DARK_THEME");
bool bConvertToDarkTheme = bIconsForDarkTheme; bool bConvertToDarkTheme = bIconsForDarkTheme;
if (eFlags & ImageLoadFlags::IgnoreDarkTheme) if (rParameters.meFlags & ImageLoadFlags::IgnoreDarkTheme)
bConvertToDarkTheme = false; bConvertToDarkTheme = false;
float aScaleFactor = Application::GetDefaultDevice()->GetDPIScaleFactor(); float aScaleFactor = Application::GetDefaultDevice()->GetDPIScaleFactor();
if (eFlags & ImageLoadFlags::IgnoreScalingFactor) if (rParameters.meFlags & ImageLoadFlags::IgnoreScalingFactor)
aScaleFactor = 1.0f; aScaleFactor = 1.0f;
if (rPath.endsWith(".png")) if (rPath.endsWith(".png"))
{ {
vcl::PNGReader aPNGReader(*xStream); vcl::PNGReader aPNGReader(*xStream);
aPNGReader.SetIgnoreGammaChunk( true ); aPNGReader.SetIgnoreGammaChunk(true);
rBitmap = aPNGReader.Read(); rParameters.mrBitmap = aPNGReader.Read();
} }
else if (rPath.endsWith(".svg")) else if (rPath.endsWith(".svg"))
{ {
vcl::bitmap::loadFromSvg(*xStream.get(), rPath, rBitmap, double(aScaleFactor)); vcl::bitmap::loadFromSvg(*xStream.get(), rPath, rParameters.mrBitmap, double(aScaleFactor));
if (bConvertToDarkTheme) if (bConvertToDarkTheme)
rBitmap = BitmapProcessor::createLightImage(rBitmap); rParameters.mrBitmap = BitmapProcessor::createLightImage(rParameters.mrBitmap);
return; return;
} }
else else
{ {
ReadDIBBitmapEx(rBitmap, *xStream); ReadDIBBitmapEx(rParameters.mrBitmap, *xStream);
} }
if (bConvertToDarkTheme) if (bConvertToDarkTheme)
rBitmap = BitmapProcessor::createLightImage(rBitmap); rParameters.mrBitmap = BitmapProcessor::createLightImage(rParameters.mrBitmap);
if (aScaleFactor > 1.0f) if (aScaleFactor > 1.0f)
rBitmap.Scale(double(aScaleFactor), double(aScaleFactor), BmpScaleFlag::Fast); rParameters.mrBitmap.Scale(double(aScaleFactor), double(aScaleFactor), BmpScaleFlag::Fast);
} }
} } // end anonymous namespace
ImplImageTree::ImplImageTree() ImplImageTree::ImplImageTree()
{ {
...@@ -263,7 +263,8 @@ bool ImplImageTree::loadImage(OUString const & name, OUString const & style, Bit ...@@ -263,7 +263,8 @@ bool ImplImageTree::loadImage(OUString const & name, OUString const & style, Bit
{ {
try try
{ {
if (doLoadImage(name, aStyle, rBitmap, localized, eFlags)) ImageRequestParameters aParameters(name, aStyle, rBitmap, localized, eFlags);
if (doLoadImage(aParameters))
return true; return true;
} }
catch (css::uno::RuntimeException &) catch (css::uno::RuntimeException &)
...@@ -276,19 +277,20 @@ bool ImplImageTree::loadImage(OUString const & name, OUString const & style, Bit ...@@ -276,19 +277,20 @@ bool ImplImageTree::loadImage(OUString const & name, OUString const & style, Bit
bool ImplImageTree::loadDefaultImage(OUString const & style, BitmapEx& bitmap, const ImageLoadFlags eFlags) bool ImplImageTree::loadDefaultImage(OUString const & style, BitmapEx& bitmap, const ImageLoadFlags eFlags)
{ {
return doLoadImage("res/grafikde.png", style, bitmap, false, eFlags); ImageRequestParameters aParameters("res/grafikde.png", style, bitmap, false, eFlags);
return doLoadImage(aParameters);
} }
OUString createVariant(const ImageLoadFlags eFlags) OUString createVariant(ImageRequestParameters& rParameters)
{ {
static bool bIconsForDarkTheme = !!getenv("VCL_ICONS_FOR_DARK_THEME"); static bool bIconsForDarkTheme = !!getenv("VCL_ICONS_FOR_DARK_THEME");
bool bConvertToDarkTheme = bIconsForDarkTheme; bool bConvertToDarkTheme = bIconsForDarkTheme;
if (eFlags & ImageLoadFlags::IgnoreDarkTheme) if (rParameters.meFlags & ImageLoadFlags::IgnoreDarkTheme)
bConvertToDarkTheme = false; bConvertToDarkTheme = false;
sal_Int32 aScalePercentage = Application::GetDefaultDevice()->GetDPIScalePercentage(); sal_Int32 aScalePercentage = Application::GetDefaultDevice()->GetDPIScalePercentage();
if (eFlags & ImageLoadFlags::IgnoreScalingFactor) if (rParameters.meFlags & ImageLoadFlags::IgnoreScalingFactor)
aScalePercentage = 100; aScalePercentage = 100;
OUString aVariant; OUString aVariant;
...@@ -303,22 +305,22 @@ OUString createVariant(const ImageLoadFlags eFlags) ...@@ -303,22 +305,22 @@ OUString createVariant(const ImageLoadFlags eFlags)
return aVariant; return aVariant;
} }
bool loadDiskCachedVersion(OUString const & sStyle, OUString const & sVariant, OUString const & sName, BitmapEx & rBitmapEx) bool loadDiskCachedVersion(OUString const & sVariant, ImageRequestParameters& rParameters)
{ {
OUString sUrl(getIconCacheUrl(sStyle, sVariant, sName)); OUString sUrl(getIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
if (!urlExists(sUrl)) if (!urlExists(sUrl))
return false; return false;
SvFileStream aFileStream(sUrl, StreamMode::READ); SvFileStream aFileStream(sUrl, StreamMode::READ);
vcl::PNGReader aPNGReader(aFileStream); vcl::PNGReader aPNGReader(aFileStream);
aPNGReader.SetIgnoreGammaChunk( true ); aPNGReader.SetIgnoreGammaChunk( true );
rBitmapEx = aPNGReader.Read(); rParameters.mrBitmap = aPNGReader.Read();
return true; return true;
} }
void cacheBitmapToDisk(OUString const & sStyle, OUString const & sVariant, OUString const & sName, BitmapEx & rBitmapEx) void cacheBitmapToDisk(OUString const & sVariant, ImageRequestParameters& rParameters)
{ {
OUString sUrl(createIconCacheUrl(sStyle, sVariant, sName)); OUString sUrl(createIconCacheUrl(rParameters.msStyle, sVariant, rParameters.msName));
vcl::PNGWriter aWriter(rBitmapEx); vcl::PNGWriter aWriter(rParameters.mrBitmap);
try try
{ {
SvFileStream aStream(sUrl, StreamMode::WRITE); SvFileStream aStream(sUrl, StreamMode::WRITE);
...@@ -329,38 +331,44 @@ void cacheBitmapToDisk(OUString const & sStyle, OUString const & sVariant, OUStr ...@@ -329,38 +331,44 @@ void cacheBitmapToDisk(OUString const & sStyle, OUString const & sVariant, OUStr
{} {}
} }
bool ImplImageTree::doLoadImage(OUString const & name, OUString const & style, BitmapEx & bitmap, bool localized, const ImageLoadFlags eFlags) bool ImplImageTree::doLoadImage(ImageRequestParameters& rParameters)
{ {
setStyle(style); setStyle(rParameters.msStyle);
if (iconCacheLookup(name, localized, eFlags, bitmap)) if (iconCacheLookup(rParameters))
return true; return true;
if (!bitmap.IsEmpty()) if (!rParameters.mrBitmap.IsEmpty())
bitmap.SetEmpty(); rParameters.mrBitmap.SetEmpty();
LanguageTag aLanguageTag = Application::GetSettings().GetUILanguageTag(); LanguageTag aLanguageTag = Application::GetSettings().GetUILanguageTag();
std::vector<OUString> paths = getPaths(name, aLanguageTag); std::vector<OUString> paths = getPaths(rParameters.msName, aLanguageTag);
bool found = false; bool bFound = false;
try {
found = findImage(paths, bitmap, eFlags); try
} catch (css::uno::RuntimeException &) { {
bFound = findImage(paths, rParameters);
}
catch (css::uno::RuntimeException&)
{
throw; throw;
} catch (const css::uno::Exception & e) { }
catch (const css::uno::Exception& e)
{
SAL_INFO("vcl", "ImplImageTree::doLoadImage exception " << e.Message); SAL_INFO("vcl", "ImplImageTree::doLoadImage exception " << e.Message);
} }
if (found) if (bFound)
{ {
OUString aVariant = createVariant(eFlags); OUString aVariant = createVariant(rParameters);
if (!aVariant.isEmpty()) if (!aVariant.isEmpty())
cacheBitmapToDisk(style, aVariant, name, bitmap); cacheBitmapToDisk(aVariant, rParameters);
getCurrentIconSet().maIconCache[name] = std::make_pair(localized, bitmap); getCurrentIconSet().maIconCache[rParameters.msName] = std::make_pair(rParameters.mbLocalized, rParameters.mrBitmap);
} }
return found; return bFound;
} }
void ImplImageTree::shutdown() void ImplImageTree::shutdown()
...@@ -407,25 +415,25 @@ void ImplImageTree::createStyle() ...@@ -407,25 +415,25 @@ void ImplImageTree::createStyle()
loadImageLinks(); loadImageLinks();
} }
bool ImplImageTree::iconCacheLookup(OUString const & name, bool localized, const ImageLoadFlags eFlags, BitmapEx & bitmap) bool ImplImageTree::iconCacheLookup(ImageRequestParameters& rParameters)
{ {
IconCache& rIconCache = getCurrentIconSet().maIconCache; IconCache& rIconCache = getCurrentIconSet().maIconCache;
IconCache::iterator i(rIconCache.find(getRealImageName(name))); IconCache::iterator i(rIconCache.find(getRealImageName(rParameters.msName)));
if (i != rIconCache.end() && i->second.first == localized) if (i != rIconCache.end() && i->second.first == rParameters.mbLocalized)
{ {
bitmap = i->second.second; rParameters.mrBitmap = i->second.second;
return true; return true;
} }
OUString aVariant = createVariant(eFlags); OUString aVariant = createVariant(rParameters);
if (!aVariant.isEmpty() && loadDiskCachedVersion(maCurrentStyle, aVariant, name, bitmap)) if (!aVariant.isEmpty() && loadDiskCachedVersion(aVariant, rParameters))
return true; return true;
return false; return false;
} }
bool ImplImageTree::findImage(std::vector<OUString> const & paths, BitmapEx & bitmap, const ImageLoadFlags eFlags) bool ImplImageTree::findImage(std::vector<OUString> const & paths, ImageRequestParameters& rParameters)
{ {
if (!checkPathAccess()) if (!checkPathAccess())
return false; return false;
...@@ -441,7 +449,7 @@ bool ImplImageTree::findImage(std::vector<OUString> const & paths, BitmapEx & bi ...@@ -441,7 +449,7 @@ bool ImplImageTree::findImage(std::vector<OUString> const & paths, BitmapEx & bi
assert(ok); assert(ok);
(void)ok; // prevent unused warning in release build (void)ok; // prevent unused warning in release build
loadImageFromStream(wrapStream(aStream), rPath, bitmap, eFlags); loadImageFromStream(wrapStream(aStream), rPath, rParameters);
return true; return true;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment