[vlmc-devel] [PATCH] RecentProjects: Use QVariantList to store m_recentsProjects

Yikai Lu luyikei.qmltu at gmail.com
Sun Apr 10 14:53:35 CEST 2016


---
 src/Gui/wizard/WelcomePage.cpp |  10 ++--
 src/Project/RecentProjects.cpp | 103 +++++++++--------------------------------
 src/Project/RecentProjects.h   |  21 ++-------
 3 files changed, 30 insertions(+), 104 deletions(-)

diff --git a/src/Gui/wizard/WelcomePage.cpp b/src/Gui/wizard/WelcomePage.cpp
index a598383..6e44cb8 100644
--- a/src/Gui/wizard/WelcomePage.cpp
+++ b/src/Gui/wizard/WelcomePage.cpp
@@ -118,13 +118,13 @@ void
 WelcomePage::loadRecentsProjects()
 {
     m_ui.projectsListWidget->clear();
-    const RecentProjects::List& recents = Core::instance()->recentProjects()->list();
 
-    for ( int i = 0; i < recents.count(); ++i )
+    for ( const auto& var : Core::instance()->recentProjects()->toVariant().toList() )
     {
-        RecentProjects::RecentProject project = recents.at( i );
-        QListWidgetItem* item = new QListWidgetItem( project.name );
-        item->setData( FilePath, project.filePath );
+        const auto& name = var.toMap()["name"].toString();
+        const auto& filePath = var.toMap()["file"].toString();
+        QListWidgetItem* item = new QListWidgetItem( name );
+        item->setData( FilePath, filePath );
         m_ui.projectsListWidget->addItem( item );
     }
 }
diff --git a/src/Project/RecentProjects.cpp b/src/Project/RecentProjects.cpp
index 70c5a95..4985d5b 100644
--- a/src/Project/RecentProjects.cpp
+++ b/src/Project/RecentProjects.cpp
@@ -20,105 +20,46 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <QStringList>
-
 #include "RecentProjects.h"
-
-#include "Project/Project.h"
 #include "Settings/Settings.h"
-#include "Tools/VlmcDebug.h"
 
 RecentProjects::RecentProjects( Settings* vlmcSettings, QObject *parent )
     : QObject(parent)
-    , m_settings( vlmcSettings )
 {
-	SettingValue* recentProjects = vlmcSettings->createVar( SettingValue::String, "private/RecentsProjects", "",
+    m_recentsProjects = vlmcSettings->createVar( SettingValue::List, "private/RecentsProjects", QVariantList(),
                                                 "", "", SettingValue::Private );
-
-	connect( recentProjects, SIGNAL( changed( QVariant ) ), this, SLOT( loadRecentProjects( QVariant ) ) );
-}
-
-void
-RecentProjects::projectLoaded(const QString& projectName, const QString& projectFile)
-{
-    removeFromRecentProjects( projectName );
-    RecentProject project;
-    project.name = projectName;
-    project.filePath = projectFile;
-    m_recentsProjects.prepend( project );
-    while ( m_recentsProjects.count() > 15 )
-        m_recentsProjects.removeLast();
-
-    Core::instance()->settings()->setValue( "private/RecentsProjects", flattenProjectList() );
 }
 
-const RecentProjects::List&
-RecentProjects::list() const
+QVariant
+RecentProjects::toVariant() const
 {
-    return m_recentsProjects;
-}
-
-QString
-RecentProjects::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;
+    return QVariant( m_recentsProjects->get().toList() );
 }
 
 void
-RecentProjects::removeFromRecentProjects( const QString &projectPath )
+RecentProjects::remove( const QString &projectFile )
 {
-    List::iterator  it = m_recentsProjects.begin();
-    List::iterator  ite = m_recentsProjects.end();
-
-    while ( it != ite )
+    QVariantList l = m_recentsProjects->get().toList();
+    for ( int i = 0; i < l.count(); ++i )
     {
-        if ( (*it).filePath == projectPath )
-            it = m_recentsProjects.erase( it );
-        else
-            ++it;
+        if ( l[i].toMap()["file"].toString() == projectFile )
+        {
+            l.removeAt( i );
+            --i;
+        }
     }
+    m_recentsProjects->set( l );
 }
 
 void
-RecentProjects::remove( const QString& projectPath )
+RecentProjects::projectLoaded( const QString& projectName, const QString& projectFile )
 {
-    removeFromRecentProjects( projectPath );
-    Core::instance()->settings()->setValue( "private/RecentsProjects", flattenProjectList() );
-}
-
-void
-RecentProjects::loadRecentProjects( const QVariant& recentProjects )
-{
-    // Only watch initial loading, we are now taking ownership of "private/RecentsProjects settings"
-    disconnect( this, SLOT( loadRecentProjects( QVariant ) ) );
-
-    const QStringList   recentProjectsList = recentProjects.toString().split( '#' );
-
-    if ( recentProjectsList.count() == 0 )
-        return ;
-
-    QStringList::const_iterator     it = recentProjectsList.begin();
-    QStringList::const_iterator     ite = recentProjectsList.end();
-    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 );
-    }
+    QVariantList l = m_recentsProjects->get().toList();
+    QVariantMap var {
+        { "name", projectName },
+        { "file", projectFile }
+    };
+    l.removeAll( var );
+    l.insert( 0, var );
+    m_recentsProjects->set( l );
 }
diff --git a/src/Project/RecentProjects.h b/src/Project/RecentProjects.h
index eb2c64f..82f5c44 100644
--- a/src/Project/RecentProjects.h
+++ b/src/Project/RecentProjects.h
@@ -26,6 +26,7 @@
 #include <QObject>
 
 class   Project;
+class   SettingValue;
 class   Settings;
 
 class RecentProjects : public QObject
@@ -33,32 +34,16 @@ class RecentProjects : public QObject
     Q_OBJECT
 
     public:
-        struct RecentProject
-        {
-            QString name;
-            QString filePath;
-        };
-        typedef QList<RecentProject>      List;
-
         explicit RecentProjects(Settings* vlmcSettings, QObject *parent = 0 );
 
+        QVariant        toVariant() const;
         void            remove( const QString& projectFile );
-        const List&     list() const;
-
-    private:
-        void            removeFromRecentProjects( const QString &projectPath );
-        QString         flattenProjectList() const;
-
 
     public slots:
         void            projectLoaded( const QString& projectName, const QString& projectFile );
 
-    private slots:
-        void            loadRecentProjects(const QVariant& recentProjects);
-
     private:
-        Settings*       m_settings;
-        List            m_recentsProjects;
+        SettingValue*   m_recentsProjects;
 
 };
 
-- 
1.9.1



More information about the Vlmc-devel mailing list