From cdb864f35e765cfef374cce18583fffff57359a3 Mon Sep 17 00:00:00 2001 From: Pierre Rossi <pierre.rossi@digia.com> Date: Tue, 18 Jun 2013 17:29:21 +0200 Subject: [PATCH] Startup URL from command line args --- examples/common.pri | 5 ++- examples/common/util.h | 72 +++++++++++++++++++++++++++++++ examples/qtquick/quickwindow.cpp | 11 ++--- examples/qtquick/quickwindow.qml | 2 +- examples/widgets/widgetwindow.cpp | 4 +- 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 examples/common/util.h diff --git a/examples/common.pri b/examples/common.pri index b85159ce8..4a1b676f2 100644 --- a/examples/common.pri +++ b/examples/common.pri @@ -1,7 +1,10 @@ -INCLUDEPATH += $$absolute_path(../lib, $$PWD) +INCLUDEPATH += $$absolute_path(../lib, $$PWD) \ + $$absolute_path(common, $$PWD) LIBPATH = $$getOutDir()/$$getConfigDir()/lib +HEADERS += common/util.h + LIBS += -L$$LIBPATH -lQt5WebEngine QMAKE_RPATHDIR += $$LIBPATH diff --git a/examples/common/util.h b/examples/common/util.h new file mode 100644 index 000000000..7db6e2cd5 --- /dev/null +++ b/examples/common/util.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef UTIL_H +#define UTIL_H + +#include <QtCore/QCoreApplication> +#include <QtCore/QFileInfo> +#include <QtCore/QUrl> + +QUrl urlFromUserInput(const QString& userInput) +{ + QFileInfo fileInfo(userInput); + if (fileInfo.exists()) + return QUrl(fileInfo.absoluteFilePath()); + return QUrl::fromUserInput(userInput); +} + +QUrl startupUrl() +{ + QUrl ret; + QStringList args(qApp->arguments()); + args.takeFirst(); + Q_FOREACH(const QString& arg, args) { + if (arg.startsWith(QLatin1Char('-'))) + continue; + ret = urlFromUserInput(arg); + if (ret.isValid()) + return ret; + } + return QUrl(QStringLiteral("http://qt-project.org/")); +} + + +#endif // UTIL_H diff --git a/examples/qtquick/quickwindow.cpp b/examples/qtquick/quickwindow.cpp index 6dd11cc11..1f753c691 100644 --- a/examples/qtquick/quickwindow.cpp +++ b/examples/qtquick/quickwindow.cpp @@ -41,6 +41,8 @@ #include "quickwindow.h" +#include "util.h" + #include <QFileInfo> #include <QObject> #include <QQmlContext> @@ -51,13 +53,8 @@ class Utils : public QObject { Q_OBJECT public: Utils(QObject* parent = 0) : QObject(parent) { } - Q_INVOKABLE static QUrl fromUserInput(const QString& userInput) - { - QFileInfo fileInfo(userInput); - if (fileInfo.exists()) - return QUrl(fileInfo.absoluteFilePath()); - return QUrl::fromUserInput(userInput); - } + Q_INVOKABLE static QUrl fromUserInput(const QString& userInput) { return urlFromUserInput(userInput); } + Q_INVOKABLE static QUrl initialUrl() { return startupUrl(); } }; #include "quickwindow.moc" diff --git a/examples/qtquick/quickwindow.qml b/examples/qtquick/quickwindow.qml index 7d42b2c01..7962be141 100644 --- a/examples/qtquick/quickwindow.qml +++ b/examples/qtquick/quickwindow.qml @@ -47,7 +47,7 @@ ApplicationWindow { id: webContentsView focus: true anchors.fill: parent - url: "http://qt-project.org/" + url: utils.initialUrl() onUrlChanged: addressBar.text = url } diff --git a/examples/widgets/widgetwindow.cpp b/examples/widgets/widgetwindow.cpp index dad64cb7f..589ef13c3 100644 --- a/examples/widgets/widgetwindow.cpp +++ b/examples/widgets/widgetwindow.cpp @@ -42,6 +42,7 @@ #include "widgetwindow.h" #include "qwebcontentsview.h" +#include "util.h" static const int margin = 1; @@ -91,7 +92,7 @@ WidgetWindow::WidgetWindow() connect(m_webView.data(), SIGNAL(titleChanged(const QString&)), SLOT(setWindowTitle(const QString&))); connect(m_webView.data(), SIGNAL(urlChanged(const QUrl&)), SLOT(setAddressBarUrl(const QUrl&))); - m_webView->load(QUrl(QStringLiteral("http://qt-project.org/"))); + m_webView->load(startupUrl()); } WidgetWindow::~WidgetWindow() @@ -115,5 +116,6 @@ void WidgetWindow::loadStarted() void WidgetWindow::loadFinished(bool success) { + Q_UNUSED(success); reloadButton->setIcon(QIcon::fromTheme("view-refresh")); } -- GitLab