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  , maStockName(aStockName)
41 {
42 }
43 
44 bool ImplImage::loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
45 {
46  BitmapEx aBitmapEx;
47 
48  ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
49  sal_Int32 nScalePercentage = -1;
50 
51  if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
52  {
53  nScalePercentage = fScale * 100.0;
55  }
56 
58  if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
59  nScalePercentage, eScalingFlags))
60  {
61  /* If the uno command has parameters, passed in from a toolbar,
62  * recover from failure by removing the parameters from the file name
63  */
64  if (maStockName.indexOf("%3f") > 0)
65  {
66  sal_Int32 nStart = maStockName.indexOf("%3f");
67  sal_Int32 nEnd = maStockName.lastIndexOf(".");
68 
69  OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, "");
70  if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
71  nScalePercentage, eScalingFlags))
72  {
73  SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
74  " and " << aFileName << " at " << fScale);
75  return false;
76  }
77  }
78  else
79  {
80  SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
81  " at " << fScale);
82  return false;
83  }
84  }
85  rBitmapEx = aBitmapEx;
86  return true;
87 }
88 
90 {
91  Size aRet;
92  if (!isSizeEmpty())
93  aRet = maSizePixel;
94  else if (isStock())
95  {
96  if (loadStockAtScale(1.0, maBitmapEx))
97  {
98  assert(maDisabledBitmapEx.IsEmpty());
99  assert(maBitmapChecksum == 0);
101  aRet = maSizePixel;
102  }
103  else
104  SAL_WARN("vcl", "Failed to load stock icon " << maStockName);
105  }
106  return aRet;
107 }
108 
110 BitmapEx const & ImplImage::getBitmapEx(bool bDisabled)
111 {
112  getSizePixel(); // force load, and at unity scale.
113  if (bDisabled)
114  {
115  // Changed since we last generated this.
116  BitmapChecksum aChecksum = maBitmapEx.GetChecksum();
117  if (maBitmapChecksum != aChecksum ||
119  {
122  maBitmapChecksum = aChecksum;
123  }
124  return maDisabledBitmapEx;
125  }
126 
127  return maBitmapEx;
128 }
129 
130 bool ImplImage::isEqual(const ImplImage &ref) const
131 {
132  if (isStock() != ref.isStock())
133  return false;
134  if (isStock())
135  return maStockName == ref.maStockName;
136  else
137  return maBitmapEx == ref.maBitmapEx;
138 }
139 
141 {
142  if (isStock())
143  { // check we have the right bitmap cached.
144  // FIXME: DPI scaling should be tied to the outdev really ...
145  double fScale = comphelper::LibreOfficeKit::getDPIScale();
146  Size aTarget(maSizePixel.Width()*fScale,
147  maSizePixel.Height()*fScale);
148  if (maBitmapEx.GetSizePixel() != aTarget)
149  loadStockAtScale(fScale, maBitmapEx);
150  }
151  return getBitmapEx(bDisabled);
152 }
153 
154 /* 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:733
BitmapChecksum GetChecksum() const
Definition: BitmapEx.cxx:230
constexpr tools::Long Width() const
BitmapEx maBitmapEx
Original bitmap - or cache of a potentially scaled bitmap.
Definition: image.h:36
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:130
bool IsEmpty() const
Definition: BitmapEx.cxx:177
bool isStock() const
Definition: image.h:45
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:140
Size GetSizePixel() const override
Definition: dockwin.cxx:860
OUString maStockName
If set - defines the bitmap via images.zip*.
Definition: image.h:32
constexpr tools::Long Height() const
BitmapChecksum maBitmapChecksum
Definition: image.h:28
BitmapEx const & getBitmapEx(bool bDisabled=false)
Legacy - the original bitmap.
Definition: ImplImage.cxx:110
bool loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
Definition: ImplImage.cxx:44
#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:73
Size getSizePixel()
get size in co-ordinates not scaled for HiDPI
Definition: ImplImage.cxx:89