[poppler] 3 commits - qt4/demos qt4/src

Pino Toscano pino at kemper.freedesktop.org
Tue Jun 2 16:27:28 PDT 2009


 qt4/demos/CMakeLists.txt |    1 
 qt4/demos/pageview.cpp   |    5 ++
 qt4/demos/pageview.h     |    3 +
 qt4/demos/thumbnails.cpp |   84 +++++++++++++++++++++++++++++++++++++++++++++++
 qt4/demos/thumbnails.h   |   51 ++++++++++++++++++++++++++++
 qt4/demos/viewer.cpp     |   11 +++++-
 qt4/src/poppler-page.cc  |   21 +++++++++++
 qt4/src/poppler-qt4.h    |   11 ++++++
 8 files changed, 185 insertions(+), 2 deletions(-)

New commits:
commit f69a4fe47d3478cccbc03e4ac0929c7eada681c0
Author: Pino Toscano <pino at kde.org>
Date:   Wed Jun 3 01:24:21 2009 +0200

    [Qt4 demo] Add a thumbnail dock widget
    
    Useful to show the embedded page thumbnails in the document, if any.
    Based on a patch by Shawn Rutledge <shawn.t.rutledge at gmail.com>, reworked by me to be a QListWidget showing the page items at their full size.

diff --git a/qt4/demos/CMakeLists.txt b/qt4/demos/CMakeLists.txt
index 59b3f61..76accf8 100644
--- a/qt4/demos/CMakeLists.txt
+++ b/qt4/demos/CMakeLists.txt
@@ -19,6 +19,7 @@ set(poppler_qt4viewer_SRCS
   optcontent.cpp
   pageview.cpp
   permissions.cpp
+  thumbnails.cpp
   toc.cpp
   viewer.cpp
 )
