]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
Prevents deadlocks with input in qt4
[vlc] / modules / gui / qt4 / components / infopanels.cpp
1 /*****************************************************************************
2  * infopanels.cpp : Panels for the information dialogs
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 "qt4.hpp"
26 #include "components/infopanels.hpp"
27
28 #include <QTreeWidget>
29 #include <QListView>
30 #include <QPushButton>
31 #include <QHeaderView>
32 #include <QList>
33 #include <QStringList>
34 #include <QGridLayout>
35 #include <QLineEdit>
36 #include <QLabel>
37 #include <QSpinBox>
38 #include <QTabWidget>
39
40 /************************************************************************
41  * Single panels
42  ************************************************************************/
43
44 /**
45  * First Panel - Meta Info
46  * All the usual MetaData are displayed and can be changed.
47  **/
48 MetaPanel::MetaPanel( QWidget *parent,
49                       intf_thread_t *_p_intf )
50                       : QWidget( parent ), p_intf( _p_intf )
51 {
52     QGridLayout *l = new QGridLayout( this );
53     int line = 0; /* Counter for GridLayout */
54     p_input = NULL;
55
56 #define ADD_META( string, widget ) {                             \
57     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
58     widget = new QLineEdit;                                      \
59     l->addWidget( widget, line, 1, 1, 9 );                       \
60     line++;            }
61
62     /* Title, artist and album*/
63     ADD_META( VLC_META_TITLE, title_text ); /* OK */
64     ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
65     ADD_META( VLC_META_COLLECTION, collection_text ); /* OK */
66
67     /* Genre Name */
68     /* FIXME List id3genres.h is not includable yet ? */
69     genre_text = new QLineEdit;
70     l->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
71     l->addWidget( genre_text, line, 1, 1, 6 );
72
73     /* Date (Should be in years) */
74     date_text = new QSpinBox; setSpinBounds( date_text );
75     l->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 7 );
76     l->addWidget( date_text, line, 8, 1, 2 );
77     line++;
78
79     /* Number and Rating */
80     l->addWidget( new QLabel( qtr( "Track number/Position" )  + " :" ),
81                   line, 0 );
82     seqnum_text = new QSpinBox; setSpinBounds( seqnum_text );
83     l->addWidget( seqnum_text, line, 1, 1, 4 );
84
85     l->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 5 );
86     rating_text = new QSpinBox; setSpinBounds( rating_text) ;
87     l->addWidget( rating_text, line, 6, 1, 4 );
88     line++;
89
90     /* Now Playing ? */
91     ADD_META( VLC_META_NOW_PLAYING, nowplaying_text );
92
93     /* Language and settings */
94     l->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 0 );
95     language_text = new QLineEdit;
96     l->addWidget( language_text, line, 1, 1, 4 );
97     l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
98     setting_text = new QLineEdit;
99     l->addWidget( setting_text, line, 6, 1, 4 );
100     line++;
101
102     /* ART_URL */
103     art_cover = new QLabel( "" );
104     art_cover->setMinimumHeight( 128 );
105     art_cover->setMinimumWidth( 128 );
106     art_cover->setMaximumHeight( 128 );
107     art_cover->setMaximumWidth( 128 );
108     art_cover->setScaledContents( true );
109     art_cover->setPixmap( QPixmap( ":/noart.png" ) );
110     l->addWidget( art_cover, line, 8, 4, 2 );
111
112 #define ADD_META_2( string, widget ) {                             \
113     l->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
114     widget = new QLineEdit;                                      \
115     l->addWidget( widget, line, 1, 1, 7 );                       \
116     line++;            }
117
118     ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
119     ADD_META_2( VLC_META_PUBLISHER, publisher_text );
120
121     ADD_META_2( VLC_META_ENCODED_BY, publisher_text );
122     ADD_META_2( VLC_META_DESCRIPTION, description_text );
123
124     /*  ADD_META( TRACKID )  Useless ? */
125     /*  ADD_URI - DO not show it, done outside */
126
127 #undef ADD_META
128 #undef ADD_META_2
129 }
130
131 MetaPanel::~MetaPanel(){}
132
133 /**
134  * Save the MetaData, triggered by parent->save Button
135  **/
136 void MetaPanel::saveMeta()
137 {
138     playlist_t *p_playlist;
139     char psz[5];
140
141     meta_export_t p_export;
142     p_export.p_item = p_input;
143
144     if( p_input == NULL )
145         return;
146
147     /* we can write meta data only in a file */
148     if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
149         ( p_input->i_type == ITEM_TYPE_VFILE ) )
150         /* some audio files are detected as video files */
151     {
152         char *psz_uri = p_input->psz_uri;
153         if( !strncmp( psz_uri, "file://", 7 ) )
154             psz_uri += 7; /* strlen("file://") = 7 */
155
156         p_export.psz_file = strndup( psz_uri, PATH_MAX );
157     }
158     else
159         return;
160
161     /* now we read the modified meta data */
162     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
163     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
164     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
165
166     snprintf( psz, sizeof(psz), "%d", date_text->value() );
167     input_item_SetDate(  p_input, psz );
168
169     snprintf( psz, sizeof(psz), "%d", seqnum_text->value() );
170     input_item_SetTrackNum(  p_input, psz );
171
172     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
173
174     p_playlist = pl_Yield( p_intf );
175
176     PL_LOCK;
177     p_playlist->p_private = &p_export;
178
179     module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
180     if( p_mod )
181         module_Unneed( p_playlist, p_mod );
182     PL_UNLOCK;
183     pl_Release( p_playlist );
184 }
185
186 /**
187  * Update all the MetaData and art on an "item-changed" event
188  **/
189 void MetaPanel::update( input_item_t *p_item )
190 {
191     char *psz_meta; 
192 #define UPDATE_META( meta, widget ) {               \
193     psz_meta = input_item_Get##meta( p_item );      \
194     if( !EMPTY_STR( psz_meta ) )                    \
195         widget->setText( qfu( psz_meta ) );         \
196     else                                            \
197         widget->setText( "" ); }                    \
198     free( psz_meta );
199
200 #define UPDATE_META_INT( meta, widget ) {           \
201     psz_meta = input_item_Get##meta( p_item );      \
202     if( !EMPTY_STR( psz_meta ) )                    \
203         widget->setValue( atoi( psz_meta ) ); }     \
204     free( psz_meta );
205
206
207     /* Name / Title */
208     psz_meta = input_item_GetTitle( p_item );
209     if( !EMPTY_STR( psz_meta ) )
210         title_text->setText( qfu( psz_meta ) );
211     else if( !EMPTY_STR( p_item->psz_name ) )
212         title_text->setText( qfu( p_item->psz_name ) );
213     else title_text->setText( "" );
214     free( psz_meta );
215
216     /* URL / URI */
217     psz_meta = input_item_GetURL( p_item );
218     if( !EMPTY_STR( psz_meta ) )
219         emit uriSet( QString( psz_meta ) );
220     else if( !EMPTY_STR( p_item->psz_uri ) )
221         emit uriSet( QString( p_item->psz_uri ) );
222     free( psz_meta );
223
224     /* Other classic though */
225     UPDATE_META( Artist, artist_text );
226     UPDATE_META( Genre, genre_text );
227     UPDATE_META( Copyright, copyright_text );
228     UPDATE_META( Album, collection_text );
229     UPDATE_META( Description, description_text );
230     UPDATE_META( Language, language_text );
231     UPDATE_META( NowPlaying, nowplaying_text );
232     UPDATE_META( Publisher, publisher_text );
233     UPDATE_META( Setting, setting_text );
234
235     UPDATE_META_INT( Date, date_text );
236     UPDATE_META_INT( TrackNum, seqnum_text );
237     UPDATE_META_INT( Rating, rating_text );
238
239 #undef UPDATE_META_INT
240 #undef UPDATE_META
241
242     /* Art Urls */
243     psz_meta = input_item_GetArtURL( p_item );
244     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
245     {
246         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
247         art_cover->setPixmap( QPixmap( artUrl ) );
248     }
249     else
250         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
251     free( psz_meta );
252 }
253
254 /*
255  * Clear all the metadata widgets
256  * Unused yet
257  */
258 void MetaPanel::clear(){}
259
260 /**
261  * Second Panel - Shows the extra metadata in a tree, non editable.
262  **/
263 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
264                                 intf_thread_t *_p_intf )
265                                 : QWidget( parent ), p_intf( _p_intf )
266 {
267      QGridLayout *layout = new QGridLayout(this);
268
269      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
270                  " are shown in this list.\n" ) );
271      topLabel->setWordWrap( true );
272      layout->addWidget( topLabel, 0, 0 );
273
274      extraMetaTree = new QTreeWidget( this );
275      extraMetaTree->setAlternatingRowColors( true );
276      extraMetaTree->setColumnCount( 2 );
277      extraMetaTree->header()->hide();
278 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
279  *                                             << qtr( "Value" ) );
280  * Useless, add this header if you think it would help the user          **
281  */
282
283      layout->addWidget( extraMetaTree, 1, 0 );
284 }
285
286 /**
287  * Update the Extra Metadata from p_meta->i_extras 
288  **/
289 void ExtraMetaPanel::update( input_item_t *p_item )
290 {
291     vlc_mutex_lock( &p_item->lock );
292     vlc_meta_t *p_meta = p_item->p_meta;
293     if( !p_meta )
294         return;
295     QStringList tempItem;
296
297     QList<QTreeWidgetItem *> items;
298     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
299     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
300     for (int i = 0; ppsz_allkey[i] ; i++ )
301     {
302         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
303                 p_dict, ppsz_allkey[i] );
304         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
305         tempItem.append( qfu( psz_value ) );
306         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
307         free( ppsz_allkey[i] );
308     }
309     vlc_mutex_unlock( &p_item->lock );
310     free( ppsz_allkey );
311     extraMetaTree->addTopLevelItems( items );
312 }
313
314 /**
315  * Clear the ExtraMetaData Tree
316  **/
317 void ExtraMetaPanel::clear()
318 {
319     extraMetaTree->clear();
320 }
321
322 /**
323  * Third panel - Stream info
324  * Display all codecs and muxers info that we could gather.
325  **/
326 InfoPanel::InfoPanel( QWidget *parent,
327                       intf_thread_t *_p_intf )
328                       : QWidget( parent ), p_intf( _p_intf )
329 {
330      QGridLayout *layout = new QGridLayout(this);
331
332      QList<QTreeWidgetItem *> items;
333
334      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
335               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
336               "are shown." ) );
337      topLabel->setWordWrap( true );
338      layout->addWidget( topLabel, 0, 0 );
339
340      InfoTree = new QTreeWidget(this);
341      InfoTree->setColumnCount( 1 );
342      InfoTree->header()->hide();
343      layout->addWidget(InfoTree, 1, 0 );
344 }
345
346 InfoPanel::~InfoPanel()
347 {
348 }
349
350 /**
351  * Update the Codecs information on parent->update()
352  **/
353 void InfoPanel::update( input_item_t *p_item)
354 {
355     InfoTree->clear();
356     QTreeWidgetItem *current_item = NULL;
357     QTreeWidgetItem *child_item = NULL;
358
359     for( int i = 0; i< p_item->i_categories ; i++)
360     {
361         current_item = new QTreeWidgetItem();
362         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
363         InfoTree->addTopLevelItem( current_item );
364
365         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
366         {
367             child_item = new QTreeWidgetItem ();
368             child_item->setText( 0,
369                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
370                     + ": "
371                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
372
373             current_item->addChild(child_item);
374         }
375          InfoTree->setItemExpanded( current_item, true);
376     }
377 }
378
379 /**
380  * Clear the tree
381  **/
382 void InfoPanel::clear()
383 {
384     InfoTree->clear();
385 }
386
387 /**
388  * Save all the information to a file
389  * Not yet implemented.
390  **/
391 /*
392 void InfoPanel::saveCodecsInfo()
393 {
394
395 }
396 */
397
398 /**
399  * Fourth Panel - Stats
400  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
401  */
402 InputStatsPanel::InputStatsPanel( QWidget *parent,
403                                   intf_thread_t *_p_intf )
404                                   : QWidget( parent ), p_intf( _p_intf )
405 {
406      QGridLayout *layout = new QGridLayout(this);
407
408      QList<QTreeWidgetItem *> items;
409
410      QLabel *topLabel = new QLabel( qtr( "Various statistics about the current"
411                  " media or stream.\n Played and streamed info are shown." ) );
412      topLabel->setWordWrap( true );
413      layout->addWidget( topLabel, 0, 0 );
414
415      StatsTree = new QTreeWidget(this);
416      StatsTree->setColumnCount( 3 );
417      StatsTree->header()->hide();
418
419 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
420     itemName =                                                                 \
421       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
422     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
423
424 #define CREATE_CATEGORY( catName, itemText ) {                           \
425     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
426     catName->setExpanded( true );                                        \
427     StatsTree->addTopLevelItem( catName );    }
428
429 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
430     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
431     catName->addChild( itemName ); }
432
433     /* Create the main categories */
434     CREATE_CATEGORY( input, qtr("Input") );
435     CREATE_CATEGORY( video, qtr("Video") );
436     CREATE_CATEGORY( streaming, qtr("Streaming") );
437     CREATE_CATEGORY( audio, qtr("Audio") );
438
439     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
440                            "0", input , "kB" );
441     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
442                            "0", input, "kb/s" );
443     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
444     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
445                            "0", input, "kb/s" );
446
447     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
448                            "0", video, "" );
449     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
450                            "0", video, "" );
451     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
452                            "0", video, "" );
453
454     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
455     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
456                            "0", streaming, "kB" );
457     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrates"),
458                            "0", streaming, "kb/s" );
459
460     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
461                            "0", audio, "" );
462     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
463                            "0", audio, "" );
464     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
465
466     input->setExpanded( true );
467     video->setExpanded( true );
468     streaming->setExpanded( true );
469     audio->setExpanded( true );
470
471     StatsTree->resizeColumnToContents( 0 );
472     StatsTree->setColumnWidth( 1 , 100 );
473
474     layout->addWidget(StatsTree, 1, 0 );
475 }
476
477 InputStatsPanel::~InputStatsPanel()
478 {
479 }
480
481 /**
482  * Update the Statistics
483  **/
484 void InputStatsPanel::update( input_item_t *p_item )
485 {
486     vlc_mutex_lock( &p_item->p_stats->lock );
487
488 #define UPDATE( widget, format, calc... ) \
489     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
490
491     UPDATE( read_media_stat, "%8.0f",
492             (float)(p_item->p_stats->i_read_bytes)/1000);
493     UPDATE( input_bitrate_stat, "%6.0f",
494                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
495     UPDATE( demuxed_stat, "%8.0f",
496                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
497     UPDATE( stream_bitrate_stat, "%6.0f",
498                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
499
500     /* Video */
501     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
502     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
503     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
504
505     /* Sout */
506     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
507     UPDATE( send_bytes_stat, "%8.0f",
508             (float)(p_item->p_stats->i_sent_bytes)/1000 );
509     UPDATE( send_bitrate_stat, "%6.0f",
510             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
511
512     /* Audio*/
513     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
514     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
515     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
516
517     vlc_mutex_unlock(& p_item->p_stats->lock );
518 }
519
520 void InputStatsPanel::clear()
521 {
522 }
523
524