Qt/EPG: Set the EPGView start time correctly.
[vlc/gmpfix.git] / modules / gui / qt4 / components / epg / EPGView.cpp
blobcb6690c7a24b1e04fa4130f24072dc820d4bf830
1 /*****************************************************************************
2 * EPGView.cpp: EPGView
3 ****************************************************************************
4 * Copyright © 2009-2010 VideoLAN
5 * $Id$
7 * Authors: Ludovic Fauvet <etix@l0cal.com>
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.
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.
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 *****************************************************************************/
24 #include "EPGView.hpp"
25 #include "EPGItem.hpp"
27 #include <QDateTime>
28 #include <QMatrix>
29 #include <QPaintEvent>
30 #include <QScrollBar>
31 #include <QtDebug>
32 #include <QGraphicsTextItem>
34 EPGView::EPGView( QWidget *parent ) : QGraphicsView( parent )
36 setContentsMargins( 0, 0, 0, 0 );
37 setFrameStyle( QFrame::Box );
38 setAlignment( Qt::AlignLeft | Qt::AlignTop );
40 m_startTime = QDateTime::currentDateTime();
42 QGraphicsScene *EPGscene = new QGraphicsScene( this );
44 setScene( EPGscene );
47 void EPGView::setScale( double scaleFactor )
49 m_scaleFactor = scaleFactor;
50 QMatrix matrix;
51 matrix.scale( scaleFactor, 1 );
52 setMatrix( matrix );
55 void EPGView::updateStartTime()
57 QList<QGraphicsItem*> itemList = items();
59 /* Set the new start time. */
60 for ( int i = 0; i < itemList.count(); ++i )
62 EPGItem* item = qgraphicsitem_cast<EPGItem*>( itemList.at( i ) );
63 if ( !item )
64 continue;
65 if( i == 0 )
66 m_startTime = item->start();
67 if ( item->start() < m_startTime )
68 m_startTime = item->start();
71 /* Update the position of all items. */
72 for ( int i = 0; i < itemList.count(); ++i )
74 EPGItem* item = qgraphicsitem_cast<EPGItem*>( itemList.at( i ) );
75 if ( !item )
76 continue;
77 item->updatePos();
80 // Our start time may have changed.
81 emit startTimeChanged( m_startTime );
84 const QDateTime& EPGView::startTime()
86 return m_startTime;
89 void EPGView::addEvent( EPGEvent* event )
91 if ( !m_channels.contains( event->channelName ) )
92 m_channels.append( event->channelName );
94 EPGItem* item = new EPGItem( this );
95 item->setChannel( m_channels.indexOf( event->channelName ) );
96 item->setStart( event->start );
97 item->setDuration( event->duration );
98 item->setName( event->name );
99 item->setDescription( event->description );
100 item->setShortDescription( event->shortDescription );
101 item->setCurrent( event->current );
103 event->item = item;
105 scene()->addItem( item );
108 void EPGView::delEvent( EPGEvent* event )
110 if( event->item != NULL )
111 scene()->removeItem( event->item );
112 event->item = NULL;
115 void EPGView::updateDuration()
117 QDateTime lastItem;
118 QList<QGraphicsItem*> list = items();
120 for ( int i = 0; i < list.count(); ++i )
122 EPGItem* item = qgraphicsitem_cast<EPGItem*>( list.at( i ) );
123 if ( !item ) continue;
124 QDateTime itemEnd = item->start().addSecs( item->duration() );
126 if ( itemEnd > lastItem )
127 lastItem = itemEnd;
129 m_duration = m_startTime.secsTo( lastItem );
130 emit durationChanged( m_duration );
133 QList<QString> EPGView::getChannelList()
135 return m_channels;
138 void EPGView::eventFocused( EPGEvent *ev )
140 emit eventFocusedChanged( ev );