~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kstyles/oxygen/transitions/oxygenlabeldata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
// oxygenlabeldata.cpp
 
3
// data container for QLabel transition
 
4
// -------------------
 
5
//
 
6
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
 
7
//
 
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
9
// of this software and associated documentation files (the "Software"), to
 
10
// deal in the Software without restriction, including without limitation the
 
11
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 
12
// sell copies of the Software, and to permit persons to whom the Software is
 
13
// furnished to do so, subject to the following conditions:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
23
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
24
// IN THE SOFTWARE.
 
25
//////////////////////////////////////////////////////////////////////////////
 
26
 
 
27
#include "oxygenlabeldata.h"
 
28
#include "oxygenlabeldata.moc"
 
29
 
 
30
#include <QtCore/QEvent>
 
31
#include <QtCore/QTextStream>
 
32
#include <QtGui/QPainter>
 
33
 
 
34
namespace Oxygen
 
35
{
 
36
 
 
37
    // use 300 milliseconds for animation lock
 
38
    const int LabelData::_lockTime = 300;
 
39
 
 
40
    //______________________________________________________
 
41
    LabelData::LabelData( QObject* parent, QLabel* target, int duration ):
 
42
        TransitionData( parent, target, duration ),
 
43
        _target( target )
 
44
    {
 
45
        _target.data()->installEventFilter( this );
 
46
 
 
47
        const bool hasProxy( _target.data()->graphicsProxyWidget() );
 
48
        const bool hasMessageWidget( hasParent( target, "KMessageWidget" ) );
 
49
 
 
50
        transition().data()->setFlags( hasProxy||hasMessageWidget ? TransitionWidget::Transparent : TransitionWidget::GrabFromWindow );
 
51
 
 
52
        connect( _target.data(), SIGNAL( destroyed() ), SLOT( targetDestroyed() ) );
 
53
 
 
54
    }
 
55
 
 
56
    //___________________________________________________________________
 
57
    bool LabelData::eventFilter( QObject* object, QEvent* event )
 
58
    {
 
59
 
 
60
        if( object != _target.data() ) return TransitionData::eventFilter( object, event );
 
61
        switch( event->type() )
 
62
        {
 
63
 
 
64
            case QEvent::Show:
 
65
            /*
 
66
            at show event, on set the old text to current
 
67
            to avoid animate the "first" paint event.
 
68
            text mnemonic is always removed to avoid triggering the animation when only the
 
69
            latter is changed
 
70
            */
 
71
            _text = _target.data()->text().remove( '&' );
 
72
            break;
 
73
 
 
74
            case QEvent::Paint:
 
75
            {
 
76
 
 
77
                if( enabled() && _target  )
 
78
                {
 
79
 
 
80
                    // remove showMnemonic from text before comparing
 
81
                    QString text( _target.data()->text().remove( '&' ) );
 
82
                    if( text == _text )
 
83
                    {
 
84
                        if(
 
85
                            transparent() &&
 
86
                            transition().data()->isAnimated() &&
 
87
                            TransitionWidget::paintEnabled() ) return true;
 
88
                        else break;
 
89
                    }
 
90
 
 
91
                    // update text and pixmap
 
92
                    _text = text;
 
93
 
 
94
                    if( !(transition() && _target.data()->isVisible() ) ) break;
 
95
 
 
96
                    if( transition().data()->isAnimated() )
 
97
                    { transition().data()->endAnimation(); }
 
98
 
 
99
                    // check whether animations are locked
 
100
                    if( isLocked() )
 
101
                    {
 
102
 
 
103
                        // hide transition widget
 
104
                        transition().data()->hide();
 
105
 
 
106
                        // restart the lock timer
 
107
                        // and abort transition
 
108
                        lockAnimations();
 
109
                        break;
 
110
                    }
 
111
 
 
112
                    // restart the lock timer
 
113
                    // and prepare transition
 
114
                    lockAnimations();
 
115
                    initializeAnimation();
 
116
                    _timer.start( 0, this );
 
117
 
 
118
                    if( !transition().data()->startPixmap().isNull() && TransitionWidget::paintEnabled() )
 
119
                    {
 
120
 
 
121
                        // show the transition widget
 
122
                        // and disable this event painting
 
123
                        transition().data()->show();
 
124
                        transition().data()->raise();
 
125
                        if( transparent() ) return true;
 
126
                        else break;
 
127
 
 
128
                    } else {
 
129
 
 
130
                        // hide transition widget and abort transition
 
131
                        transition().data()->hide();
 
132
                        break;
 
133
 
 
134
                    }
 
135
 
 
136
                } else if( transition().data()->isAnimated() && TransitionWidget::paintEnabled() ) {
 
137
 
 
138
                    // disable painting when transition is running
 
139
                    // since label is obscured by transition widget
 
140
                    return true;
 
141
 
 
142
                } else break;
 
143
            }
 
144
 
 
145
            default: break;
 
146
        }
 
147
 
 
148
        return TransitionData::eventFilter( object, event );
 
149
 
 
150
    }
 
151
 
 
152
    //___________________________________________________________________
 
153
    void LabelData::timerEvent( QTimerEvent* event )
 
154
    {
 
155
        if( event->timerId() == _timer.timerId() )
 
156
        {
 
157
 
 
158
            _timer.stop();
 
159
 
 
160
            // check transition and widget validity
 
161
            if( !( enabled() && _target && transition() ) ) return;
 
162
 
 
163
            // assign end pixmap
 
164
            transition().data()->setEndPixmap( transition().data()->grab( _target.data() ) );
 
165
 
 
166
            // start animation
 
167
            animate();
 
168
 
 
169
        } else if( event->timerId() == _animationLockTimer.timerId() ) {
 
170
 
 
171
            unlockAnimations();
 
172
 
 
173
            // check transition and widget validity
 
174
            if( !( enabled() && _target && transition() ) ) return;
 
175
 
 
176
            // reassign end pixmap for the next transition to be properly initialized
 
177
            transition().data()->setEndPixmap( transition().data()->grab( _target.data() ) );
 
178
 
 
179
        } else return TransitionData::timerEvent( event );
 
180
 
 
181
    }
 
182
 
 
183
    //___________________________________________________________________
 
184
    bool LabelData::initializeAnimation( void )
 
185
    {
 
186
 
 
187
        transition().data()->setOpacity(0);
 
188
        QRect current( _target.data()->geometry() );
 
189
        if( _widgetRect.isValid() && _widgetRect != current )
 
190
        {
 
191
 
 
192
            _widgetRect = current;
 
193
            transition().data()->resetStartPixmap();
 
194
            transition().data()->resetEndPixmap();
 
195
            return false;
 
196
 
 
197
        }
 
198
 
 
199
        transition().data()->setStartPixmap( transition().data()->currentPixmap() );
 
200
        transition().data()->setGeometry( _target.data()->rect() );
 
201
        _widgetRect = current;
 
202
        return true;
 
203
    }
 
204
 
 
205
    //___________________________________________________________________
 
206
    bool LabelData::animate( void )
 
207
    {
 
208
 
 
209
        if( transition().data()->startPixmap().isNull() ) return false;
 
210
 
 
211
        transition().data()->animate();
 
212
        return true;
 
213
 
 
214
    }
 
215
 
 
216
    //___________________________________________________________________
 
217
    void LabelData::targetDestroyed( void )
 
218
    {
 
219
        setEnabled( false );
 
220
        _target.clear();
 
221
    }
 
222
 
 
223
}