ImportController.cpp 7.92 KB
Newer Older
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*****************************************************************************
 * ImportController.cpp
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Geoffroy Lacarriere <geoffroylaca@gmail.com>
 *          Thomas Boquet <thomas.boquet@gmail.com>
 *
 * 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
25
26
27
#include "ImportController.h"
#include "ui_ImportController.h"
#include "ClipRenderer.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
28
#include "Library.h"
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
29
#include "QPalette"
Thomas Boquet's avatar
Thomas Boquet committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

ImportController::ImportController(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::ImportController)
{
    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
    filters << "*.mov" << "*.avi" << "*.mkv" << "*.mpg" << "*.mpeg" << "*.wmv" << "*.mp4";
    //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 );

59
    m_model->setFilter( filters );
Thomas Boquet's avatar
Thomas Boquet committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    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
79
    connect( m_model, SIGNAL( updateMediaRequested( Media* ) ), this, SLOT( updateMediaRequested( Media* ) ) );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
80
81
82

    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
83
84
85

    connect( m_mediaListController, SIGNAL( mediaSelected( QUuid ) ), this, SLOT( mediaSelection( QUuid ) ) );
    connect( m_mediaListController, SIGNAL( mediaDeleted( QUuid ) ), this, SLOT( mediaDeletion( QUuid ) ) );
Thomas Boquet's avatar
Thomas Boquet committed
86
87
88
89
90
91
92
93
94
95
96
97
98
}

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
99
100
101
102
103
104
105
    switch ( e->type() )
    {
        case QEvent::LanguageChange:
            m_ui->retranslateUi( this );
            break;
        default:
            break;
Thomas Boquet's avatar
Thomas Boquet committed
106
107
108
109
110
    }
}

void        ImportController::newMediaLoaded( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
111
    m_mediaListController->addMedia( media );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
112
113
114
115
}

void        ImportController::mediaSelection( const QUuid& uuid )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
116
117
118
119
120
121
122
123
124
    if ( !m_currentUuid.isNull() )
        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 ) );
    emit mediaSelected( m_model->getMedia( uuid ) );
    m_currentUuid = uuid;
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
125
126
127
128
129
}

void        ImportController::clipSelection( const QUuid& uuid )
{
    Q_UNUSED( uuid );
Thomas Boquet's avatar
Thomas Boquet committed
130
131
132
133
}

void        ImportController::updateMediaRequested( Media* media )
{
134
135
136
    ImportMediaCellView*    cell = m_mediaListController->getCell( media->getUuid() );
    cell->setThumbnail( media->getSnapshot() );
    cell->setLength( media->getLengthMS() );
Thomas Boquet's avatar
Thomas Boquet committed
137
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
138
139
void    ImportController::setUIMetaData( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
140
141
142
143
    if ( media != NULL )
    {
        //Duration
        QTime   duration;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
144
        duration = duration.addMSecs( media->getLengthMS() );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        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
163
164
}

Thomas Boquet's avatar
Thomas Boquet committed
165
166
167

void    ImportController::forwardButtonClicked()
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
168
    m_model->loadFile( m_filesModel->fileInfo( m_ui->treeView->selectionModel()->currentIndex() ).filePath() );
Thomas Boquet's avatar
Thomas Boquet committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
}

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
187

188
189
190
void    ImportController::reject()
{
    m_preview->stop();
191
    done( Rejected );
192
193
}

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
194
195
196
197
198
199
200
201
void    ImportController::accept()
{
    QUuid id;
    foreach( id, m_model->getMedias()->keys() )
    {
        Media* media = m_model->getMedias()->value( id );
        Library::getInstance()->addMedia( media );
    }
202
    m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
203
204
    done( Accepted );
}
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
205
206
207
208

void        ImportController::mediaDeletion( const QUuid& uuid )
{
    m_mediaListController->removeMedia( uuid );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
209
    delete m_model->getMedias()->take( uuid );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
210
    if ( uuid == m_currentUuid )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
211
    {
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
212
        setUIMetaData( NULL );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
213
        m_currentUuid = QUuid();
214
        m_preview->stop();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
215
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
216
217
218
219
}

void        ImportController::clipDeletion( const QUuid& uuid )
{
220
221
222
223
224
    m_mediaListController->removeClip( uuid );
    QUuid id;
    foreach(id, m_model->getMedias()->keys() )
    {
        Media* media = m_model->getMedias()->value( id );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
225
        if (media->clip( uuid) )
226
227
            media->removeClip( uuid );
    }
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
228
}