]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
6fb76d1efdab02c58d206f9682fd06514f6c70a4
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_inteface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
22
23 #include "qt4.hpp"
24 #include "main_interface.hpp"
25 #include "input_manager.hpp"
26 #include "util/input_slider.hpp"
27 #include "util/qvlcframe.hpp"
28 #include "dialogs_provider.hpp"
29 #include "components/video_widget.hpp"
30 #include <QCloseEvent>
31 #include <assert.h>
32 #include <QPushButton>
33 #include "menus.hpp"
34
35 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
36                              vlc_value_t, void *);
37
38 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
39 {
40     /* All UI stuff */
41     QWidget *main = new QWidget( this );
42     setCentralWidget( main );
43     setWindowTitle( QString::fromUtf8( _("VLC media player") ) );
44     ui.setupUi( centralWidget() );
45
46     slider = new InputSlider( Qt::Horizontal, ui.sliderBox );
47     QVBoxLayout *box_layout = new QVBoxLayout();
48     box_layout->addWidget( slider );
49     ui.sliderBox->setLayout( box_layout );
50     ui.prevButton->setText( "" );
51     ui.nextButton->setText( "" );
52     ui.playButton->setText( "" ); 
53     ui.stopButton->setText( "" ); 
54     ui.prevButton->setIcon( QIcon( ":/pixmaps/previous.png" ) );
55     ui.nextButton->setIcon( QIcon( ":/pixmaps/next.png" ) );
56     ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
57     ui.stopButton->setIcon( QIcon( ":/pixmaps/stop.png" ) );
58     ui.volLowLabel->setPixmap( QPixmap( ":/pixmaps/volume-low.png" ) );
59     ui.volHighLabel->setPixmap( QPixmap( ":/pixmaps/volume-high.png" ) );
60
61     QVLCMenu::createMenuBar( menuBar(), p_intf );
62
63     resize (500, 131 );
64 //    if( config_GetInt( p_intf, "embedded" ) )
65
66     {
67         videoWidget = new VideoWidget( p_intf );
68         if( config_GetInt( p_intf, "qt-always-video" ) )
69         {
70             QSettings settings( "VideoLAN", "VLC" );
71             settings.beginGroup( "MainWindow" );
72             videoSize = settings.value( "videoSize", QSize( 200, 200 ) ).
73                                                 toSize();
74         } 
75         else
76             videoSize = QSize( 1,1 );
77         videoWidget->resize( videoSize );
78         ui.vboxLayout->insertWidget( 0, videoWidget );
79     }
80     fprintf( stderr, "Margin : %i\n",ui.vboxLayout->margin() );
81     readSettings( "MainWindow" );
82
83     addSize = QSize( ui.vboxLayout->margin() * 2, 131 );
84     
85     if( config_GetInt( p_intf, "qt-always-video" ) )
86         mainSize = videoSize + addSize;
87     else
88         mainSize = QSize( 500,131 );
89         resize( 500,131 );
90     resize( mainSize );
91     mainSize = size();
92
93     fprintf( stderr, "Size is %ix%i - Video %ix%i\n", mainSize.width(), mainSize.height(), videoSize.width(), videoSize.height() );
94
95     fprintf( stderr, "Additional size around video %ix%i", addSize.width(), addSize.height() );
96     setMinimumSize( 500, addSize.height() );
97
98     /* Init input manager */
99     MainInputManager::getInstance( p_intf );
100
101     /* Get timer updates */
102     connect( THEDP->fixed_timer, SIGNAL( timeout() ),
103              this, SLOT(updateOnTimer() ) );
104
105     /* Connect the input manager to the GUI elements it manages */
106     connect( THEMIM->getIM(),SIGNAL(positionUpdated( float, int, int ) ),
107              slider, SLOT( setPosition( float,int, int ) ) );
108     connect( THEMIM->getIM(), SIGNAL( positionUpdated( float, int, int ) ),
109              this, SLOT( setDisplay( float, int, int ) ) );
110     connect( THEMIM->getIM(), SIGNAL( statusChanged( int ) ),
111              this, SLOT( setStatus( int ) ) );
112     connect( slider, SIGNAL( sliderDragged( float ) ),
113              THEMIM->getIM(),SLOT( sliderUpdate( float ) ) );
114
115     /* Actions */
116     connect( ui.playButton, SLOT( clicked() ), this, SLOT( play() ) );
117     connect( ui.stopButton, SLOT( clicked() ), this, SLOT( stop() ) );
118     connect( ui.nextButton, SLOT( clicked() ), this, SLOT( next() ) );
119     connect( ui.prevButton, SLOT( clicked() ), this, SLOT( prev() ) );
120
121     connect( ui.playlistButton, SLOT(clicked()),
122              THEDP, SLOT( playlistDialog() ) );
123
124     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
125     var_AddCallback( p_intf, "interaction", InteractCallback, this );
126     p_intf->b_interaction = VLC_TRUE;
127 }
128
129 MainInterface::~MainInterface()
130 {
131     writeSettings( "MainWindow" );
132     if( config_GetInt( p_intf, "qt-always-video" ) )
133     {
134         QSettings s("VideoLAN", "VLC" );
135         s.beginGroup( "MainWindow" );
136         s.setValue( "videoSize", videoSize );
137         s.endGroup();
138     }
139     p_intf->b_interaction = VLC_FALSE;
140     var_DelCallback( p_intf, "interaction", InteractCallback, this );
141 }
142
143 void MainInterface::resizeEvent( QResizeEvent *e )
144 {
145     videoSize.setHeight( e->size().height() - addSize.height() );
146     videoSize.setWidth( e->size().width() - addSize.width() );
147     p_intf->p_sys->p_video->updateGeometry() ;
148 }
149
150 void MainInterface::stop()
151 {
152     playlist_Stop( THEPL );
153 }
154 void MainInterface::play()
155 {
156     if( !THEPL->i_size || !THEPL->i_enabled )
157     {
158         /* The playlist is empty, open a file requester */
159         THEDP->openDialog();
160         setStatus( 0 );
161         return;
162     }
163     THEMIM->togglePlayPause();
164 }
165 void MainInterface::prev()
166 {
167     playlist_Prev( THEPL );
168 }
169 void MainInterface::next()
170 {
171     playlist_Next( THEPL );
172 }
173
174 void MainInterface::setDisplay( float pos, int time, int length )
175 {
176     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
177     secstotimestr( psz_length, length );
178     secstotimestr( psz_time, time );
179     QString title;
180     title.sprintf( "%s/%s", psz_time, psz_length );
181     ui.sliderBox->setTitle( title );
182 }
183
184 void MainInterface::setStatus( int status )
185 {
186     fprintf( stderr, "Status is now %i\n", status );
187     if( status == 2 ) // Playing
188         ui.playButton->setIcon( QIcon( ":/pixmaps/pause.png" ) );
189     else
190         ui.playButton->setIcon( QIcon( ":/pixmaps/play.png" ) );
191 }
192
193 void MainInterface::updateOnTimer()
194 {
195     if( p_intf->b_die )
196     {
197         QApplication::quit();
198     }
199 }
200
201 void MainInterface::closeEvent( QCloseEvent *e )
202 {
203     hide();
204     p_intf->b_die = VLC_TRUE;
205 }
206
207 static int InteractCallback( vlc_object_t *p_this,
208                              const char *psz_var, vlc_value_t old_val,
209                              vlc_value_t new_val, void *param )
210 {
211     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
212     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
213     
214     MainInterface *p_interface = (MainInterface*)param;
215     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
216     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
217     return VLC_SUCCESS;
218 }