~ubuntu-branches/ubuntu/wily/oxygen/wily

« back to all changes in this revision

Viewing changes to kdecoration/oxygentitleanimationdata.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Scarlett Clark, Jonathan Riddell
  • Date: 2015-08-10 23:18:51 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20150810231851-wtw33zvkigya4f7t
Tags: 4:5.3.95-0ubuntu1
[ Scarlett Clark ]
* Vivid backport. 

[ Jonathan Riddell ]
* new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//////////////////////////////////////////////////////////////////////////////
2
 
// oxygentitleanimationdata.h
3
 
// handles transition when window title is changed
4
 
// -------------------
5
 
//
6
 
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
7
 
// Copyright (c) 2015  David Edmundson <davidedmundson@kde.org>
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to
11
 
// deal in the Software without restriction, including without limitation the
12
 
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13
 
// sell copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
//
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
//
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
 
// IN THE SOFTWARE.
26
 
//////////////////////////////////////////////////////////////////////////////
27
 
 
28
 
#include "oxygentitleanimationdata.h"
29
 
#include "oxygentitleanimationdata.moc"
30
 
 
31
 
#include <QPainter>
32
 
 
33
 
namespace Oxygen
34
 
{
35
 
 
36
 
    // use 300 milliseconds for animation lock
37
 
    const int TitleAnimationData::_lockTime = 300;
38
 
 
39
 
    //_________________________________________________________
40
 
    TitleAnimationData::TitleAnimationData( QObject* parent ):
41
 
        QObject( parent ),
42
 
        _dirty( false ),
43
 
        _animation( new Animation( 200, this ) ),
44
 
        _opacity(0)
45
 
    {}
46
 
 
47
 
    //_________________________________________________________
48
 
    void TitleAnimationData::initialize( void )
49
 
    {
50
 
 
51
 
        // setup title animation
52
 
        animation().data()->setStartValue( 0 );
53
 
        animation().data()->setEndValue( 1 );
54
 
        animation().data()->setTargetObject( this );
55
 
        animation().data()->setPropertyName( "opacity" );
56
 
        animation().data()->setEasingCurve( QEasingCurve::InOutQuad );
57
 
 
58
 
    }
59
 
 
60
 
 
61
 
    //_________________________________________________________
62
 
    void TitleAnimationData::setPixmaps( QRect rect, QPixmap pixmap, QPixmap contrast )
63
 
    {
64
 
 
65
 
        // stop animation
66
 
        if( isAnimated() ) animation().data()->stop();
67
 
 
68
 
        // update pixmaps
69
 
        _contrastPixmap.initialize( rect, contrast );
70
 
        _pixmap.initialize( rect, pixmap );
71
 
 
72
 
        setOpacity(0);
73
 
        updatePixmaps();
74
 
 
75
 
    }
76
 
 
77
 
    //_________________________________________________________
78
 
    void TitleAnimationData::updatePixmaps( void )
79
 
    {
80
 
        _contrastPixmap.blend( opacity() );
81
 
        _pixmap.blend( opacity() );
82
 
        emit pixmapsChanged();
83
 
    }
84
 
 
85
 
    //_________________________________________________________
86
 
    void TitleAnimationData::timerEvent( QTimerEvent* e )
87
 
    {
88
 
 
89
 
        if( e->timerId() != _animationLockTimer.timerId() )
90
 
        { return QObject::timerEvent( e ); }
91
 
 
92
 
        // unlock
93
 
        unlockAnimations();
94
 
 
95
 
        if( !isAnimated() )
96
 
        {
97
 
            // triggers pixmap updates
98
 
            reset();
99
 
            emit pixmapsChanged();
100
 
        }
101
 
 
102
 
    }
103
 
 
104
 
    //_________________________________________________________
105
 
    void TitleAnimationData::BlendedPixmap::blend( qreal opacity )
106
 
    {
107
 
 
108
 
        _currentPixmap = QPixmap( _endRect.size() );
109
 
        _currentPixmap.fill( Qt::transparent );
110
 
 
111
 
        QPainter painter( &_currentPixmap );
112
 
        if( opacity < 1 && !_startPixmap.isNull() )
113
 
        { painter.drawPixmap( _startRect.topLeft() - _endRect.topLeft(), fade( _startPixmap, 1.0 - opacity ) ); }
114
 
 
115
 
        if( opacity > 0 && !_endPixmap.isNull() )
116
 
        { painter.drawPixmap( QPoint(0,0), fade( _endPixmap, opacity ) ); }
117
 
 
118
 
        painter.end();
119
 
        return;
120
 
 
121
 
    }
122
 
 
123
 
    //_________________________________________________________
124
 
    QPixmap TitleAnimationData::BlendedPixmap::fade( QPixmap source, qreal opacity ) const
125
 
    {
126
 
 
127
 
        if( source.isNull() ) return QPixmap();
128
 
 
129
 
        QPixmap out( source.size() );
130
 
        out.fill( Qt::transparent );
131
 
 
132
 
        // do nothing if opacity is too small
133
 
        if( opacity*255 < 1 ) return out;
134
 
 
135
 
        // draw pixmap
136
 
        QPainter p( &out );
137
 
        p.drawPixmap( QPoint(0,0), source );
138
 
 
139
 
        // opacity mask
140
 
        if( opacity*255 <= 254 )
141
 
        {
142
 
            p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
143
 
            QColor color( Qt::black );
144
 
            color.setAlphaF( opacity );
145
 
            p.fillRect(out.rect(), color );
146
 
        }
147
 
 
148
 
        p.end();
149
 
        return out;
150
 
 
151
 
    }
152
 
 
153
 
}