]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGView.cpp
5a8da879a47e7b31a01590982185cef979852d0e
[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::Box );
38     setAlignment( Qt::AlignLeft | Qt::AlignTop );
39
40     m_startTime = QDateTime::currentDateTime();
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 = qgraphicsitem_cast<EPGItem*>( itemList.at( i ) );
64         if ( !item ) continue;
65         item->setStart( item->start().addSecs( diff ) );
66     }
67
68     m_startTime = startTime;
69
70     // Our start time has changed
71     emit startTimeChanged( startTime );
72 }
73
74 const QDateTime& EPGView::startTime()
75 {
76     return m_startTime;
77 }
78
79 void EPGView::addEvent( EPGEvent* event )
80 {
81     if ( !m_channels.contains( event->channelName ) )
82         m_channels.append( event->channelName );
83
84     EPGItem* item = new EPGItem( this );
85     item->setChannel( m_channels.indexOf( event->channelName ) );
86     item->setStart( event->start );
87     item->setDuration( event->duration );
88     item->setName( event->name );
89     item->setDescription( event->description );
90     item->setShortDescription( event->shortDescription );
91     item->setCurrent( event->current );
92
93     scene()->addItem( item );
94 }
95
96 void EPGView::updateEvent( EPGEvent* event )
97 {
98     //qDebug() << "Update event: " << event->name;
99 }
100
101 void EPGView::delEvent( EPGEvent* event )
102 {
103     //qDebug() << "Del event: " << event->name;
104 }
105
106 void EPGView::updateDuration()
107 {
108     QDateTime lastItem;
109     QList<QGraphicsItem*> list = items();
110
111     for ( int i = 0; i < list.count(); ++i )
112     {
113         EPGItem* item = qgraphicsitem_cast<EPGItem*>( list.at( i ) );
114         if ( !item ) continue;
115         QDateTime itemEnd = item->start().addSecs( item->duration() );
116
117         if ( itemEnd > lastItem )
118             lastItem = itemEnd;
119     }
120     m_duration = m_startTime.secsTo( lastItem );
121     emit durationChanged( m_duration );
122 }
123
124 QList<QString> EPGView::getChannelList()
125 {
126     return m_channels;
127 }
128
129 void EPGView::eventFocused( EPGEvent *ev )
130 {
131     emit eventFocusedChanged( ev );
132 }