ImportController.cpp 14.5 KB
Newer Older
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
1 2 3
/*****************************************************************************
 * ImportController.cpp
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
5 6 7
 *
 * Authors: Geoffroy Lacarriere <geoffroylaca@gmail.com>
 *          Thomas Boquet <thomas.boquet@gmail.com>
8
 *          Clement Chavance <chavance.c@gmail.com>
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
9
 *          Hugo Beauzée-Luyssen <hugo@vlmc.org>
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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.
 *****************************************************************************/

Thomas Boquet's avatar
Thomas Boquet committed
26
#include "ui_ImportController.h"
27

Thomas Boquet's avatar
Thomas Boquet committed
28
#include "ClipRenderer.h"
29
#include "ImportController.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
30
#include "Library.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
31
#include "MetaDataManager.h"
32 33

#include <QPalette>
34
#include <QSettings>
35
#include <QTime>
Thomas Boquet's avatar
Thomas Boquet committed
36 37 38

ImportController::ImportController(QWidget *parent) :
    QDialog(parent),
39 40 41
    m_ui(new Ui::ImportController),
    m_clipListController( 0 ),
    m_controllerSwitched( false )
Thomas Boquet's avatar
Thomas Boquet committed
42 43 44 45 46 47
{
    m_ui->setupUi(this);
    m_preview = new PreviewWidget( new ClipRenderer, m_ui->previewContainer );
    m_stackNav = new StackViewController( m_ui->stackViewContainer, false );
    m_mediaListController = new ImportMediaListController( m_stackNav );
    m_tag = new TagWidget( m_ui->tagContainer, 6 );
48
    m_filesModel = new QDirModel();
Thomas Boquet's avatar
Thomas Boquet committed
49 50 51
    m_stackNav->pushViewController( m_mediaListController );

    QStringList filters;
52 53
    filters << "*.mov" << "*.avi" << "*.mkv" << "*.mpg" << "*.mpeg" << "*.wmv" << "*.mp4"
            << "*.ogg" << "*.ogv";
Thomas Boquet's avatar
Thomas Boquet committed
54 55
    filters << "*.mp3" << "*.oga" << "*.flac" << "*.aac" << "*.wav";
    filters << "*.gif" << "*.png" << "*.jpg";
56
    m_filesModel->setFilter( QDir::AllDirs | QDir::Files | QDir::Readable | QDir::Drives |
57
                             QDir::NoDotAndDotDot );
Thomas Boquet's avatar
Thomas Boquet committed
58 59 60 61
    m_filesModel->sort( 2, Qt::AscendingOrder );
    m_filesModel->sort( 0, Qt::AscendingOrder );
    m_filesModel->setNameFilters( filters );

62
    Library::getInstance()->setFilter( filters );
63 64 65

    restoreCurrentPath();

Thomas Boquet's avatar
Thomas Boquet committed
66
    m_ui->treeView->setModel( m_filesModel );
67
    m_ui->treeView->setHeaderHidden( true );
68
    m_ui->treeView->setCurrentIndex( m_filesModel->index( m_currentlyWatchedDir ) );
Thomas Boquet's avatar
Thomas Boquet committed
69
    m_ui->treeView->setExpanded( m_ui->treeView->currentIndex() , true );
70 71
    m_ui->treeView->header()->setStretchLastSection( false );
    m_ui->treeView->header()->setResizeMode( QHeaderView::ResizeToContents );
Thomas Boquet's avatar
Thomas Boquet committed
72 73 74
    m_ui->treeView->setColumnHidden( 1, true );
    m_ui->treeView->setColumnHidden( 2, true );
    m_ui->treeView->setColumnHidden( 3, true );
75
    m_ui->forwardButton->setEnabled( true );
Thomas Boquet's avatar
Thomas Boquet committed
76 77

    m_fsWatcher = new QFileSystemWatcher();
78
    m_fsWatcher->addPath( m_currentlyWatchedDir );
Thomas Boquet's avatar
Thomas Boquet committed
79

80 81 82 83 84
    m_progressDialog = new QProgressDialog( tr("Importing files..."),
                                            tr("Cancel"), 0, 0, NULL);
    m_progressDialog->setWindowModality( Qt::WindowModal );
    m_progressDialog->setMinimumDuration( 1000 );

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    connect( m_fsWatcher, SIGNAL( directoryChanged( QString ) ),
             m_filesModel, SLOT( refresh() ) );
    connect( m_ui->treeView, SIGNAL( clicked( QModelIndex ) ),
             this, SLOT( treeViewClicked( QModelIndex ) ) );
    connect( m_ui->treeView, SIGNAL( doubleClicked( QModelIndex ) ),
             this, SLOT( treeViewDoubleClicked( QModelIndex ) ) );
    connect( m_ui->forwardButton, SIGNAL( clicked() ),
             this, SLOT( forwardButtonClicked() ) );

    connect( this, SIGNAL( mediaSelected( Media* ) ),
             m_preview->getGenericRenderer(), SLOT( setMedia( Media* ) ) );
    connect( this, SIGNAL( mediaSelected( Media* ) ),
             m_tag, SLOT( mediaSelected( Media* ) ) );

    connect( m_mediaListController, SIGNAL( mediaSelected( const QUuid& ) ),
             this, SLOT( mediaSelection( const QUuid& ) ) );
    connect( m_mediaListController, SIGNAL( mediaDeleted( const QUuid& ) ),
             this, SLOT( mediaDeletion( const QUuid& ) ) );

    connect( m_mediaListController, SIGNAL( showClipListAsked( const QUuid& ) ),
             this, SLOT( showClipList( const QUuid& ) ) );
    connect( m_preview, SIGNAL( addClip( Clip* ) ),
             m_mediaListController, SLOT( clipAdded( Clip* ) ) ); //StackViewController
    connect( m_stackNav, SIGNAL( previousButtonPushed() ),
             this, SLOT( restoreContext() ) );
Thomas Boquet's avatar
Thomas Boquet committed
110 111 112 113
}

