typo fix
[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 #include <QPrinter>
20 #include <QPrintDialog>
21 #include <QWebInspector>
22 #include <QWebFrame>
23 #include <QWebHistory>
24
25 //#include "Viewer.mpp"
26 #define prefix_
27 ///////////////////////////////cc.p////////////////////////////////////////
28
29 #ifdef POPPLER
30
31 namespace {
32
33     class PDFWebPluginFactory
34         : public QWebPluginFactory
35     {
36     public:
37         PDFWebPluginFactory(QNetworkAccessManager * manager, QObject * parent = 0);
38
39         QObject * create(QString const & mimeType, QUrl const & url,
40                          QStringList const & argumentNames, QStringList const & argumentValues)
41             const;
42         QList<QWebPluginFactory::Plugin> plugins() const;
43
44     private:
45         QNetworkAccessManager * manager_;
46     };
47
48 }
49
50 prefix_ PDFWebPluginFactory::PDFWebPluginFactory(QNetworkAccessManager * manager,
51                                                  QObject * parent)
52     : QWebPluginFactory(parent), manager_ (manager)
53 {}
54
55 prefix_ QObject * PDFWebPluginFactory::create(QString const & mimeType, QUrl const & url,
56                                               QStringList const & argumentNames,
57                                               QStringList const & argumentValues)
58     const
59 {
60     if (mimeType == "application/pdf") {
61         int idix (argumentNames.indexOf("id"));
62         QString id;
63         if (idix>0)
64             id = argumentValues[idix];
65         pykit::PDFWidget * plugin = new pykit::PDFWidget(
66             id, manager_, dynamic_cast<QWidget*>(parent()));
67         plugin->load(url);
68         return plugin;
69     }
70     return 0;
71 }
72
73 prefix_ QList<QWebPluginFactory::Plugin> PDFWebPluginFactory::plugins()
74     const
75 {
76     QList<QWebPluginFactory::Plugin> plugins;
77
78     {
79         QWebPluginFactory::Plugin plugin;
80         plugin.name = "PDF viewer";
81         plugin.description = "View PDF files";
82         {
83             QWebPluginFactory::MimeType mimeType;
84             mimeType.name = "application/pdf";
85             mimeType.description = "PDF file";
86             plugin.mimeTypes += mimeType;
87         }
88         plugins += plugin;
89     }
90
91     return plugins;
92 }
93
94 #endif
95
96 ///////////////////////////////////////////////////////////////////////////
97 // pykit::Viewer
98
99 namespace {
100     pykit::Viewer * instance_ (0);
101 }
102
103 prefix_ pykit::Viewer::Viewer(QUrl const & url, Publisher * publisher, QWidget * parent)
104     : QWebView (parent)
105 {
106     instance_ = this;
107     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
108     if (! QWebSettings::globalSettings()->testAttribute(QWebSettings::DeveloperExtrasEnabled))
109         setContextMenuPolicy(Qt::NoContextMenu);
110     page()->setNetworkAccessManager(
111         new InternalNetworkAccessManager(page()->networkAccessManager(), this, publisher));
112 #ifdef POPPLER
113     page()->setPluginFactory(new PDFWebPluginFactory (page()->networkAccessManager(), this));
114 #endif
115     connect(page(), SIGNAL(printRequested(QWebFrame*)), this, SLOT(printRequested()));
116     load(url);
117     inspector_ = new QWebInspector();
118     inspector_->setPage(page());
119     connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
120             this, SLOT(initJSObjects()));
121     initJSObjects();
122 }
123
124 prefix_ pykit::Viewer::~Viewer()
125 {
126     if (inspector_)
127         delete inspector_;
128 }
129
130 prefix_ pykit::Viewer * pykit::Viewer::instance()
131 {
132     return instance_;
133 }
134
135 prefix_ bool pykit::Viewer::canGoBack()
136 {
137     return page()->history()->canGoBack();
138 }
139
140 prefix_ bool pykit::Viewer::canGoForward()
141 {
142     return page()->history()->canGoForward();
143 }
144
145 prefix_ void pykit::Viewer::initJSObjects()
146 {
147     page()->mainFrame()->addToJavaScriptWindowObject("view", this);
148 }
149
150 prefix_ void pykit::Viewer::printRequested()
151 {
152     QPrinter printer;
153     QPrintDialog printDialog(&printer, this);
154     if (printDialog.exec() == QPrintDialog::Accepted) {
155         print(&printer);
156     }
157 }
158
159 ///////////////////////////////cc.e////////////////////////////////////////
160 #undef prefix_
161 //#include "Viewer.mpp"
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "j32"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -U"
172 // End: