4 // Stefan Bund <info@j32.de>
7 \brief PDFWidget non-inline non-template implementation */
9 #include "PDFWidget.hh"
10 //#include "PDFWidget.ih"
14 #include <QNetworkRequest>
15 #include <QNetworkReply>
19 #include <QApplication>
22 //#include "PDFWidget.mpp"
24 ///////////////////////////////cc.p////////////////////////////////////////
26 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QNetworkAccessManager * manager,
28 : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
31 QWebView * webView (dynamic_cast<QWebView*>(parent));
32 if (webView && !id_.isEmpty())
33 webView->page()->mainFrame()->addToJavaScriptWindowObject(id_,this);
36 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QString const & document,
37 QNetworkAccessManager * manager, QWidget * parent)
38 : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
41 setFocusPolicy(Qt::WheelFocus);
42 document_.reset(Poppler::Document::load(document));
43 QWebView * webView (dynamic_cast<QWebView*>(parent));
44 if (webView && !id_.isEmpty())
45 webView->page()->mainFrame()->addToJavaScriptWindowObject(id_,this);
49 prefix_ pykit::PDFWidget::~PDFWidget()
56 prefix_ void pykit::PDFWidget::load(QUrl const & url)
58 QNetworkRequest request (url);
59 QNetworkReply * reply (manager_->get(request));
60 connect(reply, SIGNAL(finished()), this, SLOT(netLoadDocument()));
63 prefix_ void pykit::PDFWidget::resizeEvent(QResizeEvent *)
67 QSizeF pageSize (document_->page(currentPage_)->pageSizeF());
68 double n = 72.0 * width() / pageSize.width();
75 prefix_ void pykit::PDFWidget::mousePressEvent(QMouseEvent * event)
77 origin_ = event->pos();
79 rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this);
80 rubberBand_->setGeometry(QRect(origin_, QSize()));
84 prefix_ void pykit::PDFWidget::mouseMoveEvent(QMouseEvent * event)
87 rubberBand_->setGeometry(QRect(origin_, event->pos()).normalized());
90 prefix_ void pykit::PDFWidget::mouseReleaseEvent(QMouseEvent * event)
93 QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0);
94 QRectF rect (matrix.inverted().mapRect(QRect(rubberBand_->pos(), rubberBand_->size())));
95 QString text = document_->page(currentPage_)->text(rect);
97 QApplication::clipboard()->setText(text);
101 prefix_ void pykit::PDFWidget::netLoadDocument()
103 QNetworkReply * reply = static_cast<QNetworkReply*>(sender());
104 if (reply->error() != QNetworkReply::NoError)
106 QByteArray data (reply->read(reply->size()));
107 document_.reset(Poppler::Document::loadFromData(data));
109 reply->deleteLater();
112 prefix_ void pykit::PDFWidget::documentSetup()
116 document_->setRenderHint(Poppler::Document::Antialiasing, true);
117 document_->setRenderHint(Poppler::Document::TextAntialiasing, true);
118 document_->setRenderHint(Poppler::Document::TextHinting, false);
123 prefix_ void pykit::PDFWidget::showPage()
127 QImage image (document_->page(currentPage_)->renderToImage(
128 dpi_, dpi_, 0, 0, width(), height()));
129 setPixmap(QPixmap::fromImage(image));
132 ///////////////////////////////cc.e////////////////////////////////////////
134 //#include "PDFWidget.mpp"
140 // comment-column: 40
141 // c-file-style: "j32"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "make"