diff --git a/qt4/demos/thumbnails.cpp b/qt4/demos/thumbnails.cpp
new file mode 100644
index 0000000..ba82c2d
--- /dev/null
+++ b/qt4/demos/thumbnails.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2009, Shawn Rutledge <shawn.t.rutledge at gmail.com>
+ * Copyright (C) 2009, Pino Toscano <pino at kde.org>
+ *
+ * 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, 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 "thumbnails.h"
+
+#include <poppler-qt4.h>
+
+#include <QtGui/QListWidget>
+
+static const int PageRole = Qt::UserRole + 1;
+
+ThumbnailsDock::ThumbnailsDock(QWidget *parent)
+    : AbstractInfoDock(parent)
+{
+    m_list = new QListWidget(this);
+    setWidget(m_list);
+    setWindowTitle(tr("Thumbnails"));
+    m_list->setViewMode(QListView::ListMode);
+    m_list->setMovement(QListView::Static);
+    m_list->setVerticalScrollMode(QListView::ScrollPerPixel);
+    connect(m_list, SIGNAL(itemActivated(QListWidgetItem*)),
+            this, SLOT(slotItemActivated(QListWidgetItem*)));
+}
+
+ThumbnailsDock::~ThumbnailsDock()
+{
+}
+
+void ThumbnailsDock::fillInfo()
+{
+    const int num = document()->numPages();
+    QSize maxSize;
+    for (int i = 0; i < num; ++i) {
+        const Poppler::Page *page = document()->page(i);
+        const QImage image = page->thumbnail();
+        if (!image.isNull()) {
+            QListWidgetItem *item = new QListWidgetItem();
+            item->setText(QString::number(i + 1));
+            item->setData(Qt::DecorationRole, QPixmap::fromImage(image));
+            item->setData(PageRole, i);
+            m_list->addItem(item);
+            maxSize.setWidth(qMax(maxSize.width(), image.width()));
+            maxSize.setHeight(qMax(maxSize.height(), image.height()));
+        }
+        delete page;
+    }
+    if (num > 0) {
+        m_list->setGridSize(maxSize);
+        m_list->setIconSize(maxSize);
+    }
+}
+
+void ThumbnailsDock::documentClosed()
+{
+    m_list->clear();
+    AbstractInfoDock::documentClosed();
+}
+
+void ThumbnailsDock::slotItemActivated(QListWidgetItem *item)
+{
+    if (!item) {
+        return;
+    }
+
+    emit pageClicked(item->data(PageRole).toInt());
+}
+
+#include "thumbnails.moc"
diff --git a/qt4/demos/thumbnails.h b/qt4/demos/thumbnails.h
new file mode 100644
index 0000000..e426b12
--- /dev/null
+++ b/qt4/demos/thumbnails.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2009, Shawn Rutledge <shawn.t.rutledge at gmail.com>
+ * Copyright (C) 2009, Pino Toscano <pino at kde.org>
+ *
+ * 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, 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 THUMBNAILS_H
+#define THUMBNAILS_H
+
+#include "abstractinfodock.h"
+
+class QListWidget;
+class QListWidgetItem;
+
+class ThumbnailsDock : public AbstractInfoDock
+{
+    Q_OBJECT
+
+public:
+    ThumbnailsDock(QWidget *parent = 0);
+    ~ThumbnailsDock();
+
+    /*virtual*/ void documentClosed();
+
+Q_SIGNALS:
+    void pageClicked(int page);
+
+protected:
+    /*virtual*/ void fillInfo();
+
+private Q_SLOTS:
+    void slotItemActivated(QListWidgetItem *item);
+
+private:
+    QListWidget *m_list;
+};
+
+#endif
diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index 61cea0a..c85e079 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -1,6 +1,7 @@
 /*
- * Copyright (C) 2008, Pino Toscano <pino at kde.org>
+ * Copyright (C) 2008-2009, Pino Toscano <pino at kde.org>
  * Copyright (C) 2008, Albert Astals Cid <aacid at kde.org>
+ * Copyright (C) 2009, Shawn Rutledge <shawn.t.rutledge at 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
@@ -27,6 +28,7 @@
 #include "optcontent.h"
 #include "pageview.h"
 #include "permissions.h"
+#include "thumbnails.h"
 #include "toc.h"
 
 #include <poppler-qt4.h>
@@ -117,6 +119,13 @@ PdfViewer::PdfViewer()
     viewMenu->addAction(permissionsDock->toggleViewAction());
     m_observers.append(permissionsDock);
 
+    ThumbnailsDock *thumbnailsDock = new ThumbnailsDock(this);
+    addDockWidget(Qt::LeftDockWidgetArea, thumbnailsDock);
+    thumbnailsDock->hide();
+    viewMenu->addAction(thumbnailsDock->toggleViewAction());
+    m_observers.append(thumbnailsDock);
+    connect(thumbnailsDock, SIGNAL(pageClicked(int)), view, SLOT(setPage(int)));
+
     EmbeddedFilesDock *embfilesDock = new EmbeddedFilesDock(this);
     addDockWidget(Qt::BottomDockWidgetArea, embfilesDock);
     embfilesDock->hide();
commit 2f24ac0f4bbd6de1c9f137110b3ac2bd3b23b0b9
Author: Shawn Rutledge <shawn.t.rutledge at gmail.com>
Date:   Wed Jun 3 01:22:31 2009 +0200

    [Qt4 demo] add a public slot to set the current page

diff --git a/qt4/demos/pageview.cpp b/qt4/demos/pageview.cpp
index 3420a63..b0165e2 100644
--- a/qt4/demos/pageview.cpp
+++ b/qt4/demos/pageview.cpp
@@ -60,4 +60,9 @@ void PageView::pageChanged(int page)
     delete popplerPage;
 }
 
+void PageView::setPage(int page)
+{
+    DocumentObserver::setPage(page);
+}
+
 #include "pageview.moc"
diff --git a/qt4/demos/pageview.h b/qt4/demos/pageview.h
index abf5a50..a6f10ec 100644
--- a/qt4/demos/pageview.h
+++ b/qt4/demos/pageview.h
@@ -37,6 +37,9 @@ public:
     /*virtual*/ void documentClosed();
     /*virtual*/ void pageChanged(int page);
 
