ImportController.cpp 11.3 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 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.
 *****************************************************************************/

#include <QTime>
Thomas Boquet's avatar
Thomas Boquet committed
26 27 28
#include "ImportController.h"
#include "ui_ImportController.h"
#include "ClipRenderer.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
29
#include "Library.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
30
#include "QPalette"
Thomas Boquet's avatar
Thomas Boquet committed
31 32 33

ImportController::ImportController(QWidget *parent) :
    QDialog(parent),
34 35 36
    m_ui(new Ui::ImportController),
    m_clipListController( 0 ),
    m_controllerSwitched( false )
Thomas Boquet's avatar
Thomas Boquet committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
{
    m_ui->setupUi(this);

    m_model = new ImportModel();
    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;
    //Video
51
    filters << "*.mov" << "*.avi" << "*.mkv" << "*.mpg" << "*.mpeg" << "*.wmv" << "*.mp4" << "*.ogg" << "*.ogv";
Thomas Boquet's avatar
Thomas Boquet committed
52 53 54 55 56 57 58 59 60 61
    //Audio
    filters << "*.mp3" << "*.oga" << "*.flac" << "*.aac" << "*.wav";
    //Picture
    filters << "*.gif" << "*.png" << "*.jpg";

    m_filesModel->setFilter( QDir::AllDirs | QDir::Files | QDir::Readable | QDir::NoDotAndDotDot );
    m_filesModel->sort( 2, Qt::AscendingOrder );
    m_filesModel->sort( 0, Qt::AscendingOrder );
    m_filesModel->setNameFilters( filters );

62
    m_model->setFilter( filters );
Thomas Boquet's avatar
Thomas Boquet committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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();

    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( m_model, SIGNAL( newMediaLoaded( Media* ) ), this, SLOT( newMediaLoaded( Media* ) ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
82
    connect( m_model, SIGNAL( updateMediaRequested( Media* ) ), this, SLOT( updateMediaRequested( Media* ) ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
83 84 85

    connect( this, SIGNAL( mediaSelected( Media* ) ), m_preview->getGenericRenderer(), SLOT( setMedia( Media* ) ) );
    connect( this, SIGNAL( mediaSelected( Media* ) ), m_tag, SLOT( mediaSelected( Media* ) ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
86

87
    //Media
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
88 89
    connect( m_mediaListController, SIGNAL( mediaSelected( QUuid ) ), this, SLOT( mediaSelection( QUuid ) ) );
    connect( m_mediaListController, SIGNAL( mediaDeleted( QUuid ) ), this, SLOT( mediaDeletion( QUuid ) ) );
90 91
    //Clips
    connect( m_mediaListController, SIGNAL( showClipListAsked( const QUuid& ) ), this, SLOT( showClipList( const QUuid& ) ) );
92
    connect( m_preview, SIGNAL( addClip( Clip* ) ), m_mediaListController, SLOT( clipAdded( Clip* ) ) );
93 94
    //StackViewController
    connect( m_stackNav, SIGNAL( previousButtonPushed() ), this, SLOT( restoreContext() ) );
Thomas Boquet's avatar
Thomas Boquet committed
95 96 97 98 99 100 101 102 103 104 105 106 107
}

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

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

void        ImportController::newMediaLoaded( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
120
    m_mediaListController->addMedia( media );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
121 122 123 124
}

void        ImportController::mediaSelection( const QUuid& uuid )
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138
    if ( m_mediaListController->contains( uuid ) )
    {
        if ( !m_currentUuid.isNull() && m_mediaListController->contains( m_currentUuid ) )
            m_mediaListController->getCell( m_currentUuid )->setPalette( palette() );
        QPalette p = m_mediaListController->getCell( uuid )->palette();
        p.setColor( QPalette::Window, QColor( Qt::darkBlue ) );
        m_mediaListController->getCell( uuid )->setPalette( p );

        setUIMetaData( m_model->getMedia( uuid ) );
        if ( uuid != NULL && uuid != m_currentUuid )
            m_preview->stop();
        m_currentUuid = uuid;
        emit mediaSelected( m_model->getMedia( uuid ) );
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
139 140 141 142
}

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

void        ImportController::updateMediaRequested( Media* media )
{
166 167 168 169 170 171
    if ( m_mediaListController->contains( media->getUuid() ) )
    {
        ImportMediaCellView*    cell = m_mediaListController->getCell( media->getUuid() );
        cell->setThumbnail( media->getSnapshot() );
        cell->setLength( media->getLengthMS() );
    }
Thomas Boquet's avatar
Thomas Boquet committed
172
}
173

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
174 175
void    ImportController::setUIMetaData( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
176 177 178 179
    if ( media != NULL )
    {
        //Duration
        QTime   duration;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
180
        duration = duration.addMSecs( media->getLengthMS() );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        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() )
                                           + " x " + QString::number( media->getHeight() ) );
        //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
199 200
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
void        ImportController::setUIMetaData( Clip* clip )
{
    //compute clip length
    QTime   time;
    qint64  length = clip->getLengthSecond();
    time = time.addSecs( length );
    qDebug() << time;
    m_ui->durationValueLabel->setText( time.toString( "hh:mm:ss" ) );
    //Filename || title
    m_ui->nameValueLabel->setText( clip->getParent()->getFileInfo()->fileName() );
    m_ui->nameValueLabel->setWordWrap( true );
    setWindowTitle( clip->getParent()->getFileInfo()->fileName() + " " + tr( "properties" ) );
    //Resolution
    m_ui->resolutionValueLabel->setText( QString::number( clip->getParent()->getWidth() )
            + " x " + QString::number( clip->getParent()->getHeight() ) );
    //FPS
    m_ui->fpsValueLabel->setText( QString::number( clip->getParent()->getFps() ) );
}
Thomas Boquet's avatar
Thomas Boquet committed
219 220 221

void    ImportController::forwardButtonClicked()
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
222
    m_model->loadFile( m_filesModel->fileInfo( m_ui->treeView->selectionModel()->currentIndex() ).filePath() );
Thomas Boquet's avatar
Thomas Boquet committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

void    ImportController::treeViewClicked( const QModelIndex& index )
{
    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 );
}

void    ImportController::treeViewDoubleClicked( const QModelIndex& index )
{
    if ( !m_filesModel->isDir( index ) )
        forwardButtonClicked();
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
241

242 243 244
void    ImportController::reject()
{
    m_preview->stop();
245
    m_model->removeAllMedias();
246
    done( Rejected );
247 248
}

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
249 250 251 252 253 254 255 256
void    ImportController::accept()
{
    QUuid id;
    foreach( id, m_model->getMedias()->keys() )
    {
        Media* media = m_model->getMedias()->value( id );
        Library::getInstance()->addMedia( media );
    }
257
    m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
258 259
    done( Accepted );
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
260 261 262 263

void        ImportController::mediaDeletion( const QUuid& uuid )
{
    m_mediaListController->removeMedia( uuid );
264 265
    m_model->removeMedia( uuid );

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
266
    if ( uuid == m_currentUuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
267
    {
268
        setUIMetaData( static_cast<Media*>( 0 ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
269
        m_currentUuid = QUuid();
270
        m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
271
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
272 273 274 275
}

void        ImportController::clipDeletion( const QUuid& uuid )
{
276
    m_clipListController->removeClip( uuid );
277
    QUuid id;
278
    foreach( id, m_model->getMedias()->keys() )
279 280
    {
        Media* media = m_model->getMedias()->value( id );
281 282
        if ( media->clip( uuid ) )
        {
283
            media->removeClip( uuid );
284
        }
285
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
286
}
287 288 289 290 291 292 293

void        ImportController::showClipList( const QUuid& uuid )
{
    Media*  media = m_model->getMedia( uuid );

    if ( media->clips()->size() == 0 )
        return ;
294 295 296 297 298 299 300 301 302 303 304 305
    m_clipListController = new ImportMediaListController( m_stackNav );
    connect( m_clipListController, SIGNAL( clipSelected( const QUuid& ) ), this, SLOT( clipSelection( const QUuid& ) ) );
    connect( m_clipListController, SIGNAL( clipDeleted( const QUuid& ) ), this, SLOT( clipDeletion( const QUuid& ) ) );
    m_clipListController->addClipsFromMedia( media );
    if ( !m_currentUuid.isNull() )
        m_savedUuid = m_currentUuid;
    m_controllerSwitched = true;
    m_stackNav->pushViewController( m_clipListController );
}

void        ImportController::restoreContext()
{
306 307 308 309 310
    if ( m_clipListController->getNbDeletions() != 0 )
    {
        if ( !m_savedUuid.isNull() )
            m_mediaListController->getCell( m_savedUuid )->decrementClipCount( m_clipListController->getNbDeletions() );
    }
311 312 313
    if ( !m_savedUuid.isNull() )
        m_currentUuid = m_savedUuid;
    m_controllerSwitched = false;
314
}