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 *****************************************************************************/
26 #include <QFontMetrics>
29 #include <QFocusEvent>
30 #include <QGraphicsScene>
31 #include <QStyleOptionGraphicsItem>
32 #include <QGraphicsSceneHoverEvent>
35 #include "EPGItem.hpp"
36 #include "EPGView.hpp"
40 EPGItem::EPGItem( vlc_epg_event_t *data, EPGView *view )
45 m_boundingRect.setHeight( TRACKS_HEIGHT );
46 setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
47 setAcceptHoverEvents( true );
50 QRectF EPGItem::boundingRect() const
52 return m_boundingRect;
55 void EPGItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget*)
60 // Draw in view's coordinates
61 painter->setWorldMatrixEnabled( false );
63 // Draw high-quality items
64 //painter->setRenderHint( QPainter::Antialiasing );
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() );
70 QLinearGradient gradient( mapped.topLeft(), mapped.bottomLeft() );
72 bool b_simultaneous = playsAt( m_view->baseTime() );
73 if ( m_current || b_simultaneous )
74 gradientColor.setRgb( 244, 125, 0 , b_simultaneous ? 192 : 255 );
76 gradientColor.setRgb( 201, 217, 242 );
78 gradient.setColorAt( 0.0, gradientColor.lighter( 120 ) );
79 gradient.setColorAt( 1.0, gradientColor );
81 pen.setColor( option->state & QStyle::State_MouseOver || hasFocus()
82 ? QColor( 0, 0, 0 ) : QColor( 192, 192, 192 ) );
84 pen.setStyle( option->state & QStyle::State_MouseOver && !hasFocus()
85 ? Qt::DashLine : Qt::SolidLine );
87 painter->setBrush( QBrush( gradient ) );
88 painter->setPen( pen );
89 mapped.adjust( 1, 2, -1, -2 );
90 painter->drawRoundedRect( mapped, 10, 10 );
95 QFont f = painter->font();
97 // Get the font metrics
98 QFontMetrics fm = painter->fontMetrics();
100 // Adjust the drawing rect
101 mapped.adjust( 6, 6, -6, -6 );
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() ) );
107 if ( m_rating > 0 && mapped.width() > 40 )
109 QRectF iconsRect = QRectF( mapped.bottomRight(), mapped.bottomRight() );
110 iconsRect.adjust( -20, -20, 0, 0 );
112 painter->setBrush( Qt::white );
114 painter->setFont( f );
115 painter->drawRect( iconsRect );
116 painter->drawText( iconsRect, Qt::AlignCenter, QString("%1+").arg( m_rating ) );
120 mapped.adjust( 0, 20, 0, 0 );
122 QDateTime m_end = m_start.addSecs( m_duration );
123 f.setPixelSize( 10 );
125 painter->setFont( f );
127 /* Draw the hours. */
128 painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft,
129 fm.elidedText( start().toString( "hh:mm" ) + " - " +
130 m_end.toString( "hh:mm" ),
131 Qt::ElideRight, mapped.width() ) );
134 const QDateTime& EPGItem::start() const
139 QDateTime EPGItem::end() const
141 return QDateTime( m_start ).addSecs( m_duration );
144 int EPGItem::duration() const
149 void EPGItem::setRow( unsigned int i_row_ )
155 bool EPGItem::setData( vlc_epg_event_t *data )
157 QDateTime newtime = QDateTime::fromTime_t( data->i_start );
158 QString newname = qfu( data->psz_name );
159 QString newdesc = qfu( data->psz_description );
160 QString newshortdesc = qfu( data->psz_short_description );
162 if ( m_start != newtime ||
164 m_description != newdesc ||
165 m_shortDescription != newshortdesc ||
166 m_duration != data->i_duration )
170 setToolTip( newname );
171 m_description = newdesc;
172 m_shortDescription = newshortdesc;
173 setDuration( data->i_duration );
174 setRating( data->i_rating );
181 void EPGItem::setCurrent( bool b_current )
183 m_current = b_current;
186 bool EPGItem::endsBefore( const QDateTime &ref ) const
188 return m_start.addSecs( m_duration ) < ref;
191 bool EPGItem::playsAt( const QDateTime & ref ) const
193 return (m_start <= ref) && !endsBefore( ref );
196 void EPGItem::setDuration( int duration )
198 m_duration = duration;
199 m_boundingRect.setWidth( duration );
202 void EPGItem::setRating( uint8_t i_rating )
207 QString EPGItem::description() const
209 if( m_description.isEmpty() )
210 return m_shortDescription;
212 QString text( m_description );
213 if( !m_shortDescription.isEmpty() )
214 text += QString(" - ") += m_shortDescription;
218 void EPGItem::updatePos()
220 int x = m_view->startTime().secsTo( m_start );
221 setPos( x, i_row * TRACKS_HEIGHT );
224 void EPGItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
230 void EPGItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
231 { /* required to redraw our background without flaws */
232 hoverEnterEvent( event );
235 void EPGItem::focusInEvent( QFocusEvent * event )
238 m_view->focusItem( this );