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