Skip to content
Snippets Groups Projects
Commit cf3c3c3d authored by luyikei's avatar luyikei Committed by Hugo Beauzée-Luyssen
Browse files

RecentProjects: Use QVariantList to store m_recentsProjects

parent 7c5b378a
No related branches found
No related tags found
No related merge requests found
......@@ -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 );
}
}
......
......@@ -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 );
}
......@@ -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;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment