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