From: Stefan Bund Date: Sat, 4 May 2013 21:25:39 +0000 (+0200) Subject: PDF highlighting support X-Git-Url: http://g0dil.de/git?p=pykit.git;a=commitdiff_plain;h=ff400575e86c51dd05f1b729b5a3889c5deed8df PDF highlighting support --- diff --git a/Makefile b/Makefile index 18b004d..f7b97d3 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ Makefile.qmake: qmake -o Makefile.qmake pykit.pro $(QMAKEOPTS) %: Makefile.qmake FORCE - @$(MAKE) -f Makefile.qmake $@ + @$(MAKE) --no-print-directory -f Makefile.qmake $@ FORCE: ; clean: diff --git a/PDFWidget.cc b/PDFWidget.cc index 6998d1e..7203d30 100644 --- a/PDFWidget.cc +++ b/PDFWidget.cc @@ -11,6 +11,7 @@ // Custom includes #include +#include #include #include #include @@ -87,7 +88,7 @@ prefix_ void pykit::PDFWidget::mouseMoveEvent(QMouseEvent * event) rubberBand_->setGeometry(QRect(origin_, event->pos()).normalized()); } -prefix_ void pykit::PDFWidget::mouseReleaseEvent(QMouseEvent * event) +prefix_ void pykit::PDFWidget::mouseReleaseEvent(QMouseEvent *) { if (rubberBand_) { QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0); @@ -120,12 +121,36 @@ prefix_ void pykit::PDFWidget::documentSetup() showPage(); } +prefix_ void pykit::PDFWidget::highlightString(QString const & str) +{ + QRectF highlightArea; + Poppler::Page::SearchDirection dir (Poppler::Page::FromTop); + while (document_->page(currentPage_)->search(str, highlightArea, dir, + Poppler::Page::CaseInsensitive)) { + highlightAreas_.push_back(highlightArea); + dir = Poppler::Page::NextResult; + } +} + prefix_ void pykit::PDFWidget::showPage() { if (! document_) return; QImage image (document_->page(currentPage_)->renderToImage( dpi_, dpi_, 0, 0, width(), height())); + QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0); + + BOOST_FOREACH(QRectF const & highlightArea, highlightAreas_) { + QRect highlightRect = matrix.mapRect(highlightArea).toRect(); + highlightRect.adjust(-2, -2, 2, 2); + QImage highlight = image.copy(highlightRect); + QPainter painter; + painter.begin(&image); + painter.fillRect(image.rect(), QColor(0, 0, 0, 32)); + painter.drawImage(highlightRect, highlight); + painter.end(); + } + setPixmap(QPixmap::fromImage(image)); } diff --git a/PDFWidget.hh b/PDFWidget.hh index 041a29c..1104b48 100644 --- a/PDFWidget.hh +++ b/PDFWidget.hh @@ -10,6 +10,7 @@ #define HH_PyKit_PDFWidget_ 1 // Custom includes +#include #include #include #include @@ -35,6 +36,7 @@ namespace pykit { ~PDFWidget(); void load(QUrl const & url); + Q_INVOKABLE void highlightString(QString const & str); protected: virtual void resizeEvent(QResizeEvent * event); @@ -57,6 +59,7 @@ namespace pykit { double dpi_; QRubberBand * rubberBand_; QPoint origin_; + std::vector highlightAreas_; }; } @@ -75,5 +78,5 @@ namespace pykit { // c-file-style: "j32" // indent-tabs-mode: nil // ispell-local-dictionary: "american" -// compile-command: "scons -U" +// compile-command: "make" // End: