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

« back to all changes in this revision

Viewing changes to libmuon/Category/CategoryModel.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2015-03-24 07:36:31 UTC
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: package-import@ubuntu.com-20150324073631-7nmay5episnfkdlt
Tags: upstream-5.2.2
ImportĀ upstreamĀ versionĀ 5.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
// KDE includes
27
27
#include <KCategorizedSortFilterProxyModel>
28
28
 
 
29
Q_GLOBAL_STATIC_WITH_ARGS(QList<Category*>, s_categories, (CategoriesReader().populateCategories()))
 
30
 
29
31
CategoryModel::CategoryModel(QObject* parent)
30
32
    : QStandardItemModel(parent)
31
33
{
40
42
 
41
43
void CategoryModel::setCategories(const QList<Category *> &categoryList, const QString &rootName)
42
44
{
 
45
    clear();
 
46
 
43
47
    invisibleRootItem()->removeRows(0, invisibleRootItem()->rowCount());
44
 
    qDeleteAll(m_categoryList);
45
 
    m_categoryList = categoryList;
46
 
    foreach (Category *category, m_categoryList) {
 
48
    foreach (Category *category, categoryList) {
47
49
        QStandardItem *categoryItem = new QStandardItem;
48
50
        categoryItem->setText(category->name());
49
51
        categoryItem->setIcon(QIcon::fromTheme(category->icon()));
50
52
        categoryItem->setEditable(false);
51
53
        categoryItem->setData(rootName, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
52
54
        categoryItem->setData(qVariantFromValue<QObject*>(category), CategoryRole);
 
55
        connect(category, &QObject::destroyed, this, &CategoryModel::categoryDeleted);
53
56
 
54
57
        appendRow(categoryItem);
55
58
    }
56
59
}
57
60
 
 
61
void CategoryModel::categoryDeleted(QObject* cat)
 
62
{
 
63
    for(int i=0; i<rowCount(); ++i) {
 
64
        if (cat == item(i)->data(CategoryRole).value<QObject*>()) {
 
65
            removeRow(i);
 
66
        }
 
67
    }
 
68
}
 
69
 
58
70
Category* CategoryModel::categoryForRow(int row)
59
71
{
60
 
    return m_categoryList.at(row);
61
 
}
62
 
 
63
 
QList<Category*> CategoryModel::populateCategories()
64
 
{
65
 
    static QList<Category*> cats;
66
 
    if(cats.isEmpty()) {
67
 
        CategoriesReader reader;
68
 
        cats = reader.populateCategories();
69
 
    }
70
 
    return cats;
 
72
    return qobject_cast<Category*>(item(row)->data(CategoryRole).value<QObject*>());
71
73
}
72
74
 
73
75
void CategoryModel::setDisplayedCategory(Category* c)
76
78
    if(c)
77
79
        setCategories(c->subCategories(), c->name());
78
80
    else
79
 
        setCategories(populateCategories(), QString());
 
81
        setCategories(*s_categories, QString());
80
82
}
81
83
 
82
84
Category* CategoryModel::displayedCategory() const
89
91
    if(root->name()==name)
90
92
        return root;
91
93
    else if(root->hasSubCategories()) {
92
 
        QList<Category*> subs = root->subCategories();
 
94
        const QList<Category*> subs = root->subCategories();
93
95
        for(Category* c : subs) {
94
96
            Category* ret = recFindCategory(c, name);
95
97
            if(ret)
101
103
 
102
104
Category* CategoryModel::findCategoryByName(const QString& name)
103
105
{
104
 
    QList<Category*> cats = populateCategories();
 
106
    const QList<Category*> cats = *s_categories;
105
107
    for(Category* cat : cats) {
106
108
        Category* ret = recFindCategory(cat, name);
107
109
        if(ret)
109
111
    }
110
112
    return 0;
111
113
}
 
114
 
 
115
void CategoryModel::blacklistPlugin(const QString& name)
 
116
{
 
117
    const QSet<QString> plugins = {name};
 
118
    for(QList<Category*>::iterator it = s_categories->begin(), itEnd = s_categories->end(); it!=itEnd; ) {
 
119
        if ((*it)->blacklistPlugins(plugins)) {
 
120
            delete *it;
 
121
            it = s_categories->erase(it);
 
122
        } else
 
123
            ++it;
 
124
    }
 
125
}