LibreOffice Module vcl (master)  1
ImplImage.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/log.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/bitmapex.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/BitmapFilter.hxx>
25 #include <vcl/ImageTree.hxx>
27 #include <comphelper/lok.hxx>
28 
29 #include <image.h>
30 
31 ImplImage::ImplImage(const BitmapEx &rBitmapEx)
32  : maBitmapChecksum(0)
33  , maSizePixel(rBitmapEx.GetSizePixel())
34  , maPreferedSizePixel()
35  , maBitmapEx(rBitmapEx)
36 {
37 }
38 
39 ImplImage::ImplImage(const OUString &aStockName, Size const & rPreferedSize)
40  : maBitmapChecksum(0)
41  , maSizePixel() // defer size lookup
42  , maPreferedSizePixel(rPreferedSize)
43  , maStockName(aStockName)
44 {
45 }
46 
47 bool ImplImage::loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
48 {
49  BitmapEx aBitmapEx;
50 
51  ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
52  sal_Int32 nScalePercentage = -1;
53 
54  if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
55  {
56  nScalePercentage = fScale * 100.0;
58  }
59 
61  if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
62  nScalePercentage, eScalingFlags))
63  {
64  /* If the uno command has parameters, passed in from a toolbar,
65  * recover from failure by removing the parameters from the file name
66  */
67  if (maStockName.indexOf("%3f") > 0)
68  {
69  sal_Int32 nStart = maStockName.indexOf("%3f");
70  sal_Int32 nEnd = maStockName.lastIndexOf(".");
71 
72  OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, "");
73  if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
74  nScalePercentage, eScalingFlags))
75  {
76  SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
77  " and " << aFileName << " at " << fScale);
78  return false;
79  }
80  }
81  }
82  if (maPreferedSizePixel != Size())
83  {
84  Size aScaleSize(maPreferedSizePixel.Width() * fScale, maPreferedSizePixel.Height() * fScale);
85  aBitmapEx.Scale(aScaleSize);
86  }
87  rBitmapEx = aBitmapEx;
88  return true;
89 }
90 
92 {
93  Size aRet;
94  if (!isSizeEmpty())
95  aRet = maSizePixel;
96  else if (isStock())
97  {
98  if (loadStockAtScale(1.0, maBitmapEx))
99  {
100  assert(!maDisabledBitmapEx);
101  assert(maBitmapChecksum == 0);
103  aRet = maSizePixel;
104  }
105  else
106  SAL_WARN("vcl", "Failed to load stock icon " << maStockName);
107  }
108  return aRet;
109 }
110 
112 BitmapEx const & ImplImage::getBitmapEx(bool bDisabled)
113 {
114  getSizePixel(); // force load, and at unity scale.
115  if (bDisabled)
116  {
117  // Changed since we last generated this.
118  BitmapChecksum aChecksum = maBitmapEx.GetChecksum();
119  if (maBitmapChecksum != aChecksum ||
121  {
124  maBitmapChecksum = aChecksum;
125  }
126  return maDisabledBitmapEx;
127  }
128 
129  return maBitmapEx;
130 }
131 
132 bool ImplImage::isEqual(const ImplImage &ref) const
133 {
134  if (isStock() != ref.isStock())
135  return false;
136  if (isStock())
137  return maStockName == ref.maStockName;
138  else
139  return maBitmapEx == ref.maBitmapEx;
140 }
141 
143 {
144  if (isStock())
145  { // check we have the right bitmap cached.
146  // FIXME: DPI scaling should be tied to the outdev really ...
147  double fScale = comphelper::LibreOfficeKit::getDPIScale();
148  Size aTarget(maSizePixel.Width()*fScale,
149  maSizePixel.Height()*fScale);
150  if (maBitmapEx.GetSizePixel() != aTarget)
151  loadStockAtScale(fScale, maBitmapEx);
152  }
153  return getBitmapEx(bDisabled);
154 }
155 
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
ImageLoadFlags
Definition: ImageTree.hxx:31
OUString DetermineIconTheme() const
Determine which icon theme should be used.
long Height() const
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
Definition: bitmapex.cxx:370
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:704
BitmapChecksum GetChecksum() const
Definition: bitmapex.cxx:292
BitmapEx maBitmapEx
Original bitmap - or cache of a potentially scaled bitmap.
Definition: image.h:37
static VCL_DLLPUBLIC ImageTree & get()
Definition: ImageTree.cxx:16
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
bool isEqual(const ImplImage &ref) const
Definition: ImplImage.cxx:132
Size maPreferedSizePixel
Definition: image.h:31
bool isStock() const
Definition: image.h:46
Size maSizePixel
if non-empty: cached original size of maStockName else Size of maBitmap
Definition: image.h:30
BitmapEx const & getBitmapExForHiDPI(bool bDisabled=false)
Taking account of HiDPI scaling.
Definition: ImplImage.cxx:142
OUString maStockName
If set - defines the bitmap via images.zip*.
Definition: image.h:33
BitmapChecksum maBitmapChecksum
Definition: image.h:28
BitmapEx const & getBitmapEx(bool bDisabled=false)
Legacy - the original bitmap.
Definition: ImplImage.cxx:112
bool loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
Definition: ImplImage.cxx:47
#define SAL_WARN(area, stream)
bool isSizeEmpty() const
Definition: image.h:64
ImplImage(const BitmapEx &rBitmapEx)
Definition: ImplImage.cxx:31
BitmapEx maDisabledBitmapEx
Definition: image.h:38
const Size & GetSizePixel() const
Definition: bitmapex.hxx:83
Size getSizePixel()
get size in co-ordinates not scaled for HiDPI
Definition: ImplImage.cxx:91