Coverage Report

Created: 2026-09-28 10:59

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