[poppler] 6 commits - qt4/demos

Pino Toscano pino at kemper.freedesktop.org
Sat Feb 16 09:50:31 PST 2008


 qt4/demos/CMakeLists.txt        |    5 +
 qt4/demos/Makefile.am           |    5 +
 qt4/demos/documentobserver.cpp  |   45 +++++++++++++++
 qt4/demos/documentobserver.h    |   49 ++++++++++++++++
 qt4/demos/fonts.cpp             |   78 ++++++++++++++++++++++++++
 qt4/demos/fonts.h               |   44 +++++++++++++++
 qt4/demos/info.cpp              |   78 ++++++++++++++++++++++++++
 qt4/demos/info.h                |   44 +++++++++++++++
 qt4/demos/navigationtoolbar.cpp |   98 +++++++++++++++++++++++++++++++++
 qt4/demos/navigationtoolbar.h   |   56 +++++++++++++++++++
 qt4/demos/pageview.cpp          |   61 +++++++++++++++++++++
 qt4/demos/pageview.h            |   44 +++++++++++++++
 qt4/demos/viewer.cpp            |  116 ++++++++++++++++++++++++++++++++++++++--
 qt4/demos/viewer.h              |   17 +++++
 14 files changed, 736 insertions(+), 4 deletions(-)

New commits:
commit 0f7d51c7fc2439ee9392c166576c341238f00f36
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 18:49:52 2008 +0100

    Delete the poppler page after usage.

diff --git a/qt4/demos/pageview.cpp b/qt4/demos/pageview.cpp
index f5c929c..9e57227 100644
--- a/qt4/demos/pageview.cpp
+++ b/qt4/demos/pageview.cpp
@@ -55,6 +55,7 @@ void PageView::pageChanged(int page)
         m_imageLabel->resize(0, 0);
         m_imageLabel->setPixmap(QPixmap());
     }
+    delete popplerPage;
 }
 
 #include "pageview.moc"
commit b41d069cdd1435ddb14b3de2986875069523c814
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 18:48:57 2008 +0100

    Initialize the current page number correctly.

diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index 5e10790..79cd0ff 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -35,7 +35,7 @@
 #include <QtGui/QMessageBox>
 
 PdfViewer::PdfViewer()
