Skip to content

Commit

Permalink
add toolbutton grid actions to embedded pdf-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
octaeder committed Jan 10, 2025
1 parent 34619fc commit 7f83e03
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 32 deletions.
117 changes: 90 additions & 27 deletions src/pdfviewer/PDFDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ void PDFWidget::setPDFDocument(PDFDocument *docu)
pdfdocument = docu;
}

void PDFWidget::setDocument(const QSharedPointer<Poppler::Document> &doc)
void PDFWidget::setDocument(const QSharedPointer<Poppler::Document> &doc, bool embedded)
{
pages.clear();
document = doc;
Expand All @@ -906,7 +906,10 @@ void PDFWidget::setDocument(const QSharedPointer<Poppler::Document> &doc)

if (!document.isNull()) {
docPages = document->numPages();
setSinglePageStep(globalConfig->singlepagestep);
if (embedded)
setSinglePageStep(false);
else
setSinglePageStep(globalConfig->singlepagestep);
} else
docPages = 0;
#ifdef MEDIAPLAYER
Expand Down Expand Up @@ -2098,10 +2101,14 @@ void PDFWidget::setPageOffset(int offset, bool setAsDefault, bool refresh){
pageOffset = offset;
else {
pageOffset = gridx - 1;
globalConfig->pageOffset = pageOffset;
}
if (!setAsDefault)
globalConfig->pageOffset = pageOffset;
if (!setAsDefault) {
bool embedded = pdfdocument->embeddedMode;
if (embedded)
pageOffsetEmbedded = pageOffset;
else
globalConfig->pageOffset = pageOffset;
}

if (!refresh)
return;
Expand Down Expand Up @@ -2132,16 +2139,23 @@ int PDFWidget::getPageOffset() const

void PDFWidget::setGridSize(int gx, int gy, bool setAsDefault)
{
bool embedded = pdfdocument->embeddedMode;
if (gridx == gx && gridy == gy)
return;
gridx = gx;
gridy = gy;
if (gridx == 1)
if (gridx == 1) {
setPageOffset(0, true, true);
else if (gridx == 2 && gridy == 1)
}
else if (gridx == 2 && gridy == 1) {
setPageOffset(1, false, true);
else
}
else if (!embedded) {
setPageOffset(globalConfig->pageOffset, true, true);
}
else {
setPageOffset(pageOffsetEmbedded, true, true);
}

if (setAsDefault)
return;
Expand Down Expand Up @@ -2199,7 +2213,15 @@ int PDFWidget::pageStep()
*/
int PDFWidget::gridCols(bool fromConfig) const
{
int result= fromConfig ? globalConfig->gridx : gridx;
int result;
if (!fromConfig)
result = gridx;
else {
if (pdfdocument->embeddedMode)
result = gridxEmbedded;
else
result = globalConfig->gridx;
}
return result;
}
/*!
Expand All @@ -2209,7 +2231,15 @@ int PDFWidget::gridCols(bool fromConfig) const
*/
int PDFWidget::gridRows(bool fromConfig) const
{
int result= fromConfig ? globalConfig->gridy : gridy;
int result;
if (!fromConfig)
result = gridy;
else {
if (pdfdocument->embeddedMode)
result = gridyEmbedded;
else
result = globalConfig->gridy;
}
return result;
}

Expand Down Expand Up @@ -2453,7 +2483,7 @@ void PDFWidget::fitTextWidth(bool checked)
if (!textRect.isValid()) return;
qreal targetWidth = maxPageSizeFDpiAdjusted().width() * (gridx - 1) + textRect.width() * dpi / 72.0;
//qreal targetWidth = textRect.width() * dpi / 72.0;
// total with of all pages in the grid - textMargin of a single page
// total width of all pages in the grid - textMargin of a single page
// for a 1x grid, targetWith is the same as textRect.width()
scaleFactor = portWidth / ((targetWidth ) + 2 * margin);
if (scaleFactor < kMinScaleFactor)
Expand All @@ -2478,8 +2508,11 @@ void PDFWidget::fitWindow(bool checked)
PDFScrollArea *scrollArea = getScrollArea();
if (scrollArea && !pages.isEmpty()) {
qreal portWidth = scrollArea->viewport()->width() - GridBorder * (gridx - 1);
int gy=globalConfig->gridy;
if(pdfdocument->embeddedMode) gy=1;
int gy;
if(pdfdocument->embeddedMode)
gy=gridyEmbedded;
else
gy=globalConfig->gridy;
qreal portHeight = scrollArea->viewport()->height() - GridBorder * (gy - 1); // use globalConfig->gridy as gridy is automatically increased in continous mode to force rendering of surrounding pages
QSizeF pageSize = maxPageSizeFDpiAdjusted();
qreal sfh = portWidth / pageSize.width() / gridx;
Expand Down Expand Up @@ -2919,7 +2952,7 @@ PDFDocument::~PDFDocument()
/*!
* \brief setup ToolBar
*/
void PDFDocument::setupToolBar(){
void PDFDocument::setupToolBar(bool embedded){
toolBar = new QToolBar(this);
toolBar->setWindowTitle(tr("Toolbar"));
toolBar->setObjectName(QString("toolBar"));
Expand Down Expand Up @@ -2949,7 +2982,18 @@ void PDFDocument::setupToolBar(){
toolBar->addAction(actionFit_to_Text_Width);
toolBar->addAction(actionFit_to_Window);
toolBar->addSeparator();
toolBar->addAction(actionAutoHideToolbars);
if (embedded) {
QToolButton *tbPdfView = new QToolButton(toolBar);
actionContinuous->setCheckable(true);
actionContinuous->setChecked(true);
menuGrid->addAction(actionContinuous);
tbPdfView->setMenu(menuGrid);
tbPdfView->setPopupMode(QToolButton::MenuButtonPopup);
tbPdfView->setText(tr("Grid"));
toolBar->addWidget(tbPdfView);
toolBar->addSeparator();
}
toolBar->addAction(actionAutoHideToolbars);
toolBar->addAction(actionEnlargeViewer);
toolBar->addAction(actionShrinkViewer);
toolBar->addAction(actionToggleEmbedded);
Expand Down Expand Up @@ -3147,7 +3191,7 @@ void PDFDocument::init(bool embedded)
//if (!embedded)
setupMenus(embedded);

setupToolBar();
setupToolBar(embedded);


setAttribute(Qt::WA_DeleteOnClose, true);
Expand Down Expand Up @@ -3369,10 +3413,13 @@ void PDFDocument::init(bool embedded)
conf->registerOption("Preview/Continuous", &globalConfig->continuous, true);
conf->linkOptionToObject(&globalConfig->continuous, actionContinuous, LO_NONE);
} else {
pdfWidget->setGridSize(1, 1, true);
pdfWidget->setPageOffset(0, true);
pdfWidget->setSinglePageStep(true);
pdfWidget->setGridxEmbedded(1);
pdfWidget->setGridyEmbedded(1);
pdfWidget->setGridSize(pdfWidget->getGridxEmbedded(), pdfWidget->getGridyEmbedded(), true);
pdfWidget->setPageOffsetEmbedded(0);
pdfWidget->setSinglePageStep(false);
scrollArea->setContinuous(true);
connect(actionContinuous, SIGNAL(toggled(bool)), scrollArea, SLOT(setContinuous(bool)));
}

//connect(actionZoom_In, SIGNAL(triggered()), pdfWidget, SLOT(zoomIn()));
Expand Down Expand Up @@ -3696,11 +3743,11 @@ void PDFDocument::loadCurrentFile(bool fillCache)
break; // message is handled via messageFrame
}
pdfWidget->hide();
pdfWidget->setDocument(document);
pdfWidget->setDocument(document, embeddedMode);
if (error == PDFRenderManager::FileIncomplete)
reloadWhenIdle();
} else {
pdfWidget->setDocument(document);
pdfWidget->setDocument(document, embeddedMode);
pdfWidget->show();

annotations = new PDFAnnotations(this);
Expand Down Expand Up @@ -3805,11 +3852,20 @@ void PDFDocument::setGrid()
d.addVariable(&y , "Y-Grid:");
if (d.exec()) {
pdfWidget->setGridSize(x, y);
globalConfig->gridx = x;
globalConfig->gridy = y;
if (embeddedMode) {
pdfWidget->setGridxEmbedded(x);
pdfWidget->setGridyEmbedded(y);
} else{
globalConfig->gridx = x;
globalConfig->gridy = y;
}
}
// set grid menu entry checked
QString gs=QString("%1x%2").arg(globalConfig->gridx).arg(globalConfig->gridy);
QString gs;
if (embeddedMode)
gs=QString("%1x%2").arg(pdfWidget->getGridxEmbedded()).arg(pdfWidget->getGridyEmbedded());
else
gs=QString("%1x%2").arg(globalConfig->gridx).arg(globalConfig->gridy);
bool found=false;
for(QAction *a:actionGroupGrid->actions()){
if(a->property("grid").toString()==gs){
Expand All @@ -3824,9 +3880,16 @@ void PDFDocument::setGrid()
}
} else {
int p = gs.indexOf("x");
globalConfig->gridx = gs.left(p).toInt();
globalConfig->gridy = gs.mid(p + 1).toInt();
pdfWidget->setGridSize(globalConfig->gridx, globalConfig->gridy);
int x = gs.left(p).toInt();
int y = gs.mid(p + 1).toInt();
pdfWidget->setGridSize(x, y);
if (embeddedMode) {
pdfWidget->setGridxEmbedded(x);
pdfWidget->setGridyEmbedded(y);
} else{
globalConfig->gridx = x;
globalConfig->gridy = y;
}
}
pdfWidget->windowResized();
}
Expand Down
19 changes: 14 additions & 5 deletions src/pdfviewer/PDFDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class PDFWidget : public QLabel
explicit PDFWidget(bool embedded = false);
virtual ~PDFWidget();

void setDocument(const QSharedPointer<Poppler::Document> &doc);
void setDocument(const QSharedPointer<Poppler::Document> &doc, bool embedded);
void setPDFDocument(PDFDocument *docu);

void saveState(); // used when toggling full screen mode
Expand Down Expand Up @@ -232,6 +232,14 @@ class PDFWidget : public QLabel
Q_INVOKABLE void zoom(qreal scale);

virtual void wheelEvent(QWheelEvent *event);
int getGridxEmbedded() {return gridxEmbedded;};
int getGridyEmbedded() {return gridyEmbedded;};
int getPageOffsetEmbedded() {return pageOffsetEmbedded;};
bool getSinglePageStepEmbedded() {return singlePageStepEmbedded;};
void setGridxEmbedded(int x) {gridxEmbedded=x;};
void setGridyEmbedded(int y) {gridyEmbedded=y;};
void setPageOffsetEmbedded(int o) {pageOffsetEmbedded=o;};
void setSinglePageStepEmbedded(bool s) {singlePageStepEmbedded=s;};

protected slots: //not private, so scripts have access
void goFirst();
Expand Down Expand Up @@ -324,7 +332,6 @@ public slots:
QSharedPointer<Poppler::Document> document;
QMutex textwidthCalculationMutex;

//QList<int> pages;
QSharedPointer<Poppler::Link> clickedLink;
QSharedPointer<Poppler::Annotation> clickedAnnotation;

Expand Down Expand Up @@ -363,9 +370,10 @@ public slots:
#endif
int currentTool; // the current tool selected in the toolbar
int usingTool; // the tool actually being used in an ongoing mouse drag
bool singlePageStep;
bool singlePageStep, singlePageStepEmbedded;

int gridx, gridy, pageOffset;
int gridxEmbedded, gridyEmbedded, pageOffsetEmbedded;

bool forceUpdate;

Expand Down Expand Up @@ -561,8 +569,8 @@ private slots:

private:
void init(bool embedded = false);
void setupMenus(bool embedded);
void setupToolBar();
void setupMenus(bool embedded);
void setupToolBar(bool embedded);
void setCurrentFile(const QString &fileName);
void loadSyncData();

Expand Down Expand Up @@ -675,6 +683,7 @@ private slots:

QStatusBar *statusbar;
QToolBar *toolBar;
QToolBar *tbPdfView;
QTimer *toolBarTimer;
public:
QMenu *menuShow;
Expand Down

0 comments on commit 7f83e03

Please sign in to comment.