1 /*****************************************************************************
3 ****************************************************************************
4 * Copyright © 2009-2010 VideoLAN
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"
29 #include <QPaintEvent>
32 #include <QGraphicsTextItem>
34 EPGView::EPGView( QWidget *parent ) : QGraphicsView( parent )
36 setContentsMargins( 0, 0, 0, 0 );
37 setFrameStyle( QFrame::NoFrame );
38 setAlignment( Qt::AlignLeft | Qt::AlignTop );
39 setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
41 m_startTime = QDateTime::currentDateTime();
43 QGraphicsScene *EPGscene = new QGraphicsScene( this );
47 connect( horizontalScrollBar(), SIGNAL( valueChanged(int) ),
48 this, SLOT( updateOverlayPosition(int) ) );
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 );
54 sceneRectChanged( scene()->sceneRect() );
56 connect( scene(), SIGNAL( sceneRectChanged(QRectF) ),
57 this, SLOT( sceneRectChanged(QRectF) ) );
60 void EPGView::updateOverlayPosition( int value )
62 int pos = value * matrix().inverted().m11();
63 m_overlay->setPos( pos, 0 );
66 void EPGView::setScale( double scaleFactor )
68 m_scaleFactor = scaleFactor;
70 matrix.scale( scaleFactor, 1 );
74 void EPGView::setStartTime( const QDateTime& startTime )
76 QList<QGraphicsItem*> itemList = items();
78 int diff = startTime.secsTo( m_startTime );
80 for ( int i = 0; i < itemList.count(); ++i )
82 EPGItem* item = dynamic_cast<EPGItem*>( itemList.at( i ) );
83 if ( !item ) continue;
84 item->setStart( item->start().addSecs( diff ) );
87 m_startTime = startTime;
89 // Our start time has changed
90 emit startTimeChanged( startTime );
93 const QDateTime& EPGView::startTime()
98 void EPGView::addEvent( EPGEvent* event )
100 if ( !m_channels.contains( event->channelName ) )
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 );
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 );
118 scene()->addItem( item );
121 void EPGView::updateEvent( EPGEvent* event )
123 //qDebug() << "Update event: " << event->name;
126 void EPGView::delEvent( EPGEvent* event )
128 //qDebug() << "Del event: " << event->name;
131 void EPGView::drawBackground( QPainter *painter, const QRectF &rect )
133 painter->setPen( QPen( QColor( 72, 72, 72 ) ) );
135 QPointF p = mapToScene( width(), 0 );
138 for ( int i = 0; i < m_channels.count() + 1; ++i )
140 painter->drawLine( 0,
148 void EPGView::updateDuration()
151 QList<QGraphicsItem*> list = items();
153 for ( int i = 0; i < list.count(); ++i )
155 EPGItem* item = dynamic_cast<EPGItem*>( list.at( i ) );
156 if ( !item ) continue;
157 QDateTime itemEnd = item->start().addSecs( item->duration() );
159 if ( itemEnd > lastItem )
162 m_duration = m_startTime.secsTo( lastItem );
163 emit durationChanged( m_duration );
166 void EPGView::eventFocused( EPGEvent *ev )
168 emit eventFocusedChanged( ev );
171 void EPGView::sceneRectChanged( const QRectF& rect )
173 m_overlay->setRect( 0, 0, m_overlay->rect().width(), rect.height() );