browsertabproxy.cpp Example File
demos/browser/browsertabproxy.cpp
#include "browsertabproxy.h"
#include "browsermainwindow.h"
#include "webview.h"
#include <QtCore/QSignalMapper>
#include <QtGui/QAction>
#include <QtGui/QStatusBar>
#define INITIALIZE_WEB_ACTION(slotName, actionName, webAction) \
m_webActionConnections.append(WebActionConnection(slotName, &m_mainWindow->actionName, webAction));
BrowserTabProxy::BrowserTabProxy(BrowserMainWindow* mainWindow)
: QObject(mainWindow)
, m_mainWindow(mainWindow)
, m_mapper(0)
{
}
void BrowserTabProxy::init()
{
if (m_mapper)
return;
m_mapper = new QSignalMapper(this);
QObject::connect(m_mapper, SIGNAL(mapped(int)), this, SLOT(slotUpdateAction(int)));
INITIALIZE_WEB_ACTION(SLOT(slotUndo()), m_undo, QWebPage::Undo)
INITIALIZE_WEB_ACTION(SLOT(slotRedo()), m_redo, QWebPage::Redo)
INITIALIZE_WEB_ACTION(SLOT(slotCut()), m_cut, QWebPage::Cut)
INITIALIZE_WEB_ACTION(SLOT(slotCopy()), m_copy, QWebPage::Copy)
INITIALIZE_WEB_ACTION(SLOT(slotPaste()), m_paste, QWebPage::Paste)
INITIALIZE_WEB_ACTION(SLOT(slotStop()), m_stop, QWebPage::Stop)
INITIALIZE_WEB_ACTION(SLOT(slotReload()), m_reload, QWebPage::Reload)
INITIALIZE_WEB_ACTION(SLOT(slotRedo()), m_reload, QWebPage::Redo)
INITIALIZE_WEB_ACTION(SLOT(slotBack()), m_historyBack, QWebPage::Back)
INITIALIZE_WEB_ACTION(SLOT(slotForward()), m_historyForward, QWebPage::Forward);
}
void BrowserTabProxy::setTab(WebView *newTab)
{
init();
disconnect(m_currentTab);
connect(newTab);
update(newTab);
m_currentTab = newTab;
if (m_currentTab)
m_currentTab->setFocus();
}
void BrowserTabProxy::updateAction(int index, WebView* tab)
{
init();
QAction* source = tab->page()->action(m_webActionConnections[index].webAction);
(*m_webActionConnections[index].action)->setChecked(source->isChecked());
(*m_webActionConnections[index].action)->setEnabled(source->isEnabled());
(*m_webActionConnections[index].action)->setVisible(source->isVisible());
}
void BrowserTabProxy::slotUpdateAction(int index)
{
updateAction(index, m_currentTab);
}
void BrowserTabProxy::disconnect(WebView* oldTab)
{
Q_ASSERT(oldTab == m_currentTab);
if (!oldTab)
return;
m_currentTab->disconnect(m_mainWindow, SLOT(slotLoadStarted()));
m_currentTab->disconnect(m_mainWindow, SLOT(slotLoadFinished()));
m_currentTab->disconnect(m_mainWindow, SLOT(slotUpdateWindowTitle(const QString&)));
m_currentTab->disconnect(m_mainWindow->statusBar(), SLOT(showMessage(const QString&)));
m_currentTab->disconnect(m_mainWindow, SLOT(slotUrlChanged(const QUrl &)));
m_currentTab->page()->disconnect(this, SIGNAL(linkHovered(const QString&)));
for (int i = 0; i < m_webActionConnections.size(); ++i) {
QAction* action = oldTab->page()->action(m_webActionConnections[i].webAction);
action->disconnect(m_mainWindow);
m_mapper->removeMappings(action);
}
}
void BrowserTabProxy::connect(WebView* newTab)
{
if (!newTab) {
m_mainWindow->slotUpdateWindowTitle(QString());
return;
}
for (int i = 0; i < m_webActionConnections.size(); ++i) {
QAction* action = newTab->page()->action(m_webActionConnections[i].webAction);
QObject::connect(action, SIGNAL(triggered(bool)), m_mainWindow, m_webActionConnections[i].slot);
QObject::connect(action, SIGNAL(changed()), m_mapper, SLOT(map()));
m_mapper->setMapping(action, i);
updateAction(i, newTab);
}
QObject::connect(newTab, SIGNAL(statusBarMessage(const QString&)),
m_mainWindow->statusBar(), SLOT(showMessage(const QString&)));
QObject::connect(newTab, SIGNAL(titleChanged(const QString&)),
m_mainWindow, SLOT(slotUpdateWindowTitle(const QString&)));
QObject::connect(newTab, SIGNAL(loadStarted()),
m_mainWindow, SLOT(slotLoadStarted()));
QObject::connect(newTab, SIGNAL(loadFinished()),
m_mainWindow, SLOT(slotLoadFinished()));
QObject::connect(newTab, SIGNAL(urlChanged(const QUrl &)),
m_mainWindow, SLOT(slotUrlChanged(const QUrl &)));
QObject::connect(newTab->page(), SIGNAL(linkHovered(const QString&, const QString&, const QString&)),
this, SIGNAL(linkHovered(const QString&)));
}
void BrowserTabProxy::update(WebView* newTab)
{
if (!newTab)
return;
m_mainWindow->replaceCurrentUrl(newTab->url());
m_mainWindow->statusBar()->showMessage(newTab->lastStausBarText());
m_mainWindow->slotUpdateWindowTitle(newTab->title());
newTab->loadIsFinished() ?
m_mainWindow->slotLoadFinished() :
m_mainWindow->slotLoadStarted();
}
BrowserTabProxy::WebActionConnection::WebActionConnection(const char* _slot, QAction** _action, QWebPage::WebAction _webAction)
: slot(_slot)
, action(_action)
, webAction(_webAction)
{}