Debian Codegraph @

src/ApvlvView.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 ApvlvView.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <QApplication>
29 #include <QFileDialog>
30 #include <QInputDialog>
31 #include <QLineEdit>
32 #include <filesystem>
33 #include <sstream>
34
35 #include "ApvlvDired.h"
36 #include "ApvlvInfo.h"
37 #include "ApvlvParams.h"
38 #include "ApvlvSearchDialog.h"
39 #include "ApvlvView.h"
40
41 namespace apvlv
42 {
43 using namespace std;
44 using namespace CommandModeType;
45
46 static bool
47 isAltEscape (QKeyEvent *event)
48 {
49 if (event->key () == Qt::Key_BracketLeft &&
50 /* ...so we can ignore mod keys like numlock and capslock. */
51 (event->modifiers () & Qt::ControlModifier))
52 {
53 return true;
54 }
55
56 return false;
57 }
58
59 void
60 ApvlvCommandBar::keyPressEvent (QKeyEvent *evt)
61 {
62 if (evt->key () == Qt::Key_Escape || isAltEscape (evt)
63 || evt->key () == Qt::Key_Tab || evt->key () == Qt::Key_Up
64 || evt->key () == Qt::Key_Down)
65 {
66 emit keyPressed (evt);
67 return;
68 }
69
70 QLineEdit::keyPressEvent (evt);
71 }
72
73 bool
74 ApvlvCommandBar::eventFilter (QObject *obj, QEvent *event)
75 {
76 if (event->type () == QEvent::KeyPress
77 && dynamic_cast<QKeyEvent *> (event)->key () == Qt::Key_Tab)
78 {
79 qDebug () << "Ate key press tab";
80 emit keyPressed (dynamic_cast<QKeyEvent *> (event));
81 return true;
82 }
83 else
84 {
85 return QObject::eventFilter (obj, event);
86 }
87 }
88
89 ApvlvView::ApvlvView (ApvlvView *parent) : mCmds (this), mSearchDialog (this)
90 {
91 mParent = parent;
92 if (mParent)
93 {
94 mParent->appendChild (this);
95 }
96
97 auto guiopt
98 = ApvlvParams::instance ()->getStringOrDefault ("guioptions", "mTS");
99
100 mCmdType = CmdStatusType::CMD_NONE;
101
102 mProCmd = 0;
103 mProCmdCnt = 0;
104
105 mCurrHistroy = -1;
106
107 mHasFull = false;
108
109 keyLastEnd = true;
110
111 processInLast = false;
112
113 int w = ApvlvParams::instance ()->getIntOrDefault ("width");
114 int h = ApvlvParams::instance ()->getIntOrDefault ("height");
115
116 if (ApvlvParams::instance ()->getBoolOrDefault ("fullscreen"))
117 {
118 fullScreen ();
119 }
120 else
121 {
122 resize (w > 1 ? w : 800, h > 1 ? h : 600);
123 }
124 show ();
125
126 setCentralWidget (&mCentral);
127
128 setupMenuBar (guiopt);
129 setMenuBar (&mMenuBar);
130 setupToolBar ();
131 addToolBar (Qt::TopToolBarArea, &mToolBar);
132
133 bool isMenuBarShow = guiopt.find ('m') != string::npos;
134 if (!isMenuBarShow)
135 {
136 mMenuBar.hide ();
137 }
138 bool isToolBarShow = guiopt.find ('T') != string::npos;
139 if (!isToolBarShow)
140 {
141 mToolBar.hide ();
142 }
143
144 mCentral.setLayout (&mVBoxLayout);
145
146 mTabContainer.setTabBarAutoHide (true);
147 mVBoxLayout.addWidget (&mTabContainer, 1);
148
149 QObject::connect (&mTabContainer, SIGNAL (currentChanged (int)), this,
150 SLOT (tabSwitched (int)));
151
152 mVBoxLayout.addWidget (&mCommandBar, 0);
153 QObject::connect (&mCommandBar, SIGNAL (textEdited (const QString &)), this,
154 SLOT (commandbarEdited (const QString &)));
155 QObject::connect (&mCommandBar, SIGNAL (returnPressed ()), this,
156 SLOT (commandbarReturn ()));
157 QObject::connect (&mCommandBar, SIGNAL (keyPressed (QKeyEvent *)), this,
158 SLOT (commandbarKeyPressed (QKeyEvent *)));
159
160 QObject::connect (&mSearchDialog,
161 SIGNAL (loadFile (const std::string &, int)), this,
162 SLOT (loadFileOnPage (const std::string &, int)));
163
164 cmdHide ();
165 }
166
167 ApvlvView::~ApvlvView ()
168 {
169 if (mParent)
170 {
171 mParent->eraseChild (this);
172 }
173
174 auto itr = mChildren.begin ();
175 while (itr != mChildren.end ())
176 {
177 delete *itr;
178 itr = mChildren.begin ();
179 }
180
181 mCmdHistroy.clear ();
182 }
183
184 ApvlvWindow *
185 ApvlvView::currentWindow ()
186 {
187 auto index = mTabContainer.currentIndex ();
188 if (index < 0)
189 return nullptr;
190
191 auto root_win = dynamic_cast<ApvlvWindow *> (mTabContainer.widget (index));
192 Q_ASSERT (root_win);
193
194 auto widget = QApplication::focusWidget ();
195 if (widget)
196 {
197 auto win = root_win->findWindowByWidget (widget);
198 if (win)
199 {
200 root_win->setActiveWindow (win);
201 return win;
202 }
203 }
204
205 auto win = root_win->getActiveWindow ();
206 if (win)
207 return win;
208
209 return root_win->firstFrameWindow ();
210 }
211
212 void
213 ApvlvView::delCurrentWindow ()
214 {
215 if (auto win = currentWindow (); win)
216 win->perish ();
217 updateTabName ();
218 }
219
220 void
221 ApvlvView::open ()
222 {
223 QString dirname;
224 auto fp = ApvlvInfo::instance ()->lastFile ();
225 if (fp)
226 {
227 dirname = QString::fromLocal8Bit (
228 filesystem::path (fp.value ()->file).parent_path ().string ());
229 }
230 else
231 {
232 dirname = QDir::homePath ();
233 }
234
235 // qDebug ("lastfile: [%s], dirname: [%s]", fp ? fp->file.c_str () : "",
236 // dirname);
237 auto const mimes = FileFactory::supportMimeTypes ();
238 QString filters;
239 for (const auto &m : mimes)
240 {
241 filters += m.first.c_str ();
242 filters += "(";
243 filters += ('*' + m.second[0]);
244 for (decltype (m.second.size ()) index = 1; index < m.second.size ();
245 ++index)
246 filters += (" *" + m.second[index]);
247 filters += ");;";
248 }
249
250 QString selected;
251 auto filename = QFileDialog::getOpenFileName (this, "open file", dirname,
252 filters, &selected);
253 if (!filename.isEmpty ())
254 {
255 loadFile (string (filename.toLocal8Bit ().constData ()));
256 }
257 }
258
259 void
260 ApvlvView::openDir ()
261 {
262 QString dirname;
263 auto fp = ApvlvInfo::instance ()->lastFile ();
264 if (fp)
265 {
266 dirname = QString::fromLocal8Bit (
267 filesystem::path (fp.value ()->file).parent_path ().string ());
268 }
269 else
270 {
271 dirname = QDir::homePath ();
272 }
273
274 auto filename = QFileDialog::getExistingDirectory (
275 this, "open dir", dirname, QFileDialog::ShowDirsOnly);
276 if (!filename.isEmpty ())
277 {
278 loadDir (filename.toStdString ());
279 }
280 }
281
282 void
283 ApvlvView::openRrl ()
284 {
285 auto res = QInputDialog::getText (this, "url", tr ("input url: "));
286 if (!res.isEmpty ())
287 {
288 currentFrame ()->loadUri (res.toStdString ());
289 }
290 }
291
292 bool
293 ApvlvView::loadDir (const std::string &path)
294 {
295 currentFrame ()->setDirIndex (path);
296 return true;
297 }
298
299 void
300 ApvlvView::quit (bool only_tab)
301 {
302 if (mTabContainer.count () <= 1 || only_tab == false)
303 {
304 closeEvent (nullptr);
305 return;
306 }
307
308 auto index = mTabContainer.currentIndex ();
309 Q_ASSERT (index != -1);
310 mTabContainer.removeTab (index);
311 }
312
313 void
314 ApvlvView::search ()
315 {
316 promptCommand ('/');
317 }
318
319 void
320 ApvlvView::backSearch ()
321 {
322 promptCommand ('?');
323 }
324
325 void
326 ApvlvView::advancedSearch ()
327 {
328 mSearchDialog.show ();
329 }
330
331 void
332 ApvlvView::dired ()
333 {
334 auto diag = DiredDialog (this);
335 QObject::connect (&diag, SIGNAL (loadFile (const string &, int)), this,
336 SLOT (loadFileOnPage (const string &, int)));
337 diag.exec ();
338 }
339
340 bool
341 ApvlvView::newTab (const std::string &filename)
342 {
343 auto docname = filename;
344 if (filesystem::is_directory (filename))
345 {
346 docname = HelpPdf;
347 }
348
349 auto optndoc = hasLoaded (docname);
350 if (!optndoc)
351 {
352 auto ndoc = new ApvlvFrame (this);
353 if (!ndoc->loadFile (docname, true, true))
354 {
355 delete ndoc;
356 ndoc = nullptr;
357 }
358
359 if (ndoc)
360 {
361 regLoaded (ndoc);
362 optndoc = make_optional<ApvlvFrame *> (ndoc);
363 }
364 }
365
366 if (optndoc)
367 {
368 newTab (optndoc.value ());
369 if (filesystem::is_directory (filename))
370 {
371 loadDir (filename);
372 }
373 return true;
374 }
375 else
376 {
377 return false;
378 }
379 }
380
381 bool
382 ApvlvView::newTab (ApvlvFrame *core)
383 {
384 auto win = new ApvlvWindow ();
385 win->setFrame (core);
386
387 auto basename
388 = core->filename ()
389 ? filesystem::path (core->filename ()).filename ().string ()
390 : "NONE";
391 auto pos = mTabContainer.currentIndex () + 1;
392 mTabContainer.insertTab (pos, win, QString::fromLocal8Bit (basename));
393 mTabContainer.setCurrentIndex (pos);
394
395 return true;
396 }
397
398 bool
399 ApvlvView::loadFile (const std::string &filename)
400 {
401 auto abpath = filesystem::absolute (filename).string ();
402
403 ApvlvWindow *win = currentWindow ();
404 ApvlvFrame *ndoc = nullptr;
405
406 auto optndoc = hasLoaded (abpath);
407 if (!optndoc)
408 {
409 ndoc = new ApvlvFrame (this);
410 if (!ndoc->loadFile (filename, true, true))
411 {
412 delete ndoc;
413 ndoc = nullptr;
414 }
415 else
416 {
417 regLoaded (ndoc);
418 optndoc = make_optional<ApvlvFrame *> (ndoc);
419 }
420 }
421
422 if (optndoc)
423 {
424 win->setFrame (optndoc.value ());
425 updateTabName ();
426 }
427
428 return ndoc != nullptr;
429 }
430
431 void
432 ApvlvView::loadFileOnPage (const string &filename, int pn)
433 {
434 auto cdoc = currentFrame ();
435 if (cdoc)
436 {
437 if (loadFile (filename))
438 {
439 cdoc->showPage (pn, 0.0);
440 }
441 }
442 }
443
444 optional<ApvlvFrame *>
445 ApvlvView::hasLoaded (string_view abpath)
446 {
447 for (auto &core : mDocs)
448 {
449 if (!core->inuse () && abpath == core->filename ())
450 {
451 return make_optional<ApvlvFrame *> (core.get ());
452 }
453 }
454
455 return nullopt;
456 }
457
458 void
459 ApvlvView::regLoaded (ApvlvFrame *doc)
460 {
461 auto cache_count
462 = ApvlvParams::instance ()->getIntOrDefault ("cache_count", 10);
463 if (mDocs.size () >= static_cast<size_t> (cache_count))
464 {
465 auto found_itr = mDocs.end ();
466 for (auto itr = mDocs.begin (); itr != mDocs.end (); ++itr)
467 {
468 if ((*itr)->inuse () == false)
469 {
470 found_itr = itr;
471 break;
472 }
473 }
474
475 if (found_itr != mDocs.end ())
476 {
477 mDocs.erase (found_itr);
478 }
479 }
480
481 mDocs.push_back (unique_ptr<ApvlvFrame> (doc));
482 }
483
484 void
485 ApvlvView::setupMenuBar (const string &guiopt)
486 {
487 // File -> open, openDir
488 auto mfile = new QMenu (tr ("File"));
489 mMenuBar.addMenu (mfile);
490
491 auto action = mfile->addAction (tr ("Open"));
492 QObject::connect (action, SIGNAL (triggered (bool)), this, SLOT (open ()));
493 action = mfile->addAction (tr ("OpenDir"));
494 QObject::connect (action, SIGNAL (triggered (bool)), this,
495 SLOT (openDir ()));
496 action = mfile->addAction (tr ("OpenUrl"));
497 QObject::connect (action, SIGNAL (triggered (bool)), this,
498 SLOT (openRrl ()));
499 action = mfile->addAction (tr ("New Tab"));
500 QObject::connect (action, SIGNAL (triggered (bool)), this, SLOT (newTab ()));
501 action = mfile->addAction (tr ("Close Tab"));
502 QObject::connect (action, SIGNAL (triggered (bool)), this,
503 SLOT (closeTab ()));
504 action = mfile->addAction (tr ("Quit"));
505 QObject::connect (action, SIGNAL (triggered (bool)), this,
506 SLOT (quit (bool)));
507
508 // Edit ->
509 auto medit = new QMenu (tr ("Edit"));
510 mMenuBar.addMenu (medit);
511 action = medit->addAction (tr ("Search"));
512 QObject::connect (action, SIGNAL (triggered (bool)), this, SLOT (search ()));
513 action = medit->addAction (tr ("Back Search"));
514 QObject::connect (action, SIGNAL (triggered (bool)), this,
515 SLOT (backSearch ()));
516
517 // View ->
518 auto mview = new QMenu (tr ("View"));
519 mMenuBar.addMenu (mview);
520
521 action = mview->addAction (tr ("ToolBar"));
522 action->setCheckable (true);
523 QObject::connect (action, SIGNAL (triggered (bool)), this,
524 SLOT (toggleToolBar ()));
525 bool isToolBarShow = guiopt.find ('T') != string::npos;
526 action->setChecked (isToolBarShow);
527
528 action = mview->addAction (tr ("StatusBar"));
529 action->setCheckable (true);
530 QObject::connect (action, SIGNAL (triggered (bool)), this,
531 SLOT (toggleStatus ()));
532 bool isStatusShow = guiopt.find ('S') != string::npos;
533 action->setChecked (isStatusShow);
534
535 action = mview->addAction (tr ("Horizontal Split"));
536 QObject::connect (action, SIGNAL (triggered (bool)), this,
537 SLOT (horizontalSplit ()));
538 action = mview->addAction (tr ("Vertical Split"));
539 QObject::connect (action, SIGNAL (triggered (bool)), this,
540 SLOT (verticalSplit ()));
541 action = mview->addAction (tr ("Close Split"));
542 QObject::connect (action, SIGNAL (triggered (bool)), this,
543 SLOT (unBirth ()));
544
545 // Navigate ->
546 auto mnavigate = new QMenu (tr ("Navigate"));
547 mMenuBar.addMenu (mnavigate);
548 action = mnavigate->addAction (tr ("Previous Page"));
549 QObject::connect (action, SIGNAL (triggered (bool)), this,
550 SLOT (previousPage ()));
551 action = mnavigate->addAction (tr ("Next Page"));
552 QObject::connect (action, SIGNAL (triggered (bool)), this,
553 SLOT (nextPage ()));
554
555 // Tools
556 auto mtools = new QMenu (tr ("Tools"));
557 mMenuBar.addMenu (mtools);
558 action = mtools->addAction (tr ("Advanced Search"));
559 QObject::connect (action, SIGNAL (triggered (bool)), this,
560 SLOT (advancedSearch ()));
561 action = mtools->addAction (tr ("Dired"));
562 QObject::connect (action, SIGNAL (trigged (bool)), this, SLOT (dired ()));
563
564 // Help -> about
565 auto mhelp = new QMenu (tr ("Help"));
566 mMenuBar.addMenu (mhelp);
567 }
568
569 void
570 ApvlvView::setupToolBar ()
571 {
572 auto action = mToolBar.addAction (tr ("Open"));
573 action->setIcon (
574 QApplication::style ()->standardIcon (QStyle::SP_DialogOpenButton));
575 QObject::connect (action, SIGNAL (triggered (bool)), this, SLOT (open ()));
576
577 action = mToolBar.addAction (tr ("OpenDir"));
578 action->setIcon (
579 QApplication::style ()->standardIcon (QStyle::SP_DirOpenIcon));
580 QObject::connect (action, SIGNAL (triggered (bool)), this,
581 SLOT (openDir ()));
582
583 action = mToolBar.addAction (tr ("Toggle Directory"));
584 QObject::connect (action, SIGNAL (triggered (bool)), this,
585 SLOT (toggleDirectory ()));
586
587 action = mToolBar.addAction (tr ("Quit"));
588 QObject::connect (action, SIGNAL (triggered (bool)), this,
589 SLOT (quit (bool)));
590 }
591
592 void
593 ApvlvView::promptCommand (char ch)
594 {
595 QString s{ ch };
596 mCommandBar.setText (s);
597 cmdShow (CmdStatusType::CMD_CMD);
598 }
599
600 void
601 ApvlvView::promptCommand (const char *str)
602 {
603 mCommandBar.setText (str);
604 cmdShow (CmdStatusType::CMD_CMD);
605 }
606
607 char *
608 ApvlvView::input (const char *str, int width, int height,
609 const string &content)
610 {
611 // need impl
612 return nullptr;
613 }
614
615 void
616 ApvlvView::cmdShow (CmdStatusType cmdtype)
617 {
618 mCmdType = cmdtype;
619
620 mCommandBar.show ();
621 mCommandBar.setCursorPosition (1);
622 mCommandBar.setFocus ();
623 }
624
625 void
626 ApvlvView::cmdHide ()
627 {
628 mCmdType = CmdStatusType::CMD_NONE;
629 mCmdTime = chrono::steady_clock::now ();
630
631 mCommandBar.hide ();
632 mTabContainer.setFocus ();
633 }
634
635 void
636 ApvlvView::cmdAuto (const char *ps)
637 {
638 stringstream ss (ps);
639 string cmd;
640 string np;
641 string argu;
642
643 ss >> cmd >> np >> argu;
644
645 if (np.empty ())
646 {
647 return;
648 }
649
650 if (np[np.length () - 1] == '\\')
651 {
652 np.replace (np.length () - 1, 1, 1, ' ');
653 np += argu;
654 ss >> argu;
655 }
656
657 if (cmd.empty () || np.empty ())
658 {
659 return;
660 }
661
662 auto comp = ApvlvCompletion{};
663 if (cmd == "o" || cmd == "open" || cmd == "TOtext")
664 {
665 comp.addPath (np);
666 }
667 else if (cmd == "doc")
668 {
669 vector<string> items;
670 for (auto &doc : mDocs)
671 {
672 items.emplace_back (doc->filename ());
673 }
674 comp.addItems (items);
675 }
676
677 qDebug () << "find match: " << np;
678 auto comtext = comp.complete (np);
679 if (!comtext.empty ())
680 {
681 qDebug () << "get a match: " << comtext;
682 QString s = QString::fromLocal8Bit (comtext);
683 s.replace (" ", "\\ ");
684 QString linetext = QString::asprintf (":%s %s", cmd.c_str (),
685 s.toStdString ().c_str ());
686 mCommandBar.setText (linetext);
687 }
688 else
689 {
690 qDebug () << "no get match";
691 }
692 }
693
694 void
695 ApvlvView::fullScreen ()
696 {
697 if (mHasFull == false)
698 {
699 ApvlvFrame *core = currentFrame ();
700 if (core)
701 core->toggleDirectory (false);
702
703 showFullScreen ();
704 mHasFull = true;
705 }
706 else
707 {
708 showNormal ();
709 mHasFull = false;
710 }
711 }
712
713 void
714 ApvlvView::nextPage ()
715 {
716 currentFrame ()->nextPage (1);
717 }
718
719 void
720 ApvlvView::previousPage ()
721 {
722 currentFrame ()->previousPage (1);
723 }
724
725 void
726 ApvlvView::toggleDirectory ()
727 {
728 currentFrame ()->toggleDirectory ();
729 }
730
731 void
732 ApvlvView::toggleToolBar ()
733 {
734 if (mToolBar.isHidden ())
735 {
736 mToolBar.show ();
737 }
738 else
739 {
740 mToolBar.hide ();
741 }
742 }
743
744 void
745 ApvlvView::toggleStatus ()
746 {
747 for (auto &doc : mDocs)
748 {
749 if (doc->isStatusHidden ())
750 {
751 doc->statusShow ();
752 }
753 else
754 {
755 doc->statusHide ();
756 }
757 }
758 }
759
760 void
761 ApvlvView::newTab ()
762 {
763 auto doc = currentFrame ()->clone ();
764 newTab (doc);
765 }
766
767 void
768 ApvlvView::closeTab ()
769 {
770 quit (true);
771 }
772
773 void
774 ApvlvView::horizontalSplit ()
775 {
776 currentWindow ()->birth (ApvlvWindow::WindowType::SP, nullptr);
777 }
778
779 void
780 ApvlvView::verticalSplit ()
781 {
782 currentWindow ()->birth (ApvlvWindow::WindowType::VSP, nullptr);
783 }
784
785 void
786 ApvlvView::unBirth ()
787 {
788 currentWindow ()->perish ();
789 }
790
791 ApvlvFrame *
792 ApvlvView::currentFrame ()
793 {
794 if (auto widget = QApplication::focusWidget (); widget)
795 {
796 if (auto doc = ApvlvFrame::findByWidget (widget); doc)
797 return doc;
798 }
799
800 ApvlvWindow *curwin = currentWindow ();
801 Q_ASSERT (curwin);
802 return curwin->getFrame ();
803 }
804
805 CmdReturn
806 ApvlvView::subProcess (int times, uint keyval)
807 {
808 uint procmd = mProCmd;
809 mProCmd = 0;
810 mProCmdCnt = 0;
811 switch (procmd)
812 {
813 case 'Z':
814 if (keyval == 'Z')
815 quit (true);
816
817 case ctrlValue ('w'):
818 if (keyval == 'q' || keyval == ctrlValue ('Q'))
819 {
820 if (currentWindow ()->isRoot ())
821 {
822 quit (true);
823 }
824 else
825 {
826 delCurrentWindow ();
827 }
828 }
829 else
830 {
831 CmdReturn rv = currentWindow ()->process (times, keyval);
832 updateTabName ();
833 return rv;
834 }
835 break;
836
837 case 'g':
838 if (keyval == 't')
839 {
840 if (times == 0)
841 switchTab (mTabContainer.currentIndex () + 1);
842 else
843 switchTab (times - 1);
844 }
845 else if (keyval == 'T')
846 {
847 if (times == 0)
848 switchTab (mTabContainer.currentIndex () - 1);
849 else
850 switchTab (times - 1);
851 }
852 else if (keyval == 'g')
853 {
854 if (times == 0)
855 times = 1;
856 currentFrame ()->showPage (times - 1, 0.0);
857 }
858 break;
859
860 default:
861 return CmdReturn::NO_MATCH;
862 }
863
864 return CmdReturn::MATCH;
865 }
866
867 CmdReturn
868 ApvlvView::process (int has, int ct, uint key)
869 {
870 if (mProCmd != 0)
871 {
872 auto ret = subProcess (mProCmdCnt, key);
873 if (ret == CmdReturn::MATCH)
874 {
875 saveKey (has, ct, key, true);
876 }
877 return ret;
878 }
879
880 switch (key)
881 {
882 case 'Z':
883 mProCmd = 'Z';
884 return CmdReturn::NEED_MORE;
885
886 case ctrlValue ('w'):
887 mProCmd = ctrlValue ('w');
888 mProCmdCnt = has ? ct : 1;
889 saveKey (has, ct, key, false);
890 return CmdReturn::NEED_MORE;
891 case 'q':
892 quit (true);
893 break;
894 case 'f':
895 fullScreen ();
896 break;
897 case '.':
898 processLastKey ();
899 break;
900 case 'g':
901 mProCmd = 'g';
902 mProCmdCnt = has ? ct : 0;
903 saveKey (has, ct, key, false);
904 return CmdReturn::NEED_MORE;
905 default:
906 CmdReturn ret = currentFrame ()->process (has, ct, key);
907 if (ret == CmdReturn::NEED_MORE)
908 {
909 saveKey (has, ct, key, false);
910 }
911 else if (ret == CmdReturn::MATCH)
912 {
913 saveKey (has, ct, key, true);
914 }
915 }
916
917 return CmdReturn::MATCH;
918 }
919
920 bool
921 ApvlvView::run (const char *str)
922 {
923 bool ret;
924
925 switch (*str)
926 {
927 case SEARCH:
928 currentFrame ()->markposition ('\'');
929 ret = currentFrame ()->search (str + 1, false);
930 break;
931
932 case BACKSEARCH:
933 currentFrame ()->markposition ('\'');
934 ret = currentFrame ()->search (str + 1, true);
935 break;
936
937 case COMMANDMODE:
938 ret = runCommand (str + 1);
939 break;
940
941 case FIND:
942 ret = currentFrame ()->search (str + 1, false);
943 break;
944
945 default:
946 ret = false;
947 break;
948 }
949
950 return ret;
951 }
952
953 void
954 ApvlvView::setTitle (const std::string &title)
955 {
956 setWindowTitle (QString::fromLocal8Bit (title));
957 }
958
959 bool
960 ApvlvView::runCommand (const char *str)
961 {
962 bool ret = true;
963
964 if (*str == '!')
965 {
966 system (str + 1);
967 }
968 else
969 {
970 stringstream ss (str);
971 string cmd;
972 string subcmd;
973 string argu;
974 ss >> cmd >> subcmd >> argu;
975
976 if (!subcmd.empty () && subcmd[subcmd.length () - 1] == '\\')
977 {
978 subcmd.replace (subcmd.length () - 1, 1, 1, ' ');
979 subcmd += argu;
980 ss >> argu;
981 }
982
983 if (cmd == "set")
984 {
985 if (subcmd == "skip")
986 {
987 currentFrame ()->setSkip (
988 int (strtol (argu.c_str (), nullptr, 10)));
989 }
990 else
991 {
992 ApvlvParams::instance ()->push (subcmd, argu);
993 }
994 }
995 else if (cmd == "map" && !subcmd.empty ())
996 {
997 ApvlvCmds::buildCommandMap (subcmd, argu);
998 }
999 else if ((cmd == "o" || cmd == "open" || cmd == "doc")
1000 && !subcmd.empty ())
1001 {
1002 char *home;
1003
1004 if (subcmd[0] == '~')
1005 {
1006 home = getenv ("HOME");
1007
1008 if (home)
1009 {
1010 subcmd.replace (0, 1, home);
1011 }
1012 }
1013
1014 if (filesystem::is_directory (subcmd))
1015 {
1016 ret = loadDir (subcmd);
1017 }
1018 else if (filesystem::is_regular_file (subcmd))
1019 {
1020 ret = loadFile (subcmd);
1021 }
1022 else
1023 {
1024 errorMessage (string ("no file: "), subcmd);
1025 ret = false;
1026 }
1027 }
1028 else if (cmd == "TOtext" && !subcmd.empty ())
1029 {
1030 currentFrame ()->totext (subcmd.c_str ());
1031 }
1032 else if ((cmd == "pr" || cmd == "print"))
1033 {
1034 currentFrame ()->print (
1035 subcmd.empty () ? -1
1036 : int (strtol (subcmd.c_str (), nullptr, 10)));
1037 }
1038 else if (cmd == "sp")
1039 {
1040 if (currentWindow ()->birth (ApvlvWindow::WindowType::SP, nullptr))
1041 {
1042 windowAdded ();
1043 }
1044 }
1045 else if (cmd == "vsp")
1046 {
1047 if (currentWindow ()->birth (ApvlvWindow::WindowType::VSP, nullptr))
1048 {
1049 windowAdded ();
1050 }
1051 }
1052 else if ((cmd == "zoom" || cmd == "z") && !subcmd.empty ())
1053 {
1054 currentFrame ()->setZoomString (subcmd.c_str ());
1055 }
1056 else if (cmd == "forwardpage" || cmd == "fp")
1057 {
1058 if (subcmd.empty ())
1059 currentFrame ()->nextPage (1);
1060 else
1061 currentFrame ()->nextPage (
1062 int (strtol (subcmd.c_str (), nullptr, 10)));
1063 }
1064 else if (cmd == "prewardpage" || cmd == "bp")
1065 {
1066 if (subcmd.empty ())
1067 currentFrame ()->previousPage (1);
1068 else
1069 currentFrame ()->previousPage (
1070 int (strtol (subcmd.c_str (), nullptr, 10)));
1071 }
1072 else if (cmd == "directory")
1073 {
1074 currentFrame ()->toggleDirectory ();
1075 }
1076 else if (cmd == "goto" || cmd == "g")
1077 {
1078 currentFrame ()->markposition ('\'');
1079 auto p = strtol (subcmd.c_str (), nullptr, 10);
1080 p += currentFrame ()->getSkip ();
1081 currentFrame ()->showPage (int (p - 1), 0.0);
1082 }
1083 else if (cmd == "help" || cmd == "h")
1084 {
1085 loadFile (HelpPdf);
1086 }
1087 else if (cmd == "q" || cmd == "quit")
1088 {
1089 if (currentWindow ()->isRoot ())
1090 {
1091 quit (true);
1092 }
1093 else
1094 {
1095 delCurrentWindow ();
1096 }
1097 }
1098 else if (cmd == "qall")
1099 {
1100 quit (false);
1101 }
1102 else if (cmd == "tabnew")
1103 {
1104 newTab (HelpPdf);
1105 }
1106 else if (cmd == "tabn" || cmd == "tabnext")
1107 {
1108 switchTab (mTabContainer.currentIndex () + 1);
1109 }
1110 else if (cmd == "tabp" || cmd == "tabprevious")
1111 {
1112 switchTab (mTabContainer.currentIndex () - 1);
1113 }
1114 else if (cmd == "toc")
1115 {
1116 loadFile (currentFrame ()->filename ());
1117 }
1118 else
1119 {
1120 bool isn = true;
1121 for (char i : cmd)
1122 {
1123 if (i < '0' || i > '9')
1124 {
1125 isn = false;
1126 break;
1127 }
1128 }
1129 if (isn && currentFrame ())
1130 {
1131 auto p = strtol (cmd.c_str (), nullptr, 10);
1132 p += currentFrame ()->getSkip ();
1133 if (p != currentFrame ()->pageNumber ())
1134 {
1135 currentFrame ()->showPage (int (p - 1), 0.0);
1136 }
1137 }
1138 else
1139 {
1140 errorMessage (string ("no command: "), cmd);
1141 ret = false;
1142 }
1143 }
1144 }
1145
1146 return ret;
1147 }
1148
1149 void
1150 ApvlvView::keyPressEvent (QKeyEvent *evt)
1151 {
1152 if (mCmdType != CmdStatusType::CMD_NONE)
1153 {
1154 return;
1155 }
1156
1157 auto now = chrono::steady_clock::now ();
1158 auto millis
1159 = chrono::duration_cast<chrono::milliseconds> (now - mCmdTime).count ();
1160 if (millis < 1000L)
1161 return;
1162
1163 mCmds.append (evt);
1164 }
1165
1166 void
1167 ApvlvView::commandbarEdited ([[maybe_unused]] const QString &str)
1168 {
1169 // need impl
1170 }
1171
1172 void
1173 ApvlvView::commandbarReturn ()
1174 {
1175 if (mCmdType == CmdStatusType::CMD_CMD)
1176 {
1177 auto str = mCommandBar.text ();
1178 if (!str.isEmpty ())
1179 {
1180 if (run (str.toStdString ().c_str ()))
1181 {
1182 mCmdHistroy.emplace_back (str.toStdString ());
1183 mCurrHistroy = mCmdHistroy.size () - 1;
1184 cmdHide ();
1185 }
1186 }
1187 else
1188 {
1189 cmdHide ();
1190 }
1191 }
1192 else if (mCmdType == CmdStatusType::CMD_MESSAGE)
1193 {
1194 cmdHide ();
1195 }
1196 }
1197
1198 void
1199 ApvlvView::commandbarKeyPressed (QKeyEvent *gek)
1200 {
1201 if (gek->key () == Qt::Key_Tab)
1202 {
1203 auto str = mCommandBar.text ();
1204 if (!str.isEmpty ())
1205 {
1206 cmdAuto (str.toStdString ().c_str () + 1);
1207 }
1208 }
1209 else if (gek->key () == Qt::Key_Backspace)
1210 {
1211 auto str = mCommandBar.text ();
1212 if (str.length () == 1)
1213 {
1214 cmdHide ();
1215 mCurrHistroy = mCmdHistroy.size () - 1;
1216 }
1217 }
1218 else if (gek->key () == Qt::Key_Escape || isAltEscape (gek))
1219 {
1220 cmdHide ();
1221 mCurrHistroy = mCmdHistroy.size () - 1;
1222 }
1223 else if (gek->key () == Qt::Key_Up)
1224 {
1225 if (!mCmdHistroy.empty ())
1226 {
1227 mCommandBar.setText (QString::fromLocal8Bit (
1228 mCurrHistroy > 0 ? mCmdHistroy[mCurrHistroy--]
1229 : mCmdHistroy[0]));
1230 }
1231 }
1232 else if (gek->key () == Qt::Key_Down)
1233 {
1234 if (!mCmdHistroy.empty ())
1235 {
1236 mCommandBar.setText (QString::fromLocal8Bit (
1237 (size_t)mCurrHistroy < mCmdHistroy.size () - 1
1238 ? mCmdHistroy[++mCurrHistroy]
1239 : mCmdHistroy[mCmdHistroy.size () - 1]));
1240 }
1241 }
1242 }
1243
1244 void
1245 ApvlvView::closeEvent (QCloseEvent *evt)
1246 {
1247 if (mParent == nullptr)
1248 {
1249 QGuiApplication::exit ();
1250 }
1251 }
1252
1253 void
1254 ApvlvView::tabSwitched (int pnum)
1255 {
1256 qDebug () << "tabwidget switch to: " << pnum;
1257 if (pnum == -1)
1258 return;
1259
1260 auto adoc = currentFrame ();
1261 if (adoc && adoc->filename ())
1262 {
1263 auto filename
1264 = filesystem::path (currentFrame ()->filename ()).filename ();
1265 setTitle (filename.string ());
1266 }
1267 }
1268
1269 void
1270 ApvlvView::switchTab (int tabPos)
1271 {
1272 int ntabs = mTabContainer.count ();
1273 while (tabPos < 0)
1274 tabPos += ntabs;
1275
1276 tabPos = tabPos % ntabs;
1277 mTabContainer.setCurrentIndex (tabPos);
1278 }
1279
1280 void
1281 ApvlvView::windowAdded ()
1282 {
1283 auto index = mTabContainer.currentIndex ();
1284 Q_ASSERT (index != -1);
1285 updateTabName ();
1286 }
1287
1288 void
1289 ApvlvView::updateTabName ()
1290 {
1291 auto win = currentWindow ();
1292 if (win == nullptr)
1293 return;
1294
1295 auto frame = win->getFrame ();
1296 if (frame == nullptr)
1297 return;
1298
1299 const char *filename = frame->filename ();
1300 string gfilename;
1301
1302 if (filename == nullptr)
1303 gfilename = "None";
1304 else
1305 gfilename = filesystem::path (filename).filename ().string ();
1306
1307 setTitle (gfilename);
1308
1309 auto index = mTabContainer.currentIndex ();
1310 Q_ASSERT (index != -1);
1311 auto tagname = QString::fromLocal8Bit (gfilename);
1312 // need impl tagname = QString("[%1] %2").arg (mTabList[index]).arg
1313 // (gfilename.c_str());
1314 mTabContainer.setTabText (index, tagname);
1315 }
1316
1317 void
1318 ApvlvView::appendChild (ApvlvView *view)
1319 {
1320 mChildren.push_back (view);
1321 }
1322
1323 void
1324 ApvlvView::eraseChild (ApvlvView *view)
1325 {
1326 auto itr = mChildren.begin ();
1327 while (*itr != view && itr != mChildren.end ())
1328 itr++;
1329
1330 if (*itr == view)
1331 {
1332 mChildren.erase (itr);
1333 }
1334 }
1335
1336 void
1337 ApvlvView::saveKey (int has, int ct, uint key, bool end)
1338 {
1339 if (processInLast)
1340 return;
1341
1342 if (keyLastEnd)
1343 {
1344 keySquence.clear ();
1345 }
1346
1347 struct keyNode key_node = { has, ct, key, end };
1348 keySquence.push_back (key_node);
1349 keyLastEnd = end;
1350 }
1351
1352 void
1353 ApvlvView::processLastKey ()
1354 {
1355 processInLast = true;
1356 for (auto node : keySquence)
1357 {
1358 process (node.Has, node.Ct, node.Key);
1359 }
1360 processInLast = false;
1361 }
1362 }
1363
1364 // Local Variables:
1365 // mode: c++
1366 // End: