]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGView.cpp
epg: add an overlay containing the channels list
[vlc] / modules / gui / qt4 / components / epg / EPGView.cpp
1 /*****************************************************************************
2  * EPGView.cpp: EPGView
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 #include "EPGView.hpp"
25 #include "EPGItem.hpp"
26
27 #include <QDateTime>
28 #include <QMatrix>
29 #include <QPaintEvent>
30 #include <QScrollBar>
31 #include <QtDebug>
32 #include <QGraphicsTextItem>
33
34 EPGView::EPGView( QWidget *parent ) : QGraphicsView( parent )
35 {
36     setContentsMargins( 0, 0, 0, 0 );
37     setFrameStyle( QFrame::NoFrame );
38     setAlignment( Qt::AlignLeft | Qt::AlignTop );
39     setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
40
41     m_startTime = QDateTime::currentDateTime();
42
43     QGraphicsScene *EPGscene = new QGraphicsScene( this );
44
45     setScene( EPGscene );
46
47     connect( horizontalScrollBar(), SIGNAL( valueChanged(int) ),
48              this, SLOT( updateOverlayPosition(int) ) );
49
50     m_overlay = EPGscene->addRect( 0, 0, 100, 1, QPen(), QBrush( QColor( 40, 86, 255, 220 ) ) );
51     m_overlay->setFlag( QGraphicsItem::ItemIgnoresTransformations );
52     m_overlay->setZValue( 100 );
53
54     sceneRectChanged( scene()->sceneRect() );
55
56     connect( scene(), SIGNAL( sceneRectChanged(QRectF) ),
57              this, SLOT( sceneRectChanged(QRectF) ) );
58 }
59
60 void EPGView::updateOverlayPosition( int value )
61 {
62     int pos = value * matrix().inverted().m11();
63     m_overlay->setPos( pos, 0 );
64 }
65
66 void EPGView::setScale( double scaleFactor )
67 {
68     m_scaleFactor = scaleFactor;
69     QMatrix matrix;
70     matrix.scale( scaleFactor, 1 );
71     setMatrix( matrix );
72 }
73
74 void EPGView::setStartTime( const QDateTime& startTime )
75 {
76     QList<QGraphicsItem*> itemList = items();
77
78     int diff = startTime.secsTo( m_startTime );
79
80     for ( int i = 0; i < itemList.count(); ++i )
81     {
82         EPGItem* item = dynamic_cast<EPGItem*>( itemList.at( i ) );
83         if ( !item ) continue;
84         item->setStart( item->start().addSecs( diff ) );
85     }
86
87     m_startTime = startTime;
88
89     // Our start time has changed
90     emit startTimeChanged( startTime );
91 }
92
93 const QDateTime& EPGView::startTime()
94 {
95     return m_startTime;
96 }
97
98 void EPGView::addEvent( EPGEvent* event )
99 {
100     if ( !m_channels.contains( event->channelName ) )
101     {
102         m_channels.append( event->channelName );
103         QGraphicsTextItem* channelTitle = new QGraphicsTextItem( event->channelName, m_overlay );
104         channelTitle->setZValue( 101 );
105         channelTitle->setPos( 0, m_channels.indexOf( event->channelName ) * TRACKS_HEIGHT );
106         channelTitle->setTextWidth( 100 );
107     }
108
109     EPGItem* item = new EPGItem( this );
110     item->setChannel( m_channels.indexOf( event->channelName ) );
111     item->setStart( event->start );
112     item->setDuration( event->duration );
113     item->setName( event->name );
114     item->setDescription( event->description );
115     item->setShortDescription( event->shortDescription );
116     item->setCurrent( event->current );
117
118     scene()->addItem( item );
119 }
120
121 void EPGView::updateEvent( EPGEvent* event )
122 {
123     //qDebug() << "Update event: " << event->name;
124 }
125
126 void EPGView::delEvent( EPGEvent* event )
127 {
128     //qDebug() << "Del event: " << event->name;
129 }
130
131 void EPGView::drawBackground( QPainter *painter, const QRectF &rect )
132 {
133     painter->setPen( QPen( QColor( 72, 72, 72 ) ) );
134
135     QPointF p = mapToScene( width(), 0 );
136
137     int y = 0;
138     for ( int i = 0; i < m_channels.count() + 1; ++i )
139     {
140         painter->drawLine( 0,
141                            y * TRACKS_HEIGHT,
142                            p.x(),
143                            y * TRACKS_HEIGHT );
144         ++y;
145     }
146 }
147
148 void EPGView::updateDuration()
149 {
150     QDateTime lastItem;
151     QList<QGraphicsItem*> list = items();
152
153     for ( int i = 0; i < list.count(); ++i )
154     {
155         EPGItem* item = dynamic_cast<EPGItem*>( list.at( i ) );
156         if ( !item ) continue;
157         QDateTime itemEnd = item->start().addSecs( item->duration() );
158
159         if ( itemEnd > lastItem )
160             lastItem = itemEnd;
161     }
162     m_duration = m_startTime.secsTo( lastItem );
163     emit durationChanged( m_duration );
164 }
165
166 void EPGView::eventFocused( EPGEvent *ev )
167 {
168     emit eventFocusedChanged( ev );
169 }
170
171 void EPGView::sceneRectChanged( const QRectF& rect )
172 {
173     m_overlay->setRect( 0, 0, m_overlay->rect().width(), rect.height() );
174 }