~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to libmuon/Category/CategoryModel.cpp

Tags: upstream-1.3.65
Import upstream version 1.3.65

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>       *
 
3
 *                                                                         *
 
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.                    *
 
11
 *                                                                         *
 
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.                          *
 
16
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
// Own includes
 
22
#include "CategoryModel.h"
 
23
#include "Category.h"
 
24
 
 
25
// KDE includes
 
26
#include <KIcon>
 
27
#include <KCategorizedSortFilterProxyModel>
 
28
 
 
29
CategoryModel::CategoryModel(QObject* parent)
 
30
    : QStandardItemModel(parent)
 
31
{
 
32
    QHash< int, QByteArray > names = roleNames();
 
33
    names[CategoryTypeRole] = "categoryType";
 
34
    names[AndOrFilterRole] = "andOrFilter";
 
35
    names[NotFilterRole] = "notFilter";
 
36
    names[CategoryRole] = "category";
 
37
    setRoleNames(names);
 
38
}
 
39
 
 
40
void CategoryModel::setCategories(const QList<Category *> &categoryList,
 
41
                                  const QString &rootName)
 
42
{
 
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);
 
55
 
 
56
        if (category->hasSubCategories()) {
 
57
            categoryItem->setData(SubCatType, CategoryTypeRole);
 
58
        } else {
 
59
            categoryItem->setData(CategoryType, CategoryTypeRole);
 
60
        }
 
61
 
 
62
        appendRow(categoryItem);
 
63
    }
 
64
}
 
65
 
 
66
Category* CategoryModel::categoryForIndex(int row)
 
67
{
 
68
    return m_categoryList.at(row);
 
69
}
 
70
 
 
71
void CategoryModel::populateCategories(const QString& rootName)
 
72
{
 
73
    setCategories(Category::populateCategories(), rootName);
 
74
}
 
75
 
 
76
void CategoryModel::setSubcategories(Category* c)
 
77
{
 
78
    m_currentCategory = c;
 
79
    if(c)
 
80
        setCategories(c->subCategories(), c->name());
 
81
    else
 
82
        populateCategories(QString());
 
83
}
 
84
 
 
85
Category* CategoryModel::displayedCategory() const
 
86
{
 
87
    return m_currentCategory;
 
88
}