main_interface.cpp 8.8 KB
Newer Older
zorglub's avatar
zorglub committed
1 2 3
/*****************************************************************************
 * main_inteface.cpp : Main interface
 ****************************************************************************
4 5
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
zorglub's avatar
zorglub committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/

23
#include "qt4.hpp"
zorglub's avatar
zorglub committed
24
#include "main_interface.hpp"
zorglub's avatar
zorglub committed
25
#include "input_manager.hpp"
26
#include "util/input_slider.hpp"
zorglub's avatar
zorglub committed
27
#include "util/qvlcframe.hpp"
zorglub's avatar
zorglub committed
28
#include "dialogs_provider.hpp"
29
#include "components/video_widget.hpp"
zorglub's avatar
zorglub committed
30
#include <QCloseEvent>
31
#include <assert.h>
zorglub's avatar
zorglub committed
32
#include <QPushButton>
zorglub's avatar
zorglub committed
33
#include <QStatusBar>
34
#include "menus.hpp"
zorglub's avatar
zorglub committed
35

zorglub's avatar
zorglub committed
36 37 38 39 40 41 42
#ifdef WIN32
    #define PREF_W 430
    #define PREF_H 121
#else
    #define PREF_W 480
    #define PREF_H 125
#endif
zorglub's avatar
zorglub committed
43

zorglub's avatar
zorglub committed
44 45 46
static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
                             vlc_value_t, void *);

zorglub's avatar
zorglub committed
47
MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
zorglub's avatar
zorglub committed
48
{
49 50 51
    /* All UI stuff */
    QWidget *main = new QWidget( this );
    setCentralWidget( main );
zorglub's avatar
zorglub committed
52
    setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
53
    ui.setupUi( centralWidget() );
zorglub's avatar
Icons  
zorglub committed
54

zorglub's avatar
zorglub committed
55 56
    slider = new InputSlider( Qt::Horizontal, NULL );
    ui.hboxLayout->insertWidget( 0, slider );
zorglub's avatar
zorglub committed
57 58
    ui.prevButton->setText( "" );
    ui.nextButton->setText( "" );
zorglub's avatar
zorglub committed
59 60
    ui.playButton->setText( "" );
    ui.stopButton->setText( "" );
zorglub's avatar
Icons  
zorglub committed
61 62 63 64 65 66
    ui.prevButton->setIcon( QIcon( ":/pixmaps/previous.png" ) );
    ui.nextButton->setIcon( QIcon( ":/pixmaps/next.png" ) );
    ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
    ui.stopButton->setIcon( QIcon( ":/pixmaps/stop.png" ) );
    ui.volLowLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
    ui.volHighLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
zorglub's avatar
zorglub committed
67
    ui.volumeSlider->setMaximum( 100 );
zorglub's avatar
zorglub committed
68
    ui.playlistButton->setText( "" );
69 70
    ui.playlistButton->setIcon( QIcon( ":/pixmaps/volume-low.png" ) );

zorglub's avatar
zorglub committed
71 72 73
    VolumeClickHandler *h = new VolumeClickHandler( this );
    ui.volLowLabel->installEventFilter(h);
    ui.volHighLabel->installEventFilter(h);
zorglub's avatar
Icons  
zorglub committed
74

75
    QVLCMenu::createMenuBar( menuBar(), p_intf );
zorglub's avatar
zorglub committed
76

zorglub's avatar
zorglub committed
77 78
    timeLabel = new QLabel( 0 );
    nameLabel = new QLabel( 0 );
zorglub's avatar
zorglub committed
79 80 81 82
    statusBar()->addWidget( nameLabel, 4 );
    statusBar()->addPermanentWidget( timeLabel, 1 );

    resize ( PREF_W, PREF_H );
83
//    if( config_GetInt( p_intf, "embedded" ) )
zorglub's avatar
zorglub committed
84

85
    {
86
        videoWidget = new VideoWidget( p_intf, config_GetInt( p_intf, "qt-always-video" ) ? true:false );
zorglub's avatar
zorglub committed
87 88 89 90 91 92
        if( config_GetInt( p_intf, "qt-always-video" ) )
        {
            QSettings settings( "VideoLAN", "VLC" );
            settings.beginGroup( "MainWindow" );
            videoSize = settings.value( "videoSize", QSize( 200, 200 ) ).
                                                toSize();
zorglub's avatar
zorglub committed
93
        }
zorglub's avatar
zorglub committed
94 95 96
        else
            videoSize = QSize( 1,1 );
        videoWidget->resize( videoSize );
97 98
        ui.vboxLayout->insertWidget( 0, videoWidget );
    }
