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