2013-07-14 08:04:24 +02:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QToolBar>
|
2013-07-14 10:41:31 +02:00
|
|
|
#include <QAction>
|
2013-07-14 08:04:24 +02:00
|
|
|
#include <QPointer>
|
2013-07-17 20:42:42 +02:00
|
|
|
|
|
|
|
#include <QMessageBox>
|
2013-07-14 08:04:24 +02:00
|
|
|
|
|
|
|
#include "core/basenetworkaccessmanager.h"
|
|
|
|
#include "gui/basewebview.h"
|
2013-07-17 21:12:51 +02:00
|
|
|
#include "gui/basewebpage.h"
|
2013-07-14 08:04:24 +02:00
|
|
|
#include "gui/webbrowser.h"
|
2013-07-17 20:42:42 +02:00
|
|
|
#include "gui/locationlineedit.h"
|
2013-07-14 08:04:24 +02:00
|
|
|
#include "gui/themefactory.h"
|
|
|
|
|
|
|
|
|
|
|
|
QPointer<BaseNetworkAccessManager> WebBrowser::m_networkManager;
|
2013-07-14 10:41:31 +02:00
|
|
|
QList<WebBrowser*> WebBrowser::m_runningWebBrowsers;
|
2013-07-14 08:04:24 +02:00
|
|
|
|
|
|
|
WebBrowser::WebBrowser(QWidget *parent)
|
2013-07-14 10:41:31 +02:00
|
|
|
: QWidget(parent), m_layout(new QVBoxLayout(this)),
|
|
|
|
m_toolBar(new QToolBar(tr("Navigation panel"), this)),
|
|
|
|
m_webView(new BaseWebView(this)),
|
2013-07-17 20:42:42 +02:00
|
|
|
m_txtLocation(new LocationLineEdit(this)),
|
2013-07-14 10:41:31 +02:00
|
|
|
m_actionBack(m_webView->pageAction(QWebPage::Back)),
|
|
|
|
m_actionForward(m_webView->pageAction(QWebPage::Forward)),
|
|
|
|
m_actionReload(m_webView->pageAction(QWebPage::Reload)),
|
|
|
|
m_actionStop(m_webView->pageAction(QWebPage::Stop)) {
|
|
|
|
|
|
|
|
// Add this new instance to the global list of web browsers.
|
|
|
|
// NOTE: This is used primarily for dynamic icon theme switching.
|
|
|
|
m_runningWebBrowsers.append(this);
|
2013-07-14 08:04:24 +02:00
|
|
|
|
|
|
|
// TODO: Make this better, add location box, search box, better icons for buttons,
|
|
|
|
// note that icons must be loaded via separate method,
|
|
|
|
// and main window will be responsible for reloading
|
|
|
|
// icons on all web browsers.
|
|
|
|
|
2013-07-17 20:42:42 +02:00
|
|
|
// Set properties of some components.
|
2013-07-14 10:41:31 +02:00
|
|
|
m_toolBar->layout()->setMargin(0);
|
|
|
|
m_toolBar->setFloatable(false);
|
|
|
|
m_toolBar->setMovable(false);
|
|
|
|
|
2013-07-17 20:42:42 +02:00
|
|
|
// Modify action texts.
|
|
|
|
m_actionBack->setText(tr("Back"));
|
|
|
|
m_actionBack->setToolTip(tr("Go back"));
|
|
|
|
m_actionForward->setText(tr("Forward"));
|
|
|
|
m_actionForward->setToolTip(tr("Go forward"));
|
|
|
|
m_actionReload->setText(tr("Reload"));
|
|
|
|
m_actionReload->setToolTip(tr("Reload current web page"));
|
|
|
|
m_actionStop->setText(tr("Stop"));
|
|
|
|
m_actionStop->setToolTip(tr("Stop web page loading"));
|
|
|
|
|
2013-07-14 10:41:31 +02:00
|
|
|
// Add needed actions into toolbar.
|
|
|
|
m_toolBar->addAction(m_actionBack);
|
|
|
|
m_toolBar->addAction(m_actionForward);
|
|
|
|
m_toolBar->addSeparator();
|
|
|
|
m_toolBar->addAction(m_actionReload);
|
|
|
|
m_toolBar->addAction(m_actionStop);
|
2013-07-17 20:42:42 +02:00
|
|
|
m_toolBar->addWidget(m_txtLocation);
|
2013-07-14 10:41:31 +02:00
|
|
|
|
|
|
|
// Setup layout.
|
|
|
|
m_layout->addWidget(m_toolBar);
|
|
|
|
m_layout->addWidget(m_webView);
|
2013-07-14 08:04:24 +02:00
|
|
|
m_layout->setMargin(0);
|
|
|
|
|
2013-07-17 20:42:42 +02:00
|
|
|
createConnections();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebBrowser::createConnections() {
|
|
|
|
// When user confirms new url, then redirect to it.
|
|
|
|
connect(m_txtLocation, &LocationLineEdit::submitted,
|
|
|
|
this, &WebBrowser::navigateToUrl);
|
|
|
|
// If new page loads, then update current url.
|
|
|
|
connect(m_webView, &BaseWebView::urlChanged,
|
|
|
|
this, &WebBrowser::updateUrl);
|
|
|
|
|
|
|
|
// Change location textbox status according to webpage status.
|
2013-07-17 21:12:51 +02:00
|
|
|
connect(m_webView->page(), &BaseWebPage::loadProgress,
|
2013-07-17 20:42:42 +02:00
|
|
|
m_txtLocation, &LocationLineEdit::setProgress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebBrowser::updateUrl(const QUrl &url) {
|
|
|
|
m_txtLocation->setText(url.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebBrowser::navigateToUrl(const QString &textual_url) {
|
|
|
|
QUrl extracted_url = QUrl::fromUserInput(textual_url);
|
|
|
|
|
|
|
|
if (extracted_url.isValid()) {
|
|
|
|
m_webView->setUrl(extracted_url);
|
|
|
|
}
|
2013-07-14 08:04:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WebBrowser::~WebBrowser() {
|
|
|
|
qDebug("Erasing WebBrowser instance.");
|
|
|
|
|
2013-07-14 10:41:31 +02:00
|
|
|
// Remove this instance from the global list of web browsers.
|
|
|
|
m_runningWebBrowsers.removeAll(this);
|
|
|
|
|
|
|
|
// Delete members.
|
2013-07-14 08:04:24 +02:00
|
|
|
delete m_layout;
|
|
|
|
}
|
|
|
|
|
2013-07-14 10:41:31 +02:00
|
|
|
void WebBrowser::setupIcons() {
|
|
|
|
m_actionBack->setIcon(ThemeFactory::fromTheme("go-previous"));
|
|
|
|
m_actionForward->setIcon(ThemeFactory::fromTheme("go-next"));
|
|
|
|
m_actionReload->setIcon(ThemeFactory::fromTheme("view-refresh"));
|
|
|
|
m_actionStop->setIcon(ThemeFactory::fromTheme("process-stop"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<WebBrowser *> WebBrowser::runningWebBrowsers() {
|
|
|
|
return m_runningWebBrowsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseNetworkAccessManager *WebBrowser::globalNetworkManager() {
|
2013-07-14 08:04:24 +02:00
|
|
|
if (m_networkManager.isNull()) {
|
|
|
|
m_networkManager = new BaseNetworkAccessManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_networkManager;
|
|
|
|
}
|