"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/qweb/kit/c2bWebBrowser.cpp" (12 Feb 2021, 5801 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "c2bWebBrowser.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.0.0_vs_2.0.1.
1 /***************************************************************************
2 * Copyright (C) 2004-2021 by Pere Constans
3 * constans@molspaces.com
4 *
5 * See the LICENSE file that comes with this distribution.
6 ***************************************************************************/
7 #include "c2bWebBrowser.h"
8
9 #include "c2bUtils.h"
10
11 #include <QApplication>
12 #include <QClipboard>
13 #include <QContextMenuEvent>
14 #include <QShortcut>
15 #include <QTimer>
16
17 #include <QWebFrame>
18
19
20 c2bWebBrowser::c2bWebBrowser(QWidget* parentw) : QWebView(parentw)
21 {
22 // Avoid flickering (Qt 4.3)
23 setAttribute(Qt::WA_NoSystemBackground, true);
24 setAttribute(Qt::WA_OpaquePaintEvent, true);
25
26 // Set default background to application
27 page()->setPalette(qApp->palette());
28
29 // Actions
30 setContextMenuPolicy(Qt::ActionsContextMenu);
31 viewBackwardAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewBackward.png")), tr("Backward"), this);
32 viewBackwardAction->setShortcut(QKeySequence("Left"));
33 viewBackwardAction->setStatusTip(tr("Backward"));
34 addAction(viewBackwardAction);
35 viewForwardAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewForward.png")), tr("Forward"), this);
36 viewForwardAction->setShortcut(QKeySequence("Right"));
37 viewForwardAction->setStatusTip(tr("Forward"));
38 addAction(viewForwardAction);
39 c2bUtils::addSeparator(this);
40 viewHomeAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewHome.png")), tr("Home"), this);
41 viewHomeAction->setShortcut(QKeySequence("Esc"));
42 viewHomeAction->setStatusTip(tr("Home page"));
43 addAction(viewHomeAction);
44 c2bUtils::addSeparator(this);
45 viewReloadAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewReload.png")), tr("Reload"), this);
46 viewReloadAction->setShortcut(QKeySequence("R"));
47 viewReloadAction->setStatusTip(tr("Reload current page"));
48 addAction(viewReloadAction);
49 c2bUtils::addSeparator(this);
50 viewZoomInAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewZoomIn.png")), tr("Zoom In"), this);
51 viewZoomInAction->setShortcut(QKeySequence(QKeySequence::ZoomIn));
52 viewZoomInAction->setStatusTip(tr("Zoom in"));
53 addAction(viewZoomInAction);
54 viewZoomOutAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewZoomOut.png")), tr("Zoom Out"), this);
55 viewZoomOutAction->setShortcut(QKeySequence(QKeySequence::ZoomOut));
56 viewZoomOutAction->setStatusTip(tr("Zoom out"));
57 addAction(viewZoomOutAction);
58
59 connect(viewForwardAction, SIGNAL(triggered()), this, SLOT(forward()));
60 connect(viewBackwardAction, SIGNAL(triggered()), this, SLOT(back()));
61 connect(viewHomeAction, SIGNAL(triggered()), this, SLOT(home()));
62 connect(viewReloadAction, SIGNAL(triggered()), this, SLOT(reload()));
63 connect(viewZoomInAction, SIGNAL(triggered()), this, SLOT(zoomIn()));
64 connect(viewZoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
65
66 QShortcut* copy_shortcut = new QShortcut(QKeySequence(QKeySequence::Copy), this);
67 connect(copy_shortcut, SIGNAL(activated()), this, SLOT(copy()));
68 connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString)));
69 connect(page(), SIGNAL(statusBarMessage(QString)), this, SIGNAL(statusMessage(QString)));
70
71 connect(this, SIGNAL(titleChanged(QString)), this, SLOT(_set_window_title(QString)));
72 }
73
74 c2bWebBrowser::~c2bWebBrowser() {}
75
76
77 void c2bWebBrowser::setHomePage(const QString& hp_url)
78 {
79 _home_page = hp_url;
80 home();
81 }
82
83 void c2bWebBrowser::setFont(const QFont& qfont)
84 {
85 QWebSettings* wsettings = settings();
86 wsettings->setFontFamily(QWebSettings::StandardFont, qfont.family());
87 wsettings->setFontSize(QWebSettings::DefaultFontSize, qfont.pointSize());
88 }
89
90 void c2bWebBrowser::setFixedFont(const QFont& qfont)
91 {
92 QWebSettings* wsettings = settings();
93 wsettings->setFontFamily(QWebSettings::FixedFont, qfont.family());
94 wsettings->setFontSize(QWebSettings::DefaultFixedFontSize, qfont.pointSize());
95 }
96
97 void c2bWebBrowser::copy()
98 {
99 page()->triggerAction(QWebPage::Copy);
100 }
101
102 void c2bWebBrowser::home()
103 {
104 if (!_home_page.isEmpty())
105 load(c2bUtils::stringToUrl(_home_page));
106 }
107
108 void c2bWebBrowser::loadPage(const QString& p_url)
109 {
110 if (!p_url.isEmpty())
111 load(c2bUtils::stringToUrl(p_url));
112 }
113
114 void c2bWebBrowser::zoomIn()
115 {
116 setTextSizeMultiplier(textSizeMultiplier() + 0.1);
117 }
118
119 void c2bWebBrowser::zoomOut()
120 {
121 setTextSizeMultiplier(textSizeMultiplier() - 0.1);
122 }
123
124 QWebView* c2bWebBrowser::createWindow(QWebPage::WebWindowType /* type */)
125 {
126 if (!_hovered_link.isEmpty())
127 c2bUtils::openFile(_hovered_link, this);
128 return 0;
129 }
130
131 void c2bWebBrowser::mousePressEvent(QMouseEvent* qevent)
132 {
133 if (qevent->button() == Qt::BackButton)
134 {
135 back();
136 qevent->accept();
137 }
138 QWebView::mousePressEvent(qevent);
139 }
140
141 void c2bWebBrowser::_set_window_title(const QString& pagetitle)
142 {
143 // This fixes anchor clicked cases where titleChanged signal emits an empty document title
144 if (!pagetitle.isEmpty())
145 setWindowTitle(pagetitle);
146 }
147
148 void c2bWebBrowser::linkHovered(const QString& link)
149 {
150 _hovered_link = link;
151 if (_hovered_link.isEmpty())
152 emit statusMessage(QString());
153 else if (_hovered_link.contains(".tex.html#"))
154 {
155 const QString q(QFileInfo(link).fileName().split('#').last());
156 const QVariant v(
157 page()->mainFrame()->evaluateJavaScript("document.querySelector('a[id*=\"" + q + "\"]').text"));
158 if (!v.isValid())
159 emit statusMessage(QString());
160 else
161 emit statusMessage(v.toString());
162 }
163 else
164 {
165 QString vlink(link);
166 emit statusMessage(vlink.remove("file://"));
167 }
168 }