]> git.sesse.net Git - vlc/blob - modules/gui/qt4/util/timetooltip.cpp
Qt, controller: cleaning
[vlc] / modules / gui / qt4 / util / timetooltip.cpp
1 /*****************************************************************************
2  * Copyright © 2011 VideoLAN
3  * $Id$
4  *
5  * Authors: Ludovic Fauvet <etix@l0cal.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #include "timetooltip.hpp"
23
24 #include <QApplication>
25 #include <QPainter>
26 #include <QPainterPath>
27 #include <QBitmap>
28 #include <QFontMetrics>
29
30 #define TIP_HEIGHT 5
31
32 TimeTooltip::TimeTooltip( QWidget *parent ) :
33     QWidget( parent )
34 {
35     setWindowFlags( Qt::ToolTip );
36
37     // Tell Qt that it doesn't need to erase the background before
38     // a paintEvent occurs. This should save some CPU cycles.
39     setAttribute( Qt::WA_OpaquePaintEvent );
40
41     // Inherit from the system default font size -5
42     mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) );
43
44     QFontMetrics metrics( mFont );
45
46     // Get the bounding box required to print the text and add some padding
47     QRect textbox = metrics.boundingRect( "00:00:00" ).adjusted( -2, -2, 2, 2 );
48     mBox = QRect( 0, 0, textbox.width(), textbox.height() );
49
50     // Resize the widget to fit our needs
51     resize( mBox.width() + 1, mBox.height() + TIP_HEIGHT + 1 );
52
53     // Prepare the painter path for future use so
54     // we only have to generate the text at runtime.
55
56     // Draw the text box
57     mPainterPath.addRect( mBox );
58
59     // Draw the tip
60     int center = mBox.width() / 2;
61     QPolygon polygon;
62     polygon << QPoint( center - 3,   mBox.height() )
63             << QPoint( center,       mBox.height() + TIP_HEIGHT )
64             << QPoint( center + 3,   mBox.height() );
65
66     mPainterPath.addPolygon( polygon );
67
68     // Store the simplified version of the path
69     mPainterPath = mPainterPath.simplified();
70
71     // Create the mask used to erase the background
72     // Note: this is a binary bitmap (black & white)
73     mMask = QBitmap( size() );
74     QPainter painter( &mMask );
75     painter.fillRect( mMask.rect(), Qt::white );
76     painter.setPen( QColor( 0, 0, 0 ) );
77     painter.setBrush( QColor( 0, 0, 0 ) );
78     painter.drawPath( mPainterPath );
79     painter.end();
80
81     setMask( mMask );
82
83     // Set default text
84     setTime("00:00");
85 }
86
87 void TimeTooltip::setTime( const QString& time )
88 {
89     mTime = time;
90     update();
91 }
92
93 void TimeTooltip::paintEvent( QPaintEvent * )
94 {
95     QPainter p( this );
96     p.setRenderHints( QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing );
97
98     p.setPen( Qt::black );
99     p.setBrush( qApp->palette().base() );
100     p.drawPath( mPainterPath );
101
102     p.setFont( mFont );
103     p.setPen( QPen( qApp->palette().text(), 1 ) );
104     p.drawText( mBox, Qt::AlignCenter, mTime );
105 }
106
107 #undef TIP_HEIGHT