1
/***************************************************************************
2
* Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License or (at your option) version 3 or any later version *
8
* accepted by the membership of KDE e.V. (or its successor approved *
9
* by the membership of KDE e.V.), which shall act as a proxy *
10
* defined in Section 14 of version 3 of the license. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19
***************************************************************************/
22
#include "CategoryModel.h"
27
#include <KCategorizedSortFilterProxyModel>
29
CategoryModel::CategoryModel(QObject* parent)
30
: QStandardItemModel(parent)
32
QHash< int, QByteArray > names = roleNames();
33
names[CategoryTypeRole] = "categoryType";
34
names[AndOrFilterRole] = "andOrFilter";
35
names[NotFilterRole] = "notFilter";
36
names[CategoryRole] = "category";
40
void CategoryModel::setCategories(const QList<Category *> &categoryList,
41
const QString &rootName)
43
invisibleRootItem()->removeRows(0, invisibleRootItem()->rowCount());
44
qDeleteAll(m_categoryList);
45
m_categoryList = categoryList;
46
foreach (Category *category, m_categoryList) {
47
if(!category->parent())
48
category->setParent(this);
49
QStandardItem *categoryItem = new QStandardItem;
50
categoryItem->setText(category->name());
51
categoryItem->setIcon(KIcon(category->icon()));
52
categoryItem->setEditable(false);
53
categoryItem->setData(rootName, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
54
categoryItem->setData(qVariantFromValue<QObject*>(category), CategoryRole);
56
if (category->hasSubCategories()) {
57
categoryItem->setData(SubCatType, CategoryTypeRole);
59
categoryItem->setData(CategoryType, CategoryTypeRole);
62
appendRow(categoryItem);
66
Category* CategoryModel::categoryForIndex(int row)
68
return m_categoryList.at(row);
71
void CategoryModel::populateCategories(const QString& rootName)
73
setCategories(Category::populateCategories(), rootName);
76
void CategoryModel::setSubcategories(Category* c)
78
m_currentCategory = c;
80
setCategories(c->subCategories(), c->name());
82
populateCategories(QString());
85
Category* CategoryModel::displayedCategory() const
87
return m_currentCategory;