Skip to content

Commit b7e0e9b

Browse files
committed
New Feature: getPosFromClick
ctrl+shift+leftClick on pdf view copies the cursor coordinates x and y (in cm, w.r.t. bottom left corner of the page)
1 parent 0e89350 commit b7e0e9b

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/pdfviewer/PDFDocument.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121

2222
#ifndef NO_POPPLER_PREVIEW
23-
2423
#include "PDFDocument.h"
2524
#include "PDFDocks.h"
2625
//#include "FindDialog.h"
@@ -1059,6 +1058,13 @@ void PDFWidget::openAnnotationDialog(const PDFAnnotation *annon)
10591058
dlg->show();
10601059
}
10611060

1061+
void PDFWidget::callGetPosFromClick(const QPoint &p){
1062+
int page = pageFromPos(p);
1063+
if (page < 0) return;
1064+
QRect r = pageRect(page);
1065+
emit getPosFromCLick(QPointF(p - r.topLeft()) / totalScaleFactor());
1066+
}
1067+
10621068
void PDFWidget::mouseReleaseEvent(QMouseEvent *event)
10631069
{
10641070
if (pdfdocument && pdfdocument->embeddedMode)
@@ -1092,11 +1098,17 @@ void PDFWidget::mouseReleaseEvent(QMouseEvent *event)
10921098
else if (event->button() == Qt::RightButton) goPrev();
10931099
break;
10941100
}
1101+
// ctrl-shift-click to get position
1102+
if ((mouseDownModifiers & Qt::ControlModifier) && (mouseDownModifiers & Qt::ShiftModifier)) {
1103+
if ((event->modifiers() & Qt::ControlModifier) && (event->modifiers() & Qt::ShiftModifier)) {
1104+
callGetPosFromClick(event->pos());
1105+
}
1106+
break;
1107+
}
10951108
// Ctrl-click to sync
10961109
if (mouseDownModifiers & Qt::ControlModifier) {
10971110
if (event->modifiers() & Qt::ControlModifier)
10981111
syncWindowClick(event->pos(), true);
1099-
11001112
break;
11011113
}
11021114
// check whether to zoom
@@ -1498,6 +1510,8 @@ void PDFWidget::syncWindowClick(const QPoint &p, bool activate)
14981510

14991511
}
15001512

1513+
1514+
15011515
void PDFWidget::syncCurrentPage(bool activate)
15021516
{
15031517
if (pages.isEmpty()) return;
@@ -2733,7 +2747,21 @@ void PDFDocument::setupMenus(bool embedded)
27332747

27342748
configManager->modifyManagedShortcuts("pdf");
27352749
}
2736-
2750+
/*
2751+
* Copies to clipboard position in cm captured from the pdf viewer (w.r.t. south west corner)
2752+
*/
2753+
void PDFDocument::getPosFromCLick(const QPointF &pos)
2754+
{
2755+
int page = pdfWidget->pageFromPos(pos.toPoint());
2756+
if (page < 0) return;
2757+
float height = (pdfWidget->pageRect(page).height()/pdfWidget->totalScaleFactor()) / 28.45;
2758+
QClipboard *clipboard = QGuiApplication::clipboard();
2759+
QString tmp;
2760+
QTextStream(&tmp) << "" << pos.x() / 28.45 << "," << height- pos.y() / 28.45;
2761+
clipboard->setText(tmp);
2762+
//qDebug() << "Position: " << qPrintable(tmp) << "\n";
2763+
2764+
}
27372765
/*!
27382766
* \brief the shortcuts will only be triggered if this widget has focus (used in embedded mode)
27392767
* \param actions
@@ -2994,6 +3022,7 @@ void PDFDocument::init(bool embedded)
29943022
connect(pdfWidget, SIGNAL(changedZoom(qreal)), this, SLOT(enableZoomActions(qreal)));
29953023
connect(pdfWidget, SIGNAL(changedScaleOption(autoScaleOption)), this, SLOT(adjustScaleActions(autoScaleOption)));
29963024
connect(pdfWidget, SIGNAL(syncClick(int,const QPointF&,bool)), this, SLOT(syncClick(int,const QPointF&,bool)));
3025+
connect(pdfWidget, SIGNAL(getPosFromCLick(const QPointF&)), this, SLOT(getPosFromCLick(const QPointF&)));
29973026

29983027
if (actionZoom_In->shortcut() == QKeySequence("Ctrl++"))
29993028
new QShortcut(QKeySequence("Ctrl+="), pdfWidget, SLOT(zoomIn()), Q_NULLPTR, Qt::WidgetShortcut);

src/pdfviewer/PDFDocument.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public slots:
241241
void fitWindow(bool checked = true);
242242
void setTool(int tool);
243243
void syncWindowClick(const QPoint &p, bool activate);
244+
void callGetPosFromClick(const QPoint &p);
244245
void syncCurrentPage(bool activate);
245246
void fixedScale(qreal scale = 1.0);
246247
void setImage(QPixmap img, int pageNr);
@@ -251,6 +252,7 @@ public slots:
251252
void changedZoom(qreal);
252253
void changedScaleOption(autoScaleOption);
253254
void syncClick(int, const QPointF &, bool activate); //page position in page coordinates
255+
void getPosFromCLick(const QPointF &);
254256

255257
protected:
256258
virtual void paintEvent(QPaintEvent *event);
@@ -467,6 +469,7 @@ private slots:
467469
void enableZoomActions(qreal);
468470
void adjustScaleActions(autoScaleOption);
469471
void syncClick(int page, const QPointF &pos, bool activate);
472+
void getPosFromCLick(const QPointF &pos);
470473
void stopReloadTimer();
471474
void reloadWhenIdle();
472475
void idleReload();

0 commit comments

Comments
 (0)