zorglub's avatar
zorglub committed
99
    readSettings( "MainWindow" );
zorglub's avatar
zorglub committed
100

zorglub's avatar
zorglub committed
101
    addSize = QSize( ui.vboxLayout->margin() * 2, PREF_H );
102 103 104 105 106 107
//    if( config_GetInt( p_intf, "qt-always-video" ) )
        mainSize.setWidth( videoSize.width() + addSize.width() );
        mainSize.setHeight( videoSize.height() + addSize.height() );
//    else
//        mainSize = QSize( PREF_W, PREF_H );
    fprintf( stderr, "Resulting size %ix%i", mainSize.width(), mainSize.height() );
zorglub's avatar
zorglub committed
108 109
    resize( mainSize );
    mainSize = size();
110
    fprintf( stderr, "After size %ix%i", mainSize.width(), mainSize.height() );
zorglub's avatar
Icons  
zorglub committed
111

zorglub's avatar
zorglub committed
112
    setMinimumSize( PREF_W, addSize.height() );
zorglub's avatar
zorglub committed
113 114

    /* Init input manager */
115
    MainInputManager::getInstance( p_intf );
zorglub's avatar
zorglub committed
116

zorglub's avatar
zorglub committed
117 118 119 120
    /* Volume control */
    connect( ui.volumeSlider, SIGNAL( valueChanged(int) ),
             this, SLOT( updateVolume(int) ) );

zorglub's avatar
zorglub committed
121
    /* Get timer updates */
122 123
    connect( THEDP->fixed_timer, SIGNAL( timeout() ),
             this, SLOT(updateOnTimer() ) );
zorglub's avatar
zorglub committed
124

zorglub's avatar
zorglub committed
125
    /* Connect the input manager to the GUI elements it manages */
126
    connect( THEMIM->getIM(),SIGNAL(positionUpdated( float, int, int ) ),
zorglub's avatar
zorglub committed
127
             slider, SLOT( setPosition( float,int, int ) ) );
128
    connect( THEMIM->getIM(), SIGNAL( positionUpdated( float, int, int ) ),
zorglub's avatar
zorglub committed
129
             this, SLOT( setDisplay( float, int, int ) ) );
zorglub's avatar
zorglub committed
130 131
    connect( THEMIM->getIM(), SIGNAL( nameChanged( QString ) ),
             this, SLOT( setName( QString ) ) );
132 133 134 135
    connect( THEMIM->getIM(), SIGNAL( statusChanged( int ) ),
             this, SLOT( setStatus( int ) ) );
    connect( slider, SIGNAL( sliderDragged( float ) ),
             THEMIM->getIM(),SLOT( sliderUpdate( float ) ) );
136

zorglub's avatar
zorglub committed
137
    /* Actions */
zorglub's avatar
zorglub committed
138 139 140 141
    connect( ui.playButton, SIGNAL( clicked() ), this, SLOT( play() ) );
    connect( ui.stopButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
    connect( ui.nextButton, SIGNAL( clicked() ), this, SLOT( next() ) );
    connect( ui.prevButton, SIGNAL( clicked() ), this, SLOT( prev() ) );
zorglub's avatar
zorglub committed
142

zorglub's avatar
zorglub committed
143
    connect( ui.playlistButton, SIGNAL(clicked()),
144
             THEDP, SLOT( playlistDialog() ) );
zorglub's avatar
zorglub committed
145 146 147 148

    var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
    var_AddCallback( p_intf, "interaction", InteractCallback, this );
    p_intf->b_interaction = VLC_TRUE;
zorglub's avatar
zorglub committed
149 150 151 152
}

MainInterface::~MainInterface()
{
zorglub's avatar
zorglub committed
153 154 155 156 157 158 159 160 161 162
    writeSettings( "MainWindow" );
    if( config_GetInt( p_intf, "qt-always-video" ) )
    {
        QSettings s("VideoLAN", "VLC" );
        s.beginGroup( "MainWindow" );
        s.setValue( "videoSize", videoSize );
        s.endGroup();
    }
    p_intf->b_interaction = VLC_FALSE;
    var_DelCallback( p_intf, "interaction", InteractCallback, this );
zorglub's avatar
zorglub committed
163
}
zorglub's avatar
zorglub committed
164

