1 /* Copyright (C) 2012 J.F.Dockes
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the
14  *   Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 #ifndef _SNIPPETS_W_H_INCLUDED_
18 #define _SNIPPETS_W_H_INCLUDED_
19 
20 #include "autoconfig.h"
21 
22 #include <memory>
23 
24 #include <QString>
25 
26 #include "rcldoc.h"
27 #include "docseq.h"
28 #include "rclmain_w.h"
29 
30 #include "ui_snippets.h"
31 
32 class SnippetsW : public QWidget, public Ui::Snippets
33 {
34     Q_OBJECT
35 public:
36     SnippetsW(Rcl::Doc doc, std::shared_ptr<DocSequence> source,
37               QWidget* parent = 0)
QWidget(parent)38         : QWidget(parent) {
39         setupUi((QDialog*)this);
40         init();
41         onSetDoc(doc, source);
42     }
43 
44     /** List shortcuts so that the prefs can be edited before any preview
45         is created */
46     static void listShortcuts();
47 
48 public slots:
49     virtual void onLinkClicked(const QUrl &);
50     virtual void onSetDoc(Rcl::Doc doc, std::shared_ptr<DocSequence> source);
51     virtual void createPopupMenu(const QPoint& pos);
52     virtual void onNewShortcuts();
53 
54 protected slots:
55     virtual void slotEditFind();
56     virtual void slotEditFindNext();
57     virtual void slotEditFindPrevious();
58     virtual void slotSearchTextChanged(const QString&);
59     virtual void reloadByRelevance();
60     virtual void reloadByPage();
61 
62 signals:
63     void startNativeViewer(Rcl::Doc, int pagenum, QString term);
64 
65 private:
66     void init();
67     std::shared_ptr<DocSequence> m_source;
68     Rcl::Doc m_doc;
69     bool m_sortingByPage;
70     QShortcut *m_find1sc{nullptr};
71     QShortcut *m_find2sc{nullptr};
72     QShortcut *m_findnextsc{nullptr};
73     QShortcut *m_findprevsc{nullptr};
74     QShortcut *m_hidesc{nullptr};
75 };
76 
77 #ifdef USING_WEBENGINE
78 #include <QWebEnginePage>
79 // Subclass the page to hijack the link clicks
80 class SnipWebPage: public QWebEnginePage {
81     Q_OBJECT
82 public:
SnipWebPage(SnippetsW * parent)83     SnipWebPage(SnippetsW *parent)
84     : QWebEnginePage((QWidget *)parent), m_parent(parent) {}
85 protected:
acceptNavigationRequest(const QUrl & url,NavigationType tp,bool)86     virtual bool acceptNavigationRequest(const QUrl& url,
87                                          NavigationType tp,
88                                          bool) {
89         if (tp == QWebEnginePage::NavigationTypeLinkClicked) {
90             m_parent->onLinkClicked(url);
91             return false;
92         } else {
93             return true;
94         }
95     }
96 private:
97     SnippetsW *m_parent;
98 };
99 #endif
100 
101 
102 #endif /* _SNIPPETS_W_H_INCLUDED_ */
103