LibreOffice Module vcl (master) 1
throbber.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
21#include <vcl/svapp.hxx>
22
23#include <com/sun/star/uno/XComponentContext.hpp>
24#include <com/sun/star/graphic/GraphicProvider.hpp>
25#include <com/sun/star/graphic/XGraphicProvider.hpp>
26#include <com/sun/star/awt/ImageScaleMode.hpp>
27
30#include <rtl/ustrbuf.hxx>
31#include <sal/log.hxx>
32#include <tools/urlobj.hxx>
33
34#include <limits>
35
36using ::com::sun::star::uno::Reference;
37using ::com::sun::star::graphic::XGraphic;
38using ::com::sun::star::graphic::XGraphicProvider;
39using ::com::sun::star::uno::Exception;
40namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
41
42Throbber::Throbber( vcl::Window* i_parentWindow, WinBits i_style )
43 :ImageControl( i_parentWindow, i_style )
44 ,mbRepeat( true )
45 ,mnStepTime( 100 )
46 ,mnCurStep( 0 )
47 ,maWaitTimer("Throbber maWaitTimer")
48{
49 maWaitTimer.SetTimeout( mnStepTime );
50 maWaitTimer.SetInvokeHandler( LINK( this, Throbber, TimeOutHdl ) );
51
52 SetScaleMode( ImageScaleMode::NONE );
53 initImages();
54}
55
56Throbber::~Throbber()
57{
58 disposeOnce();
59}
60
61void Throbber::dispose()
62{
63 maWaitTimer.Stop();
64 ImageControl::dispose();
65}
66
67namespace
68{
69 ::std::vector< Image > lcl_loadImageSet( const Throbber::ImageSet i_imageSet )
70 {
71 ::std::vector< Image > aImages;
72
73 const Reference< css::uno::XComponentContext > aContext( ::comphelper::getProcessComponentContext() );
74 const Reference< XGraphicProvider > xGraphicProvider( css::graphic::GraphicProvider::create(aContext) );
75
76 ::std::vector< OUString > aImageURLs( Throbber::getDefaultImageURLs( i_imageSet ) );
77 aImages.reserve( aImageURLs.size() );
78
80 for ( const auto& rImageURL : aImageURLs )
81 {
82 Reference< XGraphic > xGraphic;
83 aMediaProperties.put( "URL", rImageURL );
84 xGraphic = xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() );
85 aImages.emplace_back( xGraphic );
86 }
87
88 return aImages;
89 }
90}
91
92void Throbber::Resize()
93{
94 ImageControl::Resize();
95 initImages();
96}
97
98void Throbber::initImages()
99{
100 try
101 {
102 ::std::vector< ::std::vector< Image > > aImageSets
103 {
104 lcl_loadImageSet( ImageSet::N16px ),
105 lcl_loadImageSet( ImageSet::N32px ),
106 lcl_loadImageSet( ImageSet::N64px )
107 };
108
109 // find the best matching image set (size-wise)
110 const ::Size aWindowSizePixel = GetSizePixel();
111 size_t nPreferredSet = 0;
112 if ( aImageSets.size() > 1 )
113 {
114 tools::Long nMinimalDistance = ::std::numeric_limits< tools::Long >::max();
115 for ( ::std::vector< ::std::vector< Image > >::const_iterator check = aImageSets.begin();
116 check != aImageSets.end();
117 ++check
118 )
119 {
120 if ( check->empty() )
121 {
122 SAL_WARN( "vcl.control", "Throbber::initImages: illegal image!" );
123 continue;
124 }
125
126 const Size aImageSize = (*check)[0].GetSizePixel();
127
128 if ( ( aImageSize.Width() > aWindowSizePixel.Width() )
129 || ( aImageSize.Height() > aWindowSizePixel.Height() )
130 )
131 // do not use an image set which doesn't fit into the window
132 continue;
133
134 const sal_Int64 distance =
135 ( aWindowSizePixel.Width() - aImageSize.Width() ) * ( aWindowSizePixel.Width() - aImageSize.Width() )
136 + ( aWindowSizePixel.Height() - aImageSize.Height() ) * ( aWindowSizePixel.Height() - aImageSize.Height() );
137 if ( distance < nMinimalDistance )
138 {
139 nMinimalDistance = distance;
140 nPreferredSet = check - aImageSets.begin();
141 }
142 }
143 }
144
145 if ( nPreferredSet < aImageSets.size() )
146 setImageList( std::vector(aImageSets[nPreferredSet]) );
147 }
148 catch( const Exception& )
149 {
150 }
151}
152
153void Throbber::start()
154{
155 maWaitTimer.Start();
156}
157
158void Throbber::stop()
159{
160 maWaitTimer.Stop();
161}
162
163bool Throbber::isRunning() const
164{
165 return maWaitTimer.IsActive();
166}
167
168void Throbber::setImageList( ::std::vector< Image > && i_images )
169{
170 SAL_WARN_IF( i_images.size()>=SAL_MAX_INT32, "vcl.control", "Throbber::setImageList: too many images!" );
171
172 maImageList = std::move(i_images);
173
174 const Image aInitialImage( !maImageList.empty() ? maImageList[ 0 ] : Image() );
175 SetImage( aInitialImage );
176}
177
178::std::vector< OUString > Throbber::getDefaultImageURLs( const ImageSet i_imageSet )
179{
180 ::std::vector< OUString > aImageURLs;
181
182 sal_Unicode const* const pResolutions[] = { u"16", u"32", u"64" };
183 size_t const nImageCounts[] = { 6, 12, 12 };
184
185 size_t index = 0;
186 switch ( i_imageSet )
187 {
188 case ImageSet::N16px: index = 0; break;
189 case ImageSet::N32px: index = 1; break;
190 case ImageSet::N64px: index = 2; break;
191 }
192
193 aImageURLs.reserve( nImageCounts[index] );
194 for ( size_t i=0; i<nImageCounts[index]; ++i )
195 {
196 OUStringBuffer aURL( OUString::Concat("private:graphicrepository/vcl/res/spinner-")
197 + pResolutions[index]
198 + "-" );
199 if ( i < 9 )
200 aURL.append( "0" );
201 aURL.append( OUString::number( sal_Int32( i + 1 ) ) + ".png" );
202
203 aImageURLs.push_back( aURL.makeStringAndClear() );
204 }
205
206 return aImageURLs;
207}
208
209IMPL_LINK_NOARG(Throbber, TimeOutHdl, Timer *, void)
210{
211 SolarMutexGuard aGuard;
212 if ( maImageList.empty() )
213 return;
214
215 if ( mnCurStep < static_cast<sal_Int32>(maImageList.size()-1) )
216 ++mnCurStep;
217 else
218 {
219 if ( mbRepeat )
220 {
221 // start over
222 mnCurStep = 0;
223 }
224 else
225 {
226 stop();
227 }
228 }
229
230 SetImage( maImageList[ mnCurStep ] );
231}
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: image.hxx:40
constexpr tools::Long Height() const
constexpr tools::Long Width() const
Definition: timer.hxx:27
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
css::uno::Sequence< css::beans::PropertyValue > getPropertyValues() const
URL aURL
float u
double distance
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
def stop(arg=None)
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > > check(dp_misc::DescriptionInfoset const &infoset)
int i
index
long Long
IMPL_LINK_NOARG(Throbber, TimeOutHdl, Timer *, void)
Definition: throbber.cxx:209
sal_uInt16 sal_Unicode
sal_Int64 WinBits
Definition: wintypes.hxx:109