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