-    : QMainWindow(), m_doc(0)
+    : QMainWindow(), m_currentPage(0), m_doc(0)
 {
     // setup the menus
     QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
commit 75e516af0e72a2e3041660300e522ad00869372b
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 18:43:56 2008 +0100

    Add a side dock for showing the fonts of the document.

diff --git a/qt4/demos/CMakeLists.txt b/qt4/demos/CMakeLists.txt
index 327439c..ca3b828 100644
--- a/qt4/demos/CMakeLists.txt
+++ b/qt4/demos/CMakeLists.txt
@@ -10,6 +10,7 @@ include_directories(
 
 set(poppler_qt4viewer_SRCS
   documentobserver.cpp
+  fonts.cpp
   info.cpp
   main_viewer.cpp
   navigationtoolbar.cpp
diff --git a/qt4/demos/Makefile.am b/qt4/demos/Makefile.am
index dee3501..33354a9 100644
--- a/qt4/demos/Makefile.am
+++ b/qt4/demos/Makefile.am
@@ -22,6 +22,7 @@ noinst_PROGRAMS = poppler_qt4viewer
 
 poppler_qt4viewer_SOURCES =			\
 	documentobserver.cpp			\
+	fonts.cpp				\
 	info.cpp				\
 	main_viewer.cpp				\
 	navigationtoolbar.cpp			\
diff --git a/qt4/demos/fonts.cpp b/qt4/demos/fonts.cpp
new file mode 100644
index 0000000..017faae
--- /dev/null
+++ b/qt4/demos/fonts.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2008, 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 "fonts.h"
+
+#include <poppler-qt4.h>
+
+#include <QtGui/QTableWidget>
+
+static QString yesNoStatement(bool value)
+{
+    return value ? QString::fromLatin1("yes") : QString::fromLatin1("no");
+}
+
+FontsDock::FontsDock(QWidget *parent)
+    : QDockWidget(parent)
+{
+    m_table = new QTableWidget(this);
+    setWidget(m_table);
+    setWindowTitle(tr("Fonts"));
+    m_table->setColumnCount(5);
+    m_table->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Type") << tr("Embedded") << tr("Subset") << tr("File"));
+#if QT_VERSION >= 0x040200
+    m_table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
+#endif
+}
+
+FontsDock::~FontsDock()
+{
+}
+
+void FontsDock::documentLoaded()
+{
+    const QList<Poppler::FontInfo> fonts = document()->fonts();
+    m_table->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Type") << tr("Embedded") << tr("Subset") << tr("File"));
+    m_table->setRowCount(fonts.count());
+    int i = 0;
+    Q_FOREACH(const Poppler::FontInfo &font, fonts) {
+        if (font.name().isNull()) {
+            m_table->setItem(i, 0, new QTableWidgetItem(QString::fromLatin1("[none]")));
+        } else {
+            m_table->setItem(i, 0, new QTableWidgetItem(font.name()));
+        }
+        m_table->setItem(i, 1, new QTableWidgetItem(font.typeName()));
+        m_table->setItem(i, 2, new QTableWidgetItem(yesNoStatement(font.isEmbedded())));
+        m_table->setItem(i, 3, new QTableWidgetItem(yesNoStatement(font.isSubset())));
+        m_table->setItem(i, 4, new QTableWidgetItem(font.file()));
+        ++i;
+    }
+}
+
+void FontsDock::documentClosed()
+{
+    m_table->clear();
+    m_table->setRowCount(0);
+}
+
+void FontsDock::pageChanged(int page)
+{
+    Q_UNUSED(page)
+}
+
+#include "fonts.moc"
diff --git a/qt4/demos/fonts.h b/qt4/demos/fonts.h
new file mode 100644
index 0000000..8612a33
--- /dev/null
+++ b/qt4/demos/fonts.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2008, 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 FONTS_H
+#define FONTS_H
+
+#include <QtGui/QDockWidget>
+
+#include "documentobserver.h"
+
+class QTableWidget;
+
+class FontsDock : public QDockWidget, public DocumentObserver
+{
+    Q_OBJECT
+
+public:
+    FontsDock(QWidget *parent = 0);
+    ~FontsDock();
+
+    /*virtual*/ void documentLoaded();
+    /*virtual*/ void documentClosed();
+    /*virtual*/ void pageChanged(int page);
+
+private:
+    QTableWidget *m_table;
+};
+
+#endif
diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index e693266..5e10790 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -18,6 +18,7 @@
 
 #include "viewer.h"
 
+#include "fonts.h"
 #include "info.h"
 #include "navigationtoolbar.h"
 #include "pageview.h"
@@ -59,6 +60,12 @@ PdfViewer::PdfViewer()
     viewMenu->addAction(infoDock->toggleViewAction());
     m_observers.append(infoDock);
 
+    FontsDock *fontsDock = new FontsDock(this);
+    addDockWidget(Qt::LeftDockWidgetArea, fontsDock);
+    fontsDock->hide();
+    viewMenu->addAction(fontsDock->toggleViewAction());
+    m_observers.append(fontsDock);
+
     Q_FOREACH(DocumentObserver *obs, m_observers) {
         obs->m_viewer = this;
     }
commit 8e642d1cbd48d4790a6769287cbfd90c3bc9fc34
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 17:21:30 2008 +0100

    Add a View menu where the dock widget toogle actions will be.
    
    As start, put the Info dock there.

diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index 21a5be2..e693266 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -43,6 +43,8 @@ PdfViewer::PdfViewer()
     QAction *act = fileMenu->addAction(tr("&Quit"), qApp, SLOT(closeAllWindows()));
     act->setShortcut(Qt::CTRL + Qt::Key_Q);
 
+    QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
+
     NavigationToolBar *navbar = new NavigationToolBar(this);
     addToolBar(navbar);
     m_observers.append(navbar);
@@ -53,6 +55,8 @@ PdfViewer::PdfViewer()
 
     InfoDock *infoDock = new InfoDock(this);
     addDockWidget(Qt::LeftDockWidgetArea, infoDock);
+    infoDock->hide();
+    viewMenu->addAction(infoDock->toggleViewAction());
     m_observers.append(infoDock);
 
     Q_FOREACH(DocumentObserver *obs, m_observers) {
commit c1feb7c2b5008cc04edb3c028f888072920ce4dc
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 17:14:00 2008 +0100

    Add a side dock for showing the info keys.

diff --git a/qt4/demos/CMakeLists.txt b/qt4/demos/CMakeLists.txt
index ca5cbbd..327439c 100644
--- a/qt4/demos/CMakeLists.txt
+++ b/qt4/demos/CMakeLists.txt
@@ -10,6 +10,7 @@ include_directories(
 
 set(poppler_qt4viewer_SRCS
   documentobserver.cpp
+  info.cpp
   main_viewer.cpp
   navigationtoolbar.cpp
   pageview.cpp
diff --git a/qt4/demos/Makefile.am b/qt4/demos/Makefile.am
index 6195ae4..dee3501 100644
--- a/qt4/demos/Makefile.am
+++ b/qt4/demos/Makefile.am
@@ -22,6 +22,7 @@ noinst_PROGRAMS = poppler_qt4viewer
 
 poppler_qt4viewer_SOURCES =			\
 	documentobserver.cpp			\
+	info.cpp				\
 	main_viewer.cpp				\
 	navigationtoolbar.cpp			\
 	pageview.cpp				\
diff --git a/qt4/demos/info.cpp b/qt4/demos/info.cpp
new file mode 100644
index 0000000..6ba8dff
--- /dev/null
+++ b/qt4/demos/info.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2008, 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 "info.h"
+
+#include <poppler-qt4.h>
+
+#include <QtGui/QTableWidget>
+
+InfoDock::InfoDock(QWidget *parent)
+    : QDockWidget(parent)
+{
+    m_table = new QTableWidget(this);
+    setWidget(m_table);
+    setWindowTitle(tr("Information"));
+    m_table->setColumnCount(2);
+    m_table->setHorizontalHeaderLabels(QStringList() << tr("Key") << tr("Value"));
+#if QT_VERSION >= 0x040200
+    m_table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
+#endif
+}
+
+InfoDock::~InfoDock()
+{
+}
+
+void InfoDock::documentLoaded()
+{
+    QStringList keys = document()->infoKeys();
+    m_table->setHorizontalHeaderLabels(QStringList() << tr("Key") << tr("Value"));
+    m_table->setRowCount(keys.count());
+    QStringList dateKeys;
+    dateKeys << QString::fromLatin1("CreationDate");
+    dateKeys << QString::fromLatin1("ModDate");
+    int i = 0;
+    Q_FOREACH(const QString &date, dateKeys) {
+        const int id = keys.indexOf(date);
+        if (id != -1) {
+            m_table->setItem(i, 0, new QTableWidgetItem(date));
+            m_table->setItem(i, 1, new QTableWidgetItem(document()->date(date).toString(Qt::SystemLocaleDate)));
+            ++i;
+            keys.removeAt(id);
+        }
+    }
+    Q_FOREACH(const QString &key, keys) {
+        m_table->setItem(i, 0, new QTableWidgetItem(key));
+        m_table->setItem(i, 1, new QTableWidgetItem(document()->info(key)));
+        ++i;
+    }
+}
+
+void InfoDock::documentClosed()
+{
+    m_table->clear();
+    m_table->setRowCount(0);
+}
+
+void InfoDock::pageChanged(int page)
+{
+    Q_UNUSED(page)
+}
+
+#include "info.moc"
diff --git a/qt4/demos/info.h b/qt4/demos/info.h
new file mode 100644
index 0000000..6c46e1d
--- /dev/null
+++ b/qt4/demos/info.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2008, 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 INFO_H
+#define INFO_H
+
+#include <QtGui/QDockWidget>
+
+#include "documentobserver.h"
+
+class QTableWidget;
+
+class InfoDock : public QDockWidget, public DocumentObserver
+{
+    Q_OBJECT
+
+public:
+    InfoDock(QWidget *parent = 0);
+    ~InfoDock();
+
+    /*virtual*/ void documentLoaded();
+    /*virtual*/ void documentClosed();
+    /*virtual*/ void pageChanged(int page);
+
+private:
+    QTableWidget *m_table;
+};
+
+#endif
diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index f6e4a17..21a5be2 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -18,6 +18,7 @@
 
 #include "viewer.h"
 
+#include "info.h"
 #include "navigationtoolbar.h"
 #include "pageview.h"
 
@@ -50,6 +51,10 @@ PdfViewer::PdfViewer()
     setCentralWidget(view);
     m_observers.append(view);
 
+    InfoDock *infoDock = new InfoDock(this);
+    addDockWidget(Qt::LeftDockWidgetArea, infoDock);
+    m_observers.append(infoDock);
+
     Q_FOREACH(DocumentObserver *obs, m_observers) {
         obs->m_viewer = this;
     }
commit abc9b00c6470f9f6b66c280455f544ad0ee3aa8c
Author: Pino Toscano <pino at kde.org>
Date:   Sat Feb 16 16:09:52 2008 +0100

    Basic work in the demo PDF viewer, open documents and navigate into the pages.
    
    An observer structure was introduced, so extra components can be easily added and made aware of the document/page changes.
    Both the navigation toolbar and the page view are implemented as observers.

diff --git a/qt4/demos/CMakeLists.txt b/qt4/demos/CMakeLists.txt
index 17367bb..ca5cbbd 100644
--- a/qt4/demos/CMakeLists.txt
+++ b/qt4/demos/CMakeLists.txt
@@ -9,7 +9,10 @@ include_directories(
 )
 
 set(poppler_qt4viewer_SRCS
+  documentobserver.cpp
   main_viewer.cpp
+  navigationtoolbar.cpp
+  pageview.cpp
   viewer.cpp
 )
 qt4_automoc(${poppler_qt4viewer_SRCS})
diff --git a/qt4/demos/Makefile.am b/qt4/demos/Makefile.am
index a93adb7..6195ae4 100644
--- a/qt4/demos/Makefile.am
+++ b/qt4/demos/Makefile.am
@@ -21,7 +21,10 @@ SUFFIXES: .moc
 noinst_PROGRAMS = poppler_qt4viewer
 
 poppler_qt4viewer_SOURCES =			\
+	documentobserver.cpp			\
 	main_viewer.cpp				\
+	navigationtoolbar.cpp			\
+	pageview.cpp				\
 	viewer.cpp				\
 	viewer.moc
 
diff --git a/qt4/demos/documentobserver.cpp b/qt4/demos/documentobserver.cpp
new file mode 100644
index 0000000..611ba79
--- /dev/null
+++ b/qt4/demos/documentobserver.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2008, 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 "documentobserver.h"
+
+#include "viewer.h"
+
+DocumentObserver::DocumentObserver()
+    : m_viewer(0)
+{
+}
+
+DocumentObserver::~DocumentObserver()
+{
+}
+
+Poppler::Document* DocumentObserver::document() const
+{
+    return m_viewer->m_doc;
+}
+
+void DocumentObserver::setPage(int page)
+{
+    m_viewer->setPage(page);
+}
+
+int DocumentObserver::page() const
+{
+    return m_viewer->page();
+}
diff --git a/qt4/demos/documentobserver.h b/qt4/demos/documentobserver.h
new file mode 100644
index 0000000..c6e287b
--- /dev/null
+++ b/qt4/demos/documentobserver.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2008, 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 DOCUMENTOBSERVER_H
+#define DOCUMENTOBSERVER_H
+
+class PdfViewer;
+namespace Poppler {
+class Document;
+}
+
+class DocumentObserver
+{
+friend class PdfViewer;
+
+public:
+    virtual ~DocumentObserver();
+
+    virtual void documentLoaded() = 0;
+    virtual void documentClosed() = 0;
+    virtual void pageChanged(int page) = 0;
+
+protected:
+    DocumentObserver();
+
+    Poppler::Document* document() const;
+    void setPage(int page);
+    int page() const;
+
+private:
+    PdfViewer *m_viewer;
+};
+
+#endif
diff --git a/qt4/demos/navigationtoolbar.cpp b/qt4/demos/navigationtoolbar.cpp
new file mode 100644
index 0000000..65a4c54
--- /dev/null
+++ b/qt4/demos/navigationtoolbar.cpp
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2008, 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 "navigationtoolbar.h"
+
+#include <poppler-qt4.h>
+
+#include <QtGui/QAction>
+#include <QtGui/QComboBox>
+
+NavigationToolBar::NavigationToolBar(QWidget *parent)
+    : QToolBar(parent)
+{
+    m_firstAct = addAction(tr("First"), this, SLOT(slotGoFirst()));
+    m_prevAct = addAction(tr("Previous"), this, SLOT(slotGoPrev()));
+    m_pageCombo = new QComboBox(this);
+    connect(m_pageCombo, SIGNAL(activated(int)), this, SLOT(slotComboActivated(int)));
+    addWidget(m_pageCombo);
+    m_nextAct = addAction(tr("Next"), this, SLOT(slotGoNext()));
+    m_lastAct = addAction(tr("Last"), this, SLOT(slotGoLast()));
+
+    documentClosed();
+}
+
+NavigationToolBar::~NavigationToolBar()
+{
+}
+
+void NavigationToolBar::documentLoaded()
+{
+    const int pageCount = document()->numPages();
+    for (int i = 0; i < pageCount; ++i) {
+        m_pageCombo->addItem(QString::number(i));
+    }
+    m_pageCombo->setEnabled(true);
+}
+
+void NavigationToolBar::documentClosed()
+{
+    m_firstAct->setEnabled(false);
+    m_prevAct->setEnabled(false);
+    m_nextAct->setEnabled(false);
+    m_lastAct->setEnabled(false);
+    m_pageCombo->clear();
+    m_pageCombo->setEnabled(false);
+}
+
+void NavigationToolBar::pageChanged(int page)
+{
+    const int pageCount = document()->numPages();
+    m_firstAct->setEnabled(page > 0);
+    m_prevAct->setEnabled(page > 0);
+    m_nextAct->setEnabled(page < (pageCount - 1));
+    m_lastAct->setEnabled(page < (pageCount - 1));
+    m_pageCombo->setCurrentIndex(page);
+}
+
+void NavigationToolBar::slotGoFirst()
+{
+    setPage(0);
+}
+
+void NavigationToolBar::slotGoPrev()
+{
+    setPage(page() - 1);
+}
+
+void NavigationToolBar::slotGoNext()
+{
+    setPage(page() + 1);
+}
+
+void NavigationToolBar::slotGoLast()
+{
+    setPage(document()->numPages() - 1);
+}
+
+void NavigationToolBar::slotComboActivated(int index)
+{
+    setPage(index);
+}
+
+#include "navigationtoolbar.moc"
diff --git a/qt4/demos/navigationtoolbar.h b/qt4/demos/navigationtoolbar.h
new file mode 100644
index 0000000..3c79de6
--- /dev/null
+++ b/qt4/demos/navigationtoolbar.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2008, 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 NAVIGATIONTOOLBAR_H
+#define NAVIGATIONTOOLBAR_H
+
+#include <QtGui/QToolBar>
+
+#include "documentobserver.h"
+
+class QAction;
+class QComboBox;
+
+class NavigationToolBar : public QToolBar, public DocumentObserver
+{
+    Q_OBJECT
+
+public:
+    NavigationToolBar(QWidget *parent = 0);
+    ~NavigationToolBar();
+
+    /*virtual*/ void documentLoaded();
+    /*virtual*/ void documentClosed();
+    /*virtual*/ void pageChanged(int page);
+
+private Q_SLOTS:
+    void slotGoFirst();
+    void slotGoPrev();
+    void slotGoNext();
+    void slotGoLast();
+    void slotComboActivated(int index);
+
+private:
+    QAction *m_firstAct;
+    QAction *m_prevAct;
+    QComboBox *m_pageCombo;
+    QAction *m_nextAct;
+    QAction *m_lastAct;
+};
+
+#endif
diff --git a/qt4/demos/pageview.cpp b/qt4/demos/pageview.cpp
new file mode 100644
index 0000000..f5c929c
--- /dev/null
+++ b/qt4/demos/pageview.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2008, 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 "pageview.h"
+
+#include <poppler-qt4.h>
+
+#include <QtGui/QImage>
+#include <QtGui/QLabel>
+#include <QtGui/QPixmap>
+
+PageView::PageView(QWidget *parent)
+    : QScrollArea(parent)
+{
+    m_imageLabel = new QLabel(this);
+    m_imageLabel->resize(0, 0);
+    setWidget(m_imageLabel);
+}
+
+PageView::~PageView()
+{
+}
+
+void PageView::documentLoaded()
+{
+}
+
+void PageView::documentClosed()
+{
+}
+
+void PageView::pageChanged(int page)
+{
+    Poppler::Page *popplerPage = document()->page(page);
+    QImage image = popplerPage->renderToImage();
+    if (!image.isNull()) {
+        m_imageLabel->resize(image.size());
+        m_imageLabel->setPixmap(QPixmap::fromImage(image));
+    } else {
+        m_imageLabel->resize(0, 0);
+        m_imageLabel->setPixmap(QPixmap());
+    }
+}
+
+#include "pageview.moc"
diff --git a/qt4/demos/pageview.h b/qt4/demos/pageview.h
new file mode 100644
index 0000000..abf5a50
--- /dev/null
+++ b/qt4/demos/pageview.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2008, 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 PAGEVIEW_H
+#define PAGEVIEW_H
+
+#include <QtGui/QScrollArea>
+
+#include "documentobserver.h"
+
+class QLabel;
+
+class PageView : public QScrollArea, public DocumentObserver
+{
+    Q_OBJECT
+
+public:
+    PageView(QWidget *parent = 0);
+    ~PageView();
+
+    /*virtual*/ void documentLoaded();
+    /*virtual*/ void documentClosed();
+    /*virtual*/ void pageChanged(int page);
+
+private:
+    QLabel *m_imageLabel;
+};
+
+#endif
diff --git a/qt4/demos/viewer.cpp b/qt4/demos/viewer.cpp
index a726bd3..f6e4a17 100644
--- a/qt4/demos/viewer.cpp
+++ b/qt4/demos/viewer.cpp
@@ -18,29 +18,121 @@
 
 #include "viewer.h"
 
+#include "navigationtoolbar.h"
+#include "pageview.h"
+
 #include <poppler-qt4.h>
 
+#include <QtCore/QDir>
 #include <QtGui/QAction>
 #include <QtGui/QApplication>
+#include <QtGui/QFileDialog>
+#include <QtGui/QInputDialog>
 #include <QtGui/QMenu>
 #include <QtGui/QMenuBar>
+#include <QtGui/QMessageBox>
 
 PdfViewer::PdfViewer()
     : QMainWindow(), m_doc(0)
 {
     // setup the menus
     QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
-    m_fileOpenAct = fileMenu->addAction(tr("&Open"));
-    fileMenu->addAction(tr("&Quit"), qApp, SLOT(closeAllWindows()));
+    m_fileOpenAct = fileMenu->addAction(tr("&Open"), this, SLOT(slotOpenFile()));
+    m_fileOpenAct->setShortcut(Qt::CTRL + Qt::Key_O);
+    QAction *act = fileMenu->addAction(tr("&Quit"), qApp, SLOT(closeAllWindows()));
+    act->setShortcut(Qt::CTRL + Qt::Key_Q);
+
+    NavigationToolBar *navbar = new NavigationToolBar(this);
+    addToolBar(navbar);
+    m_observers.append(navbar);
+
+    PageView *view = new PageView(this);
+    setCentralWidget(view);
+    m_observers.append(view);
+
+    Q_FOREACH(DocumentObserver *obs, m_observers) {
+        obs->m_viewer = this;
+    }
 }
 
 PdfViewer::~PdfViewer()
 {
-    delete m_doc;
+    closeDocument();
+}
+
+QSize PdfViewer::sizeHint() const
+{
+    return QSize(500, 600);
 }
 
 void PdfViewer::loadDocument(const QString &file)
 {
+    Poppler::Document *newdoc = Poppler::Document::load(file);
+    if (!newdoc) {
+        QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Cannot open:\n") + file,
+                           QMessageBox::Ok, this);
+        msgbox.exec();
+        return;
+    }
+
+    while (newdoc->isLocked()) {
+        bool ok = true;
+        QString password = QInputDialog::getText(this, tr("Document Password"),
+                                                 tr("Please insert the password of the document:"),
+                                                 QLineEdit::Password, QString(), &ok);
+        if (!ok) {
+            delete newdoc;
+            return;
+        }
+        newdoc->unlock(password.toLatin1(), password.toLatin1());
+    }
+
+    closeDocument();
+
+    m_doc = newdoc;
+
+    Q_FOREACH(DocumentObserver *obs, m_observers) {
+        obs->documentLoaded();
+        obs->pageChanged(0);
+    }
+}
+
+void PdfViewer::closeDocument()
+{
+    if (!m_doc) {
+        return;
+    }
+
+    Q_FOREACH(DocumentObserver *obs, m_observers) {
+        obs->documentClosed();
+    }
+
+    delete m_doc;
+    m_doc = 0;
+}
+
+void PdfViewer::slotOpenFile()
+{
+    QString fileName = QFileDialog::getOpenFileName(this, tr("Open PDF Document"), QDir::homePath(), tr("PDF Documents (*.pdf)"));
+    if (fileName.isEmpty()) {
+        return;
+    }
+
+    loadDocument(fileName);
+}
+
+void PdfViewer::setPage(int page)
+{
+    Q_FOREACH(DocumentObserver *obs, m_observers) {
+        obs->pageChanged(page);
+    }
+
+    m_currentPage = page;
+}
+
+int PdfViewer::page() const
+{
+    return m_currentPage;
 }
 
 #include "viewer.moc"
diff --git a/qt4/demos/viewer.h b/qt4/demos/viewer.h
index 02f7f9b..c180b06 100644
--- a/qt4/demos/viewer.h
+++ b/qt4/demos/viewer.h
@@ -22,6 +22,8 @@
 #include <QtGui/QMainWindow>
 
 class QAction;
+class QLabel;
+class DocumentObserver;
 namespace Poppler {
 class Document;
 }
@@ -30,15 +32,30 @@ class PdfViewer : public QMainWindow
 {
     Q_OBJECT
 
+    friend class DocumentObserver;
+
 public:
     PdfViewer();
     ~PdfViewer();
 
+    /*virtual*/ QSize sizeHint() const;
+
     void loadDocument(const QString &file);
+    void closeDocument();
+
+private Q_SLOTS:
+    void slotOpenFile();
 
 private:
+    void setPage(int page);
+    int page() const;
+
+    int m_currentPage;
+
     QAction *m_fileOpenAct;
 
+    QList<DocumentObserver *> m_observers;
+
     Poppler::Document *m_doc;
 };
 


More information about the poppler mailing list