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;
|
74
|
67 import qt.webkit.QWebSettings;
|
45
|
68
|
|
69 import QWebHistoryInterface;
|
|
70
|
74
|
71 import autosaver;
|
65
|
72 import browserapplication;
|
|
73 import modelmenu;
|
|
74
|
|
75
|
74
|
76 static const uint HISTORY_VERSION = 23;
|
45
|
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");
|
74
|
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
|
74
|
150 bool historyContains(QString url)
|
45
|
151 {
|
|
152 return m_historyFilterModel.historyContains(url);
|
|
153 }
|
|
154
|
74
|
155 void addHistoryEntry(QString url)
|
45
|
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
|
74
|
164 void updateHistoryItem(QUrl url, QString title)
|
45
|
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
|
74
|
192 HistoryItem[] history()
|
45
|
193 {
|
|
194 return m_history;
|
|
195 }
|
|
196
|
74
|
197 void setHistory(HistoryItem[] history, bool loadedAndSorted = false)
|
45
|
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
|
74
|
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
|
74
|
282 auto historyFile = new QFile(directory ~ QLatin1String("/history"));
|
65
|
283 // When saving everything use a temporary file to prevent possible data loss.
|
74
|
284 auto tempFile = new QTemporaryFile;
|
65
|
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
|
74
|
375 HistoryItem[] list;
|
65
|
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;
|
74
|
429 HistoryItem[] m_history;
|
65
|
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 {
|
74
|
493 HistoryItem[] lst = m_history.history();
|
45
|
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);
|
74
|
547 HistoryItem[] lst = m_history.history();
|
45
|
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
|
74
|
588 int historyLocation(QString url)
|
65
|
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
|
74
|
649 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
45
|
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
|
74
|
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 */
|
74
|
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
|
74
|
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 {
|
74
|
720 dataChanged.emit(mapFromSource(topLeft), mapFromSource(bottomRight));
|
45
|
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
|
74
|
771 int[] m_sourceRow;
|
|
772 int[QString] m_historyHash;
|
65
|
773 bool m_loaded;
|
45
|
774 }
|
|
775
|
73
|
776
|
45
|
777 /*
|
65
|
778 The history menu
|
|
779 - Removes the first twenty entries and puts them as children of the top level.
|
|
780 - If there are less then twenty entries then the first folder is also removed.
|
45
|
781
|
65
|
782 The mapping is done by knowing that HistoryTreeModel is over a table
|
|
783 We store that row offset in our index's private data.
|
45
|
784 */
|
|
785 class HistoryMenuModel : public QAbstractProxyModel
|
|
786 {
|
|
787 public:
|
|
788
|
65
|
789 /*
|
|
790 Maps the first bunch of items of the source model to the root
|
|
791 */
|
74
|
792 this(HistoryTreeModel sourceModel, QObject parent = null)
|
65
|
793 {
|
|
794 super(parent);
|
|
795 m_treeModel = sourceModel;
|
|
796 setSourceModel(sourceModel);
|
|
797 }
|
45
|
798
|
65
|
799 int columnCount(QModelIndex parent)
|
|
800 {
|
|
801 return m_treeModel.columnCount(mapToSource(parent));
|
|
802 }
|
45
|
803
|
74
|
804 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
805 {
|
|
806 if (parent.column() > 0)
|
|
807 return 0;
|
|
808
|
|
809 if (!parent.isValid()) {
|
|
810 int folders = sourceModel().rowCount();
|
|
811 int bumpedItems = bumpedRows();
|
|
812 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
813 --folders;
|
|
814 return bumpedItems + folders;
|
|
815 }
|
|
816
|
|
817 if (parent.internalId() == -1) {
|
|
818 if (parent.row() < bumpedRows())
|
|
819 return 0;
|
|
820 }
|
|
821
|
|
822 QModelIndex idx = mapToSource(parent);
|
|
823 int defaultCount = sourceModel().rowCount(idx);
|
|
824 if (idx == sourceModel().index(0, 0))
|
|
825 return defaultCount - bumpedRows();
|
|
826 return defaultCount;
|
|
827 }
|
|
828
|
65
|
829 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
830 {
|
|
831 // currently not used or autotested
|
65
|
832 assert(false);
|
45
|
833 int sr = m_treeModel.mapToSource(sourceIndex).row();
|
|
834 return createIndex(sourceIndex.row(), sourceIndex.column(), sr);
|
|
835 }
|
|
836
|
65
|
837 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
838 {
|
|
839 if (!proxyIndex.isValid())
|
|
840 return QModelIndex();
|
|
841
|
|
842 if (proxyIndex.internalId() == -1) {
|
|
843 int bumpedItems = bumpedRows();
|
|
844 if (proxyIndex.row() < bumpedItems)
|
|
845 return m_treeModel.index(proxyIndex.row(), proxyIndex.column(), m_treeModel.index(0, 0));
|
|
846 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(m_treeModel.index(0, 0)))
|
|
847 --bumpedItems;
|
|
848 return m_treeModel.index(proxyIndex.row() - bumpedItems, proxyIndex.column());
|
|
849 }
|
|
850
|
|
851 QModelIndex historyIndex = m_treeModel.sourceModel().index(proxyIndex.internalId(), proxyIndex.column());
|
|
852 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
853 return treeIndex;
|
|
854 }
|
|
855
|
74
|
856 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
857 {
|
65
|
858 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
45
|
859 return QModelIndex();
|
65
|
860
|
45
|
861 if (!parent.isValid())
|
|
862 return createIndex(row, column, -1);
|
|
863
|
|
864 QModelIndex treeIndexParent = mapToSource(parent);
|
|
865
|
|
866 int bumpedItems = 0;
|
|
867 if (treeIndexParent == m_treeModel.index(0, 0))
|
|
868 bumpedItems = bumpedRows();
|
|
869 QModelIndex treeIndex = m_treeModel.index(row + bumpedItems, column, treeIndexParent);
|
|
870 QModelIndex historyIndex = m_treeModel.mapToSource(treeIndex);
|
|
871 int historyRow = historyIndex.row();
|
|
872 if (historyRow == -1)
|
|
873 historyRow = treeIndex.row();
|
|
874 return createIndex(row, column, historyRow);
|
|
875 }
|
|
876
|
74
|
877 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
878 {
|
|
879 int offset = index.internalId();
|
|
880 if (offset == -1 || !index.isValid())
|
|
881 return QModelIndex();
|
|
882
|
|
883 QModelIndex historyIndex = m_treeModel.sourceModel().index(index.internalId(), 0);
|
|
884 QModelIndex treeIndex = m_treeModel.mapFromSource(historyIndex);
|
|
885 QModelIndex treeIndexParent = treeIndex.parent();
|
|
886
|
|
887 int sr = m_treeModel.mapToSource(treeIndexParent).row();
|
|
888 int bumpedItems = bumpedRows();
|
|
889 if (bumpedItems <= MOVEDROWS && bumpedItems == sourceModel().rowCount(sourceModel().index(0, 0)))
|
|
890 --bumpedItems;
|
|
891 return createIndex(bumpedItems + treeIndexParent.row(), treeIndexParent.column(), sr);
|
|
892 }
|
|
893
|
|
894 int bumpedRows()
|
|
895 {
|
|
896 QModelIndex first = m_treeModel.index(0, 0);
|
|
897 if (!first.isValid())
|
|
898 return 0;
|
|
899 return qMin(m_treeModel.rowCount(first), MOVEDROWS);
|
|
900 }
|
|
901
|
|
902 private:
|
|
903
|
65
|
904 HistoryTreeModel m_treeModel;
|
45
|
905 }
|
|
906
|
73
|
907
|
45
|
908 // Menu that is dynamically populated from the history
|
|
909 class HistoryMenu : public ModelMenu
|
|
910 {
|
74
|
911 mixin Signal!("openUrl", QUrl /*url*/);
|
65
|
912
|
45
|
913 public:
|
|
914
|
65
|
915 this(QWidget parent = null)
|
45
|
916 {
|
|
917 super(parent);
|
|
918 m_history = 0;
|
66
|
919 activated.connect(&this.activated);
|
45
|
920 setHoverRole(HistoryModel.UrlStringRole);
|
|
921 }
|
|
922
|
74
|
923 void setInitialActions(QAction[] actions)
|
66
|
924 {
|
|
925 m_initialActions = actions;
|
|
926 for (int i = 0; i < m_initialActions.count(); ++i)
|
|
927 addAction(m_initialActions.at(i));
|
|
928 }
|
45
|
929
|
|
930 protected:
|
|
931
|
|
932 bool prePopulated()
|
|
933 {
|
|
934 if (!m_history) {
|
65
|
935 m_history = BrowserApplication.historyManager();
|
45
|
936 m_historyMenuModel = new HistoryMenuModel(m_history.historyTreeModel(), this);
|
|
937 setModel(m_historyMenuModel);
|
|
938 }
|
|
939 // initial actions
|
|
940 for (int i = 0; i < m_initialActions.count(); ++i)
|
|
941 addAction(m_initialActions.at(i));
|
|
942 if (!m_initialActions.isEmpty())
|
|
943 addSeparator();
|
|
944 setFirstSeparator(m_historyMenuModel.bumpedRows());
|
|
945
|
|
946 return false;
|
|
947 }
|
|
948
|
|
949 void postPopulated()
|
|
950 {
|
|
951 if (m_history.history().count() > 0)
|
|
952 addSeparator();
|
|
953
|
65
|
954 QAction showAllAction = new QAction(tr("Show All History"), this);
|
|
955 showAllAction.triggered.connect(&this.showHistoryDialog);
|
45
|
956 addAction(showAllAction);
|
|
957
|
65
|
958 QAction clearAction = new QAction(tr("Clear History"), this);
|
|
959 clearAction.triggered.connect(&m_history.clear);
|
45
|
960 addAction(clearAction);
|
|
961 }
|
|
962
|
73
|
963 private:
|
|
964
|
65
|
965 void activated(QModelIndex index)
|
45
|
966 {
|
74
|
967 openUrl.emit(index.data(HistoryModel.UrlRole).toUrl());
|
45
|
968 }
|
|
969
|
|
970 void showHistoryDialog()
|
|
971 {
|
65
|
972 auto dialog = new HistoryDialog(this);
|
|
973 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl));
|
45
|
974 dialog.show();
|
|
975 }
|
|
976
|
|
977 private:
|
|
978
|
65
|
979 HistoryManager m_history;
|
|
980 HistoryMenuModel m_historyMenuModel;
|
74
|
981 QAction[] m_initialActions;
|
45
|
982 }
|
|
983
|
66
|
984
|
45
|
985 // proxy model for the history model that
|
|
986 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
|
|
987 class HistoryCompletionModel : public QAbstractProxyModel
|
|
988 {
|
73
|
989 public:
|
45
|
990
|
65
|
991 this(QObject parent = null)
|
45
|
992 {
|
|
993 super(parent);
|
|
994 }
|
|
995
|
65
|
996 QVariant data(QModelIndex index, int role)
|
45
|
997 {
|
73
|
998 if (sourceModel() && (role == Qt.EditRole || role == Qt.DisplayRole) && index.isValid()) {
|
45
|
999 QModelIndex idx = mapToSource(index);
|
|
1000 idx = idx.sibling(idx.row(), 1);
|
|
1001 QString urlString = idx.data(HistoryModel.UrlStringRole).toString();
|
|
1002 if (index.row() % 2) {
|
|
1003 QUrl url = urlString;
|
65
|
1004 QString s = url.toString(QUrl.RemoveScheme | QUrl.RemoveUserInfo | QUrl.StripTrailingSlash);
|
45
|
1005 return s.mid(2); // strip // from the front
|
|
1006 }
|
|
1007 return urlString;
|
|
1008 }
|
65
|
1009 return QAbstractProxyModel.data(index, role);
|
45
|
1010 }
|
|
1011
|
65
|
1012 int rowCount(QModelIndex parent = QModelIndex())
|
45
|
1013 {
|
|
1014 return (parent.isValid() || !sourceModel()) ? 0 : sourceModel().rowCount(parent) * 2;
|
|
1015 }
|
|
1016
|
65
|
1017 int columnCount(QModelIndex parent = QModelIndex())
|
45
|
1018 {
|
|
1019 return (parent.isValid()) ? 0 : 1;
|
|
1020 }
|
|
1021
|
65
|
1022 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1023 {
|
|
1024 int row = sourceIndex.row() * 2;
|
|
1025 return index(row, sourceIndex.column());
|
|
1026 }
|
|
1027
|
65
|
1028 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1029 {
|
|
1030 if (!sourceModel())
|
|
1031 return QModelIndex();
|
|
1032 int row = proxyIndex.row() / 2;
|
|
1033 return sourceModel().index(row, proxyIndex.column());
|
|
1034 }
|
|
1035
|
65
|
1036 QModelIndex index(int, int, QModelIndex = QModelIndex())
|
45
|
1037 {
|
65
|
1038 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
|
45
|
1039 return QModelIndex();
|
|
1040 return createIndex(row, column, 0);
|
|
1041 }
|
|
1042
|
74
|
1043 QModelIndex parent(QModelIndex index= QModelIndex())
|
45
|
1044 {
|
|
1045 return QModelIndex();
|
|
1046 }
|
|
1047
|
74
|
1048 void setSourceModel(QAbstractItemModel sourceModel)
|
45
|
1049 {
|
|
1050 if (sourceModel()) {
|
65
|
1051 sourceModel.modelReset.disconnect(&this.sourceReset);
|
73
|
1052 sourceModel.rowsInserted.disconnect(&this.sourceReset);
|
|
1053 sourceModel.rowsRemoved.disconnect(&this.sourceReset);
|
45
|
1054 }
|
|
1055
|
65
|
1056 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1057
|
|
1058 if (newSourceModel) {
|
65
|
1059 newSourceModel.modelReset.connect(&this.sourceReset);
|
73
|
1060 sourceModel.rowsInserted.connect(&this.sourceReset);
|
|
1061 sourceModel.rowsRemoved.connect(&this.sourceReset);
|
45
|
1062 }
|
|
1063
|
|
1064 reset();
|
|
1065 }
|
|
1066
|
73
|
1067 private:
|
45
|
1068
|
|
1069 void sourceReset()
|
|
1070 {
|
|
1071 reset();
|
|
1072 }
|
|
1073 }
|
|
1074
|
73
|
1075
|
45
|
1076 // proxy model for the history model that converts the list
|
|
1077 // into a tree, one top level node per day.
|
|
1078 // Used in the HistoryDialog.
|
|
1079 class HistoryTreeModel : public QAbstractProxyModel
|
|
1080 {
|
|
1081 public:
|
|
1082
|
65
|
1083 this(QAbstractItemModel sourceModel, QObject parent = null)
|
45
|
1084 {
|
|
1085 super(parent);
|
|
1086 setSourceModel(sourceModel);
|
|
1087 }
|
|
1088
|
74
|
1089 QVariant data(QModelIndex index, int role = Qt.DisplayRole)
|
45
|
1090 {
|
|
1091 if ((role == Qt.EditRole || role == Qt.DisplayRole)) {
|
|
1092 int start = index.internalId();
|
|
1093 if (start == 0) {
|
|
1094 int offset = sourceDateRow(index.row());
|
|
1095 if (index.column() == 0) {
|
|
1096 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1097 QDate date = idx.data(HistoryModel.DateRole).toDate();
|
65
|
1098 if (date == QDate.currentDate())
|
45
|
1099 return tr("Earlier Today");
|
|
1100 return date.toString(QLatin1String("dddd, MMMM d, yyyy"));
|
|
1101 }
|
|
1102 if (index.column() == 1) {
|
|
1103 return tr("%1 items").arg(rowCount(index.sibling(index.row(), 0)));
|
|
1104 }
|
|
1105 }
|
|
1106 }
|
|
1107 if (role == Qt.DecorationRole && index.column() == 0 && !index.parent().isValid())
|
|
1108 return QIcon(QLatin1String(":history.png"));
|
|
1109 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) {
|
|
1110 int offset = sourceDateRow(index.row());
|
|
1111 QModelIndex idx = sourceModel().index(offset, 0);
|
|
1112 return idx.data(HistoryModel.DateRole);
|
|
1113 }
|
|
1114
|
65
|
1115 return QAbstractProxyModel.data(index, role);
|
45
|
1116 }
|
|
1117
|
74
|
1118 int columnCount(QModelIndex parent)
|
45
|
1119 {
|
|
1120 return sourceModel().columnCount(mapToSource(parent));
|
|
1121 }
|
|
1122
|
65
|
1123 int rowCount(QModelIndex parent = QModelIndex())
|
|
1124 {
|
|
1125 if ( parent.internalId() != 0 || parent.column() > 0 || !sourceModel())
|
|
1126 return 0;
|
45
|
1127
|
65
|
1128 // row count OF dates
|
|
1129 if (!parent.isValid()) {
|
|
1130 if (!m_sourceRowCache.isEmpty())
|
|
1131 return m_sourceRowCache.count();
|
|
1132 QDate currentDate;
|
|
1133 int rows = 0;
|
|
1134 int totalRows = sourceModel().rowCount();
|
45
|
1135
|
65
|
1136 for (int i = 0; i < totalRows; ++i) {
|
|
1137 QDate rowDate = sourceModel().index(i, 0).data(HistoryModel.DateRole).toDate();
|
|
1138 if (rowDate != currentDate) {
|
|
1139 m_sourceRowCache.append(i);
|
|
1140 currentDate = rowDate;
|
|
1141 ++rows;
|
|
1142 }
|
|
1143 }
|
|
1144 assert(m_sourceRowCache.count() == rows);
|
|
1145 return rows;
|
|
1146 }
|
45
|
1147
|
65
|
1148 // row count FOR a date
|
|
1149 int start = sourceDateRow(parent.row());
|
|
1150 int end = sourceDateRow(parent.row() + 1);
|
|
1151 return (end - start);
|
|
1152 }
|
45
|
1153
|
74
|
1154 QModelIndex mapFromSource(QModelIndex sourceIndex)
|
45
|
1155 {
|
|
1156 if (!sourceIndex.isValid())
|
|
1157 return QModelIndex();
|
|
1158
|
|
1159 if (m_sourceRowCache.isEmpty())
|
66
|
1160 rowCount(QModelIndex());
|
45
|
1161
|
74
|
1162 int[].iterator it;
|
45
|
1163 it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), sourceIndex.row());
|
|
1164 if (*it != sourceIndex.row())
|
66
|
1165 --it;
|
45
|
1166 int dateRow = qMax(0, it - m_sourceRowCache.begin());
|
|
1167 int row = sourceIndex.row() - m_sourceRowCache.at(dateRow);
|
|
1168 return createIndex(row, sourceIndex.column(), dateRow + 1);
|
|
1169 }
|
|
1170
|
65
|
1171 QModelIndex mapToSource(QModelIndex proxyIndex)
|
45
|
1172 {
|
|
1173 int offset = proxyIndex.internalId();
|
|
1174 if (offset == 0)
|
|
1175 return QModelIndex();
|
|
1176 int startDateRow = sourceDateRow(offset - 1);
|
|
1177 return sourceModel().index(startDateRow + proxyIndex.row(), proxyIndex.column());
|
|
1178 }
|
|
1179
|
65
|
1180 QModelIndex index(int row, int column, QModelIndex parent = QModelIndex())
|
45
|
1181 {
|
65
|
1182 if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
|
|
1183 return QModelIndex();
|
45
|
1184
|
65
|
1185 if (!parent.isValid())
|
|
1186 return createIndex(row, column, 0);
|
|
1187 return createIndex(row, column, parent.row() + 1);
|
45
|
1188 }
|
|
1189
|
65
|
1190 QModelIndex parent(QModelIndex index = QModelIndex())
|
45
|
1191 {
|
|
1192 int offset = index.internalId();
|
|
1193 if (offset == 0 || !index.isValid())
|
|
1194 return QModelIndex();
|
|
1195 return createIndex(offset - 1, 0, 0);
|
|
1196 }
|
|
1197
|
65
|
1198 bool hasChildren(QModelIndex parent = QModelIndex())
|
45
|
1199 {
|
|
1200 QModelIndex grandparent = parent.parent();
|
|
1201 if (!grandparent.isValid())
|
|
1202 return true;
|
|
1203 return false;
|
|
1204 }
|
|
1205
|
65
|
1206 Qt.ItemFlags flags(QModelIndex index)
|
45
|
1207 {
|
|
1208 if (!index.isValid())
|
|
1209 return Qt.NoItemFlags;
|
|
1210 return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled;
|
|
1211 }
|
|
1212
|
74
|
1213 bool removeRows(int row, int count, QModelIndex parent = QModelIndex())
|
65
|
1214 {
|
|
1215 if (row < 0 || count <= 0 || row + count > rowCount(parent))
|
|
1216 return false;
|
45
|
1217
|
65
|
1218 if (parent.isValid()) {
|
|
1219 // removing pages
|
|
1220 int offset = sourceDateRow(parent.row());
|
|
1221 return sourceModel().removeRows(offset + row, count);
|
|
1222 } else {
|
|
1223 // removing whole dates
|
|
1224 for (int i = row + count - 1; i >= row; --i) {
|
|
1225 QModelIndex dateParent = index(i, 0);
|
|
1226 int offset = sourceDateRow(dateParent.row());
|
|
1227 if (!sourceModel().removeRows(offset, rowCount(dateParent)))
|
|
1228 return false;
|
|
1229 }
|
|
1230 }
|
|
1231 return true;
|
|
1232 }
|
45
|
1233
|
65
|
1234 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
|
|
1235 {
|
|
1236 return sourceModel().headerData(section, orientation, role);
|
|
1237 }
|
45
|
1238
|
65
|
1239 void setSourceModel(QAbstractItemModel newSourceModel)
|
|
1240 {
|
|
1241 if (sourceModel()) {
|
|
1242 sourceModel.modelReset.disconnect(&this.sourceReset);
|
|
1243 sourceModel.layoutChanged.disconnect(&this.sourceReset);
|
|
1244 sourceModel.rowsInserted.disconnect(&this.sourceRowsInserted);
|
|
1245 sourceModel.rowsRemoved.disconnect(&this.sourceRowsRemoved);
|
|
1246 }
|
45
|
1247
|
65
|
1248 QAbstractProxyModel.setSourceModel(newSourceModel);
|
45
|
1249
|
65
|
1250 if (newSourceModel) {
|
|
1251 sourceModel.modelReset.connect(&this.sourceReset);
|
|
1252 sourceModel.layoutChanged.connect(&this.sourceReset);
|
|
1253 sourceModel.rowsInserted.connect(&this.sourceRowsInserted);
|
|
1254 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved);
|
|
1255 }
|
45
|
1256
|
65
|
1257 reset();
|
|
1258 }
|
45
|
1259
|
73
|
1260 private:
|
45
|
1261
|
|
1262 void sourceReset()
|
|
1263 {
|
|
1264 m_sourceRowCache.clear();
|
|
1265 reset();
|
|
1266 }
|
|
1267
|
74
|
1268 void sourceRowsInserted(QModelIndex parent, int start, int end)
|
45
|
1269 {
|
73
|
1270 //Q_UNUSED(parent); // Avoid warnings when compiling release
|
65
|
1271 assert(!parent.isValid());
|
45
|
1272 if (start != 0 || start != end) {
|
|
1273 m_sourceRowCache.clear();
|
|
1274 reset();
|
|
1275 return;
|
|
1276 }
|
|
1277
|
|
1278 m_sourceRowCache.clear();
|
|
1279 QModelIndex treeIndex = mapFromSource(sourceModel().index(start, 0));
|
|
1280 QModelIndex treeParent = treeIndex.parent();
|
|
1281 if (rowCount(treeParent) == 1) {
|
|
1282 beginInsertRows(QModelIndex(), 0, 0);
|
|
1283 endInsertRows();
|
|
1284 } else {
|
|
1285 beginInsertRows(treeParent, treeIndex.row(), treeIndex.row());
|
|
1286 endInsertRows();
|
|
1287 }
|
|
1288 }
|
|
1289
|
65
|
1290 void sourceRowsRemoved(QModelIndex parent, int start, int end);
|
|
1291 {
|
|
1292 //Q_UNUSED(parent); // Avoid warnings when compiling release
|
|
1293 assert(!parent.isValid());
|
|
1294 if (m_sourceRowCache.isEmpty())
|
|
1295 return;
|
|
1296 for (int i = end; i >= start;) {
|
74
|
1297 int[]::iterator it;
|
65
|
1298 it = qLowerBound(m_sourceRowCache.begin(), m_sourceRowCache.end(), i);
|
|
1299 // playing it safe
|
|
1300 if (it == m_sourceRowCache.end()) {
|
|
1301 m_sourceRowCache.clear();
|
|
1302 reset();
|
|
1303 return;
|
|
1304 }
|
45
|
1305
|
65
|
1306 if (*it != i)
|
|
1307 --it;
|
|
1308 int row = qMax(0, it - m_sourceRowCache.begin());
|
|
1309 int offset = m_sourceRowCache[row];
|
|
1310 QModelIndex dateParent = index(row, 0);
|
|
1311 // If we can remove all the rows in the date do that and skip over them
|
|
1312 int rc = rowCount(dateParent);
|
|
1313 if (i - rc + 1 == offset && start <= i - rc + 1) {
|
|
1314 beginRemoveRows(QModelIndex(), row, row);
|
|
1315 m_sourceRowCache.removeAt(row);
|
|
1316 i -= rc + 1;
|
|
1317 } else {
|
|
1318 beginRemoveRows(dateParent, i - offset, i - offset);
|
|
1319 ++row;
|
|
1320 --i;
|
|
1321 }
|
|
1322 for (int j = row; j < m_sourceRowCache.count(); ++j)
|
|
1323 --m_sourceRowCache[j];
|
|
1324 endRemoveRows();
|
|
1325 }
|
|
1326 }
|
45
|
1327
|
|
1328 private:
|
|
1329
|
|
1330 // Translate the top level date row into the offset where that date starts
|
|
1331 int sourceDateRow(int row)
|
|
1332 {
|
|
1333 if (row <= 0)
|
|
1334 return 0;
|
|
1335
|
|
1336 if (m_sourceRowCache.isEmpty())
|
|
1337 rowCount(QModelIndex());
|
|
1338
|
|
1339 if (row >= m_sourceRowCache.count()) {
|
|
1340 if (!sourceModel())
|
|
1341 return 0;
|
|
1342 return sourceModel().rowCount();
|
|
1343 }
|
|
1344 return m_sourceRowCache.at(row);
|
|
1345 }
|
|
1346
|
74
|
1347 int[] m_sourceRowCache;
|
45
|
1348 }
|
|
1349
|
73
|
1350
|
45
|
1351 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
|
|
1352 // so filtering is only done on the children.
|
|
1353 // Used in the HistoryDialog
|
|
1354 class TreeProxyModel : public QSortFilterProxyModel
|
|
1355 {
|
65
|
1356 public:
|
45
|
1357
|
65
|
1358 this(QObject parent = null)
|
45
|
1359 {
|
|
1360 super(parent);
|
|
1361 setSortRole(HistoryModel.DateTimeRole);
|
|
1362 setFilterCaseSensitivity(Qt.CaseInsensitive);
|
|
1363 }
|
65
|
1364
|
|
1365 protected:
|
|
1366 bool filterAcceptsRow(int source_row, QModelIndex source_parent)
|
45
|
1367 {
|
|
1368 if (!source_parent.isValid())
|
|
1369 return true;
|
65
|
1370 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent);
|
45
|
1371 }
|
|
1372 }
|
|
1373
|
73
|
1374
|
65
|
1375 import ui_history;
|
45
|
1376
|
|
1377 class HistoryDialog : public QDialog, public Ui_HistoryDialog
|
|
1378 {
|
74
|
1379 mixin Signal!("openUrl", QUrl /*url*/);
|
45
|
1380
|
|
1381 public:
|
|
1382
|
74
|
1383 this(QWidget parent = null, HistoryManager history = null)
|
|
1384 //: QDialog(parent)
|
45
|
1385 {
|
65
|
1386 HistoryManager history = setHistory;
|
45
|
1387 if (!history)
|
65
|
1388 history = BrowserApplication.historyManager();
|
45
|
1389 setupUi(this);
|
|
1390 tree.setUniformRowHeights(true);
|
65
|
1391 tree.setSelectionBehavior(QAbstractItemView.SelectRows);
|
45
|
1392 tree.setTextElideMode(Qt.ElideMiddle);
|
65
|
1393 auto model = history.historyTreeModel();
|
|
1394 auto proxyModel = new TreeProxyModel(this);
|
|
1395 search.textChanged(QString).connect(&proxyModel.setFilterFixedString(QString));
|
|
1396 removeButton.clicked.connect(&tree.removeOne);
|
|
1397 removeAllButton.clicked.connect(&history.clear);
|
45
|
1398 proxyModel.setSourceModel(model);
|
|
1399 tree.setModel(proxyModel);
|
|
1400 tree.setExpanded(proxyModel.index(0, 0), true);
|
|
1401 tree.setAlternatingRowColors(true);
|
65
|
1402 QFontMetrics fm = font();
|
45
|
1403 int header = fm.width(QLatin1Char('m')) * 40;
|
|
1404 tree.header().resizeSection(0, header);
|
|
1405 tree.header().setStretchLastSection(true);
|
65
|
1406 tree.activated(QModelIndex).connect(this.open);
|
45
|
1407 tree.setContextMenuPolicy(Qt.CustomContextMenu);
|
65
|
1408 tree.customContextMenuRequested(QPoint).connect(&this.customContextMenuRequested(QPoint));
|
45
|
1409 }
|
|
1410
|
65
|
1411 private:
|
45
|
1412
|
65
|
1413 void customContextMenuRequested(QPoint pos)
|
45
|
1414 {
|
|
1415 QMenu menu;
|
|
1416 QModelIndex index = tree.indexAt(pos);
|
|
1417 index = index.sibling(index.row(), 0);
|
|
1418 if (index.isValid() && !tree.model().hasChildren(index)) {
|
|
1419 menu.addAction(tr("Open"), this, SLOT(open()));
|
|
1420 menu.addSeparator();
|
|
1421 menu.addAction(tr("Copy"), this, SLOT(copy()));
|
|
1422 }
|
|
1423 menu.addAction(tr("Delete"), tree, SLOT(removeOne()));
|
65
|
1424 menu.exec(QCursor.pos());
|
45
|
1425 }
|
|
1426
|
|
1427 void open()
|
|
1428 {
|
|
1429 QModelIndex index = tree.currentIndex();
|
|
1430 if (!index.parent().isValid())
|
|
1431 return;
|
|
1432 emit openUrl(index.data(HistoryModel.UrlRole).toUrl());
|
|
1433 }
|
|
1434
|
|
1435 void copy()
|
|
1436 {
|
|
1437 QModelIndex index = tree.currentIndex();
|
|
1438 if (!index.parent().isValid())
|
|
1439 return;
|
|
1440 QString url = index.data(HistoryModel.UrlStringRole).toString();
|
|
1441
|
65
|
1442 QClipboard clipboard = QApplication.clipboard();
|
45
|
1443 clipboard.setText(url);
|
|
1444 }
|
|
1445 }
|