PDF highlighting support
Stefan Bund [Sat, 4 May 2013 21:25:39 +0000 (23:25 +0200)]
Makefile
PDFWidget.cc
PDFWidget.hh

index 18b004d..f7b97d3 100644 (file)
--- 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:
index 6998d1e..7203d30 100644 (file)
@@ -11,6 +11,7 @@
 
 // Custom includes
 #include <iostream>
+#include <boost/foreach.hpp>
 #include <QNetworkRequest>
 #include <QNetworkReply>
 #include <QKeyEvent>
@@ -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));
 }
 
index 041a29c..1104b48 100644 (file)
@@ -10,6 +10,7 @@
 #define HH_PyKit_PDFWidget_ 1
 
 // Custom includes
+#include <vector>
 #include <poppler/qt4/poppler-qt4.h>
 #include <boost/scoped_ptr.hpp>
 #include <QLabel>
@@ -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<QRectF> 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: