]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/epg/EPGRuler.cpp
Qt: EPG: thicker ruler timeline
[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 #include <QDateTime>
31
32 EPGRuler::EPGRuler( QWidget* parent )
33     : QWidget( parent )
34 {
35     setContentsMargins( 0, 0, 0, 0 );
36     setMinimumHeight( 30 );
37     setMaximumHeight( 30 );
38     m_offset = 0;
39 }
40
41 void EPGRuler::setScale( double scale )
42 {
43     m_scale = scale;
44     update();
45 }
46
47 void EPGRuler::setStartTime( const QDateTime& startTime )
48 {
49     m_startTime = startTime;
50     update();
51 }
52
53 void EPGRuler::setDuration( int duration )
54 {
55     m_duration = duration;
56     update();
57 }
58
59 void EPGRuler::setOffset( int offset )
60 {
61     m_offset = offset;
62     update();
63 }
64
65 void EPGRuler::paintEvent( QPaintEvent *event )
66 {
67     Q_UNUSED( event );
68
69     QPainter p( this );
70
71     int secondsOffset = m_offset / m_scale;
72
73     QDateTime localStartTime;
74     localStartTime = m_startTime.addSecs( secondsOffset );
75
76     QDateTime diff( localStartTime );
77     diff.setTime( QTime( localStartTime.time().hour(), 0, 0, 0 ) );
78
79     int secondsToHour = localStartTime.secsTo( diff );
80
81     QDateTime current( localStartTime.addSecs( secondsToHour ) );
82
83     int spacing = ( m_scale * 60 ) * 60;
84     int posx = secondsToHour * m_scale;
85
86     // Count the number of items to draw
87     int itemsToDraw = ( width() / spacing ) + 1;
88
89     for ( ; itemsToDraw >= 0; --itemsToDraw )
90     {
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
97     /* draw current time line */
98     posx = localStartTime.secsTo( QDateTime::currentDateTime() ) * m_scale;
99     if ( posx <= width() && posx >= 0 )
100     {
101         QPen pen( QPen( QColor( 255, 0 , 0, 128 ) ) );
102         pen.setWidth( 3 );
103         p.setPen( pen );
104         p.drawLine( posx - 1, 15, posx - 1, 30 );
105     }
106 }