| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2010> <Alf> |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE ApvlvPdf.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QApplication> |
| 28 |
#include <QInputDialog> |
| 29 |
#include <QPainter> |
| 30 |
#include <QPdfPageNavigator> |
| 31 |
#include <QThread> |
| 32 |
#include <QtPdfWidgets/QPdfView> |
| 33 |
#include <filesystem> |
| 34 |
#include <fstream> |
| 35 |
|
| 36 |
#include "ApvlvQtPdf.h" |
| 37 |
#include "ApvlvUtil.h" |
| 38 |
|
| 39 |
namespace apvlv |
| 40 |
{ |
| 41 |
using namespace std; |
| 42 |
|
| 43 |
FILE_TYPE_DEFINITION ("QtPdf", ApvlvPDF, { ".pdf" }); |
| 44 |
|
| 45 |
bool |
| 46 |
ApvlvPDF::load (const string &filename) |
| 47 |
{ |
| 48 |
mDoc = make_unique<QPdfDocument> (); |
| 49 |
mView = nullptr; |
| 50 |
auto res = mDoc->load (QString::fromLocal8Bit (filename)); |
| 51 |
if (res == QPdfDocument::Error::IncorrectPassword) |
| 52 |
{ |
| 53 |
if (QThread::currentThread () == QApplication::instance ()->thread ()) |
| 54 |
{ |
| 55 |
auto text |
| 56 |
= QInputDialog::getText (nullptr, "password", "input password"); |
| 57 |
if (text.isEmpty ()) |
| 58 |
return false; |
| 59 |
|
| 60 |
auto pass = QByteArray::fromStdString (text.toStdString ()); |
| 61 |
mDoc->setPassword (pass); |
| 62 |
res = mDoc->load (QString::fromLocal8Bit (filename)); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
qWarning () << "file: " << filename << " has password, skip !!!"; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (res != QPdfDocument::Error::None) |
| 71 |
{ |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
mSearchModel = make_unique<QPdfSearchModel> (); |
| 76 |
mSearchModel->setDocument (mDoc.get ()); |
| 77 |
|
| 78 |
generateIndex (); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
PDFWidget * |
| 83 |
ApvlvPDF::getWidget () |
| 84 |
{ |
| 85 |
auto wid = new PDFWidget (); |
| 86 |
wid->setFile (this); |
| 87 |
return wid; |
| 88 |
} |
| 89 |
|
| 90 |
SizeF |
| 91 |
ApvlvPDF::pageSizeF (int pn, int rot) |
| 92 |
{ |
| 93 |
auto qsize = mDoc->pagePointSize (pn); |
| 94 |
if (rot == 0 || rot == 180) |
| 95 |
{ |
| 96 |
return { qsize.width (), qsize.height () }; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
return { qsize.height (), qsize.width () }; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
int |
| 105 |
ApvlvPDF::sum () |
| 106 |
{ |
| 107 |
return mDoc ? mDoc->pageCount () : 0; |
| 108 |
} |
| 109 |
|
| 110 |
unique_ptr<WordListRectangle> |
| 111 |
ApvlvPDF::pageSearch (int pn, const char *str) |
| 112 |
{ |
| 113 |
if (mDoc == nullptr) |
| 114 |
return nullptr; |
| 115 |
|
| 116 |
mSearchModel->setSearchString (str); |
| 117 |
auto results = mSearchModel->resultsOnPage (pn); |
| 118 |
if (results.empty ()) |
| 119 |
return nullptr; |
| 120 |
|
| 121 |
auto poses = make_unique<WordListRectangle> (); |
| 122 |
for (auto const &res : results) |
| 123 |
{ |
| 124 |
WordRectangle word_rectangle; |
| 125 |
for (auto const &rect : res.rectangles ()) |
| 126 |
{ |
| 127 |
word_rectangle.rect_list.push_back ( |
| 128 |
{ rect.left (), rect.bottom (), rect.right (), rect.top () }); |
| 129 |
} |
| 130 |
poses->push_back (word_rectangle); |
| 131 |
} |
| 132 |
return poses; |
| 133 |
} |
| 134 |
|
| 135 |
bool |
| 136 |
ApvlvPDF::pageIsOnlyImage (int pn) |
| 137 |
{ |
| 138 |
auto sel = mDoc->getAllText (pn); |
| 139 |
auto has_text = sel.isValid (); |
| 140 |
return !has_text; |
| 141 |
} |
| 142 |
|
| 143 |
bool |
| 144 |
ApvlvPDF::pageRenderToImage (int pn, double zm, int rot, QImage *pix) |
| 145 |
{ |
| 146 |
if (mDoc == nullptr) |
| 147 |
return false; |
| 148 |
|
| 149 |
using enum QPdfDocumentRenderOptions::Rotation; |
| 150 |
auto sizeF = pageSizeF (pn, rot); |
| 151 |
QSize image_size{ int (sizeF.width * zm), int (sizeF.height * zm) }; |
| 152 |
auto prot = None; |
| 153 |
if (rot == 90) |
| 154 |
prot = Clockwise90; |
| 155 |
if (rot == 180) |
| 156 |
prot = Clockwise180; |
| 157 |
if (rot == 270) |
| 158 |
prot = Clockwise270; |
| 159 |
QPdfDocumentRenderOptions options{}; |
| 160 |
options.setRotation (prot); |
| 161 |
options.setScaledSize (image_size); |
| 162 |
*pix = mDoc->render (pn, image_size, options); |
| 163 |
|
| 164 |
if (auto comments = mNote.getCommentsInPage (pn); !comments.empty ()) |
| 165 |
{ |
| 166 |
pageRenderComments (pn, pix, comments); |
| 167 |
} |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
bool |
| 173 |
ApvlvPDF::pageText (int pn, const Rectangle &rect, string &text) |
| 174 |
{ |
| 175 |
if (mDoc == nullptr) |
| 176 |
return false; |
| 177 |
|
| 178 |
auto selection = mDoc->getSelection (pn, { rect.p1x, rect.p1y }, |
| 179 |
{ rect.p2x, rect.p2y }); |
| 180 |
text = selection.text ().toStdString (); |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
ApvlvPDF::pageRenderComments (int pn, QImage *img, |
| 186 |
const std::vector<Comment> &comments) |
| 187 |
{ |
| 188 |
auto model = make_unique<QPdfSearchModel> (); |
| 189 |
model->setDocument (mDoc.get ()); |
| 190 |
QPainter painter (img); |
| 191 |
painter.setPen (QPen (Qt::blue, 0.4)); |
| 192 |
|
| 193 |
// TODO |
| 194 |
// search not work, need better impl |
| 195 |
for (auto const &comment : comments) |
| 196 |
{ |
| 197 |
model->setSearchString (QString::fromLocal8Bit (comment.quoteText)); |
| 198 |
auto links = model->resultsOnPage (pn); |
| 199 |
if (links.empty ()) |
| 200 |
continue; |
| 201 |
|
| 202 |
for (auto const &link : links) |
| 203 |
{ |
| 204 |
auto rects = link.rectangles (); |
| 205 |
auto brect = rects[0]; |
| 206 |
auto erect = rects[rects.count () - 1]; |
| 207 |
painter.drawLine (brect.x(), brect.y() + brect.height (), erect.x(), brect.y() + brect.height ()); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
painter.end (); |
| 212 |
} |
| 213 |
|
| 214 |
bool |
| 215 |
ApvlvPDF::generateIndex () |
| 216 |
{ |
| 217 |
auto bookmark_model = make_unique<QPdfBookmarkModel> (); |
| 218 |
bookmark_model->setDocument (mDoc.get ()); |
| 219 |
mIndex = { "", 0, getFilename (), FileIndexType::FILE }; |
| 220 |
getIndexIter (mIndex, bookmark_model.get (), QModelIndex ()); |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
void |
| 225 |
ApvlvPDF::getIndexIter (FileIndex &file_index, |
| 226 |
const QPdfBookmarkModel *bookmark_model, |
| 227 |
const QModelIndex &parent) |
| 228 |
{ |
| 229 |
for (auto row = 0; row < bookmark_model->rowCount (parent); ++row) |
| 230 |
{ |
| 231 |
auto index = bookmark_model->index (row, 0, parent); |
| 232 |
auto title = bookmark_model->data (index, Qt::UserRole); |
| 233 |
auto level = bookmark_model->data (index, 257); |
| 234 |
auto page = bookmark_model->data (index, 258); |
| 235 |
auto location = bookmark_model->data (index, 259); |
| 236 |
|
| 237 |
FileIndex child_index (title.toString ().toStdString (), page.toInt (), |
| 238 |
"", FileIndexType::PAGE); |
| 239 |
if (bookmark_model->hasChildren (index)) |
| 240 |
{ |
| 241 |
getIndexIter (child_index, bookmark_model, index); |
| 242 |
} |
| 243 |
|
| 244 |
file_index.mChildrenIndex.emplace_back (child_index); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void |
| 249 |
PDFWidget::setFile (File *file) |
| 250 |
{ |
| 251 |
mFile = file; |
| 252 |
auto pdf = dynamic_cast<ApvlvPDF *> (mFile); |
| 253 |
mPdfView.setDocument (pdf->mDoc.get ()); |
| 254 |
mPdfView.setSearchModel (pdf->mSearchModel.get ()); |
| 255 |
} |
| 256 |
|
| 257 |
void |
| 258 |
PDFWidget::showPage (int p, double s) |
| 259 |
{ |
| 260 |
auto nav = mPdfView.pageNavigator (); |
| 261 |
nav->jump (p, { 0, 0 }); |
| 262 |
scrollTo (s, 0.0); |
| 263 |
mPageNumber = p; |
| 264 |
mScrollValue = s; |
| 265 |
} |
| 266 |
|
| 267 |
void |
| 268 |
PDFWidget::showPage (int p, const string &anchor) |
| 269 |
{ |
| 270 |
auto nav = mPdfView.pageNavigator (); |
| 271 |
nav->jump (p, { 0, 0 }); |
| 272 |
mPageNumber = p; |
| 273 |
mScrollValue = 0; |
| 274 |
} |
| 275 |
|
| 276 |
void |
| 277 |
PDFWidget::setSearchSelect (int select) |
| 278 |
{ |
| 279 |
auto pdf = dynamic_cast<ApvlvPDF *> (mFile); |
| 280 |
auto model = pdf->mSearchModel.get (); |
| 281 |
|
| 282 |
auto doc_select = 0; |
| 283 |
for (auto pn = 0; pn < mPageNumber; ++pn) |
| 284 |
{ |
| 285 |
auto res = model->resultsOnPage (pn); |
| 286 |
doc_select += static_cast<int> (res.size ()); |
| 287 |
} |
| 288 |
doc_select += select; |
| 289 |
mPdfView.setCurrentSearchResultIndex (doc_select); |
| 290 |
|
| 291 |
auto link = model->resultAtIndex (doc_select); |
| 292 |
auto scr = static_cast<int> (link.location ().y ()); |
| 293 |
mValScrollBar->setValue (scr); |
| 294 |
|
| 295 |
qDebug ("link: %d,{%f,%f}: select: %d", link.page (), link.location ().x (), |
| 296 |
link.location ().y (), doc_select); |
| 297 |
mSearchSelect = select; |
| 298 |
} |
| 299 |
|
| 300 |
void |
| 301 |
PDFWidget::setZoomrate (double zm) |
| 302 |
{ |
| 303 |
mZoomrate = zm; |
| 304 |
mPdfView.setZoomFactor (zm); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Local Variables: |
| 309 |
// mode: c++ |
| 310 |
// End: |