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

« back to all changes in this revision

Viewing changes to kstyles/oxygen/animations/oxygenprogressbardata.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
// oxygenprogressbar.cpp
 
3
// data container for progressbar animations
 
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 "oxygenprogressbardata.h"
 
28
#include "oxygenprogressbardata.moc"
 
29
 
 
30
#include <QtGui/QProgressBar>
 
31
#include <cassert>
 
32
 
 
33
namespace Oxygen
 
34
{
 
35
 
 
36
    //______________________________________________
 
37
    ProgressBarData::ProgressBarData( QObject* parent, QWidget* target, int duration ):
 
38
        GenericData( parent, target, duration ),
 
39
        _startValue(0),
 
40
        _endValue(0)
 
41
    {
 
42
 
 
43
        target->installEventFilter( this );
 
44
 
 
45
        // set animation curve shape
 
46
        animation().data()->setEasingCurve( QEasingCurve::InOutQuad );
 
47
 
 
48
        // make sure target is a progressbar and store relevant values
 
49
        QProgressBar* progress = qobject_cast<QProgressBar*>( target );
 
50
        assert( progress );
 
51
        setStartValue( progress->value() );
 
52
        setEndValue( progress->value() );
 
53
 
 
54
        // setup connections
 
55
        connect( target, SIGNAL( valueChanged( int ) ), SLOT( valueChanged( int ) ) );
 
56
 
 
57
    }
 
58
 
 
59
    //______________________________________________
 
60
    bool ProgressBarData::eventFilter( QObject* object, QEvent* event )
 
61
    {
 
62
 
 
63
        if( !( enabled() && object && object == target().data() ) ) return AnimationData::eventFilter( object, event );
 
64
        switch( event->type() )
 
65
        {
 
66
            case QEvent::Show:
 
67
            {
 
68
 
 
69
                // reset start and end value
 
70
                QProgressBar* progress = static_cast<QProgressBar*>( target().data() );
 
71
                setStartValue( progress->value() );
 
72
                setEndValue( progress->value() );
 
73
                break;
 
74
 
 
75
            }
 
76
 
 
77
            case QEvent::Hide:
 
78
            {
 
79
                if( animation().data()->isRunning() )
 
80
                { animation().data()->stop(); }
 
81
                break;
 
82
            }
 
83
 
 
84
            default: break;
 
85
 
 
86
        }
 
87
 
 
88
        return AnimationData::eventFilter( object, event );
 
89
 
 
90
    }
 
91
 
 
92
    //______________________________________________
 
93
    void ProgressBarData::valueChanged( int value )
 
94
    {
 
95
 
 
96
        // do nothing if not enabled
 
97
        if( !enabled() ) return;
 
98
 
 
99
        // do nothing if progress is invalid
 
100
        QProgressBar* progress = static_cast<QProgressBar*>( target().data() );
 
101
        if( !( progress && progress->maximum() != progress->minimum() ) ) return;
 
102
 
 
103
        // update start and end values
 
104
        bool isRunning( animation().data()->isRunning() );
 
105
        if( isRunning )
 
106
        {
 
107
 
 
108
            // in case next value arrives while animation is running,
 
109
            // end animation, set value immediately
 
110
            // and trigger target update. This increases responsiveness of progressbars
 
111
            setStartValue( value );
 
112
            setEndValue( value );
 
113
            animation().data()->stop();
 
114
            setOpacity(0);
 
115
 
 
116
            if( target() ) target().data()->update();
 
117
 
 
118
            return;
 
119
 
 
120
        }
 
121
 
 
122
        setStartValue( endValue() );
 
123
        setEndValue( value );
 
124
 
 
125
        // start animation only if target is enabled, visible, not running,
 
126
        // and if end and start values are different enough
 
127
        // (with end value being larger than start value)
 
128
        if( !(target() && target().data()->isEnabled() && target().data()->isVisible()) ) return;
 
129
        if( isRunning || endValue()-startValue() < 2 ) return;
 
130
 
 
131
        animation().data()->start();
 
132
 
 
133
    }
 
134
 
 
135
}