Bladeren bron

Load document in thread

Kandrashin Denis 13 jaren geleden
bovenliggende
commit
17631b7674
6 gewijzigde bestanden met toevoegingen van 101 en 43 verwijderingen
  1. 0 17
      source/fb2doc.cpp
  2. 0 1
      source/fb2doc.h
  3. 15 20
      source/fb2main.cpp
  4. 3 1
      source/fb2main.h
  5. 54 4
      source/fb2read.cpp
  6. 29 0
      source/fb2read.h

+ 0 - 17
source/fb2doc.cpp

@@ -1,20 +1,3 @@
 #include "fb2doc.h"
 #include "fb2read.h"
 
-Fb2MainDocument * Fb2MainDocument::load(QIODevice &io)
-{
-    Fb2MainDocument * document = new Fb2MainDocument;
-
-    Fb2Handler handler(*document);
-    QXmlSimpleReader reader;
-    reader.setContentHandler(&handler);
-    reader.setErrorHandler(&handler);
-    QXmlInputSource source(&io);
-
-    if (reader.parse(source)) {
-        return document;
-    } else {
-        delete document;
-        return NULL;
-    }
-}

+ 0 - 1
source/fb2doc.h

@@ -21,7 +21,6 @@ class Fb2MainDocument : public QTextDocument
 {
     Q_OBJECT
 public:
-    static Fb2MainDocument * load(QIODevice &io);
     explicit Fb2MainDocument(QObject *parent = 0) : QTextDocument(parent), m_child(*this) {}
     Fb2ChildDocument & child() { return m_child; }
 private:

+ 15 - 20
source/fb2main.cpp

@@ -3,6 +3,7 @@
 
 #include "fb2main.h"
 #include "fb2doc.h"
+#include "fb2read.h"
 
 #include <Qsci/qsciscintilla.h>
 #include <Qsci/qscilexerxml.h>
@@ -26,12 +27,15 @@ MainWindow::MainWindow(const QString &filename, Fb2MainDocument * document)
 {
     init();
     createText();
-    if (!document) document = loadFB2(filename);
-    setCurrentFile(filename, document);
+    thread = new Fb2ReadThread(this, filename);
+    connect(thread, SIGNAL(sendDocument(const QString&, Fb2MainDocument*)), SLOT(sendDocument(const QString&, Fb2MainDocument*)));
+    thread->start();
 }
 
 void MainWindow::init()
 {
+    thread = NULL;
+
     connect(qApp, SIGNAL(logMessage(QString)), SLOT(logMessage(QString)));
 
     setAttribute(Qt::WA_DeleteOnClose);
@@ -76,19 +80,6 @@ bool MainWindow::loadXML(const QString &filename)
     return false;
 }
 
-Fb2MainDocument * MainWindow::loadFB2(const QString &filename)
-{
-    if (filename.isEmpty()) return NULL;
-
-    QFile file(filename);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        qCritical() << tr("Cannot read file %1:\n%2.").arg(filename).arg(file.errorString());
-        return NULL;
-    }
-
-    return Fb2MainDocument::load(file);
-}
-
 void MainWindow::closeEvent(QCloseEvent *event)
 {
     if (maybeSave()) {
@@ -120,13 +111,12 @@ void MainWindow::fileOpen()
     }
 
     if (textEdit) {
-        Fb2MainDocument * document = loadFB2(filename);
-        if (!document) return;
-
         if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
-            setCurrentFile(filename, document);
+            thread = new Fb2ReadThread(this, filename);
+            connect(thread, SIGNAL(sendDocument(const QString&, Fb2MainDocument*)), SLOT(sendDocument(const QString&, Fb2MainDocument*)));
+            thread->start();
         } else {
-            MainWindow * other = new MainWindow(filename, document);
+            MainWindow * other = new MainWindow(filename, NULL);
             other->move(x() + 40, y() + 40);
             other->show();
         }
@@ -144,6 +134,11 @@ void MainWindow::fileOpen()
 
 }
 
