99
1010#include " valuemapfiltermodel.h"
1111
12+ #include < QtConcurrentRun>
13+
1214ValueMapFilterModel::ValueMapFilterModel ( QObject *parent )
1315 : QAbstractListModel( parent )
1416{
17+ connect ( &mResultWatcher , &QFutureWatcher<QList<Item>>::finished, this , &ValueMapFilterModel::onLoadingFinished );
1518}
1619
1720int ValueMapFilterModel::rowCount ( const QModelIndex &parent ) const
@@ -51,6 +54,11 @@ QVariantMap ValueMapFilterModel::config() const
5154 return mConfig ;
5255}
5356
57+ bool ValueMapFilterModel::isLoading () const
58+ {
59+ return mIsLoading ;
60+ }
61+
5462void ValueMapFilterModel::setConfig ( const QVariantMap &config )
5563{
5664 if ( mConfig == config )
@@ -59,17 +67,29 @@ void ValueMapFilterModel::setConfig( const QVariantMap &config )
5967 mConfig = config;
6068 emit configChanged ();
6169
62- populate ();
70+ if ( mResultWatcher .isRunning () )
71+ {
72+ mHasPendingLoad = true ;
73+ return ;
74+ }
75+
76+ startLoad ();
6377}
6478
65- void ValueMapFilterModel::populate ()
79+ void ValueMapFilterModel::startLoad ()
6680{
67- beginResetModel ();
81+ mIsLoading = true ;
82+ emit isLoadingChanged ();
6883
69- mItems .clear ();
84+ mResultWatcher .setFuture ( QtConcurrent::run ( &ValueMapFilterModel::loadItems, mConfig ) );
85+ }
86+
87+ QList<ValueMapFilterModel::Item> ValueMapFilterModel::loadItems ( const QVariantMap &config )
88+ {
89+ const QVariantList mapList = config.value ( QStringLiteral ( " map" ) ).toList ();
7090
71- const QVariantList mapList = mConfig . value ( QStringLiteral ( " map " ) ). toList () ;
72- mItems .reserve ( mapList.size () );
91+ QList<Item> items ;
92+ items .reserve ( mapList.size () );
7393
7494 for ( const QVariant &entry : mapList )
7595 {
@@ -83,8 +103,29 @@ void ValueMapFilterModel::populate()
83103 item.description = entryMap.constBegin ().key ();
84104 item.key = entryMap.constBegin ().value ().toString ();
85105
86- mItems .append ( item );
106+ items .append ( item );
87107 }
88108
109+ return items;
110+ }
111+
112+ void ValueMapFilterModel::onLoadingFinished ()
113+ {
114+ beginResetModel ();
115+
116+ mItems = mResultWatcher .result ();
117+
89118 endResetModel ();
119+ emit countChanged ();
120+
121+ if ( mHasPendingLoad )
122+ {
123+ mHasPendingLoad = false ;
124+ startLoad ();
125+ }
126+ else
127+ {
128+ mIsLoading = false ;
129+ emit isLoadingChanged ();
130+ }
90131}
0 commit comments