LibreOffice Module framework (master) 1
ImageList.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 <tools/debug.hxx>
22#include <vcl/image.hxx>
23#include "ImageList.hxx"
24
26{
27}
28
29ImageList::ImageList(const std::vector< OUString >& rNameVector,
30 const OUString& rPrefix)
31{
32 SAL_INFO( "vcl", "vcl: ImageList::ImageList(const vector< OUString >& ..." );
33
34 maImages.reserve( rNameVector.size() );
35
36 maPrefix = rPrefix;
37 for( size_t i = 0; i < rNameVector.size(); ++i )
38 ImplAddImage( rPrefix, rNameVector[ i ], static_cast< sal_uInt16 >( i ) + 1, Image() );
39}
40
41// FIXME: Rather a performance hazard
43{
44 sal_uInt16 nCount = maImages.size();
45 if( !nCount )
46 return BitmapEx();
47
48 BitmapEx aTempl = maImages[ 0 ]->maImage.GetBitmapEx();
49 Size aImageSize(aTempl.GetSizePixel());
50 Size aSize(aImageSize.Width() * nCount, aImageSize.Height());
51 BitmapEx aResult( aTempl, Point(), aSize );
52
53 tools::Rectangle aSrcRect( Point( 0, 0 ), aImageSize );
54 for (sal_uInt16 nIdx = 0; nIdx < nCount; nIdx++)
55 {
56 tools::Rectangle aDestRect( Point( nIdx * aImageSize.Width(), 0 ), aImageSize );
57 ImageAryData *pData = maImages[ nIdx ].get();
58 BitmapEx aTmp = pData->maImage.GetBitmapEx();
59 aResult.CopyPixel( aDestRect, aSrcRect, &aTmp);
60 }
61
62 return aResult;
63}
64
66 const std::vector< OUString > &rNameVector )
67{
68 sal_uInt16 nItems = sal::static_int_cast< sal_uInt16 >( rNameVector.size() );
69
70 if (!nItems)
71 return;
72
73 Size aSize( rBitmapEx.GetSizePixel() );
74 DBG_ASSERT (rBitmapEx.GetSizePixel().Width() % nItems == 0,
75 "ImageList::InsertFromHorizontalStrip - very odd size");
76 aSize.setWidth( aSize.Width() / nItems );
77 maImages.clear();
78 maNameHash.clear();
79 maImages.reserve( nItems );
80 maPrefix.clear();
81
82 for (sal_uInt16 nIdx = 0; nIdx < nItems; nIdx++)
83 {
84 BitmapEx aBitmap( rBitmapEx, Point( nIdx * aSize.Width(), 0 ), aSize );
85 ImplAddImage( maPrefix, rNameVector[ nIdx ], nIdx + 1, Image( aBitmap ) );
86 }
87}
88
89sal_uInt16 ImageList::ImplGetImageId( const OUString& rImageName ) const
90{
91 auto it = maNameHash.find( rImageName );
92 if (it == maNameHash.end())
93 return 0;
94 return it->second->mnId;
95}
96
97void ImageList::AddImage( const OUString& rImageName, const Image& rImage )
98{
99 SAL_WARN_IF( GetImagePos( rImageName ) != IMAGELIST_IMAGE_NOTFOUND, "vcl", "ImageList::AddImage() - ImageName already exists" );
100
101 ImplAddImage( maPrefix, rImageName, GetImageCount() + 1, rImage );
102}
103
104void ImageList::ReplaceImage( const OUString& rImageName, const Image& rImage )
105{
106 const sal_uInt16 nId = ImplGetImageId( rImageName );
107
108 if( nId )
109 {
110 // Just replace the bitmap rather than doing RemoveImage / AddImage
111 // which breaks index-based iteration.
112 ImageAryData *pImg = maNameHash[ rImageName ];
113 pImg->maImage = rImage;
114 }
115}
116
117void ImageList::RemoveImage( sal_uInt16 nId )
118{
119 for( size_t i = 0; i < maImages.size(); ++i )
120 {
121 if( maImages[ i ]->mnId == nId )
122 {
123 ImplRemoveImage( static_cast< sal_uInt16 >( i ) );
124 break;
125 }
126 }
127}
128
129Image ImageList::GetImage( const OUString& rImageName ) const
130{
131 auto it = maNameHash.find( rImageName );
132 if (it == maNameHash.end())
133 return Image();
134 return it->second->maImage;
135}
136
137sal_uInt16 ImageList::GetImageCount() const
138{
139 return static_cast< sal_uInt16 >( maImages.size() );
140}
141
142sal_uInt16 ImageList::GetImagePos( std::u16string_view rImageName ) const
143{
144 if( !rImageName.empty() )
145 {
146 for( size_t i = 0; i < maImages.size(); i++ )
147 {
148 if (maImages[i]->maName == rImageName)
149 return static_cast< sal_uInt16 >( i );
150 }
151 }
152
154}
155
156sal_uInt16 ImageList::GetImageId( sal_uInt16 nPos ) const
157{
158 return maImages[ nPos ]->mnId;
159}
160
161const OUString & ImageList::GetImageName( sal_uInt16 nPos ) const
162{
163 return maImages[ nPos ]->maName;
164}
165
166void ImageList::GetImageNames( std::vector< OUString >& rNames ) const
167{
168 SAL_INFO( "vcl", "vcl: ImageList::GetImageNames" );
169
170 rNames = std::vector< OUString >();
171
172 for(auto const & pImage : maImages)
173 {
174 const OUString& rName( pImage->maName );
175 if( !rName.isEmpty())
176 rNames.push_back( rName );
177 }
178}
179
180void ImageList::ImplAddImage( std::u16string_view aPrefix, const OUString &aName,
181 sal_uInt16 nId, const Image &aImage )
182{
183 Image aInsert = aImage;
184 if (!aInsert)
185 aInsert = Image( OUString::Concat("private:graphicrepository/") + aPrefix + aName );
186
187 ImageAryData *pImg = new ImageAryData{ aName, nId, aInsert };
188 maImages.emplace_back( pImg );
189 if( !aName.isEmpty() )
190 maNameHash [ aName ] = pImg;
191}
192
193void ImageList::ImplRemoveImage( sal_uInt16 nPos )
194{
195 ImageAryData *pImg = maImages[ nPos ].get();
196 if( !pImg->maName.isEmpty() )
197 maNameHash.erase( pImg->maName );
198 maImages.erase( maImages.begin() + nPos );
199}
200
201/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maName
bool CopyPixel(const tools::Rectangle &rRectDst, const tools::Rectangle &rRectSrc, const BitmapEx *pBmpExSrc)
const Size & GetSizePixel() const
void AddImage(const OUString &rImageName, const Image &rImage)
Definition: ImageList.cxx:97
void ReplaceImage(const OUString &rImageName, const Image &rImage)
Definition: ImageList.cxx:104
sal_uInt16 ImplGetImageId(const OUString &rImageName) const
Definition: ImageList.cxx:89
sal_uInt16 GetImagePos(std::u16string_view rImageName) const
Definition: ImageList.cxx:142
Image GetImage(const OUString &rImageName) const
Definition: ImageList.cxx:129
sal_uInt16 GetImageId(sal_uInt16 nPos) const
Definition: ImageList.cxx:156
const OUString & GetImageName(sal_uInt16 nPos) const
Definition: ImageList.cxx:161
std::unordered_map< OUString, ImageAryData * > maNameHash
Definition: ImageList.hxx:67
BitmapEx GetAsHorizontalStrip() const
Definition: ImageList.cxx:42
void ImplAddImage(std::u16string_view aPrefix, const OUString &aName, sal_uInt16 nId, const Image &aImage)
Definition: ImageList.cxx:180
void GetImageNames(::std::vector< OUString > &rNames) const
Definition: ImageList.cxx:166
void RemoveImage(sal_uInt16 nId)
Definition: ImageList.cxx:117
void ImplRemoveImage(sal_uInt16 nPos)
Definition: ImageList.cxx:193
std::vector< std::unique_ptr< ImageAryData > > maImages
Definition: ImageList.hxx:66
void InsertFromHorizontalStrip(const BitmapEx &rBitmapEx, const std::vector< OUString > &rNameVector)
Definition: ImageList.cxx:65
OUString maPrefix
Definition: ImageList.hxx:68
sal_uInt16 GetImageCount() const
Definition: ImageList.cxx:137
constexpr tools::Long Height() const
void setWidth(tools::Long nWidth)
constexpr tools::Long Width() const
int nCount
#define DBG_ASSERT(sCon, aError)
sal_uInt16 mnId
#define IMAGELIST_IMAGE_NOTFOUND
OUString aName
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
int i
sal_Int16 nId
OUString maName
Definition: ImageList.hxx:31
Image maImage
Definition: ImageList.hxx:33