[qet] [4576] ElementsCollectionModel : model use multithreading itself for load collections

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4576
Author:   blacksun
Date:     2016-07-14 13:58:56 +0200 (Thu, 14 Jul 2016)
Log Message:
-----------
ElementsCollectionModel : model use multithreading itself for load collections

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementscollectionmodel.cpp
    trunk/sources/ElementsCollection/elementscollectionmodel.h
    trunk/sources/ElementsCollection/elementscollectionwidget.cpp
    trunk/sources/elementdialog.cpp

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-07-13 23:28:00 UTC (rev 4575)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-07-14 11:58:56 UTC (rev 4576)
@@ -24,6 +24,8 @@
 #include "qetproject.h"
 #include "elementcollectionhandler.h"
 
+#include <QtConcurrent>
+
 /**
  * @brief ElementsCollectionModel::ElementsCollectionModel
  * Constructor
@@ -210,6 +212,35 @@
 }
 
 /**
+ * @brief ElementsCollectionModel::loadCollections
+ * Load the several collections in this model.
+ * Prefer use this method instead of addCommonCollection, addCustomCollection and addProject,
+ * because it use multithreading to speed up the loading.
+ * This method emit loadingMaxValue(int) for know the maximum progress value
+ * This method emit loadingProgressValue(int) for know the current progress value
+ * @param common_collection : true for load the common collection
+ * @param custom_collection : true for load the custom collection
+ * @param projects : list of projects to load
+ */
+void ElementsCollectionModel::loadCollections(bool common_collection, bool custom_collection, QList<QETProject *> projects)
+{
+	if (common_collection)
+		addCommonCollection(false);
+	if (custom_collection)
+		addCustomCollection(false);
+
+	foreach (QETProject *project, projects)
+		addProject(project, false);
+
+	QList <ElementCollectionItem *> list = items();
+	QFuture<void> futur = QtConcurrent::map(list, setUpData);
+	emit loadingMaxValue(futur.progressMaximum());
+	while (futur.isRunning()) {
+		emit loadingProgressValue(futur.progressValue());
+	}
+}
+
+/**
  * @brief ElementsCollectionModel::addCommonCollection
  * Add the common elements collection to this model
  */
@@ -305,7 +336,7 @@
 	XmlProjectElementCollectionItem *xpeci = new XmlProjectElementCollectionItem();
 	m_project_hash.insert(project, xpeci);
 
-	xpeci->setProject(project);
+	xpeci->setProject(project, set_data);
 	insertRow(row, xpeci);
 	if (set_data)
 		xpeci->setUpData();

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.h
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-07-13 23:28:00 UTC (rev 4575)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.h	2016-07-14 11:58:56 UTC (rev 4576)
@@ -41,6 +41,8 @@
 		virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const;
 		virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
 
+		void loadCollections(bool common_collection, bool custom_collection, QList<QETProject *> projects);
+
 		void addCommonCollection(bool set_data = true);
 		void addCustomCollection(bool set_data = true);
 		void addLocation(ElementsLocation location);
@@ -56,6 +58,10 @@
 		bool isHideElement() {return m_hide_element;}
 		QModelIndex indexFromLocation(const ElementsLocation &location);
 
+	signals:
+		void loadingMaxValue(int);
+		void loadingProgressValue(int);
+
 	private:
 		void elementIntegratedToCollection (QString path);
 		void itemRemovedFromCollection (QString path);

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-07-13 23:28:00 UTC (rev 4575)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-07-14 11:58:56 UTC (rev 4576)
@@ -35,7 +35,6 @@
 #include <QDesktopServices>
 #include <QUrl>
 #include <QTimer>
