LibreOffice Module avmedia (master) 1
MediaControlBase.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 */
21#include <avmedia/mediaitem.hxx>
22#include <tools/time.hxx>
23#include <tools/duration.hxx>
25#include <strings.hrc>
26#include <helpids.h>
27#include <mediamisc.hxx>
28
29constexpr sal_Int32 AVMEDIA_DB_RANGE = -40;
30constexpr double AVMEDIA_LINEINCREMENT = 1.0;
31constexpr double AVMEDIA_PAGEINCREMENT = 10.0;
32
33namespace avmedia {
34
35MediaControlBase::MediaControlBase()
36 : mbCurrentlySettingZoom(false)
37{
38}
39
40void MediaControlBase::UpdateTimeField( MediaItem const & aMediaItem, double fTime )
41{
42 if( aMediaItem.getURL().isEmpty())
43 return;
44
45 OUString aTimeString;
46
47 SvtSysLocale aSysLocale;
48 const LocaleDataWrapper& rLocaleData = aSysLocale.GetLocaleData();
49
50 aTimeString += rLocaleData.getDuration(
51 tools::Duration( 0, 0, 0, static_cast<sal_uInt32>( floor( fTime )), 0)) +
52 " / " +
53 rLocaleData.getDuration(
54 tools::Duration( 0, 0, 0, static_cast<sal_uInt32>( floor( aMediaItem.getDuration())), 0));
55
56 if( mxTimeEdit->get_text() != aTimeString )
57 mxTimeEdit->set_text( aTimeString );
58}
59
60void MediaControlBase::UpdateVolumeSlider( MediaItem const & aMediaItem )
61{
62 if( aMediaItem.getURL().isEmpty() )
63 mxVolumeSlider->set_sensitive(false);
64 else
65 {
66 mxVolumeSlider->set_sensitive(true);
67 const sal_Int32 nVolumeDB = aMediaItem.getVolumeDB();
68 mxVolumeSlider->set_value( std::clamp( nVolumeDB, AVMEDIA_DB_RANGE, sal_Int32(0)) );
69 }
70}
71
72void MediaControlBase::UpdateTimeSlider( MediaItem const & aMediaItem )
73{
74 if( aMediaItem.getURL().isEmpty() )
75 mxTimeSlider->set_sensitive(false);
76 else
77 {
78 mxTimeSlider->set_sensitive(true);
79
80 const double fDuration = aMediaItem.getDuration();
81
82 if( fDuration > 0.0 )
83 {
84 const double fTime = std::min( aMediaItem.getTime(), fDuration );
85
86 bool bChanged(false);
87 int nStep(0), nPage(0);
88 mxTimeSlider->get_increments(nStep, nPage);
89 if (!nStep)
90 {
91 nStep = AVMEDIA_TIME_RANGE * AVMEDIA_LINEINCREMENT / fDuration;
92 bChanged = true;
93 }
94 if (!nPage)
95 {
96 nPage = AVMEDIA_TIME_RANGE * AVMEDIA_PAGEINCREMENT / fDuration;
97 bChanged = true;
98 }
99 if (bChanged)
100 mxTimeSlider->set_increments(nStep, nPage);
101
102 mxTimeSlider->set_value(fTime / fDuration * AVMEDIA_TIME_RANGE);
103 }
104 }
105}
106
107void MediaControlBase::InitializeWidgets()
108{
109 mxPlayToolBox->set_item_help_id("play", HID_AVMEDIA_TOOLBOXITEM_PLAY);
110 mxPlayToolBox->set_item_help_id("pause", HID_AVMEDIA_TOOLBOXITEM_PAUSE);
111 mxPlayToolBox->set_item_help_id("stop", HID_AVMEDIA_TOOLBOXITEM_STOP);
112 mxPlayToolBox->set_item_help_id("loop", HID_AVMEDIA_TOOLBOXITEM_LOOP);
113 mxMuteToolBox->set_item_help_id("mute", HID_AVMEDIA_TOOLBOXITEM_MUTE);
114
115 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_50), AvmResId( AVMEDIA_STR_ZOOM_50 ));
116 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_100), AvmResId( AVMEDIA_STR_ZOOM_100 ));
117 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_200), AvmResId( AVMEDIA_STR_ZOOM_200 ));
118 mxZoomListBox->append(OUString::number(AVMEDIA_ZOOMLEVEL_FIT), AvmResId( AVMEDIA_STR_ZOOM_FIT ));
119 mxZoomListBox->set_help_id( HID_AVMEDIA_ZOOMLISTBOX );
120 mxZoomListBox->set_tooltip_text(AvmResId( AVMEDIA_STR_ZOOM_TOOLTIP ));
121
122 mxTimeEdit->set_text( " 00:00:00/00:00:00 " );
123 mxTimeEdit->set_help_id( HID_AVMEDIA_TIMEEDIT );
124 mxTimeEdit->set_sensitive(false);
125
126 mxVolumeSlider->set_range(AVMEDIA_DB_RANGE, 0);
127 mxVolumeSlider->set_tooltip_text( AvmResId( AVMEDIA_STR_VOLUME ));
128 mxVolumeSlider->set_help_id( HID_AVMEDIA_VOLUMESLIDER );
129
130 mxTimeSlider->set_range( 0, AVMEDIA_TIME_RANGE );
131 mxTimeSlider->set_tooltip_text( AvmResId( AVMEDIA_STR_POSITION ));
132}
133
134void MediaControlBase::UpdatePlayState(const MediaItem& rMediaItem)
135{
136 if (rMediaItem.getState() == MediaState::Play)
137 {
138 mxPlayToolBox->set_item_active("play", true);
139 mxPlayToolBox->set_item_active("pause", false);
140 mxPlayToolBox->set_item_active("stop", false);
141 }
142 else if( rMediaItem.getState() == MediaState::Pause )
143 {
144 mxPlayToolBox->set_item_active("play", false);
145 mxPlayToolBox->set_item_active("pause", true);
146 mxPlayToolBox->set_item_active("stop", false);
147 }
148 else
149 {
150 mxPlayToolBox->set_item_active("play", false);
151 mxPlayToolBox->set_item_active("pause", false);
152 mxPlayToolBox->set_item_active("stop", true);
153 }
154}
155
156void MediaControlBase::UpdateToolBoxes(const MediaItem& rMediaItem)
157{
158 const bool bValidURL = !rMediaItem.getURL().isEmpty();
159 mxPlayToolBox->set_item_sensitive("play", bValidURL);
160 mxPlayToolBox->set_item_sensitive("pause", bValidURL);
161 mxPlayToolBox->set_item_sensitive("stop", bValidURL);
162 mxPlayToolBox->set_item_sensitive("loop", bValidURL);
163 mxMuteToolBox->set_item_sensitive("mute", bValidURL);
164 if( !bValidURL )
165 {
166 mxZoomListBox->set_sensitive(false);
167 mxMuteToolBox->set_sensitive(false);
168 }
169 else
170 {
171 mxPlayToolBox->set_sensitive(true);
172 mxMuteToolBox->set_sensitive(true);
173 UpdatePlayState(rMediaItem);
174 mxPlayToolBox->set_item_active("loop", rMediaItem.isLoop());
175 mxMuteToolBox->set_item_active("mute", rMediaItem.isMute());
176 if (!mbCurrentlySettingZoom)
177 {
178 sal_uInt16 nSelectEntryPos ;
179
180 switch( rMediaItem.getZoom() )
181 {
182 case css::media::ZoomLevel_ZOOM_1_TO_2:
183 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_50;
184 break;
185 case css::media::ZoomLevel_ORIGINAL:
186 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_100;
187 break;
188 case css::media::ZoomLevel_ZOOM_2_TO_1:
189 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_200;
190 break;
191 case css::media::ZoomLevel_FIT_TO_WINDOW_FIXED_ASPECT:
192 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_FIT;
193 break;
194 case css::media::ZoomLevel_FIT_TO_WINDOW:
195 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_SCALED;
196 break;
197
198 default:
199 nSelectEntryPos = AVMEDIA_ZOOMLEVEL_INVALID;
200 break;
201 }
202
203 if( nSelectEntryPos != AVMEDIA_ZOOMLEVEL_INVALID )
204 {
205 mxZoomListBox->show();
206 mxZoomListBox->set_sensitive(true);
207 mxZoomListBox->set_active(nSelectEntryPos);
208 }
209 else
210 mxZoomListBox->set_sensitive(false);
211 }
212 }
213}
214
215void MediaControlBase::SelectPlayToolBoxItem( MediaItem& aExecItem, MediaItem const & aItem, std::u16string_view rId)
216{
217 if (rId == u"apply")
218 {
219 MediaFloater* pFloater = avmedia::getMediaFloater();
220
221 if( pFloater )
222 pFloater->dispatchCurrentURL();
223 }
224 else if (rId == u"play")
225 {
226 aExecItem.setState( MediaState::Play );
227
228 if( aItem.getTime() == aItem.getDuration() )
229 aExecItem.setTime( 0.0 );
230 else
231 aExecItem.setTime( aItem.getTime() );
232
233 UpdatePlayState(aExecItem);
234 }
235 else if (rId == u"pause")
236 {
237 aExecItem.setState( MediaState::Pause );
238
239 UpdatePlayState(aExecItem);
240 }
241 else if (rId == u"stop")
242 {
243 aExecItem.setState( MediaState::Stop );
244 aExecItem.setTime( 0.0 );
245
246 UpdatePlayState(aExecItem);
247 }
248 else if (rId == u"mute")
249 {
250 aExecItem.setMute( mxMuteToolBox->get_item_active("mute") );
251 }
252 else if (rId == u"loop")
253 {
254 aExecItem.setLoop( mxPlayToolBox->get_item_active("loop") );
255 }
256}
257
258void MediaControlBase::disposeWidgets()
259{
260 mxZoomListBox.reset();
261 mxTimeEdit.reset();
262 mxVolumeSlider.reset();
263 mxMuteToolBox.reset();
264 mxTimeSlider.reset();
265 mxPlayToolBox.reset();
266}
267
268}
269
270/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_Int32 AVMEDIA_DB_RANGE
constexpr double AVMEDIA_LINEINCREMENT
constexpr double AVMEDIA_PAGEINCREMENT
#define AVMEDIA_ZOOMLEVEL_FIT
#define AVMEDIA_TIME_RANGE
#define AVMEDIA_ZOOMLEVEL_50
#define AVMEDIA_ZOOMLEVEL_SCALED
#define AVMEDIA_ZOOMLEVEL_100
#define AVMEDIA_ZOOMLEVEL_INVALID
#define AVMEDIA_ZOOMLEVEL_200
OUString getDuration(const tools::Duration &rDuration, bool bSec=true, bool b100Sec=false) const
const LocaleDataWrapper & GetLocaleData() const
float u
constexpr OUStringLiteral HID_AVMEDIA_TOOLBOXITEM_LOOP
Definition: helpids.h:28
constexpr OUStringLiteral HID_AVMEDIA_TOOLBOXITEM_STOP
Definition: helpids.h:26
constexpr OUStringLiteral HID_AVMEDIA_TIMEEDIT
Definition: helpids.h:32
constexpr OUStringLiteral HID_AVMEDIA_VOLUMESLIDER
Definition: helpids.h:33
constexpr OUStringLiteral HID_AVMEDIA_TOOLBOXITEM_MUTE
Definition: helpids.h:27
constexpr OUStringLiteral HID_AVMEDIA_TOOLBOXITEM_PLAY
Definition: helpids.h:24
constexpr OUStringLiteral HID_AVMEDIA_ZOOMLISTBOX
Definition: helpids.h:31
constexpr OUStringLiteral HID_AVMEDIA_TOOLBOXITEM_PAUSE
Definition: helpids.h:25
OUString AvmResId(TranslateId aId)
Definition: mediamisc.hxx:36
MediaFloater * getMediaFloater()
Definition: mediaplayer.hxx:61