MediaLibrary.cpp 5.17 KB
Newer Older
1 2 3
/*****************************************************************************
 * MediaLibrary.cpp: VLMC media library's ui
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
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 24
 *
 * 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 "MediaLibrary.h"

25
#include "Project/Project.h"
26 27 28
#include "Media/Clip.h"
#include "Library/Library.h"
#include "Media/Media.h"
29
#include "MediaCellView.h"
30 31
#include "MediaListView.h"
#include "StackViewController.h"
32
#include "ViewController.h"
33
#include "Tools/VlmcDebug.h"
34

35
#include <QUrl>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
#include <QMimeData>
37

38 39 40 41
MediaLibrary::MediaLibrary(QWidget *parent) : QWidget(parent),
    m_ui( new Ui::MediaLibrary() )
{
    m_ui->setupUi( this );
42 43
    setAcceptDrops( true );

44
    StackViewController *nav = new StackViewController( m_ui->mediaListContainer );
45
    m_mediaListView = new MediaListView( nav );
46
    nav->pushViewController( m_mediaListView );
47 48 49

    connect( m_ui->importButton, SIGNAL( clicked() ),
             this, SIGNAL( importRequired() ) );
50
    connect( m_mediaListView, SIGNAL( clipSelected( Clip* ) ),
51
             this, SIGNAL( clipSelected( Clip* ) ) );
52 53
    connect( m_ui->filterInput, SIGNAL( textChanged( const QString& ) ),
             this, SLOT( filterUpdated( const QString& ) ) );
54 55
    connect( nav, SIGNAL( viewChanged( ViewController* ) ),
             this, SLOT( viewChanged( ViewController* ) ) );
56 57
    connect( m_ui->filterType, SIGNAL( currentIndexChanged( int ) ),
             this, SLOT( filterTypeChanged() ) );
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73
void
MediaLibrary::changeEvent( QEvent *e )
{
    QWidget::changeEvent( e );
    switch ( e->type() )
    {
        case QEvent::LanguageChange:
            m_ui->retranslateUi( this );
            break;
        default:
            break;
    }
}

74 75 76
void
MediaLibrary::filterUpdated( const QString &filter )
{
77 78 79
    const MediaListView::MediaList              &medias = m_mediaListView->mediaList();
    MediaListView::MediaList::const_iterator    it = medias.begin();
    MediaListView::MediaList::const_iterator    ite = medias.end();
80 81 82 83 84 85 86 87

    while ( it != ite )
    {
        MediaCellView  *mcv = it.value();

        mcv->setVisible( currentFilter()( mcv->clip(), filter ) );
        ++it;
    }
88 89
}

90 91 92
MediaLibrary::Filter
MediaLibrary::currentFilter()
{
93 94
    switch ( m_ui->filterType->currentIndex() )
    {
95 96 97 98 99 100
        case 0:
            return &filterByName;
        case 1:
            return &filterByTags;
        default:
            return &filterByName;
101
    }
102 103
}

104 105 106
void
MediaLibrary::viewChanged( ViewController *view )
{
107
    MediaListView       *mlv = qobject_cast<MediaListView*>( view );
108 109 110

    if ( mlv == NULL )
        return ;
111

112 113 114 115 116
    m_mediaListView = mlv;
    //Force an update as the media has changed
    filterUpdated( m_ui->filterInput->text() );
}

117 118 119 120 121
bool
MediaLibrary::filterByName( const Clip *clip, const QString &filter )
{
    return ( clip->getMedia()->fileName().contains( filter, Qt::CaseInsensitive ) );
}
122 123 124 125 126 127

bool
MediaLibrary::filterByTags( const Clip *clip, const QString &filter )
{
    return ( clip->matchMetaTag( filter ) );
}
128 129 130 131 132 133

void
MediaLibrary::filterTypeChanged()
{
    filterUpdated( m_ui->filterInput->text() );
}
134 135 136 137

void
MediaLibrary::dragEnterEvent( QDragEnterEvent *event )
{
138
    if ( event->mimeData()->hasFormat( "text/uri-list" ) )
139
    {
140
        event->acceptProposedAction();
141 142 143 144 145
    }
    else
        event->ignore();
}

146 147 148 149 150 151 152 153 154 155 156 157
void
MediaLibrary::dragMoveEvent( QDragMoveEvent *event )
{
    event->acceptProposedAction();
}

void
MediaLibrary::dragLeaveEvent( QDragLeaveEvent *event )
{
   event->accept();
}

158 159 160
void
MediaLibrary::dropEvent( QDropEvent *event )
{
161
    const QList<QUrl>         &fileList = event->mimeData()->urls();
162 163

    if ( fileList.isEmpty() )
164
    {
165 166 167 168
        event->ignore();
        return;
    }

169
    Q_ASSERT( Project::getInstance()->library() != NULL );
170

171
    foreach ( const QUrl &url, fileList )
172
    {
173
        const QString       &fileName = url.toLocalFile();
174 175 176 177

        if ( fileName.isEmpty() )
            continue;

178
        Media       *media = Project::getInstance()->library()->addMedia( fileName );
179

180 181
        if ( media != NULL )
        {
182
            Clip*       clip = new Clip( media );
183
            media->setBaseClip( clip );
184
            Project::getInstance()->library()->addClip( clip );
185 186 187
            event->accept();
        }
        else
188
            vlmcCritical() << "Clip already present in library or an error occurred while loading media:" << fileName;
189
    }
190
    event->accept();
191
}