comparison demos/browser/history.d @ 73:7bfd46c330dc

more porting
author mandel
date Fri, 22 May 2009 10:59:00 +0000
parents b149ef2cb18b
children 37caa90ce503
comparison
equal deleted inserted replaced
72:b149ef2cb18b 73:7bfd46c330dc
61 import qt.core.QHash; 61 import qt.core.QHash;
62 import qt.core.QObject; 62 import qt.core.QObject;
63 import qt.core.QTimer; 63 import qt.core.QTimer;
64 import qt.core.QUrl; 64 import qt.core.QUrl;
65 65
66 import QtWebKit.QWebHistoryInterface; 66 import qt.webkit.QWebHistoryInterface;
67 import QtWebKit.QWebSettings; 67 import qtWebkit.QWebSettings;
68 68
69 import QWebHistoryInterface; 69 import QWebHistoryInterface;
70 70
71 import autosaver 71 import autosaver
72 import browserapplication; 72 import browserapplication;
108 } 108 }
109 109
110 110
111 class HistoryManager : public QWebHistoryInterface 111 class HistoryManager : public QWebHistoryInterface
112 { 112 {
113 //Q_PROPERTY(int historyLimit READ historyLimit WRITE setHistoryLimit)
114
115 mixin Signal!("historyReset"); 113 mixin Signal!("historyReset");
116 mixin Signal!("entryAdded", HistoryItem item); 114 mixin Signal!("entryAdded", HistoryItem item);
117 mixin Signal!("entryRemoved", HistoryItem item); 115 mixin Signal!("entryRemoved", HistoryItem item);
118 mixin Signal!("entryUpdated", int offset); 116 mixin Signal!("entryUpdated", int offset);
119 117
142 140
143 // QWebHistoryInterface will delete the history manager 141 // QWebHistoryInterface will delete the history manager
144 QWebHistoryInterface.setDefaultInterface(this); 142 QWebHistoryInterface.setDefaultInterface(this);
145 } 143 }
146 144
147
148 ~this() 145 ~this()
149 { 146 {
150 m_saveTimer.saveIfNeccessary(); 147 m_saveTimer.saveIfNeccessary();
151 } 148 }
152
153 149
154 bool historyContains(QString &url) 150 bool historyContains(QString &url)
155 { 151 {
156 return m_historyFilterModel.historyContains(url); 152 return m_historyFilterModel.historyContains(url);
157 } 153 }
182 int historyLimit() 178 int historyLimit()
183 { 179 {
184 return m_historyLimit; 180 return m_historyLimit;
185 } 181 }
186 182
187
188 void setHistoryLimit(int limit) 183 void setHistoryLimit(int limit)
189 { 184 {
190 if (m_historyLimit == limit) 185 if (m_historyLimit == limit)
191 return; 186 return;
192 m_historyLimit = limit; 187 m_historyLimit = limit;
193 checkForExpired(); 188 checkForExpired();
194 m_saveTimer.changeOccurred(); 189 m_saveTimer.changeOccurred();
195 } 190 }
196 191
197
198 QList<HistoryItem> history() 192 QList<HistoryItem> history()
199 { 193 {
200 return m_history; 194 return m_history;
201 } 195 }
202
203 196
204 void setHistory(QList<HistoryItem> &history, bool loadedAndSorted = false); 197 void setHistory(QList<HistoryItem> &history, bool loadedAndSorted = false);
205 { 198 {
206 m_history = history; 199 m_history = history;
207 200
218 m_saveTimer.changeOccurred(); 211 m_saveTimer.changeOccurred();
219 } 212 }
220 emit historyReset(); 213 emit historyReset();
221 } 214 }
222 215
223
224 // History manager keeps around these models for use by the completer and other classes 216 // History manager keeps around these models for use by the completer and other classes
225 HistoryModel historyModel(); 217 HistoryModel historyModel();
226 { 218 {
227 return m_historyModel; 219 return m_historyModel;
228 } 220 }
229 221
230
231 HistoryFilterModel historyFilterModel() 222 HistoryFilterModel historyFilterModel()
232 { 223 {
233 return m_historyFilterModel; 224 return m_historyFilterModel;
234 } 225 }
235 226
236
237 HistoryTreeModel historyTreeModel() 227 HistoryTreeModel historyTreeModel()
238 { 228 {
239 return m_historyTreeModel; 229 return m_historyTreeModel;
240 } 230 }
241 231
242 232 public:
243 public slots:
244 233
245 void clear() 234 void clear()
246 { 235 {
247 m_history.clear(); 236 m_history.clear();
248 m_lastSavedUrl = QString(); 237 m_lastSavedUrl = QString();
443 HistoryModel m_historyModel; 432 HistoryModel m_historyModel;
444 HistoryFilterModel m_historyFilterModel; 433 HistoryFilterModel m_historyFilterModel;
445 HistoryTreeModel m_historyTreeModel; 434 HistoryTreeModel m_historyTreeModel;
446 } 435 }
447 436
437
448 class HistoryModel : public QAbstractTableModel 438 class HistoryModel : public QAbstractTableModel
449 { 439 {
450 public: 440 public:
451 441
452 void historyReset() 442 void historyReset()
471 enum Roles { 461 enum Roles {
472 DateRole = Qt.UserRole + 1, 462 DateRole = Qt.UserRole + 1,
473 DateTimeRole = Qt.UserRole + 2, 463 DateTimeRole = Qt.UserRole + 2,
474 UrlRole = Qt.UserRole + 3, 464 UrlRole = Qt.UserRole + 3,
475 UrlStringRole = Qt.UserRole + 4 465 UrlStringRole = Qt.UserRole + 4
476 } 466 };
477 467
478 this(HistoryManager history, QObject parent = null) 468 this(HistoryManager history, QObject parent = null)
479 { 469 {
480 super(parent); 470 super(parent);
481 m_history = history; 471 m_history = history;
627 return QModelIndex(); 617 return QModelIndex();
628 618
629 return createIndex(realRow, sourceIndex.column(), sourceModel().rowCount() - sourceIndex.row()); 619 return createIndex(realRow, sourceIndex.column(), sourceModel().rowCount() - sourceIndex.row());
630 } 620 }
631 621
632
633 QModelIndex mapToSource(QModelIndex proxyIndex) 622 QModelIndex mapToSource(QModelIndex proxyIndex)
634 { 623 {
635 load(); 624 load();
636 int sourceRow = sourceModel().rowCount() - proxyIndex.internalId(); 625 int sourceRow = sourceModel().rowCount() - proxyIndex.internalId();
637 return sourceModel().index(sourceRow, proxyIndex.column()); 626 return sourceModel().index(sourceRow, proxyIndex.column());
716 QVariant data(QModelIndex index, int role = Qt.DisplayRole); 705 QVariant data(QModelIndex index, int role = Qt.DisplayRole);
717 { 706 {
718 return QAbstractProxyModel.data(index, role); 707 return QAbstractProxyModel.data(index, role);
719 } 708 }
720 709
721 private slots: 710 private:
722 711
723 void sourceReset() 712 void sourceReset()
724 { 713 {
725 m_loaded = false; 714 m_loaded = false;
726 reset(); 715 reset();
727 } 716 }
728
729 717
730 void sourceDataChanged(QModelIndex topLeft, QModelIndex bottomRight) 718 void sourceDataChanged(QModelIndex topLeft, QModelIndex bottomRight)
731 { 719 {
732 emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight)); 720 emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight));
733 } 721 }
736 { 724 {
737 //Q_UNUSED(start); 725 //Q_UNUSED(start);
738 //Q_UNUSED(end); 726 //Q_UNUSED(end);
739 sourceReset(); 727 sourceReset();
740 } 728 }
741
742 729
743 void sourceRowsInserted(QModelIndex parent, int start, int end) 730 void sourceRowsInserted(QModelIndex parent, int start, int end)
744 { 731 {
745 assert(start == end && start == 0); 732 assert(start == end && start == 0);
746 //Q_UNUSED(end); 733 //Q_UNUSED(end);
785 QList<int> m_sourceRow; 772 QList<int> m_sourceRow;
786 QHash<QString, int> m_historyHash; 773 QHash<QString, int> m_historyHash;
787 bool m_loaded; 774 bool m_loaded;
788 } 775 }
789 776
777
790 /* 778 /*
791 The history menu 779 The history menu
792 - Removes the first twenty entries and puts them as children of the top level. 780 - Removes the first twenty entries and puts them as children of the top level.
793 - If there are less then twenty entries then the first folder is also removed. 781 - If there are less then twenty entries then the first folder is also removed.
794 782
916 private: 904 private:
917 905
918 HistoryTreeModel m_treeModel; 906 HistoryTreeModel m_treeModel;
919 } 907 }
920 908
909
921 // Menu that is dynamically populated from the history 910 // Menu that is dynamically populated from the history
922 class HistoryMenu : public ModelMenu 911 class HistoryMenu : public ModelMenu
923 { 912 {
924 mixin Signal!("openUrl", QUrl url); 913 mixin Signal!("openUrl", QUrl url);
925 914
971 QAction clearAction = new QAction(tr("Clear History"), this); 960 QAction clearAction = new QAction(tr("Clear History"), this);
972 clearAction.triggered.connect(&m_history.clear); 961 clearAction.triggered.connect(&m_history.clear);
973 addAction(clearAction); 962 addAction(clearAction);
974 } 963 }
975 964
976 private slots: 965 private:
966
977 void activated(QModelIndex index) 967 void activated(QModelIndex index)
978 { 968 {
979 emit openUrl(index.data(HistoryModel.UrlRole).toUrl()); 969 emit openUrl(index.data(HistoryModel.UrlRole).toUrl());
980 } 970 }
981 971
983 { 973 {
984 auto dialog = new HistoryDialog(this); 974 auto dialog = new HistoryDialog(this);
985 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl)); 975 dialog.openUrl(QUrl).connect(&this.openUrl(QUrl));
986 dialog.show(); 976 dialog.show();
987 } 977 }
988
989 978
990 private: 979 private:
991 980
992 HistoryManager m_history; 981 HistoryManager m_history;
993 HistoryMenuModel m_historyMenuModel; 982 HistoryMenuModel m_historyMenuModel;
997 986
998 // proxy model for the history model that 987 // proxy model for the history model that
999 // exposes each url http://www.foo.com and it url starting at the host www.foo.com 988 // exposes each url http://www.foo.com and it url starting at the host www.foo.com
1000 class HistoryCompletionModel : public QAbstractProxyModel 989 class HistoryCompletionModel : public QAbstractProxyModel
1001 { 990 {
1002
1003 public: 991 public:
992
1004 this(QObject parent = null) 993 this(QObject parent = null)
1005 { 994 {
1006 super(parent); 995 super(parent);
1007 } 996 }
1008 997
1009 QVariant data(QModelIndex index, int role) 998 QVariant data(QModelIndex index, int role)
1010 { 999 {
1011 if (sourceModel() 1000 if (sourceModel() && (role == Qt.EditRole || role == Qt.DisplayRole) && index.isValid()) {
1012 && (role == Qt.EditRole || role == Qt.DisplayRole)
1013 && index.isValid()) {
1014 QModelIndex idx = mapToSource(index); 1001 QModelIndex idx = mapToSource(index);
1015 idx = idx.sibling(idx.row(), 1); 1002 idx = idx.sibling(idx.row(), 1);
1016 QString urlString = idx.data(HistoryModel.UrlStringRole).toString(); 1003 QString urlString = idx.data(HistoryModel.UrlStringRole).toString();
1017 if (index.row() % 2) { 1004 if (index.row() % 2) {
1018 QUrl url = urlString; 1005 QUrl url = urlString;
1063 1050
1064 void setSourceModel(QAbstractItemModel sourceModel); 1051 void setSourceModel(QAbstractItemModel sourceModel);
1065 { 1052 {
1066 if (sourceModel()) { 1053 if (sourceModel()) {
1067 sourceModel.modelReset.disconnect(&this.sourceReset); 1054 sourceModel.modelReset.disconnect(&this.sourceReset);
1068 sourceModel.rowsInserted(QModelIndex , int, int).disconnect(&this.sourceReset); 1055 sourceModel.rowsInserted.disconnect(&this.sourceReset);
1069 sourceModel.rowsRemoved(QModelIndex , int, int).disconnect(&this.sourceReset); 1056 sourceModel.rowsRemoved.disconnect(&this.sourceReset);
1070 } 1057 }
1071 1058
1072 QAbstractProxyModel.setSourceModel(newSourceModel); 1059 QAbstractProxyModel.setSourceModel(newSourceModel);
1073 1060
1074 if (newSourceModel) { 1061 if (newSourceModel) {
1075 newSourceModel.modelReset.connect(&this.sourceReset); 1062 newSourceModel.modelReset.connect(&this.sourceReset);
1076 sourceModel.rowsInserted(QModelIndex , int, int).connect(&this.sourceReset); 1063 sourceModel.rowsInserted.connect(&this.sourceReset);
1077 sourceModel.rowsRemoved(QModelIndex , int, int).connect(&this.sourceReset); 1064 sourceModel.rowsRemoved.connect(&this.sourceReset);
1078 } 1065 }
1079 1066
1080 reset(); 1067 reset();
1081 } 1068 }
1082 1069
1083 private slots: 1070 private:
1084 1071
1085 void sourceReset() 1072 void sourceReset()
1086 { 1073 {
1087 reset(); 1074 reset();
1088 } 1075 }
1089 } 1076 }
1077
1090 1078
1091 // proxy model for the history model that converts the list 1079 // proxy model for the history model that converts the list
1092 // into a tree, one top level node per day. 1080 // into a tree, one top level node per day.
1093 // Used in the HistoryDialog. 1081 // Used in the HistoryDialog.
1094 class HistoryTreeModel : public QAbstractProxyModel 1082 class HistoryTreeModel : public QAbstractProxyModel
1251 } 1239 }
1252 } 1240 }
1253 return true; 1241 return true;
1254 } 1242 }
1255 1243
1256
1257 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole) 1244 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole)
1258 { 1245 {
1259 return sourceModel().headerData(section, orientation, role); 1246 return sourceModel().headerData(section, orientation, role);
1260 } 1247 }
1261
1262
1263 1248
1264 void setSourceModel(QAbstractItemModel newSourceModel) 1249 void setSourceModel(QAbstractItemModel newSourceModel)
1265 { 1250 {
1266 if (sourceModel()) { 1251 if (sourceModel()) {
1267 sourceModel.modelReset.disconnect(&this.sourceReset); 1252 sourceModel.modelReset.disconnect(&this.sourceReset);
1280 } 1265 }
1281 1266
1282 reset(); 1267 reset();
1283 } 1268 }
1284 1269
1285 1270 private:
1286 private slots:
1287 1271
1288 void sourceReset() 1272 void sourceReset()
1289 { 1273 {
1290 m_sourceRowCache.clear(); 1274 m_sourceRowCache.clear();
1291 reset(); 1275 reset();
1292 } 1276 }
1293 1277
1294 void sourceRowsInserted(QModelIndex parent, int start, int end); 1278 void sourceRowsInserted(QModelIndex parent, int start, int end);
1295 { 1279 {
1296 Q_UNUSED(parent); // Avoid warnings when compiling release 1280 //Q_UNUSED(parent); // Avoid warnings when compiling release
1297 assert(!parent.isValid()); 1281 assert(!parent.isValid());
1298 if (start != 0 || start != end) { 1282 if (start != 0 || start != end) {
1299 m_sourceRowCache.clear(); 1283 m_sourceRowCache.clear();
1300 reset(); 1284 reset();
1301 return; 1285 return;
1371 } 1355 }
1372 1356
1373 QList<int> m_sourceRowCache; 1357 QList<int> m_sourceRowCache;
1374 } 1358 }
1375 1359
1360
1376 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree 1361 // A modified QSortFilterProxyModel that always accepts the root nodes in the tree
1377 // so filtering is only done on the children. 1362 // so filtering is only done on the children.
1378 // Used in the HistoryDialog 1363 // Used in the HistoryDialog
1379 class TreeProxyModel : public QSortFilterProxyModel 1364 class TreeProxyModel : public QSortFilterProxyModel
1380 { 1365 {
1393 if (!source_parent.isValid()) 1378 if (!source_parent.isValid())
1394 return true; 1379 return true;
1395 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent); 1380 return QSortFilterProxyModel.filterAcceptsRow(source_row, source_parent);
1396 } 1381 }
1397 } 1382 }
1383
1398 1384
1399 import ui_history; 1385 import ui_history;
1400 1386
1401 class HistoryDialog : public QDialog, public Ui_HistoryDialog 1387 class HistoryDialog : public QDialog, public Ui_HistoryDialog
1402 { 1388 {
1445 } 1431 }
1446 menu.addAction(tr("Delete"), tree, SLOT(removeOne())); 1432 menu.addAction(tr("Delete"), tree, SLOT(removeOne()));
1447 menu.exec(QCursor.pos()); 1433 menu.exec(QCursor.pos());
1448 } 1434 }
1449 1435
1450
1451 void open() 1436 void open()
1452 { 1437 {
1453 QModelIndex index = tree.currentIndex(); 1438 QModelIndex index = tree.currentIndex();
1454 if (!index.parent().isValid()) 1439 if (!index.parent().isValid())
1455 return; 1440 return;