+public Q_SLOTS:
+    void setPage(int page);
+
 private:
     QLabel *m_imageLabel;
 };
commit 1f1baf186f9e37606765e51ec5b9893ddfbfa893
Author: Shawn Rutledge <shawn.t.rutledge at gmail.com>
Date:   Wed Jun 3 01:19:16 2009 +0200

    [Qt4] Add Page::thumbnail()
    
    This function can be used to get the embedded thumbnail of the page, present in the document.
    Reindented by me, and avoid a memory leak because of non-freed data.

diff --git a/qt4/src/poppler-page.cc b/qt4/src/poppler-page.cc
index 9a27538..f3edfe6 100644
--- a/qt4/src/poppler-page.cc
+++ b/qt4/src/poppler-page.cc
@@ -3,8 +3,9 @@
  * Copyright (C) 2005, Brad Hards <bradh at frogmouth.net>
  * Copyright (C) 2005-2008, Albert Astals Cid <aacid at kde.org>
  * Copyright (C) 2005, Stefan Kebekus <stefan.kebekus at math.uni-koeln.de>
- * Copyright (C) 2006-2008, Pino Toscano <pino at kde.org>
+ * Copyright (C) 2006-2009, Pino Toscano <pino at kde.org>
  * Copyright (C) 2008 Carlos Garcia Campos <carlosgc at gnome.org>
+ * Copyright (C) 2009 Shawn Rutledge <shawn.t.rutledge at 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
@@ -277,6 +278,24 @@ QImage Page::renderToImage(double xres, double yres, int x, int y, int w, int h,
   return img;
 }
 
+QImage Page::thumbnail() const
+{
+  unsigned char* data = 0;
+  int w = 0;
+  int h = 0;
+  int rowstride = 0;
+  GBool r = m_page->page->loadThumb(&data, &w, &h, &rowstride);
+  QImage ret;
+  if (r)
+  {
+    // first construct a temporary image with the data got,
+    // then force a copy of it so we can free the raw thumbnail data
+    ret = QImage(data, w, h, rowstride, QImage::Format_RGB888).copy();
+    gfree(data);
+  }
+  return ret;
+}
+
 QString Page::text(const QRectF &r) const
 {
   TextOutputDev *output_dev;
diff --git a/qt4/src/poppler-qt4.h b/qt4/src/poppler-qt4.h
index 05c4c0d..257162e 100644
--- a/qt4/src/poppler-qt4.h
+++ b/qt4/src/poppler-qt4.h
@@ -4,6 +4,7 @@
  * Copyright (C) 2005-2008, Albert Astals Cid <aacid at kde.org>
  * Copyright (C) 2005, Stefan Kebekus <stefan.kebekus at math.uni-koeln.de>
  * Copyright (C) 2006-2009, Pino Toscano <pino at kde.org>
+ * Copyright (C) 2009 Shawn Rutledge <shawn.t.rutledge at 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
@@ -417,6 +418,16 @@ while (it->hasNext()) {
 	QImage renderToImage(double xres=72.0, double yres=72.0, int x=-1, int y=-1, int w=-1, int h=-1, Rotation rotate = Rotate0) const;
 
 	/**
+	   Get the page thumbnail if it exists.
+
+	   \return a QImage of the thumbnail, or a null image
+	   if the PDF does not contain one for this page
+
+	   \since 0.12
+	*/
+	QImage thumbnail() const;
+
+	/**
 	   Returns the text that is inside a specified rectangle
 
 	   \param rect the rectangle specifying the area of interest,


More information about the poppler mailing list