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

Yikai Lu luyikei.qmltu at gmail.com
Sun Apr 10 09:33:06 CEST 2016


---
 src/Gui/wizard/WelcomePage.cpp | 10 ++---
 src/Project/RecentProjects.cpp | 95 ++++++++++--------------------------------
 src/Project/RecentProjects.h   | 20 +--------
 3 files changed, 28 insertions(+), 97 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 ec4df43..a14e83f 100644
--- a/src/Project/RecentProjects.cpp
+++ b/src/Project/RecentProjects.cpp
@@ -32,94 +32,41 @@ RecentProjects::RecentProjects( Settings* vlmcSettings, QObject *parent )
     : QObject(parent)
     , m_settings( new Settings )
 {
-    SettingValue* recentProjects = m_settings->createVar( SettingValue::String, "private/RecentsProjects", "",
+    SettingValue* recentProjects = m_settings->createVar( SettingValue::List, "private/RecentsProjects", QVariantList(),
                                                 "", "", SettingValue::Private );
     vlmcSettings->addSettings( "RecentProjects", *m_settings );
 
-	connect( recentProjects, SIGNAL( changed( QVariant ) ), this, SLOT( loadRecentProjects( QVariant ) ) );
+    connect( m_settings, &Settings::postLoad, this,
+             [this, recentProjects](){ m_recentsProjects = recentProjects->get().toList(); } );
 }
 
-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();
-
-    m_settings->setValue( "private/RecentsProjects", flattenProjectList() );
-}
-
-const RecentProjects::List&
-RecentProjects::list() const
-{
-    return m_recentsProjects;
-}
-
-QString
-RecentProjects::flattenProjectList() const
+QVariant
+RecentProjects::toVariant() 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 );
 }
 
 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 )
+    for ( int i = 0; i < m_recentsProjects.count(); ++i )
     {
-        if ( (*it).filePath == projectPath )
-            it = m_recentsProjects.erase( it );
-        else
-            ++it;
+        if ( m_recentsProjects[i].toMap()["file"].toString() == projectFile )
+        {
+            m_recentsProjects.removeAt( i );
+            --i;
+        }
     }
 }
 
 void
-RecentProjects::remove( const QString& projectPath )
-{
-    removeFromRecentProjects( projectPath );
-    m_settings->setValue( "private/RecentsProjects", flattenProjectList() );
-}
-
-void
-RecentProjects::loadRecentProjects( const QVariant& recentProjects )
+RecentProjects::projectLoaded( const QString& projectName, const QString& projectFile )
 {
-    // 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 );
-    }
+    QVariantMap var {
+        { "name", projectName },
+        { "file", projectFile }
+    };
+    m_recentsProjects.removeAll( var );
+    m_recentsProjects.insert( 0, var );
+    m_settings->setValue( "private/RecentsProjects", m_recentsProjects );
 }
diff --git a/src/Project/RecentProjects.h b/src/Project/RecentProjects.h
index eb2c64f..692612a 100644
--- a/src/Project/RecentProjects.h
+++ b/src/Project/RecentProjects.h
@@ -33,32 +33,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;
+        QVariantList    m_recentsProjects;
 
 };
 
-- 
1.9.1



More information about the Vlmc-devel mailing list