]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGItem.cpp
epg: highlight ongoing programs in the epg view
[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
32 #include "EPGItem.hpp"
33 #include "EPGView.hpp"
34 #include "EPGEvent.hpp"
35
36 EPGItem::EPGItem( EPGView *view )
37     : m_view( view )
38 {
39     m_current = false;
40
41     m_boundingRect.setHeight( TRACKS_HEIGHT );
42     setFlags( QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
43 }
44
45 QRectF EPGItem::boundingRect() const
46 {
47     return m_boundingRect;
48 }
49
50 void EPGItem::paint( QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
51 {
52     // Draw in view's coordinates
53     painter->setWorldMatrixEnabled( false );
54
55     // Draw high-quality items
56     //painter->setRenderHint( QPainter::Antialiasing );
57
58     // Get the transformations required to map the text on the viewport
59     QTransform viewPortTransform = m_view->viewportTransform();
60     QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );
61
62     painter->setPen( QPen( Qt::black ) );
63
64     if ( m_current )
65         painter->setBrush( QBrush( Qt::red ) );
66     else
67         painter->setBrush( QBrush( Qt::blue ) );
68
69     painter->drawRect( mapped );
70
71
72     /* Draw text */
73
74     // Setup the font
75     QFont f = painter->font();
76     f.setPointSize( 9 );
77     f.setBold( true );
78     painter->setFont( f );
79
80     // Get the font metrics
81     QFontMetrics fm = painter->fontMetrics();
82
83     // Adjust the drawing rect
84     mapped.adjust( 2, 2, -2, -2 );
85
86     painter->setPen( Qt::white );
87     painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft, fm.elidedText( m_name, Qt::ElideRight, mapped.width() ) );
88
89
90     f.setBold( false );
91     f.setItalic( true );
92     f.setPointSize( 8 );
93     painter->setFont( f );
94
95     QTextOption textoption;
96     textoption.setWrapMode( QTextOption::WordWrap );
97     textoption.setAlignment( Qt::AlignTop | Qt::AlignLeft );
98
99     painter->drawText( mapped.adjusted( 0, 20, 0, 0 ),
100                        m_shortDescription,
101                        textoption );
102 }
103
104 const QDateTime& EPGItem::start() const
105 {
106     return m_start;
107 }
108
109 int EPGItem::duration() const
110 {
111     return m_duration;
112 }
113
114 void EPGItem::setChannel( int channelNb )
115 {
116     //qDebug() << "Channel" << channelNb;
117     m_channelNb = channelNb;
118     setPos( pos().x(), m_channelNb * TRACKS_HEIGHT );
119 }
120
121 void EPGItem::setStart( const QDateTime& start )
122 {
123     m_start = start;
124     int x = m_view->startTime().secsTo( start );
125     setPos( x, pos().y() );
126 }
127
128 void EPGItem::setDuration( int duration )
129 {
130     m_duration = duration;
131     m_boundingRect.setWidth( duration );
132 }
133
134 void EPGItem::setName( const QString& name )
135 {
136     m_name = name;
137 }
138
139 void EPGItem::setDescription( const QString& description )
140 {
141     m_description = description;
142 }
143
144 void EPGItem::setShortDescription( const QString& shortDescription )
145 {
146     m_shortDescription = shortDescription;
147 }
148
149 void EPGItem::setCurrent( bool current )
150 {
151     m_current = current;
152 }
153
154 void EPGItem::focusInEvent( QFocusEvent * event )
155 {
156     EPGEvent *evEPG = new EPGEvent( m_name );
157     evEPG->description = m_description;
158     evEPG->shortDescription = m_shortDescription;
159     m_view->eventFocused( evEPG );
160 }