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