ImportController::~ImportController()
{
114
    delete m_preview;
Thomas Boquet's avatar
Thomas Boquet committed
115 116 117 118 119
    delete m_ui;
    delete m_stackNav;
    delete m_tag;
}

120 121
void
ImportController::changeEvent( QEvent *e )
Thomas Boquet's avatar
Thomas Boquet committed
122 123
{
    QDialog::changeEvent( e );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
124 125 126 127 128 129 130
    switch ( e->type() )
    {
        case QEvent::LanguageChange:
            m_ui->retranslateUi( this );
            break;
        default:
            break;
Thomas Boquet's avatar
Thomas Boquet committed
131 132 133
    }
}

134 135
void
ImportController::mediaSelection( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
136
{
137 138 139
    if ( m_mediaListController->contains( uuid ) )
    {
        if ( !m_currentUuid.isNull() && m_mediaListController->contains( m_currentUuid ) )
140 141
            m_mediaListController->cell( m_currentUuid )->setPalette( this->palette() );
        QPalette p = m_mediaListController->cell( uuid )->palette();
142
        p.setColor( QPalette::Window, QColor( Qt::darkBlue ) );
143
        m_mediaListController->cell( uuid )->setPalette( p );
144

145
        setUIMetaData( m_temporaryMedias[uuid] );
146 147 148
        if ( uuid != NULL && uuid != m_currentUuid )
            m_preview->stop();
        m_currentUuid = uuid;
149
        emit mediaSelected( m_temporaryMedias[uuid] );
150
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
151 152
}

153 154
void
ImportController::clipSelection( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
155
{
156
    if ( !m_currentUuid.isNull() && !m_controllerSwitched )
157
        m_clipListController->cell( m_currentUuid )->setPalette( palette() );
158 159
    else
        m_controllerSwitched = false;
160
    QPalette p = m_clipListController->cell( uuid )->palette();
161
    p.setColor( QPalette::Window, QColor( Qt::darkBlue ) );
162
    m_clipListController->cell( uuid )->setPalette( p );
163
    Clip*   clip;
164
    foreach( Media* media, m_temporaryMedias.values() )
165
    {
166 167
        if ( ( clip = media->clip( uuid ) ) != 0 )
            break;
168
    }
169 170
    if ( clip == 0 )
        return ;
171
    setUIMetaData( clip );
172 173 174 175
    if ( uuid != m_currentUuid )
        m_preview->stop();
    emit clipSelected( clip );
    m_currentUuid = uuid;
Thomas Boquet's avatar
Thomas Boquet committed
176 177
}

178
void
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
179
ImportController::updateMediaRequested( Media *media )
180
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
181
    if ( m_temporaryMedias.contains( media->getUuid() ) == false )
182
        return ;
183 184 185 186 187 188 189 190 191
    ImportMediaCellView*    cell = m_mediaListController->cell( media->getUuid() );
    if ( cell == NULL )
        return;
    cell->setThumbnail( media->getSnapshot() );
    cell->setLength( media->getLengthMS() );
}

void
ImportController::setUIMetaData( Media* media )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
192
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
193 194 195 196
    if ( media != NULL )
    {
        //Duration
        QTime   duration;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
197
        duration = duration.addMSecs( media->getLengthMS() );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
198 199 200 201 202 203 204
        m_ui->durationValueLabel->setText( duration.toString( "hh:mm:ss" ) );
        //Filename || title
        m_ui->nameValueLabel->setText( media->getFileInfo()->fileName() );
        m_ui->nameValueLabel->setWordWrap( true );
        setWindowTitle( media->getFileInfo()->fileName() + " " + tr( "properties" ) );
        //Resolution
        m_ui->resolutionValueLabel->setText( QString::number( media->getWidth() )
205
                                        + " x " + QString::number( media->getHeight() ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
206 207 208 209 210
        //FPS
        m_ui->fpsValueLabel->setText( QString::number( media->getFps() ) );
    }
    else
    {
211 212 213 214
        m_ui->durationValueLabel->setText( "--:--:--" );
        m_ui->nameValueLabel->setText( "none" );
        m_ui->resolutionValueLabel->setText( "-- x --" );
        m_ui->fpsValueLabel->setText( "--" );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
215
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
216 217
}

218 219
void
ImportController::setUIMetaData( Clip* clip )
220 221 222 223 224 225 226
{
    QTime   time;
    qint64  length = clip->getLengthSecond();
    time = time.addSecs( length );
    m_ui->durationValueLabel->setText( time.toString( "hh:mm:ss" ) );
    m_ui->nameValueLabel->setText( clip->getParent()->getFileInfo()->fileName() );
    m_ui->nameValueLabel->setWordWrap( true );
227 228
    setWindowTitle( clip->getParent()->getFileInfo()->fileName() + " " +
                    tr( "properties" ) );
229 230 231 232
    m_ui->resolutionValueLabel->setText( QString::number( clip->getParent()->getWidth() )
            + " x " + QString::number( clip->getParent()->getHeight() ) );
    m_ui->fpsValueLabel->setText( QString::number( clip->getParent()->getFps() ) );
}
Thomas Boquet's avatar
Thomas Boquet committed
233

234
void
235
ImportController::importMedia( const QString &filePath )
Thomas Boquet's avatar
Thomas Boquet committed
236
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
237 238 239 240 241 242
    foreach ( Media* media, m_temporaryMedias.values() )
        if ( media->getFileInfo()->filePath() == filePath )
            return ;
    if ( Library::getInstance()->mediaAlreadyLoaded( filePath ) == true )
        return ;

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
243 244 245 246 247 248 249 250
    Media*          media = new Media( filePath );
    connect( media, SIGNAL( metaDataComputed( Media* ) ),
             this, SLOT( updateMediaRequested( Media* ) ) );
    connect( media, SIGNAL( snapshotComputed( Media* ) ),
             this, SLOT( updateMediaRequested( Media* ) ) );
    m_temporaryMedias[media->getUuid()] = media;
    MetaDataManager::getInstance()->computeMediaMetadata( media );
    m_mediaListController->addMedia( media );
Thomas Boquet's avatar
Thomas Boquet committed
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
void
ImportController::importDir( const QString &path )
{
    QDir            dir( path );
    QFileInfoList   files = dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Readable
                                               | QDir::AllEntries );

    foreach ( QFileInfo fInfo, files )
    {
        if ( fInfo.isDir() == true )
            importDir( fInfo.absolutePath() );
        else
        {
            QString ext = fInfo.suffix();

            if ( Media::AudioExtensions.contains( ext ) ||
                 Media::VideoExtensions.contains( ext ) ||
                 Media::ImageExtensions.contains( ext ) )
            {
                importMedia( fInfo.absoluteFilePath() );
            }
        }
    }
}

void
ImportController::forwardButtonClicked()
{
    QModelIndex     index = m_ui->treeView->selectionModel()->currentIndex();
    QString         filePath = m_filesModel->fileInfo( index ).filePath();

    if ( !m_filesModel->isDir( index ) )
        importMedia( filePath );
    else
        importDir( filePath );
}

290 291
void
ImportController::treeViewClicked( const QModelIndex& index )
Thomas Boquet's avatar
Thomas Boquet committed
292 293 294 295 296 297
{
    if ( m_filesModel->isDir( index ) )
    {
        m_fsWatcher->removePath( m_currentlyWatchedDir );
        m_currentlyWatchedDir = m_filesModel->filePath( index );
        m_fsWatcher->addPath( m_filesModel->filePath( index ) );
298
        saveCurrentPath();
Thomas Boquet's avatar
Thomas Boquet committed
299 300 301 302
    }
    m_ui->forwardButton->setEnabled( true );
}

303 304
void
ImportController::treeViewDoubleClicked( const QModelIndex& index )
Thomas Boquet's avatar
Thomas Boquet committed
305
{
306
    forwardButtonClicked();
Thomas Boquet's avatar
Thomas Boquet committed
307
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
308

309 310
void
ImportController::reject()
311 312
{
    m_preview->stop();
313
    m_mediaListController->cleanAll();
314
    deleteTemporaryMedias();
315
    collapseAllButCurrentPath();
316
    done( Rejected );
317 318
}

319 320
void
ImportController::accept()
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
321
{
322
    m_mediaListController->cleanAll();
323
    m_preview->stop();
324
    collapseAllButCurrentPath();
325 326
    foreach ( Media* media, m_temporaryMedias.values() )
        Library::getInstance()->addMedia( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
327
    m_temporaryMedias.clear();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
328 329
    done( Accepted );
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
330

331 332 333 334 335 336 337 338
void
ImportController::deleteTemporaryMedias()
{
    foreach ( Media* media, m_temporaryMedias.values() )
        delete media;
    m_temporaryMedias.clear();
}

339 340 341 342 343 344 345 346 347 348 349 350
void
ImportController::collapseAllButCurrentPath()
{
    m_ui->treeView->collapseAll();
    QStringList paths;
    for ( QDir directory( m_currentlyWatchedDir ); !directory.isRoot(); directory.cdUp() )
        paths.prepend( directory.absolutePath() );
    while ( paths.count() > 0 )
    {
        m_ui->treeView->setCurrentIndex( m_filesModel->index( paths.takeFirst() ) );
        m_ui->treeView->setExpanded( m_ui->treeView->currentIndex() , true );
    }
351
    m_ui->forwardButton->setEnabled( true );
352 353
}

354 355
void
ImportController::mediaDeletion( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
356 357
{
    m_mediaListController->removeMedia( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
358 359
    if ( m_temporaryMedias.contains( uuid ) == true )
        delete m_temporaryMedias.take( uuid );
360

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
361
    if ( uuid == m_currentUuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
362
    {
363
        setUIMetaData( static_cast<Media*>( 0 ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
364
        m_currentUuid = QUuid();
365
        m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
366
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
367 368
}

369 370
void
ImportController::clipDeletion( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
371
{
372
    m_clipListController->removeClip( uuid );
373
    foreach( Media* media, m_temporaryMedias.values() )
374
    {
375
        if ( media != NULL && media->clip( uuid ) )
376 377
            media->removeClip( uuid );
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
378
}
379

380 381
void
ImportController::showClipList( const QUuid& uuid )
382
{
383 384 385 386
    if ( m_temporaryMedias.contains( uuid ) == false )
        return ;
    Media* media = m_temporaryMedias[uuid];
    if ( media->clips()->size() == 0 )
387
        return ;
388

389
    m_clipListController = new ImportMediaListController( m_stackNav );
390 391 392 393
    connect( m_clipListController, SIGNAL( clipSelected( const QUuid& ) ),
             this, SLOT( clipSelection( const QUuid& ) ) );
    connect( m_clipListController, SIGNAL( clipDeleted( const QUuid& ) ),
             this, SLOT( clipDeletion( const QUuid& ) ) );
394 395 396 397 398 399 400
    m_clipListController->addClipsFromMedia( media );
    if ( !m_currentUuid.isNull() )
        m_savedUuid = m_currentUuid;
    m_controllerSwitched = true;
    m_stackNav->pushViewController( m_clipListController );
}

401 402
void
ImportController::restoreContext()
403
{
404
    if ( m_clipListController->nbDeletions() != 0 )
405
    {
406 407 408 409 410
        if ( !m_savedUuid.isNull() && m_mediaListController->cell( m_savedUuid ) != NULL )
        {
            ImportMediaCellView* cell = m_mediaListController->cell( m_savedUuid );
            cell->decrementClipCount( m_clipListController->nbDeletions() );
        }
411
    }
412 413 414
    if ( !m_savedUuid.isNull() )
        m_currentUuid = m_savedUuid;
    m_controllerSwitched = false;
415
}
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

void
ImportController::saveCurrentPath()
{
    QSettings s;
    s.setValue( "ImportPreviouslySelectPath", m_currentlyWatchedDir );
    s.sync();
}

void
ImportController::restoreCurrentPath()
{
    QSettings s;
    QVariant path = s.value( "ImportPreviouslySelectPath", QDir::homePath() );
    QFileInfo info = path.toString();
    m_currentlyWatchedDir = info.absoluteFilePath();
}
433 434 435 436 437 438 439 440 441 442 443 444

void
ImportController::progressDialogMax( int max )
{
    m_progressDialog->setMaximum( max );
}

void
ImportController::progressDialogValue( int value )
{
    m_progressDialog->setValue( value );
}