Debian Codegraph @

src/ApvlvFrame.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvFrame.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <QBuffer>
29 #include <QClipboard>
30 #include <QMessageBox>
31 #include <filesystem>
32 #include <fstream>
33 #include <memory>
34
35 #include "ApvlvFileWidget.h"
36 #include "ApvlvFrame.h"
37 #include "ApvlvImageWidget.h"
38 #include "ApvlvInfo.h"
39 #include "ApvlvParams.h"
40 #include "ApvlvView.h"
41 #include "ApvlvWeb.h"
42 #include "ApvlvWebViewWidget.h"
43
44 namespace apvlv
45 {
46
47 using namespace std;
48 using namespace Qt;
49 using namespace CommandModeType;
50
51 std::vector<const char *> ApvlvFrame::ZoomLabel = {
52 QT_TR_NOOP ("Default"),
53 QT_TR_NOOP ("Fit Width"),
54 QT_TR_NOOP ("Fit Height"),
55 QT_TR_NOOP ("Custom"),
56 };
57
58 ApvlvFrame::ApvlvFrame (ApvlvView *view) : mToolStatus (this)
59 {
60 mInuse = true;
61
62 mView = view;
63
64 mProCmd = 0;
65
66 mZoomMode = ZoomMode::NORMAL;
67
68 mSearchResults = nullptr;
69 mSearchStr = "";
70
71 setLayout (&mVbox);
72
73 mPaned.setHandleWidth (4);
74 mDirectoryWidth = DEFAULT_CONTENT_WIDTH;
75
76 auto f_width = ApvlvParams::instance ()->getIntOrDefault ("fix_width");
77 auto f_height = ApvlvParams::instance ()->getIntOrDefault ("fix_height");
78
79 if (f_width > 0 && f_height > 0)
80 {
81 mPaned.setFixedSize (f_width, f_height);
82
83 mVbox.addLayout (&mHBoxLayout, 1);
84 mHBoxLayout.addWidget (&mPaned, 0);
85 }
86 else
87 {
88 mVbox.addWidget (&mPaned, 1);
89 }
90
91 mDirectory.setFrame (this);
92 QObject::connect (this, SIGNAL (indexGenerited (const FileIndex &)),
93 &mDirectory, SLOT (setIndex (const FileIndex &)));
94
95 // left is Directory
96 mPaned.addWidget (&mDirectory);
97
98 // Right is Text
99 mPaned.addWidget (&mTextFrame);
100 mTextFrame.setLayout (&mTextLayout);
101 mTextLayout.addWidget (&mToolStatus, 0);
102 auto guiopt = ApvlvParams::instance ()->getStringOrDefault ("guioptions");
103 if (guiopt.find ('S') == string::npos)
104 {
105 mToolStatus.hide ();
106 }
107
108 mVbox.addWidget (&mStatus, 0);
109 if (guiopt.find ('s') == string::npos)
110 {
111 mStatus.hide ();
112 }
113 qDebug () << "ApvlvFrame: " << this << " be created";
114 }
115
116 ApvlvFrame::~ApvlvFrame ()
117 {
118 qDebug () << "ApvlvFrame: " << this << " be freed";
119 saveLastPosition (mFilestr);
120 }
121
122 void
123 ApvlvFrame::inuse (bool use)
124 {
125 mInuse = use;
126 }
127
128 bool
129 ApvlvFrame::inuse ()
130 {
131 return mInuse;
132 }
133
134 bool
135 ApvlvFrame::loadUri (const string &uri)
136 {
137 mFile = make_unique<ApvlvWEB> ();
138 mFile->load (uri);
139 setWidget (mFile->getDisplayType ());
140 refresh (0, 0.0);
141 return true;
142 }
143
144 const char *
145 ApvlvFrame::filename ()
146 {
147 return mFilestr.empty () ? nullptr : mFilestr.c_str ();
148 }
149
150 bool
151 ApvlvFrame::print ([[maybe_unused]] int ct)
152 {
153 return false;
154 }
155
156 int
157 ApvlvFrame::getSkip ()
158 {
159 return mSkip;
160 }
161
162 void
163 ApvlvFrame::setSkip (int ct)
164 {
165 mSkip = ct;
166 }
167
168 void
169 ApvlvFrame::toggleDirectory ()
170 {
171 auto show = !isShowDirectory ();
172 toggleDirectory (show);
173 }
174
175 void
176 ApvlvFrame::toggleDirectory (bool show)
177 {
178 if (show)
179 {
180 if (!mDirectory.isReady ())
181 {
182 qWarning () << "file " << mFilestr << " has no directory";
183 show = false;
184 }
185 }
186
187 auto sizes = mPaned.sizes ();
188 if (show)
189 {
190 mDirIndex = {};
191 if (sizes[0] == 0)
192 {
193 auto psize = mPaned.size ();
194 sizes = { mDirectoryWidth, psize.width () - mPaned.handleWidth ()
195 - DEFAULT_CONTENT_WIDTH };
196 mPaned.setSizes (sizes);
197 }
198 }
199 else
200 {
201 if (sizes[0] != 0)
202 mDirectoryWidth = sizes[0];
203
204 auto psize = mPaned.size ();
205 sizes = { 0, psize.width () - mPaned.handleWidth () };
206 mPaned.setSizes (sizes);
207 }
208 }
209
210 void
211 ApvlvFrame::setActive (bool act)
212 {
213 mActive = act;
214
215 auto wid = mWidget->widget ();
216 if (act)
217 {
218 QTimer::singleShot (50, wid, SLOT (setFocus ()));
219 }
220 else
221 {
222 wid->clearFocus ();
223 clearFocus ();
224 }
225
226 if (mActive && filename ())
227 {
228 auto path = filesystem::path (filename ());
229 auto base = path.filename ();
230 mView->setTitle (base.string ());
231 }
232
233 mStatus.setActive (act);
234 }
235
236 void
237 ApvlvFrame::setDirIndex (const string &path)
238 {
239 mDirIndex = { "", 0, path, FileIndexType::DIR };
240 mDirIndex.loadDirectory (path);
241 emit indexGenerited (mDirIndex);
242 toggleDirectory (true);
243 }
244
245 bool
246 ApvlvFrame::toggledControlDirectory (bool is_right)
247 {
248 if (!isShowDirectory ())
249 {
250 return false;
251 }
252
253 if (auto controlled = isControlledDirectory (); !controlled && !is_right)
254 {
255 mDirectory.setActive (true);
256 return true;
257 }
258 else if (controlled && is_right)
259 {
260 mDirectory.setActive (false);
261 return true;
262 }
263
264 return false;
265 }
266
267 bool
268 ApvlvFrame::isShowDirectory ()
269 {
270 auto sizes = mPaned.sizes ();
271 return sizes[0] > 1;
272 }
273
274 bool
275 ApvlvFrame::isControlledDirectory ()
276 {
277 if (!isShowDirectory ())
278 return false;
279
280 return mDirectory.isActive ();
281 }
282
283 ApvlvFrame *
284 ApvlvFrame::findByWidget (QWidget *widget)
285 {
286 for (auto doc = widget; doc != nullptr; doc = doc->parentWidget ())
287 {
288 if (doc->inherits ("apvlv::ApvlvFrame"))
289 return dynamic_cast<ApvlvFrame *> (doc);
290 }
291
292 return nullptr;
293 }
294
295 ApvlvStatus::ApvlvStatus ()
296 {
297 setFrameShape (QFrame::NoFrame);
298 setLayout (&mLayout);
299 }
300
301 void
302 ApvlvStatus::setActive (bool act)
303 {
304 auto children = findChildren<QLabel *> ();
305 for (auto child : children)
306 {
307 if (child)
308 {
309 child->setEnabled (act);
310 }
311 }
312 }
313
314 void
315 ApvlvStatus::showMessages (const vector<string> &msgs)
316 {
317 auto children = findChildren<QLabel *> ();
318 vector<QWidget *> newlabels;
319 for (std::size_t ind = 0; ind < msgs.size (); ++ind)
320 {
321 if (children.size () > (qsizetype)ind)
322 {
323 auto label = children[ind];
324 label->setText (QString::fromLocal8Bit (msgs[ind]));
325 }
326 else
327 {
328 auto label = new QLabel ();
329 label->setText (QString::fromLocal8Bit (msgs[ind]));
330 newlabels.push_back (label);
331 }
332 }
333
334 auto hbox = layout ();
335 for (auto label : newlabels)
336 {
337 hbox->addWidget (label);
338 }
339 }
340
341 ApvlvToolStatus::ApvlvToolStatus (ApvlvFrame *frame) : mFrame (frame)
342 {
343 auto paction = addAction (QIcon::fromTheme (QIcon::ThemeIcon::GoPrevious),
344 tr ("Previous Page"));
345 QObject::connect (paction, SIGNAL (triggered (bool)), mFrame,
346 SLOT (previousPage ()));
347 auto naction = addAction (QIcon::fromTheme (QIcon::ThemeIcon::GoNext),
348 tr ("Next Page"));
349 QObject::connect (naction, SIGNAL (triggered (bool)), mFrame,
350 SLOT (nextPage ()));
351
352 mPageValue.setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
353 QObject::connect (&mPageValue, SIGNAL (editingFinished ()), this,
354 SLOT (gotoPage ()));
355 addWidget (&mPageValue);
356 addWidget (&mPageSum);
357 addSeparator ();
358
359 addWidget (&mScrollRate);
360 addSeparator ();
361
362 auto iaction = addAction (QIcon::fromTheme (QIcon::ThemeIcon::ZoomIn),
363 tr ("Zoom In"));
364 QObject::connect (iaction, SIGNAL (triggered (bool)), mFrame,
365 SLOT (zoomIn ()));
366 auto oaction = addAction (QIcon::fromTheme (QIcon::ThemeIcon::ZoomOut),
367 tr ("Zoom Out"));
368 QObject::connect (oaction, SIGNAL (triggered (bool)), mFrame,
369 SLOT (zoomOut ()));
370
371 addWidget (&mZoomType);
372 for (auto const &str : ApvlvFrame::ZoomLabel)
373 {
374 mZoomType.addItem (ApvlvFrame::tr (str));
375 }
376 QObject::connect (&mZoomType, SIGNAL (currentIndexChanged (int)), mFrame,
377 SLOT (setZoomMode (int)));
378 mZoomType.setLineEdit (&mZoomValue);
379 addSeparator ();
380
381 #ifdef APVLV_WITH_OCR
382 addWidget (&mOcrParse);
383 mOcrParse.setText (tr ("OCR Parse"));
384 QObject::connect (&mOcrParse, SIGNAL (checkStateChanged (Qt::CheckState)),
385 mFrame, SLOT (ocrParse ()));
386
387 addAction (&mOcrCopy);
388 mOcrCopy.setIcon (QIcon::fromTheme (QIcon::ThemeIcon::Scanner));
389 mOcrCopy.setText (tr ("OCR Copy"));
390 QObject::connect (&mOcrCopy, SIGNAL (triggered (bool)), mFrame,
391 SLOT (ocrCopy ()));
392 #endif
393 }
394
395 void
396 ApvlvToolStatus::updateValue (int pn, int totpn, double zm, double sr)
397 {
398 mPageValue.setText (QString::number (pn));
399 mPageSum.setText (QString::fromLocal8Bit ("/%1").arg (totpn));
400 if (mZoomType.currentIndex () == 3)
401 {
402 mZoomValue.setText (
403 QString::fromLocal8Bit ("%1%").arg (static_cast<int> (zm * 100)));
404 }
405 mScrollRate.setText (
406 QString::fromLocal8Bit ("%1%").arg (static_cast<int> (sr * 100)));
407
408 #ifdef APVLV_WITH_OCR
409 auto need_ocr = mFrame->mFile->pageIsOnlyImage (mFrame->pageNumber ());
410 mOcrParse.setEnabled (need_ocr);
411 mOcrCopy.setEnabled (need_ocr);
412 #endif
413 }
414
415 void
416 ApvlvToolStatus::gotoPage ()
417 {
418 auto text = mPageValue.text ().trimmed ();
419 auto pn = text.toInt ();
420 if (pn != mFrame->pageNumber ())
421 {
422 mFrame->showPage (pn, 0.f);
423 }
424 }
425
426 CmdReturn
427 ApvlvFrame::subProcess ([[maybe_unused]] int ct, uint key)
428 {
429 uint procmd = mProCmd;
430 mProCmd = 0;
431 switch (procmd)
432 {
433 case 'm':
434 markposition (char (key));
435 break;
436
437 case '\'':
438 jump (char (key));
439 break;
440
441 case 'z':
442 if (key == 'i')
443 {
444 zoomIn ();
445 }
446 else if (key == 'o')
447 {
448 zoomOut ();
449 }
450 else if (key == 'h')
451 {
452 setZoomMode (static_cast<int> (ZoomMode::FITHEIGHT));
453 }
454 else if (key == 'w')
455 {
456 setZoomMode (static_cast<int> (ZoomMode::FITWIDTH));
457 }
458 break;
459
460 default:
461 return CmdReturn::NO_MATCH;
462 break;
463 }
464
465 return CmdReturn::MATCH;
466 }
467
468 void
469 ApvlvFrame::previousPage ()
470 {
471 previousPage (1);
472 }
473
474 void
475 ApvlvFrame::nextPage ()
476 {
477 nextPage (1);
478 }
479
480 void
481 ApvlvFrame::setZoomMode (int mode)
482 {
483 if (mode < static_cast<int> (ZoomMode::CUSTOM))
484 {
485 switch (static_cast<ZoomMode> (mode))
486 {
487 using enum ZoomMode;
488 case NORMAL:
489 setZoomString ("normal");
490 break;
491 case FITWIDTH:
492 setZoomString ("fitwidth");
493 break;
494 case FITHEIGHT:
495 setZoomString ("fitheight");
496 break;
497 case CUSTOM:
498 break;
499 }
500 }
501
502 updateStatus ();
503 }
504
505 void
506 ApvlvFrame::zoomIn ()
507 {
508 auto zoomrate = mWidget->zoomrate ();
509 setZoomrate (zoomrate * 1.1);
510 updateStatus ();
511 }
512
513 void
514 ApvlvFrame::zoomOut ()
515 {
516 auto zoomrate = mWidget->zoomrate ();
517 setZoomrate (zoomrate / 1.1);
518 updateStatus ();
519 }
520
521 #ifdef APVLV_WITH_OCR
522 void
523 ApvlvFrame::ocrParse ()
524 {
525 auto meta = mWidget->widget ()->metaObject ();
526 qDebug () << "widget is " << meta->className ();
527 if (!mWidget->widget ()->inherits ("apvlv::ApvlvImage"))
528 return;
529
530 auto image = dynamic_cast<ApvlvImage *> (mWidget->widget ());
531 auto state = mToolStatus.mOcrParse.checkState ();
532 image->ocrDisplay (state == Qt::Checked);
533 }
534
535 void
536 ApvlvFrame::ocrCopy ()
537 {
538 auto meta = mWidget->widget ()->metaObject ();
539 qDebug () << "widget is " << meta->className ();
540 if (!mWidget->widget ()->inherits ("apvlv::ApvlvImage"))
541 return;
542
543 auto image = dynamic_cast<ApvlvImage *> (mWidget->widget ());
544 auto text = image->ocrGetText ();
545 #ifdef QT_DEBUG
546 QMessageBox::information (this, tr ("text in clipboard"),
547 QString::fromLocal8Bit (text.get ()));
548 #endif
549 auto clipboard = QGuiApplication::clipboard ();
550 clipboard->setText (text.get ());
551 }
552 #endif
553
554 void
555 ApvlvFrame::wheelEvent (QWheelEvent *event)
556 {
557 auto angel = event->angleDelta ();
558 if (angel.y () > 0)
559 {
560 mWidget->scrollUp (1);
561 updateStatus ();
562 }
563 else
564 {
565 mWidget->scrollDown (1);
566 updateStatus ();
567 }
568 }
569
570 CmdReturn
571 ApvlvFrame::process (int has, int ct, uint key)
572 {
573 emit focusIn ();
574
575 if (mProCmd != 0)
576 {
577 return subProcess (ct, key);
578 }
579
580 if (!has)
581 {
582 ct = 1;
583 }
584
585 switch (key)
586 {
587 case Key_PageDown:
588 case ctrlValue ('f'):
589 nextPage (ct);
590 break;
591 case Key_PageUp:
592 case ctrlValue ('b'):
593 previousPage (ct);
594 break;
595 case ctrlValue ('d'):
596 halfNextPage (ct);
597 break;
598 case ctrlValue ('u'):
599 halfPreviousPage (ct);
600 break;
601 case ':':
602 case '/':
603 case '?':
604 case 'F':
605 if (isControlledDirectory ())
606 {
607 mDirectory.focusFilter ();
608 }
609 else
610 {
611 mView->promptCommand (char (key));
612 return CmdReturn::NEED_MORE;
613 }
614 case 'H':
615 mWidget->scrollTo (0.0, 0.0);
616 break;
617 case 'M':
618 mWidget->scrollTo (0.0, 0.5);
619 break;
620 case 'L':
621 mWidget->scrollTo (0.0, 1.0);
622 break;
623 case '0':
624 mWidget->scrollLeft (INT_MAX);
625 break;
626 case '$':
627 mWidget->scrollRight (INT_MAX);
628 break;
629 case ctrlValue ('p'):
630 case Key_Up:
631 case 'k':
632 if (isControlledDirectory ())
633 {
634 mDirectory.scrollUp (ct);
635 }
636 else
637 {
638 mWidget->scrollUp (ct);
639 updateStatus ();
640 }
641 break;
642 case ctrlValue ('n'):
643 case ctrlValue ('j'):
644 case Key_Down:
645 case 'j':
646 if (isControlledDirectory ())
647 {
648 mDirectory.scrollDown (ct);
649 }
650 else
651 {
652 mWidget->scrollDown (ct);
653 updateStatus ();
654 }
655 break;
656 case Key_Backspace:
657 case Key_Left:
658 case ctrlValue ('h'):
659 case 'h':
660 if (isControlledDirectory ())
661 {
662 mDirectory.scrollLeft (ct);
663 }
664 else
665 {
666 mWidget->scrollLeft (ct);
667 updateStatus ();
668 }
669 break;
670 case Key_Space:
671 case Key_Right:
672 case ctrlValue ('l'):
673 case 'l':
674 if (isControlledDirectory ())
675 {
676 mDirectory.scrollRight (ct);
677 }
678 else
679 {
680 mWidget->scrollRight (ct);
681 updateStatus ();
682 }
683 break;
684 case Key_Return:
685 directoryShowPage (mDirectory.currentItemFileIndex (), false);
686 break;
687 case 'R':
688 reload ();
689 break;
690 case ctrlValue (']'):
691 gotoLink (ct);
692 break;
693 case ctrlValue ('t'):
694 returnLink (ct);
695 break;
696 case 't':
697 mView->newTab (HelpPdf);
698 mView->open ();
699 break;
700 case 'T':
701 mView->newTab (HelpPdf);
702 mView->openDir ();
703 break;
704 case 'o':
705 mView->open ();
706 break;
707 case 'O':
708 mView->openDir ();
709 break;
710 case 'r':
711 rotate (ct);
712 break;
713 case 'G':
714 markposition ('\'');
715 if (!has)
716 {
717 showPage (mFile->sum () - 1, 0.0);
718 }
719 else
720 {
721 showPage (ct - 1, 0.0);
722 }
723 break;
724 case 'm':
725 case '\'':
726 case 'z':
727 mProCmd = key;
728 return CmdReturn::NEED_MORE;
729 break;
730 case 'n':
731 if (mSearchCmd == SEARCH)
732 {
733 markposition ('\'');
734 search ("", false);
735 }
736 else if (mSearchCmd == BACKSEARCH)
737 {
738 markposition ('\'');
739 search ("", true);
740 }
741 break;
742 case 'N':
743 if (mSearchCmd == SEARCH)
744 {
745 markposition ('\'');
746 search ("", true);
747 }
748 else if (mSearchCmd == BACKSEARCH)
749 {
750 markposition ('\'');
751 search ("", false);
752 }
753 break;
754 case 's':
755 setSkip (ct);
756 break;
757 case 'c':
758 toggleDirectory ();
759 break;
760 default:
761 return CmdReturn::NO_MATCH;
762 break;
763 }
764
765 return CmdReturn::MATCH;
766 }
767
768 ApvlvFrame *
769 ApvlvFrame::clone ()
770 {
771 auto *ndoc = new ApvlvFrame (mView);
772 ndoc->loadFile (mFilestr, false, false);
773 ndoc->showPage (mWidget->pageNumber (), mWidget->scrollRate ());
774 return ndoc;
775 }
776
777 void
778 ApvlvFrame::setZoomrate (double zm)
779 {
780 mZoomMode = ZoomMode::CUSTOM;
781 mWidget->setZoomrate (zm);
782 }
783
784 void
785 ApvlvFrame::setZoomString (const char *z)
786 {
787 auto zoomrate = mWidget->zoomrate ();
788 if (z != nullptr)
789 {
790 string_view sv (z);
791 if (sv == "normal")
792 {
793 mZoomMode = ZoomMode::NORMAL;
794 zoomrate = 1.2;
795 }
796 else if (sv == "fitwidth")
797 {
798 mZoomMode = ZoomMode::FITWIDTH;
799 }
800 else if (sv == "fitheight")
801 {
802 mZoomMode = ZoomMode::FITHEIGHT;
803 }
804 else
805 {
806 double d = strtod (z, nullptr);
807 if (d > 0)
808 {
809 mZoomMode = ZoomMode::CUSTOM;
810 zoomrate = d;
811 }
812 }
813 }
814
815 if (mFile)
816 {
817 int pn = std::max (0, pageNumber () - 1);
818 auto size = mFile->pageSizeF (pn, 0);
819
820 if (size.width > 0 && size.height > 0)
821 {
822 auto wid = mWidget->widget ();
823 if (mZoomMode == ZoomMode::FITWIDTH)
824 {
825 auto x_root = wid->width ();
826 zoomrate = x_root / size.width;
827 }
828 else if (mZoomMode == ZoomMode::FITHEIGHT)
829 {
830 auto y_root = wid->height ();
831 zoomrate = y_root / size.height;
832 }
833 }
834 mWidget->setZoomrate (zoomrate);
835 }
836 }
837
838 bool
839 ApvlvFrame::saveLastPosition (const string &filename)
840 {
841 if (filename.empty () || HelpPdf == filename
842 || ApvlvParams::instance ()->getBoolOrDefault ("noinfo", false))
843 {
844 return false;
845 }
846
847 bool ret = ApvlvInfo::instance ()->updateFile (
848 mWidget->pageNumber (), mSkip, mWidget->scrollRate (), filename);
849 return ret;
850 }
851
852 bool
853 ApvlvFrame::loadLastPosition (const string &filename)
854 {
855 if (filename.empty () || HelpPdf == filename
856 || ApvlvParams::instance ()->getBoolOrDefault ("noinfo"))
857 {
858 showPage (0, 0.0);
859 return false;
860 }
861
862 bool ret = false;
863
864 auto optfp = ApvlvInfo::instance ()->file (filename);
865 if (optfp)
866 {
867 // correctly check
868 showPage (optfp.value ()->page, 0.0);
869 setSkip (optfp.value ()->skip);
870 }
871 else
872 {
873 showPage (0, 0.0);
874 ApvlvInfo::instance ()->updateFile (0, 0.0, mWidget->zoomrate (),
875 filename);
876 }
877
878 return ret;
879 }
880
881 bool
882 ApvlvFrame::reload ()
883 {
884 return loadFile (mFilestr, false, isShowDirectory ());
885 }
886
887 int
888 ApvlvFrame::pageNumber ()
889 {
890 return mWidget ? mWidget->pageNumber () : 0;
891 }
892
893 bool
894 ApvlvFrame::loadFile (const std::string &file, bool check, bool show_directory)
895 {
896 if (check && file == mFilestr)
897 {
898 return false;
899 }
900
901 saveLastPosition (mFilestr);
902
903 mFile = FileFactory::loadFile (file);
904
905 if (mFile)
906 {
907 emit indexGenerited (mFile->getIndex ());
908
909 mFilestr = file;
910
911 if (mFile->sum () <= 1)
912 {
913 qDebug () << "sum () = " << mFile->sum ();
914 }
915
916 setWidget (mFile->getDisplayType ());
917
918 loadLastPosition (file);
919
920 setActive (true);
921
922 mSearchStr = "";
923 mSearchResults = nullptr;
924
925 if (ApvlvParams::instance ()->getIntOrDefault ("autoreload") > 0)
926 {
927 mWatcher = make_unique<QFileSystemWatcher> ();
928 QObject::connect (mWatcher.get (), SIGNAL (fileChanged ()), this,
929 SLOT (changed_cb ()));
930
931 auto systempath = filesystem::path (file);
932 if (filesystem::is_symlink (systempath))
933 {
934 auto realname = filesystem::read_symlink (systempath).string ();
935 if (filesystem::is_regular_file (realname))
936 {
937 mWatcher->addPath (QString::fromLocal8Bit (realname));
938 }
939 }
940 else
941 {
942 mWatcher->addPath (QString::fromLocal8Bit (file));
943 }
944 }
945 }
946
947 if (show_directory && mFile != nullptr)
948 {
949 toggleDirectory (true);
950 }
951 else
952 {
953 toggleDirectory (false);
954 }
955
956 return mFile != nullptr;
957 }
958
959 void
960 ApvlvFrame::markposition (const char s)
961 {
962 ApvlvDocPosition adp = { mWidget->pageNumber (), mWidget->scrollRate () };
963 mPositions[s] = adp;
964 }
965
966 void
967 ApvlvFrame::jump (const char s)
968 {
969 auto it = mPositions.find (s);
970 if (it != mPositions.end ())
971 {
972 ApvlvDocPosition adp = it->second;
973 markposition ('\'');
974 showPage (adp.pagenum, adp.scrollrate);
975 }
976 }
977
978 void
979 ApvlvFrame::showPage (int pn, double s)
980 {
981 auto rp = mFile->pageNumberWrap (pn);
982 if (rp < 0)
983 return;
984
985 mWidget->setAnchor ("");
986
987 if (!mZoominit)
988 {
989 mZoominit = true;
990 setZoomString (nullptr);
991 }
992
993 refresh (rp, s);
994 }
995
996 void
997 ApvlvFrame::showPage (int pn, const std::string &anchor)
998 {
999 auto rp = mFile->pageNumberWrap (pn);
1000 if (rp < 0)
1001 return;
1002 if (!mZoominit)
1003 {
1004 mZoominit = true;
1005 setZoomString (nullptr);
1006 }
1007
1008 mWidget->showPage (rp, anchor);
1009 updateStatus ();
1010 }
1011
1012 void
1013 ApvlvFrame::nextPage (int times)
1014 {
1015 showPage (mWidget->pageNumber () + times, 0.0f);
1016 }
1017
1018 void
1019 ApvlvFrame::previousPage (int times)
1020 {
1021 showPage (mWidget->pageNumber () - times, 0.0f);
1022 }
1023
1024 void
1025 ApvlvFrame::refresh (int pn, double s)
1026 {
1027 if (mFile == nullptr)
1028 return;
1029
1030 mWidget->showPage (pn, s);
1031 updateStatus ();
1032 }
1033
1034 void
1035 ApvlvFrame::halfNextPage (int times)
1036 {
1037 double sr = mWidget->scrollRate ();
1038 int rtimes = times / 2;
1039
1040 if (times % 2 != 0)
1041 {
1042 if (sr > 0.5)
1043 {
1044 sr = 0;
1045 rtimes += 1;
1046 }
1047 else
1048 {
1049 sr = 1;
1050 }
1051 }
1052
1053 showPage (mWidget->pageNumber () + rtimes, sr);
1054 }
1055
1056 void
1057 ApvlvFrame::halfPreviousPage (int times)
1058 {
1059 double sr = mWidget->scrollRate ();
1060 int rtimes = times / 2;
1061
1062 if (times % 2 != 0)
1063 {
1064 if (sr < 0.5)
1065 {
1066 sr = 1;
1067 rtimes += 1;
1068 }
1069 else
1070 {
1071 sr = 0;
1072 }
1073 }
1074
1075 showPage (mWidget->pageNumber () - rtimes, sr);
1076 }
1077
1078 bool
1079 ApvlvFrame::needSearch (const std::string &str, bool reverse)
1080 {
1081 if (mFile == nullptr)
1082 return false;
1083
1084 // search a different string
1085 if (!str.empty () && str != mSearchStr)
1086 {
1087 qDebug () << "different string.";
1088 mSearchStr = str;
1089 return true;
1090 }
1091
1092 else if (mSearchResults == nullptr)
1093 {
1094 qDebug () << "no result.";
1095 return true;
1096 }
1097
1098 // same string, but need to search next page
1099 else if ((!reverse
1100 && mWidget->searchSelect () == (int)mSearchResults->size () - 1)
1101 || (reverse && mWidget->searchSelect () == 0))
1102 {
1103 qDebug () << "same, but need next string: s: " << reverse
1104 << ", sel: " << mWidget->searchSelect ()
1105 << ", max: " << mSearchResults->size ();
1106 return true;
1107 }
1108
1109 // same string, not need search, but has zoomed
1110 else
1111 {
1112 qDebug () << "same, not need next string. sel: "
1113 << mWidget->searchSelect ()
1114 << ", max: " << mSearchResults->size ();
1115 if (!reverse)
1116 {
1117 setHighlightAndIndex (*mSearchResults, mWidget->searchSelect () + 1);
1118 }
1119 else
1120 {
1121 setHighlightAndIndex (*mSearchResults, mWidget->searchSelect () - 1);
1122 }
1123
1124 return false;
1125 }
1126
1127 return false;
1128 }
1129
1130 bool
1131 ApvlvFrame::search (const char *str, bool reverse)
1132 {
1133 if (*str == '\0' && mSearchStr.empty ())
1134 {
1135 return false;
1136 }
1137
1138 if (*str)
1139 {
1140 mSearchCmd
1141 = (reverse ? CommandModeType::BACKSEARCH : CommandModeType::SEARCH);
1142 }
1143
1144 if (!needSearch (str, reverse))
1145 {
1146 return true;
1147 }
1148
1149 mSearchResults = nullptr;
1150 unsetHighlight ();
1151
1152 auto wrap = ApvlvParams::instance ()->getBoolOrDefault ("wrapscan");
1153
1154 auto i = mWidget->pageNumber ();
1155 auto sum = mFile->sum ();
1156 auto from = i;
1157 bool search = false;
1158 while (true)
1159 {
1160 if (*str != 0 || search)
1161 {
1162 mSearchResults
1163 = mFile->pageSearch ((i + sum) % sum, mSearchStr.c_str ());
1164 if (mSearchResults != nullptr && !mSearchResults->empty ())
1165 {
1166 if (i != mWidget->pageNumber ())
1167 showPage (i, 0.0);
1168 auto results = *mSearchResults;
1169 auto sel = 0;
1170 if (reverse)
1171 sel = static_cast<int> (results.size () - 1);
1172 setHighlightAndIndex (results, sel);
1173 return true;
1174 }
1175 }
1176
1177 search = true;
1178
1179 if (!reverse && i < (wrap ? (from + sum) : (sum - 1)))
1180 {
1181 i++;
1182 }
1183 else if (reverse && i > (wrap ? (from - sum) : 0))
1184 {
1185 i--;
1186 }
1187 else
1188 {
1189 mView->errorMessage (string ("can't find word: "), mSearchStr);
1190 return false;
1191 }
1192 }
1193 }
1194
1195 bool
1196 ApvlvFrame::totext (const char *file)
1197 {
1198 if (mFile == nullptr)
1199 return false;
1200
1201 auto pn = mWidget->pageNumber ();
1202 string txt;
1203 auto size = mFile->pageSizeF (pn, 0);
1204 bool ret = mFile->pageText (pn, { 0, 0, size.width, size.height }, txt);
1205 if (ret)
1206 {
1207 fstream fs{ filename (), ios::out };
1208 if (fs.is_open ())
1209 {
1210 fs.write (txt.c_str (), txt.length ());
1211 fs.close ();
1212 return true;
1213 }
1214 }
1215 return false;
1216 }
1217
1218 bool
1219 ApvlvFrame::rotate (int ct)
1220 {
1221 // just hack
1222 if (ct == 1)
1223 ct = 90;
1224
1225 if (ct % 90 != 0)
1226 {
1227 mView->errorMessage ("Not a 90 times value: ", ct);
1228 return false;
1229 }
1230
1231 auto rotate = mWidget->rotate ();
1232 rotate += ct;
1233 while (rotate < 0)
1234 {
1235 rotate += 360;
1236 }
1237 while (rotate > 360)
1238 {
1239 rotate -= 360;
1240 }
1241 mWidget->setRotate (rotate);
1242 refresh (mWidget->pageNumber (), 0.0);
1243 return true;
1244 }
1245
1246 void
1247 ApvlvFrame::gotoLink ([[maybe_unused]] int ct)
1248 {
1249 // need impl
1250 }
1251
1252 void
1253 ApvlvFrame::returnLink ([[maybe_unused]] int ct)
1254 {
1255 // need impl
1256 }
1257
1258 void
1259 ApvlvFrame::directoryShowPage (const FileIndex *index, bool force)
1260 {
1261 if (index == nullptr)
1262 return;
1263
1264 if (index->type == FileIndexType::FILE)
1265 {
1266 loadFile (index->path, true, true);
1267 return;
1268 }
1269
1270 auto file = mDirectory.currentFileFileIndex ();
1271 if (file && file->path != mFilestr)
1272 loadFile (file->path, true, true);
1273
1274 if (index->type == FileIndexType::PAGE)
1275 {
1276 if (index->page != mWidget->pageNumber ()
1277 || index->anchor != mWidget->anchor ())
1278 showPage (index->page, index->anchor);
1279 }
1280 }
1281
1282 void
1283 ApvlvFrame::setWidget (DISPLAY_TYPE type)
1284 {
1285 auto sizes = mPaned.sizes ();
1286
1287 if (type == DISPLAY_TYPE::IMAGE)
1288 {
1289 mWidget = make_unique<ImageWidget> ();
1290 mWidget->setFile (mFile.get ());
1291 #ifdef APVLV_WITH_OCR
1292 ocrParse ();
1293 #endif
1294 }
1295 else if (type == DISPLAY_TYPE::HTML)
1296 {
1297 mWidget = make_unique<WebViewWidget> ();
1298 mWidget->setFile (mFile.get ());
1299 }
1300 else
1301 {
1302 mWidget.reset (mFile->getWidget ());
1303 }
1304 mTextLayout.addWidget (mWidget->widget (), 1);
1305
1306 mPaned.setSizes (sizes);
1307 }
1308
1309 void
1310 ApvlvFrame::unsetHighlight ()
1311 {
1312 mWidget->setSearchStr ("");
1313 mWidget->setSearchSelect (0);
1314 mWidget->setSearchResults ({});
1315 }
1316
1317 void
1318 ApvlvFrame::setHighlightAndIndex (const WordListRectangle &poses, int sel)
1319 {
1320 if (!poses[sel].word.empty ())
1321 {
1322 mWidget->setSearchStr (poses[sel].word);
1323 }
1324 mWidget->setSearchSelect (sel);
1325 mWidget->setSearchResults (poses);
1326
1327 auto sr = mWidget->scrollRate ();
1328 if (!poses[sel].rect_list.empty ())
1329 {
1330 auto rect = poses[sel].rect_list[0];
1331 auto size
1332 = mFile->pageSizeF (mWidget->pageNumber (), mWidget->rotate ());
1333 if (size.height > 0)
1334 {
1335 sr = rect.p2y / size.height;
1336 }
1337 }
1338
1339 refresh (mWidget->pageNumber (), sr);
1340 }
1341
1342 void
1343 ApvlvFrame::updateStatus ()
1344 {
1345 if (filename ())
1346 {
1347 vector<string> labels;
1348
1349 int pn = pageNumber () + 1;
1350 int totpn = mFile->sum ();
1351 auto zm = mWidget->zoomrate ();
1352 auto sr = mWidget->scrollRate ();
1353 string anchor = mWidget->anchor ();
1354
1355 auto systempath = filesystem::path (filename ());
1356 auto bn = systempath.filename ();
1357 labels.emplace_back (bn.string ());
1358
1359 auto ss = QString ("%1/%2").arg (pn).arg (totpn);
1360 labels.emplace_back (ss.toStdString ());
1361
1362 ss = QString ("%1%").arg (static_cast<int> (zm * 100));
1363 labels.emplace_back (ss.toStdString ());
1364
1365 ss = QString ("%1%").arg (static_cast<int> (sr * 100));
1366 labels.emplace_back (ss.toStdString ());
1367
1368 mStatus.showMessages (labels);
1369
1370 mToolStatus.updateValue (pn, totpn, zm, sr);
1371
1372 mDirectory.setCurrentIndex (mFilestr, mWidget->pageNumber (),
1373 mWidget->anchor ());
1374 }
1375 }
1376
1377 bool
1378 ApvlvFrame::isStatusHidden ()
1379 {
1380 return mStatus.isHidden ();
1381 }
1382
1383 void
1384 ApvlvFrame::statusShow ()
1385 {
1386 mStatus.show ();
1387 }
1388
1389 void
1390 ApvlvFrame::statusHide ()
1391 {
1392 mStatus.hide ();
1393 }
1394
1395 }
1396
1397 // Local Variables:
1398 // mode: c++
1399 // End: