From 2f758a7763960118305234b0cda4226747b56b14 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 24 Feb 2014 12:49:05 +0100 Subject: Polish the webbrowser example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update address edit when the browser finishes loading. Add some rudimentary bookmark/history functionality. Add relevant default bookmarks and make qt-project.org the start page. Handle URLs passed on the command line. Change-Id: I21efeb5b4d2a05941407d308a96df7b024100f19 Reviewed-by: Topi Reiniƶ --- .../activeqt/webbrowser/doc/src/webbrowser.qdoc | 4 +- examples/activeqt/webbrowser/main.cpp | 163 ++++++++++++++++++++- examples/activeqt/webbrowser/mainwindow.ui | 30 +++- .../webbrowser/mainwindow_windowsmobile.ui | 30 +++- 4 files changed, 216 insertions(+), 11 deletions(-) diff --git a/examples/activeqt/webbrowser/doc/src/webbrowser.qdoc b/examples/activeqt/webbrowser/doc/src/webbrowser.qdoc index be1ff34..07c2ef7 100644 --- a/examples/activeqt/webbrowser/doc/src/webbrowser.qdoc +++ b/examples/activeqt/webbrowser/doc/src/webbrowser.qdoc @@ -51,9 +51,7 @@ \snippet activeqt/webbrowser/main.cpp 1 The constructor initializes the user interface, installs a - progress bar on the status bar, and uses QAxBase::dynamicCall() - to invoke the \c GoHome() method of Internet Explorer to - navigate to the user's home page. + progress bar on the status bar, and loads the bookmarks. \snippet activeqt/webbrowser/main.cpp 2 Different slots handle the signals emitted by the WebBrowser object. diff --git a/examples/activeqt/webbrowser/main.cpp b/examples/activeqt/webbrowser/main.cpp index e65526d..76470bc 100644 --- a/examples/activeqt/webbrowser/main.cpp +++ b/examples/activeqt/webbrowser/main.cpp @@ -46,6 +46,9 @@ #include #include #include +#include +#include +#include #if defined(Q_WS_WINCE_WM) #include "ui_mainwindow_windowsmobile.h" @@ -54,29 +57,107 @@ #include "ui_mainwindow.h" #endif +static const char qtProjectUrl[] = "qt-project.org"; +static const char iWebBrowser2DocumentationUrl[] = "http://msdn.microsoft.com/en-us/library/aa752127%28v=vs.85%29.aspx"; + +struct Location { + Location(const QString &t = QString(), const QString &a = QString()) : title(t), address(a) {} + + QString title; + QString address; +}; + +Q_DECLARE_METATYPE(Location) + +static QList defaultBookmarks() +{ + QList result; + result.append(Location(QStringLiteral("Qt Project"), QLatin1String(qtProjectUrl))); + result.append(Location(QStringLiteral("Digia"), QStringLiteral("http://qt.digia.com/"))); + result.append(Location(QStringLiteral("IWebBrowser2 MSDN Documentation"), QLatin1String(iWebBrowser2DocumentationUrl))); + return result; +} + +static bool containsAddress(const QList &locations, const QString &address) +{ + foreach (const Location &location, locations) { + if (location.address == address) + return true; + } + return false; +} + +static inline Location locationFromAction(const QAction *action) +{ + return action->data().value(); +} + +static QList readBookMarks(QSettings &settings) +{ + QList result; + if (const int count = settings.beginReadArray(QStringLiteral("Bookmarks"))) { + const QString titleKey = QStringLiteral("title"); + const QString addressKey = QStringLiteral("address"); + for (int i = 0; i < count; ++i) { + settings.setArrayIndex(i); + result.append(Location(settings.value(titleKey).toString(), + settings.value(addressKey).toString())); + } + } + settings.endArray(); + return result; +} + +static void saveBookMarks(const QList &bookmarks, QSettings &settings) +{ + const int count = bookmarks.size(); + settings.beginWriteArray(QStringLiteral("Bookmarks")); + const QString titleKey = QStringLiteral("title"); + const QString addressKey = QStringLiteral("address"); + for (int i = 0; i < count; ++i) { + settings.setArrayIndex(i); + settings.setValue(titleKey, bookmarks.at(i).title); + settings.setValue(addressKey, bookmarks.at(i).address); + } + settings.endArray(); +} + //! [0] class MainWindow : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: MainWindow(); + ~MainWindow(); public slots: + void navigate(const QString &address); void on_WebBrowser_TitleChange(const QString &title); void on_WebBrowser_ProgressChange(int a, int b); void on_WebBrowser_CommandStateChange(int cmd, bool on); void on_WebBrowser_BeforeNavigate(); - void on_WebBrowser_NavigateComplete(QString); + void on_WebBrowser_NavigateComplete(const QString &address); void on_actionGo_triggered(); void on_actionNewWindow_triggered(); + void on_actionAddBookmark_triggered(); void on_actionAbout_triggered(); void on_actionAboutQt_triggered(); void on_actionFileClose_triggered(); private: + inline const QString address() const + { return addressEdit->text().trimmed(); } + QList bookmarks() const; + QAction *addLocation(const Location &location, QMenu *menu); + inline void addBookmark(const Location &location) + { bookmarkActions << addLocation(location, BookmarksMenu); } + QProgressBar *pb; QLineEdit *addressEdit; + QList bookmarkActions; + QList historyActions; + QSignalMapper locationActionMapper; }; //! [0] //! [1] @@ -101,13 +182,66 @@ MainWindow::MainWindow() pb->hide(); statusBar()->addPermanentWidget(pb); - WebBrowser->dynamicCall("GoHome()"); + connect(&locationActionMapper, SIGNAL(mapped(QString)), this, SLOT(navigate(QString))); + + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + QCoreApplication::organizationName(), QCoreApplication::applicationName()); + QList bookmarks = readBookMarks(settings); + if (bookmarks.isEmpty()) + bookmarks = defaultBookmarks(); + foreach (const Location &bookmark, bookmarks) + addBookmark(bookmark); +} + +//! [1] + +MainWindow::~MainWindow() +{ + QSettings settings(QSettings::IniFormat, QSettings::UserScope, + QCoreApplication::organizationName(), QCoreApplication::applicationName()); + saveBookMarks(bookmarks(), settings); +} + +QAction *MainWindow::addLocation(const Location &location, QMenu *menu) +{ + QAction *action = menu->addAction(location.title); + action->setData(QVariant::fromValue(location)); + locationActionMapper.setMapping(action, location.address); + connect(action, SIGNAL(triggered()), &locationActionMapper, SLOT(map())); + return action; +} + +QList MainWindow::bookmarks() const +{ + QList result; + foreach (const QAction *action, bookmarkActions) + result.append(locationFromAction(action)); + return result; +} + +void MainWindow::on_actionAddBookmark_triggered() +{ + if (!historyActions.isEmpty()) { + const Location location = locationFromAction(historyActions.last()); + if (!containsAddress(bookmarks(), location.address)) + addBookmark(location); + } } -//! [1] //! [2] +//! [2] void MainWindow::on_WebBrowser_TitleChange(const QString &title) { + // This is called multiple times after NavigateComplete(). + // Add new URLs to history here. setWindowTitle("Qt WebBrowser - " + title); + const QString currentAddress = address(); + const QString historyAddress = historyActions.isEmpty() ? + QString() : locationFromAction(historyActions.last()).address; + if (currentAddress.isEmpty() || currentAddress == "about:blank" || currentAddress == historyAddress) + return; + historyActions << addLocation(Location(title, currentAddress), HistoryMenu); + if (historyActions.size() > 10) + delete historyActions.takeFirst(); } void MainWindow::on_WebBrowser_ProgressChange(int a, int b) @@ -138,15 +272,25 @@ void MainWindow::on_WebBrowser_BeforeNavigate() actionStop->setEnabled(true); } -void MainWindow::on_WebBrowser_NavigateComplete(QString) +void MainWindow::on_WebBrowser_NavigateComplete(const QString &url) { actionStop->setEnabled(false); + const bool blocked = addressEdit->blockSignals(true); + addressEdit->setText(url); + addressEdit->blockSignals(blocked); } -//! [2] //! [3] +//! [3] void MainWindow::on_actionGo_triggered() { - WebBrowser->dynamicCall("Navigate(const QString&)", addressEdit->text()); + navigate(address()); +} + +//! [2] + +void MainWindow::navigate(const QString &url) +{ + WebBrowser->dynamicCall("Navigate(const QString&)", url); } void MainWindow::on_actionNewWindow_triggered() @@ -184,7 +328,14 @@ void MainWindow::on_actionFileClose_triggered() int main(int argc, char ** argv) { QApplication a(argc, argv); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCoreApplication::setApplicationName("Active Qt Web Browser"); + QCoreApplication::setOrganizationName("QtProject"); MainWindow w; + const QStringList arguments = QCoreApplication::arguments(); + const QString url = arguments.size() > 1 ? + arguments.at(1) : QString::fromLatin1(qtProjectUrl); + w.navigate(url); #if defined(Q_OS_WINCE) w.showMaximized(); #else diff --git a/examples/activeqt/webbrowser/mainwindow.ui b/examples/activeqt/webbrowser/mainwindow.ui index 9af0f8e..0117771 100644 --- a/examples/activeqt/webbrowser/mainwindow.ui +++ b/examples/activeqt/webbrowser/mainwindow.ui @@ -114,7 +114,25 @@ - + + + BookmarksMenu + + + &Bookmarks + + + + + + + HistoryMenu + + + Hi&story + + + unnamed @@ -125,6 +143,8 @@ + + @@ -226,6 +246,14 @@ About + + + actionAddBookmark + + + Add Bookmark + + actionAboutQt diff --git a/examples/activeqt/webbrowser/mainwindow_windowsmobile.ui b/examples/activeqt/webbrowser/mainwindow_windowsmobile.ui index 768dc43..29cdc6d 100644 --- a/examples/activeqt/webbrowser/mainwindow_windowsmobile.ui +++ b/examples/activeqt/webbrowser/mainwindow_windowsmobile.ui @@ -114,7 +114,25 @@ - + + + BookmarksMenu + + + &Bookmarks + + + + + + + HistoryMenu + + + Hi&story + + + unnamed @@ -125,6 +143,8 @@ + + @@ -226,6 +246,14 @@ About + + + actionAddBookmark + + + Add Bookmark + + actionAboutQt -- cgit v1.2.3