Blame style/animations/adwaitaheaderviewdata.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
// adwaitaheaderviewdata.cpp
Packit 8e9c33
// data container for QHeaderView 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 "adwaitaheaderviewdata.h"
Packit 8e9c33
Packit 8e9c33
#include <QHoverEvent>
Packit 8e9c33
#include <QTextStream>
Packit 8e9c33
Packit 8e9c33
namespace Adwaita
Packit 8e9c33
{
Packit 8e9c33
Packit 8e9c33
    //______________________________________________
Packit 8e9c33
    HeaderViewData::HeaderViewData( 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
    bool HeaderViewData::updateState( const QPoint& position , bool hovered )
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() ) return false;
Packit 8e9c33
Packit 8e9c33
        const QHeaderView* local( qobject_cast<const QHeaderView*>( target().data() ) );
Packit 8e9c33
        if( !local ) return false;
Packit 8e9c33
Packit 8e9c33
        int index( local->logicalIndexAt( 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
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
    Animation::Pointer HeaderViewData::animation( const QPoint& position ) const
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() )  return Animation::Pointer();
Packit 8e9c33
Packit 8e9c33
        const QHeaderView* local( qobject_cast<const QHeaderView*>( target().data() ) );
Packit 8e9c33
        if( !local ) return Animation::Pointer();
Packit 8e9c33
Packit 8e9c33
        int index( local->logicalIndexAt( 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
    qreal HeaderViewData::opacity( const QPoint& position ) const
Packit 8e9c33
    {
Packit 8e9c33
Packit 8e9c33
        if( !enabled() ) return OpacityInvalid;
Packit 8e9c33
Packit 8e9c33
        const QHeaderView* local( qobject_cast<const QHeaderView*>( target().data() ) );
Packit 8e9c33
        if( !local ) return OpacityInvalid;
Packit 8e9c33
Packit 8e9c33
        int index( local->logicalIndexAt( 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
    //__________________________________________________________
Packit 8e9c33
    void HeaderViewData::setDirty( void ) const
Packit 8e9c33
    {
Packit 8e9c33
        QHeaderView* header = qobject_cast<QHeaderView*>( target().data() );
Packit 8e9c33
        if( !header ) return;
Packit 8e9c33
Packit 8e9c33
        // get first and last index, sorted
Packit 8e9c33
        int lastIndex( qMax( previousIndex(), currentIndex() ) );
Packit 8e9c33
        if( lastIndex < 0 ) return;
Packit 8e9c33
Packit 8e9c33
        int firstIndex( qMin( previousIndex(), currentIndex() ) );
Packit 8e9c33
        if( firstIndex < 0 ) firstIndex = lastIndex;
Packit 8e9c33
Packit 8e9c33
        // find relevant rectangle to be updated, in viewport coordinate
Packit 8e9c33
        QWidget* viewport( header->viewport() );
Packit 8e9c33
        int left = header->sectionViewportPosition( firstIndex );
Packit 8e9c33
        int right = header->sectionViewportPosition( lastIndex ) + header->sectionSize( lastIndex );
Packit 8e9c33
Packit 8e9c33
        // trigger update
Packit 8e9c33
        if( header->orientation() == Qt::Horizontal ) viewport->update( left, 0, right-left, header->height() );
Packit 8e9c33
        else viewport->update( 0, left, header->width(), right-left );
Packit 8e9c33
Packit 8e9c33
    }
Packit 8e9c33
Packit 8e9c33
}