1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
45 #include "modelmenu.h"
47 #include <QtCore/QDateTime>
48 #include <QtCore/QHash>
49 #include <QtCore/QObject>
50 #include <QtCore/QTimer>
51 #include <QtCore/QUrl>
53 #include <QtGui/QSortFilterProxyModel>
55 #include <QWebHistoryInterface>
61 HistoryItem(const QString
&u
,
62 const QDateTime
&d
= QDateTime(), const QString
&t
= QString())
63 : title(t
), url(u
), dateTime(d
) {}
65 inline bool operator==(const HistoryItem
&other
) const
66 { return other
.title
== title
67 && other
.url
== url
&& other
.dateTime
== dateTime
; }
69 // history is sorted in reverse
70 inline bool operator <(const HistoryItem
&other
) const
71 { return dateTime
> other
.dateTime
; }
80 class HistoryFilterModel
;
81 class HistoryTreeModel
;
82 class HistoryManager
: public QWebHistoryInterface
85 Q_PROPERTY(int historyLimit READ historyLimit WRITE setHistoryLimit
)
89 void entryAdded(const HistoryItem
&item
);
90 void entryRemoved(const HistoryItem
&item
);
91 void entryUpdated(int offset
);
94 HistoryManager(QObject
*parent
= 0);
97 bool historyContains(const QString
&url
) const;
98 void addHistoryEntry(const QString
&url
);
100 void updateHistoryItem(const QUrl
&url
, const QString
&title
);
102 int historyLimit() const;
103 void setHistoryLimit(int limit
);
105 QList
<HistoryItem
> history() const;
106 void setHistory(const QList
<HistoryItem
> &history
, bool loadedAndSorted
= false);
108 // History manager keeps around these models for use by the completer and other classes
109 HistoryModel
*historyModel() const;
110 HistoryFilterModel
*historyFilterModel() const;
111 HistoryTreeModel
*historyTreeModel() const;
119 void checkForExpired();
122 void addHistoryItem(const HistoryItem
&item
);
127 AutoSaver
*m_saveTimer
;
129 QTimer m_expiredTimer
;
130 QList
<HistoryItem
> m_history
;
131 QString m_lastSavedUrl
;
133 HistoryModel
*m_historyModel
;
134 HistoryFilterModel
*m_historyFilterModel
;
135 HistoryTreeModel
*m_historyTreeModel
;
138 class HistoryModel
: public QAbstractTableModel
145 void entryUpdated(int offset
);
149 DateRole
= Qt::UserRole
+ 1,
150 DateTimeRole
= Qt::UserRole
+ 2,
151 UrlRole
= Qt::UserRole
+ 3,
152 UrlStringRole
= Qt::UserRole
+ 4
155 HistoryModel(HistoryManager
*history
, QObject
*parent
= 0);
156 QVariant
headerData(int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const;
157 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
158 int columnCount(const QModelIndex
&parent
= QModelIndex()) const;
159 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
160 bool removeRows(int row
, int count
, const QModelIndex
&parent
= QModelIndex());
163 HistoryManager
*m_history
;
167 Proxy model that will remove any duplicate entries.
168 Both m_sourceRow and m_historyHash store their offsets not from
169 the front of the list, but as offsets from the back.
171 class HistoryFilterModel
: public QAbstractProxyModel
176 HistoryFilterModel(QAbstractItemModel
*sourceModel
, QObject
*parent
= 0);
178 inline bool historyContains(const QString
&url
) const
179 { load(); return m_historyHash
.contains(url
); }
180 int historyLocation(const QString
&url
) const;
182 QModelIndex
mapFromSource(const QModelIndex
&sourceIndex
) const;
183 QModelIndex
mapToSource(const QModelIndex
&proxyIndex
) const;
184 void setSourceModel(QAbstractItemModel
*sourceModel
);
185 QVariant
headerData(int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const;
186 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
187 int columnCount(const QModelIndex
&parent
= QModelIndex()) const;
188 QModelIndex
index(int, int, const QModelIndex
& = QModelIndex()) const;
189 QModelIndex
parent(const QModelIndex
& index
= QModelIndex()) const;
190 bool removeRows(int row
, int count
, const QModelIndex
&parent
= QModelIndex());
191 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
195 void sourceDataChanged(const QModelIndex
&topLeft
, const QModelIndex
&bottomRight
);
196 void sourceRowsInserted(const QModelIndex
&parent
, int start
, int end
);
197 void sourceRowsRemoved(const QModelIndex
&, int, int);
202 mutable QList
<int> m_sourceRow
;
203 mutable QHash
<QString
, int> m_historyHash
;
204 mutable bool m_loaded
;
209 - Removes the first twenty entries and puts them as children of the top level.
210 - If there are less then twenty entries then the first folder is also removed.
212 The mapping is done by knowing that HistoryTreeModel is over a table
213 We store that row offset in our index's private data.
215 class HistoryMenuModel
: public QAbstractProxyModel
220 HistoryMenuModel(HistoryTreeModel
*sourceModel
, QObject
*parent
= 0);
221 int columnCount(const QModelIndex
&parent
) const;
222 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
223 QModelIndex
mapFromSource(const QModelIndex
& sourceIndex
) const;
224 QModelIndex
mapToSource(const QModelIndex
& proxyIndex
) const;
225 QModelIndex
index(int, int, const QModelIndex
&parent
= QModelIndex()) const;
226 QModelIndex
parent(const QModelIndex
&index
= QModelIndex()) const;
228 int bumpedRows() const;
231 HistoryTreeModel
*m_treeModel
;
234 // Menu that is dynamically populated from the history
235 class HistoryMenu
: public ModelMenu
240 void openUrl(const QUrl
&url
);
243 HistoryMenu(QWidget
*parent
= 0);
244 void setInitialActions(QList
<QAction
*> actions
);
248 void postPopulated();
251 void activated(const QModelIndex
&index
);
252 void showHistoryDialog();
255 HistoryManager
*m_history
;
256 HistoryMenuModel
*m_historyMenuModel
;
257 QList
<QAction
*> m_initialActions
;
260 // proxy model for the history model that
261 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
262 class HistoryCompletionModel
: public QAbstractProxyModel
267 HistoryCompletionModel(QObject
*parent
= 0);
268 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
269 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
270 int columnCount(const QModelIndex
&parent
= QModelIndex()) const;
271 QModelIndex
mapFromSource(const QModelIndex
&sourceIndex
) const;
272 QModelIndex
mapToSource(const QModelIndex
&proxyIndex
) const;
273 QModelIndex
index(int, int, const QModelIndex
& = QModelIndex()) const;
274 QModelIndex
parent(const QModelIndex
& index
= QModelIndex()) const;
275 void setSourceModel(QAbstractItemModel
*sourceModel
);
282 // proxy model for the history model that converts the list
283 // into a tree, one top level node per day.
284 // Used in the HistoryDialog.
285 class HistoryTreeModel
: public QAbstractProxyModel
290 HistoryTreeModel(QAbstractItemModel
*sourceModel
, QObject
*parent
= 0);
291 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
292 int columnCount(const QModelIndex
&parent
) const;
293 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
294 QModelIndex
mapFromSource(const QModelIndex
&sourceIndex
) const;
295 QModelIndex
mapToSource(const QModelIndex
&proxyIndex
) const;
296 QModelIndex
index(int row
, int column
, const QModelIndex
&parent
= QModelIndex()) const;
297 QModelIndex
parent(const QModelIndex
&index
= QModelIndex()) const;
298 bool hasChildren(const QModelIndex
&parent
= QModelIndex()) const;
299 Qt::ItemFlags
flags(const QModelIndex
&index
) const;
300 bool removeRows(int row
, int count
, const QModelIndex
&parent
= QModelIndex());
301 QVariant
headerData(int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const;
303 void setSourceModel(QAbstractItemModel
*sourceModel
);
307 void sourceRowsInserted(const QModelIndex
&parent
, int start
, int end
);
308 void sourceRowsRemoved(const QModelIndex
&parent
, int start
, int end
);
311 int sourceDateRow(int row
) const;
312 mutable QList
<int> m_sourceRowCache
;
316 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
317 // so filtering is only done on the children.
318 // Used in the HistoryDialog
319 class TreeProxyModel
: public QSortFilterProxyModel
324 TreeProxyModel(QObject
*parent
= 0);
327 bool filterAcceptsRow(int source_row
, const QModelIndex
&source_parent
) const;
330 #include "ui_history.h"
332 class HistoryDialog
: public QDialog
, public Ui_HistoryDialog
337 void openUrl(const QUrl
&url
);
340 HistoryDialog(QWidget
*parent
= 0, HistoryManager
*history
= 0);
343 void customContextMenuRequested(const QPoint
&pos
);