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

List:       kde-commits
Subject:    =?utf-8?q?=5Bcirkuit/zoomable-preview=5D_src=3A_Removed_the_rend?=
From:       Matteo Agostinelli <agostinelli () gmail ! com>
Date:       2011-01-31 18:35:30
Message-ID: 20110131183530.794BAA60C2 () git ! kde ! org
[Download RAW message or body]

Git commit 6f7645ba81e300333f91e7ee610493a3670e75bf by Matteo Agostinelli.
Pushed by agostinelli into branch 'zoomable-preview'.

Removed the renderer from libcirkuit and created a more efficient
separate threaded class

M  +4    -2    src/CMakeLists.txt     
M  +6    -4    src/generatorthread.cpp     
M  +4    -1    src/generatorthread.h     
M  +1    -3    src/lib/CMakeLists.txt     
M  +0    -37   src/lib/generator.cpp     
M  +0    -6    src/lib/generator.h     
A  +104  -0    src/renderthread.cpp         [License: GPL (v2+)]
A  +55   -0    src/renderthread.h         [License: GPL (v2+)]

http://commits.kde.org/74f3f67f/6f7645ba81e300333f91e7ee610493a3670e75bf

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d20b1c8..f57fda0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,8 @@
 project(cirkuit)
 
 find_package(KDE4 REQUIRED)
-include_directories(${KDE4_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} \
${CMAKE_CURRENT_BINARY_DIR}/widgets ${QT_QTNETWORK_INCLUDE_DIR}) \
+find_package(Poppler REQUIRED) +include_directories(${KDE4_INCLUDES} \
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/widgets \
${QT_QTNETWORK_INCLUDE_DIR} ${POPPLER_INCLUDE_DIR})  
 add_subdirectory( mimetypes )
 add_subdirectory( syntax )
@@ -15,13 +16,14 @@ set(cirkuit_SRCS
 	mainwindow.cpp
 	circuitmacrosmanager.cpp
 	generatorthread.cpp
+	renderthread.cpp
 )
 
 kde4_add_kcfg_files(cirkuit_SRCS GENERATE_MOC cirkuitsettings.kcfgc)
 kde4_add_ui_files(cirkuit_SRCS cirkuit_general_settings.ui)
 kde4_add_executable(cirkuit ${cirkuit_SRCS})
 
