Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / uiconfiguration / ImageList.cxx
blobc287adc44f31b0d2efa0b6d55ced345e108cb4d6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/file.hxx>
21 #include <sal/log.hxx>
22 #include <tools/debug.hxx>
23 #include <tools/stream.hxx>
24 #include <vcl/settings.hxx>
25 #include <vcl/outdev.hxx>
26 #include <vcl/graph.hxx>
27 #include <vcl/graphicfilter.hxx>
28 #include <vcl/svapp.hxx>
29 #include <vcl/image.hxx>
30 #include <vcl/imagerepository.hxx>
31 #include <vcl/ImageTree.hxx>
32 #include "ImageList.hxx"
34 ImageList::ImageList()
38 ImageList::ImageList(const std::vector< OUString >& rNameVector,
39 const OUString& rPrefix)
41 SAL_INFO( "vcl", "vcl: ImageList::ImageList(const vector< OUString >& ..." );
43 maImages.reserve( rNameVector.size() );
45 maPrefix = rPrefix;
46 for( size_t i = 0; i < rNameVector.size(); ++i )
47 ImplAddImage( rPrefix, rNameVector[ i ], static_cast< sal_uInt16 >( i ) + 1, Image() );
50 // FIXME: Rather a performance hazard
51 BitmapEx ImageList::GetAsHorizontalStrip() const
53 sal_uInt16 nCount = maImages.size();
54 if( !nCount )
55 return BitmapEx();
56 Size aSize( maImageSize.Width() * nCount, maImageSize.Height() );
58 BitmapEx aTempl = maImages[ 0 ]->maImage.GetBitmapEx();
59 BitmapEx aResult( aTempl, Point(), aSize );
61 tools::Rectangle aSrcRect( Point( 0, 0 ), maImageSize );
62 for (sal_uInt16 nIdx = 0; nIdx < nCount; nIdx++)
64 tools::Rectangle aDestRect( Point( nIdx * maImageSize.Width(), 0 ),
65 maImageSize );
66 ImageAryData *pData = maImages[ nIdx ].get();
67 BitmapEx aTmp = pData->maImage.GetBitmapEx();
68 aResult.CopyPixel( aDestRect, aSrcRect, &aTmp);
71 return aResult;
74 void ImageList::InsertFromHorizontalStrip( const BitmapEx &rBitmapEx,
75 const std::vector< OUString > &rNameVector )
77 sal_uInt16 nItems = sal::static_int_cast< sal_uInt16 >( rNameVector.size() );
79 if (!nItems)
80 return;
82 Size aSize( rBitmapEx.GetSizePixel() );
83 DBG_ASSERT (rBitmapEx.GetSizePixel().Width() % nItems == 0,
84 "ImageList::InsertFromHorizontalStrip - very odd size");
85 aSize.setWidth( aSize.Width() / nItems );
86 maImages.clear();
87 maNameHash.clear();
88 maImages.reserve( nItems );
89 maImageSize = aSize;
90 maPrefix.clear();
92 for (sal_uInt16 nIdx = 0; nIdx < nItems; nIdx++)
94 BitmapEx aBitmap( rBitmapEx, Point( nIdx * aSize.Width(), 0 ), aSize );
95 ImplAddImage( maPrefix, rNameVector[ nIdx ], nIdx + 1, Image( aBitmap ) );
99 sal_uInt16 ImageList::ImplGetImageId( const OUString& rImageName ) const
101 auto it = maNameHash.find( rImageName );
102 if (it == maNameHash.end())
103 return 0;
104 return it->second->mnId;
107 void ImageList::AddImage( const OUString& rImageName, const Image& rImage )
109 SAL_WARN_IF( GetImagePos( rImageName ) != IMAGELIST_IMAGE_NOTFOUND, "vcl", "ImageList::AddImage() - ImageName already exists" );
111 ImplAddImage( maPrefix, rImageName, GetImageCount() + 1, rImage );
114 void ImageList::ReplaceImage( const OUString& rImageName, const Image& rImage )
116 const sal_uInt16 nId = ImplGetImageId( rImageName );
118 if( nId )
120 // Just replace the bitmap rather than doing RemoveImage / AddImage
121 // which breaks index-based iteration.
122 ImageAryData *pImg = maNameHash[ rImageName ];
123 pImg->maImage = rImage;
127 void ImageList::RemoveImage( sal_uInt16 nId )
129 for( size_t i = 0; i < maImages.size(); ++i )
131 if( maImages[ i ]->mnId == nId )
133 ImplRemoveImage( static_cast< sal_uInt16 >( i ) );
134 break;
139 Image ImageList::GetImage( const OUString& rImageName ) const
141 auto it = maNameHash.find( rImageName );
142 if (it == maNameHash.end())
143 return Image();
144 return it->second->maImage;
147 sal_uInt16 ImageList::GetImageCount() const
149 return static_cast< sal_uInt16 >( maImages.size() );
152 sal_uInt16 ImageList::GetImagePos( const OUString& rImageName ) const
154 if( !rImageName.isEmpty() )
156 for( size_t i = 0; i < maImages.size(); i++ )
158 if (maImages[i]->maName == rImageName)
159 return static_cast< sal_uInt16 >( i );
163 return IMAGELIST_IMAGE_NOTFOUND;
166 sal_uInt16 ImageList::GetImageId( sal_uInt16 nPos ) const
168 return maImages[ nPos ]->mnId;
171 OUString ImageList::GetImageName( sal_uInt16 nPos ) const
173 return maImages[ nPos ]->maName;
176 void ImageList::GetImageNames( std::vector< OUString >& rNames ) const
178 SAL_INFO( "vcl", "vcl: ImageList::GetImageNames" );
180 rNames = std::vector< OUString >();
182 for(auto const & pImage : maImages)
184 const OUString& rName( pImage->maName );
185 if( !rName.isEmpty())
186 rNames.push_back( rName );
190 void ImageList::ImplAddImage( const OUString &aPrefix, const OUString &aName,
191 sal_uInt16 nId, const Image &aImage )
193 Image aInsert = aImage;
194 if (!aInsert)
195 aInsert = Image( "private:graphicrepository/" + aPrefix + aName );
197 ImageAryData *pImg = new ImageAryData{ aName, nId, aInsert };
198 maImages.emplace_back( pImg );
199 if( !aName.isEmpty() )
200 maNameHash [ aName ] = pImg;
203 void ImageList::ImplRemoveImage( sal_uInt16 nPos )
205 ImageAryData *pImg = maImages[ nPos ].get();
206 if( !pImg->maName.isEmpty() )
207 maNameHash.erase( pImg->maName );
208 maImages.erase( maImages.begin() + nPos );
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */