| 255 | } |
| 256 | |
| 257 | void CAddressCompleter::performSearch() |
| 258 | { |
| 259 | qDebug(log) << Q_FUNC_INFO; |
| 260 | QString keyword = m_pLineEdit->text().trimmed(); |
| 261 | if (keyword.isEmpty() || !m_pListWidget) { |
| 262 | hideCompleter(); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // 清空现有项 |
| 267 | m_pListWidget->clear(); |
| 268 | m_currentSelectedIndex = -1; |
| 269 | |
| 270 | // 增加 “@” 命令 |
| 271 | if(keyword.startsWith('@')) { |
| 272 | QList<Command> lstCommonds; |
| 273 | lstCommonds << Command{tr("Search"), "@search:", QIcon::fromTheme("system-search")}; |
| 274 | lstCommonds << Command{tr("Setting"), "@setting", QIcon::fromTheme("system-settings")}; |
| 275 | lstCommonds << Command{tr("History"), "@history", QIcon()}; |
| 276 | lstCommonds << Command{tr("Bookmarks"), "@bookmarks", QIcon::fromTheme("user-bookmarks")}; |
| 277 | |
| 278 | // 根据 keyword 进行排序 |
| 279 | std::sort(lstCommonds.begin(), lstCommonds.end(), [keyword](Command a, Command b){ |
| 280 | // 1. 完全匹配的排在最前面 |
| 281 | if (a.cmd.startsWith(keyword) && !b.cmd.startsWith(keyword)) |
| 282 | return true; |
| 283 | if (!a.cmd.startsWith(keyword) && b.cmd.startsWith(keyword)) |
| 284 | return false; |
| 285 | |
| 286 | // 2. 包含关键字的排在前面 |
| 287 | bool aContains = a.cmd.contains(keyword, Qt::CaseInsensitive); |
| 288 | bool bContains = b.cmd.contains(keyword, Qt::CaseInsensitive); |
| 289 | if (aContains && !bContains) return true; |
| 290 | if (!aContains && bContains) return false; |
| 291 | |
| 292 | // 3. 否则按字母顺序排序 |
| 293 | return a.cmd < b.cmd; |
| 294 | }); |
| 295 | foreach(auto cmd, lstCommonds) { |
| 296 | QListWidgetItem *item = new QListWidgetItem(m_pListWidget); |
| 297 | item->setSizeHint(QSize(0, 40)); |
| 298 | item->setData(Qt::UserRole, cmd.cmd); |
| 299 | CAddressCompleterItem *pCompleterItem = new CAddressCompleterItem( |
| 300 | cmd.title, cmd.cmd, cmd.icon); |
| 301 | if(pCompleterItem) |
| 302 | m_pListWidget->setItemWidget(item, pCompleterItem); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // 搜索历史记录 |
| 307 | QList<HistoryItem> lstHistory; |
| 308 | if(m_pDatabase) |
| 309 | lstHistory = m_pDatabase->searchHistory(keyword); |
| 310 | |
| 311 | // 添加搜索结果 |
| 312 | int count = 0; |
| 313 | QStringList addedUrls; // 用于去重 |
| 314 |
nothing calls this directly
no test coverage detected