ImportController.cpp 11.9 KB
Newer Older
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
1 2 3 4 5 6 7
/*****************************************************************************
 * ImportController.cpp
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Geoffroy Lacarriere <geoffroylaca@gmail.com>
 *          Thomas Boquet <thomas.boquet@gmail.com>
8
 *          Clement Chavance <chavance.c@gmail.com>
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
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.
 *****************************************************************************/

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

Thomas Boquet's avatar
Thomas Boquet committed
27
#include "ClipRenderer.h"
28
#include "ImportController.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
29
#include "Library.h"
30 31 32

#include <QPalette>
#include <QTime>
Thomas Boquet's avatar
Thomas Boquet committed
33 34 35

ImportController::ImportController(QWidget *parent) :
    QDialog(parent),
36 37 38
    m_ui(new Ui::ImportController),
    m_clipListController( 0 ),
    m_controllerSwitched( false )
Thomas Boquet's avatar
Thomas Boquet committed
39 40 41 42 43 44 45 46 47 48
{
    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 );
    m_filesModel = new QDirModel();
    m_stackNav->pushViewController( m_mediaListController );

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

59
    Library::getInstance()->setFilter( filters );
Thomas Boquet's avatar
Thomas Boquet committed
60 61 62 63 64 65 66 67 68 69 70 71 72
    m_ui->treeView->setModel( m_filesModel );
    m_ui->treeView->setRootIndex( m_filesModel->index( QDir::rootPath() ) );
    m_ui->treeView->setCurrentIndex( m_filesModel->index( QDir::homePath() ) );
    m_ui->treeView->setExpanded( m_ui->treeView->currentIndex() , true );
    m_ui->treeView->setColumnHidden( 1, true );
    m_ui->treeView->setColumnHidden( 2, true );
    m_ui->treeView->setColumnHidden( 3, true );
    m_ui->forwardButton->setEnabled( false );

    m_fsWatcher = new QFileSystemWatcher();
    m_fsWatcher->addPath( QDir::homePath() );
    m_currentlyWatchedDir = QDir::homePath();

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    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( Library::getInstance(), SIGNAL( newMediaLoaded( const QUuid& ) ),
             this, SLOT( newMediaLoaded( const QUuid& ) ) );
    connect( Library::getInstance(), SIGNAL( updateMediaRequested( const QUuid& ) ),
             this, SLOT( updateMediaRequested( const QUuid& ) ) );

    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
103 104 105 106 107 108 109 110 111
}

ImportController::~ImportController()
{
    delete m_ui;
    delete m_stackNav;
    delete m_tag;
}

112 113
void
ImportController::changeEvent( QEvent *e )
Thomas Boquet's avatar
Thomas Boquet committed
114 115
{
    QDialog::changeEvent( e );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
116 117 118 119 120 121 122
    switch ( e->type() )
    {
        case QEvent::LanguageChange:
            m_ui->retranslateUi( this );
            break;
        default:
            break;
Thomas Boquet's avatar
Thomas Boquet committed
123 124 125
    }
}

