Sfoglia il codice sorgente

Load document in thread

Kandrashin Denis 13 anni fa
parent
commit
a4d8020499
4 ha cambiato i file con 48 aggiunte e 54 eliminazioni
  1. 7 18
      source/fb2main.cpp
  2. 4 3
      source/fb2main.h
  3. 27 25
      source/fb2read.cpp
  4. 10 8
      source/fb2read.h

+ 7 - 18
source/fb2main.cpp

@@ -28,7 +28,7 @@ MainWindow::MainWindow(const QString &filename, Fb2MainDocument * document)
     init();
     init();
     createText();
     createText();
     thread = new Fb2ReadThread(this, filename);
     thread = new Fb2ReadThread(this, filename);
-    connect(thread, SIGNAL(sendDocument(const QString&, Fb2MainDocument*)), SLOT(sendDocument(const QString&, Fb2MainDocument*)));
+    connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
     thread->start();
     thread->start();
 }
 }
 
 
@@ -113,7 +113,7 @@ void MainWindow::fileOpen()
     if (textEdit) {
     if (textEdit) {
         if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
         if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
             thread = new Fb2ReadThread(this, filename);
             thread = new Fb2ReadThread(this, filename);
-            connect(thread, SIGNAL(sendDocument(const QString&, Fb2MainDocument*)), SLOT(sendDocument(const QString&, Fb2MainDocument*)));
+            connect(thread, SIGNAL(sendDocument()), SLOT(sendDocument()));
             thread->start();
             thread->start();
         } else {
         } else {
             MainWindow * other = new MainWindow(filename, NULL);
             MainWindow * other = new MainWindow(filename, NULL);
@@ -129,14 +129,12 @@ void MainWindow::fileOpen()
             other->move(x() + 40, y() + 40);
             other->move(x() + 40, y() + 40);
             other->show();
             other->show();
         }
         }
-
     }
     }
-
 }
 }
 
 
-void MainWindow::sendDocument(const QString &filename, Fb2MainDocument * document)
+void MainWindow::sendDocument()
 {
 {
-    setCurrentFile(filename, document);
+    setCurrentFile(thread->file(), thread->doc());
 }
 }
 
 
 bool MainWindow::fileSave()
 bool MainWindow::fileSave()
@@ -481,7 +479,7 @@ bool MainWindow::saveFile(const QString &fileName)
     return true;
     return true;
 }
 }
 
 
