4 // Stefan Bund <info@j32.de>
7 \brief PDFWidget non-inline non-template implementation */
9 #include "PDFWidget.hh"
10 //#include "PDFWidget.ih"
14 #include <boost/foreach.hpp>
15 #include <QNetworkRequest>
16 #include <QNetworkReply>
20 #include <QApplication>
23 //#include "PDFWidget.mpp"
25 ///////////////////////////////cc.p////////////////////////////////////////
27 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QNetworkAccessManager * manager,
29 : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
32 QWebView * webView (dynamic_cast<QWebView*>(parent));
33 if (webView && !id_.isEmpty())
34 webView->page()->mainFrame()->addToJavaScriptWindowObject(id_, this);
37 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QString const & document,
38 QNetworkAccessManager * manager, QWidget * parent)
39 : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
42 setFocusPolicy(Qt::WheelFocus);
43 document_.reset(Poppler::Document::load(document));
44 QWebView * webView (dynamic_cast<QWebView*>(parent));
45 if (webView && !id_.isEmpty())
46 webView->page()->mainFrame()->addToJavaScriptWindowObject(id_, this);
50 prefix_ pykit::PDFWidget::~PDFWidget()
57 prefix_ void pykit::PDFWidget::load(QUrl const & url)
59 QNetworkRequest request (url);
60 QNetworkReply * reply (manager_->get(request));
61 connect(reply, SIGNAL(finished()), this, SLOT(netLoadDocument()));
64 prefix_ void pykit::PDFWidget::resizeEvent(QResizeEvent *)
68 QSizeF pageSize (document_->page(currentPage_)->pageSizeF());
69 double n = 72.0 * width() / pageSize.width();
76 prefix_ void pykit::PDFWidget::mousePressEvent(QMouseEvent * event)
78 origin_ = event->pos();
80 rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this);
81 rubberBand_->setGeometry(QRect(origin_, QSize()));
85 prefix_ void pykit::PDFWidget::mouseMoveEvent(QMouseEvent * event)
88 rubberBand_->setGeometry(QRect(origin_, event->pos()).normalized());
91 prefix_ void pykit::PDFWidget::mouseReleaseEvent(QMouseEvent *)
94 QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0);
95 QRectF rect (matrix.inverted().mapRect(QRect(rubberBand_->pos(), rubberBand_->size())));
96 QString text = document_->page(currentPage_)->text(rect);
98 QApplication::clipboard()->setText(text);
102 prefix_ void pykit::PDFWidget::netLoadDocument()
104 QNetworkReply * reply = static_cast<QNetworkReply*>(sender());
105 if (reply->error() != QNetworkReply::NoError)
107 QByteArray data (reply->read(reply->size()));
108 document_.reset(Poppler::Document::loadFromData(data));
110 reply->deleteLater();
113 prefix_ void pykit::PDFWidget::documentSetup()
117 document_->setRenderHint(Poppler::Document::Antialiasing, true);
118 document_->setRenderHint(Poppler::Document::TextAntialiasing, true);
119 document_->setRenderHint(Poppler::Document::TextHinting, false);
121 Poppler::Page::SearchDirection dir (Poppler::Page::FromTop);
122 highlightAreas_.clear();
123 BOOST_FOREACH(QString const & str, highlightStrings_) {
124 QRectF highlightArea;
125 while (document_->page(currentPage_)->search(str, highlightArea, dir,
126 Poppler::Page::CaseInsensitive)) {
127 highlightAreas_.push_back(highlightArea);
128 dir = Poppler::Page::NextResult;
134 prefix_ void pykit::PDFWidget::highlightString(QString const & str)
136 highlightStrings_.append(str);
140 prefix_ void pykit::PDFWidget::showPage()
144 QImage image (document_->page(currentPage_)->renderToImage(
145 dpi_, dpi_, 0, 0, width(), height()));
146 QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0);
149 painter.begin(&image);
150 painter.setCompositionMode(QPainter::CompositionMode_Darken);
151 BOOST_FOREACH(QRectF const & highlightArea, highlightAreas_) {
152 QRect highlightRect = matrix.mapRect(highlightArea).toRect();
153 highlightRect.adjust(-2, -2, 2, 1);
154 painter.fillRect(highlightRect, QColor(244, 229, 0));
158 setPixmap(QPixmap::fromImage(image));
161 ///////////////////////////////cc.e////////////////////////////////////////
163 //#include "PDFWidget.mpp"
169 // comment-column: 40
170 // c-file-style: "j32"
171 // indent-tabs-mode: nil
172 // ispell-local-dictionary: "american"
173 // compile-command: "make"