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