/* * File: QCefView.h * Project: QCefView * Created: 29th March 2016 * Author: Sheen * Source: https://github.com/cefview/qcefview * Docs: https://cefview.github.io/QCefView/ */ #ifndef QCEFVIEW_H #define QCEFVIEW_H #pragma once #include #pragma region qt_headers #include #include #include #include #pragma endregion #include #include #include #include /// /// Type alias for CEF browser id /// #define QCefBrowserId int /// /// Type alias for CEF frame id /// #if CEF_VERSION_MAJOR < 122 #define QCefFrameId qint64 #else #define QCefFrameId QString #endif /// /// Private implementation /// class QCefViewPrivate; /// /// Represents the CEF browser view /// class QCEFVIEW_EXPORT QCefView : public QWidget { Q_OBJECT Q_DECLARE_PRIVATE(QCefView) Q_DISABLE_COPY(QCefView) QScopedPointer d_ptr; public: /// /// The main frame identity /// static const QCefFrameId MainFrameID; /// /// The identifier for all frames /// static const QCefFrameId AllFrameID; /// /// Represents the CEF pop-up windows open disposition /// enum CefWindowOpenDisposition { CefWindowOpenDispositionUnknown, CefWindowOpenDispositionCurrentTab, CefWindowOpenDispositionSingletonTab, CefWindowOpenDispositionNewForeGroundTab, CefWindowOpenDispositionNewBackgroundTab, CefWindowOpenDispositionNewPopup, CefWindowOpenDispositionNewWindow, CefWindowOpenDispositionSaveToDisk, CefWindowOpenDispositionOffTheRecord, CefWindowOpenDispositionIgnoreAction, }; Q_ENUM(CefWindowOpenDisposition) public: /// /// Constructs a QCefView instance /// /// The target url /// The instance /// The parent /// The Qt WindowFlags QCefView(const QString& url, const QCefSetting* setting, QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); /// /// Constructs a QCefView instance /// /// The parent /// The Qt WindowFlags QCefView(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); /// /// Destructs the QCefView instance /// ~QCefView(); /// /// Adds a url mapping item with local web resource directory /// /// The path to the local resource directory /// The url to be mapped to /// The priority void addLocalFolderResource(const QString& path, const QString& url, int priority = 0); /// /// Adds a url mapping item with local archive (.zip) file which contains the web resource /// /// The path to the local archive file /// The url to be mapped to /// The password of the archive /// The priority void addArchiveResource(const QString& path, const QString& url, const QString& password = "", int priority = 0); /// /// Gets the browser id /// /// The browser id QCefBrowserId browserId(); /// /// Navigates to the content. /// /// The content void navigateToString(const QString& content); /// /// Navigates to the URL /// /// The url void navigateToUrl(const QString& url); /// /// Checks whether the browser can go back /// /// True if can; otherwise false bool browserCanGoBack(); /// /// Checks whether the browser can go forward /// /// True if can; otherwise false bool browserCanGoForward(); /// /// Requires the browser to go back /// void browserGoBack(); /// /// Requires the browser to go forward /// void browserGoForward(); /// /// Checks whether the browser is loading /// /// True if it is loading; otherwise false bool browserIsLoading(); /// /// Requires the browser to reload /// void browserReload(); /// /// Requires the browser to stop load /// void browserStopLoad(); /// /// Triggers the event for main frame /// /// The instance /// True on successful; otherwise false bool triggerEvent(const QCefEvent& event); /// /// Triggers the event for specified frame /// /// The instance /// The frame id /// True on successful; otherwise false bool triggerEvent(const QCefEvent& event, const QCefFrameId& frameId); /// /// Broad cast the event for all frames /// /// The instance /// True on successful; otherwise false bool broadcastEvent(const QCefEvent& event); /// /// Response the request /// /// The query instance /// True on successful; otherwise false bool responseQCefQuery(const QCefQuery& query); /// /// Executes javascript code in specified frame, this method does not report the result of the javascript. /// To get the result of the javascript execution use /// /// The frame id /// The javascript code /// /// The URL where the script in question can be found, if any. The renderer may request this URL to show the developer /// the source of the error /// /// True on successful; otherwise false bool executeJavascript(const QCefFrameId& frameId, const QString& code, const QString& url); /// /// Executes javascript code in specified frame and the result will be reported through signal /// /// The frame id /// The javascript code /// /// The URL where the script in question can be found, if any. The renderer may request this URL to show the developer /// the source of the error /// /// The context used to identify the one execution /// True on successful; otherwise false bool executeJavascriptWithResult(const QCefFrameId& frameId, const QString& code, const QString& url, const QString& context); /// /// Sets the preference for this browser /// /// The preference name /// /// The preference value, if this value is QVariant::UnknownType or QVariant::Invalid, the /// preference will be restored to default value /// /// The error message populated on failure /// True on successful; otherwise false bool setPreference(const QString& name, const QVariant& value, const QString& error); /// /// Sets whether to disable the context menu for popup browser /// /// True to disable; otherwise false void setDisablePopupContextMenu(bool disable); /// /// Gets whether to disable the context menu for popup browser /// /// True to disable; otherwise false bool isPopupContextMenuDisabled(); /// /// Detects whether this browser has a devtools opened /// /// True if opend already; otherwise false bool hasDevTools(); /// /// Opens the devtools dialog /// void showDevTools(); /// /// Closes the devtools dialog /// void closeDevTools(); /// /// Sets whether to enable drag and drop /// /// True to enable; otherwise false /// /// This function does not work for OSR mode. There is a problem, when dragging a file to a non dragging area, /// the content of the file will be displayed. You need to solve the problem yourself. /// void setEnableDragAndDrop(bool enable); /// /// Gets whether to enable drag and drop /// /// True to enable; otherwise false bool isDragAndDropEnabled() const; signals: /// /// Gets called on loading state changed /// /// Indicates the browser id /// Indicates the browser is loading /// Indicates the browser can go back /// Indicates the browser can go forward void loadingStateChanged(const QCefBrowserId& browserId, bool isLoading, bool canGoBack, bool canGoForward); /// /// Gets called on loading starts /// /// Indicates the browser id /// Indicates the frame id /// Indicates the whether this is the main frame /// transition type void loadStart(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int transitionType); /// /// Gets called on loading ends /// /// Indicates the browser id /// Indicates the frame id /// Indicates the whether this is the main frame /// The HTTP status code void loadEnd(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int httpStatusCode); /// /// Gets called on loading failed due to error /// /// Indicates the browser id /// Indicates the frame id /// Indicates the whether this is the main frame /// The error code /// The error message /// The url caused the failure void loadError(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int errorCode, const QString& errorMsg, const QString& failedUrl); /// /// Gets called on draggable region changed /// /// The new draggable region /// The new non-draggable region void draggableRegionChanged(const QRegion& draggableRegion, const QRegion& nonDraggableRegion); /// /// Gets called on the address changed /// /// The frame id /// The address void addressChanged(const QCefFrameId& frameId, const QString& url); /// /// Gets called on title changed /// /// The title void titleChanged(const QString& title); /// /// Gets called on favicon url changed /// /// The urls void faviconURLChanged(const QStringList& urls); /// /// Gets called on fullscreen mode changed /// /// The current fullscreen mode void fullscreenModeChanged(bool fullscreen); /// /// Gets called on status message changed /// /// The status message void statusMessage(const QString& message); /// /// Gets called on console message from the web content /// /// The message /// The level void consoleMessage(const QString& message, int level); /// /// Gets called on loading progress changed /// /// Current progress void loadingProgressChanged(double progress); /// /// Gets called on built-in scheme URL access /// /// The browser id /// The frame id /// The full url void cefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url); /// /// Gets called on new request /// /// The browser id /// The frame id /// The query request void cefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query); /// /// Gets called on invoking method request from web content(Javascript) /// /// The browser id /// The frame id /// The method name /// The arguments list void invokeMethod(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& method, const QVariantList& arguments); /// /// Gets called on the result of the javascript executed with returned /// /// The browser id /// The frame id /// The context /// The result void reportJavascriptResult(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& context, const QVariant& result); /// /// Gets called after the native browser window created. This slot does not work for OSR mode. /// /// The native browser windows void nativeBrowserCreated(QWindow* window); protected: /// /// Gets called before a new browser created (only for browser created by non-JavaScript) /// /// The source frame id /// The target URL /// The target name /// Target window open method /// Rect to be used for the popup /// Settings to be used for the popup /// True to cancel the popup; false to allow /// virtual QCefView* onNewBrowser(const QCefFrameId& sourceFrameId, const QString& url, const QString& name, QCefView::CefWindowOpenDisposition targetDisposition, QRect& rect, QCefSetting& settings); /// /// Gets called before the popup browser created (only for browser created by JavaScript) /// /// The source frame id /// The target URL /// The target name /// Target window open method /// Rect to be used for the popup /// Settings to be used for the popup /// True to cancel the popup; false to allow virtual bool onNewPopup(const QCefFrameId& frameId, const QString& targetUrl, QString& targetFrameName, QCefView::CefWindowOpenDisposition targetDisposition, QRect& rect, QCefSetting& settings, bool& disableJavascriptAccess); /// /// Gets called on new download item was required. Keep reference to the download item /// and call method to allow and start the download, /// Ignore the download item to disallow the download /// /// The new download item /// The new suggested name virtual void onNewDownloadItem(const QSharedPointer& item, const QString& suggestedName); /// /// Gets called on download item updated. To get this method called method must /// be called in method /// /// The download item virtual void onUpdateDownloadItem(const QSharedPointer& item); /// /// Gets called on close request from web /// /// True to allow the close, false to cancel the close virtual bool onRequestCloseFromWeb(); #pragma region QWidget public slots: /// /// /// inline void setFocus() { setFocus(Qt::OtherFocusReason); } public: /// /// Please refer to QWidget::setFocus /// void setFocus(Qt::FocusReason reason); /// /// Please refer to QWidget::inputMethodQuery /// QVariant inputMethodQuery(Qt::InputMethodQuery query) const override; /// /// Renders the view content to target painter /// /// The target painter void render(QPainter* painter); protected: /// /// Please refer to QWidget::paintEngine /// QPaintEngine* paintEngine() const override; /// /// Please refer to QWidget::paintEvent /// void paintEvent(QPaintEvent* event) override; /// /// Please refer to QWidget::inputMethodEvent /// void inputMethodEvent(QInputMethodEvent* event) override; /// /// Please refer to QWidget::showEvent /// void showEvent(QShowEvent* event) override; /// /// Please refer to QWidget::hideEvent /// void hideEvent(QHideEvent* event) override; /// /// Please refer to QWidget::focusInEvent /// void focusInEvent(QFocusEvent* event) override; /// /// Please refer to QWidget::focusOutEvent /// void focusOutEvent(QFocusEvent* event) override; /// /// Please refer to QWidget::resizeEvent /// void resizeEvent(QResizeEvent* event) override; /// /// Please refer to QWidget::keyPressEvent /// void keyPressEvent(QKeyEvent* event) override; /// /// Please refer to QWidget::keyReleaseEvent /// void keyReleaseEvent(QKeyEvent* event) override; /// /// Please refer to QWidget::mouseMoveEvent /// void mouseMoveEvent(QMouseEvent* event) override; /// /// Please refer to QWidget::mousePressEvent /// void mousePressEvent(QMouseEvent* event) override; /// /// Please refer to QWidget::mouseReleaseEvent /// void mouseReleaseEvent(QMouseEvent* event) override; /// /// Please refer to QWidget::wheelEvent /// void wheelEvent(QWheelEvent* event) override; /// /// Please refer to QWidget::leaveEvent /// void leaveEvent(QEvent* event) override; /// /// Please refer to QWidget::contextMenuEvent /// void contextMenuEvent(QContextMenuEvent* event) override; #pragma endregion }; #endif // QCEFVIEW_H