[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [bookmanager/exportImport] managerpart: Fix communication of worker thread with main thread
From:       Riccardo Bellini <riccardo.bellini1988 () gmail ! com>
Date:       2014-02-28 15:53:03
Message-ID: E1WJPkF-0003Hk-K2 () scm ! kde ! org
[Download RAW message or body]

Git commit 55e5ff133e0c5e35b5d6238fc83ced141239c110 by Riccardo Bellini.
Committed on 28/02/2014 at 15:51.
Pushed by bellini into branch 'exportImport'.

Fix communication of worker thread with main thread

Implement signal/slot connections to update the GUI while the worker
thread copies the collection. Implementation of the worker thread is
still to be done, for now dummy code is executed

M  +19   -7    managerpart/collectionorganizerwidget.cpp
M  +4    -0    managerpart/collectionorganizerwidget.h
M  +12   -5    managerpart/tools/collectionorganizer/collectionorganizer.cpp
M  +6    -2    managerpart/tools/collectionorganizer/collectionorganizer.h
M  +16   -1    managerpart/tools/collectionorganizer/copycollectionworker.cpp
M  +13   -0    managerpart/tools/collectionorganizer/copycollectionworker.h

http://commits.kde.org/bookmanager/55e5ff133e0c5e35b5d6238fc83ced141239c110

diff --git a/managerpart/collectionorganizerwidget.cpp \
b/managerpart/collectionorganizerwidget.cpp index 5d9c64a..ccbd1c1 100644
--- a/managerpart/collectionorganizerwidget.cpp
+++ b/managerpart/collectionorganizerwidget.cpp
@@ -35,7 +35,7 @@
 CollectionOrganizerWidget::CollectionOrganizerWidget(CollectionDB * collection,
         QWidget * parent,
         Qt::WindowFlags flags)
-    : QWidget(parent, flags), m_collection(collection)
+    : QWidget(parent, flags), m_collection(collection), m_collectionOrganizer(0)
 {
     setupUi(this);
     rootFolderUrlRequester->setMode(KFile::Directory | KFile::LocalOnly);
@@ -54,7 +54,8 @@ CollectionOrganizerWidget::CollectionOrganizerWidget(CollectionDB * \
collection,  
 CollectionOrganizerWidget::~CollectionOrganizerWidget()
 {
-
+    // WARNING move deletion into the slot collectionOrganizationCompleted()?
+    delete m_collectionOrganizer;
 }
 
 
@@ -72,14 +73,19 @@ void CollectionOrganizerWidget::organizeCollection()
                     "local file"), i18n("Invalid root folder"));
         return;
     }
-    // initialize CollectionOrganizer object
-    CollectionOrganizer organizer(m_collection, this);
+    // initialize CollectionOrganizer object if necessary
+    if (!m_collectionOrganizer) {
+        m_collectionOrganizer = new CollectionOrganizer(m_collection, this);
+        connect(m_collectionOrganizer, SIGNAL(organizationCompleted()),
+                this, SLOT(collectionOrganizationCompleted()));
+    }
     // set root folder and collection structure
-    organizer.setRootFolderUrl(rootFolderUrl);
-    organizer.setCollectionStructure(structureLineEdit->text());
-    organizer.organizeCollection();
+    m_collectionOrganizer->setRootFolderUrl(rootFolderUrl);
+    m_collectionOrganizer->setCollectionStructure(structureLineEdit->text());
+    m_collectionOrganizer->organizeCollection();
 }
 
+
 // private slots
 void CollectionOrganizerWidget::sizeComputed(quint64 size)
 {
@@ -96,6 +102,12 @@ void CollectionOrganizerWidget::sizeComputed(quint64 size)
 }
 
 
