]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGRuler.cpp
Initial commit for EPG class for EPG Viewing
[vlc] / modules / gui / qt4 / components / epg / EPGRuler.cpp
1 /*****************************************************************************
2  * EPGRuler.coo: EPGRuler
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 "EPGRuler.hpp"
25
26 #include <QPainter>
27 #include <QFont>
28 #include <QPaintEvent>
29 #include <QtDebug>
30
31 EPGRuler::EPGRuler( QWidget* parent )
32     : QWidget( parent )
33 {
34     setContentsMargins( 0, 0, 0, 0 );
35     setMinimumHeight( 30 );
36     setMaximumHeight( 30 );
37     m_offset = 0;
38 }
39
40 void EPGRuler::setScale( double scale )
41 {
42     m_scale = scale;
43     update();
44 }
45
46 void EPGRuler::setStartTime( const QDateTime& startTime )
47 {
48     m_startTime = startTime;
49     update();
50 }
51
52 void EPGRuler::setDuration( int duration )
53 {
54     m_duration = duration;
55     update();
56 }
57
58 void EPGRuler::setOffset( int offset )
59 {
60     m_offset = offset;
61     update();
62 }
63
64 void EPGRuler::paintEvent( QPaintEvent *event )
65 {
66     Q_UNUSED( event );
67
68     QPainter p( this );
69
70     int secondsOffset = m_offset / m_scale;
71
72     QDateTime localStartTime;
73     localStartTime = m_startTime.addSecs( secondsOffset );
74
75     QDateTime diff( localStartTime );
76     diff.setTime( QTime( localStartTime.time().hour(), 0, 0, 0 ) );
77
78     int secondsToHour = localStartTime.secsTo( diff );
79
80     QDateTime current( localStartTime.addSecs( secondsToHour ) );
81
82     int spacing = ( m_scale * 60 ) * 60;
83     int posx = secondsToHour * m_scale;
84
85     // Count the number of items to draw
86     int itemsToDraw = ( width() / spacing ) + 1;
87
88     for ( ; itemsToDraw >= 0; --itemsToDraw )
89     {
90         p.setFont( QFont( "Verdana", 8 ) );
91         p.drawLine( posx, 15, posx, 30 );
92         p.drawText( posx + 1, 12, 50, 15, Qt::AlignLeft, current.toString( "hh'h'" ) );
93         posx += spacing;
94         current = current.addSecs( 60 * 60 );
95     }
96 }