]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGWidget.cpp
You shall include <config.h>
[vlc] / modules / gui / qt4 / components / epg / EPGWidget.cpp
1 /*****************************************************************************
2  * EPGWidget.h : EPGWidget
3  ****************************************************************************
4  * Copyright © 2009-2010 VideoLAN
5  * $Id$
6  *
7  * Authors: Ludovic Fauvet <etix@l0cal.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include "EPGWidget.hpp"
29
30 #include <QGridLayout>
31 #include <QScrollBar>
32 #include <QDebug>
33 #include <QLabel>
34 #include "qt4.hpp"
35
36 ChannelsWidget::ChannelsWidget( QWidget *parent ) : QWidget( parent )
37 {
38     setContentsMargins( 0, 0, 0, 0 );
39     setMaximumWidth( 50 );
40     setFocusPolicy( Qt::ClickFocus );
41 }
42
43 EPGWidget::EPGWidget( QWidget *parent ) : QWidget( parent )
44 {
45     QGridLayout* layout = new QGridLayout( this );
46
47     m_rulerWidget = new EPGRuler( this );
48     m_channelsWidget = new ChannelsWidget( this );
49     m_epgView = new EPGView( this );
50
51     m_channelsWidget->setMinimumWidth( 40 );
52
53     m_epgView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
54     setZoom( 1 );
55
56     layout->addWidget( m_rulerWidget,       0, 1 );
57     layout->addWidget( m_channelsWidget,    1, 0 );
58     layout->addWidget( m_epgView,           1, 1 );
59     layout->setSpacing( 0 );
60     setLayout( layout );
61
62     connect( m_epgView, SIGNAL( startTimeChanged(QDateTime) ),
63              m_rulerWidget, SLOT( setStartTime(QDateTime) ) );
64     connect( m_epgView, SIGNAL( durationChanged(int) ),
65              m_rulerWidget, SLOT( setDuration(int) ) );
66     connect( m_epgView->horizontalScrollBar(), SIGNAL( valueChanged(int) ),
67              m_rulerWidget, SLOT( setOffset(int) ) );
68     connect( m_epgView, SIGNAL( eventFocusedChanged(EPGEvent*)),
69              this, SIGNAL(itemSelectionChanged(EPGEvent*)) );
70 }
71
72 void EPGWidget::setZoom( int level )
73 {
74     double scale = (double)level / 20;
75     m_epgView->setScale( scale );
76     m_rulerWidget->setScale( scale );
77 }
78
79 void EPGWidget::updateEPG( vlc_epg_t **pp_epg, int i_epg )
80 {
81     m_epgView->setStartTime( QDateTime::currentDateTime() );
82     for ( int i = 0; i < i_epg; ++i )
83     {
84         vlc_epg_t *p_epg = pp_epg[i];
85         QString channelName = qfu( p_epg->psz_name );
86
87         for ( int j = 0; j < p_epg->i_event; ++j )
88         {
89             EPGEvent *item = NULL;
90             vlc_epg_event_t *p_event = p_epg->pp_event[j];
91             QString eventName = qfu( p_event->psz_name );
92
93             QList<EPGEvent*> events = m_events.values( channelName );
94
95             for ( int k = 0; k < events.count(); ++k )
96             {
97                 if ( events.at( k )->name == eventName &&
98                      events.at( k )->channelName == channelName )
99                 {
100                     item = events.at( k );
101                     item->updated = true;
102                     item->description = qfu( p_event->psz_description );
103                     item->shortDescription = qfu( p_event->psz_short_description );
104                     item->start = QDateTime::fromTime_t( p_event->i_start );
105                     item->duration = p_event->i_duration;
106                     item->current = ( p_epg->p_current == p_event ) ? true : false;
107
108                     if ( item->start < m_epgView->startTime() )
109                         m_epgView->setStartTime( item->start );
110
111                     m_epgView->updateEvent( item );
112                     break;
113                 }
114             }
115
116             if ( !item )
117             {
118                 item = new EPGEvent( eventName );
119                 item->description = qfu( p_event->psz_description );
120                 item->shortDescription = qfu( p_event->psz_short_description );
121                 item->start = QDateTime::fromTime_t( p_event->i_start );
122                 item->duration = p_event->i_duration;
123                 item->channelName = channelName;
124                 item->current = ( p_epg->p_current == p_event ) ? true : false;
125                 m_events.insert( channelName, item );
126
127                 if ( item->start < m_epgView->startTime() )
128                     m_epgView->setStartTime( item->start );
129
130                 m_epgView->addEvent( item );
131             }
132         }
133     }
134
135     // Remove old items
136     QMap<QString, EPGEvent*>::iterator i = m_events.begin();
137     while ( i != m_events.end() )
138     {
139         EPGEvent* item = i.value();
140         if ( !item->updated )
141         {
142             m_epgView->delEvent( item );
143             delete item;
144             i = m_events.erase( i );
145         }
146         else
147             item->updated = false;
148
149         ++i;
150     }
151
152     // Update the global duration
153     m_epgView->updateDuration();
154 }
155