|
| 1 | +// Author: Kang Lin <[email protected]> |
| 2 | + |
| 3 | +#include <QApplication> |
| 4 | +#include <QClipboard> |
| 5 | +#include <QMessageBox> |
| 6 | +#include <QMenu> |
| 7 | +#include <QLoggingCategory> |
| 8 | +#include "FrmHistory.h" |
| 9 | +#include "ui_FrmHistory.h" |
| 10 | + |
| 11 | +static Q_LOGGING_CATEGORY(log, "WebBrowser.History") |
| 12 | +CFrmHistory::CFrmHistory(CHistoryDatabase *pDatabase, QWidget *parent) |
| 13 | + : QWidget(parent) |
| 14 | + , ui(new Ui::CFrmHistory) |
| 15 | + , m_pModelHistory(nullptr) |
| 16 | +{ |
| 17 | + ui->setupUi(this); |
| 18 | + ui->treeView->hide(); |
| 19 | + |
| 20 | + setWindowTitle(tr("History")); |
| 21 | + |
| 22 | + bool check = false; |
| 23 | + check = connect(ui->tableView, &QTableView::customContextMenuRequested, |
| 24 | + this, &CFrmHistory::slotCustomContextMenuRequested); |
| 25 | + ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 26 | + ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 27 | + ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 28 | + |
| 29 | + if(pDatabase) { |
| 30 | + m_pModelHistory = new CHistoryModel(pDatabase, this); |
| 31 | + if(m_pModelHistory) { |
| 32 | + ui->tableView->setModel(m_pModelHistory); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + ui->tableView->resizeColumnsToContents(); |
| 37 | +} |
| 38 | + |
| 39 | +CFrmHistory::~CFrmHistory() |
| 40 | +{ |
| 41 | + delete ui; |
| 42 | +} |
| 43 | + |
| 44 | +void CFrmHistory::on_tableView_doubleClicked(const QModelIndex &index) |
| 45 | +{ |
| 46 | + if(!index.isValid()) return; |
| 47 | + auto item = m_pModelHistory->getItem(index); |
| 48 | + emit sigOpenUrl(item.url); |
| 49 | +} |
| 50 | + |
| 51 | +void CFrmHistory::slotCustomContextMenuRequested(const QPoint &pos) |
| 52 | +{ |
| 53 | + auto index = ui->tableView->indexAt(pos); |
| 54 | + QMenu menu(this); |
| 55 | + // 添加菜单项 |
| 56 | + // 只有在有效行上才显示某些菜单项 |
| 57 | + if (index.isValid()) { |
| 58 | + // 获取当前行的数据 |
| 59 | + QString url = ui->tableView->model()->data( |
| 60 | + index.siblingAtColumn(CHistoryModel::ColumnUrl)).toString(); |
| 61 | + QString title = ui->tableView->model()->data( |
| 62 | + index.siblingAtColumn(CHistoryModel::ColumnTitle)).toString(); |
| 63 | + |
| 64 | + // 打开操作 |
| 65 | + QAction *openAction = menu.addAction(QIcon::fromTheme("document-open"), |
| 66 | + tr("Open")); |
| 67 | + connect(openAction, &QAction::triggered, this, [this, url]() { |
| 68 | + emit sigOpenUrl(url); |
| 69 | + }); |
| 70 | + |
| 71 | + // 在新标签页中打开 |
| 72 | + QAction *openNewTabAction = menu.addAction( |
| 73 | + QIcon::fromTheme("document-open"), |
| 74 | + tr("Open in new tab")); |
| 75 | + connect(openNewTabAction, &QAction::triggered, this, [this, url]() { |
| 76 | + emit sigOpenUrlInNewTab(url); |
| 77 | + }); |
| 78 | + |
| 79 | + // 复制URL |
| 80 | + QAction *copyUrlAction = menu.addAction(QIcon::fromTheme("edit-copy"), |
| 81 | + tr("Copy url")); |
| 82 | + connect(copyUrlAction, &QAction::triggered, this, [url]() { |
| 83 | + QApplication::clipboard()->setText(url); |
| 84 | + }); |
| 85 | + |
| 86 | + // 复制标题 |
| 87 | + QAction *copyTitleAction = menu.addAction( |
| 88 | + QIcon::fromTheme("edit-copy"), |
| 89 | + tr("Copy title")); |
| 90 | + connect(copyTitleAction, &QAction::triggered, this, [title]() { |
| 91 | + QApplication::clipboard()->setText(title); |
| 92 | + }); |
| 93 | + |
| 94 | + menu.addSeparator(); |
| 95 | + |
| 96 | + // 删除操作 |
| 97 | + QAction *deleteAction = menu.addAction( |
| 98 | + QIcon::fromTheme("edit-delete"), tr("Delete")); |
| 99 | + connect(deleteAction, &QAction::triggered, this, [this, index]() { |
| 100 | + onDeleteHistoryItem(index); |
| 101 | + }); |
| 102 | + |
| 103 | + // 删除所有类似网站 |
| 104 | + QString domain = extractDomain(url); |
| 105 | + if (!domain.isEmpty()) { |
| 106 | + QAction *deleteDomainAction = menu.addAction( |
| 107 | + QIcon::fromTheme("edit-delete"), |
| 108 | + tr("Delete all urls from %1").arg(domain)); |
| 109 | + connect(deleteDomainAction, &QAction::triggered, this, [this, domain]() { |
| 110 | + onDeleteHistoryByDomain(domain); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + menu.addSeparator(); |
| 115 | + |
| 116 | + // 属性/详细信息 |
| 117 | + QAction *propertiesAction = menu.addAction( |
| 118 | + QIcon::fromTheme("document-properties"), tr("Properties")); |
| 119 | + connect(propertiesAction, &QAction::triggered, this, [this, index]() { |
| 120 | + onShowHistoryProperties(index); |
| 121 | + }); |
| 122 | + } else { |
| 123 | + // 在没有选中行的情况下显示的菜单项 |
| 124 | + menu.addAction(tr("No item selected"))->setEnabled(false); |
| 125 | + } |
| 126 | + |
| 127 | + menu.addSeparator(); |
| 128 | + |
| 129 | + // 总是显示的菜单项 |
| 130 | + QAction *refreshAction = menu.addAction( |
| 131 | + QIcon::fromTheme("view-refresh"), tr("Refresh")); |
| 132 | + connect(refreshAction, &QAction::triggered, this, [&]() { |
| 133 | + if(m_pModelHistory) |
| 134 | + m_pModelHistory->refresh(); |
| 135 | + }); |
| 136 | + |
| 137 | + QAction *clearAllAction = menu.addAction( |
| 138 | + QIcon::fromTheme("edit-clear"), tr("Clear all urls")); |
| 139 | + connect(clearAllAction, &QAction::triggered, this, [&]() { |
| 140 | + if(m_pModelHistory->removeRows(0, m_pModelHistory->rowCount())); |
| 141 | + }); |
| 142 | + |
| 143 | + // 设置菜单 |
| 144 | + // QAction *settingsAction = menu.addAction( |
| 145 | + // QIcon::fromTheme("system-settings"), tr("Settings")); |
| 146 | + // connect(settingsAction, &QAction::triggered, this, &CFrmHistory::showHistorySettings); |
| 147 | + |
| 148 | + // 4. 显示菜单 |
| 149 | + menu.exec(ui->tableView->viewport()->mapToGlobal(pos)); |
| 150 | +} |
| 151 | + |
| 152 | +QString CFrmHistory::extractDomain(const QString &url) |
| 153 | +{ |
| 154 | + QUrl qurl(url); |
| 155 | + if (qurl.isValid()) { |
| 156 | + QString host = qurl.host(); |
| 157 | + // 移除 www. 前缀 |
| 158 | + if (host.startsWith("www.")) { |
| 159 | + host = host.mid(4); |
| 160 | + } |
| 161 | + return host; |
| 162 | + } |
| 163 | + return QString(); |
| 164 | +} |
| 165 | + |
| 166 | +void CFrmHistory::onDeleteHistoryItem(const QModelIndex &index) |
| 167 | +{ |
| 168 | + if (!index.isValid() || !m_pModelHistory) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + QString title = ui->tableView->model()->data( |
| 173 | + index.siblingAtColumn(CHistoryModel::ColumnTitle)).toString(); |
| 174 | + if(title.isEmpty()) { |
| 175 | + title = ui->tableView->model()->data( |
| 176 | + index.siblingAtColumn(CHistoryModel::ColumnUrl)).toString(); |
| 177 | + } |
| 178 | + // 确认对话框 |
| 179 | + QMessageBox::StandardButton reply = QMessageBox::question( |
| 180 | + this, |
| 181 | + tr("Delete the url"), |
| 182 | + tr("Are you sure you want to delete the url \"%1\"?").arg(title), |
| 183 | + QMessageBox::Yes | QMessageBox::No, |
| 184 | + QMessageBox::No |
| 185 | + ); |
| 186 | + |
| 187 | + if (reply == QMessageBox::Yes) { |
| 188 | + // 从模型中删除 |
| 189 | + if (m_pModelHistory->removeRow(index.row())) { |
| 190 | + qDebug(log) << "History item deleted"; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +void CFrmHistory::onDeleteHistoryByDomain(const QString &domain) |
| 196 | +{ |
| 197 | + if (domain.isEmpty() || !m_pModelHistory) { |
| 198 | + return; |
| 199 | + } |
| 200 | + |
| 201 | + // 确认对话框 |
| 202 | + QMessageBox::StandardButton reply = QMessageBox::question( |
| 203 | + this, |
| 204 | + tr("Delete the url"), |
| 205 | + tr("Are you sure you want to delete all url from \"%1\"?").arg(domain), |
| 206 | + QMessageBox::Yes | QMessageBox::No, |
| 207 | + QMessageBox::No |
| 208 | + ); |
| 209 | + |
| 210 | + if (reply == QMessageBox::Yes) { |
| 211 | + m_pModelHistory->removeDomainItems(domain); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void CFrmHistory::onShowHistoryProperties(const QModelIndex &index) |
| 216 | +{ |
| 217 | + if (!index.isValid() || !m_pModelHistory) { |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + // 获取详细数据 |
| 222 | + QString url = ui->tableView->model()->data( |
| 223 | + index.siblingAtColumn(CHistoryModel::ColumnUrl)).toString(); |
| 224 | + QString title = ui->tableView->model()->data( |
| 225 | + index.siblingAtColumn(CHistoryModel::ColumnTitle)).toString(); |
| 226 | + QString visitTime = ui->tableView->model()->data( |
| 227 | + index.siblingAtColumn(CHistoryModel::ColumnVisitTime)).toString(); |
| 228 | + QString visitCount = ui->tableView->model()->data( |
| 229 | + index.siblingAtColumn(CHistoryModel::ColumnVisitCount)).toString(); |
| 230 | + QString lastVisitTime = ui->tableView->model()->data( |
| 231 | + index.siblingAtColumn(CHistoryModel::ColumnLastVisitTime)).toString(); |
| 232 | + |
| 233 | + // 创建详细信息对话框 |
| 234 | + QString details = QString( |
| 235 | + "<h3>%1</h3>" |
| 236 | + "<table border='0' cellspacing='5'>" |
| 237 | + "<tr><td><b>%2</b></td><td>%3</td></tr>" |
| 238 | + "<tr><td><b>%4</b></td><td>%5</td></tr>" |
| 239 | + "<tr><td><b>%6</b></td><td>%7</td></tr>" |
| 240 | + "<tr><td><b>%8</b></td><td>%9</td></tr>" |
| 241 | + "</table>") |
| 242 | + .arg(title.toHtmlEscaped()) |
| 243 | + .arg(tr("Url:")) |
| 244 | + .arg(url.toHtmlEscaped()) |
| 245 | + .arg(tr("Visit Count:")) |
| 246 | + .arg(visitCount) |
| 247 | + .arg(tr("Visit Time:")) |
| 248 | + .arg(visitTime) |
| 249 | + .arg(tr("Last Visit Time:")) |
| 250 | + .arg(lastVisitTime); |
| 251 | + |
| 252 | + QMessageBox::information(this, tr("Properties"), details); |
| 253 | +} |
0 commit comments