45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module history;
|
|
42
|
|
43
|
|
44 import QtCore.QBuffer;
|
|
45 import QtCore.QDir;
|
|
46 import QtCore.QFile;
|
|
47 import QtCore.QFileInfo;
|
|
48 import QtCore.QSettings;
|
|
49 import QtCore.QTemporaryFile;
|
|
50 import QtCore.QTextStream;
|
|
51
|
|
52 import QtCore.QtAlgorithms;
|
|
53
|
|
54 import QtGui.QClipboard;
|
|
55 import QtGui.QDesktopServices;
|
|
56 import QtGui.QHeaderView;
|
|
57 import QtGui.QStyle;
|
|
58
|
|
59 import QtWebKit.QWebHistoryInterface;
|
|
60 import QtWebKit.QWebSettings;
|
|
61
|
|
62 import QtCore.QDebug;
|
|
63
|
|
64 import QtCore.QDateTime;
|
|
65 import QtCore.QHash;
|
|
66 import QtCore.QObject;
|
|
67 import QtCore.QTimer;
|
|
68 import QtCore.QUrl;
|
|
69
|
|
70 import QtGui.QSortFilterProxyModel;
|
|
71
|
|
72 import QWebHistoryInterface;
|
|
73
|
65
|
74
|
|
75 import autosaver
|
|
76 import browserapplication;
|
|
77 import modelmenu;
|
|
78
|
|
79
|
45
|
80 static const unsigned int HISTORY_VERSION = 23;
|
|
81
|
|
82
|
|
83 class HistoryItem
|
|
84 {
|
|
85 public:
|
65
|
86
|
|
87 this() {}
|
45
|
88
|
65
|
89 this(QString u, QDateTime d = QDateTime(), QString t = QString())
|
|
90 {
|
|
91 title = t;
|
|
92 url = u;
|
|
93 dateTime = d;
|
45
|
94 }
|
|
95
|
65
|
96 //bool operator==(HistoryItem &other)
|
|
97 int opEquals(HistoryItem other)
|
|
98 {
|
|
99 return cast(int) (other.title == title && other.url == url && other.dateTime == dateTime);
|
|
100 }
|
45
|
101
|
65
|
102 // history is sorted in reverse
|
|
103 //bool operator <(HistoryItem &other)
|
|
104 int opCmp(HistoryItem other)
|
|
105 {
|
|
106 return cast(int) (dateTime > other.dateTime);
|
|
107 }
|
|
108
|
|
109 QString title;
|
|
110 QString url;
|
|
111 QDateTime dateTime;
|
45
|
112 }
|
|
113
|
|
114
|
|
115 class HistoryManager : public QWebHistoryInterface
|
|
116 {
|
65
|
117 //Q_PROPERTY(int historyLimit READ historyLimit WRITE setHistoryLimit)
|
45
|
118
|
65
|
119 mixin Signal!("historyReset");
|
|
120 mixin Signal!("entryAdded", HistoryItem item);
|
|
121 mixin Signal!("entryRemoved", HistoryItem item);
|
|
122 mixin Signal!("entryUpdated", int offset);
|
45
|
123
|
|
124 public:
|
|
125
|
65
|
126 this(QObject parent = null)
|
45
|
127 {
|
|
128 super(parent);
|
|
129 m_saveTimer = new AutoSaver(this);
|
|
130 m_historyLimit = 30;
|
|
131 m_historyModel = 0;
|
|
132 m_historyFilterModel = 0;
|
|
133 m_historyTreeModel = 0;
|
|
134
|
|
135 m_expiredTimer.setSingleShot(true);
|
65
|
136
|
|
137 m_expiredTimer.timeout.connect(&this.checkForExpired);
|
|
138 this.entryAdded.connect(&m_saveTimer.changeOccurred);
|
|
139 this.entryRemoved.connect(&m_saveTimer.changeOccurred);
|
|
140
|
45
|
141 load();
|
|
142
|
|
143 m_historyModel = new HistoryModel(this, this);
|
|
144 m_historyFilterModel = new HistoryFilterModel(m_historyModel, this);
|
|
145 m_historyTreeModel = new HistoryTreeModel(m_historyFilterModel, this);
|
|
146
|
|
147 // QWebHistoryInterface will delete the history manager
|
65
|
148 QWebHistoryInterface.setDefaultInterface(this);
|
45
|
149 }
|
|
150
|
|
151
|
|
152 ~this()
|
|
153 {
|
|
154 m_saveTimer.saveIfNeccessary();
|
|
155 }
|
|
156
|
|
157
|
65
|
158 bool historyContains(QString &url)
|
45
|
159 {
|
|
160 return m_historyFilterModel.historyContains(url);
|
|
161 }
|
|
162
|
|
163 void addHistoryEntry(QString &url)
|
|
164 {
|
|
165 QUrl cleanUrl(url);
|
|
166 cleanUrl.setPassword(QString());
|
|
167 cleanUrl.setHost(cleanUrl.host().toLower());
|
65
|
168 auto item = new HistoryItem(cleanUrl.toString(), QDateTime.currentDateTime());
|
45
|
169 addHistoryItem(item);
|
|
170 }
|
|
171
|
|
172 void updateHistoryItem(QUrl &url, QString &title)
|
|
173 {
|
|
174 for (int i = 0; i < m_history.count(); ++i) {
|
|
175 if (url == m_history.at(i).url) {
|
|
176 m_history[i].title = title;
|
|
177 m_saveTimer.changeOccurred();
|
|
178 if (m_lastSavedUrl.isEmpty())
|
|
179 m_lastSavedUrl = m_history.at(i).url;
|
|
180 emit entryUpdated(i);
|
|
181 break;
|
|
182 }
|
|
183 }
|
|
184 }
|
|
185
|
|
186 int historyLimit()
|
|
187 {
|
|
188 return m_historyLimit;
|
|
189 }
|
|
190
|
|
191
|
|
192 void setHistoryLimit(int limit)
|
|
193 {
|
|
194 if (m_historyLimit == limit)
|
|
195 return;
|
|
196 m_historyLimit = limit;
|
|
197 checkForExpired();
|
|
198 m_saveTimer.changeOccurred();
|
|
199 }
|
|
200
|
|
201
|
|
202 QList<HistoryItem> history()
|
|
203 {
|
|
204 return m_history;
|
|
205 }
|
|
206
|
|
207
|
|
208 void setHistory(QList<HistoryItem> &history, bool loadedAndSorted = false);
|
|
209 {
|
|
210 m_history = history;
|
|
211
|
|
212 // verify that it is sorted by date
|
|
213 if (!loadedAndSorted)
|
|
214 qSort(m_history.begin(), m_history.end());
|
|
215
|
|
216 checkForExpired();
|
|
217
|
|
218 if (loadedAndSorted) {
|
|
219 m_lastSavedUrl = m_history.value(0).url;
|
|
220 } else {
|
|
221 m_lastSavedUrl = QString();
|
|
222 m_saveTimer.changeOccurred();
|
|
223 }
|
|
224 emit historyReset();
|
|
225 }
|
|
226
|
|
227
|
|
228 // History manager keeps around these models for use by the completer and other classes
|
65
|
229 HistoryModel historyModel();
|
45
|
230 {
|
|
231 return m_historyModel;
|
|
232 }
|
|
233
|
|
234
|
65
|
235 HistoryFilterModel historyFilterModel()
|
45
|
236 {
|
|
237 return m_historyFilterModel;
|
|
238 }
|
|
239
|
|
240
|
65
|
241 HistoryTreeModel historyTreeModel()
|
45
|
242 {
|
|
243 return m_historyTreeModel;
|
|
244 }
|
|
245
|
|
246
|
|
247 public slots:
|
|
248
|
|
249 void clear()
|
|
250 {
|
|
251 m_history.clear();
|
|
252 m_lastSavedUrl = QString();
|
|
253 m_saveTimer.changeOccurred();
|
|
254 m_saveTimer.saveIfNeccessary();
|
|
255 historyReset();
|
|
256 }
|
|
257
|
|
258 void loadSettings()
|
|
259 {
|
|
260 // load settings
|
|
261 QSettings settings;
|
|
262 settings.beginGroup(QLatin1String("history"));
|
|
263 m_historyLimit = settings.value(QLatin1String("historyLimit"), 30).toInt();
|
|
264 }
|
|
265
|
65
|
266 private:
|
|
267
|
|
268 void save()
|
|
269 {
|
|
270 QSettings settings;
|
|
271 settings.beginGroup(QLatin1String("history"));
|
|
272 settings.setValue(QLatin1String("historyLimit"), m_historyLimit);
|
45
|
273
|
65
|
274 bool saveAll = m_lastSavedUrl.isEmpty();
|
|
275 int first = m_history.count() - 1;
|
|
276 if (!saveAll) {
|
|
277 // find the first one to save
|
|
278 for (int i = 0; i < m_history.count(); ++i) {
|
|
279 if (m_history.at(i).url == m_lastSavedUrl) {
|
|
280 first = i - 1;
|
|
281 break;
|
|
282 }
|
|
283 }
|
|
284 }
|
|
285 if (first == m_history.count() - 1)
|
|
286 saveAll = true;
|
45
|
287
|
65
|
288 QString directory = QDesktopServices.storageLocation(QDesktopServices.DataLocation);
|
|
289 if (directory.isEmpty())
|
|
290 directory = QDir.homePath() + QLatin1String("/.") + QCoreApplication.applicationName();
|
|
291 if (!QFile.exists(directory)) {
|
|
292 QDir dir;
|
|
293 dir.mkpath(directory);
|
|
294 }
|
45
|
295
|
65
|
296 QFile historyFile(directory + QLatin1String("/history"));
|
|
297 // When saving everything use a temporary file to prevent possible data loss.
|
|
298 QTemporaryFile tempFile;
|
|
299 tempFile.setAutoRemove(false);
|
|
300 bool open = false;
|
|
301 if (saveAll) {
|
|
302 open = tempFile.open();
|
|
303 } else {
|
|
304 open = historyFile.open(QFile.Append);
|
|
305 }
|
45
|
306
|
65
|
307 if (!open) {
|
|
308 qWarning() << "Unable to open history file for saving"
|
|
309 << (saveAll ? tempFile.fileName() : historyFile.fileName());
|
|
310 return;
|
|
311 }
|
45
|
312
|
65
|
313 QDataStream out_ = (saveAll ? tempFile : historyFile);
|
|
314 for (int i = first; i >= 0; --i) {
|
|
315 QByteArray data;
|
|
316 auto stream = new QDataStream(data, QIODevice.WriteOnly);
|
|
317 HistoryItem item = m_history.at(i);
|
|
318 stream << HISTORY_VERSION << item.url << item.dateTime << item.title;
|
|
319 out_ << data;
|
|
320 }
|
|
321 tempFile.close();
|
45
|
322
|
65
|
323 if (saveAll) {
|
|
324 if (historyFile.exists() && !historyFile.remove())
|
|
325 qWarning() << "History: error removing old history." << historyFile.errorString();
|
|
326 if (!tempFile.rename(historyFile.fileName()))
|
|
327 qWarning() << "History: error moving new history over old." << tempFile.errorString() << historyFile.fileName();
|
|
328 }
|
|
329 m_lastSavedUrl = m_history.value(0).url;
|
|
330 }
|
45
|
331
|
65
|
332 void checkForExpired()
|
|
333 {
|
|
334 if (m_historyLimit < 0 || m_history.isEmpty())
|
|
335 return;
|
45
|
336
|
65
|
337 QDateTime now = QDateTime.currentDateTime();
|
|
338 int nextTimeout = 0;
|
45
|
339
|
65
|
340 while (!m_history.isEmpty()) {
|
|
341 QDateTime checkForExpired = m_history.last().dateTime;
|
|
342 checkForExpired.setDate(checkForExpired.date().addDays(m_historyLimit));
|
|
343 if (now.daysTo(checkForExpired) > 7) {
|
|
344 // check at most in a week to prevent int overflows on the timer
|
|
345 nextTimeout = 7 * 86400;
|
|
346 } else {
|
|
347 nextTimeout = now.secsTo(checkForExpired);
|
|
348 }
|
|
349 if (nextTimeout > 0)
|
|
350 break;
|
|
351 HistoryItem item = m_history.takeLast();
|
|
352 // remove from saved file also
|
|
353 m_lastSavedUrl = QString();
|
|
354 emit entryRemoved(item);
|
|
355 }
|
45
|
356
|
65
|
357 if (nextTimeout > 0)
|
|
358 m_expiredTimer.start(nextTimeout * 1000);
|
|
359 }
|
45
|
360
|
|
361 protected:
|
|
362
|
65
|
363 void addHistoryItem(HistoryItem item)
|
|
364 {
|
|
365 QWebSettings globalSettings = QWebSettings.globalSettings();
|
|
366 if (globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled))
|
|
367 return;
|
|
368
|
|
369 m_history.prepend(item);
|
|
370 emit entryAdded(item);
|
|
371 if (m_history.count() == 1)
|
|
372 checkForExpired();
|
|
373 }
|
45
|
374
|
|
375 private:
|
|
376
|
65
|
377 void load()
|
|
378 {
|
|
379 loadSettings();
|
45
|
380
|
65
|
381 historyFile = new QFile(QDesktopServices.storageLocation(QDesktopServices.DataLocation) + QLatin1String("/history"));
|
|
382 if (!historyFile.exists())
|
|
383 return;
|
|
384 if (!historyFile.open(QFile.ReadOnly)) {
|
|
385 qWarning() << "Unable to open history file" << historyFile.fileName();
|
|
386 return;
|
|
387 }
|
45
|
388
|
65
|
389 QList<HistoryItem> list;
|
|
390 auto in_ = new QDataStream(&historyFile);
|
|
391 // Double check that the history file is sorted as it is read in
|
|
392 bool needToSort = false;
|
|
393 HistoryItem lastInsertedItem;
|
|
394 QByteArray data;
|
|
395 QDataStream stream;
|
|
396 QBuffer buffer;
|
|
397 stream.setDevice(buffer);
|
|
398 while (!historyFile.atEnd()) {
|
|
399 in_ >> data;
|
|
400 buffer.close();
|
|
401 buffer.setBuffer(data);
|
|
402 buffer.open(QIODevice.ReadOnly);
|
|
403 quint32 ver;
|
|
404 stream >> ver;
|
|
405 if (ver != HISTORY_VERSION)
|
|
406 continue;
|
|
407 HistoryItem item;
|
|
408 stream >> item.url;
|
|
409 stream >> item.dateTime;
|
|
410 stream >> item.title;
|
45
|
411
|
65
|
412 if (!item.dateTime.isValid())
|
|
413 continue;
|
45
|
414
|
65
|
415 if (item == lastInsertedItem) {
|
|
416 if (lastInsertedItem.title.isEmpty() && !list.isEmpty())
|
|
417 list[0].title = item.title;
|
|
418 continue;
|
|
419 }
|
45
|
420
|
65
|
421 if (!needToSort && !list.isEmpty() && lastInsertedItem < item)
|
|
422 needToSort = true;
|
45
|
423
|
65
|
424 list.prepend(item);
|
|
425 lastInsertedItem = item;
|
|
426 }
|
|
427
|
|
428 if (needToSort)
|
|
429 qSort(list.begin(), list.end());
|
45
|
430
|
65
|
431 setHistory(list, true);
|
45
|
432
|
65
|
433 // If we had to sort re-write the whole history sorted
|
|
434 if (needToSort) {
|
|
435 m_lastSavedUrl = QString();
|
|
436 m_saveTimer.changeOccurred();
|
|
437 }
|
|
438 }
|
45
|
439
|
65
|
440 AutoSaver m_saveTimer;
|
|
441 int m_historyLimit;
|
|
442 QTimer m_expiredTimer;
|
|
443 QList<HistoryItem> m_history;
|
|
444 QString m_lastSavedUrl;
|
45
|
445
|
65
|
446 HistoryModel m_historyModel;
|
|
447 HistoryFilterModel m_historyFilterModel;
|
|
448 HistoryTreeModel m_historyTreeModel;
|
|
449 }
|
45
|
450
|
|
451 class HistoryModel : public QAbstractTableModel
|
|
452 {
|
65
|
453 public:
|
45
|
454
|
65
|
455 void historyReset()
|
|
456 {
|
|
457 reset();
|
|
458 }
|
45
|
459
|
65
|
460 void entryAdded()
|
|
461 {
|
|
462 beginInsertRows(QModelIndex(), 0, 0);
|
|
463 endInsertRows();
|
|
464 }
|
45
|
465
|
65
|
466 void entryUpdated(int offset)
|
|
467 {
|
|
468 QModelIndex idx = index(offset, 0);
|
|
469 emit dataChanged(idx, idx);
|
|
470 }
|
45
|
471
|
|
472 public:
|
|
473
|
|
474 enum Roles {
|
|
475 DateRole = Qt.UserRole + 1,
|
|
476 DateTimeRole = Qt.UserRole + 2,
|
|
477 UrlRole = Qt.UserRole + 3,
|
|
478 UrlStringRole = Qt.UserRole + 4
|
65
|
479 }
|
45
|
480
|
65
|
481 this(HistoryManager history, QObject parent = null)
|
45
|
482 {
|
|
483 super(parent);
|
|
484 m_history = history;
|
|
485
|
65
|
486 assert(m_history);
|
|
487
|
|
488 m_history.historyReset.connect(&this.historyReset);
|
|
489 m_history.entryRemoved.connect(&this.historyReset);
|
|
490 m_history.entryAdded.connect(&this.entryAdded);
|
|
491 m_history.entryUpdated.connect(&this.entryUpdated);
|
45
|
492 }
|
|
493
|
|
494 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
|
495 {
|
|
496 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) {
|
|
497 switch (section) {
|
|
498 case 0: return tr("Title");
|
|
499 case 1: return tr("Address");
|
|
500 }
|
|
501 }
|
65
|
502 return QAbstractTableModel.headerData(section, orientation, role);
|
45
|
503 }
|
|
504
|
65
|
505 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
506 {
|
|
507 QList<HistoryItem> lst = m_history.history();
|
|
508 if (index.row() < 0 || index.row() >= lst.size())
|
|
509 return QVariant();
|
|
510
|
65
|
511 HistoryItem item = lst.at(index.row());
|
45
|
512 switch (role) {
|
|
513 case DateTimeRole:
|
|
514 return item.dateTime;
|
|
515 case DateRole:
|
|
516 return item.dateTime.date();
|
|
517 case UrlRole:
|
|
518 return QUrl(item.url);
|
|
519 case UrlStringRole:
|
|
520 return item.url;
|
|
521 case Qt.DisplayRole:
|
|
522 case Qt.EditRole: {
|
|
523 switch (index.column()) {
|
|
524 case 0:
|
|
525 // when there is no title try to generate one from the url
|
|
526 if (item.title.isEmpty()) {
|
|
527 QString page = QFileInfo(QUrl(item.url).path()).fileName();
|
|
528 if (!page.isEmpty())
|
|
529 return page;
|
|
530 return item.url;
|
|
531 }
|
|
532 return item.title;
|
|
533 case 1:
|
|
534 return item.url;
|
|
535 }
|
|
536 }
|
|
537 case Qt.DecorationRole:
|
|
538 if (index.column() == 0) {
|
65
|
539 return BrowserApplication.instance().icon(item.url);
|
45
|
540 }
|
|
541 }
|
|
542 return QVariant();
|
|
543 }
|
|
544
|
65
|
545 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
546 {
|
|
547 return (parent.isValid()) ? 0 : 2;
|
|
548 }
|
|
549
|
65
|
550 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
551 {
|
|
552 return (parent.isValid()) ? 0 : m_history.history().count();
|
|
553 }
|
|
554
|
65
|
555 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
45
|
556 {
|
|
557 if (parent.isValid())
|
|
558 return false;
|
|
559 int lastRow = row + count - 1;
|
|
560 beginRemoveRows(parent, row, lastRow);
|
|
561 QList<HistoryItem> lst = m_history.history();
|
|
562 for (int i = lastRow; i >= row; --i)
|
|
563 lst.removeAt(i);
|
65
|
564 m_history.historyReset.disconnect(&this.historyReset);
|
45
|
565 m_history.setHistory(lst);
|
65
|
566 m_history.historyReset.connect(&this.historyReset);
|
45
|
567 endRemoveRows();
|
|
568 return true;
|
|
569 }
|
|
570
|
|
571
|
|
572 private:
|
65
|
573 HistoryManager m_history;
|
45
|
574 }
|
|
575
|
|
576 const uint MOVEDROWS = 15;
|
|
577
|
|
578 /*!
|
65
|
579 Proxy model that will remove any duplicate entries.
|
|
580 Both m_sourceRow and m_historyHash store their offsets not from
|
|
581 the front of the list, but as offsets from the back.
|
|
582 */
|
45
|
583 class HistoryFilterModel : public QAbstractProxyModel
|
|
584 {
|
|
585 public:
|
|
586
|
65
|
587 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
588 {
|
|
589 super(parent);
|
|
590 m_loaded = false;
|
|
591 setSourceModel(sourceModel);
|
|
592 }
|
|
593
|
65
|
594 bool historyContains(QString url)
|
45
|
595 {
|
|
596 load();
|
|
597 return m_historyHash.contains(url);
|
|
598 }
|
|
599
|
65
|
600 int historyLocation(QString url);
|
|
601 {
|
|
602 load();
|
|
603 if (!m_historyHash.contains(url))
|
|
604 return 0;
|
|
605 return sourceModel().rowCount() - m_historyHash.value(url);
|
|
606 }
|
45
|
607
|
65
|
608 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
|
609 {
|
|
610 load();
|
|
611 QString url = sourceIndex.data(HistoryModel.UrlStringRole).toString();
|
|
612 if (!m_historyHash.contains(url))
|
|
613 return QModelIndex();
|
45
|
614
|
65
|
615 // This can be done in a binary search, but we can't use qBinary find
|
|
616 // because it can't take: qBinaryFind(m_sourceRow.end(), m_sourceRow.begin(), v);
|
|
617 // so if this is a performance bottlneck then convert to binary search, until then
|
|
618 // the cleaner/easier to read code wins the day.
|
|
619 int realRow = -1;
|
|
620 int sourceModelRow = sourceModel().rowCount() - sourceIndex.row();
|
45
|
621
|
65
|
622 for (int i = 0; i < m_sourceRow.count(); ++i) {
|
|
623 if (m_sourceRow.at(i) == sourceModelRow) {
|
|
624 realRow = i;
|
|
625 break;
|
|
626 }
|
|
627 }
|
|
628 if (realRow == -1)
|
|
629 return QModelIndex();
|
45
|
630
|
65
|
631 return createIndex(realRow, sourceIndex.column(), sourceModel().rowCount() - sourceIndex.row());
|
|
632 }
|
45
|
633
|
|
634
|
65
|
635 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
636 {
|
|
637 load();
|
|
638 int sourceRow = sourceModel().rowCount() - proxyIndex.internalId();
|
|
639 return sourceModel().index(sourceRow, proxyIndex.column());
|
|
640 }
|
|
641
|
65
|
642 void setSourceModel(QAbstractItemModel newSourceModel)
|
45
|
643 {
|
|
644 if (sourceModel()) {
|
65
|
645 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
646 sourceModel.dataChanged.disconnect(&this.dataChanged);
|
|
647 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
648 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
45
|
649 }
|
|
650
|
65
|
651 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
652
|
|
653 if (sourceModel()) {
|
|
654 m_loaded = false;
|
65
|
655 sourceModel.modelReset.connect(&this.sourceReset);
|
|
656 sourceModel.dataChanged.connect(&this.sourceDataChanged);
|
|
657 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
658 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
659 }
|
|
660 }
|
|
661
|
|
662 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole);
|
|
663 {
|
|
664 return sourceModel().headerData(section, orientation, role);
|
|
665 }
|
|
666
|
65
|
667 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
668 {
|
|
669 load();
|
|
670 if (parent.isValid())
|
|
671 return 0;
|
|
672 return m_historyHash.count();
|
|
673 }
|
|
674
|
65
|
675 int columnCount(QModelIndex parent = QModelIndex());
|
45
|
676 {
|
|
677 return (parent.isValid()) ? 0 : 2;
|
|
678 }
|
|
679
|
65
|
680 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
681 {
|
|
682 load();
|
|
683 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
|
684 return QModelIndex();
|
|
685
|
|
686 return createIndex(row, column, m_sourceRow[row]);
|
|
687 }
|
|
688
|
65
|
689 QModelIndex parent(QModelIndex index= QModelIndex())
|
45
|
690 {
|
|
691 return QModelIndex();
|
|
692 }
|
|
693
|
65
|
694 /*
|
|
695 Removing a continuous block of rows will remove filtered rows too as this is
|
|
696 the users intention.
|
|
697 */
|
|
698 bool removeRows(int row, int count, QModelIndex parent = QModelIndex());
|
45
|
699 {
|
|
700 if (row < 0 || count <= 0 || row + count > rowCount(parent) || parent.isValid())
|
|
701 return false;
|
|
702 int lastRow = row + count - 1;
|
65
|
703 sourceModel.rowsRemoved,disconnect(&this.sourceRowsRemoved);
|
45
|
704 beginRemoveRows(parent, row, lastRow);
|
|
705 int oldCount = rowCount();
|
|
706 int start = sourceModel().rowCount() - m_sourceRow.value(row);
|
|
707 int end = sourceModel().rowCount() - m_sourceRow.value(lastRow);
|
|
708 sourceModel().removeRows(start, end - start + 1);
|
|
709 endRemoveRows();
|
65
|
710 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
45
|
711 m_loaded = false;
|
|
712 if (oldCount - count != rowCount())
|
|
713 reset();
|
|
714 return true;
|
|
715 }
|
|
716
|
|
717
|
65
|
718 QVariant data(QModelIndex index, int role = Qt.DisplayRole);
|
45
|
719 {
|
65
|
720 return QAbstractProxyModel.data(index, role);
|
45
|
721 }
|
|
722
|
|
723 private slots:
|
|
724
|
|
725 void sourceReset()
|
|
726 {
|
|
727 m_loaded = false;
|
|
728 reset();
|
|
729 }
|
|
730
|
|
731
|
65
|
732 void sourceDataChanged(QModelIndex topLeft, QModelIndex bottomRight)
|
45
|
733 {
|
|
734 emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
|
|
735 }
|
|
736
|
65
|
737 void sourceRowsRemoved(QModelIndex , int start, int end)
|
45
|
738 {
|
65
|
739 //Q_UNUSED(start);
|
|
740 //Q_UNUSED(end);
|
45
|
741 sourceReset();
|
|
742 }
|
|
743
|
|
744
|
65
|
745 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
746 {
|
65
|
747 assert(start == end && start == 0);
|
|
748 //Q_UNUSED(end);
|
45
|
749 if (!m_loaded)
|
|
750 return;
|
|
751 QModelIndex idx = sourceModel().index(start, 0, parent);
|
|
752 QString url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
753 if (m_historyHash.contains(url)) {
|
|
754 int sourceRow = sourceModel().rowCount() - m_historyHash[url];
|
|
755 int realRow = mapFromSource(sourceModel().index(sourceRow, 0)).row();
|
|
756 beginRemoveRows(QModelIndex(), realRow, realRow);
|
|
757 m_sourceRow.removeAt(realRow);
|
|
758 m_historyHash.remove(url);
|
|
759 endRemoveRows();
|
|
760 }
|
|
761 beginInsertRows(QModelIndex(), 0, 0);
|
|
762 m_historyHash.insert(url, sourceModel().rowCount() - start);
|
|
763 m_sourceRow.insert(0, sourceModel().rowCount());
|
|
764 endInsertRows();
|
|
765 }
|
|
766
|
|
767 private:
|
|
768 void load()
|
|
769 {
|
|
770 if (m_loaded)
|
|
771 return;
|
|
772 m_sourceRow.clear();
|
|
773 m_historyHash.clear();
|
|
774 m_historyHash.reserve(sourceModel().rowCount());
|
|
775 for (int i = 0; i < sourceModel().rowCount(); ++i) {
|
|
776 QModelIndex idx = sourceModel().index(i, 0);
|
|
777 QString url = idx.data(HistoryModel.UrlStringRole).toString();
|
|
778 if (!m_historyHash.contains(url)) {
|
|
779 m_sourceRow.append(sourceModel().rowCount() - i);
|
|
780 m_historyHash[url] = sourceModel().rowCount() - i;
|
|
781 }
|
|
782 }
|
|
783 m_loaded = true;
|
|
784 }
|
|
785
|
|
786
|
65
|
787 QList<int> m_sourceRow;
|
|
788 QHash<QString, int> m_historyHash;
|
|
789 bool m_loaded;
|
45
|
790 }
|
|
791
|
|
792 /*
|
65
|
793 The history menu
|
|
794 - Removes the first twenty entries and puts them as children of the top level.
|
|
795 - If there are less then twenty entries then the first folder is also removed.
|
45
|
796
|
65
|
797 The mapping is done by knowing that HistoryTreeModel is over a table
|
|
798 We store that row offset in our index's private data.
|
45
|
799 */
|
|
800 class HistoryMenuModel : public QAbstractProxyModel
|
|
801 {
|
|
802 public:
|
|
803
|
65
|
804 /*
|
|
805 Maps the first bunch of items of the source model to the root
|
|
806 */
|
|
807 HistoryMenuModel(HistoryTreeModel sourceModel, QObject parent = null)
|
|
808 {
|
|
809 super(parent);
|
|
810 m_treeModel = sourceModel;
|
|
811 setSourceModel(sourceModel);
|
|
812 }
|
45
|
813
|
65
|
814 int columnCount(QModelIndex parent)
|
|
815 {
|
|
816 return m_treeModel.columnCount(mapToSource(parent));
|
|
817 }
|
45
|
818
|
65
|
819 int rowCount(QModelIndex parent = QModelIndex());
|
45
|
820 {
|
|
821 if (parent.column() > 0)
|
|
822 return 0;
|
|
823
|
|
824 if (!parent.isValid()) {
|
|
825 int folders = sourceModel().rowCount();
|
|
826 int bumpedItems = bumpedRows();
|
|
827 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
828 --folders;
|
|
829 return bumpedItems + folders;
|
|
830 }
|
|
831
|
|
832 if (parent.internalId() == -1) {
|
|
833 if (parent.row() < bumpedRows())
|
|
834 return 0;
|
|
835 }
|
|
836
|
|
837 QModelIndex idx = mapToSource(parent);
|
|
838 int defaultCount = sourceModel().rowCount(idx);
|
|
839 if (idx == sourceModel().index(0, 0))
|
|
840 return defaultCount - bumpedRows();
|
|
841 return defaultCount;
|
|
842 }
|
|
843
|
65
|
844 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
845 {
|
|
846 // currently not used or autotested
|
65
|
847 assert(false);
|
45
|
848 int sr = m_treeModel.mapToSource(sourceIndex).row();
|
|
849 return createIndex(sourceIndex.row(), sourceIndex.column(), sr);
|
|
850 }
|
|
851
|
65
|
852 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
853 {
|
|
854 if (!proxyIndex.isValid())
|
|
855 return QModelIndex();
|
|
856
|
|
857 if (proxyIndex.internalId() == -1) {
|
|
858 int bumpedItems = bumpedRows();
|
|
859 if (proxyIndex.row() < bumpedItems)
|
|
860 return m_treeModel.index(proxyIndex.row(), proxyIndex.column(), m_treeModel.index(0, 0));
|
|
861 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(m_treeModel.index(0, 0)))
|
|
862 --bumpedItems;
|
|
863 return m_treeModel.index(proxyIndex.row() - bumpedItems, proxyIndex.column());
|
|
864 }
|
|
865
|
|
866 QModelIndex historyIndex = m_treeModel.sourceModel().index(proxyIndex.internalId(), proxyIndex.column());
|
|
867 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
868 return treeIndex;
|
|
869 }
|
|
870
|
|
871
|
|
872 QModelIndex index(int, int, QModelIndex &parent = QModelIndex());
|
|
873 {
|
65
|
874 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
45
|
875 return QModelIndex();
|
65
|
876
|
45
|
877 if (!parent.isValid())
|
|
878 return createIndex(row, column, -1);
|
|
879
|
|
880 QModelIndex treeIndexParent = mapToSource(parent);
|
|
881
|
|
882 int bumpedItems = 0;
|
|
883 if (treeIndexParent == m_treeModel.index(0, 0))
|
|
884 bumpedItems = bumpedRows();
|
|
885 QModelIndex treeIndex = m_treeModel.index(row + bumpedItems, column, treeIndexParent);
|
|
886 QModelIndex historyIndex = m_treeModel.mapToSource(treeIndex);
|
|
887 int historyRow = historyIndex.row();
|
|
888 if (historyRow == -1)
|
|
889 historyRow = treeIndex.row();
|
|
890 return createIndex(row, column, historyRow);
|
|
891 }
|
|
892
|
65
|
893 QModelIndex parent(QModelIndex index = QModelIndex());
|
45
|
894 {
|
|
895 int offset = index.internalId();
|
|
896 if (offset == -1 || !index.isValid())
|
|
897 return QModelIndex();
|
|
898
|
|
899 QModelIndex historyIndex = m_treeModel.sourceModel().index(index.internalId(), 0);
|
|
900 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
901 QModelIndex treeIndexParent = treeIndex.parent();
|
|
902
|
|
903 int sr = m_treeModel.mapToSource(treeIndexParent).row();
|
|
904 int bumpedItems = bumpedRows();
|
|
905 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
906 --bumpedItems;
|
|
907 return createIndex(bumpedItems + treeIndexParent.row(), treeIndexParent.column(), sr);
|
|
908 }
|
|
909
|
|
910 int bumpedRows()
|
|
911 {
|
|
912 QModelIndex first = m_treeModel.index(0, 0);
|
|
913 if (!first.isValid())
|
|
914 return 0;
|
|
915 return qMin(m_treeModel.rowCount(first), MOVEDROWS);
|
|
916 }
|
|
917
|
|
918 private:
|
|
919
|
65
|
920 HistoryTreeModel m_treeModel;
|
45
|
921 }
|
|
922
|
|
923 // Menu that is dynamically populated from the history
|
|
924 class HistoryMenu : public ModelMenu
|
|
925 {
|
65
|
926 mixin Signal!("openUrl", QUrl url);
|
|
927
|
45
|
928 public:
|
|
929
|
65
|
930 this(QWidget parent = null)
|
45
|
931 {
|
|
932 super(parent);
|
|
933 m_history = 0;
|
65
|
934 connect(this, SIGNAL(activated(QModelIndex )),
|
|
935 this, SLOT(activated(QModelIndex )));
|
45
|
936 setHoverRole(HistoryModel.UrlStringRole);
|
|
937 }
|
|
938
|
65
|
939 void setInitialActions(QList<QAction> actions)
|
45
|
940 {
|
|
941 m_initialActions = actions;
|
|
942 for (int i = 0; i < m_initialActions.count(); ++i)
|
|
943 addAction(m_initialActions.at(i));
|
|
944 }
|
|
945
|
|
946 protected:
|
|
947
|
|
948 bool prePopulated()
|
|
949 {
|
|
950 if (!m_history) {
|
65
|
951 m_history = BrowserApplication.historyManager();
|
45
|
952 m_historyMenuModel = new HistoryMenuModel(m_history.historyTreeModel(), this);
|
|
953 setModel(m_historyMenuModel);
|
|
954 }
|
|
955 // initial actions
|
|
956 for (int i = 0; i < m_initialActions.count(); ++i)
|
|
957 addAction(m_initialActions.at(i));
|
|
958 if (!m_initialActions.isEmpty())
|
|
959 addSeparator();
|
|
960 setFirstSeparator(m_historyMenuModel.bumpedRows());
|
|
961
|
|
962 return false;
|
|
963 }
|
|
964
|
|
965 void postPopulated()
|
|
966 {
|
|
967 if (m_history.history().count() > 0)
|
|
968 addSeparator();
|
|
969
|
65
|
970 QAction showAllAction = new QAction(tr("Show All History"), this);
|
|
971 showAllAction.triggered.connect(&this.showHistoryDialog);
|
45
|
972 addAction(showAllAction);
|
|
973
|
65
|
974 QAction clearAction = new QAction(tr("Clear History"), this);
|
|
975 clearAction.triggered.connect(&m_history.clear);
|
45
|
976 addAction(clearAction);
|
|
977 }
|
|
978
|
|
979 private slots:
|
65
|
980 void activated(QModelIndex index)
|
45
|
981 {
|
|
982 emit openUrl(index.data(HistoryModel.UrlRole).toUrl());
|
|
983 }
|
|
984
|
|
985 void showHistoryDialog()
|
|
986 {
|
65
|
987 auto dialog = new HistoryDialog(this);
|
|
988 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl));
|
45
|
989 dialog.show();
|
|
990 }
|
|
991
|
|
992
|
|
993 private:
|
|
994
|
65
|
995 HistoryManager m_history;
|
|
996 HistoryMenuModel m_historyMenuModel;
|
|
997 QList<QAction> m_initialActions;
|
45
|
998 }
|
|
999
|
|
1000 // proxy model for the history model that
|
|
1001 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
|
|
1002 class HistoryCompletionModel : public QAbstractProxyModel
|
|
1003 {
|
|
1004
|
|
1005 public:
|
65
|
1006 this(QObject parent = null)
|
45
|
1007 {
|
|
1008 super(parent);
|
|
1009 }
|
|
1010
|
65
|
1011 QVariant data(QModelIndex index, int role)
|
45
|
1012 {
|
|
1013 if (sourceModel()
|
|
1014 && (role == Qt.EditRole || role == Qt.DisplayRole)
|
|
1015 && index.isValid()) {
|
|
1016 QModelIndex idx = mapToSource(index);
|
|
1017 idx = idx.sibling(idx.row(), 1);
|
|
1018 QString urlString = idx.data(HistoryModel.UrlStringRole).toString();
|
|
1019 if (index.row() % 2) {
|
|
1020 QUrl url = urlString;
|
65
|
1021 QString s = url.toString(QUrl.RemoveScheme | QUrl.RemoveUserInfo | QUrl.StripTrailingSlash);
|
45
|
1022 return s.mid(2); // strip // from the front
|
|
1023 }
|
|
1024 return urlString;
|
|
1025 }
|
65
|
1026 return QAbstractProxyModel.data(index, role);
|
45
|
1027 }
|
|
1028
|
65
|
1029 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
1030 {
|
|
1031 return (parent.isValid() || !sourceModel()) ? 0 : sourceModel().rowCount(parent) * 2;
|
|
1032 }
|
|
1033
|
65
|
1034 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
1035 {
|
|
1036 return (parent.isValid()) ? 0 : 1;
|
|
1037 }
|
|
1038
|
65
|
1039 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1040 {
|
|
1041 int row = sourceIndex.row() * 2;
|
|
1042 return index(row, sourceIndex.column());
|
|
1043 }
|
|
1044
|
|
1045
|
65
|
1046 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1047 {
|
|
1048 if (!sourceModel())
|
|
1049 return QModelIndex();
|
|
1050 int row = proxyIndex.row() / 2;
|
|
1051 return sourceModel().index(row, proxyIndex.column());
|
|
1052 }
|
|
1053
|
65
|
1054 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
1055 {
|
65
|
1056 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
45
|
1057 return QModelIndex();
|
|
1058 return createIndex(row, column, 0);
|
|
1059 }
|
|
1060
|
65
|
1061 QModelIndex parent(QModelIndex index= QModelIndex());
|
45
|
1062 {
|
|
1063 return QModelIndex();
|
|
1064 }
|
|
1065
|
65
|
1066 void setSourceModel(QAbstractItemModel sourceModel);
|
45
|
1067 {
|
|
1068 if (sourceModel()) {
|
65
|
1069 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
1070 sourceModel.rowsInserted(QModelIndex , int, int).disconnect(&this.sourceReset);
|
|
1071 sourceModel.rowsRemoved(QModelIndex , int, int).disconnect(&this.sourceReset);
|
45
|
1072 }
|
|
1073
|
65
|
1074 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1075
|
|
1076 if (newSourceModel) {
|
65
|
1077 newSourceModel.modelReset.connect(&this.sourceReset);
|
|
1078 sourceModel.rowsInserted(QModelIndex , int, int).connect(&this.sourceReset);
|
|
1079 sourceModel.rowsRemoved(QModelIndex , int, int).connect(&this.sourceReset);
|
45
|
1080 }
|
|
1081
|
|
1082 reset();
|
|
1083 }
|
|
1084
|
|
1085 private slots:
|
|
1086
|
|
1087 void sourceReset()
|
|
1088 {
|
|
1089 reset();
|
|
1090 }
|
|
1091 }
|
|
1092
|
|
1093 // proxy model for the history model that converts the list
|
|
1094 // into a tree, one top level node per day.
|
|
1095 // Used in the HistoryDialog.
|
|
1096 class HistoryTreeModel : public QAbstractProxyModel
|
|
1097 {
|
|
1098 public:
|
|
1099
|
65
|
1100 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
1101 {
|
|
1102 super(parent);
|
|
1103 setSourceModel(sourceModel);
|
|
1104 }
|
|
1105
|
65
|
1106 QVariant data(QModelIndex index, int role = Qt.DisplayRole);
|
45
|
1107 {
|
|
1108 if ((role == Qt.EditRole || role == Qt.DisplayRole)) {
|
|
1109 int start = index.internalId();
|
|
1110 if (start == 0) {
|
|
1111 int offset = sourceDateRow(index.row());
|
|
1112 if (index.column() == 0) {
|
|
1113 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1114 QDate date = idx.data(HistoryModel.DateRole).toDate();
|
65
|
1115 if (date == QDate.currentDate())
|
45
|
1116 return tr("Earlier Today");
|
|
1117 return date.toString(QLatin1String("dddd, MMMM d, yyyy"));
|
|
1118 }
|
|
1119 if (index.column() == 1) {
|
|
1120 return tr("%1 items").arg(rowCount(index.sibling(index.row(), 0)));
|
|
1121 }
|
|
1122 }
|
|
1123 }
|
|
1124 if (role == Qt.DecorationRole && index.column() == 0 && !index.parent().isValid())
|
|
1125 return QIcon(QLatin1String(":history.png"));
|
|
1126 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) {
|
|
1127 int offset = sourceDateRow(index.row());
|
|
1128 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1129 return idx.data(HistoryModel.DateRole);
|
|
1130 }
|
|
1131
|
65
|
1132 return QAbstractProxyModel.data(index, role);
|
45
|
1133 }
|
|
1134
|
|
1135
|
65
|
1136 int columnCount(QModelIndex parent);
|
45
|
1137 {
|
|
1138 return sourceModel().columnCount(mapToSource(parent));
|
|
1139 }
|
|
1140
|
|
1141
|
65
|
1142 int rowCount(QModelIndex parent = QModelIndex())
|
|
1143 {
|
|
1144 if ( parent.internalId() != 0 || parent.column() > 0 || !sourceModel())
|
|
1145 return 0;
|
45
|
1146
|
65
|
1147 // row count OF dates
|
|
1148 if (!parent.isValid()) {
|
|
1149 if (!m_sourceRowCache.isEmpty())
|
|
1150 return m_sourceRowCache.count();
|
|
1151 QDate currentDate;
|
|
1152 int rows = 0;
|
|
1153 int totalRows = sourceModel().rowCount();
|
45
|
1154
|
65
|
1155 for (int i = 0; i < totalRows; ++i) {
|
|
1156 QDate rowDate = sourceModel().index(i, 0).data(HistoryModel.DateRole).toDate();
|
|
1157 if (rowDate != currentDate) {
|
|
1158 m_sourceRowCache.append(i);
|
|
1159 currentDate = rowDate;
|
|
1160 ++rows;
|
|
1161 }
|
|
1162 }
|
|
1163 assert(m_sourceRowCache.count() == rows);
|
|
1164 return rows;
|
|
1165 }
|
45
|
1166
|
65
|
1167 // row count FOR a date
|
|
1168 int start = sourceDateRow(parent.row());
|
|
1169 int end = sourceDateRow(parent.row() + 1);
|
|
1170 return (end - start);
|
|
1171 }
|
45
|
1172
|
|
1173
|
65
|
1174 QModelIndex mapFromSource(QModelIndex sourceIndex);
|
45
|
1175 {
|
|
1176 if (!sourceIndex.isValid())
|
|
1177 return QModelIndex();
|
|
1178
|
|
1179 if (m_sourceRowCache.isEmpty())
|
|
1180 rowCount(QModelIndex());
|
|
1181
|
65
|
1182 QList<int>.iterator it;
|
45
|
1183 it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), sourceIndex.row());
|
|
1184 if (*it != sourceIndex.row())
|
|
1185 --it;
|
|
1186 int dateRow = qMax(0, it - m_sourceRowCache.begin());
|
|
1187 int row = sourceIndex.row() - m_sourceRowCache.at(dateRow);
|
|
1188 return createIndex(row, sourceIndex.column(), dateRow + 1);
|
|
1189 }
|
|
1190
|
|
1191
|
65
|
1192 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1193 {
|
|
1194 int offset = proxyIndex.internalId();
|
|
1195 if (offset == 0)
|
|
1196 return QModelIndex();
|
|
1197 int startDateRow = sourceDateRow(offset - 1);
|
|
1198 return sourceModel().index(startDateRow + proxyIndex.row(), proxyIndex.column());
|
|
1199 }
|
|
1200
|
|
1201
|
65
|
1202 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
1203 {
|
65
|
1204 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
|
1205 return QModelIndex();
|
45
|
1206
|
65
|
1207 if (!parent.isValid())
|
|
1208 return createIndex(row, column, 0);
|
|
1209 return createIndex(row, column, parent.row() + 1);
|
45
|
1210 }
|
|
1211
|
|
1212
|
65
|
1213 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1214 {
|
|
1215 int offset = index.internalId();
|
|
1216 if (offset == 0 || !index.isValid())
|
|
1217 return QModelIndex();
|
|
1218 return createIndex(offset - 1, 0, 0);
|
|
1219 }
|
|
1220
|
|
1221
|
65
|
1222 bool hasChildren(QModelIndex parent = QModelIndex())
|
45
|
1223 {
|
|
1224 QModelIndex grandparent = parent.parent();
|
|
1225 if (!grandparent.isValid())
|
|
1226 return true;
|
|
1227 return false;
|
|
1228 }
|
|
1229
|
|
1230
|
65
|
1231 Qt.ItemFlags flags(QModelIndex index)
|
45
|
1232 {
|
|
1233 if (!index.isValid())
|
|
1234 return Qt.NoItemFlags;
|
|
1235 return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled;
|
|
1236 }
|
|
1237
|
65
|
1238 bool removeRows(int row, int count, QModelIndex parent = QModelIndex());
|
|
1239 {
|
|
1240 if (row < 0 || count <= 0 || row + count > rowCount(parent))
|
|
1241 return false;
|
45
|
1242
|
65
|
1243 if (parent.isValid()) {
|
|
1244 // removing pages
|
|
1245 int offset = sourceDateRow(parent.row());
|
|
1246 return sourceModel().removeRows(offset + row, count);
|
|
1247 } else {
|
|
1248 // removing whole dates
|
|
1249 for (int i = row + count - 1; i >= row; --i) {
|
|
1250 QModelIndex dateParent = index(i, 0);
|
|
1251 int offset = sourceDateRow(dateParent.row());
|
|
1252 if (!sourceModel().removeRows(offset, rowCount(dateParent)))
|
|
1253 return false;
|
|
1254 }
|
|
1255 }
|
|
1256 return true;
|
|
1257 }
|
45
|
1258
|
|
1259
|
65
|
1260 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
|
1261 {
|
|
1262 return sourceModel().headerData(section, orientation, role);
|
|
1263 }
|
45
|
1264
|
|
1265
|
|
1266
|
65
|
1267 void setSourceModel(QAbstractItemModel newSourceModel)
|
|
1268 {
|
|
1269 if (sourceModel()) {
|
|
1270 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
1271 sourceModel.layoutChanged.disconnect(&this.sourceReset);
|
|
1272 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
1273 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
|
1274 }
|
45
|
1275
|
65
|
1276 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1277
|
65
|
1278 if (newSourceModel) {
|
|
1279 sourceModel.modelReset.connect(&this.sourceReset);
|
|
1280 sourceModel.layoutChanged.connect(&this.sourceReset);
|
|
1281 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
1282 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
|
1283 }
|
45
|
1284
|
65
|
1285 reset();
|
|
1286 }
|
45
|
1287
|
|
1288
|
|
1289 private slots:
|
|
1290
|
|
1291 void sourceReset()
|
|
1292 {
|
|
1293 m_sourceRowCache.clear();
|
|
1294 reset();
|
|
1295 }
|
|
1296
|
65
|
1297 void sourceRowsInserted(QModelIndex parent, int start, int end);
|
45
|
1298 {
|
|
1299 Q_UNUSED(parent); // Avoid warnings when compiling release
|
65
|
1300 assert(!parent.isValid());
|
45
|
1301 if (start != 0 || start != end) {
|
|
1302 m_sourceRowCache.clear();
|
|
1303 reset();
|
|
1304 return;
|
|
1305 }
|
|
1306
|
|
1307 m_sourceRowCache.clear();
|
|
1308 QModelIndex treeIndex = mapFromSource(sourceModel().index(start, 0));
|
|
1309 QModelIndex treeParent = treeIndex.parent();
|
|
1310 if (rowCount(treeParent) == 1) {
|
|
1311 beginInsertRows(QModelIndex(), 0, 0);
|
|
1312 endInsertRows();
|
|
1313 } else {
|
|
1314 beginInsertRows(treeParent, treeIndex.row(), treeIndex.row());
|
|
1315 endInsertRows();
|
|
1316 }
|
|
1317 }
|
|
1318
|
65
|
1319 void sourceRowsRemoved(QModelIndex parent, int start, int end);
|
|
1320 {
|
|
1321 //Q_UNUSED(parent); // Avoid warnings when compiling release
|
|
1322 assert(!parent.isValid());
|
|
1323 if (m_sourceRowCache.isEmpty())
|
|
1324 return;
|
|
1325 for (int i = end; i >= start;) {
|
|
1326 QList<int>::iterator it;
|
|
1327 it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), i);
|
|
1328 // playing it safe
|
|
1329 if (it == m_sourceRowCache.end()) {
|
|
1330 m_sourceRowCache.clear();
|
|
1331 reset();
|
|
1332 return;
|
|
1333 }
|
45
|
1334
|
65
|
1335 if (*it != i)
|
|
1336 --it;
|
|
1337 int row = qMax(0, it - m_sourceRowCache.begin());
|
|
1338 int offset = m_sourceRowCache[row];
|
|
1339 QModelIndex dateParent = index(row, 0);
|
|
1340 // If we can remove all the rows in the date do that and skip over them
|
|
1341 int rc = rowCount(dateParent);
|
|
1342 if (i - rc + 1 == offset && start <= i - rc + 1) {
|
|
1343 beginRemoveRows(QModelIndex(), row, row);
|
|
1344 m_sourceRowCache.removeAt(row);
|
|
1345 i -= rc + 1;
|
|
1346 } else {
|
|
1347 beginRemoveRows(dateParent, i - offset, i - offset);
|
|
1348 ++row;
|
|
1349 --i;
|
|
1350 }
|
|
1351 for (int j = row; j < m_sourceRowCache.count(); ++j)
|
|
1352 --m_sourceRowCache[j];
|
|
1353 endRemoveRows();
|
|
1354 }
|
|
1355 }
|
45
|
1356
|
|
1357 private:
|
|
1358
|
|
1359 // Translate the top level date row into the offset where that date starts
|
|
1360 int sourceDateRow(int row)
|
|
1361 {
|
|
1362 if (row <= 0)
|
|
1363 return 0;
|
|
1364
|
|
1365 if (m_sourceRowCache.isEmpty())
|
|
1366 rowCount(QModelIndex());
|
|
1367
|
|
1368 if (row >= m_sourceRowCache.count()) {
|
|
1369 if (!sourceModel())
|
|
1370 return 0;
|
|
1371 return sourceModel().rowCount();
|
|
1372 }
|
|
1373 return m_sourceRowCache.at(row);
|
|
1374 }
|
|
1375
|
65
|
1376 QList<int> m_sourceRowCache;
|
45
|
1377 }
|
|
1378
|
|
1379 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
|
|
1380 // so filtering is only done on the children.
|
|
1381 // Used in the HistoryDialog
|
|
1382 class TreeProxyModel : public QSortFilterProxyModel
|
|
1383 {
|
65
|
1384 public:
|
45
|
1385
|
65
|
1386 this(QObject parent = null)
|
45
|
1387 {
|
|
1388 super(parent);
|
|
1389 setSortRole(HistoryModel.DateTimeRole);
|
|
1390 setFilterCaseSensitivity(Qt.CaseInsensitive);
|
|
1391 }
|
65
|
1392
|
|
1393 protected:
|
|
1394 bool filterAcceptsRow(int source_row, QModelIndex source_parent)
|
45
|
1395 {
|
|
1396 if (!source_parent.isValid())
|
|
1397 return true;
|
65
|
1398 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent);
|
45
|
1399 }
|
|
1400 }
|
|
1401
|
65
|
1402 import ui_history;
|
45
|
1403
|
|
1404 class HistoryDialog : public QDialog, public Ui_HistoryDialog
|
|
1405 {
|
65
|
1406 mixin Signal!("openUrl", QUrl url);
|
45
|
1407
|
|
1408 public:
|
|
1409
|
65
|
1410 this(QWidget parent = null, HistoryManager history = null) : QDialog(parent)
|
45
|
1411 {
|
65
|
1412 HistoryManager history = setHistory;
|
45
|
1413 if (!history)
|
65
|
1414 history = BrowserApplication.historyManager();
|
45
|
1415 setupUi(this);
|
|
1416 tree.setUniformRowHeights(true);
|
65
|
1417 tree.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
1418 tree.setTextElideMode(Qt.ElideMiddle);
|
65
|
1419 auto model = history.historyTreeModel();
|
|
1420 auto proxyModel = new TreeProxyModel(this);
|
|
1421 search.textChanged(QString).connect(&proxyModel.setFilterFixedString(QString));
|
|
1422 removeButton.clicked.connect(&tree.removeOne);
|
|
1423 removeAllButton.clicked.connect(&history.clear);
|
45
|
1424 proxyModel.setSourceModel(model);
|
|
1425 tree.setModel(proxyModel);
|
|
1426 tree.setExpanded(proxyModel.index(0, 0), true);
|
|
1427 tree.setAlternatingRowColors(true);
|
65
|
1428 QFontMetrics fm = font();
|
45
|
1429 int header = fm.width(QLatin1Char('m')) * 40;
|
|
1430 tree.header().resizeSection(0, header);
|
|
1431 tree.header().setStretchLastSection(true);
|
65
|
1432 tree.activated(QModelIndex).connect(this.open);
|
45
|
1433 tree.setContextMenuPolicy(Qt.CustomContextMenu);
|
65
|
1434 tree.customContextMenuRequested(QPoint).connect(&this.customContextMenuRequested(QPoint));
|
45
|
1435 }
|
|
1436
|
65
|
1437 private:
|
45
|
1438
|
65
|
1439 void customContextMenuRequested(QPoint pos)
|
45
|
1440 {
|
|
1441 QMenu menu;
|
|
1442 QModelIndex index = tree.indexAt(pos);
|
|
1443 index = index.sibling(index.row(), 0);
|
|
1444 if (index.isValid() && !tree.model().hasChildren(index)) {
|
|
1445 menu.addAction(tr("Open"), this, SLOT(open()));
|
|
1446 menu.addSeparator();
|
|
1447 menu.addAction(tr("Copy"), this, SLOT(copy()));
|
|
1448 }
|
|
1449 menu.addAction(tr("Delete"), tree, SLOT(removeOne()));
|
65
|
1450 menu.exec(QCursor.pos());
|
45
|
1451 }
|
|
1452
|
|
1453
|
|
1454 void open()
|
|
1455 {
|
|
1456 QModelIndex index = tree.currentIndex();
|
|
1457 if (!index.parent().isValid())
|
|
1458 return;
|
|
1459 emit openUrl(index.data(HistoryModel.UrlRole).toUrl());
|
|
1460 }
|
|
1461
|
|
1462 void copy()
|
|
1463 {
|
|
1464 QModelIndex index = tree.currentIndex();
|
|
1465 if (!index.parent().isValid())
|
|
1466 return;
|
|
1467 QString url = index.data(HistoryModel.UrlStringRole).toString();
|
|
1468
|
65
|
1469 QClipboard clipboard = QApplication.clipboard();
|
45
|
1470 clipboard.setText(url);
|
|
1471 }
|
|
1472 }
|