Blame style/animations/adwaitatabbardata.cpp

Packit 8e9c33
/*************************************************************************
Packit 8e9c33
 * Copyright (C) 2014 by Hugo Pereira Da Costa <hugo.pereira@free.fr>    *
Packit 8e9c33
 *                                                                       *
Packit 8e9c33
 * This program is free software; you can redistribute it and/or modify  *
Packit 8e9c33
 * it under the terms of the GNU General Public License as published by  *
Packit 8e9c33
 * the Free Software Foundation; either version 2 of the License, or     *
Packit 8e9c33
 * (at your option) any later version.                                   *
Packit 8e9c33
 *                                                                       *
Packit 8e9c33
 * This program is distributed in the hope that it will be useful,       *
Packit 8e9c33
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
Packit 8e9c33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
Packit 8e9c33
 * GNU General Public License for more details.                          *
Packit 8e9c33
 *                                                                       *
Packit 8e9c33
 * You should have received a copy of the GNU General Public License     *
Packit 8e9c33
 * along with this program; if not, write to the                         *
Packit 8e9c33
 * Free Software Foundation, Inc.,                                       *
Packit 8e9c33
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
Packit 8e9c33
 *************************************************************************/
Packit 8e9c33
Packit 8e9c33
//////////////////////////////////////////////////////////////////////////////
Packit 8e9c33
// adwaitatabbardata.cpp
Packit 8e9c33
// data container for QTabBar animations
Packit 8e9c33
// -------------------
Packit 8e9c33
//
Packit 8e9c33
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
Packit 8e9c33
//
Packit 8e9c33
// Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 8e9c33
// of this software and associated documentation files (the "Software"), to
Packit 8e9c33
// deal in the Software without restriction, including without limitation the
Packit 8e9c33
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit 8e9c33
// sell copies of the Software, and to permit persons to whom the Software is
Packit 8e9c33
// furnished to do so, subject to the following conditions:
Packit 8e9c33
//
Packit 8e9c33
// The above copyright notice and this permission notice shall be included in
Packit 8e9c33
// all copies or substantial portions of the Software.
Packit 8e9c33
//
Packit 8e9c33
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 8e9c33
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 8e9c33
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit 8e9c33
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 8e9c33
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit 8e9c33
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit 8e9c33
// IN THE SOFTWARE.
Packit 8e9c33
//////////////////////////////////////////////////////////////////////////////
Packit 8e9c33
Packit 8e9c33
#include "adwaitatabbardata.h"
Packit 8e9c33
Packit 8e9c33
#include <QHoverEvent>
Packit 8e9c33
#include <QTabBar>
Packit 8e9c33
Packit 8e9c33
namespace Adwaita
Packit 8e9c33
{
Packit 8e9c33
Packit 8e9c33
    //______________________________________________
Packit 8e9c33
    TabBarData::TabBarData( QObject* parent, QWidget* target, int duration ):
Packit 8e9c33
        AnimationData( parent, target )
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        _current._animation = new Animation( duration, this );
Packit 8e9c33
        setupAnimation( currentIndexAnimation(), "currentOpacity" );
Packit 8e9c33
        currentIndexAnimation().data()->setDirection( Animation::Forward );
Packit 8e9c33
Packit 8e9c33
        _previous._animation = new Animation( duration, this );
Packit 8e9c33
        setupAnimation( previousIndexAnimation(), "previousOpacity" );
Packit 8e9c33
        previousIndexAnimation().data()->setDirection( Animation::Backward );
Packit 8e9c33
Packit 8e9c33
    }
Packit 8e9c33
Packit 8e9c33
    //______________________________________________
Packit 8e9c33
    Animation::Pointer TabBarData::animation( const QPoint& position ) const
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() ) return Animation::Pointer();
Packit 8e9c33
Packit 8e9c33
        const QTabBar* local( qobject_cast<const QTabBar*>( target().data() ) );
Packit 8e9c33
        if( !local ) return Animation::Pointer();
Packit 8e9c33
Packit 8e9c33
        int index( local->tabAt( position ) );
Packit 8e9c33
        if( index < 0 ) return Animation::Pointer();
Packit 8e9c33
        else if( index == currentIndex() ) return currentIndexAnimation();
Packit 8e9c33
        else if( index == previousIndex() ) return previousIndexAnimation();
Packit 8e9c33
        else return Animation::Pointer();
Packit 8e9c33
Packit 8e9c33
    }
Packit 8e9c33
Packit 8e9c33
    //______________________________________________
Packit 8e9c33
    bool TabBarData::updateState( const QPoint& position , bool hovered )
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() ) return false;
Packit 8e9c33
Packit 8e9c33
        const QTabBar* local( qobject_cast<const QTabBar*>( target().data() ) );
Packit 8e9c33
        if( !local ) return false;
Packit 8e9c33
Packit 8e9c33
        int index( local->tabAt( position ) );
Packit 8e9c33
        if( index < 0 ) return false;
Packit 8e9c33
Packit 8e9c33
        if( hovered )
Packit 8e9c33
        {
Packit 8e9c33
Packit 8e9c33
Packit 8e9c33
            if( index != currentIndex() )
Packit 8e9c33
            {
Packit 8e9c33
Packit 8e9c33
                if( currentIndex() >= 0 )
Packit 8e9c33
                {
Packit 8e9c33
                    setPreviousIndex( currentIndex() );
Packit 8e9c33
                    setCurrentIndex( -1 );
Packit 8e9c33
                    previousIndexAnimation().data()->restart();
Packit 8e9c33
                }
Packit 8e9c33
Packit 8e9c33
                setCurrentIndex( index );
Packit 8e9c33
                currentIndexAnimation().data()->restart();
Packit 8e9c33
                return true;
Packit 8e9c33
Packit 8e9c33
            } else return false;
Packit 8e9c33
Packit 8e9c33
        } else if( index == currentIndex() ) {
Packit 8e9c33
Packit 8e9c33
            setPreviousIndex( currentIndex() );
Packit 8e9c33
            setCurrentIndex( -1 );
Packit 8e9c33
            previousIndexAnimation().data()->restart();
Packit 8e9c33
            return true;
Packit 8e9c33
Packit 8e9c33
        } else return false;
Packit 8e9c33
Packit 8e9c33
    }
Packit 8e9c33
Packit 8e9c33
    //______________________________________________
Packit 8e9c33
    qreal TabBarData::opacity( const QPoint& position ) const
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() ) return OpacityInvalid;
Packit 8e9c33
Packit 8e9c33
        const QTabBar* local( qobject_cast<const QTabBar*>( target().data() ) );
Packit 8e9c33
        if( !local ) return OpacityInvalid;
Packit 8e9c33
Packit 8e9c33
        int index( local->tabAt( position ) );
Packit 8e9c33
        if( index < 0 ) return OpacityInvalid;
Packit 8e9c33
        else if( index == currentIndex() ) return currentOpacity();
Packit 8e9c33
        else if( index == previousIndex() ) return previousOpacity();
Packit 8e9c33
        else return OpacityInvalid;
Packit 8e9c33
Packit 8e9c33
    }
Packit 8e9c33
Packit 8e9c33
Packit 8e9c33
}