2024-03-04 17:55:42 +08:00
|
|
|
|
/*
|
2021-05-22 21:29:43 +08:00
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2020, KylinSoft Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* Authors: zhangjiaping <zhangjiaping@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-03-04 17:55:42 +08:00
|
|
|
|
#include <QMetaEnum>
|
2021-05-22 21:29:43 +08:00
|
|
|
|
#include "search-result-model.h"
|
2021-12-14 14:43:35 +08:00
|
|
|
|
using namespace UkuiSearch;
|
2021-05-22 21:29:43 +08:00
|
|
|
|
|
|
|
|
|
SearchResultModel::SearchResultModel(const QString &plugin_id)
|
|
|
|
|
{
|
|
|
|
|
m_plugin_id = plugin_id;
|
|
|
|
|
m_search_manager = new SearchResultManager(plugin_id);
|
2022-01-05 16:38:52 +08:00
|
|
|
|
m_timer = new QTimer(this);
|
|
|
|
|
m_timer->setInterval(500);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
initConnections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex SearchResultModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
|
{
|
2023-04-24 14:07:56 +08:00
|
|
|
|
Q_UNUSED(parent)
|
2023-09-01 11:07:57 +08:00
|
|
|
|
if(row < 0 || row > m_items.length() - 1)
|
2021-05-22 21:29:43 +08:00
|
|
|
|
return QModelIndex();
|
|
|
|
|
// QVector<SearchPluginIface::ResultInfo> * m_info = &m_result_info_list;
|
2023-09-01 11:07:57 +08:00
|
|
|
|
return createIndex(row, column, nullptr);
|
|
|
|
|
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex SearchResultModel::parent(const QModelIndex &child) const
|
|
|
|
|
{
|
2023-04-24 14:07:56 +08:00
|
|
|
|
Q_UNUSED(child)
|
2021-05-22 21:29:43 +08:00
|
|
|
|
return QModelIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SearchResultModel::rowCount(const QModelIndex &index) const
|
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
|
return index.isValid() ? 0 : (m_isExpanded ? m_items.length() : NUM_LIMIT_SHOWN_DEFAULT);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SearchResultModel::columnCount(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
return index.isValid() ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SearchResultModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
|
if (!index.isValid() || index.row() >= m_items.count()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
switch(role) {
|
|
|
|
|
case Qt::DecorationRole: {
|
2024-03-04 17:55:42 +08:00
|
|
|
|
return m_items.at(index.row())->info().icon;
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
case Qt::DisplayRole: {
|
2024-03-04 17:55:42 +08:00
|
|
|
|
return m_items.at(index.row())->info().name;
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
2022-05-24 17:42:03 +08:00
|
|
|
|
case Qt::ToolTipRole: {
|
2024-03-04 17:55:42 +08:00
|
|
|
|
return m_items.at(index.row())->info().toolTip;
|
|
|
|
|
}
|
|
|
|
|
case AdditionalRoles::ShowToolTip: {
|
|
|
|
|
return m_items.at(index.row())->showToolTip();
|
2022-05-24 17:42:03 +08:00
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
default:
|
2024-03-04 17:55:42 +08:00
|
|
|
|
return {};
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:38:52 +08:00
|
|
|
|
void SearchResultModel::appendInfo(const SearchPluginIface::ResultInfo &info)//TODO 代码逻辑可尝试梳理优化
|
2021-05-22 21:29:43 +08:00
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
|
if (m_items.length() > 5 //搜索结果大于5个并且搜索结果处于收起状态时只存储数据无需刷新UI
|
2022-01-05 16:38:52 +08:00
|
|
|
|
and !m_isExpanded) {
|
2024-03-04 17:55:42 +08:00
|
|
|
|
m_items.append(new OneResult(info));
|
2022-01-05 16:38:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-09-01 11:07:57 +08:00
|
|
|
|
if (m_items.length() > 50
|
2022-01-05 16:38:52 +08:00
|
|
|
|
and m_isExpanded) {//搜索结果大于50个并且搜索结果处于展开状态时只存储数据并启动定时,500ms刷新一次UI
|
2024-03-04 17:55:42 +08:00
|
|
|
|
m_items.append(new OneResult(info));
|
2022-01-05 16:38:52 +08:00
|
|
|
|
if (!m_timer->isActive()) {
|
|
|
|
|
m_timer->start();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-09-01 11:07:57 +08:00
|
|
|
|
// this->beginResetModel();
|
|
|
|
|
beginInsertRows(QModelIndex(), m_items.length(), m_items.length());
|
2024-03-04 17:55:42 +08:00
|
|
|
|
m_items.append(new OneResult(info));
|
2023-09-01 11:07:57 +08:00
|
|
|
|
// this->endResetModel();
|
|
|
|
|
endInsertRows();
|
|
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2021-10-27 11:37:55 +08:00
|
|
|
|
if (m_plugin_id != "Web Page") {
|
2024-03-04 17:55:42 +08:00
|
|
|
|
Q_EMIT this->sendBestListData(m_plugin_id, m_items.at(0)->info());
|
2021-10-27 11:37:55 +08:00
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 19:42:40 +08:00
|
|
|
|
void SearchResultModel::startSearch(const QString &keyword)
|
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
|
if (!m_items.isEmpty()) {
|
2021-05-25 19:42:40 +08:00
|
|
|
|
this->beginResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
|
m_items.clear();
|
2021-05-25 19:42:40 +08:00
|
|
|
|
this->endResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
|
|
|
|
m_search_manager->startSearch(keyword);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 21:29:43 +08:00
|
|
|
|
void SearchResultModel::initConnections()
|
|
|
|
|
{
|
|
|
|
|
connect(this, &SearchResultModel::stopSearch, m_search_manager, &SearchResultManager::stopSearch);
|
|
|
|
|
connect(m_search_manager, &SearchResultManager::gotResultInfo, this, &SearchResultModel::appendInfo);
|
2022-01-05 16:38:52 +08:00
|
|
|
|
connect(m_timer, &QTimer::timeout, [ = ] () {//500ms刷新一次UI,防止搜索结果数据量过大导致的UI卡顿
|
2023-09-01 11:07:57 +08:00
|
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2022-01-05 16:38:52 +08:00
|
|
|
|
m_timer->stop();
|
|
|
|
|
});
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SearchPluginIface::ResultInfo &SearchResultModel::getInfo(const QModelIndex &index)
|
|
|
|
|
{
|
2024-03-04 17:55:42 +08:00
|
|
|
|
return m_items.at(index.row())->info();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 10:25:15 +08:00
|
|
|
|
void SearchResultModel::setExpanded(const bool &is_expanded)
|
|
|
|
|
{
|
|
|
|
|
this->beginResetModel();
|
|
|
|
|
m_isExpanded = is_expanded;
|
|
|
|
|
this->endResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2021-05-27 10:25:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool &SearchResultModel::isExpanded()
|
|
|
|
|
{
|
|
|
|
|
return m_isExpanded;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:38:52 +08:00
|
|
|
|
void SearchResultModel::refresh()
|
|
|
|
|
{
|
|
|
|
|
this->beginResetModel();
|
|
|
|
|
this->endResetModel();
|
2021-05-27 10:25:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 11:07:57 +08:00
|
|
|
|
void SearchResultModel::stopSearchSlot()
|
2021-05-22 21:29:43 +08:00
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
|
beginResetModel();
|
2024-03-04 17:55:42 +08:00
|
|
|
|
qDeleteAll(m_items);
|
2023-09-01 11:07:57 +08:00
|
|
|
|
m_items.clear();
|
|
|
|
|
m_items.squeeze();
|
|
|
|
|
endResetModel();
|
|
|
|
|
Q_EMIT stopSearch();
|
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
|
2023-09-01 11:07:57 +08:00
|
|
|
|
SearchResultModel::~SearchResultModel()
|
|
|
|
|
{
|
2024-03-04 17:55:42 +08:00
|
|
|
|
qDeleteAll(m_items);
|
2023-09-01 11:07:57 +08:00
|
|
|
|
m_items.clear();
|
|
|
|
|
m_items.squeeze();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
2024-03-04 17:55:42 +08:00
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> SearchResultModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
|
|
|
|
|
QMetaEnum e = metaObject()->enumerator(metaObject()->indexOfEnumerator("AdditionalRoles"));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < e.keyCount(); ++i) {
|
|
|
|
|
roles.insert(e.value(i), e.key(i));
|
|
|
|
|
}
|
|
|
|
|
return roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SearchResultModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
{
|
|
|
|
|
if(!index.isValid() || index.row() >= m_items.length()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(role == ShowToolTip) {
|
|
|
|
|
m_items.at(index.row())->setShowToolTip(value.toBool());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|