"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/qweb/engine/c2bWebBrowser.cpp" (12 Feb 2021, 5718 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 <QWebEngineSettings>
   18 
   19 
   20 c2bWebBrowser::c2bWebBrowser(QWidget* parentw) : QWebEngineView(parentw)
   21 {
   22 // Set default background to application
   23 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
   24     page()->setBackgroundColor(qApp->palette().color(QPalette::Window));
   25 #endif
   26 
   27     // Actions
   28     setContextMenuPolicy(Qt::ActionsContextMenu);
   29     viewBackwardAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewBackward.png")), tr("Backward"), this);
   30     viewBackwardAction->setShortcut(QKeySequence("Left"));
   31     viewBackwardAction->setStatusTip(tr("Backward"));
   32     addAction(viewBackwardAction);
   33     viewForwardAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewForward.png")), tr("Forward"), this);
   34     viewForwardAction->setShortcut(QKeySequence("Right"));
   35     viewForwardAction->setStatusTip(tr("Forward"));
   36     addAction(viewForwardAction);
   37     c2bUtils::addSeparator(this);
   38     viewHomeAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewHome.png")), tr("Home"), this);
   39     viewHomeAction->setShortcut(QKeySequence("Esc"));
   40     viewHomeAction->setStatusTip(tr("Home page"));
   41     addAction(viewHomeAction);
   42     c2bUtils::addSeparator(this);
   43     viewReloadAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewReload.png")), tr("Reload"), this);
   44     viewReloadAction->setShortcut(QKeySequence("R"));
   45     viewReloadAction->setStatusTip(tr("Reload current page"));
   46     addAction(viewReloadAction);
   47     c2bUtils::addSeparator(this);
   48     viewZoomInAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewZoomIn.png")), tr("Zoom In"), this);
   49     viewZoomInAction->setShortcut(QKeySequence(QKeySequence::ZoomIn));
   50     viewZoomInAction->setStatusTip(tr("Zoom in"));
   51     addAction(viewZoomInAction);
   52     viewZoomOutAction = new QAction(QIcon(QString::fromUtf8(":/icons/icons/viewZoomOut.png")), tr("Zoom Out"), this);
   53     viewZoomOutAction->setShortcut(QKeySequence(QKeySequence::ZoomOut));
   54     viewZoomOutAction->setStatusTip(tr("Zoom out"));
   55     addAction(viewZoomOutAction);
   56 
   57     connect(viewForwardAction, SIGNAL(triggered()), this, SLOT(forward()));
   58     connect(viewBackwardAction, SIGNAL(triggered()), this, SLOT(back()));
   59     connect(viewHomeAction, SIGNAL(triggered()), this, SLOT(home()));
   60     connect(viewReloadAction, SIGNAL(triggered()), this, SLOT(reload()));
   61     connect(viewZoomInAction, SIGNAL(triggered()), this, SLOT(zoomIn()));
   62     connect(viewZoomOutAction, SIGNAL(triggered()), this, SLOT(zoomOut()));
   63 
   64     QShortcut* copy_shortcut = new QShortcut(QKeySequence(QKeySequence::Copy), this);
   65     connect(copy_shortcut, SIGNAL(activated()), this, SLOT(copy()));
   66     connect(page(), SIGNAL(linkHovered(QString)), this, SLOT(linkHovered(QString)));
   67 
   68     connect(this, SIGNAL(titleChanged(QString)), this, SLOT(setWindowTitle(QString)));
   69 }
   70 
   71 c2bWebBrowser::~c2bWebBrowser() {}
   72 
   73 
   74 void c2bWebBrowser::setHomePage(const QString& hp_url)
   75 {
   76     _home_page = hp_url;
   77     home();
   78 }
   79 
   80 void c2bWebBrowser::setFont(const QFont& qfont)
   81 {
   82     QWebEngineSettings* wsettings = settings();
   83     wsettings->setFontFamily(QWebEngineSettings::StandardFont, qfont.family());
   84     wsettings->setFontSize(QWebEngineSettings::DefaultFontSize, qfont.pointSize());
   85 }
   86 
   87 void c2bWebBrowser::setFixedFont(const QFont& qfont)
   88 {
   89     QWebEngineSettings* wsettings = settings();
   90     wsettings->setFontFamily(QWebEngineSettings::FixedFont, qfont.family());
   91     wsettings->setFontSize(QWebEngineSettings::DefaultFixedFontSize, qfont.pointSize());
   92 }
   93 
   94 void c2bWebBrowser::copy()
   95 {
   96     page()->triggerAction(QWebEnginePage::Copy);
   97 }
   98 
   99 void c2bWebBrowser::home()
  100 {
  101     if (!_home_page.isEmpty())
  102         load(c2bUtils::stringToUrl(_home_page));
  103 }
  104 
  105 void c2bWebBrowser::loadPage(const QString& p_url)
  106 {
  107     if (!p_url.isEmpty())
  108         load(c2bUtils::stringToUrl(p_url));
  109 }
  110 
  111 void c2bWebBrowser::zoomIn()
  112 {
  113     setZoomFactor(zoomFactor() + 0.1);
  114 }
  115 
  116 void c2bWebBrowser::zoomOut()
  117 {
  118     setZoomFactor(zoomFactor() - 0.1);
  119 }
  120 
  121 QWebEngineView* c2bWebBrowser::createWindow(QWebEnginePage::WebWindowType /* type */)
  122 {
  123     if (!_hovered_link.isEmpty())
  124         c2bUtils::openFile(_hovered_link, this);
  125     return 0;
  126 }
  127 
  128 template <typename Arg, typename R, typename C>
  129 struct InvokeWrapper
  130 {
  131     R* receiver;
  132     void (C::*memberFun)(Arg);
  133     void operator()(Arg result)
  134     {
  135         (receiver->*memberFun)(result);
  136     }
  137 };
  138 
  139 template <typename Arg, typename R, typename C>
  140 InvokeWrapper<Arg, R, C> invoke(R* receiver, void (C::*memberFun)(Arg))
  141 {
  142     InvokeWrapper<Arg, R, C> wrapper = { receiver, memberFun };
  143     return wrapper;
  144 }
  145 
  146 void c2bWebBrowser::showTitle(const QVariant& v)
  147 {
  148     if (!v.isValid())
  149         emit statusMessage(QString());
  150     else
  151         emit statusMessage(v.toString());
  152 }
  153 
  154 void c2bWebBrowser::linkHovered(const QString& link)
  155 {
  156     _hovered_link = link;
  157     if (_hovered_link.isEmpty())
  158         emit statusMessage(QString());
  159     else if (_hovered_link.contains(".tex.html#"))
  160     {
  161         const QString q(QFileInfo(link).fileName().split('#').last());
  162         page()->runJavaScript("document.querySelector('a[id*=\"" + q + "\"]').text",
  163                               invoke(this, &c2bWebBrowser::showTitle));
  164     }
  165     else
  166     {
  167         QString vlink(link);
  168         emit statusMessage(vlink.remove("file://"));
  169     }
  170 }