-#include <QtConcurrent>
 
 /**
  * @brief ElementsCollectionWidget::ElementsCollectionWidget
@@ -77,7 +76,14 @@
  */
 void ElementsCollectionWidget::addProject(QETProject *project) {
 	if (m_model) {
-		m_model->addProject(project);
+		QList <QETProject *> prj; prj.append(project);
+		m_progress_bar->show();
+		connect(m_model, &ElementsCollectionModel::loadingMaxValue, m_progress_bar, &QProgressBar::setMaximum);
+		connect(m_model, &ElementsCollectionModel::loadingProgressValue, m_progress_bar, &QProgressBar::setValue);
+		m_model->loadCollections(false,false, prj);
+		disconnect(m_model, &ElementsCollectionModel::loadingMaxValue, m_progress_bar, &QProgressBar::setMaximum);
+		disconnect(m_model, &ElementsCollectionModel::loadingProgressValue, m_progress_bar, &QProgressBar::setValue);
+		m_progress_bar->hide();
 		m_model->highlightUnusedElement();
 	}
 	else
@@ -462,27 +468,22 @@
 {
 	m_progress_bar->show();
 	ElementsCollectionModel *new_model = new ElementsCollectionModel(m_tree_view);
-	new_model->addCommonCollection(false);
-	new_model->addCustomCollection(false);
 
-	if (!m_waiting_project.isEmpty()) {
-		foreach(QETProject *prj, m_waiting_project)
-			new_model->addProject(prj, false);
-		m_waiting_project.clear();
-	}
-
+	QList <QETProject *> project_list;
+	project_list.append(m_waiting_project);
+	m_waiting_project.clear();
 	if (m_model)
-		foreach (QETProject *project, m_model->project())
-			new_model->addProject(project, false);
+		project_list.append(m_model->project());
 
-	QList <ElementCollectionItem *> list = new_model->items();
-	QFuture<void> futur = QtConcurrent::map(list, setUpData);
-	m_progress_bar->setMinimum(futur.progressMinimum());
-	m_progress_bar->setMaximum(futur.progressMaximum());
-	while (futur.isRunning()) {
-		m_progress_bar->setValue(futur.progressValue());
-	}
 
+	connect(new_model, &ElementsCollectionModel::loadingMaxValue, m_progress_bar, &QProgressBar::setMaximum);
+	connect(new_model, &ElementsCollectionModel::loadingProgressValue, m_progress_bar, &QProgressBar::setValue);
+
+	new_model->loadCollections(true, true, project_list);
+
+	disconnect(new_model, &ElementsCollectionModel::loadingMaxValue, m_progress_bar, &QProgressBar::setMaximum);
+	disconnect(new_model, &ElementsCollectionModel::loadingProgressValue, m_progress_bar, &QProgressBar::setValue);
+
 	new_model->highlightUnusedElement();
 	m_tree_view->setModel(new_model);
 	m_index_at_context_menu = QModelIndex();

Modified: trunk/sources/elementdialog.cpp
===================================================================
--- trunk/sources/elementdialog.cpp	2016-07-13 23:28:00 UTC (rev 4575)
+++ trunk/sources/elementdialog.cpp	2016-07-14 11:58:56 UTC (rev 4576)
@@ -87,21 +87,20 @@
 	m_tree_view = new QTreeView(this);
 
 	m_model = new ElementsCollectionModel(m_tree_view);
-	if (m_mode == OpenElement)
-		m_model->addCommonCollection(false);
-	m_model->addCustomCollection(false);
 
-	foreach (QETProject *project, QETApp::registeredProjects())
-		m_model->addProject(project, false);
+	QList <QETProject *> prjs;
+	foreach(QETProject *prj, QETApp::registeredProjects())
+			prjs.append(prj);
 
-	QList <ElementCollectionItem *> list = m_model->items();
-	QtConcurrent::blockingMap(list, setUpData);
+	if (m_mode == OpenElement)
+		m_model->loadCollections(true, true, prjs);
+	else
+		m_model->loadCollections(false, true, prjs);
 
 	m_tree_view->setModel(m_model);
 	m_tree_view->setHeaderHidden(true);
 	layout->addWidget(m_tree_view);
 
-
 	m_buttons_box = new QDialogButtonBox(this);
 
 	if (m_mode == SaveCategory || m_mode == SaveElement)


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/