Commit 3484582d authored by Ludovic Fauvet's avatar Ludovic Fauvet Committed by Jean-Baptiste Kempf

Initial commit for EPG class for EPG Viewing

parent 1dc6a868
/*****************************************************************************
* EPGEvent.h : EPGEvent
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EPGEVENT_H
#define EPGEVENT_H
#include <QString>
#include <QDateTime>
class EPGEvent
{
public:
EPGEvent( const QString& eventName )
: current( false ), updated( true )
{
name = eventName;
}
QDateTime start;
int duration;
QString name;
QString description;
QString shortDescription;
QString channelName;
bool current;
bool updated;
};
#endif // EPGEVENT_H
/*****************************************************************************
* EPGItem.cpp: EPGItem
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <QTransform>
#include <QFont>
#include <QFontMetrics>
#include <QDebug>
#include "EPGItem.hpp"
#include "EPGView.hpp"
EPGItem::EPGItem( EPGView *view )
: m_view( view )
{
m_current = false;
m_boundingRect.setHeight( TRACKS_HEIGHT );
}
QRectF EPGItem::boundingRect() const
{
return m_boundingRect;
}
void EPGItem::paint( QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
// Draw in view's coordinates
painter->setWorldMatrixEnabled( false );
// Draw high-quality items
//painter->setRenderHint( QPainter::Antialiasing );
// Get the transformations required to map the text on the viewport
QTransform viewPortTransform = m_view->viewportTransform();
QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );
painter->setPen( QPen( Qt::black ) );
painter->setBrush( QBrush( Qt::blue ) );
painter->drawRect( mapped );
/* Draw text */
// Setup the font
QFont f = painter->font();
f.setPointSize( 9 );
f.setBold( true );
painter->setFont( f );
// Get the font metrics
QFontMetrics fm = painter->fontMetrics();
// Adjust the drawing rect
mapped.adjust( 2, 2, -2, -2 );
painter->setPen( Qt::white );
painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft, fm.elidedText( m_name, Qt::ElideRight, mapped.width() ) );
f.setBold( false );
f.setItalic( true );
f.setPointSize( 8 );
painter->setFont( f );
QTextOption textoption;
textoption.setWrapMode( QTextOption::WordWrap );
textoption.setAlignment( Qt::AlignTop | Qt::AlignLeft );
painter->drawText( mapped.adjusted( 0, 20, 0, 0 ),
m_shortDescription,
textoption );
}
const QDateTime& EPGItem::start() const
{
return m_start;
}
int EPGItem::duration() const
{
return m_duration;
}
void EPGItem::setChannel( int channelNb )
{
qDebug() << "Channel" << channelNb;
m_channelNb = channelNb;
setPos( pos().x(), m_channelNb * TRACKS_HEIGHT );
}
void EPGItem::setStart( const QDateTime& start )
{
m_start = start;
int x = m_view->startTime().secsTo( start );
setPos( x, pos().y() );
}
void EPGItem::setDuration( int duration )
{
m_duration = duration;
m_boundingRect.setWidth( duration );
}
void EPGItem::setName( const QString& name )
{
m_name = name;
}
void EPGItem::setDescription( const QString& description )
{
m_description = description;
}
void EPGItem::setShortDescription( const QString& shortDescription )
{
m_shortDescription = shortDescription;
}
void EPGItem::setCurrent( bool current )
{
m_current = current;
}
/*****************************************************************************
* EPGItem.h : EPGItem
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EPGITEM_H
#define EPGITEM_H
#include <QObject>
#include <QGraphicsItem>
#include <QPainter>
#include <QString>
#include <QDateTime>
class EPGView;
class EPGItem : public QObject, public QGraphicsItem
{
public:
EPGItem( EPGView *view );
virtual ~EPGItem() { }
virtual QRectF boundingRect() const;
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0 );
const QDateTime& start() const;
int duration() const;
void setChannel( int channelNb );
void setStart( const QDateTime& start );
void setDuration( int duration );
void setName( const QString& name );
void setDescription( const QString& description );
void setShortDescription( const QString& shortDescription );
void setCurrent( bool current );
private:
EPGView *m_view;
QRectF m_boundingRect;
int m_channelNb;
QDateTime m_start;
int m_duration;
QString m_name;
QString m_description;
QString m_shortDescription;
bool m_current;
};
#endif // EPGITEM_H
/*****************************************************************************
* EPGRuler.coo: EPGRuler
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "EPGRuler.hpp"
#include <QPainter>
#include <QFont>
#include <QPaintEvent>
#include <QtDebug>
EPGRuler::EPGRuler( QWidget* parent )
: QWidget( parent )
{
setContentsMargins( 0, 0, 0, 0 );
setMinimumHeight( 30 );
setMaximumHeight( 30 );
m_offset = 0;
}
void EPGRuler::setScale( double scale )
{
m_scale = scale;
update();
}
void EPGRuler::setStartTime( const QDateTime& startTime )
{
m_startTime = startTime;
update();
}
void EPGRuler::setDuration( int duration )
{
m_duration = duration;
update();
}
void EPGRuler::setOffset( int offset )
{
m_offset = offset;
update();
}
void EPGRuler::paintEvent( QPaintEvent *event )
{
Q_UNUSED( event );
QPainter p( this );
int secondsOffset = m_offset / m_scale;
QDateTime localStartTime;
localStartTime = m_startTime.addSecs( secondsOffset );
QDateTime diff( localStartTime );
diff.setTime( QTime( localStartTime.time().hour(), 0, 0, 0 ) );
int secondsToHour = localStartTime.secsTo( diff );
QDateTime current( localStartTime.addSecs( secondsToHour ) );
int spacing = ( m_scale * 60 ) * 60;
int posx = secondsToHour * m_scale;
// Count the number of items to draw
int itemsToDraw = ( width() / spacing ) + 1;
for ( ; itemsToDraw >= 0; --itemsToDraw )
{
p.setFont( QFont( "Verdana", 8 ) );
p.drawLine( posx, 15, posx, 30 );
p.drawText( posx + 1, 12, 50, 15, Qt::AlignLeft, current.toString( "hh'h'" ) );
posx += spacing;
current = current.addSecs( 60 * 60 );
}
}
/*****************************************************************************
* EPGRuler.h: EPGRuler
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EPGRULER_H
#define EPGRULER_H
#include <QWidget>
#include <QDateTime>
class EPGRuler : public QWidget
{
Q_OBJECT
public:
EPGRuler( QWidget* parent = 0 );
virtual ~EPGRuler() { }
public slots:
void setScale( double scale );
void setStartTime( const QDateTime& startTime );
void setDuration( int duration );
void setOffset( int offset );
protected:
virtual void paintEvent( QPaintEvent *event );
private:
qreal m_scale;
int m_duration;
int m_offset;
QDateTime m_startTime;
};
#endif // EPGRULER_H
/*****************************************************************************
* EPGView.cpp: EPGView
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <QMatrix>
#include "EPGView.hpp"
#include "EPGItem.hpp"
#include <QtDebug>
EPGView::EPGView( QWidget *parent ) : QGraphicsView( parent )
{
setContentsMargins( 0, 0, 0, 0 );
setFrameStyle( QFrame::NoFrame );
setAlignment( Qt::AlignLeft | Qt::AlignTop );
m_startTime = QDateTime::currentDateTime();
//tmp
setSceneRect( 0, 0, 20000, 200 );
QGraphicsScene *EPGscene = new QGraphicsScene( this );
setScene( EPGscene );
}
void EPGView::setScale( double scaleFactor )
{
m_scaleFactor = scaleFactor;
QMatrix matrix;
matrix.scale( scaleFactor, 1 );
setMatrix( matrix );
}
void EPGView::setStartTime( const QDateTime& startTime )
{
QList<QGraphicsItem*> itemList = items();
int diff = startTime.secsTo( m_startTime );
for ( int i = 0; i < itemList.count(); ++i )
{
EPGItem* item = static_cast<EPGItem*>( itemList.at( i ) );
item->setStart( item->start().addSecs( diff ) );
}
m_startTime = startTime;
// Our start time has changed
emit startTimeChanged( startTime );
}
const QDateTime& EPGView::startTime()
{
return m_startTime;
}
void EPGView::addEvent( EPGEvent* event )
{
if ( !m_channels.contains( event->channelName ) )
m_channels.append( event->channelName );
EPGItem* item = new EPGItem( this );
item->setChannel( m_channels.indexOf( event->channelName ) );
item->setStart( event->start );
item->setDuration( event->duration );
item->setName( event->name );
item->setDescription( event->description );
item->setShortDescription( event->shortDescription );
item->setCurrent( event->current );
scene()->addItem( item );
}
void EPGView::updateEvent( EPGEvent* event )
{
qDebug() << "Update event: " << event->name;
}
void EPGView::delEvent( EPGEvent* event )
{
qDebug() << "Del event: " << event->name;
}
void EPGView::drawBackground( QPainter *painter, const QRectF &rect )
{
painter->setPen( QPen( QColor( 72, 72, 72 ) ) );
QPointF p = mapToScene( width(), 0 );
int y = 0;
for ( int i = 0; i < m_channels.count() + 1; ++i )
{
painter->drawLine( 0,
y * TRACKS_HEIGHT,
p.x(),
y * TRACKS_HEIGHT );
++y;
}
}
void EPGView::updateDuration()
{
QDateTime lastItem;
QList<QGraphicsItem*> list = items();
for ( int i = 0; i < list.count(); ++i )
{
EPGItem* item = static_cast<EPGItem*>( list.at( i ) );
QDateTime itemEnd = item->start().addSecs( item->duration() );
if ( itemEnd > lastItem )
lastItem = itemEnd;
}
m_duration = m_startTime.secsTo( lastItem );
emit durationChanged( m_duration );
}
/*****************************************************************************
* EPGView.h : EPGView
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EPGVIEW_H
#define EPGVIEW_H
#include <QGraphicsView>
#include <QVBoxLayout>
#include <QList>
#include "EPGEvent.hpp"
#define TRACKS_HEIGHT 75
class EPGView : public QGraphicsView
{
Q_OBJECT
public:
explicit EPGView( QWidget *parent = 0 );
void setScale( double scaleFactor );
void setStartTime( const QDateTime& startTime );
const QDateTime& startTime();
void addEvent( EPGEvent* event );
void updateEvent( EPGEvent* event );
void delEvent( EPGEvent* event );
void updateDuration();
signals:
void startTimeChanged( const QDateTime& startTime );
void durationChanged( int seconds );
protected:
virtual void drawBackground( QPainter *painter, const QRectF &rect );
QList<QString> m_channels;
QDateTime m_startTime;
int m_scaleFactor;
int m_duration;
};
#endif // EPGVIEW_H
/*****************************************************************************
* EPGWidget.h : EPGWidget
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <QGridLayout>
#include <QScrollBar>
#include <QDebug>
#include "EPGWidget.hpp"
ChannelsWidget::ChannelsWidget( QWidget *parent ) : QWidget( parent )
{
setContentsMargins( 0, 0, 0, 0 );
setMaximumWidth( 50 );
}
EPGWidget::EPGWidget( QWidget *parent ) : QWidget( parent )
{
QGridLayout* layout = new QGridLayout( this );
m_rulerWidget = new EPGRuler( this );
m_channelsWidget = new ChannelsWidget( this );
m_epgView = new EPGView( this );
m_description = new QLabel( "<b>Hello world</b><br/>blablabla" );
m_channelsWidget->setMinimumWidth( 40 );
m_description->setAlignment( Qt::AlignTop | Qt::AlignLeft );
m_description->setMinimumHeight( 70 );
m_epgView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
setZoom( 1 );
layout->addWidget( m_rulerWidget, 0, 1 );
layout->addWidget( m_channelsWidget, 1, 0 );
layout->addWidget( m_epgView, 1, 1 );
layout->addWidget( m_description, 2, 1 );
layout->setSpacing( 0 );
setLayout( layout );
connect( m_epgView, SIGNAL( startTimeChanged(QDateTime) ),
m_rulerWidget, SLOT( setStartTime(QDateTime) ) );
connect( m_epgView, SIGNAL( durationChanged(int) ),
m_rulerWidget, SLOT( setDuration(int) ) );
connect( m_epgView->horizontalScrollBar(), SIGNAL( valueChanged(int) ),
m_rulerWidget, SLOT( setOffset(int) ) );
}
void EPGWidget::setZoom( int level )
{
double scale = (double)level / 20;
m_epgView->setScale( scale );
m_rulerWidget->setScale( scale );
}
void EPGWidget::updateEPG( vlc_epg_t **pp_epg, int i_epg )
{
m_epgView->setStartTime( QDateTime::currentDateTime() );
for ( int i = 0; i < i_epg; ++i )
{
vlc_epg_t *p_epg = pp_epg[i];
QString channelName = QString( p_epg->psz_name );
for ( int j = 0; j < p_epg->i_event; ++j )
{
EPGEvent *item = NULL;
vlc_epg_event_t *p_event = p_epg->pp_event[j];
QString eventName = QString( p_event->psz_name );
QList<EPGEvent*> events = m_events.values( channelName );
for ( int k = 0; k < events.count(); ++k )
{
if ( events.at( k )->name == eventName &&
events.at( k )->channelName == channelName )
{
item = events.at( k );
item->updated = true;
item->description = QString( p_event->psz_description );
item->shortDescription = QString( p_event->psz_short_description );
item->start = QDateTime::fromTime_t( p_event->i_start );
item->duration = p_event->i_duration;
item->current = ( p_epg->p_current == p_event ) ? true : false;
if ( item->start < m_epgView->startTime() )
m_epgView->setStartTime( item->start );
m_epgView->updateEvent( item );
break;
}
}
if ( !item )
{
item = new EPGEvent( eventName );
item->description = QString( p_event->psz_description );
item->shortDescription = QString( p_event->psz_short_description );
item->start = QDateTime::fromTime_t( p_event->i_start );
item->duration = p_event->i_duration;
item->channelName = channelName;
item->current = ( p_epg->p_current == p_event ) ? true : false;
m_events.insert( channelName, item );
if ( item->start < m_epgView->startTime() )
m_epgView->setStartTime( item->start );
m_epgView->addEvent( item );
}
}
}
// Remove old items
QMap<QString, EPGEvent*>::iterator i = m_events.begin();
while ( i != m_events.end() )
{
EPGEvent* item = i.value();
if ( !item->updated )
{
m_epgView->delEvent( item );
delete item;
i = m_events.erase( i );
}
else
item->updated = false;
++i;
}
// Update the global duration
m_epgView->updateDuration();
}
/*****************************************************************************
* EPGWidget.h : EPGWidget
****************************************************************************
* Copyright © 2009-2010 VideoLAN
* $Id$
*
* Authors: Ludovic Fauvet <etix@l0cal.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EPGWIDGET_H
#define EPGWIDGET_H
#include <QWidget>
#include <QMap>
#include <QMultiMap>
#include <QDateTime>
#include <QLabel>
#include "EPGView.hpp"
#include "EPGEvent.hpp"
#include "EPGRuler.hpp"
#include <vlc_common.h>
#include <vlc_epg.h>
class ChannelsWidget : public QWidget
{
Q_OBJECT
public:
explicit ChannelsWidget( QWidget* parent = 0 );
};
class EPGWidget : public QWidget
{
Q_OBJECT
public:
explicit EPGWidget( QWidget* parent = 0 );
public slots:
void setZoom( int level );
void updateEPG( vlc_epg_t **pp_epg, int i_epg );
private:
ChannelsWidget* m_channelsWidget;
EPGRuler* m_rulerWidget;
EPGView* m_epgView;
QLabel* m_description;
QMultiMap<QString, EPGEvent*> m_events;
};
#endif // EPGWIDGET_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment