/* * Copyright (C) 2013 Canonical, Ltd. * * Authors: * MichaƂ Sawicz * Michal Hruby * * 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; version 3. * * 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 . */ // self #include "categories.h" #include "categoryresults.h" // TODO: use something from libunity once it's public enum CategoryColumn { ID, DISPLAY_NAME, ICON_HINT, RENDERER_NAME, HINTS }; Categories::Categories(QObject* parent) : DeeListModel(parent) { m_roles[Categories::RoleId] = "id"; m_roles[Categories::RoleName] = "name"; m_roles[Categories::RoleIcon] = "icon"; m_roles[Categories::RoleRenderer] = "renderer"; m_roles[Categories::RoleContentType] = "contentType"; m_roles[Categories::RoleHints] = "hints"; m_roles[Categories::RoleResults] = "results"; m_roles[Categories::RoleCount] = "count"; // TODO This should not be needed but accumulatting the count changes // makes the visualization more stable and also makes crashes on fast // change of the search term harder to reproduce m_timer.setSingleShot(true); m_timer.setInterval(50); connect(&m_timer, &QTimer::timeout, this, &Categories::onEmitCountChanged); } DeeListModel* Categories::getResults(int index) const { if (!m_results.contains(index)) { CategoryResults* results = new CategoryResults(const_cast(this)); results->setCategoryIndex(index); connect(results, &DeeListModel::countChanged, this, &Categories::onCountChanged); unsigned categoryIndex = static_cast(index); auto unity_results = m_unityScope->GetResultsForCategory(categoryIndex); results->setModel(unity_results->model()); m_results.insert(index, results); } return m_results[index]; } void Categories::onCategoriesModelChanged(unity::glib::Object model) { m_updatedCategories.clear(); // FIXME: this might destroy the renderer view and re-create it, optimize? Q_FOREACH(DeeListModel* model, m_results) { delete model; } m_results.clear(); setModel(model); } void Categories::setUnityScope(const unity::dash::Scope::Ptr& scope) { m_unityScope = scope; // no need to call this, we'll get notified //setModel(m_unityScope->categories()->model()); m_categoriesChangedConnection.disconnect(); m_categoriesChangedConnection = m_unityScope->categories()->model.changed.connect(sigc::mem_fun(this, &Categories::onCategoriesModelChanged)); } void Categories::onCountChanged() { CategoryResults* results = qobject_cast(sender()); if (results) { m_updatedCategories << results->categoryIndex(); m_timer.start(); } } void Categories::onEmitCountChanged() { QVector roles; roles.append(Categories::RoleCount); Q_FOREACH(int categoryIndex, m_updatedCategories) { if (!m_results.contains(categoryIndex)) continue; QModelIndex changedIndex = index(categoryIndex); Q_EMIT dataChanged(changedIndex, changedIndex, roles); } m_updatedCategories.clear(); } QHash Categories::roleNames() const { return m_roles; } QVariant Categories::data(const QModelIndex& index, int role) const { if (!index.isValid()) { return QVariant(); } switch (role) { case RoleId: return QVariant::fromValue(index.row()); case RoleName: return DeeListModel::data(index, CategoryColumn::DISPLAY_NAME); case RoleIcon: return DeeListModel::data(index, CategoryColumn::ICON_HINT); case RoleRenderer: return DeeListModel::data(index, CategoryColumn::RENDERER_NAME); case RoleContentType: { auto hints = DeeListModel::data(index, CategoryColumn::HINTS).toHash(); return hints.contains("content-type") ? hints["content-type"] : QVariant(QString("default")); } case RoleHints: return DeeListModel::data(index, CategoryColumn::HINTS); case RoleResults: return QVariant::fromValue(getResults(index.row())); case RoleCount: return QVariant::fromValue(getResults(index.row())->rowCount()); default: return QVariant(); } }