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  , maBitmapEx(rBitmapEx)
35 {
36 }
37 
38 ImplImage::ImplImage(const OUString &aStockName)
39  : maBitmapChecksum(0)
40  , maSizePixel() // defer size lookup
41  , maStockName(aStockName)
42 {
43 }
44 
45 bool ImplImage::loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
46 {
47  BitmapEx aBitmapEx;
48 
49  ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
50  sal_Int32 nScalePercentage = -1;
51 
52  if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
53  {
54  nScalePercentage = fScale * 100.0;
56  }
57 
59  if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
60  nScalePercentage, eScalingFlags))
61  {
62  /* If the uno command has parameters, passed in from a toolbar,
63  * recover from failure by removing the parameters from the file name
64  */
65  if (maStockName.indexOf("%3f") > 0)
66  {
67  sal_Int32 nStart = maStockName.indexOf("%3f");
68  sal_Int32 nEnd = maStockName.lastIndexOf(".");
69 
70  OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, "");
71  if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
72  nScalePercentage, eScalingFlags))
73  {
74  SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
75  " and " << aFileName << " at " << fScale);
76  return false;
77  }
78  }
79  else
80  {
81  SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
82  " at " << fScale);
83  return false;
84  }
85  }
86  rBitmapEx = aBitmapEx;
87  return true;
88 }
89 
91 {
92  Size aRet;
93  if (!isSizeEmpty())
94  aRet = maSizePixel;
95  else if (isStock())
96  {
97  if (loadStockAtScale(1.0, maBitmapEx))
98  {
100  assert(maBitmapChecksum == 0);
102  aRet = maSizePixel;
103  }
104  else
105  SAL_WARN("vcl", "Failed to load stock icon " << maStockName);
106  }
107  return aRet;
108 }
109 
111 BitmapEx const & ImplImage::getBitmapEx(bool bDisabled)
112 {
113  getSizePixel(); // force load, and at unity scale.
114  if (bDisabled)
115  {
116  // Changed since we last generated this.
117  BitmapChecksum aChecksum = maBitmapEx.GetChecksum();
118  if (maBitmapChecksum != aChecksum ||
120  {
123  maBitmapChecksum = aChecksum;
124  }
125  return maDisabledBitmapEx;
126  }
127 
128  return maBitmapEx;
129 }
130 
131 bool ImplImage::isEqual(const ImplImage &ref) const
132 {
133  if (isStock() != ref.isStock())
134  return false;
135  if (isStock())
136  return maStockName == ref.maStockName;
137  else
138  return maBitmapEx == ref.maBitmapEx;
139 }
140 
142 {
143  if (isStock())
144  { // check we have the right bitmap cached.
145  // FIXME: DPI scaling should be tied to the outdev really ...
146  double fScale = comphelper::LibreOfficeKit::getDPIScale();
147  Size aTarget(maSizePixel.Width()*fScale,
148  maSizePixel.Height()*fScale);
149  if (maBitmapEx.GetSizePixel() != aTarget)
150  loadStockAtScale(fScale, maBitmapEx);
151  }
152  return getBitmapEx(bDisabled);
153 }
154 
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
ImageLoadFlags
Definition: ImageTree.hxx:31
OUString DetermineIconTheme() const
Determine which icon theme should be used.
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:728
BitmapChecksum GetChecksum() const
Definition: BitmapEx.cxx:279
BitmapEx maBitmapEx
Original bitmap - or cache of a potentially scaled bitmap.
Definition: image.h:36
static VCL_DLLPUBLIC ImageTree & get()
Definition: ImageTree.cxx:16
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
bool isEqual(const ImplImage &ref) const
Definition: ImplImage.cxx:131
bool isStock() const
Definition: image.h:45
tools::Long Width() const
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:141
Size GetSizePixel() const override
Definition: dockwin.cxx:860
OUString maStockName
If set - defines the bitmap via images.zip*.
Definition: image.h:32
BitmapChecksum maBitmapChecksum
Definition: image.h:28
BitmapEx const & getBitmapEx(bool bDisabled=false)
Legacy - the original bitmap.
Definition: ImplImage.cxx:111
tools::Long Height() const
bool loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
Definition: ImplImage.cxx:45
#define SAL_WARN(area, stream)
bool isSizeEmpty() const
Definition: image.h:63
ImplImage(const BitmapEx &rBitmapEx)
Definition: ImplImage.cxx:31
BitmapEx maDisabledBitmapEx
Definition: image.h:37
const Size & GetSizePixel() const
Definition: bitmapex.hxx:84
Size getSizePixel()
get size in co-ordinates not scaled for HiDPI
Definition: ImplImage.cxx:90