[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_Image_resizing_i?=
From:       Matteo Agostinelli <agostinelli () gmail ! com>
Date:       2011-01-31 21:32:31
Message-ID: 20110131213231.EEC53A60A9 () git ! kde ! org
[Download RAW message or body]

Git commit 416523752e914269c8de1c8a060de0d114001a87 by Matteo Agostinelli.
Pushed by agostinelli into branch 'zoomable-preview'.

Image resizing is now working

M  +10   -5    src/generatorthread.cpp     
M  +9    -6    src/generatorthread.h     
M  +13   -8    src/mainwindow.cpp     
M  +25   -56   src/renderthread.cpp     
M  +1    -8    src/renderthread.h     
M  +17   -8    src/widgets/imageview.cpp     
M  +4    -0    src/widgets/imageview.h     

http://commits.kde.org/74f3f67f/416523752e914269c8de1c8a060de0d114001a87

diff --git a/src/generatorthread.cpp b/src/generatorthread.cpp
index a5f0e0b..3a033c5 100644
--- a/src/generatorthread.cpp
+++ b/src/generatorthread.cpp
@@ -30,12 +30,13 @@
 
 using namespace Cirkuit;
 
-GeneratorThread::GeneratorThread(const Cirkuit::Format& in, const Cirkuit::Format& \
out, Cirkuit::Document* doc, QObject* parent): QThread(parent) \
+GeneratorThread::GeneratorThread(QObject* parent): QThread(parent)  {
+    m_previewUrl = QString();
     m_backend = 0;
+    m_doc = 0;
     m_render = new RenderThread;
     connect(m_render, SIGNAL(previewReady(QImage)), this, \
                SIGNAL(previewReady(QImage)));
-    setup(in, out, m_backend, doc, false);
 }
 
 void GeneratorThread::run()
@@ -67,6 +68,8 @@ void GeneratorThread::run()
         return;
     }
     
+    m_previewUrl = gen->formatPath(Format::Pdf);
+    emit previewUrl(m_previewUrl);
     if (m_output == Format::QtImage) {        
         m_render->generatePreview(gen->formatPath(Format::Pdf));
     }
@@ -83,16 +86,18 @@ GeneratorThread::~GeneratorThread()
     
 }
 
-void GeneratorThread::setup(const Cirkuit::Format& in, const Cirkuit::Format& out, \
Cirkuit::Backend* backend, Cirkuit::Document* doc, bool saveToFile) +void \
GeneratorThread::generate(const Cirkuit::Format& in, const Cirkuit::Format& out, \
Cirkuit::Backend* backend, Cirkuit::Document* doc, bool saveToFile)  {
     m_input = in;
     m_output = out;
     m_doc = doc;
     m_saveToFile = saveToFile;
     m_backend = backend;
+    
+    start(LowPriority);
 }
 
-Generator* GeneratorThread::generator()
+QString GeneratorThread::previewUrl() const
 {
-    return 0;
+    return m_previewUrl;
 }
diff --git a/src/generatorthread.h b/src/generatorthread.h
index b358d91..a06a249 100644
--- a/src/generatorthread.h
+++ b/src/generatorthread.h
@@ -37,20 +37,22 @@ class GeneratorThread : public QThread
 {
     Q_OBJECT
 public:
-    GeneratorThread(const Cirkuit::Format& in, const Cirkuit::Format& out, \
Cirkuit::Document* doc = 0, QObject* parent = 0); +    GeneratorThread(QObject* \
parent = 0);  ~GeneratorThread();
-        
-    Cirkuit::Generator* generator();
+    
+    QString previewUrl() const;
     
 protected:
     Cirkuit::Format m_input, m_output;
     
-public slots:
-    void setup(const Cirkuit::Format& in, const Cirkuit::Format& out, \
Cirkuit::Backend* backend = 0, Cirkuit::Document* doc = 0, bool saveToFile = false);  \
void run();  
+public slots:
+    void generate(const Cirkuit::Format& in, const Cirkuit::Format& out, \
Cirkuit::Backend* backend = 0, Cirkuit::Document* doc = 0, bool saveToFile = false); \
+      signals:
     void previewReady(const QImage);
+    void previewUrl(const QString);
     void fileReady(const QString);
     
     void error(const QString& appname, const QString& msg);
@@ -61,9 +63,10 @@ signals:
 private:
     Cirkuit::Document* m_doc;
     Cirkuit::Backend* m_backend;
-    
     RenderThread* m_render;
     bool m_saveToFile;
+    
+    QString m_previewUrl;
 };
 
 #endif // GRAPHICSGENERATOR_H
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 9109e61..dd79868 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -100,7 +100,7 @@ MainWindow::MainWindow(QWidget *)
 
     setGeometry(100,100,CirkuitSettings::width(),CirkuitSettings::height());
 