+void CollectionOrganizerWidget::collectionOrganizationCompleted()
+{
+    KMessageBox::information(0, i18n("The collection has been organized \
correctly.")); +}
+
+
 // private methods
 void CollectionOrganizerWidget::m_computeDiskSpace()
 {
diff --git a/managerpart/collectionorganizerwidget.h \
b/managerpart/collectionorganizerwidget.h index 93a763a..79b3e30 100644
--- a/managerpart/collectionorganizerwidget.h
+++ b/managerpart/collectionorganizerwidget.h
@@ -27,6 +27,7 @@
 
 // Forward declarations
 class CollectionDB;
+class CollectionOrganizer;
 
 class CollectionOrganizerWidget : public QWidget, private \
Ui::CollectionOrganizerWidget {  Q_OBJECT
@@ -41,10 +42,13 @@ public:
 
 private slots:
     void sizeComputed(quint64);
+    void collectionOrganizationCompleted();
 
 private:
     void m_computeDiskSpace();
     CollectionDB * m_collection;
+    
+    CollectionOrganizer * m_collectionOrganizer;
 
     quint64 m_requiredSpace;
     quint64 m_availableSpace;
diff --git a/managerpart/tools/collectionorganizer/collectionorganizer.cpp \
b/managerpart/tools/collectionorganizer/collectionorganizer.cpp index \
                639f7cb..28cc95d 100644
--- a/managerpart/tools/collectionorganizer/collectionorganizer.cpp
+++ b/managerpart/tools/collectionorganizer/collectionorganizer.cpp
@@ -24,7 +24,6 @@
 
 // Qt includes
 #include <qthread.h>
-#include <qtimer.h>
 
 
 CollectionOrganizer::CollectionOrganizer (CollectionDB * collectionDb,
@@ -50,18 +49,24 @@ void CollectionOrganizer::organizeCollection()
     // TODO check if collection is a valid collection
     // copyCollectionWorker is without a parent, because objects with parent can't \
be moved to  // another thread
+    // Despite being without a parent, the worker and the thread that manages it are \
deleted +    // when the organization is finished
     m_copyCollectionWorker = new CopyCollectionWorker(m_collection);
     m_copyCollectionWorker->setStructureStr(m_collectionStructure);
     m_copyCollectionWorker->setRootFolderUrl(m_rootFolderUrl);
     m_copyCollectionWorker->setTokenList(tokenList);
-    // TODO connect signals and slot to update the view through a progress bar
     // create a new thread and move the worker to that thread
     m_copyCollectionThread = new QThread;
     m_copyCollectionWorker->moveToThread(m_copyCollectionThread);
+    connect(m_copyCollectionWorker, SIGNAL(bookCopied(QString)),
+            this, SLOT(bookCompleted(QString)), Qt::QueuedConnection);
+    connect(m_copyCollectionThread, SIGNAL(finished()),
+            this, SLOT(copyFinished()));
+    connect(m_copyCollectionWorker, SIGNAL(copyFinished()), m_copyCollectionThread, \
SLOT(quit())); +    connect(m_copyCollectionThread, SIGNAL(started()),
+            m_copyCollectionWorker, SLOT(copyCollection()));
     // start thread
     m_copyCollectionThread->start();
-    // start copy method with a single shot timer
-    QTimer::singleShot(0, m_copyCollectionWorker, SLOT(copyCollection()));
 }
 
 
@@ -92,7 +97,7 @@ void CollectionOrganizer::setCollectionStructure(const QString & \
structure)  
 
 // private slots
-void CollectionOrganizer::bookCompleted(const QString & title)
+void CollectionOrganizer::bookCompleted(QString bookTitle)
 {
     // TODO update progress bar
 }
@@ -103,4 +108,6 @@ void CollectionOrganizer::copyFinished()
     // TODO update progress bar
     m_copyCollectionWorker->deleteLater();
     m_copyCollectionThread->deleteLater();
+    // signal the widget that the collection has been organized
+    emit organizationCompleted();
 }
diff --git a/managerpart/tools/collectionorganizer/collectionorganizer.h \
b/managerpart/tools/collectionorganizer/collectionorganizer.h index 756fbd8..26459e2 \
                100644
--- a/managerpart/tools/collectionorganizer/collectionorganizer.h
+++ b/managerpart/tools/collectionorganizer/collectionorganizer.h
@@ -31,6 +31,7 @@ class QThread;
 class CopyCollectionWorker;
 
 class CollectionOrganizer : public QObject {
+    Q_OBJECT
 public:
     CollectionOrganizer (CollectionDB * collectionDb = 0, QObject * parent = 0);
     virtual ~CollectionOrganizer ();
@@ -44,9 +45,12 @@ public:
     // setters
     void setRootFolderUrl(const KUrl & url);
     void setCollectionStructure(const QString & structure);
+    
+signals:
+    void organizationCompleted();
 
-private slots:
-    void bookCompleted(const QString & title);
+public slots:
+    void bookCompleted(QString);
     void copyFinished();
 
 private:
diff --git a/managerpart/tools/collectionorganizer/copycollectionworker.cpp \
b/managerpart/tools/collectionorganizer/copycollectionworker.cpp index \
                c233aed..f00328d 100644
--- a/managerpart/tools/collectionorganizer/copycollectionworker.cpp
+++ b/managerpart/tools/collectionorganizer/copycollectionworker.cpp
@@ -19,6 +19,7 @@
 // Book Manager includes
 #include "copycollectionworker.h"
 
+#include <unistd.h>
 
 CopyCollectionWorker::CopyCollectionWorker(CollectionDB * collectionDB,
         QObject * parent)
@@ -78,6 +79,20 @@ void CopyCollectionWorker::setCollectionDB(CollectionDB * \
collectionDB)  
 void CopyCollectionWorker::copyCollection()
 {
-    // TODO
+    // dummy method, sleep and emit some signals
+    QString titles[7] = {
+        "Book1",
+        "Book2",
+        "Book3",
+        "Book4",
+        "Book5",
+        "Book6",
+        "Book7"
+    };
+    for (int i = 0; i < 7; ++i) {
+        usleep(500000);
+        emit bookCopied(titles[i]);
+    }
+    emit copyFinished();
 }
 
diff --git a/managerpart/tools/collectionorganizer/copycollectionworker.h \
b/managerpart/tools/collectionorganizer/copycollectionworker.h index 70a4a72..5391085 \
                100644
--- a/managerpart/tools/collectionorganizer/copycollectionworker.h
+++ b/managerpart/tools/collectionorganizer/copycollectionworker.h
@@ -91,9 +91,22 @@ class CopyCollectionWorker : public QObject {
          * @param rootFolderUrl The root url of the new collection
          */
         void setRootFolderUrl(const KUrl & rootFolderUrl);
+        /**
+         * @brief This method sets the pointer to the collection
+         *
+         * @param collectionDB The pointer to the collection object
+         */
         void setCollectionDB(CollectionDB * collectionDB);
 
+    signals:
+        void bookCopied(QString);
+        void copyFinished();
+
     public slots:
+        /**
+         * @brief This slot handles the copying of the collection to the
+         * new location
+         */
         void copyCollection();
 
     private:


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic