]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGView.cpp
Initial commit for EPG class for EPG Viewing
[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 <QMatrix>
25 #include "EPGView.hpp"
26 #include "EPGItem.hpp"
27 #include <QtDebug>
28
29 EPGView::EPGView( QWidget *parent ) : QGraphicsView( parent )
30 {
31     setContentsMargins( 0, 0, 0, 0 );
32     setFrameStyle( QFrame::NoFrame );
33     setAlignment( Qt::AlignLeft | Qt::AlignTop );
34
35     m_startTime = QDateTime::currentDateTime();
36
37     //tmp
38     setSceneRect( 0, 0, 20000, 200 );
39
40     QGraphicsScene *EPGscene = new QGraphicsScene( this );
41
42     setScene( EPGscene );
43 }
44
45 void EPGView::setScale( double scaleFactor )
46 {
47     m_scaleFactor = scaleFactor;
48     QMatrix matrix;
49     matrix.scale( scaleFactor, 1 );
50     setMatrix( matrix );
51 }
52
53 void EPGView::setStartTime( const QDateTime& startTime )
54 {
55     QList<QGraphicsItem*> itemList = items();
56
57     int diff = startTime.secsTo( m_startTime );
58
59     for ( int i = 0; i < itemList.count(); ++i )
60     {
61         EPGItem* item = static_cast<EPGItem*>( itemList.at( i ) );
62         item->setStart( item->start().addSecs( diff ) );
63     }
64
65     m_startTime = startTime;
66
67     // Our start time has changed
68     emit startTimeChanged( startTime );
69 }
70
71 const QDateTime& EPGView::startTime()
72 {
73     return m_startTime;
74 }
75
76 void EPGView::addEvent( EPGEvent* event )
77 {
78     if ( !m_channels.contains( event->channelName ) )
79         m_channels.append( event->channelName );
80
81     EPGItem* item = new EPGItem( this );
82     item->setChannel( m_channels.indexOf( event->channelName ) );
83     item->setStart( event->start );
84     item->setDuration( event->duration );
85     item->setName( event->name );
86     item->setDescription( event->description );
87     item->setShortDescription( event->shortDescription );
88     item->setCurrent( event->current );
89
90     scene()->addItem( item );
91 }
92
93 void EPGView::updateEvent( EPGEvent* event )
94 {
95     qDebug() << "Update event: " << event->name;
96 }
97
98 void EPGView::delEvent( EPGEvent* event )
99 {
100     qDebug() << "Del event: " << event->name;
101 }
102
103 void EPGView::drawBackground( QPainter *painter, const QRectF &rect )
104 {
105     painter->setPen( QPen( QColor( 72, 72, 72 ) ) );
106
107     QPointF p = mapToScene( width(), 0 );
108
109     int y = 0;
110     for ( int i = 0; i < m_channels.count() + 1; ++i )
111     {
112         painter->drawLine( 0,
113                            y * TRACKS_HEIGHT,
114                            p.x(),
115                            y * TRACKS_HEIGHT );
116         ++y;
117     }
118 }
119
120 void EPGView::updateDuration()
121 {
122     QDateTime lastItem;
123     QList<QGraphicsItem*> list = items();
124
125     for ( int i = 0; i < list.count(); ++i )
126     {
127         EPGItem* item = static_cast<EPGItem*>( list.at( i ) );
128         QDateTime itemEnd = item->start().addSecs( item->duration() );
129
130         if ( itemEnd > lastItem )
131             lastItem = itemEnd;
132     }
133     m_duration = m_startTime.secsTo( lastItem );
134     emit durationChanged( m_duration );
135 }