Add PDFWidget JavaScript API
[pykit.git] / Viewer.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
4 //     Stefan Bund <info@j32.de>
5
6 /** \file
7     \brief Viewer non-inline non-template implementation */
8
9 #include "Viewer.hh"
10 //#include "Viewer.ih"
11
12 // Custom includes
13 #include <iostream>
14 #include <QWebPluginFactory>
15 #include "Publisher.hh"
16 #ifdef POPPLER
17 #include "PDFWidget.hh"
18 #endif
19
20 //#include "Viewer.mpp"
21 #define prefix_
22 ///////////////////////////////cc.p////////////////////////////////////////
23
24 #ifdef POPPLER
25
26 namespace {
27
28     class PDFWebPluginFactory
29         : public QWebPluginFactory
30     {
31     public:
32         PDFWebPluginFactory(QNetworkAccessManager * manager, QObject * parent = 0);
33
34         QObject * create(QString const & mimeType, QUrl const & url,
35                          QStringList const & argumentNames, QStringList const & argumentValues)
36             const;
37         QList<QWebPluginFactory::Plugin> plugins() const;
38
39     private:
40         QNetworkAccessManager * manager_;
41     };
42
43 }
44
45 prefix_ PDFWebPluginFactory::PDFWebPluginFactory(QNetworkAccessManager * manager,
46                                                  QObject * parent)
47     : QWebPluginFactory(parent), manager_ (manager)
48 {}
49
50 prefix_ QObject * PDFWebPluginFactory::create(QString const & mimeType, QUrl const & url,
51                                               QStringList const & argumentNames,
52                                               QStringList const & argumentValues)
53     const
54 {
55     if (mimeType == "application/pdf") {
56         int idix (argumentNames.indexOf("id"));
57         QString id;
58         if (idix>0)
59             id = argumentValues[idix];
60         pykit::PDFWidget * plugin = new pykit::PDFWidget(
61             id, manager_, dynamic_cast<QWidget*>(parent()));
62         plugin->load(url);
63         return plugin;
64     }
65     return 0;
66 }
67
68 prefix_ QList<QWebPluginFactory::Plugin> PDFWebPluginFactory::plugins()
69     const
70 {
71     QList<QWebPluginFactory::Plugin> plugins;
72
73     {
74         QWebPluginFactory::Plugin plugin;
75         plugin.name = "PDF viewer";
76         plugin.description = "View PDF files";
77         {
78             QWebPluginFactory::MimeType mimeType;
79             mimeType.name = "application/pdf";
80             mimeType.description = "PDF file";
81             plugin.mimeTypes += mimeType;
82         }
83         plugins += plugin;
84     }
85
86     return plugins;
87 }
88
89 #endif
90
91 ///////////////////////////////////////////////////////////////////////////
92 // pykit::Viewer
93
94 prefix_ pykit::Viewer::Viewer(QUrl const & url, Publisher * publisher, QWidget * parent)
95     : QWebView (parent)
96 {
97     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
98     page()->setNetworkAccessManager(
99         new InternalNetworkAccessManager(page()->networkAccessManager(), this, publisher));
100 #ifdef POPPLER
101     page()->setPluginFactory(new PDFWebPluginFactory (page()->networkAccessManager(), this));
102 #endif
103     load(url);
104 }
105
106 ///////////////////////////////cc.e////////////////////////////////////////
107 #undef prefix_
108 //#include "Viewer.mpp"
109
110 \f
111 // Local Variables:
112 // mode: c++
113 // fill-column: 100
114 // comment-column: 40
115 // c-file-style: "j32"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -U"
119 // End: