[vlmc-devel] Fix recent projects list.

Hugo Beauzée-Luyssen git at videolan.org
Sun Mar 30 17:48:41 CEST 2014


vlmc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Sun Mar 30 18:47:18 2014 +0300| [148a2cd425ce18f053522178f5be605c1e320b6a] | committer: Hugo Beauzée-Luyssen

Fix recent projects list.

It is now based on the project path, and therefor supports projects with
the same name.
This will need to go away from ProjectManager though, as it's a VLMC
wide notion, and does not belong in a project specific class

> http://git.videolan.org/gitweb.cgi/vlmc.git/?a=commit;h=148a2cd425ce18f053522178f5be605c1e320b6a
---

 src/Gui/MainWindow.cpp         |    3 +-
 src/Gui/wizard/WelcomePage.cpp |    9 ++---
 src/Project/ProjectManager.cpp |   80 ++++++++++++++++++++++++++++++++++------
 src/Project/ProjectManager.h   |   27 ++++++++++----
 src/Settings/SettingValue.h    |    1 -
 5 files changed, 94 insertions(+), 26 deletions(-)

diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp
index 3fd6fd0..db65d8e 100644
--- a/src/Gui/MainWindow.cpp
+++ b/src/Gui/MainWindow.cpp
@@ -324,7 +324,8 @@ MainWindow::initVlmcPreferences()
     VLMC_CREATE_PRIVATE_PREFERENCE_STRING( "private/ImportPreviouslySelectPath", QDir::homePath() );
     VLMC_CREATE_PRIVATE_PREFERENCE_BYTEARRAY( "private/MainWindowGeometry", "" );
     VLMC_CREATE_PRIVATE_PREFERENCE_BYTEARRAY( "private/MainWindowState", "" );
-    VLMC_CREATE_PRIVATE_PREFERENCE_STRING( "private/RecentsProjects", "" );
+    Core::getInstance()->settings()->createVar( SettingValue::String, "private/RecentsProjects", "",
+                                                "", "", SettingValue::Private );
 
     Core::getInstance()->settings()->createVar( SettingValue::Bool, "vlmc/AutomaticBackup", false,
                                  QT_TRANSLATE_NOOP( "PreferenceWidget", "Automatic save" ),
diff --git a/src/Gui/wizard/WelcomePage.cpp b/src/Gui/wizard/WelcomePage.cpp
index 521bf68..9872ad5 100644
--- a/src/Gui/wizard/WelcomePage.cpp
+++ b/src/Gui/wizard/WelcomePage.cpp
@@ -119,14 +119,13 @@ WelcomePage::loadRecentsProjects()
 {
     m_ui.projectsListWidget->clear();
     ProjectManager* pm = Project::getInstance()->projectManager();
-    QStringList recents = pm->recentsProjects();
+    const ProjectManager::RecentProjectsList& recents = pm->recentsProjects();
 
     for ( int i = 0; i < recents.count(); ++i )
     {
-        QFileInfo fi( recents.at( i ) );
-        QListWidgetItem* item = new QListWidgetItem( fi.fileName() );
-        item->setData( FilePath, fi.absoluteFilePath() );
-
+        ProjectManager::RecentProject project = recents.at( i );
+        QListWidgetItem* item = new QListWidgetItem( project.name );
+        item->setData( FilePath, project.filePath );
         m_ui.projectsListWidget->addItem( item );
     }
 }
diff --git a/src/Project/ProjectManager.cpp b/src/Project/ProjectManager.cpp
index be933d2..0a1acad 100644
--- a/src/Project/ProjectManager.cpp
+++ b/src/Project/ProjectManager.cpp
@@ -104,6 +104,7 @@ ProjectManager::ProjectManager( Settings* projectSettings, Settings* vlmcSetting
 
     projectSettings->watchValue( "vlmc/ProjectName", this, SLOT(projectNameChanged(QVariant) ) );
 
+    loadRecentProjects();
     //We have to wait for the library to be loaded before loading the workflow
     //FIXME
     //connect( Project::getInstance()->library(), SIGNAL( projectLoaded() ), this, SLOT( loadWorkflow() ) );
@@ -122,7 +123,7 @@ ProjectManager::setProjectManagerUi( IProjectManagerUiCb *projectManagerUi )
 }
 
 
-QStringList
+const ProjectManager::RecentProjectsList&
 ProjectManager::recentsProjects() const
 {
     return m_recentsProjects;
@@ -138,7 +139,7 @@ ProjectManager::loadWorkflow()
     loadTimeline( root );
     if ( m_projectFile != NULL )
     {
-        appendToRecentProject( projectName() );
+        appendToRecentProject( projectName(), m_projectFile->fileName() );
         savedState = true;
     }
     else
@@ -149,12 +150,10 @@ ProjectManager::loadWorkflow()
 }
 
 void
-ProjectManager::removeProject( const QString& project )
+ProjectManager::removeProject( const QString& projectPath )
 {
-    // Remove all occurence of fileName
-    m_recentsProjects.removeAll( project );
-
-    Core::getInstance()->settings()->setValue( SETTINGS_RECENTS, m_recentsProjects );
+    removeFromRecentProjects( projectPath );
+    Core::getInstance()->settings()->setValue( SETTINGS_RECENTS, flattenProjectList() );
 }
 
 void
@@ -221,15 +220,46 @@ ProjectManager::createAutoSaveOutputFileName( const QString& baseName ) const
 }
 
 void