+void MainWindow::sendDocument(const QString &filename, Fb2MainDocument * document)
+{
+    setCurrentFile(filename, document);
+}
+
 bool MainWindow::fileSave()
 {
     if (isUntitled) {

+ 3 - 1
source/fb2main.h

@@ -8,6 +8,7 @@ QT_BEGIN_NAMESPACE
 class QAction;
 class QMenu;
 class QFile;
+class QThread;
 class QTextEdit;
 class QTextDocument;
 QT_END_NAMESPACE
@@ -29,6 +30,7 @@ protected:
 
 public slots:
     void logMessage(const QString &message);
+    void sendDocument(const QString &filename, Fb2MainDocument * document);
 
 private slots:
     void fileNew();
@@ -51,7 +53,6 @@ private slots:
     void clipboardDataChanged();
 
 private:
-    static Fb2MainDocument * loadFB2(const QString &filename);
     bool loadXML(const QString &filename);
     void connectTextDocument(QTextDocument * document);
     QIcon icon(const QString &name);
@@ -70,6 +71,7 @@ private:
     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
     MainWindow *findMainWindow(const QString &fileName);
 
+    QThread *thread;
     QTextEdit *textEdit;
     QTextEdit *noteEdit;
     QTextEdit *messageEdit;

+ 54 - 4
source/fb2read.cpp

@@ -4,6 +4,60 @@
 
 #include "fb2read.h"
 
+//---------------------------------------------------------------------------
+//  Fb2ReadThread
+//---------------------------------------------------------------------------
+
+Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
+    : QThread(parent)
+    , m_filename(filename)
+    , m_abort(false)
+{
+}
+
+Fb2ReadThread::~Fb2ReadThread()
+{
+    QMutexLocker locker(&mutex);
+    Q_UNUSED(locker);
+    m_abort = true;
+    wait();
+}
+
+void Fb2ReadThread::stopProcess()
+{
+    QMutexLocker locker(&mutex);
+    Q_UNUSED(locker);
+    m_abort = true;
+}
+
+void Fb2ReadThread::run()
+{
+    QFile file(m_filename);
+    if (!file.open(QFile::ReadOnly | QFile::Text)) {
+        qCritical() << tr("Cannot read file %1:\n%2.").arg(m_filename).arg(file.errorString());
+        return;
+    }
+
+    Fb2MainDocument * document = new Fb2MainDocument();
+
+    Fb2Handler handler(*document);
+    QXmlSimpleReader reader;
+    reader.setContentHandler(&handler);
+    reader.setErrorHandler(&handler);
+    QXmlInputSource source(&file);
+
+    if (reader.parse(source)) {
+        document->moveToThread(QApplication::instance()->thread());
+        emit sendDocument(m_filename, document);
+    } else {
+        delete document;
+    }
+}
+
+//---------------------------------------------------------------------------
+//  Fb2Handler::BaseHandler
+//---------------------------------------------------------------------------
+
 #define FB2_BEGIN_KEYHASH(x) \
 Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
 {                                                                    \
@@ -26,10 +80,6 @@ static QString Value(const QXmlAttributes &attributes, const QString &name)
     return QString();
 }
 
-//---------------------------------------------------------------------------
-//  Fb2Handler::BaseHandler
-//---------------------------------------------------------------------------
-
 Fb2Handler::BaseHandler::~BaseHandler()
 {
     if (m_handler) delete m_handler;

+ 29 - 0
source/fb2read.h

@@ -3,6 +3,8 @@
 
 #include "fb2doc.h"
 
+#include <QMutex>
+#include <QThread>
 #include <QXmlDefaultHandler>
 #include <QTextCursor>
 #include <QStringList>
@@ -14,6 +16,33 @@ QT_BEGIN_NAMESPACE
 class QTextEdit;
 QT_END_NAMESPACE
 
+class Fb2MainDocumen;
+
+class Fb2ReadThread : public QThread
+{
+    Q_OBJECT
+
+public:
+    Fb2ReadThread(QObject *parent, const QString &filename);
+    ~Fb2ReadThread();
+    void Read(const QString &filename);
+
+signals:
+    void sendDocument(QString filename, Fb2MainDocument * document);
+
+public slots:
+    void stopProcess();
+
+protected:
+    void run();
+
+private:
+    const QString m_filename;
+    bool m_abort;
+    QMutex mutex;
+};
+
+
 #define FB2_BEGIN_KEYLIST private: enum Keyword {
 
 #define FB2_END_KEYLIST None }; \