-void MainWindow::setCurrentFile(const QString &filename, Fb2MainDocument * document)
+void MainWindow::setCurrentFile(const QString &filename, QTextDocument * document)
 {
 {
     static int sequenceNumber = 1;
     static int sequenceNumber = 1;
 
 
@@ -498,19 +496,10 @@ void MainWindow::setCurrentFile(const QString &filename, Fb2MainDocument * docum
     title += QString(" - ") += qApp->applicationName();
     title += QString(" - ") += qApp->applicationName();
 
 
     if (textEdit && document) {
     if (textEdit && document) {
+        document->clearUndoRedoStacks();
+        document->setModified(false);
         textEdit->setDocument(document);
         textEdit->setDocument(document);
         connectTextDocument(textEdit->document());
         connectTextDocument(textEdit->document());
-        if (!document->child().isEmpty()) {
-            if (!noteEdit) {
-                noteEdit = new QTextEdit(this);
-                noteEdit->setDocument(&document->child());
-                QDockWidget * dock = new QDockWidget("Footnotes", this);
-                dock->setAttribute(Qt::WA_DeleteOnClose);
-                dock->setFeatures(QDockWidget::AllDockWidgetFeatures);
-                dock->setWidget(noteEdit);
-                addDockWidget(Qt::BottomDockWidgetArea, dock);
-            }
-        }
     }
     }
 
 
     setWindowModified(false);
     setWindowModified(false);

+ 4 - 3
source/fb2main.h

@@ -15,6 +15,7 @@ QT_END_NAMESPACE
 
 
 class QsciScintilla;
 class QsciScintilla;
 class Fb2MainDocument;
 class Fb2MainDocument;
+class Fb2ReadThread;
 
 
 class MainWindow : public QMainWindow
 class MainWindow : public QMainWindow
 {
 {
@@ -30,7 +31,7 @@ protected:
 
 
 public slots:
 public slots:
     void logMessage(const QString &message);
     void logMessage(const QString &message);
-    void sendDocument(const QString &filename, Fb2MainDocument * document);
+    void sendDocument();
 
 
 private slots:
 private slots:
     void fileNew();
     void fileNew();
@@ -67,11 +68,11 @@ private:
     void writeSettings();
     void writeSettings();
     bool maybeSave();
     bool maybeSave();
     bool saveFile(const QString &fileName);
     bool saveFile(const QString &fileName);
-    void setCurrentFile(const QString &fileName, Fb2MainDocument * document = NULL);
+    void setCurrentFile(const QString &fileName, QTextDocument * document = NULL);
     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
     MainWindow *findMainWindow(const QString &fileName);
     MainWindow *findMainWindow(const QString &fileName);
 
 
-    QThread *thread;
+    Fb2ReadThread *thread;
     QTextEdit *textEdit;
     QTextEdit *textEdit;
     QTextEdit *noteEdit;
     QTextEdit *noteEdit;
     QTextEdit *messageEdit;
     QTextEdit *messageEdit;

+ 27 - 25
source/fb2read.cpp

@@ -10,20 +10,34 @@
 
 
 Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
 Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
     : QThread(parent)
     : QThread(parent)
+    , m_document(NULL)
     , m_filename(filename)
     , m_filename(filename)
     , m_abort(false)
     , m_abort(false)
 {
 {
 }
 }
 
 
 Fb2ReadThread::~Fb2ReadThread()
 Fb2ReadThread::~Fb2ReadThread()
+{
+    stop();
+    wait();
+    if (m_document) delete m_document;
+}
+
+QTextDocument * Fb2ReadThread::doc()
 {
 {
     QMutexLocker locker(&mutex);
     QMutexLocker locker(&mutex);
     Q_UNUSED(locker);
     Q_UNUSED(locker);
-    m_abort = true;
-    wait();
+    return m_document ? m_document->clone() : NULL;
+}
+
+const QString & Fb2ReadThread::file()
+{
+    QMutexLocker locker(&mutex);
+    Q_UNUSED(locker);
+    return m_filename;
 }
 }
 
 
-void Fb2ReadThread::stopProcess()
+void Fb2ReadThread::stop()
 {
 {
     QMutexLocker locker(&mutex);
     QMutexLocker locker(&mutex);
     Q_UNUSED(locker);
     Q_UNUSED(locker);
@@ -38,20 +52,18 @@ void Fb2ReadThread::run()
         return;
         return;
     }
     }
 
 
-    Fb2MainDocument * document = new Fb2MainDocument();
-
-    Fb2Handler handler(*document);
+    m_document = new QTextDocument();
+    Fb2Handler handler(*m_document);
     QXmlSimpleReader reader;
     QXmlSimpleReader reader;
     reader.setContentHandler(&handler);
     reader.setContentHandler(&handler);
     reader.setErrorHandler(&handler);
     reader.setErrorHandler(&handler);
     QXmlInputSource source(&file);
     QXmlInputSource source(&file);
 
 
-    if (reader.parse(source)) {
-        document->moveToThread(QApplication::instance()->thread());
-        emit sendDocument(m_filename, document);
-    } else {
-        delete document;
-    }
+    bool ok = reader.parse(source);
+
+    m_document->moveToThread(QApplication::instance()->thread());
+
+    if (ok) emit sendDocument();
 }
 }
 
 
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
@@ -127,27 +139,24 @@ FB2_BEGIN_KEYHASH(RootHandler)
     insert("binary", Binary);
     insert("binary", Binary);
 FB2_END_KEYHASH
 FB2_END_KEYHASH
 
 
-Fb2Handler::RootHandler::RootHandler(Fb2MainDocument &document, const QString &name)
+Fb2Handler::RootHandler::RootHandler(QTextDocument &document, const QString &name)
     : BaseHandler(name)
     : BaseHandler(name)
     , m_document(document)
     , m_document(document)
     , m_cursor1(&document, false)
     , m_cursor1(&document, false)
-    , m_cursor2(&document.child(), true)
     , m_empty(true)
     , m_empty(true)
 {
 {
     m_cursor1.beginEditBlock();
     m_cursor1.beginEditBlock();
-    m_cursor2.beginEditBlock();
 }
 }
 
 
 Fb2Handler::RootHandler::~RootHandler()
 Fb2Handler::RootHandler::~RootHandler()
 {
 {
     m_cursor1.endEditBlock();
     m_cursor1.endEditBlock();
-    m_cursor2.endEditBlock();
 }
 }
 
 
 Fb2Handler::BaseHandler * Fb2Handler::RootHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
 Fb2Handler::BaseHandler * Fb2Handler::RootHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
 {
 {
     switch (toKeyword(name)) {
     switch (toKeyword(name)) {
-        case Body   : { BaseHandler * handler = new BodyHandler(m_empty ? m_cursor1 : m_cursor2, name); m_empty = false; return handler; }
+        case Body   : { BaseHandler * handler = new BodyHandler(m_cursor1, name); m_empty = false; return handler; }
         case Descr  : return new DescrHandler(name);
         case Descr  : return new DescrHandler(name);
         case Binary : return new BinaryHandler(m_document, name, attributes);
         case Binary : return new BinaryHandler(m_document, name, attributes);
         default: return NULL;
         default: return NULL;
@@ -521,22 +530,15 @@ void Fb2Handler::BinaryHandler::EndTag(const QString &name)
 //  Fb2Handler
 //  Fb2Handler
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 
 
-Fb2Handler::Fb2Handler(Fb2MainDocument & document)
+Fb2Handler::Fb2Handler(QTextDocument & document)
     : m_document(document)
     : m_document(document)
     , m_handler(NULL)
     , m_handler(NULL)
 {
 {
     document.clear();
     document.clear();
-    m_document.child().clear();
 }
 }
 
 
 Fb2Handler::~Fb2Handler()
 Fb2Handler::~Fb2Handler()
 {
 {
-    m_document.clearUndoRedoStacks();
-    m_document.child().clearUndoRedoStacks();
-
-    m_document.setModified(false);
-    m_document.child().setModified(false);
-
     if (m_handler) delete m_handler;
     if (m_handler) delete m_handler;
 }
 }
 
 

+ 10 - 8
source/fb2read.h

@@ -26,18 +26,21 @@ public:
     Fb2ReadThread(QObject *parent, const QString &filename);
     Fb2ReadThread(QObject *parent, const QString &filename);
     ~Fb2ReadThread();
     ~Fb2ReadThread();
     void Read(const QString &filename);
     void Read(const QString &filename);
+    QTextDocument * doc();
+    const QString & file();
 
 
 signals:
 signals:
-    void sendDocument(QString filename, Fb2MainDocument * document);
+    void sendDocument();
 
 
 public slots:
 public slots:
-    void stopProcess();
+    void stop();
 
 
 protected:
 protected:
     void run();
     void run();
 
 
 private:
 private:
     const QString m_filename;
     const QString m_filename;
+    QTextDocument * m_document;
     bool m_abort;
     bool m_abort;
     QMutex mutex;
     QMutex mutex;
 };
 };
@@ -52,7 +55,7 @@ static Keyword toKeyword(const QString &name); private:
 class Fb2Handler : public QXmlDefaultHandler
 class Fb2Handler : public QXmlDefaultHandler
 {
 {
 public:
 public:
-    explicit Fb2Handler(Fb2MainDocument & document);
+    explicit Fb2Handler(QTextDocument & document);
     virtual ~Fb2Handler();
     virtual ~Fb2Handler();
     bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes);
     bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes);
     bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
     bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
@@ -102,14 +105,13 @@ private:
             Binary,
             Binary,
         FB2_END_KEYLIST
         FB2_END_KEYLIST
     public:
     public:
-        explicit RootHandler(Fb2MainDocument &document, const QString &name);
+        explicit RootHandler(QTextDocument &document, const QString &name);
         virtual ~RootHandler();
         virtual ~RootHandler();
     protected:
     protected:
         virtual BaseHandler * NewTag(const QString & name, const QXmlAttributes &attributes);
         virtual BaseHandler * NewTag(const QString & name, const QXmlAttributes &attributes);
     private:
     private:
-        Fb2MainDocument &m_document;
+        QTextDocument &m_document;
         Fb2TextCursor m_cursor1;
         Fb2TextCursor m_cursor1;
-        Fb2TextCursor m_cursor2;
         bool m_empty;
         bool m_empty;
     };
     };
 
 
@@ -274,10 +276,10 @@ private:
     };
     };
 
 
 public:
 public:
-    Fb2MainDocument & document() { return m_document; }
+    QTextDocument & document() { return m_document; }
 
 
 private:
 private:
-    Fb2MainDocument & m_document;
+    QTextDocument & m_document;
     RootHandler * m_handler;
     RootHandler * m_handler;
     QString m_error;
     QString m_error;
 };
 };