diff options
author | Steve Block <steveblock@google.com> | 2009-12-15 10:12:09 +0000 |
---|---|---|
committer | Steve Block <steveblock@google.com> | 2009-12-17 17:41:10 +0000 |
commit | 643ca7872b450ea4efacab6188849e5aac2ba161 (patch) | |
tree | 6982576c228bcd1a7efe98afed544d840751094c /WebKit/qt/QtLauncher | |
parent | d026980fde6eb3b01c1fe49441174e89cd1be298 (diff) | |
download | external_webkit-643ca7872b450ea4efacab6188849e5aac2ba161.zip external_webkit-643ca7872b450ea4efacab6188849e5aac2ba161.tar.gz external_webkit-643ca7872b450ea4efacab6188849e5aac2ba161.tar.bz2 |
Merge webkit.org at r51976 : Initial merge by git.
Change-Id: Ib0e7e2f0fb4bee5a186610272edf3186f0986b43
Diffstat (limited to 'WebKit/qt/QtLauncher')
-rw-r--r-- | WebKit/qt/QtLauncher/main.cpp | 277 |
1 files changed, 195 insertions, 82 deletions
diff --git a/WebKit/qt/QtLauncher/main.cpp b/WebKit/qt/QtLauncher/main.cpp index e3c6116..2286712 100644 --- a/WebKit/qt/QtLauncher/main.cpp +++ b/WebKit/qt/QtLauncher/main.cpp @@ -30,18 +30,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <qwebpage.h> -#include <qwebview.h> -#include <qwebframe.h> -#include <qwebsettings.h> -#include <qwebplugindatabase.h> -#include <qwebelement.h> -#include <qwebinspector.h> - #include <QtGui> -#include <QDebug> #include <QtNetwork/QNetworkProxy> -#if QT_VERSION >= 0x040400 && !defined(QT_NO_PRINTER) +#include <QtNetwork/QNetworkRequest> +#if !defined(QT_NO_PRINTER) #include <QPrintPreviewDialog> #endif @@ -49,17 +41,78 @@ #include <QtUiTools/QUiLoader> #endif -#include <QVector> -#include <QTextStream> +#include <QDebug> #include <QFile> +#include <QTextStream> +#include <QVector> + #include <cstdio> +#include <qwebelement.h> +#include <qwebframe.h> +#include <qwebinspector.h> +#include <qwebpage.h> +#include <qwebsettings.h> +#include <qwebview.h> + #ifndef NDEBUG void QWEBKIT_EXPORT qt_drt_garbageCollector_collect(); #endif -class WebPage : public QWebPage +static QUrl urlFromUserInput(const QString& input) { +#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) + return QUrl::fromUserInput(input); +#else + return QUrl(input); +#endif +} + +class WebView : public QWebView { + Q_OBJECT +public: + WebView(QWidget* parent) : QWebView(parent) {} + +protected: + virtual void contextMenuEvent(QContextMenuEvent* event) + { + QMenu* menu = page()->createStandardContextMenu(); + + QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->pos()); + + if (!r.linkUrl().isEmpty()) { + QAction* newTabAction = menu->addAction(tr("Open in Default &Browser"), this, SLOT(openUrlInDefaultBrowser())); + newTabAction->setData(r.linkUrl()); + menu->insertAction(menu->actions().at(2), newTabAction); + } + + menu->exec(mapToGlobal(event->pos())); + delete menu; + } + + virtual void mousePressEvent(QMouseEvent* event) + { + mouseButtons = event->buttons(); + keyboardModifiers = event->modifiers(); + + QWebView::mousePressEvent(event); + } + +public slots: + void openUrlInDefaultBrowser(const QUrl &url = QUrl()) + { + if (QAction* action = qobject_cast<QAction*>(sender())) + QDesktopServices::openUrl(action->data().toUrl()); + else + QDesktopServices::openUrl(url); + } + +public: + Qt::MouseButtons mouseButtons; + Qt::KeyboardModifiers keyboardModifiers; +}; + +class WebPage : public QWebPage { public: WebPage(QWidget *parent) : QWebPage(parent) {} @@ -72,13 +125,49 @@ public: return false; } virtual bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output); + + + virtual bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type) + { + WebView* webView = static_cast<WebView*>(view()); + if (webView->keyboardModifiers & Qt::ShiftModifier) { + QWebPage* page = createWindow(QWebPage::WebBrowserWindow); + page->mainFrame()->load(request); + return false; + } + if (webView->keyboardModifiers & Qt::AltModifier) { + webView->openUrlInDefaultBrowser(request.url()); + return false; + } + + return QWebPage::acceptNavigationRequest(frame, request, type); + } }; -class MainWindow : public QMainWindow -{ +class WebInspector : public QWebInspector { Q_OBJECT public: - MainWindow(QString url = QString()): currentZoom(100) { + WebInspector(QWidget* parent) : QWebInspector(parent) {} +signals: + void visibleChanged(bool nowVisible); +protected: + void showEvent(QShowEvent* event) + { + QWebInspector::showEvent(event); + emit visibleChanged(true); + } + void hideEvent(QHideEvent* event) + { + QWebInspector::hideEvent(event); + emit visibleChanged(false); + } +}; + +class MainWindow : public QMainWindow { + Q_OBJECT +public: + MainWindow(QString url = QString()): currentZoom(100) + { setAttribute(Qt::WA_DeleteOnClose); if (qgetenv("QTLAUNCHER_USE_ARGB_VISUALS").toInt() == 1) setAttribute(Qt::WA_TranslucentBackground); @@ -86,7 +175,7 @@ public: QSplitter* splitter = new QSplitter(Qt::Vertical, this); setCentralWidget(splitter); - view = new QWebView(splitter); + view = new WebView(splitter); WebPage* page = new WebPage(view); view->setPage(page); connect(view, SIGNAL(loadFinished(bool)), @@ -97,16 +186,16 @@ public: this, SLOT(showLinkHover(const QString&, const QString&))); connect(view->page(), SIGNAL(windowCloseRequested()), this, SLOT(close())); - inspector = new QWebInspector(splitter); + inspector = new WebInspector(splitter); inspector->setPage(page); inspector->hide(); connect(this, SIGNAL(destroyed()), inspector, SLOT(deleteLater())); - connect(page, SIGNAL(webInspectorTriggered(const QWebElement&)), inspector, SLOT(show())); setupUI(); // set the proxy to the http_proxy env variable - if present - QUrl proxyUrl = view->guessUrlFromString(qgetenv("http_proxy")); + QUrl proxyUrl = urlFromUserInput(qgetenv("http_proxy")); + if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) { int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080; page->networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort)); @@ -116,11 +205,12 @@ public: if (fi.exists() && fi.isRelative()) url = fi.absoluteFilePath(); - QUrl qurl = view->guessUrlFromString(url); + QUrl qurl = urlFromUserInput(url); + if (qurl.scheme().isEmpty()) + qurl = QUrl("http://" + url + "/"); if (qurl.isValid()) { urlEdit->setText(qurl.toEncoded()); view->load(qurl); - } // the zoom values are chosen to be like in Mozilla Firefox 3 @@ -129,27 +219,33 @@ public: zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300; } - QWebPage* webPage() const { + QWebPage* webPage() const + { return view->page(); } - QWebView* webView() const { + QWebView* webView() const + { return view; } protected slots: - void changeLocation() { + void changeLocation() + { QString string = urlEdit->text(); - QUrl url = view->guessUrlFromString(string); - if (!url.isValid()) + QUrl url = urlFromUserInput(string); + if (url.scheme().isEmpty()) url = QUrl("http://" + string + "/"); - urlEdit->setText(url.toEncoded()); - view->load(url); - view->setFocus(Qt::OtherFocusReason); + if (url.isValid()) { + urlEdit->setText(url.toEncoded()); + view->load(url); + view->setFocus(Qt::OtherFocusReason); + } } - void loadFinished() { + void loadFinished() + { urlEdit->setText(view->url().toString()); QUrl::FormattingOptions opts; @@ -166,7 +262,8 @@ protected slots: urlModel.setStringList(urlList); } - void showLinkHover(const QString &link, const QString &toolTip) { + void showLinkHover(const QString &link, const QString &toolTip) + { statusBar()->showMessage(link); #ifndef QT_NO_TOOLTIP if (!toolTip.isEmpty()) @@ -174,22 +271,24 @@ protected slots: #endif } - void zoomIn() { + void zoomIn() + { int i = zoomLevels.indexOf(currentZoom); Q_ASSERT(i >= 0); if (i < zoomLevels.count() - 1) currentZoom = zoomLevels[i + 1]; - view->setZoomFactor(qreal(currentZoom)/100.0); + view->setZoomFactor(qreal(currentZoom) / 100.0); } - void zoomOut() { + void zoomOut() + { int i = zoomLevels.indexOf(currentZoom); Q_ASSERT(i >= 0); if (i > 0) currentZoom = zoomLevels[i - 1]; - view->setZoomFactor(qreal(currentZoom)/100.0); + view->setZoomFactor(qreal(currentZoom) / 100.0); } void resetZoom() @@ -203,8 +302,9 @@ protected slots: view->page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, b); } - void print() { -#if QT_VERSION >= 0x040400 && !defined(QT_NO_PRINTER) + void print() + { +#if !defined(QT_NO_PRINTER) QPrintPreviewDialog dlg(this); connect(&dlg, SIGNAL(paintRequested(QPrinter *)), view, SLOT(print(QPrinter *))); @@ -228,11 +328,13 @@ protected slots: } } - void setEditable(bool on) { + void setEditable(bool on) + { view->page()->setContentEditable(on); formatMenuAction->setVisible(on); } + /* void dumpPlugins() { QList<QWebPluginInfo> plugins = QWebSettings::pluginDatabase()->plugins(); foreach (const QWebPluginInfo plugin, plugins) { @@ -242,12 +344,15 @@ protected slots: } } } + */ - void dumpHtml() { + void dumpHtml() + { qDebug() << "HTML: " << view->page()->mainFrame()->toHtml(); } - void selectElements() { + void selectElements() + { bool ok; QString str = QInputDialog::getText(this, "Select elements", "Choose elements", QLineEdit::Normal, "a", &ok); @@ -262,8 +367,9 @@ protected slots: public slots: - void newWindow(const QString &url = QString()) { - MainWindow *mw = new MainWindow(url); + void newWindow(const QString &url = QString()) + { + MainWindow* mw = new MainWindow(url); mw->show(); } @@ -273,7 +379,8 @@ private: int currentZoom; // create the status bar, tool bar & menu - void setupUI() { + void setupUI() + { progress = new QProgressBar(this); progress->setRange(0, 100); progress->setMinimumSize(100, 20); @@ -289,26 +396,24 @@ private: urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy()); connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); - QCompleter *completer = new QCompleter(this); + QCompleter* completer = new QCompleter(this); urlEdit->setCompleter(completer); completer->setModel(&urlModel); - QToolBar *bar = addToolBar("Navigation"); + QToolBar* bar = addToolBar("Navigation"); bar->addAction(view->pageAction(QWebPage::Back)); bar->addAction(view->pageAction(QWebPage::Forward)); bar->addAction(view->pageAction(QWebPage::Reload)); bar->addAction(view->pageAction(QWebPage::Stop)); bar->addWidget(urlEdit); - QMenu *fileMenu = menuBar()->addMenu("&File"); - QAction *newWindow = fileMenu->addAction("New Window", this, SLOT(newWindow())); -#if QT_VERSION >= 0x040400 + QMenu* fileMenu = menuBar()->addMenu("&File"); + QAction* newWindow = fileMenu->addAction("New Window", this, SLOT(newWindow())); fileMenu->addAction(tr("Print"), this, SLOT(print()), QKeySequence::Print); -#endif QAction* screenshot = fileMenu->addAction("Screenshot", this, SLOT(screenshot())); fileMenu->addAction("Close", this, SLOT(close())); - QMenu *editMenu = menuBar()->addMenu("&Edit"); + QMenu* editMenu = menuBar()->addMenu("&Edit"); editMenu->addAction(view->pageAction(QWebPage::Undo)); editMenu->addAction(view->pageAction(QWebPage::Redo)); editMenu->addSeparator(); @@ -316,30 +421,30 @@ private: editMenu->addAction(view->pageAction(QWebPage::Copy)); editMenu->addAction(view->pageAction(QWebPage::Paste)); editMenu->addSeparator(); - QAction *setEditable = editMenu->addAction("Set Editable", this, SLOT(setEditable(bool))); + QAction* setEditable = editMenu->addAction("Set Editable", this, SLOT(setEditable(bool))); setEditable->setCheckable(true); - QMenu *viewMenu = menuBar()->addMenu("&View"); + QMenu* viewMenu = menuBar()->addMenu("&View"); viewMenu->addAction(view->pageAction(QWebPage::Stop)); viewMenu->addAction(view->pageAction(QWebPage::Reload)); viewMenu->addSeparator(); - QAction *zoomIn = viewMenu->addAction("Zoom &In", this, SLOT(zoomIn())); - QAction *zoomOut = viewMenu->addAction("Zoom &Out", this, SLOT(zoomOut())); - QAction *resetZoom = viewMenu->addAction("Reset Zoom", this, SLOT(resetZoom())); - QAction *zoomTextOnly = viewMenu->addAction("Zoom Text Only", this, SLOT(toggleZoomTextOnly(bool))); + QAction* zoomIn = viewMenu->addAction("Zoom &In", this, SLOT(zoomIn())); + QAction* zoomOut = viewMenu->addAction("Zoom &Out", this, SLOT(zoomOut())); + QAction* resetZoom = viewMenu->addAction("Reset Zoom", this, SLOT(resetZoom())); + QAction* zoomTextOnly = viewMenu->addAction("Zoom Text Only", this, SLOT(toggleZoomTextOnly(bool))); zoomTextOnly->setCheckable(true); zoomTextOnly->setChecked(false); viewMenu->addSeparator(); viewMenu->addAction("Dump HTML", this, SLOT(dumpHtml())); - viewMenu->addAction("Dump plugins", this, SLOT(dumpPlugins())); + //viewMenu->addAction("Dump plugins", this, SLOT(dumpPlugins())); - QMenu *formatMenu = new QMenu("F&ormat", this); + QMenu* formatMenu = new QMenu("F&ormat", this); formatMenuAction = menuBar()->addMenu(formatMenu); formatMenuAction->setVisible(false); formatMenu->addAction(view->pageAction(QWebPage::ToggleBold)); formatMenu->addAction(view->pageAction(QWebPage::ToggleItalic)); formatMenu->addAction(view->pageAction(QWebPage::ToggleUnderline)); - QMenu *writingMenu = formatMenu->addMenu(tr("Writing Direction")); + QMenu* writingMenu = formatMenu->addMenu(tr("Writing Direction")); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionDefault)); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionLeftToRight)); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionRightToLeft)); @@ -362,17 +467,21 @@ private: view->pageAction(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); view->pageAction(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U)); - QMenu *toolsMenu = menuBar()->addMenu("&Tools"); + QMenu* toolsMenu = menuBar()->addMenu("&Tools"); toolsMenu->addAction("Select elements...", this, SLOT(selectElements())); + QAction* showInspectorAction = toolsMenu->addAction("Show inspector", inspector, SLOT(setVisible(bool))); + showInspectorAction->setCheckable(true); + showInspectorAction->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("F12"))); + showInspectorAction->connect(inspector, SIGNAL(visibleChanged(bool)), SLOT(setChecked(bool))); } - QWebView *view; - QLineEdit *urlEdit; - QProgressBar *progress; - QWebInspector* inspector; + QWebView* view; + QLineEdit* urlEdit; + QProgressBar* progress; + WebInspector* inspector; - QAction *formatMenuAction; + QAction* formatMenuAction; QStringList urlList; QStringListModel urlModel; @@ -389,14 +498,14 @@ bool WebPage::extension(Extension extension, const ExtensionOption *option, Exte return true; } -QWebPage *WebPage::createWindow(QWebPage::WebWindowType) +QWebPage* WebPage::createWindow(QWebPage::WebWindowType) { - MainWindow *mw = new MainWindow; + MainWindow* mw = new MainWindow; mw->show(); return mw->webPage(); } -QObject *WebPage::createPlugin(const QString &classId, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues) +QObject* WebPage::createPlugin(const QString &classId, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues) { Q_UNUSED(url); Q_UNUSED(paramNames); @@ -410,8 +519,7 @@ QObject *WebPage::createPlugin(const QString &classId, const QUrl &url, const QS #endif } -class URLLoader : public QObject -{ +class URLLoader : public QObject { Q_OBJECT public: URLLoader(QWebView* view, const QString& inputFileName) @@ -492,14 +600,12 @@ int launcherMain(const QApplication& app) int main(int argc, char **argv) { QApplication app(argc, argv); - QString url = QString("%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html")); + QString defaultUrl = QString("file://%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html")); QWebSettings::setMaximumPagesInCache(4); app.setApplicationName("QtLauncher"); -#if QT_VERSION >= 0x040400 app.setApplicationVersion("0.1"); -#endif QWebSettings::setObjectCacheCapacities((16*1024*1024) / 8, (16*1024*1024) / 8, 16*1024*1024); @@ -521,21 +627,28 @@ int main(int argc, char **argv) exit(0); } MainWindow* window = new MainWindow; - QWebView *view = window->webView(); + QWebView* view = window->webView(); URLLoader loader(view, listFile); QObject::connect(view, SIGNAL(loadFinished(bool)), &loader, SLOT(loadNext())); loader.loadNext(); window->show(); launcherMain(app); } else { - if (args.count() > 1) - url = args.at(1); - - MainWindow* window = new MainWindow(url); + MainWindow* window = 0; + + // Look though the args for something we can open + for (int i = 1; i < args.count(); i++) { + if (!args.at(i).startsWith("-")) { + if (!window) + window = new MainWindow(args.at(i)); + else + window->newWindow(args.at(i)); + } + } - // Opens every given urls in new windows - for (int i = 2; i < args.count(); i++) - window->newWindow(args.at(i)); + // If not, just open the default URL + if (!window) + window = new MainWindow(defaultUrl); window->show(); launcherMain(app); |