-    m_generator = new GeneratorThread(Cirkuit::Format::Source, \
Cirkuit::Format::QtImage, m_doc); +    m_generator = new GeneratorThread;
     
     m_updateTimer = 0;
     updateConfiguration();
@@ -115,6 +115,9 @@ MainWindow::MainWindow(QWidget *)
     connect(m_generator, SIGNAL(error(QString,QString)), m_logViewWidget, \
                SLOT(displayError(QString,QString)));
     connect(m_generator, SIGNAL(output(QString,QString)), m_logViewWidget, \
SLOT(displayMessage(QString,QString)));  
+    ImageView* view = m_livePreviewWidget->view();
+    connect(m_generator, SIGNAL(previewUrl(QString)), view, \
SLOT(setPdfUrl(QString))); +    
     checkCircuitMacros();
     initializeBackend();
     newDocument();
@@ -284,9 +287,8 @@ void MainWindow::exportFile()
         QFileInfo fileinfo(path);
         Cirkuit::Format format = \
Cirkuit::Format::fromMimeType(saveFileDialog.currentFilterMimeType());  \
                m_doc->setDirectory(m_currentFile.directory());
-        m_generator->setup(Cirkuit::Format::Source, format, m_backend, m_doc, true);
         statusBar()->showMessage("Exporting image...");
-        m_generator->start();
+        m_generator->generate(Cirkuit::Format::Source, format, m_backend, m_doc, \
true);  m_tempSavePath = path;
         QFile oldFile(path);
         oldFile.remove();
@@ -325,8 +327,7 @@ void MainWindow::buildPreview()
     m_logViewWidget->hide();
     
     m_doc->setDirectory(m_currentFile.directory());
-    m_generator->setup(Cirkuit::Format::Source, Cirkuit::Format::QtImage, m_backend, \
                m_doc);
-    m_generator->start();
+    m_generator->generate(Cirkuit::Format::Source, Cirkuit::Format::QtImage, \
m_backend, m_doc);  
     kDebug() << "Preview generation in progress...";
 }
@@ -340,10 +341,14 @@ void MainWindow::openPreview()
 
 void MainWindow::openPreviewFile()
 {
-    KUrl url = m_generator->generator()->formatPath(Cirkuit::Format::Pdf);
-    KRun::runUrl(url, "application/pdf", this);
-    
     disconnect(m_generator, SIGNAL(finished()), this, SLOT(openPreviewFile()));
+    
+    KUrl url = m_generator->previewUrl();
+    if (!url.isLocalFile()) {    
+        return;
+    }
+    
+    KRun::runUrl(url, "application/pdf", this);
 }
 
 void MainWindow::builtNotification()
diff --git a/src/renderthread.cpp b/src/renderthread.cpp
index 4cdab22..f704608 100644
--- a/src/renderthread.cpp
+++ b/src/renderthread.cpp
@@ -22,83 +22,52 @@
 
 #include <QFile>
 #include <QImage>
+#include <KDebug>
 
 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;
-    }
+    start(LowPriority);
 }
 
 void RenderThread::run()
 {
-    while(true) {     
-        if (m_abort) {
-            return;
-        }
-        
-        m_mutex.lock();
-        
-        float factor = qBound(0.1, m_zoomFactor, 10.0);
+    double 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;
-        }
+    if (!QFile::exists(m_pdfUrl)) 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;
+    Poppler::Document* document = Poppler::Document::load(m_pdfUrl);
+    if (!document || document->isLocked()) {
         delete document;
-        
-        // sleep
-        m_mutex.lock();
-        if (!m_restart) {
-            m_condition.wait(&m_mutex);
-        }
-        m_restart = false;
-        m_mutex.unlock();
+        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;
     }
+
+    // Generate a QImage of the rendered page
+    QImage image = pdfPage->renderToImage(factor*300.0, factor*300.0);
+    emit previewReady(image);
+    
+    delete pdfPage;
+    delete document;
 }
