timetooltip.cpp 5.18 KB
Newer Older
1
/*****************************************************************************
2
 * Copyright © 2011-2012 VideoLAN
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * $Id$
 *
 * Authors: Ludovic Fauvet <etix@l0cal.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "timetooltip.hpp"

#include <QApplication>
#include <QPainter>
#include <QPainterPath>
#include <QBitmap>
#include <QFontMetrics>
29
#include <QDesktopWidget>
30
31
32
33

#define TIP_HEIGHT 5

TimeTooltip::TimeTooltip( QWidget *parent ) :
34
    QWidget( parent ), mInitialized( false )
35
{
36
37
38
39
    setWindowFlags( Qt::Window                  |
                    Qt::WindowStaysOnTopHint    |
                    Qt::FramelessWindowHint     |
                    Qt::X11BypassWindowManagerHint );
40
41
42
43

    // Tell Qt that it doesn't need to erase the background before
    // a paintEvent occurs. This should save some CPU cycles.
    setAttribute( Qt::WA_OpaquePaintEvent );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
44

45
#if defined( Q_OS_WIN ) || defined( Q_OS_OS2 )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
46
    /*
47
    - This attribute is required on Windows and OS/2 to avoid focus stealing of other windows.
Ludovic Fauvet's avatar
Ludovic Fauvet committed
48
49
    - When set on Linux the TimeTooltip appears behind the FSController in fullscreen.
    */
50
    setAttribute( Qt::WA_ShowWithoutActivating );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
51
#endif
52
53
54

    // Inherit from the system default font size -5
    mFont = QFont( "Verdana", qMax( qApp->font().pointSize() - 5, 7 ) );
55
    mTipX = -1;
56
57
}

58
void TimeTooltip::adjustPosition()
59
{
60
    // Get the bounding box required to print the text and add some padding
61
    QFontMetrics metrics( mFont );
62
63
64
    QRect textbox = metrics.boundingRect( mDisplayedText );
    textbox.adjust( -2, -2, 2, 2 );
    textbox.moveTo( 0, 0 );
65

66
67
    // Resize the widget to fit our needs
    QSize size( textbox.width() + 1, textbox.height() + TIP_HEIGHT + 1 );
68

69
70
71
    // The desired label position is just above the target
    QPoint position( mTarget.x() - size.width() / 2,
        mTarget.y() - size.height() + TIP_HEIGHT / 2 );
72

73
74
75
76
77
78
    // Keep the tooltip on the same screen if possible
    QRect screen = QApplication::desktop()->screenGeometry( mTarget );
    position.setX( qMax( screen.left(), qMin( position.x(),
        screen.left() + screen.width() - size.width() ) ) );
    position.setY( qMax( screen.top(), qMin( position.y(),
        screen.top() + screen.height() - size.height() ) ) );
79

80
81
82
83
84
85
86
87
88
89
90
91
92
    move( position );

    int tipX = mTarget.x() - position.x();
    if( mBox != textbox || mTipX != tipX )
    {
        mBox = textbox;
        mTipX = tipX;

        resize( size );
        buildPath();
        setMask( mMask );
    }
}
93

94
95
void TimeTooltip::buildPath()
{
96
97
98
99
    // Prepare the painter path for future use so
    // we only have to generate the text at runtime.

    // Draw the text box
100
    mPainterPath = QPainterPath();
101
102
103
104
    mPainterPath.addRect( mBox );

    // Draw the tip
    QPolygon polygon;
105
106
107
    polygon << QPoint( qMax( 0, mTipX - 3 ), mBox.height() )
            << QPoint( mTipX, mBox.height() + TIP_HEIGHT )
            << QPoint( qMin( mTipX + 3, mBox.width() ), mBox.height() );
108
109
110
111
112
113
114
115
116
117
    mPainterPath.addPolygon( polygon );

    // Store the simplified version of the path
    mPainterPath = mPainterPath.simplified();

    // Create the mask used to erase the background
    // Note: this is a binary bitmap (black & white)
    mMask = QBitmap( size() );
    QPainter painter( &mMask );
    painter.fillRect( mMask.rect(), Qt::white );
118
119
    painter.setPen( Qt::black );
    painter.setBrush( Qt::black );
120
121
122
123
    painter.drawPath( mPainterPath );
    painter.end();
}

124
void TimeTooltip::setTip( const QPoint& target, const QString& time, const QString& text )
125
{
126
    mInitialized = true;
127
    mDisplayedText = time;
tobias's avatar
tobias committed
128
129
    if ( !text.isEmpty() )
        mDisplayedText.append( " - " ).append( text );
130

131
132
133
134
135
136
137
    if( mTarget != target || time.length() != mTime.length() || mText != text )
    {
        mTarget = target;
        mTime = time;
        mText = text;
        adjustPosition();
    }
138

139
140
141
    update();
}

142
143
144
void TimeTooltip::show()
{
    QWidget::setVisible( mInitialized );
145
#ifdef Q_OS_OS2
146
147
148
149
150
151
    // Bring a tooltip on the top
    // Without this, tooltip does not appear on fullscreen
    // from the second fullscreen state change
    if( mInitialized )
        QWidget::raise();
#endif
152
153
}

154
155
156
157
158
159
160
161
162
163
164
void TimeTooltip::paintEvent( QPaintEvent * )
{
    QPainter p( this );
    p.setRenderHints( QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing );

    p.setPen( Qt::black );
    p.setBrush( qApp->palette().base() );
    p.drawPath( mPainterPath );

    p.setFont( mFont );
    p.setPen( QPen( qApp->palette().text(), 1 ) );
165
    p.drawText( mBox, Qt::AlignCenter, mDisplayedText );
166
}