]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/infopanels.cpp
Second try, show save meta button if user changes any input field, also
[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     CONNECT( title_text, textEdited( QString ), this, editMeta( QString ) );
132     CONNECT( description_text, textEdited( QString ), this, editMeta( QString ) );
133     CONNECT( artist_text, textEdited( QString ), this, editMeta( QString ) );
134     CONNECT( collection_text, textEdited( QString ), this, editMeta( QString ) );
135     CONNECT( genre_text, textEdited( QString ), this, editMeta( QString ) );
136     CONNECT( description_text, textEdited( QString ), this, editMeta( QString ) );
137     CONNECT( date_text, valueChanged( QString ), this, editMeta( QString ) );
138     CONNECT( seqnum_text, valueChanged( QString ), this, editMeta( QString ) );
139     CONNECT( rating_text, valueChanged( QString ), this, editMeta( QString ) );
140     in_edit = false;
141 }
142
143 MetaPanel::~MetaPanel(){}
144
145 /**
146  * Save the MetaData, triggered by parent->save Button
147  **/
148 void MetaPanel::saveMeta()
149 {
150     playlist_t *p_playlist;
151     char psz[5];
152
153     meta_export_t p_export;
154     p_export.p_item = p_input;
155
156     if( p_input == NULL )
157         return;
158
159     /* we can write meta data only in a file */
160     if( ( p_input->i_type == ITEM_TYPE_AFILE ) || \
161         ( p_input->i_type == ITEM_TYPE_VFILE ) )
162         /* some audio files are detected as video files */
163     {
164         char *psz_uri = p_input->psz_uri;
165         if( !strncmp( psz_uri, "file://", 7 ) )
166             psz_uri += 7; /* strlen("file://") = 7 */
167
168         p_export.psz_file = strndup( psz_uri, PATH_MAX );
169     }
170     else
171         return;
172
173     /* now we read the modified meta data */
174     input_item_SetArtist( p_input, qtu( artist_text->text() ) );
175     input_item_SetAlbum(  p_input, qtu( collection_text->text() ) );
176     input_item_SetGenre(  p_input, qtu( genre_text->text() ) );
177
178     snprintf( psz, sizeof(psz), "%d", date_text->value() );
179     input_item_SetDate(  p_input, psz );
180
181     snprintf( psz, sizeof(psz), "%d", seqnum_text->value() );
182     input_item_SetTrackNum(  p_input, psz );
183
184     input_item_SetTitle(  p_input, qtu( title_text->text() ) );
185
186     p_playlist = pl_Yield( p_intf );
187
188     PL_LOCK;
189     p_playlist->p_private = &p_export;
190
191     module_t *p_mod = module_Need( p_playlist, "meta writer", NULL, 0 );
192     if( p_mod )
193         module_Unneed( p_playlist, p_mod );
194     PL_UNLOCK;
195     pl_Release( p_playlist );
196     in_edit = false;
197 }
198
199 void MetaPanel::editMeta( QString edit )
200 {
201     in_edit = true;
202     emit editing();
203 }
204
205 void MetaPanel::setEdit( bool editing )
206 {
207    in_edit = editing;
208 }
209
210 void MetaPanel::setInput( input_item_t *input )
211 {
212     if( in_edit ) return;
213
214     p_input = input;
215 }
216
217 /**
218  * Update all the MetaData and art on an "item-changed" event
219  **/
220 void MetaPanel::update( input_item_t *p_item )
221 {
222     if( in_edit ) return;
223     char *psz_meta; 
224 #define UPDATE_META( meta, widget ) {               \
225     psz_meta = input_item_Get##meta( p_item );      \
226     if( !EMPTY_STR( psz_meta ) )                    \
227         widget->setText( qfu( psz_meta ) );         \
228     else                                            \
229         widget->setText( "" ); }                    \
230     free( psz_meta );
231
232 #define UPDATE_META_INT( meta, widget ) {           \
233     psz_meta = input_item_Get##meta( p_item );      \
234     if( !EMPTY_STR( psz_meta ) )                    \
235         widget->setValue( atoi( psz_meta ) ); }     \
236     free( psz_meta );
237
238
239     /* Name / Title */
240     psz_meta = input_item_GetTitle( p_item );
241     if( !EMPTY_STR( psz_meta ) )
242         title_text->setText( qfu( psz_meta ) );
243     else if( !EMPTY_STR( p_item->psz_name ) )
244         title_text->setText( qfu( p_item->psz_name ) );
245     else title_text->setText( "" );
246     free( psz_meta );
247
248     /* URL / URI */
249     psz_meta = input_item_GetURL( p_item );
250     if( !EMPTY_STR( psz_meta ) )
251         emit uriSet( QString( psz_meta ) );
252     else if( !EMPTY_STR( p_item->psz_uri ) )
253         emit uriSet( QString( p_item->psz_uri ) );
254     free( psz_meta );
255
256     /* Other classic though */
257     UPDATE_META( Artist, artist_text );
258     UPDATE_META( Genre, genre_text );
259     UPDATE_META( Copyright, copyright_text );
260     UPDATE_META( Album, collection_text );
261     UPDATE_META( Description, description_text );
262     UPDATE_META( Language, language_text );
263     UPDATE_META( NowPlaying, nowplaying_text );
264     UPDATE_META( Publisher, publisher_text );
265     UPDATE_META( Setting, setting_text );
266
267     UPDATE_META_INT( Date, date_text );
268     UPDATE_META_INT( TrackNum, seqnum_text );
269     UPDATE_META_INT( Rating, rating_text );
270
271 #undef UPDATE_META_INT
272 #undef UPDATE_META
273
274     /* Art Urls */
275     psz_meta = input_item_GetArtURL( p_item );
276     if( psz_meta && !strncmp( psz_meta, "file://", 7 ) )
277     {
278         QString artUrl = qfu( psz_meta ).replace( "file://",QString("" ) );
279         art_cover->setPixmap( QPixmap( artUrl ) );
280     }
281     else
282         art_cover->setPixmap( QPixmap( ":/noart.png" ) );
283     free( psz_meta );
284 }
285
286 /*
287  * Clear all the metadata widgets
288  * Unused yet
289  */
290 void MetaPanel::clear(){}
291
292 /**
293  * Second Panel - Shows the extra metadata in a tree, non editable.
294  **/
295 ExtraMetaPanel::ExtraMetaPanel( QWidget *parent,
296                                 intf_thread_t *_p_intf )
297                                 : QWidget( parent ), p_intf( _p_intf )
298 {
299      QGridLayout *layout = new QGridLayout(this);
300
301      QLabel *topLabel = new QLabel( qtr( "Extra metadata and other information"
302                  " are shown in this list.\n" ) );
303      topLabel->setWordWrap( true );
304      layout->addWidget( topLabel, 0, 0 );
305
306      extraMetaTree = new QTreeWidget( this );
307      extraMetaTree->setAlternatingRowColors( true );
308      extraMetaTree->setColumnCount( 2 );
309      extraMetaTree->header()->hide();
310 /*     QStringList headerList = ( QStringList() << qtr( "Type" )
311  *                                             << qtr( "Value" ) );
312  * Useless, add this header if you think it would help the user          **
313  */
314
315      layout->addWidget( extraMetaTree, 1, 0 );
316 }
317
318 /**
319  * Update the Extra Metadata from p_meta->i_extras 
320  **/
321 void ExtraMetaPanel::update( input_item_t *p_item )
322 {
323     vlc_mutex_lock( &p_item->lock );
324     vlc_meta_t *p_meta = p_item->p_meta;
325     if( !p_meta )
326         return;
327     QStringList tempItem;
328
329     QList<QTreeWidgetItem *> items;
330     vlc_dictionary_t * p_dict = &p_meta->extra_tags;
331     char ** ppsz_allkey = vlc_dictionary_all_keys( p_dict );
332     for (int i = 0; ppsz_allkey[i] ; i++ )
333     {
334         const char * psz_value = (const char *)vlc_dictionary_value_for_key(
335                 p_dict, ppsz_allkey[i] );
336         tempItem.append( qfu( ppsz_allkey[i] ) + " : ");
337         tempItem.append( qfu( psz_value ) );
338         items.append( new QTreeWidgetItem ( extraMetaTree, tempItem ) );
339         free( ppsz_allkey[i] );
340     }
341     vlc_mutex_unlock( &p_item->lock );
342     free( ppsz_allkey );
343     extraMetaTree->addTopLevelItems( items );
344 }
345
346 /**
347  * Clear the ExtraMetaData Tree
348  **/
349 void ExtraMetaPanel::clear()
350 {
351     extraMetaTree->clear();
352 }
353
354 /**
355  * Third panel - Stream info
356  * Display all codecs and muxers info that we could gather.
357  **/
358 InfoPanel::InfoPanel( QWidget *parent,
359                       intf_thread_t *_p_intf )
360                       : QWidget( parent ), p_intf( _p_intf )
361 {
362      QGridLayout *layout = new QGridLayout(this);
363
364      QList<QTreeWidgetItem *> items;
365
366      QLabel *topLabel = new QLabel( qtr( "Information about what your media or"
367               " stream is made of.\n Muxer, Audio and Video Codecs, Subtitles "
368               "are shown." ) );
369      topLabel->setWordWrap( true );
370      layout->addWidget( topLabel, 0, 0 );
371
372      InfoTree = new QTreeWidget(this);
373      InfoTree->setColumnCount( 1 );
374      InfoTree->header()->hide();
375      layout->addWidget(InfoTree, 1, 0 );
376 }
377
378 InfoPanel::~InfoPanel()
379 {
380 }
381
382 /**
383  * Update the Codecs information on parent->update()
384  **/
385 void InfoPanel::update( input_item_t *p_item)
386 {
387     InfoTree->clear();
388     QTreeWidgetItem *current_item = NULL;
389     QTreeWidgetItem *child_item = NULL;
390
391     for( int i = 0; i< p_item->i_categories ; i++)
392     {
393         current_item = new QTreeWidgetItem();
394         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
395         InfoTree->addTopLevelItem( current_item );
396
397         for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
398         {
399             child_item = new QTreeWidgetItem ();
400             child_item->setText( 0,
401                     qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
402                     + ": "
403                     + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
404
405             current_item->addChild(child_item);
406         }
407          InfoTree->setItemExpanded( current_item, true);
408     }
409 }
410
411 /**
412  * Clear the tree
413  **/
414 void InfoPanel::clear()
415 {
416     InfoTree->clear();
417 }
418
419 /**
420  * Save all the information to a file
421  * Not yet implemented.
422  **/
423 /*
424 void InfoPanel::saveCodecsInfo()
425 {
426
427 }
428 */
429
430 /**
431  * Fourth Panel - Stats
432  * Displays the Statistics for reading/streaming/encoding/displaying in a tree
433  */
434 InputStatsPanel::InputStatsPanel( QWidget *parent,
435                                   intf_thread_t *_p_intf )
436                                   : QWidget( parent ), p_intf( _p_intf )
437 {
438      QGridLayout *layout = new QGridLayout(this);
439
440      QList<QTreeWidgetItem *> items;
441
442      QLabel *topLabel = new QLabel( qtr( "Various statistics about the current"
443                  " media or stream.\n Played and streamed info are shown." ) );
444      topLabel->setWordWrap( true );
445      layout->addWidget( topLabel, 0, 0 );
446
447      StatsTree = new QTreeWidget(this);
448      StatsTree->setColumnCount( 3 );
449      StatsTree->header()->hide();
450
451 #define CREATE_TREE_ITEM( itemName, itemText, itemValue, unit ) {              \
452     itemName =                                                                 \
453       new QTreeWidgetItem((QStringList () << itemText << itemValue << unit )); \
454     itemName->setTextAlignment( 1 , Qt::AlignRight ) ; }
455
456 #define CREATE_CATEGORY( catName, itemText ) {                           \
457     CREATE_TREE_ITEM( catName, itemText , "", "" );                      \
458     catName->setExpanded( true );                                        \
459     StatsTree->addTopLevelItem( catName );    }
460
461 #define CREATE_AND_ADD_TO_CAT( itemName, itemText, itemValue, catName, unit ){ \
462     CREATE_TREE_ITEM( itemName, itemText, itemValue, unit );                   \
463     catName->addChild( itemName ); }
464
465     /* Create the main categories */
466     CREATE_CATEGORY( input, qtr("Input") );
467     CREATE_CATEGORY( video, qtr("Video") );
468     CREATE_CATEGORY( streaming, qtr("Streaming") );
469     CREATE_CATEGORY( audio, qtr("Audio") );
470
471     CREATE_AND_ADD_TO_CAT( read_media_stat, qtr("Read at media"),
472                            "0", input , "kB" );
473     CREATE_AND_ADD_TO_CAT( input_bitrate_stat, qtr("Input bitrate"),
474                            "0", input, "kb/s" );
475     CREATE_AND_ADD_TO_CAT( demuxed_stat, qtr("Demuxed"), "0", input, "kB") ;
476     CREATE_AND_ADD_TO_CAT( stream_bitrate_stat, qtr("Stream bitrate"),
477                            "0", input, "kb/s" );
478
479     CREATE_AND_ADD_TO_CAT( vdecoded_stat, qtr("Decoded blocks"),
480                            "0", video, "" );
481     CREATE_AND_ADD_TO_CAT( vdisplayed_stat, qtr("Displayed frames"),
482                            "0", video, "" );
483     CREATE_AND_ADD_TO_CAT( vlost_frames_stat, qtr("Lost frames"),
484                            "0", video, "" );
485
486     CREATE_AND_ADD_TO_CAT( send_stat, qtr("Sent packets"), "0", streaming, "" );
487     CREATE_AND_ADD_TO_CAT( send_bytes_stat, qtr("Sent bytes"),
488                            "0", streaming, "kB" );
489     CREATE_AND_ADD_TO_CAT( send_bitrate_stat, qtr("Sent bitrates"),
490                            "0", streaming, "kb/s" );
491
492     CREATE_AND_ADD_TO_CAT( adecoded_stat, qtr("Decoded blocks"),
493                            "0", audio, "" );
494     CREATE_AND_ADD_TO_CAT( aplayed_stat, qtr("Played buffers"),
495                            "0", audio, "" );
496     CREATE_AND_ADD_TO_CAT( alost_stat, qtr("Lost buffers"), "0", audio, "" );
497
498     input->setExpanded( true );
499     video->setExpanded( true );
500     streaming->setExpanded( true );
501     audio->setExpanded( true );
502
503     StatsTree->resizeColumnToContents( 0 );
504     StatsTree->setColumnWidth( 1 , 100 );
505
506     layout->addWidget(StatsTree, 1, 0 );
507 }
508
509 InputStatsPanel::~InputStatsPanel()
510 {
511 }
512
513 /**
514  * Update the Statistics
515  **/
516 void InputStatsPanel::update( input_item_t *p_item )
517 {
518     vlc_mutex_lock( &p_item->p_stats->lock );
519
520 #define UPDATE( widget, format, calc... ) \
521     { QString str; widget->setText( 1 , str.sprintf( format, ## calc ) );  }
522
523     UPDATE( read_media_stat, "%8.0f",
524             (float)(p_item->p_stats->i_read_bytes)/1000);
525     UPDATE( input_bitrate_stat, "%6.0f",
526                     (float)(p_item->p_stats->f_input_bitrate * 8000 ));
527     UPDATE( demuxed_stat, "%8.0f",
528                     (float)(p_item->p_stats->i_demux_read_bytes)/1000 );
529     UPDATE( stream_bitrate_stat, "%6.0f",
530                     (float)(p_item->p_stats->f_demux_bitrate * 8000 ));
531
532     /* Video */
533     UPDATE( vdecoded_stat, "%5i", p_item->p_stats->i_decoded_video );
534     UPDATE( vdisplayed_stat, "%5i", p_item->p_stats->i_displayed_pictures );
535     UPDATE( vlost_frames_stat, "%5i", p_item->p_stats->i_lost_pictures );
536
537     /* Sout */
538     UPDATE( send_stat, "%5i", p_item->p_stats->i_sent_packets );
539     UPDATE( send_bytes_stat, "%8.0f",
540             (float)(p_item->p_stats->i_sent_bytes)/1000 );
541     UPDATE( send_bitrate_stat, "%6.0f",
542             (float)(p_item->p_stats->f_send_bitrate*8)*1000 );
543
544     /* Audio*/
545     UPDATE( adecoded_stat, "%5i", p_item->p_stats->i_decoded_audio );
546     UPDATE( aplayed_stat, "%5i", p_item->p_stats->i_played_abuffers );
547     UPDATE( alost_stat, "%5i", p_item->p_stats->i_lost_abuffers );
548
549     vlc_mutex_unlock(& p_item->p_stats->lock );
550 }
551
552 void InputStatsPanel::clear()
553 {
554 }
555
556