]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGItem.cpp
Qt: EPGItem: fix constness
[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     if ( m_rating > 0 && mapped.width() > 40 )
108     {
109         QRectF iconsRect = QRectF( mapped.bottomRight(), mapped.bottomRight() );
110         iconsRect.adjust( -20, -20, 0, 0 );
111         painter->save();
112         painter->setBrush( Qt::white );
113         f.setPixelSize( 8 );
114         painter->setFont( f );
115         painter->drawRect( iconsRect );
116         painter->drawText( iconsRect, Qt::AlignCenter, QString("%1+").arg( m_rating ) );
117         painter->restore();
118     }
119
120     mapped.adjust( 0, 20, 0, 0 );
121
122     QDateTime m_end = m_start.addSecs( m_duration );
123     f.setPixelSize( 10 );
124     f.setItalic( true );
125     painter->setFont( f );
126
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() ) );
132 }
133
134 const QDateTime& EPGItem::start() const
135 {
136     return m_start;
137 }
138
139 QDateTime EPGItem::end() const
140 {
141     return QDateTime( m_start ).addSecs( m_duration );
142 }
143
144 int EPGItem::duration() const
145 {
146     return m_duration;
147 }
148
149 void EPGItem::setRow( unsigned int i_row_ )
150 {
151     i_row = i_row_;
152     updatePos();
153 }
154
155 bool EPGItem::setData( vlc_epg_event_t *data )
156 {
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 );
161
162     if ( m_start != newtime ||
163          m_name != newname ||
164          m_description != newdesc ||
165          m_shortDescription != newshortdesc ||
166          m_duration != data->i_duration )
167     {
168         m_start = newtime;
169         m_name = newname;
170         setToolTip( newname );
171         m_description = newdesc;
172         m_shortDescription = newshortdesc;
173         setDuration( data->i_duration );
174         setRating( data->i_rating );
175         update();
176         return true;
177     }
178     return false;
179 }
180
181 void EPGItem::setCurrent( bool b_current )
182 {
183     m_current = b_current;
184 }
185
186 bool EPGItem::endsBefore( const QDateTime &ref ) const
187 {
188     return m_start.addSecs( m_duration ) < ref;
189 }
190
191 bool EPGItem::playsAt( const QDateTime & ref ) const
192 {
193     return (m_start <= ref) && !endsBefore( ref );
194 }
195
196 void EPGItem::setDuration( int duration )
197 {
198     m_duration = duration;
199     m_boundingRect.setWidth( duration );
200 }
201
202 void EPGItem::setRating( uint8_t i_rating )
203 {
204     m_rating = i_rating;
205 }
206
207 QString EPGItem::description() const
208 {
209     if( m_description.isEmpty() )
210         return m_shortDescription;
211
212     QString text( m_description );
213     if( !m_shortDescription.isEmpty() )
214         text += QString(" - ") += m_shortDescription;
215     return text;
216 }
217
218 void EPGItem::updatePos()
219 {
220     int x = m_view->startTime().secsTo( m_start );
221     setPos( x, i_row * TRACKS_HEIGHT );
222 }
223
224 void EPGItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
225 {
226     event->accept();
227     scene()->update();
228 }
229
230 void EPGItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
231 { /* required to redraw our background without flaws */
232     hoverEnterEvent( event );
233 }
234
235 void EPGItem::focusInEvent( QFocusEvent * event )
236 {
237     event->accept();
238     m_view->focusItem( this );
239     update();
240 }