ProjectManager.cpp 4.06 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * ProjectManager.cpp: Manager the project loading and saving.
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * 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.
 *****************************************************************************/

23
#include <QFileDialog>
24
#include <QtDebug>
25

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
26 27 28
#include "ProjectManager.h"
#include "Library.h"
#include "MainWorkflow.h"
29
#include "SettingsManager.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30

31
ProjectManager::ProjectManager() : m_projectFile( NULL ), m_needSave( false )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
32 33 34 35 36
{
}

ProjectManager::~ProjectManager()
{
37 38
    if ( m_projectFile != NULL )
        delete m_projectFile;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39 40
}

41 42 43 44 45 46 47 48 49 50
bool    ProjectManager::needSave() const
{
    return m_needSave;
}

void    ProjectManager::cleanChanged( bool val )
{
    m_needSave = !val;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
51 52 53 54 55 56 57 58
void    ProjectManager::loadTimeline()
{
    QDomElement     root = m_domDocument->documentElement();
    MainWorkflow::getInstance()->loadProject( root.firstChildElement( "timeline" ) );
}

void    ProjectManager::loadProject()
{
59 60
    if ( loadProjectFile() == false )
        return ;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61 62 63 64 65 66 67 68 69
    m_domDocument = new QDomDocument;
    m_projectFile->open( QFile::ReadOnly );
    m_domDocument->setContent( m_projectFile );
    m_projectFile->close();

    QDomElement     root = m_domDocument->documentElement();

    connect( Library::getInstance(), SIGNAL( projectLoaded() ), this, SLOT( loadTimeline() ) );
    Library::getInstance()->loadProject( root.firstChildElement( "medias" ) );
70
    SettingsManager::getInstance()->loadSettings( "project", root.firstChildElement( "project" ) );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
71 72
}

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
bool    ProjectManager::loadProjectFile()
{
    QString fileName =
            QFileDialog::getOpenFileName( NULL, "Enter the output file name",
                                          QString(), "VLMC project file(*.vlmc)" );
    if ( fileName.length() == 0 )
        return false;
    if ( m_projectFile != NULL )
        delete m_projectFile;
    m_projectFile = new QFile( fileName );
    return true;
}

bool    ProjectManager::checkProjectOpen()
{
    if ( m_projectFile == NULL )
    {
        QString outputFileName =
            QFileDialog::getSaveFileName( NULL, "Enter the output file name",
                                          QString(), "VLMC project file(*.vlmc)" );
        if ( outputFileName.length() == 0 )
            return false;
        if ( m_projectFile != NULL )
            delete m_projectFile;
        m_projectFile = new QFile( outputFileName );
    }
    return true;
}

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
102 103
void    ProjectManager::saveProject()
{
104 105
    if ( checkProjectOpen() == false )
        return ;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
106 107 108 109 110 111 112 113 114 115 116
    QDomImplementation    implem = QDomDocument().implementation();
    //FIXME: Think about naming the project...
    QString name = "VLMCProject";
    QString publicId = "-//XADECK//DTD Stone 1.0 //EN";
    QString systemId = "http://www-imagis.imag.fr/DTD/stone1.dtd";
    QDomDocument doc(implem.createDocumentType( name, publicId, systemId ) );

    QDomElement rootNode = doc.createElement( "vlmc" );

    Library::getInstance()->saveProject( doc, rootNode );
    MainWorkflow::getInstance()->saveProject( doc, rootNode );
117
    SettingsManager::getInstance()->saveSettings( "project", doc, rootNode );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
118 119 120 121 122 123 124

    doc.appendChild( rootNode );

    m_projectFile->open( QFile::WriteOnly );
    m_projectFile->write( doc.toString().toAscii() );
    m_projectFile->close();
}