File indexing completed on 2024-05-12 04:58:31

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef WEBVIEW_H
0019 #define WEBVIEW_H
0020 
0021 #include <QPointer>
0022 #include <QWebEngineView>
0023 
0024 #include "qzcommon.h"
0025 #include "loadrequest.h"
0026 #include "wheelhelper.h"
0027 
0028 class WebPage;
0029 class LoadRequest;
0030 class WebHitTestResult;
0031 
0032 class FALKON_EXPORT WebView : public QWebEngineView
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     explicit WebView(QWidget* parent = nullptr);
0038     ~WebView() override;
0039 
0040     QIcon icon(bool allowNull = false) const;
0041     QString title(bool allowEmpty = false) const;
0042 
0043     WebPage* page() const;
0044     void setPage(WebPage* page);
0045 
0046     void load(const QUrl &url);
0047     void load(const LoadRequest &request);
0048     bool isLoading() const;
0049 
0050     int loadingProgress() const;
0051 
0052     bool backgroundActivity() const;
0053 
0054     // Set zoom level (0 - 17)
0055     int zoomLevel() const;
0056     void setZoomLevel(int level);
0057 
0058     QPointF mapToViewport(const QPointF &pos) const;
0059     QRect scrollBarGeometry(Qt::Orientation orientation) const;
0060 
0061     void addNotification(QWidget* notif);
0062     bool eventFilter(QObject *obj, QEvent *event) override;
0063 
0064     QWidget *inputWidget() const;
0065     virtual QWidget *overlayWidget() = 0;
0066 
0067     static bool isUrlValid(const QUrl &url);
0068     static QList<int> zoomLevels();
0069 
0070     // Force context menu event to be sent on mouse release
0071     // This allows to override right mouse button events (eg. for mouse gestures)
0072     static bool forceContextMenuOnMouseRelease();
0073     static void setForceContextMenuOnMouseRelease(bool force);
0074 
0075 Q_SIGNALS:
0076     void pageChanged(WebPage *page);
0077     void focusChanged(bool);
0078     void viewportResized(QSize);
0079     void showNotification(QWidget*);
0080     void privacyChanged(bool);
0081     void zoomLevelChanged(int);
0082     void backgroundActivityChanged(bool);
0083 
0084 public Q_SLOTS:
0085     void zoomIn();
0086     void zoomOut();
0087     void zoomReset();
0088 
0089     void editUndo();
0090     void editRedo();
0091     void editCut();
0092     void editCopy();
0093     void editPaste();
0094     void editSelectAll();
0095     void editDelete();
0096 
0097     void reloadBypassCache();
0098 
0099     void back();
0100     void forward();
0101 
0102     void printPage();
0103     void showSource();
0104     void sendPageByMail();
0105 
0106     void openUrlInNewTab(const QUrl &url, Qz::NewTabPositionFlags position);
0107 
0108     virtual void closeView() = 0;
0109     virtual void loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position) = 0;
0110 
0111     virtual bool isFullScreen() = 0;
0112     virtual void requestFullScreen(bool enable) = 0;
0113 
0114 protected Q_SLOTS:
0115     void slotLoadStarted();
0116     void slotLoadProgress(int progress);
0117     void slotLoadFinished(bool ok);
0118     void slotIconChanged();
0119     void slotUrlChanged(const QUrl &url);
0120     void slotTitleChanged(const QString &title);
0121 
0122     // Context menu slots
0123     void openUrlInNewWindow();
0124     void sendTextByMail();
0125     void copyLinkToClipboard();
0126     void savePageAs();
0127     void copyImageToClipboard();
0128     void downloadLinkToDisk();
0129     void downloadImageToDisk();
0130     void downloadMediaToDisk();
0131     void openActionUrl();
0132     void showSiteInfo();
0133     void searchSelectedText();
0134     void searchSelectedTextInBackgroundTab();
0135     void bookmarkLink();
0136     void openUrlInSelectedTab();
0137     void openUrlInBackgroundTab();
0138 
0139     // To support user's option whether to open in selected or background tab
0140     void userDefinedOpenUrlInNewTab(const QUrl &url = QUrl(), bool invert = false);
0141     void userDefinedOpenUrlInBgTab(const QUrl &url = QUrl());
0142 
0143 protected:
0144     void showEvent(QShowEvent *event) override;
0145     void resizeEvent(QResizeEvent *event) override;
0146     void contextMenuEvent(QContextMenuEvent *event) override;
0147 
0148     bool focusNextPrevChild(bool next) override;
0149 
0150     virtual void _wheelEvent(QWheelEvent *event);
0151     virtual void _mousePressEvent(QMouseEvent *event);
0152     virtual void _mouseReleaseEvent(QMouseEvent *event);
0153     virtual void _mouseMoveEvent(QMouseEvent *event);
0154     virtual void _keyPressEvent(QKeyEvent *event);
0155     virtual void _keyReleaseEvent(QKeyEvent *event);
0156     virtual void _contextMenuEvent(QContextMenuEvent *event);
0157 
0158     void loadRequest(const LoadRequest &req);
0159     void applyZoom();
0160 
0161     void createContextMenu(QMenu *menu, WebHitTestResult &hitTest);
0162     void createPageContextMenu(QMenu *menu);
0163     void createLinkContextMenu(QMenu *menu, const WebHitTestResult &hitTest);
0164     void createImageContextMenu(QMenu *menu, const WebHitTestResult &hitTest);
0165     void createSelectedTextContextMenu(QMenu *menu, const WebHitTestResult &hitTest);
0166     void createMediaContextMenu(QMenu *menu, const WebHitTestResult &hitTest);
0167 
0168     void checkForForm(QAction *action, const QPoint &pos);
0169     void createSearchEngine();
0170 
0171 private Q_SLOTS:
0172     void addSpeedDial();
0173     void configureSpeedDial();
0174     void reloadAllSpeedDials();
0175 
0176     void toggleMediaPause();
0177     void toggleMediaMute();
0178 
0179 private:
0180     void initializeActions();
0181 
0182     int m_currentZoomLevel;
0183     int m_progress;
0184     bool m_backgroundActivity;
0185 
0186     QUrl m_clickedUrl;
0187     QPointF m_clickedPos;
0188 
0189     WebPage* m_page;
0190     bool m_firstLoad;
0191 
0192     QPointer<QWidget> m_rwhvqt;
0193     WheelHelper m_wheelHelper;
0194 
0195     static bool s_forceContextMenuOnMouseRelease;
0196 };
0197 
0198 #endif // WEBVIEW_H