EffectInstanceListModel.cpp 3.67 KB
Newer Older
1 2 3
/*****************************************************************************
 * EffectInstanceListModel.cpp: Handle the model part of displaying an effect instance list.
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "EffectInstanceListModel.h"
24

25
#include "Main/Core.h"
26 27 28
#include "EffectsEngine/EffectHelper.h"
#include "EffectsEngine/EffectInstance.h"
#include "EffectsEngine/EffectUser.h"
29

30 31 32
#include <QApplication>
#include <QFontMetrics>

33 34
EffectInstanceListModel::EffectInstanceListModel( EffectUser *user ) :
        m_user( user )
35 36 37 38 39 40
{
}

qint32
EffectInstanceListModel::rowCount( const QModelIndex& ) const
{
41
    return m_user->effects( Effect::Filter ).size();
42 43 44 45 46 47 48 49
}

QVariant
EffectInstanceListModel::data( const QModelIndex &index, int role ) const
{
    switch ( role )
    {
    case Qt::DisplayRole:
50
        return m_user->effects( Effect::Filter ).at( index.row() )->effectInstance()->effect()->name();
51
    case Qt::ToolTipRole:
52
        return m_user->effects( Effect::Filter ).at( index.row() )->effectInstance()->effect()->description();
53
    case Qt::EditRole:
54
        return QVariant::fromValue( m_user->effects( Effect::Filter ).at( index.row() ) );
55 56 57
    case Qt::SizeHintRole:
        {
            const QFontMetrics  &fm = QApplication::fontMetrics();
58
            QSize               size( fm.width( m_user->effects( Effect::Filter ).at( index.row() )->effectInstance()->effect()->name() ), fm.height() );
59 60
            return size;
        }
61 62 63 64
    default:
        return QVariant();
    }
}
65

66 67 68
bool
EffectInstanceListModel::removeRows( int row, int count, const QModelIndex& index /*= QModelIndex()*/ )
{
69
    if ( count != 1 || row < 0 )
70 71
        return false;
    beginRemoveRows( index, row, row );
72
    m_user->removeEffect( Effect::Filter, row );
73 74 75 76
    endRemoveRows();
    return true;
}

77 78 79
void
EffectInstanceListModel::moveUp( const QModelIndex &index )
{
80
    if ( index.row() <= 0 || index.row() >= m_user->count( Effect::Filter ) )
81 82
        return ;
    emit layoutAboutToBeChanged();
83
    m_user->swapFilters( index.row(), index.row() - 1 );
84 85 86 87 88 89
    emit layoutChanged();
}

void
EffectInstanceListModel::moveDown( const QModelIndex &index )
{
90
    if ( index.row() >= m_user->count( Effect::Filter ) - 1 )
91 92
        return ;
    emit layoutAboutToBeChanged();
93
    m_user->swapFilters( index.row(), index.row() + 1 );
94 95
    emit layoutChanged();
}
96

97
EffectHelper*
98 99 100
EffectInstanceListModel::add( const QString &effectName )
{
    if ( effectName.isEmpty() == true )
101
        return NULL;
102
    Effect  *effect = Core::getInstance()->effectsEngine()->effect( effectName );
103
    if ( effect == NULL )
104
        return NULL;
105
    beginInsertRows( QModelIndex(), m_user->count( Effect::Filter ), m_user->count( Effect::Filter ) );
106
    EffectHelper        *helper = m_user->addEffect( effect );
107
    endInsertRows();
108
    return helper;
109
}