Mercurial > projects > qtd
comparison demos/browser/history.d @ 94:87bb4e622f9e
more porting
author | mandel |
---|---|
date | Thu, 28 May 2009 22:11:52 +0000 |
parents | 5c8c9c5d9ee1 |
children | dcd36d8db2da |
comparison
equal
deleted
inserted
replaced
93:55fd7080c4b9 | 94:87bb4e622f9e |
---|---|
45 import qt.gui.QClipboard; | 45 import qt.gui.QClipboard; |
46 import qt.gui.QDesktopServices; | 46 import qt.gui.QDesktopServices; |
47 import qt.gui.QHeaderView; | 47 import qt.gui.QHeaderView; |
48 import qt.gui.QStyle; | 48 import qt.gui.QStyle; |
49 import qt.gui.QSortFilterProxyModel; | 49 import qt.gui.QSortFilterProxyModel; |
50 | 50 import qt.gui.QAbstractTableModel; |
51 import qt.core.QtAlgorithms; | 51 |
52 //import qt.core.QtAlgorithms; | |
52 import qt.core.QBuffer; | 53 import qt.core.QBuffer; |
53 import qt.core.QDir; | 54 import qt.core.QDir; |
54 import qt.core.QFile; | 55 import qt.core.QFile; |
55 import qt.core.QFileInfo; | 56 import qt.core.QFileInfo; |
56 import qt.core.QSettings; | 57 import qt.core.QSettings; |
57 import qt.core.QTemporaryFile; | 58 import qt.core.QTemporaryFile; |
58 import qt.core.QTextStream; | 59 import qt.core.QTextStream; |
59 import qt.core.QDebug; | 60 //import qt.core.QDebug; |
60 import qt.core.QDateTime; | 61 import qt.core.QDateTime; |
61 import qt.core.QHash; | 62 //import qt.core.QHash; |
62 import qt.core.QObject; | 63 import qt.core.QObject; |
63 import qt.core.QTimer; | 64 import qt.core.QTimer; |
64 import qt.core.QUrl; | 65 import qt.core.QUrl; |
65 | 66 |
66 import qt.webkit.QWebHistoryInterface; | 67 import qt.webkit.QWebHistoryInterface; |
67 import qt.webkit.QWebSettings; | 68 import qt.webkit.QWebSettings; |
68 | 69 |
69 import QWebHistoryInterface; | 70 //import QWebHistoryInterface; |
70 | 71 |
71 import autosaver; | 72 import autosaver; |
72 import browserapplication; | 73 import browserapplication; |
73 import modelmenu; | 74 import modelmenu; |
74 | 75 |
289 } else { | 290 } else { |
290 open = historyFile.open(QFile.Append); | 291 open = historyFile.open(QFile.Append); |
291 } | 292 } |
292 | 293 |
293 if (!open) { | 294 if (!open) { |
294 qWarning() << "Unable to open history file for saving" | 295 qWarning("Unable to open history file for saving" ~ (saveAll ? tempFile.fileName() : historyFile.fileName())); |
295 << (saveAll ? tempFile.fileName() : historyFile.fileName()); | |
296 return; | 296 return; |
297 } | 297 } |
298 | 298 |
299 QDataStream out_ = (saveAll ? tempFile : historyFile); | 299 QDataStream out_ = (saveAll ? tempFile : historyFile); |
300 for (int i = first; i >= 0; --i) { | 300 for (int i = first; i >= 0; --i) { |
306 } | 306 } |
307 tempFile.close(); | 307 tempFile.close(); |
308 | 308 |
309 if (saveAll) { | 309 if (saveAll) { |
310 if (historyFile.exists() && !historyFile.remove()) | 310 if (historyFile.exists() && !historyFile.remove()) |
311 qWarning() << "History: error removing old history." << historyFile.errorString(); | 311 qWarning("History: error removing old history." ~ historyFile.errorString()); |
312 if (!tempFile.rename(historyFile.fileName())) | 312 if (!tempFile.rename(historyFile.fileName())) |
313 qWarning() << "History: error moving new history over old." << tempFile.errorString() << historyFile.fileName(); | 313 qWarning("History: error moving new history over old." ~ tempFile.errorString() ~ historyFile.fileName()); |
314 } | 314 } |
315 m_lastSavedUrl = m_history.value(0).url; | 315 m_lastSavedUrl = m_history.value(0).url; |
316 } | 316 } |
317 | 317 |
318 void checkForExpired() | 318 void checkForExpired() |
366 | 366 |
367 historyFile = new QFile(QDesktopServices.storageLocation(QDesktopServices.DataLocation) ~ "/history"); | 367 historyFile = new QFile(QDesktopServices.storageLocation(QDesktopServices.DataLocation) ~ "/history"); |
368 if (!historyFile.exists()) | 368 if (!historyFile.exists()) |
369 return; | 369 return; |
370 if (!historyFile.open(QFile.ReadOnly)) { | 370 if (!historyFile.open(QFile.ReadOnly)) { |
371 qWarning() << "Unable to open history file" << historyFile.fileName(); | 371 qWarning("Unable to open history file" ~ historyFile.fileName()); |
372 return; | 372 return; |
373 } | 373 } |
374 | 374 |
375 HistoryItem[] list; | 375 HistoryItem[] list; |
376 auto in_ = new QDataStream(&historyFile); | 376 auto in_ = new QDataStream(&historyFile); |
457 } | 457 } |
458 | 458 |
459 public: | 459 public: |
460 | 460 |
461 enum Roles { | 461 enum Roles { |
462 DateRole = Qt.UserRole + 1, | 462 DateRole = Qt_ItemDataRole.UserRole + 1, |
463 DateTimeRole = Qt.UserRole + 2, | 463 DateTimeRole = Qt_ItemDataRole.UserRole + 2, |
464 UrlRole = Qt.UserRole + 3, | 464 UrlRole = Qt_ItemDataRole.UserRole + 3, |
465 UrlStringRole = Qt.UserRole + 4 | 465 UrlStringRole = Qt_ItemDataRole.UserRole + 4 |
466 }; | 466 }; |
467 | 467 |
468 this(HistoryManager history, QObject parent = null) | 468 this(HistoryManager history, QObject parent = null) |
469 { | 469 { |
470 super(parent); | 470 super(parent); |
475 m_history.entryRemoved.connect(&this.historyReset); | 475 m_history.entryRemoved.connect(&this.historyReset); |
476 m_history.entryAdded.connect(&this.entryAdded); | 476 m_history.entryAdded.connect(&this.entryAdded); |
477 m_history.entryUpdated.connect(&this.entryUpdated); | 477 m_history.entryUpdated.connect(&this.entryUpdated); |
478 } | 478 } |
479 | 479 |
480 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole) | 480 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole) |
481 { | 481 { |
482 if (orientation == Qt.Horizontal && role == Qt.DisplayRole) { | 482 if (orientation == Qt_Orientation.Horizontal && role == Qt_Orientation.DisplayRole) { |
483 switch (section) { | 483 switch (section) { |
484 case 0: return tr("Title"); | 484 case 0: return tr("Title"); |
485 case 1: return tr("Address"); | 485 case 1: return tr("Address"); |
486 } | 486 } |
487 } | 487 } |
488 return QAbstractTableModel.headerData(section, orientation, role); | 488 return QAbstractTableModel.headerData(section, orientation, role); |
489 } | 489 } |
490 | 490 |
491 QVariant data(QModelIndex index, int role = Qt.DisplayRole) | 491 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole) |
492 { | 492 { |
493 HistoryItem[] lst = m_history.history(); | 493 HistoryItem[] lst = m_history.history(); |
494 if (index.row() < 0 || index.row() >= lst.length) | 494 if (index.row() < 0 || index.row() >= lst.length) |
495 return QVariant(); | 495 return new QVariant(); |
496 | 496 |
497 HistoryItem item = lst[index.row()]; | 497 HistoryItem item = lst[index.row()]; |
498 switch (role) { | 498 switch (role) { |
499 case DateTimeRole: | 499 case DateTimeRole: |
500 return item.dateTime; | 500 return item.dateTime; |
501 case DateRole: | 501 case DateRole: |
502 return item.dateTime.date(); | 502 return item.dateTime.date(); |
503 case UrlRole: | 503 case UrlRole: |
504 return QUrl(item.url); | 504 return new QUrl(item.url); |
505 case UrlStringRole: | 505 case UrlStringRole: |
506 return item.url; | 506 return item.url; |
507 case Qt.DisplayRole: | 507 case Qt_ItemDataRole.DisplayRole: |
508 case Qt.EditRole: { | 508 case Qt_ItemDataRole.EditRole: { |
509 switch (index.column()) { | 509 switch (index.column()) { |
510 case 0: | 510 case 0: |
511 // when there is no title try to generate one from the url | 511 // when there is no title try to generate one from the url |
512 if (item.title.isEmpty()) { | 512 if (item.title.isEmpty()) { |
513 string page = QFileInfo(QUrl(item.url).path()).fileName(); | 513 string page = (new QFileInfo((new QUrl(item.url)).path())).fileName(); |
514 if (!page.isEmpty()) | 514 if (!page.isEmpty()) |
515 return page; | 515 return page; |
516 return item.url; | 516 return item.url; |
517 } | 517 } |
518 return item.title; | 518 return item.title; |
519 case 1: | 519 case 1: |
520 return item.url; | 520 return item.url; |
521 } | 521 } |
522 } | 522 } |
523 case Qt.DecorationRole: | 523 case Qt_ItemDataRole.DecorationRole: |
524 if (index.column() == 0) { | 524 if (index.column() == 0) { |
525 return BrowserApplication.instance().icon(item.url); | 525 return BrowserApplication.instance().icon(item.url); |
526 } | 526 } |
527 } | 527 } |
528 return QVariant(); | 528 return new QVariant(); |
529 } | 529 } |
530 | 530 |
531 int columnCount(QModelIndex parent = QModelIndex()) | 531 int columnCount(QModelIndex parent = QModelIndex()) |
532 { | 532 { |
533 return (parent.isValid()) ? 0 : 2; | 533 return (parent.isValid()) ? 0 : 2; |
644 sourceModel.rowsInserted.connect(&this.sourceRowsInserted); | 644 sourceModel.rowsInserted.connect(&this.sourceRowsInserted); |
645 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved); | 645 sourceModel.rowsRemoved.connect(&this.sourceRowsRemoved); |
646 } | 646 } |
647 } | 647 } |
648 | 648 |
649 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole) | 649 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole) |
650 { | 650 { |
651 return sourceModel().headerData(section, orientation, role); | 651 return sourceModel().headerData(section, orientation, role); |
652 } | 652 } |
653 | 653 |
654 int rowCount(QModelIndex parent = QModelIndex()) | 654 int rowCount(QModelIndex parent = QModelIndex()) |
699 if (oldCount - count != rowCount()) | 699 if (oldCount - count != rowCount()) |
700 reset(); | 700 reset(); |
701 return true; | 701 return true; |
702 } | 702 } |
703 | 703 |
704 QVariant data(QModelIndex index, int role = Qt.DisplayRole) | 704 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole) |
705 { | 705 { |
706 return QAbstractProxyModel.data(index, role); | 706 return QAbstractProxyModel.data(index, role); |
707 } | 707 } |
708 | 708 |
709 private: | 709 private: |
991 super(parent); | 991 super(parent); |
992 } | 992 } |
993 | 993 |
994 QVariant data(QModelIndex index, int role) | 994 QVariant data(QModelIndex index, int role) |
995 { | 995 { |
996 if (sourceModel() && (role == Qt.EditRole || role == Qt.DisplayRole) && index.isValid()) { | 996 if (sourceModel() && (role == Qt_ItemDataRole.EditRole || role == Qt_ItemDataRole.DisplayRole) && index.isValid()) { |
997 QModelIndex idx = mapToSource(index); | 997 QModelIndex idx = mapToSource(index); |
998 idx = idx.sibling(idx.row(), 1); | 998 idx = idx.sibling(idx.row(), 1); |
999 string urlString = idx.data(HistoryModel.UrlStringRole).toString(); | 999 string urlString = idx.data(HistoryModel.UrlStringRole).toString(); |
1000 if (index.row() % 2) { | 1000 if (index.row() % 2) { |
1001 QUrl url = urlString; | 1001 QUrl url = urlString; |
1082 { | 1082 { |
1083 super(parent); | 1083 super(parent); |
1084 setSourceModel(sourceModel); | 1084 setSourceModel(sourceModel); |
1085 } | 1085 } |
1086 | 1086 |
1087 QVariant data(QModelIndex index, int role = Qt.DisplayRole) | 1087 QVariant data(QModelIndex index, int role = Qt_ItemDataRole.DisplayRole) |
1088 { | 1088 { |
1089 if ((role == Qt.EditRole || role == Qt.DisplayRole)) { | 1089 if ((role == Qt_ItemDataRole.EditRole || role == Qt_ItemDataRole.DisplayRole)) { |
1090 int start = index.internalId(); | 1090 int start = index.internalId(); |
1091 if (start == 0) { | 1091 if (start == 0) { |
1092 int offset = sourceDateRow(index.row()); | 1092 int offset = sourceDateRow(index.row()); |
1093 if (index.column() == 0) { | 1093 if (index.column() == 0) { |
1094 QModelIndex idx = sourceModel().index(offset, 0); | 1094 QModelIndex idx = sourceModel().index(offset, 0); |
1100 if (index.column() == 1) { | 1100 if (index.column() == 1) { |
1101 return Format(tr("{} items"), rowCount(index.sibling(index.row(), 0))); | 1101 return Format(tr("{} items"), rowCount(index.sibling(index.row(), 0))); |
1102 } | 1102 } |
1103 } | 1103 } |
1104 } | 1104 } |
1105 if (role == Qt.DecorationRole && index.column() == 0 && !index.parent().isValid()) | 1105 if (role == Qt_ItemDataRole.DecorationRole && index.column() == 0 && !index.parent().isValid()) |
1106 return new QIcon(":history.png"); | 1106 return new QIcon(":history.png"); |
1107 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) { | 1107 if (role == HistoryModel.DateRole && index.column() == 0 && index.internalId() == 0) { |
1108 int offset = sourceDateRow(index.row()); | 1108 int offset = sourceDateRow(index.row()); |
1109 QModelIndex idx = sourceModel().index(offset, 0); | 1109 QModelIndex idx = sourceModel().index(offset, 0); |
1110 return idx.data(HistoryModel.DateRole); | 1110 return idx.data(HistoryModel.DateRole); |
1201 if (!grandparent.isValid()) | 1201 if (!grandparent.isValid()) |
1202 return true; | 1202 return true; |
1203 return false; | 1203 return false; |
1204 } | 1204 } |
1205 | 1205 |
1206 Qt.ItemFlags flags(QModelIndex index) | 1206 int flags(QModelIndex index) |
1207 { | 1207 { |
1208 if (!index.isValid()) | 1208 if (!index.isValid()) |
1209 return Qt.NoItemFlags; | 1209 return Qt_ItemFlag.NoItemFlags; |
1210 return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled; | 1210 return Qt_ItemFlag.ItemIsSelectable | Qt_ItemFlag.ItemIsEnabled | Qt_ItemFlag.ItemIsDragEnabled; |
1211 } | 1211 } |
1212 | 1212 |
1213 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) | 1213 bool removeRows(int row, int count, QModelIndex parent = QModelIndex()) |
1214 { | 1214 { |
1215 if (row < 0 || count <= 0 || row + count > rowCount(parent)) | 1215 if (row < 0 || count <= 0 || row + count > rowCount(parent)) |
1229 } | 1229 } |
1230 } | 1230 } |
1231 return true; | 1231 return true; |
1232 } | 1232 } |
1233 | 1233 |
1234 QVariant headerData(int section, Qt.Orientation orientation, int role = Qt.DisplayRole) | 1234 QVariant headerData(int section, Qt_Orientation orientation, int role = Qt_ItemDataRole.DisplayRole) |
1235 { | 1235 { |
1236 return sourceModel().headerData(section, orientation, role); | 1236 return sourceModel().headerData(section, orientation, role); |
1237 } | 1237 } |
1238 | 1238 |
1239 void setSourceModel(QAbstractItemModel newSourceModel) | 1239 void setSourceModel(QAbstractItemModel newSourceModel) |
1357 | 1357 |
1358 this(QObject parent = null) | 1358 this(QObject parent = null) |
1359 { | 1359 { |
1360 super(parent); | 1360 super(parent); |
1361 setSortRole(HistoryModel.DateTimeRole); | 1361 setSortRole(HistoryModel.DateTimeRole); |
1362 setFilterCaseSensitivity(Qt.CaseInsensitive); | 1362 setFilterCaseSensitivity(Qt_CaseSensitivity.CaseInsensitive); |
1363 } | 1363 } |
1364 | 1364 |
1365 protected: | 1365 protected: |
1366 | 1366 |
1367 bool filterAcceptsRow(int source_row, QModelIndex source_parent) | 1367 bool filterAcceptsRow(int source_row, QModelIndex source_parent) |
1373 } | 1373 } |
1374 | 1374 |
1375 | 1375 |
1376 import ui_history; | 1376 import ui_history; |
1377 | 1377 |
1378 class HistoryDialog : public QDialog, public Ui_HistoryDialog | 1378 class HistoryDialog : public QDialog//, public Ui_HistoryDialog |
1379 { | 1379 { |
1380 HistoryDialog ui; | |
1381 | |
1380 mixin Signal!("openUrl", QUrl /*url*/); | 1382 mixin Signal!("openUrl", QUrl /*url*/); |
1381 | 1383 |
1382 public: | 1384 public: |
1383 | 1385 |
1384 this(QWidget parent = null, HistoryManager history = null) | 1386 this(QWidget parent = null, HistoryManager history = null) |
1385 //: QDialog(parent) | 1387 { |
1386 { | 1388 super(parent); |
1387 HistoryManager history = setHistory; | 1389 HistoryManager history = setHistory; |
1388 if (!history) | 1390 if (!history) |
1389 history = BrowserApplication.historyManager(); | 1391 history = BrowserApplication.historyManager(); |
1390 setupUi(this); | 1392 ui.setupUi(this); |
1391 tree.setUniformRowHeights(true); | 1393 tree.setUniformRowHeights(true); |
1392 tree.setSelectionBehavior(QAbstractItemView.SelectRows); | 1394 tree.setSelectionBehavior(QAbstractItemView.SelectRows); |
1393 tree.setTextElideMode(Qt.ElideMiddle); | 1395 tree.setTextElideMode(Qt_TextElideMode.ElideMiddle); |
1394 auto model = history.historyTreeModel(); | 1396 auto model = history.historyTreeModel(); |
1395 auto proxyModel = new TreeProxyModel(this); | 1397 auto proxyModel = new TreeProxyModel(this); |
1396 search.textChanged.connect(&proxyModel.setFilterFixedString); | 1398 search.textChanged.connect(&proxyModel.setFilterFixedString); |
1397 removeButton.clicked.connect(&tree.removeOne); | 1399 removeButton.clicked.connect(&tree.removeOne); |
1398 removeAllButton.clicked.connect(&history.clear); | 1400 removeAllButton.clicked.connect(&history.clear); |
1403 QFontMetrics fm = font(); | 1405 QFontMetrics fm = font(); |
1404 int header = fm.width(QLatin1Char('m')) * 40; | 1406 int header = fm.width(QLatin1Char('m')) * 40; |
1405 tree.header().resizeSection(0, header); | 1407 tree.header().resizeSection(0, header); |
1406 tree.header().setStretchLastSection(true); | 1408 tree.header().setStretchLastSection(true); |
1407 tree.activated(QModelIndex).connect(this.open); | 1409 tree.activated(QModelIndex).connect(this.open); |
1408 tree.setContextMenuPolicy(Qt.CustomContextMenu); | 1410 tree.setContextMenuPolicy(Qt_ContextMenuPolicy.CustomContextMenu); |
1409 tree.customContextMenuRequested.connect(&this.customContextMenuRequested); | 1411 tree.customContextMenuRequested.connect(&this.customContextMenuRequested); |
1410 } | 1412 } |
1411 | 1413 |
1412 private: | 1414 private: |
1413 | 1415 |