2021-08-02 13:46:59 +08:00
|
|
|
/*
|
|
|
|
*
|
2021-09-08 11:03:43 +08:00
|
|
|
* Copyright (C) 2021, KylinSoft Co., Ltd.
|
2021-08-02 13:46:59 +08:00
|
|
|
*
|
|
|
|
* 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: jixiaoxu <jixiaoxu@kylinos.cn>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "best-list-model.h"
|
2021-08-03 16:08:30 +08:00
|
|
|
#include "search-plugin-manager.h"
|
2021-12-14 14:43:35 +08:00
|
|
|
using namespace UkuiSearch;
|
2021-08-02 13:46:59 +08:00
|
|
|
|
|
|
|
BestListModel::BestListModel(QObject *parent)
|
|
|
|
: QAbstractItemModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BestListModel::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-08-02 13:46:59 +08:00
|
|
|
return QModelIndex();
|
2023-09-01 11:07:57 +08:00
|
|
|
return createIndex(row, column, nullptr);
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BestListModel::parent(const QModelIndex &index) const
|
|
|
|
{
|
2023-04-24 14:07:56 +08:00
|
|
|
Q_UNUSED(index)
|
2021-08-02 13:46:59 +08:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BestListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2023-09-01 11:07:57 +08:00
|
|
|
return parent.isValid() ? 0 : (m_isExpanded ? m_items.length() : NUM_LIMIT_SHOWN_DEFAULT);
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int BestListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return parent.isValid() ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant BestListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
switch(role) {
|
2024-03-04 17:55:42 +08:00
|
|
|
case Qt::DecorationRole: {
|
|
|
|
return m_items.at(index.row())->info().icon;
|
|
|
|
}
|
|
|
|
case Qt::DisplayRole: {
|
|
|
|
return m_items.at(index.row())->info().name;
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
return m_items.at(index.row())->info().toolTip;
|
|
|
|
}
|
|
|
|
case AdditionalRoles::ShowToolTip: {
|
|
|
|
return m_items.at(index.row())->showToolTip();
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return {};
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SearchPluginIface::ResultInfo &BestListModel::getInfo(const QModelIndex &index)
|
|
|
|
{
|
2024-03-04 17:55:42 +08:00
|
|
|
return m_items.at(index.row())->info();
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &BestListModel::getPluginInfo(const QModelIndex &index)
|
|
|
|
{
|
2024-03-04 17:55:42 +08:00
|
|
|
return m_pluginIdList.at(index.row());
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BestListModel::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-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool &BestListModel::isExpanded()
|
|
|
|
{
|
|
|
|
return m_isExpanded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BestListModel::appendInfo(const QString &pluginId, const SearchPluginIface::ResultInfo &info)
|
|
|
|
{
|
2024-03-05 16:15:42 +08:00
|
|
|
|
|
|
|
if (m_resourceTypes.contains(info.resourceType)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
m_resourceTypes.append(info.resourceType);
|
|
|
|
}
|
2024-03-04 17:55:42 +08:00
|
|
|
if (m_pluginIdList.contains(pluginId)) {
|
|
|
|
if (info.actionKey == m_items.at(m_pluginIdList.lastIndexOf(pluginId))->info().actionKey) {
|
2021-08-02 13:46:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-08-03 16:08:30 +08:00
|
|
|
this->beginResetModel();
|
2024-03-04 17:55:42 +08:00
|
|
|
delete m_items.takeAt(m_pluginIdList.lastIndexOf(pluginId));
|
|
|
|
m_items.insert(m_pluginIdList.lastIndexOf(pluginId), new OneResult(info));
|
2021-08-03 16:08:30 +08:00
|
|
|
this->endResetModel();
|
2021-08-02 13:46:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->beginResetModel();
|
2021-10-29 16:03:24 +08:00
|
|
|
// qDebug()<<"plugin ID:"<<pluginId<<"Got a result. name ="<<info.name;
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.append(pluginId);
|
|
|
|
m_items.append(new OneResult(info));
|
|
|
|
QVector<OneResult *> result_info_list_tmp;
|
2021-08-03 16:08:30 +08:00
|
|
|
QVector<QString> plugin_id_list_tmp;
|
2022-11-22 10:17:36 +08:00
|
|
|
|
|
|
|
QList<PluginInfo> infoList = SearchPluginManager::getInstance()->getPluginIds();
|
|
|
|
QVector<QString> orders(infoList.length());
|
|
|
|
for (const PluginInfo& info : infoList) {
|
|
|
|
if (info.isEnable() and info.order() > 0) {
|
|
|
|
if (info.order() > orders.size()) {
|
|
|
|
QVector<QString> tmpVct(info.order() - orders.size());
|
|
|
|
orders.append(tmpVct);
|
|
|
|
}
|
|
|
|
orders[info.order() - 1] = info.name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orders.removeAll("");
|
|
|
|
|
|
|
|
Q_FOREACH (const QString& plugin, orders) {
|
2024-03-04 17:55:42 +08:00
|
|
|
if (m_pluginIdList.contains(plugin)) {
|
|
|
|
result_info_list_tmp.append(m_items.at(m_pluginIdList.lastIndexOf(plugin)));
|
2021-08-03 16:08:30 +08:00
|
|
|
plugin_id_list_tmp.append(plugin);
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 11:07:57 +08:00
|
|
|
m_items.clear();
|
|
|
|
m_items.swap(result_info_list_tmp);
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.clear();
|
|
|
|
m_pluginIdList.swap(plugin_id_list_tmp);
|
2021-08-02 13:46:59 +08:00
|
|
|
this->endResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2021-08-02 13:46:59 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:17:36 +08:00
|
|
|
void BestListModel::removeInfo(const QString &pluginId)
|
|
|
|
{
|
|
|
|
this->beginResetModel();
|
2024-03-04 17:55:42 +08:00
|
|
|
int index = m_pluginIdList.lastIndexOf(pluginId);
|
2022-11-22 10:17:36 +08:00
|
|
|
if (index == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-01 11:07:57 +08:00
|
|
|
m_items.removeAt(index);
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.removeAll(pluginId);
|
2022-11-22 10:17:36 +08:00
|
|
|
this->endResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2022-11-22 10:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BestListModel::moveInfo(const QString &pluginName, const int pos)
|
|
|
|
{
|
|
|
|
this->beginResetModel();
|
2024-03-04 17:55:42 +08:00
|
|
|
int index = m_pluginIdList.lastIndexOf(pluginName);
|
2022-11-22 10:17:36 +08:00
|
|
|
if (index == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.removeAll(pluginName);
|
|
|
|
if (pos > m_pluginIdList.size()) {
|
|
|
|
m_pluginIdList.append(pluginName);
|
2022-11-22 10:17:36 +08:00
|
|
|
} else {
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.insert(pos - 1, pluginName);
|
2022-11-22 10:17:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 17:55:42 +08:00
|
|
|
OneResult *info = m_items.at(index);
|
2023-09-01 11:07:57 +08:00
|
|
|
m_items.removeAt(index);
|
|
|
|
if (pos > m_items.size()) {
|
|
|
|
m_items.append(info);
|
2022-11-22 10:17:36 +08:00
|
|
|
} else {
|
2023-09-01 11:07:57 +08:00
|
|
|
m_items.insert(pos - 1, info);
|
2022-11-22 10:17:36 +08:00
|
|
|
}
|
|
|
|
this->endResetModel();
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:46:59 +08:00
|
|
|
void BestListModel::startSearch(const QString &keyword)
|
|
|
|
{
|
2023-04-24 14:07:56 +08:00
|
|
|
Q_UNUSED(keyword)
|
2023-09-01 11:07:57 +08:00
|
|
|
if (!m_items.isEmpty()) {
|
2021-08-02 13:46:59 +08:00
|
|
|
this->beginResetModel();
|
2024-03-04 17:55:42 +08:00
|
|
|
m_pluginIdList.clear();
|
2024-03-05 16:15:42 +08:00
|
|
|
m_resourceTypes.clear();
|
2024-03-04 17:55:42 +08:00
|
|
|
qDeleteAll(m_items);
|
2023-09-01 11:07:57 +08:00
|
|
|
m_items.clear();
|
2021-08-02 13:46:59 +08:00
|
|
|
this->endResetModel();
|
2023-09-01 11:07:57 +08:00
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
2021-08-02 13:46:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:07:57 +08:00
|
|
|
BestListModel::~BestListModel()
|
|
|
|
{
|
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();
|
|
|
|
}
|
2024-03-04 17:55:42 +08:00
|
|
|
|
|
|
|
bool BestListModel::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;
|
|
|
|
}
|
2024-03-06 14:35:28 +08:00
|
|
|
|
|
|
|
void BestListModel::stopSearchSlot() {
|
|
|
|
beginResetModel();
|
|
|
|
qDeleteAll(m_items);
|
|
|
|
m_pluginIdList.clear();
|
|
|
|
m_resourceTypes.clear();
|
|
|
|
m_items.clear();
|
|
|
|
m_items.squeeze();
|
|
|
|
endResetModel();
|
|
|
|
Q_EMIT this->itemListChanged(m_items.length());
|
|
|
|
// Q_EMIT stopSearch();
|
|
|
|
}
|