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 #include "qmlbrowseraction.h"
0019 #include "qztools.h"
0020 #include "navigationbar.h"
0021 #include "statusbar.h"
0022 #include "pluginproxy.h"
0023 #include "qml/api/fileutils/qmlfileutils.h"
0024 #include "qml/qmlengine.h"
0025 #include "qml/qmlstaticdata.h"
0026 #include <QQuickWidget>
0027 #include <QQmlContext>
0028 
0029 QmlBrowserAction::QmlBrowserAction(QObject *parent)
0030     : QObject(parent)
0031 {
0032     m_button = new QmlBrowserActionButton();
0033 
0034     connect(this, &QmlBrowserAction::identityChanged, m_button, &QmlBrowserActionButton::setId);
0035     connect(this, &QmlBrowserAction::nameChanged, m_button, &QmlBrowserActionButton::setName);
0036     connect(this, &QmlBrowserAction::titleChanged, m_button, &QmlBrowserActionButton::setTitle);
0037     connect(this, &QmlBrowserAction::toolTipChanged, m_button, &QmlBrowserActionButton::setToolTip);
0038     connect(this, &QmlBrowserAction::iconChanged, m_button, &QmlBrowserActionButton::setIcon);
0039     connect(this, &QmlBrowserAction::badgeTextChanged, m_button, &QmlBrowserActionButton::setBadgeText);
0040     connect(this, &QmlBrowserAction::popupChanged, m_button, &QmlBrowserActionButton::setPopup);
0041 
0042     connect(m_button, &QmlBrowserActionButton::clicked, this, &QmlBrowserAction::clicked);
0043 
0044     connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &QmlBrowserAction::addButton);
0045 }
0046 
0047 void QmlBrowserAction::componentComplete()
0048 {
0049     const QList<BrowserWindow*> windows = mApp->windows();
0050     for (BrowserWindow *window : windows) {
0051         addButton(window);
0052     }
0053 }
0054 
0055 QmlBrowserAction::~QmlBrowserAction()
0056 {
0057     const QList<BrowserWindow*> windows = mApp->windows();
0058     for (BrowserWindow *window : windows) {
0059         removeButton(window);
0060     }
0061 }
0062 
0063 QmlBrowserActionButton *QmlBrowserAction::button() const
0064 {
0065     return m_button;
0066 }
0067 
0068 QmlBrowserAction::Locations QmlBrowserAction::location() const
0069 {
0070     return m_locations;
0071 }
0072 
0073 QString QmlBrowserAction::identity() const
0074 {
0075     return m_identity;
0076 }
0077 
0078 void QmlBrowserAction::setIdentity(const QString &identity)
0079 {
0080     m_identity = identity;
0081     Q_EMIT identityChanged(m_identity);
0082 }
0083 
0084 QString QmlBrowserAction::name() const
0085 {
0086     return m_name;
0087 }
0088 
0089 void QmlBrowserAction::setName(const QString &name)
0090 {
0091     m_name = name;
0092     Q_EMIT nameChanged(m_name);
0093 }
0094 
0095 QString QmlBrowserAction::title() const
0096 {
0097     return m_title;
0098 }
0099 
0100 void QmlBrowserAction::setTitle(const QString &title)
0101 {
0102     m_title = title;
0103     Q_EMIT titleChanged(m_title);
0104 }
0105 
0106 QString QmlBrowserAction::toolTip() const
0107 {
0108     return m_toolTip;
0109 }
0110 
0111 void QmlBrowserAction::setToolTip(const QString &toolTip)
0112 {
0113     m_toolTip = toolTip;
0114     Q_EMIT toolTipChanged(m_toolTip);
0115 }
0116 
0117 QString QmlBrowserAction::icon() const
0118 {
0119     return m_icon;
0120 }
0121 
0122 void QmlBrowserAction::setIcon(const QString &icon)
0123 {
0124     m_icon = icon;
0125     Q_EMIT iconChanged(m_icon);
0126 }
0127 
0128 QString QmlBrowserAction::badgeText() const
0129 {
0130     return m_badgeText;
0131 }
0132 
0133 void QmlBrowserAction::setBadgeText(const QString &badgeText)
0134 {
0135     m_badgeText = badgeText;
0136     Q_EMIT badgeTextChanged(m_badgeText);
0137 }
0138 
0139 QQmlComponent *QmlBrowserAction::popup() const
0140 {
0141     return m_popup;
0142 }
0143 
0144 void QmlBrowserAction::setPopup(QQmlComponent *popup)
0145 {
0146     m_popup = popup;
0147     Q_EMIT popupChanged(m_popup);
0148 }
0149 
0150 void QmlBrowserAction::setLocation(const Locations &locations)
0151 {
0152     m_locations = locations;
0153     Q_EMIT locationChanged(m_locations);
0154 }
0155 
0156 void QmlBrowserAction::addButton(BrowserWindow *window)
0157 {
0158     if (location().testFlag(NavigationToolBar)) {
0159         window->navigationBar()->addToolButton(button());
0160     }
0161 
0162     if (location().testFlag(StatusBar)) {
0163         window->statusBar()->addButton(button());
0164     }
0165 }
0166 
0167 void QmlBrowserAction::removeButton(BrowserWindow *window)
0168 {
0169     if (location().testFlag(NavigationToolBar)) {
0170         window->navigationBar()->removeToolButton(button());
0171     }
0172 
0173     if (location().testFlag(StatusBar)) {
0174         window->statusBar()->removeButton(button());
0175     }
0176 }
0177 
0178 QmlBrowserActionButton::QmlBrowserActionButton(QObject *parent)
0179     : AbstractButtonInterface(parent)
0180 {
0181     connect(this, &AbstractButtonInterface::clicked, this, &QmlBrowserActionButton::positionPopup);
0182 }
0183 
0184 QString QmlBrowserActionButton::id() const
0185 {
0186     return m_id;
0187 }
0188 
0189 void QmlBrowserActionButton::setId(const QString &id)
0190 {
0191     m_id = id;
0192 }
0193 
0194 QString QmlBrowserActionButton::name() const
0195 {
0196     return m_name;
0197 }
0198 
0199 void QmlBrowserActionButton::setName(const QString &name)
0200 {
0201     m_name = name;
0202 }
0203 
0204 void QmlBrowserActionButton::setTitle(const QString &title)
0205 {
0206     AbstractButtonInterface::setTitle(title);
0207 }
0208 
0209 void QmlBrowserActionButton::setToolTip(const QString &toolTip)
0210 {
0211     AbstractButtonInterface::setToolTip(toolTip);
0212 }
0213 
0214 void QmlBrowserActionButton::setIcon(const QString &icon)
0215 {
0216     m_iconUrl = icon;
0217     if (!m_popup) {
0218         return;
0219     }
0220     auto qmlEngine = qobject_cast<QmlEngine*>(m_popup->creationContext()->engine());
0221     if (!qmlEngine) {
0222         return;
0223     }
0224     const QString pluginPath = qmlEngine->extensionPath();
0225     QIcon qicon = QmlStaticData::instance().getIcon(m_iconUrl, pluginPath);
0226     AbstractButtonInterface::setIcon(qicon);
0227 }
0228 
0229 void QmlBrowserActionButton::setBadgeText(const QString &badgeText)
0230 {
0231     AbstractButtonInterface::setBadgeText(badgeText);
0232 }
0233 
0234 void QmlBrowserActionButton::setPopup(QQmlComponent *popup)
0235 {
0236     m_popup = popup;
0237 }
0238 
0239 void QmlBrowserActionButton::positionPopup(ClickController *clickController)
0240 {
0241     if (!m_popup) {
0242         qWarning() << "No popup to show";
0243         return;
0244     }
0245 
0246     auto *quickWidget = new QQuickWidget();
0247     quickWidget->setContent(m_popup->url(), m_popup, m_popup->create(m_popup->creationContext()));
0248 
0249     auto *widget = new QWidget();
0250     quickWidget->setParent(widget);
0251 
0252     widget->setWindowFlag(Qt::Popup);
0253     widget->setAttribute(Qt::WA_DeleteOnClose);
0254     widget->move(clickController->callPopupPosition(quickWidget->size()));
0255 
0256     connect(quickWidget, &QQuickWidget::destroyed, this, [clickController]{
0257         clickController->callPopupClosed();
0258     });
0259 
0260     widget->show();
0261 }