/* * * Copyright (C) 2021, 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 . * * Authors: jixiaoxu * */ #include "best-list-model.h" #include "search-plugin-manager.h" using namespace UkuiSearch; BestListModel::BestListModel(QObject *parent) : QAbstractItemModel(parent) { initConnections(); } QModelIndex BestListModel::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent) if(row < 0 || row > m_items.length() - 1) return QModelIndex(); return createIndex(row, column, nullptr); } QModelIndex BestListModel::parent(const QModelIndex &index) const { Q_UNUSED(index) return QModelIndex(); } int BestListModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : (m_isExpanded ? m_items.length() : NUM_LIMIT_SHOWN_DEFAULT); } int BestListModel::columnCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : 1; } QVariant BestListModel::data(const QModelIndex &index, int role) const { switch(role) { case Qt::DecorationRole: { return m_items.at(index.row()).icon; } case Qt::DisplayRole: { return m_items.at(index.row()).name; } case Qt::ToolTipRole: { return m_items.at(index.row()).toolTip; } default: return QVariant(); } return QVariant(); } const SearchPluginIface::ResultInfo &BestListModel::getInfo(const QModelIndex &index) { return m_items.at(index.row()); } const QString &BestListModel::getPluginInfo(const QModelIndex &index) { return m_plugin_id_list.at(index.row()); } void BestListModel::setExpanded(const bool &is_expanded) { this->beginResetModel(); m_isExpanded = is_expanded; this->endResetModel(); Q_EMIT this->itemListChanged(m_items.length()); } const bool &BestListModel::isExpanded() { return m_isExpanded; } QStringList BestListModel::getActions(const QModelIndex &index) { // if (m_item->m_result_info_list.length() > index.row() && index.row() >= 0) // return m_item->m_result_info_list.at(index.row()).actionList; Q_UNUSED(index) return QStringList(); } QString BestListModel::getKey(const QModelIndex &index) { if (m_items.length() > index.row() && index.row() >= 0) return m_items.at(index.row()).actionKey; return NULL; } void BestListModel::appendInfo(const QString &pluginId, const SearchPluginIface::ResultInfo &info) { if (m_plugin_action_key_list.contains(info.actionKey)) { // qDebug() << "plugin ID:" << pluginId << "name:" << info.name << "action key:" << info.actionKey << "is same with pre-result!"; return; } else { m_plugin_action_key_list.append(info.actionKey); } if (m_plugin_id_list.contains(pluginId)) { if (info.name == m_items.at(m_plugin_id_list.lastIndexOf(pluginId)).name) { return; } // qDebug()<<"plugin ID:"<endResetModel(); Q_EMIT this->itemListChanged(m_items.length()); } void BestListModel::removeInfo(const QString &pluginId) { this->beginResetModel(); int index = m_plugin_id_list.lastIndexOf(pluginId); if (index == -1) { return; } m_items.removeAt(index); m_plugin_id_list.removeAll(pluginId); this->endResetModel(); Q_EMIT this->itemListChanged(m_items.length()); } void BestListModel::moveInfo(const QString &pluginName, const int pos) { this->beginResetModel(); int index = m_plugin_id_list.lastIndexOf(pluginName); if (index == -1) { return; } m_plugin_id_list.removeAll(pluginName); if (pos > m_plugin_id_list.size()) { m_plugin_id_list.append(pluginName); } else { m_plugin_id_list.insert(pos - 1, pluginName); } SearchPluginIface::ResultInfo info = m_items.at(index); m_items.removeAt(index); if (pos > m_items.size()) { m_items.append(info); } else { m_items.insert(pos - 1, info); } this->endResetModel(); } void BestListModel::startSearch(const QString &keyword) { Q_UNUSED(keyword) if (!m_items.isEmpty()) { this->beginResetModel(); m_plugin_id_list.clear(); m_plugin_action_key_list.clear(); m_items.clear(); this->endResetModel(); Q_EMIT this->itemListChanged(m_items.length()); } } void BestListModel::initConnections() { } BestListModel::~BestListModel() { m_items.clear(); m_items.squeeze(); }