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