]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGItem.cpp
vlc_epg: add parental rating from ts streams.
[vlc] / modules / gui / qt4 / components / epg / EPGItem.cpp
1 /*****************************************************************************
2  * EPGItem.cpp: EPGItem
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 <QTransform>
25 #include <QFont>
26 #include <QFontMetrics>
27 #include <QDebug>
28 #include <QDateTime>
29 #include <QFocusEvent>
30 #include <QGraphicsScene>
31 #include <QStyleOptionGraphicsItem>
32 #include <QGraphicsSceneHoverEvent>
33 #include <QStyle>
34
35 #include "EPGItem.hpp"
36 #include "EPGView.hpp"
37
38 #include "qt4.hpp"
39
40 EPGItem::EPGItem( vlc_epg_event_t *data, EPGView *view )
41     : m_view( view )
42 {
43     setData( data );
44     m_current = false;
45     m_boundingRect.setHeight( TRACKS_HEIGHT );
46     setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
47     setAcceptHoverEvents( true );
48 }
49
50 QRectF EPGItem::boundingRect() const
51 {
52     return m_boundingRect;
53 }
54
55 void EPGItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget*)
56 {
57     QPen pen;
58     QColor gradientColor;
59
60     // Draw in view's coordinates
61     painter->setWorldMatrixEnabled( false );
62
63     // Draw high-quality items
64     //painter->setRenderHint( QPainter::Antialiasing );
65
66     // Get the transformations required to map the text on the viewport
67     QTransform viewPortTransform = m_view->viewportTransform();
68     QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );
69
70     QLinearGradient gradient( mapped.topLeft(), mapped.bottomLeft() );
71
72     bool b_simultaneous = playsAt( m_view->baseTime() );
73     if ( m_current || b_simultaneous )
74         gradientColor.setRgb( 244, 125, 0 , b_simultaneous ? 192 : 255 );
75     else
76         gradientColor.setRgb( 201, 217, 242 );
77
78     gradient.setColorAt( 0.0, gradientColor.lighter( 120 ) );
79     gradient.setColorAt( 1.0, gradientColor );
80
81     pen.setColor( option->state & QStyle::State_MouseOver || hasFocus()
82                   ? QColor( 0, 0, 0 ) : QColor( 192, 192, 192 ) );
83
84     pen.setStyle( option->state & QStyle::State_MouseOver && !hasFocus()
85                   ? Qt::DashLine : Qt::SolidLine );
86
87     painter->setBrush( QBrush( gradient ) );
88     painter->setPen( pen );
89     mapped.adjust( 1, 2, -1, -2 );
90     painter->drawRoundedRect( mapped, 10, 10 );
91
92     /* Draw text */
93
94     // Setup the font
95     QFont f = painter->font();
96
97     // Get the font metrics
98     QFontMetrics fm = painter->fontMetrics();
99
100     // Adjust the drawing rect
101     mapped.adjust( 6, 6, -6, -6 );
102
103     painter->setPen( Qt::black );
104     /* Draw the title. */
105     painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft, fm.elidedText( m_name, Qt::ElideRight, mapped.width() ) );
106
107     mapped.adjust( 0, 20, 0, 0 );
108
109     QDateTime m_end = m_start.addSecs( m_duration );
110     f.setPixelSize( 10 );
111     f.setItalic( true );
112     painter->setFont( f );
113
114     /* Draw the hours. */
115     painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft,
116                        fm.elidedText( start().toString( "hh:mm" ) + " - " +
117                                       m_end.toString( "hh:mm" ),
118                                       Qt::ElideRight, mapped.width() ) );
119 }
120
121 const QDateTime& EPGItem::start() const
122 {
123     return m_start;
124 }
125
126 QDateTime EPGItem::end()
127 {
128     return QDateTime( m_start ).addSecs( m_duration );
129 }
130
131 int EPGItem::duration() const
132 {
133     return m_duration;
134 }
135
136 void EPGItem::setRow( unsigned int i_row_ )
137 {
138     i_row = i_row_;
139     updatePos();
140 }
141
142 bool EPGItem::setData( vlc_epg_event_t *data )
143 {
144     QDateTime newtime = QDateTime::fromTime_t( data->i_start );
145     QString newname = qfu( data->psz_name );
146     QString newdesc = qfu( data->psz_description );
147     QString newshortdesc = qfu( data->psz_short_description );
148
149     if ( m_start != newtime ||
150          m_name != newname ||
151          m_description != newdesc ||
152          m_shortDescription != newshortdesc ||
153          m_duration != data->i_duration )
154     {
155         m_start = newtime;
156         m_name = newname;
157         setToolTip( newname );
158         m_description = newdesc;
159         m_shortDescription = newshortdesc;
160         setDuration( data->i_duration );
161         update();
162         return true;
163     }
164     return false;
165 }
166
167 void EPGItem::setCurrent( bool b_current )
168 {
169     m_current = b_current;
170 }
171
172 bool EPGItem::endsBefore( const QDateTime &ref ) const
173 {
174     return m_start.addSecs( m_duration ) < ref;
175 }
176
177 bool EPGItem::playsAt( const QDateTime & ref ) const
178 {
179     return (m_start <= ref) && !endsBefore( ref );
180 }
181
182 void EPGItem::setDuration( int duration )
183 {
184     m_duration = duration;
185     m_boundingRect.setWidth( duration );
186 }
187
188 QString EPGItem::description()
189 {
190     if( m_description.isEmpty() )
191         return m_shortDescription;
192
193     QString text( m_description );
194     if( !m_shortDescription.isEmpty() )
195         text += QString(" - ") += m_shortDescription;
196     return text;
197 }
198
199 void EPGItem::updatePos()
200 {
201     int x = m_view->startTime().secsTo( m_start );
202     setPos( x, i_row * TRACKS_HEIGHT );
203 }
204
205 void EPGItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
206 {
207     event->accept();
208     scene()->update();
209 }
210
211 void EPGItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
212 { /* required to redraw our background without flaws */
213     hoverEnterEvent( event );
214 }
215
216 void EPGItem::focusInEvent( QFocusEvent * event )
217 {
218     event->accept();
219     m_view->focusItem( this );
220     update();
221 }