mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
Support for opening popup windows from JavaScript.
- it is now possible to detect and open popup windows from window.open() javascript method. - craeted classes PopupWindow, PopupWebView and PopupWebPage for this purpose - PopupWebView inherits from WebView - PopupWebPage inherits from WebPage
This commit is contained in:
parent
bbadb4ec37
commit
a9f9876a25
Binary file not shown.
Before Width: | Height: | Size: 795 B |
72
src/popupwindow/popuplocationbar.cpp
Normal file
72
src/popupwindow/popuplocationbar.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "popuplocationbar.h"
|
||||
#include "popupwebview.h"
|
||||
#include "toolbutton.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class PopupSiteIcon : public QWidget
|
||||
{
|
||||
public:
|
||||
explicit PopupSiteIcon(QWidget* parent = 0) : QWidget(parent) { }
|
||||
void setIcon(const QIcon &icon) {
|
||||
m_icon = QIcon(icon.pixmap(16, 16));
|
||||
repaint();
|
||||
}
|
||||
|
||||
private:
|
||||
QIcon m_icon;
|
||||
|
||||
void paintEvent(QPaintEvent*) {
|
||||
QPainter p(this);
|
||||
m_icon.paint(&p, rect());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
PopupLocationBar::PopupLocationBar(QWidget* parent)
|
||||
: LineEdit(parent)
|
||||
, m_view(0)
|
||||
{
|
||||
m_siteIcon = new PopupSiteIcon(this);
|
||||
m_siteIcon->setIcon(QWebSettings::webGraphic(QWebSettings::DefaultFrameIconGraphic));
|
||||
m_siteIcon->setFixedSize(20, 26);
|
||||
|
||||
addWidget(m_siteIcon, LineEdit::LeftSide);
|
||||
setWidgetSpacing(0);
|
||||
|
||||
setFixedHeight(26);
|
||||
setReadOnly(true);
|
||||
}
|
||||
|
||||
void PopupLocationBar::setView(PopupWebView* view)
|
||||
{
|
||||
m_view = view;
|
||||
}
|
||||
|
||||
void PopupLocationBar::showUrl(const QUrl &url)
|
||||
{
|
||||
setText(url.toEncoded());
|
||||
setCursorPosition(0);
|
||||
}
|
||||
|
||||
void PopupLocationBar::showIcon()
|
||||
{
|
||||
m_siteIcon->setIcon(QIcon(m_view->icon().pixmap(16, 16)));
|
||||
}
|
51
src/popupwindow/popuplocationbar.h
Normal file
51
src/popupwindow/popuplocationbar.h
Normal file
@ -0,0 +1,51 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef POPUPLOCATIONBAR_H
|
||||
#define POPUPLOCATIONBAR_H
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
#include "lineedit.h"
|
||||
|
||||
class PopupSiteIcon;
|
||||
class PopupWebView;
|
||||
class PopupLocationBar : public LineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize)
|
||||
Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth)
|
||||
Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight)
|
||||
|
||||
public:
|
||||
explicit PopupLocationBar(QWidget* parent = 0);
|
||||
|
||||
void setView(PopupWebView* view);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void showUrl(const QUrl &url);
|
||||
void showIcon();
|
||||
|
||||
private:
|
||||
PopupWebView* m_view;
|
||||
PopupSiteIcon* m_siteIcon;
|
||||
|
||||
};
|
||||
|
||||
#endif // POPUPLOCATIONBAR_H
|
129
src/popupwindow/popupwebpage.cpp
Normal file
129
src/popupwindow/popupwebpage.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "popupwebpage.h"
|
||||
#include "popupwebview.h"
|
||||
#include "popupwindow.h"
|
||||
#include "qupzilla.h"
|
||||
|
||||
// Wrapper class to detect whether window is opened from JavaScript window.open method
|
||||
// It has to be done this way, because QtWebKit has really bad API when it comes to opening
|
||||
// new windows.
|
||||
//
|
||||
// Got an idea how to determine it from kWebKitPart.
|
||||
|
||||
PopupWebPage::PopupWebPage(QWebPage::WebWindowType type, QupZilla* mainClass)
|
||||
: WebPage(mainClass)
|
||||
, p_QupZilla(mainClass)
|
||||
, m_type(type)
|
||||
, m_createNewWindow(false)
|
||||
, m_menuBarVisible(false)
|
||||
, m_statusBarVisible(false)
|
||||
, m_toolBarVisible(false)
|
||||
, m_isLoading(false)
|
||||
, m_progress(0)
|
||||
{
|
||||
connect(this, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(slotGeometryChangeRequested(QRect)));
|
||||
connect(this, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(slotMenuBarVisibilityChangeRequested(bool)));
|
||||
connect(this, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(slotToolBarVisibilityChangeRequested(bool)));
|
||||
connect(this, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(slotStatusBarVisibilityChangeRequested(bool)));
|
||||
|
||||
connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
|
||||
connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
|
||||
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
|
||||
|
||||
settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, false);
|
||||
|
||||
QTimer::singleShot(0, this, SLOT(checkBehaviour()));
|
||||
}
|
||||
|
||||
void PopupWebPage::slotGeometryChangeRequested(const QRect &rect)
|
||||
{
|
||||
if (rect.isValid()) {
|
||||
m_geometry = rect;
|
||||
m_createNewWindow = true;
|
||||
}
|
||||
}
|
||||
|
||||
void PopupWebPage::slotMenuBarVisibilityChangeRequested(bool visible)
|
||||
{
|
||||
m_menuBarVisible = visible;
|
||||
}
|
||||
|
||||
void PopupWebPage::slotStatusBarVisibilityChangeRequested(bool visible)
|
||||
{
|
||||
m_statusBarVisible = visible;
|
||||
}
|
||||
|
||||
void PopupWebPage::slotToolBarVisibilityChangeRequested(bool visible)
|
||||
{
|
||||
m_toolBarVisible = visible;
|
||||
}
|
||||
|
||||
void PopupWebPage::slotLoadStarted()
|
||||
{
|
||||
m_isLoading = true;
|
||||
m_progress = 0;
|
||||
}
|
||||
|
||||
void PopupWebPage::slotLoadProgress(int prog)
|
||||
{
|
||||
m_progress = prog;
|
||||
}
|
||||
|
||||
void PopupWebPage::slotLoadFinished(bool state)
|
||||
{
|
||||
Q_UNUSED(state)
|
||||
|
||||
m_isLoading = false;
|
||||
m_progress = 0;
|
||||
}
|
||||
|
||||
void PopupWebPage::checkBehaviour()
|
||||
{
|
||||
if (m_createNewWindow) {
|
||||
PopupWebView* view = new PopupWebView;
|
||||
view->setWebPage(this);
|
||||
|
||||
PopupWindow* popup = new PopupWindow(view);
|
||||
popup->setWindowGeometry(m_geometry);
|
||||
popup->setMenuBarVisibility(m_menuBarVisible);
|
||||
popup->setStatusBarVisibility(m_statusBarVisible);
|
||||
popup->setToolBarVisibility(m_toolBarVisible);
|
||||
popup->show();
|
||||
|
||||
p_QupZilla->addDeleteOnCloseWidget(popup);
|
||||
|
||||
disconnect(this, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(slotGeometryChangeRequested(QRect)));
|
||||
disconnect(this, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(slotMenuBarVisibilityChangeRequested(bool)));
|
||||
disconnect(this, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(slotToolBarVisibilityChangeRequested(bool)));
|
||||
disconnect(this, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(slotStatusBarVisibilityChangeRequested(bool)));
|
||||
|
||||
disconnect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
|
||||
disconnect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
|
||||
disconnect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
|
||||
}
|
||||
else {
|
||||
int index = p_QupZilla->tabWidget()->addView(QUrl(), TabWidget::CleanSelectedPage);
|
||||
TabbedWebView* view = p_QupZilla->weView(index);
|
||||
view->setWebPage(this);
|
||||
if (m_isLoading) {
|
||||
view->slotLoadStarted();
|
||||
view->loadingProgress(m_progress);
|
||||
}
|
||||
}
|
||||
}
|
60
src/popupwindow/popupwebpage.h
Normal file
60
src/popupwindow/popupwebpage.h
Normal file
@ -0,0 +1,60 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef POPUPWEBPAGE_H
|
||||
#define POPUPWEBPAGE_H
|
||||
|
||||
#include "webpage.h"
|
||||
|
||||
class QupZilla;
|
||||
class PopupWebPage : public WebPage
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PopupWebPage(WebWindowType type, QupZilla* mainClass);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
||||
private slots:
|
||||
void slotGeometryChangeRequested(const QRect &rect);
|
||||
void slotMenuBarVisibilityChangeRequested(bool visible);
|
||||
void slotStatusBarVisibilityChangeRequested(bool visible);
|
||||
void slotToolBarVisibilityChangeRequested(bool visible);
|
||||
|
||||
void slotLoadStarted();
|
||||
void slotLoadProgress(int prog);
|
||||
void slotLoadFinished(bool state);
|
||||
|
||||
void checkBehaviour();
|
||||
|
||||
private:
|
||||
QupZilla* p_QupZilla;
|
||||
QWebPage::WebWindowType m_type;
|
||||
bool m_createNewWindow;
|
||||
|
||||
bool m_menuBarVisible;
|
||||
bool m_statusBarVisible;
|
||||
bool m_toolBarVisible;
|
||||
QRect m_geometry;
|
||||
|
||||
bool m_isLoading;
|
||||
bool m_progress;
|
||||
};
|
||||
|
||||
#endif // POPUPWEBPAGE_H
|
113
src/popupwindow/popupwindow.cpp
Normal file
113
src/popupwindow/popupwindow.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "popupwindow.h"
|
||||
#include "popupwebview.h"
|
||||
#include "popupwebpage.h"
|
||||
#include "popuplocationbar.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
PopupWindow::PopupWindow(PopupWebView* view)
|
||||
: QWidget()
|
||||
, m_view(view)
|
||||
, m_page(view->webPage())
|
||||
{
|
||||
m_layout = new QVBoxLayout(this);
|
||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||
m_layout->setSpacing(0);
|
||||
|
||||
m_locationBar = new PopupLocationBar(this);
|
||||
m_locationBar->setView(m_view);
|
||||
|
||||
m_statusBar = new QStatusBar(this);
|
||||
m_statusBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
|
||||
|
||||
m_layout->addWidget(m_locationBar);
|
||||
m_layout->addWidget(m_view);
|
||||
m_layout->addWidget(m_statusBar);
|
||||
setLayout(m_layout);
|
||||
|
||||
connect(m_view, SIGNAL(showNotification(QWidget*)), this, SLOT(showNotification(QWidget*)));
|
||||
connect(m_view, SIGNAL(titleChanged(QString)), this, SLOT(titleChanged()));
|
||||
connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar, SLOT(showUrl(QUrl)));
|
||||
connect(m_view, SIGNAL(iconChanged()), m_locationBar, SLOT(showIcon()));
|
||||
connect(m_view, SIGNAL(statusBarMessage(QString)), m_statusBar, SLOT(showMessage(QString)));
|
||||
|
||||
connect(m_page, SIGNAL(linkHovered(QString, QString, QString)), m_statusBar, SLOT(showMessage(QString)));
|
||||
connect(m_page, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(setWindowGeometry(QRect)));
|
||||
connect(m_page, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(setStatusBarVisibility(bool)));
|
||||
connect(m_page, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(setMenuBarVisibility(bool)));
|
||||
connect(m_page, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(setToolBarVisibility(bool)));
|
||||
connect(m_page, SIGNAL(windowCloseRequested()), this, SLOT(close()));
|
||||
|
||||
m_view->setFocus();
|
||||
titleChanged();
|
||||
m_locationBar->showUrl(m_view->url());
|
||||
}
|
||||
|
||||
void PopupWindow::showNotification(QWidget* notif)
|
||||
{
|
||||
if (m_layout->count() > 3) {
|
||||
delete m_layout->itemAt(1)->widget();
|
||||
}
|
||||
|
||||
m_layout->insertWidget(1, notif);
|
||||
notif->show();
|
||||
}
|
||||
|
||||
void PopupWindow::closeEvent(QCloseEvent* event)
|
||||
{
|
||||
if (m_page->isRunningLoop()) {
|
||||
event->ignore();
|
||||
return;
|
||||
}
|
||||
|
||||
m_view->deleteLater();
|
||||
m_page->disconnectObjects();
|
||||
|
||||
event->accept();
|
||||
}
|
||||
|
||||
void PopupWindow::setWindowGeometry(const QRect &rect)
|
||||
{
|
||||
if (rect.isValid()) {
|
||||
setGeometry(rect);
|
||||
}
|
||||
}
|
||||
|
||||
void PopupWindow::setStatusBarVisibility(bool visible)
|
||||
{
|
||||
Q_UNUSED(visible)
|
||||
}
|
||||
|
||||
void PopupWindow::setMenuBarVisibility(bool visible)
|
||||
{
|
||||
Q_UNUSED(visible)
|
||||
}
|
||||
|
||||
void PopupWindow::setToolBarVisibility(bool visible)
|
||||
{
|
||||
Q_UNUSED(visible)
|
||||
}
|
||||
|
||||
void PopupWindow::titleChanged()
|
||||
{
|
||||
setWindowTitle(tr("%1 - QupZilla").arg(m_view->title()));
|
||||
}
|
||||
|
||||
|
57
src/popupwindow/popupwindow.h
Normal file
57
src/popupwindow/popupwindow.h
Normal file
@ -0,0 +1,57 @@
|
||||
/* ============================================================
|
||||
* QupZilla - WebKit based browser
|
||||
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef POPUPWINDOW_H
|
||||
#define POPUPWINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QStatusBar>
|
||||
|
||||
class PopupWebView;
|
||||
class PopupWebPage;
|
||||
class PopupLocationBar;
|
||||
class PopupWindow : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit PopupWindow(PopupWebView* view);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void setWindowGeometry(const QRect &rect);
|
||||
void setStatusBarVisibility(bool visible);
|
||||
void setMenuBarVisibility(bool visible);
|
||||
void setToolBarVisibility(bool visible);
|
||||
|
||||
private slots:
|
||||
void titleChanged();
|
||||
void showNotification(QWidget* notif);
|
||||
|
||||
private:
|
||||
void closeEvent(QCloseEvent* event);
|
||||
PopupWebView* m_view;
|
||||
PopupWebPage* m_page;
|
||||
PopupLocationBar* m_locationBar;
|
||||
|
||||
QVBoxLayout* m_layout;
|
||||
QStatusBar* m_statusBar;
|
||||
|
||||
};
|
||||
|
||||
#endif // POPUPWINDOW_H
|
Loading…
Reference in New Issue
Block a user