1 /*****************************************************************************
2 * Copyright © 2011 VideoLAN
5 * Authors: Ludovic Fauvet <etix@l0cal.com>
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.
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.
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 *****************************************************************************/
22 #include "timetooltip.hpp"
24 #include <QApplication>
26 #include <QPainterPath>
28 #include <QFontMetrics>
32 TimeTooltip::TimeTooltip( QWidget *parent ) :
35 setWindowFlags( Qt::ToolTip );
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 );
41 // Inherit from the system default font size -5
42 mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) );
44 QFontMetrics metrics( mFont );
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() );
50 // Resize the widget to fit our needs
51 resize( mBox.width() + 1, mBox.height() + TIP_HEIGHT + 1 );
53 // Prepare the painter path for future use so
54 // we only have to generate the text at runtime.
57 mPainterPath.addRect( mBox );
60 int center = mBox.width() / 2;
62 polygon << QPoint( center - 3, mBox.height() )
63 << QPoint( center, mBox.height() + TIP_HEIGHT )
64 << QPoint( center + 3, mBox.height() );
66 mPainterPath.addPolygon( polygon );
68 // Store the simplified version of the path
69 mPainterPath = mPainterPath.simplified();
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 );
87 void TimeTooltip::setTime( const QString& time )
93 void TimeTooltip::paintEvent( QPaintEvent * )
96 p.setRenderHints( QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing );
98 p.setPen( Qt::black );
99 p.setBrush( qApp->palette().base() );
100 p.drawPath( mPainterPath );
103 p.setPen( QPen( qApp->palette().text(), 1 ) );
104 p.drawText( mBox, Qt::AlignCenter, mTime );