]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/mediainfo.cpp
Qt4 - MediaInformation, simplification of the states machine, removal of over-complic...
[vlc] / modules / gui / qt4 / dialogs / mediainfo.cpp
1 /*****************************************************************************
2  * mediainfo.cpp : Information about an item
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  ******************************************************************************/
24
25 #include "dialogs/mediainfo.hpp"
26 #include "input_manager.hpp"
27 #include "dialogs_provider.hpp"
28
29 #include <QTabWidget>
30 #include <QGridLayout>
31 #include <QLineEdit>
32 #include <QLabel>
33
34 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
35                         vlc_value_t oldval, vlc_value_t newval, void *param );
36 MediaInfoDialog *MediaInfoDialog::instance = NULL;
37
38 MediaInfoDialog::MediaInfoDialog( intf_thread_t *_p_intf,
39                                   input_item_t *_p_item,
40                                   bool _mainInput,
41                                   bool _stats ) :
42                                   QVLCFrame( _p_intf ), mainInput(_mainInput),
43                                   stats( _stats )
44 {
45     i_runs = 0;
46     p_item = _p_item;
47     b_need_update = true;
48
49     setWindowTitle( qtr( "Media information" ) );
50     resize( 600 , 480 );
51
52     /* TabWidgets and Tabs creation */
53     IT = new QTabWidget;
54     MP = new MetaPanel( IT, p_intf );
55     IT->addTab( MP, qtr( "&General" ) );
56     EMP = new ExtraMetaPanel( IT, p_intf );
57     IT->addTab( EMP, qtr( "&Extra Metadata" ) );
58     IP = new InfoPanel( IT, p_intf );
59     IT->addTab( IP, qtr( "&Codec Details" ) );
60     if( stats )
61     {
62         ISP = new InputStatsPanel( IT, p_intf );
63         IT->addTab( ISP, qtr( "&Statistics" ) );
64     }
65
66     QGridLayout *layout = new QGridLayout( this );
67
68     /* FIXME GNOME/KDE ? */
69     saveMetaButton = new QPushButton( qtr( "&Save Metadata" ) );
70     saveMetaButton->hide();
71     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
72     closeButton->setDefault( true );
73
74     uriLine = new QLineEdit;
75     QLabel *uriLabel = new QLabel( qtr( "Location :" ) );
76
77     layout->addWidget( IT, 0, 0, 1, 8 );
78     layout->addWidget( uriLabel, 1, 0, 1, 1 );
79     layout->addWidget( uriLine, 1, 1, 1, 7 );
80     layout->addWidget( saveMetaButton, 2, 6 );
81     layout->addWidget( closeButton, 2, 7 );
82
83     BUTTONACT( closeButton, close() );
84
85     /* The tabs buttons are shown in the main dialog for space and cosmetics */
86     CONNECT( saveMetaButton, clicked(), this, saveMeta() );
87
88     /* Let the MetaData Panel update the URI */
89     CONNECT( MP, uriSet( QString ), uriLine, setText( QString ) );
90     CONNECT( MP, editing(), this, showMetaSaveButton() );
91
92     CONNECT( IT, currentChanged( int ), this, updateButtons( int ) );
93
94     /* Create the main Update function with a time (150ms) */
95     if( mainInput ) {
96         ON_TIMEOUT( updateOnTimeOut() );
97         var_AddCallback( THEPL, "item-change", ItemChanged, this );
98     }
99 }
100
101 MediaInfoDialog::~MediaInfoDialog()
102 {
103     if( mainInput ) {
104         var_DelCallback( THEPL, "item-change", ItemChanged, this );
105     }
106     writeSettings( "mediainfo" );
107 }
108
109 void MediaInfoDialog::showTab( int i_tab = 0 )
110 {
111     this->show();
112     IT->setCurrentIndex( i_tab );
113 }
114
115 void MediaInfoDialog::showMetaSaveButton()
116 {
117     saveMetaButton->show();
118 }
119
120 void MediaInfoDialog::saveMeta()
121 {
122     MP->saveMeta();
123     saveMetaButton->hide();
124 }
125
126 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
127         vlc_value_t oldval, vlc_value_t newval, void *param )
128 {
129     MediaInfoDialog *p_d = (MediaInfoDialog *)param;
130     p_d->b_need_update = VLC_TRUE;
131     return VLC_SUCCESS;
132 }
133
134 /* Function called on TimeOut */
135 void MediaInfoDialog::updateOnTimeOut()
136 {
137     /* Timer runs at 150 ms, dont' update more than 2 times per second */
138     i_runs++;
139     if( i_runs % 4 != 0 ) return;
140
141     /* Get Input and clear if non-existant */
142     input_thread_t *p_input = THEMIM->getInput();
143     if( !p_input || p_input->b_dead )
144     {
145         clear();
146         return;
147     }
148
149     /* Launch the update in all the panels */
150     vlc_object_yield( p_input );
151
152     update( input_GetItem(p_input), b_need_update, b_need_update );
153     b_need_update = false;
154
155     vlc_object_release( p_input );
156 }
157
158 void MediaInfoDialog::update( input_item_t *p_item,
159                               bool update_info,
160                               bool update_meta )
161 {
162     if( update_info )
163         IP->update( p_item );
164     if( update_meta )
165     {
166         MP->update( p_item );
167         EMP->update( p_item );
168     }
169     if( stats )
170         ISP->update( p_item );
171 }
172
173 void MediaInfoDialog::clear()
174 {
175     IP->clear();
176     MP->clear();
177     EMP->clear();
178     if( stats ) ISP->clear();
179 }
180
181 void MediaInfoDialog::close()
182 {
183     this->toggleVisible();
184
185     if( mainInput == false ) {
186         deleteLater();
187     }
188 }
189
190 void MediaInfoDialog::updateButtons( int i_tab )
191 {
192     if( MP->isInEditMode() && i_tab == 0 )
193         saveMetaButton->show();
194     else
195         saveMetaButton->hide();
196 }