-target_link_libraries(cirkuit ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS} \
${KDE4_KTEXTEDITOR_LIBS} ${QT_QTNETWORK_LIBRARIES} cirkuitlibs cirkuitwidgets) \
+target_link_libraries(cirkuit ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS} \
${KDE4_KTEXTEDITOR_LIBS} ${QT_QTNETWORK_LIBRARIES} ${POPPLER_LIBRARY} cirkuitlibs \
cirkuitwidgets)  
 install(TARGETS cirkuit ${INSTALL_TARGETS_DEFAULT_ARGS})
 install(FILES cirkuitui.rc 
diff --git a/src/generatorthread.cpp b/src/generatorthread.cpp
index d3fbb51..a5f0e0b 100644
--- a/src/generatorthread.cpp
+++ b/src/generatorthread.cpp
@@ -23,6 +23,7 @@
 
 #include "cirkuitsettings.h"
 #include "generatorthread.h"
+#include "renderthread.h"
 
 #include <KDebug>
 #include <KLocalizedString>
@@ -32,6 +33,8 @@ using namespace Cirkuit;
 GeneratorThread::GeneratorThread(const Cirkuit::Format& in, const Cirkuit::Format& \
out, Cirkuit::Document* doc, QObject* parent): QThread(parent)  {
     m_backend = 0;
+    m_render = new RenderThread;
+    connect(m_render, SIGNAL(previewReady(QImage)), this, \
SIGNAL(previewReady(QImage)));  setup(in, out, m_backend, doc, false);
 }
 
@@ -53,20 +56,19 @@ void GeneratorThread::run()
     
     Cirkuit::Generator* gen = m_backend->generator();
     
-    connect(gen, SIGNAL(previewReady(QImage)), this, SIGNAL(previewReady(QImage)));
     connect(gen, SIGNAL(error(QString,QString)), this, \
SIGNAL(error(QString,QString)));  connect(gen, SIGNAL(error(QString,QString)), this, \
                SLOT(quit()));
     connect(gen, SIGNAL(output(QString,QString)), this, \
SIGNAL(output(QString,QString)));  connect(gen, SIGNAL(fail()), this, \
SIGNAL(fail()));  gen->setDocument(m_doc);
-	gen->setResolution(CirkuitSettings::resolutionPpm());
+    gen->setResolution(CirkuitSettings::resolutionPpm());
     if (!gen->convert(m_input, m_output)) {
         emit fail();
         return;
     }
     
-    if (m_output == Format::QtImage) {
-        gen->render();
+    if (m_output == Format::QtImage) {        
+        m_render->generatePreview(gen->formatPath(Format::Pdf));
     }
     
     if (m_saveToFile) {
diff --git a/src/generatorthread.h b/src/generatorthread.h
index 07fa0c8..b358d91 100644
--- a/src/generatorthread.h
+++ b/src/generatorthread.h
@@ -24,6 +24,7 @@
 #include <QThread>
 #include <QImage>
 
+class RenderThread;
 namespace Cirkuit
 {
 class Command;
@@ -59,7 +60,9 @@ signals:
     
 private:
     Cirkuit::Document* m_doc;
-	Cirkuit::Backend* m_backend;
+    Cirkuit::Backend* m_backend;
+    
+    RenderThread* m_render;
     bool m_saveToFile;
 };
 
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index c7ff67d..b8c418d 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -1,8 +1,6 @@
 set(GENERIC_LIB_VERSION "0.0.1")
 set(GENERIC_LIB_SOVERSION "1")
 
-find_package(Poppler REQUIRED)
-include_directories(${POPPLER_INCLUDE_DIR})
 set ( cirkuit_LIB_SRCS
     backend.cpp
     command.cpp
@@ -19,7 +17,7 @@ set( cirkuit_LIB_HDRS
 kde4_add_library( cirkuitlibs SHARED ${cirkuit_LIB_SRCS} )
 
 target_link_libraries( cirkuitlibs
-  ${KDE4_KDECORE_LIBS} ${KDE4_KTEXTEDITOR_LIBS} ${POPPLER_LIBRARY}
+  ${KDE4_KDECORE_LIBS} ${KDE4_KTEXTEDITOR_LIBS}
 )
 
 set_target_properties( cirkuitlibs
diff --git a/src/lib/generator.cpp b/src/lib/generator.cpp
index 163161c..90c0614 100644
--- a/src/lib/generator.cpp
+++ b/src/lib/generator.cpp
@@ -24,7 +24,6 @@
 
 #include <QFileInfo>
 #include <QDir>
-#include <poppler-qt4.h>
 
 #include <KStandardDirs>
 #include <KTemporaryFile>
@@ -137,42 +136,6 @@ bool Generator::execute(Cirkuit::Command* c)
     return true;
 }
 
-bool Cirkuit::Generator::render(float zoomFactor)
-{
-    float factor = zoomFactor;
-    
-    if (factor < 0.1) {
-        factor = 0.1;
-    } else if (factor > 10) {
-        factor = 10;
-    }
-    
-    if (!formatExists(Format::Pdf)) {
-        return false;
-    }
-
-    Poppler::Document* document = Poppler::Document::load(formatPath(Format::Pdf));
-    if (!document || document->isLocked()) {
-        delete document;
-        return false;
-    }
-
-    // Access page of the PDF file
-    Poppler::Page* pdfPage = document->page(0);  // Document starts at page 0
-    if (pdfPage == 0) {
-        return false;
-    }
-
-    // Generate a QImage of the rendered page
-    QImage image = pdfPage->renderToImage(factor*300, factor*300);
-    emit previewReady(image);
-
-    delete pdfPage;
-    delete document;
-    
-    return true;
-}
-
 bool Cirkuit::Generator::convert(const Cirkuit::Format& in, const Cirkuit::Format& \
out)  {
     kDebug() << "Inside the converter..." << "in: " << in.type() << " " << \
                in.extension() << ", out: " << out.type() << " " << out.extension();
diff --git a/src/lib/generator.h b/src/lib/generator.h
index 9bc41d5..97f4c00 100644
--- a/src/lib/generator.h
+++ b/src/lib/generator.h
@@ -133,12 +133,6 @@ public slots:
      */
     int resolution() const;
     
-    /**
-     * Render the graphic and generate a QImage. This is an async call, 
-     * the result will be emitted through the previewReady signal
-     */
-    bool render(float zoomFactor = 1.0);
-    
 signals:
     /**
      * Signal emitted if the generation was successful
diff --git a/src/renderthread.cpp b/src/renderthread.cpp
new file mode 100644
index 0000000..4cdab22
--- /dev/null
+++ b/src/renderthread.cpp
@@ -0,0 +1,104 @@
+/*
+    Copyright (C) 2011  Matteo Agostinelli <agostinelli@gmail.com>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "renderthread.h"
+
+#include <poppler-qt4.h>
+
+#include <QFile>
+#include <QImage>
+
+RenderThread::RenderThread(QObject* parent): QThread(parent)
+{
+    m_restart = false;
+    m_abort = false;
+
+    setTerminationEnabled(true);
+}
+
+RenderThread::~RenderThread()
+{
+    m_mutex.lock();
+    m_abort = true;
+    m_condition.wakeOne();
+    m_mutex.unlock();
+
+    wait();
+}
+
+void RenderThread::generatePreview(const QString& pdfUrl, double zoomFactor)
+{
+    QMutexLocker locker(&m_mutex);
+
+    m_pdfUrl = pdfUrl;
+    m_zoomFactor = zoomFactor;
+    
+    if (!isRunning()) {
+        start(LowPriority);
+    } else {
+        m_abort = true;
+        m_condition.wakeAll();
+        m_abort = false;
+        m_restart = true;
+    }
+}
+
+void RenderThread::run()
+{
+    while(true) {     
+        if (m_abort) {
+            return;
+        }
+        
+        m_mutex.lock();
+        
+        float factor = qBound(0.1, m_zoomFactor, 10.0);
+            
+        if (!QFile::exists(m_pdfUrl)) return;
+
+        Poppler::Document* document = Poppler::Document::load(m_pdfUrl);
+        if (!document || document->isLocked()) {
+            delete document;
+            return;
+        }
+
+        // Access page of the PDF file
+        document->setRenderHint(Poppler::Document::Antialiasing, true);
+        document->setRenderHint(Poppler::Document::TextAntialiasing, true);
+        Poppler::Page* pdfPage = document->page(0);  // Document starts at page 0
+        if (pdfPage == 0) {
+            return;
+        }
+
+        m_mutex.unlock();
+        // Generate a QImage of the rendered page
+        QImage image = pdfPage->renderToImage(factor*300, factor*300);
+        emit previewReady(image);
+        
+        delete pdfPage;
+        delete document;
+        
+        // sleep
+        m_mutex.lock();
+        if (!m_restart) {
+            m_condition.wait(&m_mutex);
+        }
+        m_restart = false;
+        m_mutex.unlock();
+    }
+}
diff --git a/src/renderthread.h b/src/renderthread.h
new file mode 100644
index 0000000..1fbf2d9
--- /dev/null
+++ b/src/renderthread.h
@@ -0,0 +1,55 @@
+/*
+    Copyright (C) 2011  Matteo Agostinelli <agostinelli@gmail.com>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+
+#ifndef RENDERTHREAD_H
+#define RENDERTHREAD_H
+
+#include <QThread>
+#include <QMutex>
+#include <QWaitCondition>
+
+class QImage;
+
+class RenderThread : public QThread
+{
+    Q_OBJECT
+public:
+    explicit RenderThread(QObject* parent = 0);
+    virtual ~RenderThread();
+    
+public slots:
+    void generatePreview(const QString& pdfUrl, double zoomFactor = 1.0);
+    
+signals:
+    void previewReady(const QImage& image);
+    
+protected:
+    void run();
+     
+private:
+    QMutex m_mutex;
+    QWaitCondition m_condition;
+    bool m_restart;
+    bool m_abort;
+
+    QString m_pdfUrl;
+    qreal m_zoomFactor;
+};
+
+#endif // RENDERTHREAD_H


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

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