Separate Viewer from MainWindow and implement PDF embedding
[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 #include "PDFWidget.hh"
17
18 //#include "Viewer.mpp"
19 #define prefix_
20 ///////////////////////////////cc.p////////////////////////////////////////
21
22 namespace {
23
24     class PDFWebPluginFactory
25         : public QWebPluginFactory
26     {
27     public:
28         PDFWebPluginFactory(QNetworkAccessManager * manager, QObject * parent = 0);
29
30         QObject * create(QString const & mimeType, QUrl const & url,
31                          QStringList const & argumentNames, QStringList const & argumentValues)
32             const;
33         QList<QWebPluginFactory::Plugin> plugins() const;
34
35     private:
36         QNetworkAccessManager * manager_;
37     };
38
39 }
40
41 prefix_ PDFWebPluginFactory::PDFWebPluginFactory(QNetworkAccessManager * manager,
42                                                  QObject * parent)
43     : QWebPluginFactory(parent), manager_ (manager)
44 {}
45
46 prefix_ QObject * PDFWebPluginFactory::create(QString const & mimeType, QUrl const & url,
47                                               QStringList const & /* argumentNames */,
48                                               QStringList const & /* argumentValues */)
49     const
50 {
51     if (mimeType == "application/x-pdf") {
52         pykit::PDFWidget * plugin = new pykit::PDFWidget(manager_);
53         plugin->load(url);
54         return plugin;
55     }
56     return 0;
57 }
58
59 prefix_ QList<QWebPluginFactory::Plugin> PDFWebPluginFactory::plugins()
60     const
61 {
62     QList<QWebPluginFactory::Plugin> plugins;
63
64     {
65         QWebPluginFactory::Plugin plugin;
66         plugin.name = "PDF viewer";
67         plugin.description = "View PDF files";
68         {
69             QWebPluginFactory::MimeType mimeType;
70             mimeType.name = "application/x-pdf";
71             mimeType.description = "PDF file";
72             plugin.mimeTypes += mimeType;
73         }
74         plugins += plugin;
75     }
76
77     return plugins;
78 }
79
80 ///////////////////////////////////////////////////////////////////////////
81 // pykit::Viewer
82
83 prefix_ pykit::Viewer::Viewer(QUrl const & url, Publisher * publisher, QWidget * parent)
84     : QWebView (parent)
85 {
86     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
87     page()->setNetworkAccessManager(
88         new InternalNetworkAccessManager(page()->networkAccessManager(), this, publisher));
89     page()->setPluginFactory(new PDFWebPluginFactory (page()->networkAccessManager(), this));
90     load(url);
91 }
92
93 ///////////////////////////////cc.e////////////////////////////////////////
94 #undef prefix_
95 //#include "Viewer.mpp"
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "j32"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -U"
106 // End: