File indexing completed on 2024-12-22 04:41:13

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@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 #pragma once
0019 #include "abstractbuttoninterface.h"
0020 #include "mainapplication.h"
0021 #include <QQmlComponent>
0022 #include <QQmlParserStatus>
0023 
0024 class QmlBrowserActionButton;
0025 
0026 /**
0027  * @brief The class exposing BrowserAction API to QML
0028  */
0029 class QmlBrowserAction : public QObject, public QQmlParserStatus
0030 {
0031     Q_OBJECT
0032     Q_INTERFACES(QQmlParserStatus)
0033 
0034     /**
0035      * @brief identity for the button. This is a required property.
0036      */
0037     Q_PROPERTY(QString identity READ identity WRITE setIdentity NOTIFY identityChanged)
0038 
0039     /**
0040      * @brief name of the button. This is a required property.
0041      */
0042     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
0043 
0044     /**
0045      * @brief title of the button.
0046      */
0047     Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
0048 
0049     /**
0050      * @brief tool tip of the button.
0051      */
0052     Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip NOTIFY toolTipChanged)
0053 
0054     /**
0055      * @brief icon path of button
0056      *
0057      * The icon path will be search in the following order
0058      * - Theme icon: if the icon is found as a theme icon, then it will
0059      *               be used even if the icon file with same name is present
0060      *               in the plugin directory
0061      * - Falkon resource: for the icons starting with ":", they are searched in
0062      *               falkon resource file
0063      * - Files in plugin directory: All other paths will be resolved relative to
0064      *               the plugin directory. If the icon path is outside the
0065      *               plugin directory, then it will be resolved as empty path.
0066      */
0067     Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged)
0068 
0069     /**
0070      * @brief badge text of the button
0071      */
0072     Q_PROPERTY(QString badgeText READ badgeText WRITE setBadgeText NOTIFY badgeTextChanged)
0073 
0074     /**
0075      * @brief the popup shown when the button is clicked. This must be a QML Window.
0076      */
0077     Q_PROPERTY(QQmlComponent* popup READ popup WRITE setPopup NOTIFY popupChanged)
0078 
0079     /**
0080      * @brief represents locations where the button is to be added.
0081      */
0082     Q_PROPERTY(Locations location READ location WRITE setLocation NOTIFY locationChanged)
0083 
0084 public:
0085     /**
0086      * @brief The Location enum
0087      */
0088     enum Location {
0089         NavigationToolBar = 0x1, //!< to add the button in navigation tool bar
0090         StatusBar = 0x2          //!< to add the button in status bar
0091     };
0092     Q_DECLARE_FLAGS(Locations, Location)
0093     Q_ENUM(Locations)
0094 
0095     explicit QmlBrowserAction(QObject *parent = nullptr);
0096     ~QmlBrowserAction() override;
0097     void classBegin() override {}
0098     void componentComplete() override;
0099     QmlBrowserActionButton *button() const;
0100     Locations location() const;
0101 
0102 Q_SIGNALS:
0103     /**
0104      * @brief This signal is emitted when identity property is changed
0105      * @param QString representing identity
0106      */
0107     void identityChanged(const QString &identity);
0108 
0109     /**
0110      * @brief This signal is emitted when name property is changed
0111      * @param QString representing name
0112      */
0113     void nameChanged(const QString &name);
0114 
0115     /**
0116      * @brief This signal is emitted when title property is changed
0117      * @param QString representing title
0118      */
0119     void titleChanged(const QString &title);
0120 
0121     /**
0122      * @brief This signal is emitted when the toolTip property is changed
0123      * @param QString representing toolTip
0124      */
0125     void toolTipChanged(const QString &toolTip);
0126 
0127     /**
0128      * @brief This signal is emitted when the icon property is changed
0129      * @param QString representing icon
0130      */
0131     void iconChanged(const QString &icon);
0132 
0133     /**
0134      * @brief This signal is emitted when the badgeText property is changed
0135      * @param QString representing badgeText
0136      */
0137     void badgeTextChanged(const QString &badgeText);
0138 
0139     /**
0140      * @brief This signal is emitted when the popup property is changed
0141      * @param QQmComponent representing popup
0142      */
0143     void popupChanged(QQmlComponent *popup);
0144 
0145     /**
0146      * @brief This signal is emitted when the locations property is changed
0147      * @param locations
0148      */
0149     void locationChanged(const Locations &locations);
0150 
0151     /**
0152      * @brief This signal is emitted when the button is clicked
0153      */
0154     void clicked();
0155 
0156 private:
0157     QString m_identity;
0158     QString m_name;
0159     QString m_title;
0160     QString m_toolTip;
0161     QString m_icon;
0162     QString m_badgeText;
0163     QQmlComponent *m_popup = nullptr;
0164     Locations m_locations = NavigationToolBar;
0165     QmlBrowserActionButton *m_button = nullptr;
0166 
0167     QString identity() const;
0168     void setIdentity(const QString &identity);
0169     QString name() const;
0170     void setName(const QString &name);
0171     QString title() const;
0172     void setTitle(const QString &title);
0173     QString toolTip() const;
0174     void setToolTip(const QString &toolTip);
0175     QString icon() const;
0176     void setIcon(const QString &icon);
0177     QString badgeText() const;
0178     void setBadgeText(const QString &badgeText);
0179     QQmlComponent *popup() const;
0180     void setPopup(QQmlComponent *popup);
0181     void setLocation(const Locations &locations);
0182 
0183     void addButton(BrowserWindow *window);
0184     void removeButton(BrowserWindow *window);
0185 };
0186 
0187 class QmlBrowserActionButton : public AbstractButtonInterface
0188 {
0189     Q_OBJECT
0190 public:
0191     explicit QmlBrowserActionButton(QObject *parent = nullptr);
0192     QString id() const override;
0193     void setId(const QString &id);
0194     QString name() const override;
0195     void setName(const QString &name);
0196     void setTitle(const QString &title);
0197     void setToolTip(const QString &toolTip);
0198     void setIcon(const QString &icon);
0199     void setBadgeText(const QString &badgeText);
0200     void setPopup(QQmlComponent *popup);
0201 
0202     void positionPopup(ClickController *clickController);
0203 
0204 private:
0205     QString m_id;
0206     QString m_name;
0207     QString m_iconUrl;
0208     QQmlComponent *m_popup = nullptr;
0209 };
0210 
0211 Q_DECLARE_OPERATORS_FOR_FLAGS(QmlBrowserAction::Locations)