zorglub's avatar
zorglub committed
165
void MainInterface::resizeEvent( QResizeEvent *e )
166
{
167 168
    videoSize.setHeight( e->size().height() - addSize.height() );
    videoSize.setWidth( e->size().width() - addSize.width() );
zorglub's avatar
zorglub committed
169
    p_intf->p_sys->p_video->updateGeometry() ;
170 171
}

zorglub's avatar
zorglub committed
172 173
void MainInterface::stop()
{
174
    playlist_Stop( THEPL );
zorglub's avatar
zorglub committed
175 176 177
}
void MainInterface::play()
{
178 179 180 181 182 183 184 185
    if( !THEPL->i_size || !THEPL->i_enabled )
    {
        /* The playlist is empty, open a file requester */
        THEDP->openDialog();
        setStatus( 0 );
        return;
    }
    THEMIM->togglePlayPause();
zorglub's avatar
zorglub committed
186 187 188
}
void MainInterface::prev()
{
189
    playlist_Prev( THEPL );
zorglub's avatar
zorglub committed
190 191 192
}
void MainInterface::next()
{
193
    playlist_Next( THEPL );
zorglub's avatar
zorglub committed
194 195 196 197 198 199 200 201 202
}

void MainInterface::setDisplay( float pos, int time, int length )
{
    char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
    secstotimestr( psz_length, length );
    secstotimestr( psz_time, time );
    QString title;
    title.sprintf( "%s/%s", psz_time, psz_length );
203
    timeLabel->setText( " "+title+" " );
zorglub's avatar
zorglub committed
204 205 206 207
}

void MainInterface::setName( QString name )
{
208
    nameLabel->setText( " " + name+" " );
zorglub's avatar
zorglub committed
209 210
}

211 212
void MainInterface::setStatus( int status )
{
zorglub's avatar
zorglub committed
213
    if( status == 1 ) // Playing
214 215 216 217 218
        ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
    else
        ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
}

zorglub's avatar
zorglub committed
219 220
static bool b_my_volume;

zorglub's avatar
zorglub committed
221 222
void MainInterface::updateOnTimer()
{
zorglub's avatar
zorglub committed
223 224 225 226
    if( p_intf->b_die )
    {
        QApplication::quit();
    }
zorglub's avatar
zorglub committed
227 228 229 230 231 232 233 234 235 236 237
    audio_volume_t i_volume;
    aout_VolumeGet( p_intf, &i_volume );
    i_volume = (i_volume *  200 )/ AOUT_VOLUME_MAX ;
    int i_gauge = ui.volumeSlider->value();
    b_my_volume = false;
    if( i_volume - i_gauge > 1 || i_gauge - i_volume > 1 )
    {
        b_my_volume = true;
        ui.volumeSlider->setValue( i_volume );
        b_my_volume = false;
    }
zorglub's avatar
zorglub committed
238 239 240 241 242 243
}

void MainInterface::closeEvent( QCloseEvent *e )
{
    hide();
    p_intf->b_die = VLC_TRUE;
zorglub's avatar
zorglub committed
244
}
zorglub's avatar
zorglub committed
245

zorglub's avatar
zorglub committed
246 247 248 249 250 251 252 253 254 255
void MainInterface::updateVolume( int sliderVolume )
{
    if( !b_my_volume )
    {
        int i_res = sliderVolume * AOUT_VOLUME_MAX /
                            (2*ui.volumeSlider->maximum() );
        aout_VolumeSet( p_intf, i_res );
    }
}

zorglub's avatar
zorglub committed
256 257 258 259 260 261
static int InteractCallback( vlc_object_t *p_this,
                             const char *psz_var, vlc_value_t old_val,
                             vlc_value_t new_val, void *param )
{
    intf_dialog_args_t *p_arg = new intf_dialog_args_t;
    p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
zorglub's avatar
zorglub committed
262

zorglub's avatar
zorglub committed
263 264
    MainInterface *p_interface = (MainInterface*)param;
    DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
265
    QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
zorglub's avatar
zorglub committed
266 267
    return VLC_SUCCESS;
}