diff --git a/src/renderthread.h b/src/renderthread.h
index 1fbf2d9..27fb7bd 100644
--- a/src/renderthread.h
+++ b/src/renderthread.h
@@ -21,8 +21,6 @@
 #define RENDERTHREAD_H
 
 #include <QThread>
-#include <QMutex>
-#include <QWaitCondition>
 
 class QImage;
 
@@ -43,13 +41,8 @@ protected:
     void run();
      
 private:
-    QMutex m_mutex;
-    QWaitCondition m_condition;
-    bool m_restart;
-    bool m_abort;
-
     QString m_pdfUrl;
-    qreal m_zoomFactor;
+    double m_zoomFactor;
 };
 
 #endif // RENDERTHREAD_H
diff --git a/src/widgets/imageview.cpp b/src/widgets/imageview.cpp
index ba04061..4d077aa 100644
--- a/src/widgets/imageview.cpp
+++ b/src/widgets/imageview.cpp
@@ -18,13 +18,15 @@
 */
 
 #include "imageview.h"
+#include "renderthread.h"
 
 #include <QLabel>
 #include <QScrollBar>
 #include <QGraphicsScene>
 #include <QGraphicsPixmapItem>
+#include <KDebug>
 
-ImageView::ImageView(QWidget* parent): QGraphicsView(parent), m_image(QImage())
+ImageView::ImageView(QWidget* parent): QGraphicsView(parent), m_image(QImage()), \
m_pdfUrl(QString())  {
     m_scene = new QGraphicsScene(this);
     m_pixmap = m_scene->addPixmap(QPixmap::fromImage(m_image));
@@ -37,6 +39,9 @@ ImageView::ImageView(QWidget* parent): QGraphicsView(parent), \
m_image(QImage())  setMinimumWidth(100);
     setMinimumHeight(50);
     
+    m_render = new RenderThread;
+    connect(m_render, SIGNAL(previewReady(QImage)), this, SLOT(setImage(QImage)));
+    
     normalSize();
 }
 
@@ -50,6 +55,12 @@ void ImageView::setImage(const QImage& image)
 {
     m_pixmap->setPixmap(QPixmap::fromImage(image));
     m_pixmap->update();
+    setSceneRect(m_scene->itemsBoundingRect());
+}
+
+void ImageView::setPdfUrl(const QString& pdfUrl)
+{
+    m_pdfUrl = pdfUrl;
 }
 
 void ImageView::clear()
@@ -67,15 +78,13 @@ void ImageView::adjustScrollBar(QScrollBar* scrollBar, double \
factor)  void ImageView::scaleImage(double factor)
 {
     m_scaleFactor *= factor;
-    m_pixmap->setScale(m_scaleFactor);
+    m_render->generatePreview(m_pdfUrl, m_scaleFactor);
 
-    adjustScrollBar(this->horizontalScrollBar(), factor);
-    adjustScrollBar(this->verticalScrollBar(), factor);
-    
-    emit enableZoomIn(m_scaleFactor < 1.5);
-    emit enableZoomOut(m_scaleFactor > 0.1);
+    //adjustScrollBar(this->horizontalScrollBar(), factor);
+    //adjustScrollBar(this->verticalScrollBar(), factor);
     
-    setSceneRect(m_scene->itemsBoundingRect());
+    emit enableZoomIn(m_scaleFactor < 3.5);
+    emit enableZoomOut(m_scaleFactor > 0.1);    
 }
 
 void ImageView::normalSize()
diff --git a/src/widgets/imageview.h b/src/widgets/imageview.h
index 4e43be6..6905cfb 100644
--- a/src/widgets/imageview.h
+++ b/src/widgets/imageview.h
@@ -23,6 +23,7 @@
 #include <QImage>
 #include <QGraphicsView>
 
+class RenderThread;
 class QGraphicsPixmapItem;
 class QLabel;
 
@@ -37,6 +38,7 @@ public:
     
 public slots:
     void setImage(const QImage& image);
+    void setPdfUrl(const QString& pdfUrl);
     void clear();
     
     void zoomIn();
@@ -53,9 +55,11 @@ signals:
     
 private:
     QImage m_image;
+    QString m_pdfUrl;
     QLabel* m_imageLabel;
     QGraphicsScene* m_scene;
     QGraphicsPixmapItem* m_pixmap;
+    RenderThread* m_render;
     
     double m_scaleFactor;
 };


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

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