]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/info_panels.cpp
qt4: use const for QString when possible.
[vlc] / modules / gui / qt4 / components / info_panels.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  *          Ilkka Ollakka <ileoo@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "qt4.hpp"
31 #include "components/info_panels.hpp"
32 #include "components/interface_widgets.hpp"
33
34 #include <assert.h>
35
36 #include <QTreeWidget>
37 #include <QHeaderView>
38 #include <QList>
39 #include <QStringList>
40 #include <QGridLayout>
41 #include <QLineEdit>
42 #include <QLabel>
43 #include <QSpinBox>
44
45 /************************************************************************
46  * Single panels
47  ************************************************************************/
48
49 /**
50  * First Panel - Meta Info
51  * All the usual MetaData are displayed and can be changed.
52  **/
53 MetaPanel::MetaPanel( QWidget *parent,
54                       intf_thread_t *_p_intf )
55                       : QWidget( parent ), p_intf( _p_intf )
56 {
57     QGridLayout *metaLayout = new QGridLayout( this );
58
59     int line = 0; /* Counter for GridLayout */
60     p_input = NULL;
61
62 #define ADD_META( string, widget ) {                                      \
63     metaLayout->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
64     widget = new QLineEdit;                                               \
65     metaLayout->addWidget( widget, line, 1, 1, 9 );                       \
66     line++;            }
67
68     /* Title, artist and album*/
69     ADD_META( VLC_META_TITLE, title_text ); /* OK */
70     ADD_META( VLC_META_ARTIST, artist_text ); /* OK */
71     ADD_META( VLC_META_ALBUM, collection_text ); /* OK */
72
73     /* Genre Name */
74     /* TODO List id3genres.h is not includable yet ? */
75     genre_text = new QLineEdit;
76     metaLayout->addWidget( new QLabel( qtr( VLC_META_GENRE ) + " :" ), line, 0 );
77     metaLayout->addWidget( genre_text, line, 1, 1, 3 );
78
79     /* Number - on the same line */
80     metaLayout->addWidget( new QLabel( qtr( VLC_META_TRACK_NUMBER )  + " :" ),
81                   line, 5, 1, 2  );
82     seqnum_text = new QLineEdit;
83     seqnum_text->setInputMask("0000");
84     seqnum_text->setAlignment( Qt::AlignRight );
85     metaLayout->addWidget( seqnum_text, line, 7, 1, 3 );
86     line++;
87
88     /* Date (Should be in years) */
89     date_text = new QLineEdit;
90     date_text->setInputMask("0000");
91     date_text->setAlignment( Qt::AlignRight );
92     metaLayout->addWidget( new QLabel( qtr( VLC_META_DATE ) + " :" ), line, 0 );
93     metaLayout->addWidget( date_text, line, 1, 1, 3 );
94
95     /* Rating - on the same line */
96     /*
97     metaLayout->addWidget( new QLabel( qtr( VLC_META_RATING ) + " :" ), line, 4, 1, 2 );
98     rating_text = new QSpinBox; setSpinBounds( rating_text );
99     metaLayout->addWidget( rating_text, line, 6, 1, 1 );
100     */
101     /* Language on the same line */
102     metaLayout->addWidget( new QLabel( qfu( VLC_META_LANGUAGE ) + " :" ), line, 5, 1, 2 );
103     language_text = new QLineEdit;
104     language_text->setReadOnly( true );
105     metaLayout->addWidget( language_text, line,  7, 1, 3 );
106     line++;
107
108     /* ART_URL */
109     art_cover = new CoverArtLabel( this, p_intf );
110     metaLayout->addWidget( art_cover, line, 8, 4, 2, Qt::AlignRight );
111
112 /* Settings is unused */
113 /*    l->addWidget( new QLabel( qtr( VLC_META_SETTING ) + " :" ), line, 5 );
114     setting_text = new QLineEdit;
115     l->addWidget( setting_text, line, 6, 1, 4 ); */
116
117 /* Less used metadata */
118 #define ADD_META_2( string, widget ) {                                    \
119     metaLayout->addWidget( new QLabel( qtr( string ) + " :" ), line, 0 ); \
120     widget = new QLineEdit;                                               \
121     metaLayout->addWidget( widget, line, 1, 1, 7 );                       \
122     line++;            }
123
124     /* Now Playing - Useful for live feeds (HTTP, DVB, ETC...) */
125     ADD_META_2( VLC_META_NOW_PLAYING, nowplaying_text );
126     nowplaying_text->setReadOnly( true );
127     ADD_META_2( VLC_META_PUBLISHER, publisher_text );
128     ADD_META_2( VLC_META_COPYRIGHT, copyright_text );
129     ADD_META_2( N_("Comments"), description_text );
130
131 /* useless metadata */
132
133     //ADD_META_2( VLC_META_ENCODED_BY, encodedby_text );
134     /*  ADD_META( TRACKID )  Useless ? */
135     /*  ADD_URI - DO not show it, done outside */
136
137     metaLayout->setColumnStretch( 1, 2 );
138     metaLayout->setColumnMinimumWidth ( 1, 80 );
139 #undef ADD_META
140 #undef ADD_META_2
141
142     CONNECT( title_text, textEdited( QString ), this, enterEditMode() );
143     CONNECT( artist_text, textEdited( QString ), this, enterEditMode() );
144     CONNECT( collection_text, textEdited( QString ), this, enterEditMode() );
145     CONNECT( genre_text, textEdited( QString ), this, enterEditMode() );
146     CONNECT( seqnum_text, textEdited( QString ), this, enterEditMode() );
147
148     CONNECT( date_text, textEdited( QString ), this, enterEditMode() );
149     CONNECT( description_text, textEdited( QString ), this, enterEditMode() );
150 /*    CONNECT( rating_text, valueChanged( QString ), this, enterEditMode( QString ) );*/
151
152     /* We are not yet in Edit Mode */
153     b_inEditMode = false;
154 }
155
156 /**
157  * Update all the MetaData and art on an "item-changed" event
158  **/
159 void MetaPanel::update( input_item_t *p_item )
160 {
161     if( !p_item )
162     {
163         clear();
164         return;
165     }
166
167     /* Don't update if you are in edit mode */
168     if( b_inEditMode ) return;
169     else p_input = p_item;
170
171     char *psz_meta;
172 #define UPDATE_META( meta, widget ) {               \
173     psz_meta = input_item_Get##meta( p_item );      \
174     if( !EMPTY_STR( psz_meta ) )                    \
175         widget->setText( qfu( psz_meta ) );         \
176     else                                            \
177         widget->setText( "" ); }                    \
178     free( psz_meta );
179
180 #define UPDATE_META_INT( meta, widget ) {           \
181     psz_meta = input_item_Get##meta( p_item );      \
182     if( !EMPTY_STR( psz_meta ) )                    \
183         widget->setValue( atoi( psz_meta ) ); }     \
184     free( psz_meta );
185
186     /* Name / Title */
187     psz_meta = input_item_GetTitle( p_item );
188     char *psz_name = input_item_GetName( p_item );
189     if( !EMPTY_STR( psz_meta ) )
190         title_text->setText( qfu( psz_meta ) );
191     else if( !EMPTY_STR( psz_name ) )
192         title_text->setText( qfu( psz_name ) );
193     else title_text->setText( "" );
194     free( psz_meta );
195     free( psz_name );
196
197     /* URL / URI */
198     psz_meta = input_item_GetURL( p_item );
199     if( !EMPTY_STR( psz_meta ) )
200         emit uriSet( psz_meta );
201     else
202     {
203         free( psz_meta );
204         psz_meta = input_item_GetURI( p_item );
205         if( !EMPTY_STR( psz_meta ) )
206             emit uriSet( psz_meta );
207     }
208     free( psz_meta );
209
210     /* Other classic though */
211     UPDATE_META( Artist, artist_text );
212     UPDATE_META( Genre, genre_text );
213     UPDATE_META( Copyright, copyright_text );
214     UPDATE_META( Album, collection_text );
215     UPDATE_META( Description, description_text );
216     UPDATE_META( Language, language_text );
217     UPDATE_META( NowPlaying, nowplaying_text );
218     UPDATE_META( Publisher, publisher_text );
219 //    UPDATE_META( Setting, setting_text );
220 //    UPDATE_META( EncodedBy, encodedby_text );
221
222     UPDATE_META( Date, date_text );
223     UPDATE_META( TrackNum, seqnum_text );
224 //    UPDATE_META_INT( Rating, rating_text );
225
226 #undef UPDATE_META_INT
227 #undef UPDATE_META
228
229 }
230
231 /**
232  * Save the MetaData, triggered by parent->save Button
233  **/
234 void MetaPanel::saveMeta()
235 {
236     playlist_t *p_playlist;
237
238     meta_export_t p_export;
239     p_export.p_item = p_input;
240
241     if( p_input == NULL )
242         return;
243
244     /* we can write meta data only in a file */
245     vlc_mutex_lock( &p_input->lock );
246     int i_type = p_input->i_type;
247     vlc_mutex_unlock( &p_input->lock );
248     if( i_type == ITEM_TYPE_FILE )
249     {
250         char *psz_uri_orig = input_item_GetURI( p_input );
251         char *psz_uri = psz_uri_orig;
252         if( !strncmp( psz_uri, "file://", 7 ) )
253             psz_uri += 7; /* strlen("file://") = 7 */
254
255         p_export.psz_file = strndup( psz_uri, PATH_MAX );
256         free( psz_uri_orig );
257     }
258     else
259         return;
260
261     /* now we read the modified meta data */
262     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
263     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
264     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
265     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
266     input_item_SetTrackNum(  p_input, qtu( seqnum_text->text() ) );
267     input_item_SetDate(  p_input, qtu( date_text->text() ) );
268
269     input_item_SetCopyright( p_input, qtu( copyright_text->text() ) );
270     input_item_SetPublisher( p_input, qtu( publisher_text->text() ) );
271     input_item_SetDescription( p_input, qtu( description_text->text() ) );
272
273     p_playlist = pl_Hold( p_intf );
274     PL_LOCK;
275     p_playlist->p_private = &p_export;
276
277     module_t *p_mod = module_need( p_playlist, "meta writer", NULL, false );
278     if( p_mod )
279         module_unneed( p_playlist, p_mod );
280     PL_UNLOCK;
281     pl_Release( p_intf );
282
283     /* Reset the status of the mode. No need to emit any signal because parent
284        is the only caller */
285     b_inEditMode = false;
286 }
287
288
289 bool MetaPanel::isInEditMode()
290 {
291     return b_inEditMode;
292 }
293
294 void MetaPanel::enterEditMode()
295 {
296     msg_Dbg( p_intf, "Entering Edit MetaData Mode" );
297     setEditMode( true );
298 }
299
300 void MetaPanel::setEditMode( bool b_editing )
301 {
302     b_inEditMode = b_editing;
303     if( b_editing )emit editing();
304 }
305
306 /*
307  * Clear all the metadata widgets
308  */
309 void MetaPanel::clear()
310 {
311     title_text->clear();
312     artist_text->clear();
313     genre_text->clear();
314     copyright_text->clear();
315     collection_text->clear();
316     seqnum_text->clear();
317     description_text->clear();
318     date_text->clear();
319     language_text->clear();
320     nowplaying_text->clear();
321     publisher_text->clear();
322
323     setEditMode( false );
324     emit uriSet( "" );
325 }
326
327 /**
328  * Second Panel - Shows the extra metadata in a tree, non editable.
329  **/
330 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
331                                 intf_thread_t *_p_intf )
332                                 : QWidget( parent ), p_intf( _p_intf )
333 {
334      QGridLayout *layout = new QGridLayout(this);
335
336      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
337                  " are shown in this panel.\n" ) );
338      topLabel->setWordWrap( true );
339      layout->addWidget( topLabel, 0, 0 );
340
341      extraMetaTree = new QTreeWidget( this );
342      extraMetaTree->setAlternatingRowColors( true );
343      extraMetaTree->setColumnCount( 2 );
344      extraMetaTree->resizeColumnToContents( 0 );
345      extraMetaTree->header()->hide();
346 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
347  *                                             << qtr( "Value" ) );
348  * Useless, add this header if you think it would help the user          **
349  */
350
351      layout->addWidget( extraMetaTree, 1, 0 );
352 }
353
354 /**
355  * Update the Extra Metadata from p_meta->i_extras
356  **/
357 void ExtraMetaPanel::update( input_item_t *p_item )
358 {
359     if( !p_item )
360     {
361         clear();
362         return;
363     }
364
365     QList<QTreeWidgetItem *> items;
366
367     extraMetaTree->clear();
368
369     vlc_mutex_lock( &p_item->lock );
370     vlc_meta_t *p_meta = p_item->p_meta;
371     if( !p_meta )
372     {
373         vlc_mutex_unlock( &p_item->lock );
374         return;
375     }
376
377     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
378     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
379
380     for( int i = 0; ppsz_allkey[i] ; i++ )
381     {
382         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
383                 p_dict, ppsz_allkey[i] );
384         QStringList tempItem;
385         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
386         tempItem.append( qfu( psz_value ) );
387         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
388         free( ppsz_allkey[i] );
389     }
390     vlc_mutex_unlock( &p_item->lock );
391     free( ppsz_allkey );
392
393     extraMetaTree->addTopLevelItems( items );
394     extraMetaTree->resizeColumnToContents( 0 );
395 }
396
397 /**
398  * Clear the ExtraMetaData Tree
399  **/
400 void ExtraMetaPanel::clear()
401 {
402     extraMetaTree->clear();
403 }
404
405 /**
406  * Third panel - Stream info
407  * Display all codecs and muxers info that we could gather.
408  **/
409 InfoPanel::InfoPanel( QWidget *parent,
410                       intf_thread_t *_p_intf )
411                       : QWidget( parent ), p_intf( _p_intf )
412 {
413      QGridLayout *layout = new QGridLayout(this);
414
415      QList<QTreeWidgetItem *> items;
416
417      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
418               " stream is made of.\nMuxer, Audio and Video Codecs, Subtitles "
419               "are shown." ) );
420      topLabel->setWordWrap( true );
421      layout->addWidget( topLabel, 0, 0 );
422
423      InfoTree = new QTreeWidget(this);
424      InfoTree->setColumnCount( 1 );
425      InfoTree->header()->hide();
426      layout->addWidget(InfoTree, 1, 0 );
427 }
428
429 /**
430  * Update the Codecs information on parent->update()
431  **/
432 void InfoPanel::update( input_item_t *p_item)
433 {
434     if( !p_item )
435     {
436         clear();
437         return;
438     }
439
440     InfoTree->clear();
441     QTreeWidgetItem *current_item = NULL;
442     QTreeWidgetItem *child_item = NULL;
443
444     for( int i = 0; i< p_item->i_categories ; i++)
445     {
446         current_item = new QTreeWidgetItem();
447         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
448         InfoTree->addTopLevelItem( current_item );
449
450         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
451         {
452             child_item = new QTreeWidgetItem ();
453             child_item->setText( 0,
454                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
455                     + ": "
456                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
457
458             current_item->addChild(child_item);
459         }
460          InfoTree->setItemExpanded( current_item, true);
461     }
462 }
463
464 /**
465  * Clear the tree
466  **/
467 void InfoPanel::clear()
468 {
469     InfoTree->clear();
470 }
471
472 /**
473  * Save all the information to a file
474  * Not yet implemented.
475  **/
476 /*
477 void InfoPanel::saveCodecsInfo()
478 {}
479 */
480
481 /**
482  * Fourth Panel - Stats
483  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
484  */
485 InputStatsPanel::InputStatsPanel( QWidget *parent,
486                                   intf_thread_t *_p_intf )
487                                   : QWidget( parent ), p_intf( _p_intf )
488 {
489      QGridLayout *layout = new QGridLayout(this);
490
491      QList<QTreeWidgetItem *> items;
492
493      QLabel *topLabel = new QLabel( qtr( "Statistics about the currently "
494                  "playing media or stream." ) );
495      topLabel->setWordWrap( true );
496      layout->addWidget( topLabel, 0, 0 );
497
498      StatsTree = new QTreeWidget(this);
499      StatsTree->setColumnCount( 3 );
500      StatsTree->header()->hide();
501
502 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
503     itemName =                                                                 \
504       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
505     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
506
507 #define CREATE_CATEGORY( catName, itemText ) {                           \
508     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
509     catName->setExpanded( true );                                        \
510     StatsTree->addTopLevelItem( catName );    }
511
512 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
513     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
514     catName->addChild( itemName ); }
515
516     /* Create the main categories */
517     CREATE_CATEGORY( audio, qtr("Audio") );
518     CREATE_CATEGORY( video, qtr("Video") );
519     CREATE_CATEGORY( input, qtr("Input") );
520     CREATE_CATEGORY( streaming, qtr("Streaming") );
521
522     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
523                            "0", input , "kB" );
524     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
525                            "0", input, "kb/s" );
526     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
527     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
528                            "0", input, "kb/s" );
529     CREATE_AND_ADD_TO_CAT( corrupted_stat, qtr("Corrupted"),
530                            "0", input, "" );
531     CREATE_AND_ADD_TO_CAT( discontinuity_stat, qtr("Discontinuities"),
532                            "0", input, "" );
533
534     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
535                            "0", video, "" );
536     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
537                            "0", video, "" );
538     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
539                            "0", video, "" );
540
541     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
542     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
543                            "0", streaming, "kB" );
544     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrate"),
545                            "0", streaming, "kb/s" );
546
547     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
548                            "0", audio, "" );
549     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
550                            "0", audio, "" );
551     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
552
553     input->setExpanded( true );
554     video->setExpanded( true );
555     streaming->setExpanded( true );
556     audio->setExpanded( true );
557
558     StatsTree->resizeColumnToContents( 0 );
559     StatsTree->setColumnWidth( 1 , 200 );
560
561     layout->addWidget(StatsTree, 1, 0 );
562 }
563
564 /**
565  * Update the Statistics
566  **/
567 void InputStatsPanel::update( input_item_t *p_item )
568 {
569     assert( p_item );
570     vlc_mutex_lock( &p_item->p_stats->lock );
571
572 #define UPDATE( widget, format, calc... ) \
573     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
574
575     UPDATE( read_media_stat, "%8.0f",
576             (float)(p_item->p_stats->i_read_bytes)/1000);
577     UPDATE( input_bitrate_stat, "%6.0f",
578                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
579     UPDATE( demuxed_stat, "%8.0f",
580                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
581     UPDATE( stream_bitrate_stat, "%6.0f",
582                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
583     UPDATE( corrupted_stat, "%5i", p_item->p_stats->i_demux_corrupted );
584     UPDATE( discontinuity_stat, "%5i", p_item->p_stats->i_demux_discontinuity );
585
586     /* Video */
587     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
588     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
589     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
590
591     /* Sout */
592     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
593     UPDATE( send_bytes_stat, "%8.0f",
594             (float)(p_item->p_stats->i_sent_bytes)/1000 );
595     UPDATE( send_bitrate_stat, "%6.0f",
596             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
597
598     /* Audio*/
599     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
600     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
601     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
602
603     vlc_mutex_unlock(& p_item->p_stats->lock );
604 }
605
606 void InputStatsPanel::clear()
607 {
608 }
609