-ProjectManager::appendToRecentProject( const QString& projectName )
+ProjectManager::removeFromRecentProjects(const QString &projectPath)
+{
+    RecentProjectsList::iterator it = m_recentsProjects.begin();
+    RecentProjectsList::iterator ite = m_recentsProjects.end();
+    while ( it != ite )
+    {
+        if ( (*it).filePath == projectPath )
+            it = m_recentsProjects.erase( it );
+        else
+            ++it;
+    }
+}
+
+QString
+ProjectManager::flattenProjectList() const
+{
+    if ( m_recentsProjects.count() == 0 )
+        return QString();
+    QString     res;
+    foreach (RecentProject p, m_recentsProjects)
+    {
+        res += p.name + '#' + p.filePath + '#';
+    }
+    res.chop(1);
+    return res;
+}
+
+void
+ProjectManager::appendToRecentProject( const QString& projectName, const QString& projectPath )
 {
     // Append the item to the recents list
-    m_recentsProjects.removeAll( projectName );
-    m_recentsProjects.prepend( projectName );
+    removeFromRecentProjects( projectName );
+    RecentProject project;
+    project.name = projectName;
+    project.filePath = projectPath;
+    m_recentsProjects.prepend( project );
     while ( m_recentsProjects.count() > 15 )
         m_recentsProjects.removeLast();
 
-    Core::getInstance()->settings()->setValue( SETTINGS_RECENTS, m_recentsProjects );
+    Core::getInstance()->settings()->setValue( SETTINGS_RECENTS, flattenProjectList() );
     Core::getInstance()->settings()->save();
 }
 
@@ -343,7 +373,7 @@ ProjectManager::newProject( const QString &projectName, const QString &workspace
     //Current project file has already been delete/nulled by ProjectManager::closeProject()
     m_projectFile = new QFile( workspacePath + '/' + "project.vlmc" );
     save();
-    appendToRecentProject( projectName );
+    appendToRecentProject( projectName, m_projectFile->fileName() );
 }
 
 void
@@ -428,6 +458,32 @@ ProjectManager::autoSaveRequired()
     ProjectManager::saveProject( createAutoSaveOutputFileName( m_projectFile->fileName() ) );
 }
 
+void
+ProjectManager::loadRecentProjects()
+{
+    QString recentProjects = m_vlmcSettings->value( SETTINGS_RECENTS )->get().toString();
+    const QStringList               recentProjectsList = recentProjects.split( '#' );
+    if ( recentProjectsList.count() == 0 )
+        return ;
+    QStringList::const_iterator     it = recentProjectsList.begin();
+    QStringList::const_iterator     ite = recentProjectsList.end();
+    m_recentsProjects.clear();
+    while ( it != ite )
+    {
+        RecentProject project;
+        project.name = *it;
+        ++it;
+        if ( it == ite )
+        {
+            vlmcWarning() << "Invalid flattened recent projects list.";
+            return ;
+        }
+        project.filePath = *it;
+        ++it;
+        m_recentsProjects.append( project );
+    }
+}
+
 bool
 ProjectManager::closeProject()
 {
diff --git a/src/Project/ProjectManager.h b/src/Project/ProjectManager.h
index 79d1cdf..575d0f0 100644
--- a/src/Project/ProjectManager.h
+++ b/src/Project/ProjectManager.h
@@ -83,16 +83,23 @@ class   ProjectManager : public QObject
     Q_DISABLE_COPY( ProjectManager )
 
 public:
-    static const QString    unNamedProject;
-    static const QString    unSavedProject;
-    static const QString    backupSuffix;
+    static const QString            unNamedProject;
+    static const QString            unSavedProject;
+    static const QString            backupSuffix;
+    struct RecentProject
+    {
+        QString name;
+        QString filePath;
+    };
+
+    typedef QList<RecentProject>    RecentProjectsList;
 
     ProjectManager( Settings *projectSettings , Settings *vlmcSettings );
     ~ProjectManager();
 
     void            setProjectManagerUi( IProjectManagerUiCb* projectManagerUi );
-    void            removeProject( const QString& fileName );
-    QStringList     recentsProjects() const;
+    void            removeProject( const QString& projectPath );
+    const RecentProjectsList&   recentsProjects() const;
     bool            closeProject();
 
     void            save();
@@ -119,7 +126,7 @@ private:
      */
     void            saveTimeline(QXmlStreamWriter& project);
     static bool     isBackupFile( const QString& projectFile );
-    void            appendToRecentProject( const QString& projectName );
+    void            appendToRecentProject(const QString& projectName , const QString &projectPath);
     /**
      *  \brief      Get the project name
      *
@@ -134,10 +141,16 @@ private:
     void            failedToLoad( const QString& reason ) const;
     void            loadTimeline( const QDomElement& ){}
     QString         createAutoSaveOutputFileName( const QString& baseName ) const;
+    void            removeFromRecentProjects( const QString& projectPath );
+    QString         flattenProjectList() const;
+    void            loadRecentProjects();
+
 
 protected:
     QFile*                  m_projectFile;
-    QStringList             m_recentsProjects;
+    // We list recent projects as a list of [ProjectName,ProjectPath].
+    // Since this is handled as a QVariant, arrays don't work.
+    RecentProjectsList      m_recentsProjects;
     QString                 m_projectName;
     QString                 m_projectDescription;
     QDomDocument*           m_domDocument;
diff --git a/src/Settings/SettingValue.h b/src/Settings/SettingValue.h
index d9103ed..60f1bea 100644
--- a/src/Settings/SettingValue.h
+++ b/src/Settings/SettingValue.h
@@ -63,7 +63,6 @@ class   SettingValue : public QObject
             Runtime         = 1 << 5, ///< Defines a variable that is not meant to be saved
         };
         Q_DECLARE_FLAGS( Flags, Flag );
-
         /**
          *  \brief      Constructs a setting value with its default value and description
          *



More information about the Vlmc-devel mailing list