126 127
void
ImportController::mediaSelection( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
128
{
129 130 131
    if ( m_mediaListController->contains( uuid ) )
    {
        if ( !m_currentUuid.isNull() && m_mediaListController->contains( m_currentUuid ) )
132 133
            m_mediaListController->cell( m_currentUuid )->setPalette( this->palette() );
        QPalette p = m_mediaListController->cell( uuid )->palette();
134
        p.setColor( QPalette::Window, QColor( Qt::darkBlue ) );
135
        m_mediaListController->cell( uuid )->setPalette( p );
136

137
        setUIMetaData( Library::getInstance()->temporaryMedia( uuid ) );
138 139 140
        if ( uuid != NULL && uuid != m_currentUuid )
            m_preview->stop();
        m_currentUuid = uuid;
141
        emit mediaSelected( Library::getInstance()->temporaryMedia( uuid ) );
142
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
143 144
}

145 146
void
ImportController::clipSelection( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
147
{
148
    if ( !m_currentUuid.isNull() && !m_controllerSwitched )
149
        m_clipListController->cell( m_currentUuid )->setPalette( palette() );
150 151
    else
        m_controllerSwitched = false;
152
    QPalette p = m_clipListController->cell( uuid )->palette();
153
    p.setColor( QPalette::Window, QColor( Qt::darkBlue ) );
154
    m_clipListController->cell( uuid )->setPalette( p );
155
    Clip*   clip;
156
    foreach(QUuid id, Library::getInstance()->medias()->keys() )
157
    {
158
        Media* media = Library::getInstance()->medias()->value( id );
159 160
        if ( ( clip = media->clip( uuid ) ) != 0 )
            break;
161
    }
162
    setUIMetaData( clip );
163 164 165 166
    if ( uuid != m_currentUuid )
        m_preview->stop();
    emit clipSelected( clip );
    m_currentUuid = uuid;
Thomas Boquet's avatar
Thomas Boquet committed
167 168
}

169 170
void
ImportController::newMediaLoaded( const QUuid& uuid )
Thomas Boquet's avatar
Thomas Boquet committed
171
{
172 173 174 175
    Media* media = Library::getInstance()->temporaryMedia( uuid );
    if ( media == NULL )
        return;
    m_mediaListController->addMedia( media );
Thomas Boquet's avatar
Thomas Boquet committed
176
}
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
void
ImportController::updateMediaRequested( const QUuid& uuid )
{
    Media* media = Library::getInstance()->temporaryMedia( uuid );
    if ( media == NULL )
        return;
    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
193
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
194 195 196 197
    if ( media != NULL )
    {
        //Duration
        QTime   duration;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
198
        duration = duration.addMSecs( media->getLengthMS() );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
199 200 201 202 203 204 205
        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() )
206
                                        + " x " + QString::number( media->getHeight() ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
207 208 209 210 211 212 213 214 215 216
        //FPS
        m_ui->fpsValueLabel->setText( QString::number( media->getFps() ) );
    }
    else
    {
            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
217 218
}

219 220
void
ImportController::setUIMetaData( Clip* clip )
221 222 223 224 225 226 227 228
{
    QTime   time;
    qint64  length = clip->getLengthSecond();
    time = time.addSecs( length );
    qDebug() << time;
    m_ui->durationValueLabel->setText( time.toString( "hh:mm:ss" ) );
    m_ui->nameValueLabel->setText( clip->getParent()->getFileInfo()->fileName() );
    m_ui->nameValueLabel->setWordWrap( true );
229 230
    setWindowTitle( clip->getParent()->getFileInfo()->fileName() + " " +
                    tr( "properties" ) );
231 232 233 234
    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
235

236 237
void
ImportController::forwardButtonClicked()
Thomas Boquet's avatar
Thomas Boquet committed
238
{
239 240 241
    QModelIndex index = m_ui->treeView->selectionModel()->currentIndex();
    QString filePath =  m_filesModel->fileInfo( index ).filePath();
    Library::getInstance()->loadFile( filePath );
Thomas Boquet's avatar
Thomas Boquet committed
242 243
}

244 245
void
ImportController::treeViewClicked( const QModelIndex& index )
Thomas Boquet's avatar
Thomas Boquet committed
246 247 248 249 250 251 252 253 254 255
{
    if ( m_filesModel->isDir( index ) )
    {
        m_fsWatcher->removePath( m_currentlyWatchedDir );
        m_currentlyWatchedDir = m_filesModel->filePath( index );
        m_fsWatcher->addPath( m_filesModel->filePath( index ) );
    }
    m_ui->forwardButton->setEnabled( true );
}

256 257
void
ImportController::treeViewDoubleClicked( const QModelIndex& index )
Thomas Boquet's avatar
Thomas Boquet committed
258 259 260 261
{
    if ( !m_filesModel->isDir( index ) )
        forwardButtonClicked();
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
262

263 264
void
ImportController::reject()
265 266
{
    m_preview->stop();
267 268
    m_mediaListController->cleanAll();
    Library::getInstance()->deleteTemporaryMedias();
269
    done( Rejected );
270 271
}

272 273
void
ImportController::accept()
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
274
{
275 276
    m_mediaListController->cleanAll();
    Library::getInstance()->importDone();
277
    m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
278 279
    done( Accepted );
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
280

281 282
void
ImportController::mediaDeletion( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
283
{
284
    Library::getInstance()->deleteMedia( uuid );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
285
    m_mediaListController->removeMedia( uuid );
286

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
287
    if ( uuid == m_currentUuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
288
    {
289
        setUIMetaData( static_cast<Media*>( 0 ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
290
        m_currentUuid = QUuid();
291
        m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
292
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
293 294
}

295 296
void
ImportController::clipDeletion( const QUuid& uuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
297
{
298
    m_clipListController->removeClip( uuid );
299
    QUuid id;
300
    foreach( id, Library::getInstance()->temporaryMedias()->keys() )
301
    {
302 303
        Media* media = Library::getInstance()->temporaryMedias()->value( id );
        if ( media != NULL && media->clip( uuid ) )
304 305
            media->removeClip( uuid );
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
306
}
307

308 309
void
ImportController::showClipList( const QUuid& uuid )
310
{
311 312
    Media* media = Library::getInstance()->temporaryMedia( uuid );
    if ( media == NULL || media->clips()->size() == 0 )
313
        return ;
314

315
    m_clipListController = new ImportMediaListController( m_stackNav );
316 317 318 319
    connect( m_clipListController, SIGNAL( clipSelected( const QUuid& ) ),
             this, SLOT( clipSelection( const QUuid& ) ) );
    connect( m_clipListController, SIGNAL( clipDeleted( const QUuid& ) ),
             this, SLOT( clipDeletion( const QUuid& ) ) );
320 321 322 323 324 325 326
    m_clipListController->addClipsFromMedia( media );
    if ( !m_currentUuid.isNull() )
        m_savedUuid = m_currentUuid;
    m_controllerSwitched = true;
    m_stackNav->pushViewController( m_clipListController );
}

327 328
void
ImportController::restoreContext()
329
{
330
    if ( m_clipListController->nbDeletions() != 0 )
331
    {
332 333 334 335 336
        if ( !m_savedUuid.isNull() && m_mediaListController->cell( m_savedUuid ) != NULL )
        {
            ImportMediaCellView* cell = m_mediaListController->cell( m_savedUuid );
            cell->decrementClipCount( m_clipListController->nbDeletions() );
        }
337
    }
338 339 340
    if ( !m_savedUuid.isNull() )
        m_currentUuid = m_savedUuid;
    m_controllerSwitched = false;
341
}