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