]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGItem.cpp
f3fbb1863d1a05d48ee6c0a37bba61bf040111c7
[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( QColor( 100, 100, 100 ) ) );
66     else
67         painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
68
69     painter->drawRect( mapped );
70
71
72     /* Draw text */
73
74     // Setup the font
75     QFont f = painter->font();
76
77     // Get the font metrics
78     QFontMetrics fm = painter->fontMetrics();
79
80     // Adjust the drawing rect
81     mapped.adjust( 6, 6, -6, -6 );
82
83     painter->setPen( Qt::white );
84     /* Draw the title. */
85     painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft, fm.elidedText( m_name, Qt::ElideRight, mapped.width() ) );
86
87     mapped.adjust( 0, 20, 0, 0 );
88
89     QDateTime m_end = m_start.addSecs( m_duration );
90     f.setPixelSize( 10 );
91     f.setItalic( true );
92     painter->setFont( f );
93
94     /* Draw the hours. */
95     painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft,
96                        fm.elidedText( m_start.toString( "hh:mm" ) + " - " +
97                                       m_end.toString( "hh:mm" ),
98                                       Qt::ElideRight, mapped.width() ) );
99 }
100
101 const QDateTime& EPGItem::start() const
102 {
103     return m_start;
104 }
105
106 int EPGItem::duration() const
107 {
108     return m_duration;
109 }
110
111 void EPGItem::setChannel( int channelNb )
112 {
113     //qDebug() << "Channel" << channelNb;
114     m_channelNb = channelNb;
115     setPos( pos().x(), m_channelNb * TRACKS_HEIGHT );
116 }
117
118 void EPGItem::setStart( const QDateTime& start )
119 {
120     m_start = start;
121     int x = m_view->startTime().secsTo( start );
122     setPos( x, pos().y() );
123 }
124
125 void EPGItem::setDuration( int duration )
126 {
127     m_duration = duration;
128     m_boundingRect.setWidth( duration );
129 }
130
131 void EPGItem::setName( const QString& name )
132 {
133     m_name = name;
134 }
135
136 void EPGItem::setDescription( const QString& description )
137 {
138     m_description = description;
139 }
140
141 void EPGItem::setShortDescription( const QString& shortDescription )
142 {
143     m_shortDescription = shortDescription;
144 }
145
146 void EPGItem::setCurrent( bool current )
147 {
148     m_current = current;
149 }
150
151 void EPGItem::focusInEvent( QFocusEvent * event )
152 {
153     EPGEvent *evEPG = new EPGEvent( m_name );
154     evEPG->description = m_description;
155     evEPG->shortDescription = m_shortDescription;
156     evEPG->start = m_start;
157     evEPG->duration = m_duration;
158     m_view->eventFocused( evEPG );
159 }