From: Stefan Bund Date: Tue, 25 Jan 2011 00:25:59 +0000 (+0100) Subject: PDF cut-and-paste X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=3299420e9e55194f4cd55eae7096c74a1f5824d3;p=pykit.git PDF cut-and-paste --- diff --git a/PDFWidget.cc b/PDFWidget.cc index d6e1417..6998d1e 100644 --- a/PDFWidget.cc +++ b/PDFWidget.cc @@ -16,6 +16,8 @@ #include #include #include +#include +#include //#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(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(sender()); diff --git a/PDFWidget.hh b/PDFWidget.hh index 3c9cb51..041a29c 100644 --- a/PDFWidget.hh +++ b/PDFWidget.hh @@ -17,6 +17,7 @@ #include #include #include +#include //#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 document_; int currentPage_; double dpi_; + QRubberBand * rubberBand_; + QPoint origin_; }; }