summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2016-05-09 14:28:30 +0200
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2016-05-09 14:28:56 +0200
commitc727b8dea9df50ca9a08bff483128bba6263ac65 (patch)
treeb2e2b7c20ba400a33721ed86ca51a8da8e8b0d79 /examples
parentd364c05de52f9ab39034e56cac4e0a7981dc541d (diff)
parente3d94abf9356bb8ee6e402fb7b9813c96919faf2 (diff)
Merge remote-tracking branch 'origin/5.6' into 5.7
Diffstat (limited to 'examples')
-rw-r--r--examples/webenginewidgets/demobrowser/browserapplication.cpp29
-rw-r--r--examples/webenginewidgets/demobrowser/browserapplication.h1
-rw-r--r--examples/webenginewidgets/demobrowser/edittreeview.cpp7
-rw-r--r--examples/webenginewidgets/demobrowser/history.cpp99
-rw-r--r--examples/webenginewidgets/demobrowser/history.h13
-rw-r--r--examples/webenginewidgets/demobrowser/tabwidget.cpp3
6 files changed, 93 insertions, 59 deletions
diff --git a/examples/webenginewidgets/demobrowser/browserapplication.cpp b/examples/webenginewidgets/demobrowser/browserapplication.cpp
index 0d5c54199..26764b93c 100644
--- a/examples/webenginewidgets/demobrowser/browserapplication.cpp
+++ b/examples/webenginewidgets/demobrowser/browserapplication.cpp
@@ -136,11 +136,7 @@ BrowserApplication::BrowserApplication(int &argc, char **argv)
socket.connectToServer(serverName);
if (socket.waitForConnected(500)) {
QTextStream stream(&socket);
- QStringList args = QCoreApplication::arguments();
- if (args.count() > 1)
- stream << args.last();
- else
- stream << QString();
+ stream << getCommandLineUrlArgument();
stream.flush();
socket.waitForBytesWritten();
return;
@@ -255,11 +251,13 @@ void BrowserApplication::postLaunch()
// newMainWindow() needs to be called in main() for this to happen
if (m_mainWindows.count() > 0) {
- QStringList args = QCoreApplication::arguments();
- if (args.count() > 1)
- mainWindow()->loadPage(args.last());
- else
+ const QString url = getCommandLineUrlArgument();
+ if (!url.isEmpty()) {
+ mainWindow()->loadPage(url);
+ } else {
mainWindow()->slotHome();
+ }
+
}
BrowserApplication::historyManager();
}
@@ -421,6 +419,19 @@ void BrowserApplication::installTranslator(const QString &name)
QApplication::installTranslator(translator);
}
+QString BrowserApplication::getCommandLineUrlArgument() const
+{
+ const QStringList args = QCoreApplication::arguments();
+ if (args.count() > 1) {
+ const QString lastArg = args.last();
+ const bool isValidUrl = QUrl::fromUserInput(lastArg).isValid();
+ if (isValidUrl)
+ return lastArg;
+ }
+
+ return QString();
+}
+
#if defined(Q_OS_OSX)
bool BrowserApplication::event(QEvent* event)
{
diff --git a/examples/webenginewidgets/demobrowser/browserapplication.h b/examples/webenginewidgets/demobrowser/browserapplication.h
index f509c67f7..5c75d41b3 100644
--- a/examples/webenginewidgets/demobrowser/browserapplication.h
+++ b/examples/webenginewidgets/demobrowser/browserapplication.h
@@ -119,6 +119,7 @@ private slots:
private:
void clean();
void installTranslator(const QString &name);
+ QString getCommandLineUrlArgument() const;
static HistoryManager *s_historyManager;
static DownloadManager *s_downloadManager;
diff --git a/examples/webenginewidgets/demobrowser/edittreeview.cpp b/examples/webenginewidgets/demobrowser/edittreeview.cpp
index 763fbec5c..f4c9a0d70 100644
--- a/examples/webenginewidgets/demobrowser/edittreeview.cpp
+++ b/examples/webenginewidgets/demobrowser/edittreeview.cpp
@@ -49,6 +49,8 @@
****************************************************************************/
#include "edittreeview.h"
+#include "browserapplication.h"
+#include "history.h"
#include <QtGui/QKeyEvent>
@@ -73,13 +75,12 @@ void EditTreeView::removeOne()
if (!model())
return;
QModelIndex ci = currentIndex();
- int row = ci.row();
- model()->removeRow(row, ci.parent());
+ BrowserApplication::historyManager()->removeHistoryEntry(model()->data(ci,HistoryModel::UrlStringRole).toString());
}
void EditTreeView::removeAll()
{
if (!model())
return;
- model()->removeRows(0, model()->rowCount(rootIndex()), rootIndex());
+ BrowserApplication::historyManager()->clear();
}
diff --git a/examples/webenginewidgets/demobrowser/history.cpp b/examples/webenginewidgets/demobrowser/history.cpp
index aaab44ac8..188490aca 100644
--- a/examples/webenginewidgets/demobrowser/history.cpp
+++ b/examples/webenginewidgets/demobrowser/history.cpp
@@ -101,7 +101,7 @@ HistoryManager::~HistoryManager()
m_saveTimer->saveIfNeccessary();
}
-QList<HistoryItem> HistoryManager::history() const
+QList<HistoryItem> &HistoryManager::history()
{
return m_history;
}
@@ -120,6 +120,15 @@ void HistoryManager::addHistoryEntry(const QString &url)
addHistoryItem(item);
}
+void HistoryManager::removeHistoryEntry(const QString &url)
+{
+ QUrl cleanUrl(url);
+ cleanUrl.setPassword(QString());
+ cleanUrl.setHost(cleanUrl.host().toLower());
+ HistoryItem item(cleanUrl.toString(), QDateTime::currentDateTime());
+ removeHistoryItem(item);
+}
+
void HistoryManager::setHistory(const QList<HistoryItem> &history, bool loadedAndSorted)
{
m_history = history;
@@ -173,7 +182,7 @@ void HistoryManager::checkForExpired()
}
if (nextTimeout > 0)
break;
- HistoryItem item = m_history.takeLast();
+ const HistoryItem& item = m_history.last();
// remove from saved file also
m_lastSavedUrl = QString();
emit entryRemoved(item);
@@ -188,12 +197,21 @@ void HistoryManager::addHistoryItem(const HistoryItem &item)
if (BrowserApplication::instance()->privateBrowsing())
return;
- m_history.prepend(item);
emit entryAdded(item);
if (m_history.count() == 1)
checkForExpired();
}
+void HistoryManager::removeHistoryItem(const HistoryItem &item)
+{
+ for (int i = m_history.count() - 1 ; i >= 0; --i) {
+ if (item.url == m_history.at(i).url) {
+ //delete all related entries with that url
+ emit entryRemoved(m_history.at(i));
+ }
+ }
+}
+
void HistoryManager::updateHistoryItem(const QUrl &url, const QString &title)
{
for (int i = 0; i < m_history.count(); ++i) {
@@ -224,7 +242,6 @@ void HistoryManager::setHistoryLimit(int limit)
void HistoryManager::clear()
{
- m_history.clear();
m_lastSavedUrl = QString();
emit historyReset();
m_saveTimer->changeOccurred();
@@ -374,10 +391,10 @@ HistoryModel::HistoryModel(HistoryManager *history, QObject *parent)
connect(m_history, SIGNAL(historyReset()),
this, SLOT(historyReset()));
connect(m_history, SIGNAL(entryRemoved(HistoryItem)),
- this, SLOT(historyReset()));
+ this, SLOT(entryRemoved(HistoryItem)));
connect(m_history, SIGNAL(entryAdded(HistoryItem)),
- this, SLOT(entryAdded()));
+ this, SLOT(entryAdded(HistoryItem)));
connect(m_history, SIGNAL(entryUpdated(int)),
this, SLOT(entryUpdated(int)));
}
@@ -385,15 +402,26 @@ HistoryModel::HistoryModel(HistoryManager *history, QObject *parent)
void HistoryModel::historyReset()
{
beginResetModel();
+ m_history->history().clear();
endResetModel();
}
-void HistoryModel::entryAdded()
+void HistoryModel::entryAdded(const HistoryItem &item)
{
beginInsertRows(QModelIndex(), 0, 0);
+ m_history->history().prepend(item);
endInsertRows();
}
+void HistoryModel::entryRemoved(const HistoryItem &item)
+{
+ int index = m_history->history().indexOf(item);
+ Q_ASSERT(index > -1);
+ beginRemoveRows(QModelIndex(),index, index);
+ m_history->history().takeAt(index);
+ endRemoveRows();
+}
+
void HistoryModel::entryUpdated(int offset)
{
QModelIndex idx = index(offset, 0);
@@ -468,12 +496,9 @@ bool HistoryModel::removeRows(int row, int count, const QModelIndex &parent)
return false;
int lastRow = row + count - 1;
beginRemoveRows(parent, row, lastRow);
- QList<HistoryItem> lst = m_history->history();
+ QList<HistoryItem> &lst = m_history->history();
for (int i = lastRow; i >= row; --i)
lst.removeAt(i);
- disconnect(m_history, SIGNAL(historyReset()), this, SLOT(historyReset()));
- m_history->setHistory(lst);
- connect(m_history, SIGNAL(historyReset()), this, SLOT(historyReset()));
endRemoveRows();
return true;
}
@@ -664,8 +689,6 @@ TreeProxyModel::TreeProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
bool TreeProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
- if (!source_parent.isValid())
- return true;
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
@@ -678,14 +701,16 @@ HistoryDialog::HistoryDialog(QWidget *parent, HistoryManager *setHistory) : QDia
tree->setUniformRowHeights(true);
tree->setSelectionBehavior(QAbstractItemView::SelectRows);
tree->setTextElideMode(Qt::ElideMiddle);
- QAbstractItemModel *model = history->historyTreeModel();
+ QAbstractItemModel *model = history->historyFilterModel();
TreeProxyModel *proxyModel = new TreeProxyModel(this);
connect(search, SIGNAL(textChanged(QString)),
proxyModel, SLOT(setFilterFixedString(QString)));
connect(removeButton, SIGNAL(clicked()), tree, SLOT(removeOne()));
- connect(removeAllButton, SIGNAL(clicked()), history, SLOT(clear()));
+ connect(removeAllButton, SIGNAL(clicked()), tree, SLOT(removeAll()));
proxyModel->setSourceModel(model);
+ proxyModel->setFilterKeyColumn(1);
tree->setModel(proxyModel);
+ tree->setSortingEnabled(true);
tree->setExpanded(proxyModel->index(0, 0), true);
tree->setAlternatingRowColors(true);
QFontMetrics fm(font());
@@ -739,25 +764,13 @@ HistoryFilterModel::HistoryFilterModel(QAbstractItemModel *sourceModel, QObject
setSourceModel(sourceModel);
}
-int HistoryFilterModel::historyLocation(const QString &url) const
-{
- load();
- if (!m_historyHash.contains(url))
- return 0;
- return sourceModel()->rowCount() - m_historyHash.value(url);
-}
-
-QVariant HistoryFilterModel::data(const QModelIndex &index, int role) const
-{
- return QAbstractProxyModel::data(index, role);
-}
-
void HistoryFilterModel::setSourceModel(QAbstractItemModel *newSourceModel)
{
+ beginResetModel();
if (sourceModel()) {
disconnect(sourceModel(), SIGNAL(modelReset()), this, SLOT(sourceReset()));
disconnect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
- this, SLOT(dataChanged(QModelIndex,QModelIndex)));
+ this, SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
disconnect(sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
@@ -767,7 +780,6 @@ void HistoryFilterModel::setSourceModel(QAbstractItemModel *newSourceModel)
QAbstractProxyModel::setSourceModel(newSourceModel);
if (sourceModel()) {
- m_loaded = false;
connect(sourceModel(), SIGNAL(modelReset()), this, SLOT(sourceReset()));
connect(sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
@@ -776,6 +788,8 @@ void HistoryFilterModel::setSourceModel(QAbstractItemModel *newSourceModel)
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
}
+ load();
+ endResetModel();
}
void HistoryFilterModel::sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
@@ -790,14 +804,13 @@ QVariant HistoryFilterModel::headerData(int section, Qt::Orientation orientation
void HistoryFilterModel::sourceReset()
{
- m_loaded = false;
beginResetModel();
+ load();
endResetModel();
}
int HistoryFilterModel::rowCount(const QModelIndex &parent) const
{
- load();
if (parent.isValid())
return 0;
return m_historyHash.count();
@@ -810,14 +823,12 @@ int HistoryFilterModel::columnCount(const QModelIndex &parent) const
QModelIndex HistoryFilterModel::mapToSource(const QModelIndex &proxyIndex) const
{
- load();
int sourceRow = sourceModel()->rowCount() - proxyIndex.internalId();
return sourceModel()->index(sourceRow, proxyIndex.column());
}
QModelIndex HistoryFilterModel::mapFromSource(const QModelIndex &sourceIndex) const
{
- load();
QString url = sourceIndex.data(HistoryModel::UrlStringRole).toString();
if (!m_historyHash.contains(url))
return QModelIndex();
@@ -843,7 +854,6 @@ QModelIndex HistoryFilterModel::mapFromSource(const QModelIndex &sourceIndex) co
QModelIndex HistoryFilterModel::index(int row, int column, const QModelIndex &parent) const
{
- load();
if (row < 0 || row >= rowCount(parent)
|| column < 0 || column >= columnCount(parent))
return QModelIndex();
@@ -858,8 +868,6 @@ QModelIndex HistoryFilterModel::parent(const QModelIndex &) const
void HistoryFilterModel::load() const
{
- if (m_loaded)
- return;
m_sourceRow.clear();
m_historyHash.clear();
m_historyHash.reserve(sourceModel()->rowCount());
@@ -871,15 +879,12 @@ void HistoryFilterModel::load() const
m_historyHash[url] = sourceModel()->rowCount() - i;
}
}
- m_loaded = true;
}
void HistoryFilterModel::sourceRowsInserted(const QModelIndex &parent, int start, int end)
{
Q_ASSERT(start == end && start == 0);
Q_UNUSED(end);
- if (!m_loaded)
- return;
QModelIndex idx = sourceModel()->index(start, 0, parent);
QString url = idx.data(HistoryModel::UrlStringRole).toString();
if (m_historyHash.contains(url)) {
@@ -1184,6 +1189,7 @@ bool HistoryTreeModel::removeRows(int row, int count, const QModelIndex &parent)
void HistoryTreeModel::setSourceModel(QAbstractItemModel *newSourceModel)
{
+ beginResetModel();
if (sourceModel()) {
disconnect(sourceModel(), SIGNAL(modelReset()), this, SLOT(sourceReset()));
disconnect(sourceModel(), SIGNAL(layoutChanged()), this, SLOT(sourceReset()));
@@ -1191,6 +1197,8 @@ void HistoryTreeModel::setSourceModel(QAbstractItemModel *newSourceModel)
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
+ disconnect(sourceModel(), &QAbstractItemModel::dataChanged, this,
+ &HistoryTreeModel::sourceDataChanged);
}
QAbstractProxyModel::setSourceModel(newSourceModel);
@@ -1202,9 +1210,9 @@ void HistoryTreeModel::setSourceModel(QAbstractItemModel *newSourceModel)
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
+ connect(sourceModel(), &QAbstractItemModel::dataChanged, this,
+ &HistoryTreeModel::sourceDataChanged);
}
-
- beginResetModel();
endResetModel();
}
@@ -1293,3 +1301,10 @@ void HistoryTreeModel::sourceRowsRemoved(const QModelIndex &parent, int start, i
endRemoveRows();
}
}
+
+void HistoryTreeModel::sourceDataChanged(const QModelIndex &topLeft,
+ const QModelIndex &bottomRight,
+ const QVector<int> roles)
+{
+ emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles);
+}
diff --git a/examples/webenginewidgets/demobrowser/history.h b/examples/webenginewidgets/demobrowser/history.h
index 0ae5a7bf7..6d7da5e6d 100644
--- a/examples/webenginewidgets/demobrowser/history.h
+++ b/examples/webenginewidgets/demobrowser/history.h
@@ -103,14 +103,16 @@ public:
~HistoryManager();
bool historyContains(const QString &url) const;
+
void addHistoryEntry(const QString &url);
+ void removeHistoryEntry(const QString &url);
void updateHistoryItem(const QUrl &url, const QString &title);
int historyLimit() const;
void setHistoryLimit(int limit);
- QList<HistoryItem> history() const;
+ QList<HistoryItem>& history();
void setHistory(const QList<HistoryItem> &history, bool loadedAndSorted = false);
// History manager keeps around these models for use by the completer and other classes
@@ -128,6 +130,7 @@ private slots:
protected:
void addHistoryItem(const HistoryItem &item);
+ void removeHistoryItem(const HistoryItem &item);
private:
void load();
@@ -149,7 +152,8 @@ class HistoryModel : public QAbstractTableModel
public slots:
void historyReset();
- void entryAdded();
+ void entryAdded(const HistoryItem &item);
+ void entryRemoved(const HistoryItem &item);
void entryUpdated(int offset);
public:
@@ -185,7 +189,6 @@ public:
inline bool historyContains(const QString &url) const
{ load(); return m_historyHash.contains(url); }
- int historyLocation(const QString &url) const;
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
@@ -196,7 +199,6 @@ public:
QModelIndex index(int, int, const QModelIndex& = QModelIndex()) const;
QModelIndex parent(const QModelIndex& index= QModelIndex()) const;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
- QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
private slots:
void sourceReset();
@@ -314,7 +316,8 @@ private slots:
void sourceReset();
void sourceRowsInserted(const QModelIndex &parent, int start, int end);
void sourceRowsRemoved(const QModelIndex &parent, int start, int end);
-
+ void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
+ const QVector<int> roles);
private:
int sourceDateRow(int row) const;
mutable QList<int> m_sourceRowCache;
diff --git a/examples/webenginewidgets/demobrowser/tabwidget.cpp b/examples/webenginewidgets/demobrowser/tabwidget.cpp
index 3b56d115b..e684d3757 100644
--- a/examples/webenginewidgets/demobrowser/tabwidget.cpp
+++ b/examples/webenginewidgets/demobrowser/tabwidget.cpp
@@ -782,6 +782,9 @@ void TabWidget::webViewUrlChanged(const QUrl &url)
int index = webViewIndex(webView);
if (-1 != index) {
m_tabBar->setTabData(index, url);
+ HistoryManager *manager = BrowserApplication::historyManager();
+ if (url.isValid())
+ manager->addHistoryEntry(url.toString());
}
emit tabsChanged();
}