Coverage Report

Created: 2025-07-07 10:01

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