PDF cut-and-paste
Stefan Bund [Tue, 25 Jan 2011 00:25:59 +0000 (01:25 +0100)]
PDFWidget.cc
PDFWidget.hh

index d6e1417..6998d1e 100644 (file)
@@ -16,6 +16,8 @@
 #include <QKeyEvent>
 #include <QWebView>
 #include <QWebFrame>
+#include <QApplication>
+#include <QClipboard>
 
 //#include "PDFWidget.mpp"
 #define prefix_
@@ -23,7 +25,7 @@
 
 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QNetworkAccessManager * manager,
                                     QWidget * parent)
-    : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0)
+    : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
 
 {
     QWebView * webView (dynamic_cast<QWebView*>(parent));
@@ -33,7 +35,7 @@ prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QNetworkAccessManager *
 
 prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QString const & document,
                                     QNetworkAccessManager * manager, QWidget * parent)
-    : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0)
+    : QLabel(parent), id_ (id), manager_ (manager), currentPage_ (0), rubberBand_ (0)
 
 {
     setFocusPolicy(Qt::WheelFocus);
@@ -44,6 +46,13 @@ prefix_ pykit::PDFWidget::PDFWidget(QString const & id, QString const & document
     documentSetup();
 }
 
+prefix_ pykit::PDFWidget::~PDFWidget()
+{
+    if (rubberBand_)
+        delete rubberBand_;
+}
+
+
 prefix_ void pykit::PDFWidget::load(QUrl const & url)
 {
     QNetworkRequest request (url);
@@ -57,12 +66,38 @@ prefix_ void pykit::PDFWidget::resizeEvent(QResizeEvent *)
         return;
     QSizeF pageSize (document_->page(currentPage_)->pageSizeF());
     double n = 72.0 * width() / pageSize.width();
-   if (n != dpi_) {
+    if (n != dpi_) {
         dpi_ = n;
         showPage();
     }
 }
 
+prefix_ void pykit::PDFWidget::mousePressEvent(QMouseEvent * event)
+{
+    origin_ = event->pos();
+    if (! rubberBand_)
+        rubberBand_ = new QRubberBand(QRubberBand::Rectangle, this);
+    rubberBand_->setGeometry(QRect(origin_, QSize()));
+    rubberBand_->show();
+}
+
+prefix_ void pykit::PDFWidget::mouseMoveEvent(QMouseEvent * event)
+{
+    if (rubberBand_)
+        rubberBand_->setGeometry(QRect(origin_, event->pos()).normalized());
+}
+
+prefix_ void pykit::PDFWidget::mouseReleaseEvent(QMouseEvent * event)
+{
+    if (rubberBand_) {
+        QMatrix matrix (dpi_ / 72.0, 0, 0, dpi_ / 72.0, 0, 0);
+        QRectF rect (matrix.inverted().mapRect(QRect(rubberBand_->pos(), rubberBand_->size())));
+        QString text = document_->page(currentPage_)->text(rect);
+        if (! text.isEmpty())
+            QApplication::clipboard()->setText(text);
+    }
+}
+
 prefix_ void pykit::PDFWidget::netLoadDocument()
 {
     QNetworkReply * reply = static_cast<QNetworkReply*>(sender());
index 3c9cb51..041a29c 100644 (file)
@@ -17,6 +17,7 @@
 #include <QWidget>
 #include <QNetworkAccessManager>
 #include <QUrl>
+#include <QRubberBand>
 
 //#include "PDFWidget.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -31,12 +32,17 @@ namespace pykit {
                            QWidget * parent = 0);
         PDFWidget(QString const & id, QString const & document, QNetworkAccessManager * manager,
                   QWidget * parent = 0);
+        ~PDFWidget();
 
         void load(QUrl const & url);
 
     protected:
         virtual void resizeEvent(QResizeEvent * event);
 
+        virtual void mousePressEvent(QMouseEvent * event);
+        virtual void mouseMoveEvent(QMouseEvent * event);
+        virtual void mouseReleaseEvent(QMouseEvent * event);
+
     private slots:
         void netLoadDocument();
 
@@ -49,6 +55,8 @@ namespace pykit {
         boost::scoped_ptr<Poppler::Document> document_;
         int currentPage_;
         double dpi_;
+        QRubberBand * rubberBand_;
+        QPoint origin_;
     };
 
 }