~marcustomlinson/unity-scopes-shell/lp-1552082

« back to all changes in this revision

Viewing changes to src/Unity/categories.cpp

  • Committer: Pawel Stolowski
  • Date: 2015-11-30 09:23:32 UTC
  • mfrom: (278 unity-scopes-shell)
  • mto: This revision was merged to the branch mainline in revision 279.
  • Revision ID: pawel.stolowski@canonical.com-20151130092332-io500es46ww36vfc
Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
class CategoryData
47
47
{
48
48
public:
49
 
    CategoryData(scopes::Category::SCPtr const& category): m_isSpecial(false)
 
49
    CategoryData(scopes::Category::SCPtr const& category)
50
50
    {
51
51
        setCategory(category);
52
52
    }
53
53
 
54
54
    CategoryData(CategoryData const& other) = delete;
55
55
 
56
 
    // constructor for special (shell-overriden) categories
57
 
    CategoryData(QString const& id, QString const& title, QString const& icon, QString const& rawTemplate, QObject* countObject):
58
 
        m_catId(id), m_catTitle(title), m_catIcon(icon), m_rawTemplate(rawTemplate.toStdString()), m_countObject(countObject), m_isSpecial(true)
59
 
    {
60
 
        parseTemplate(m_rawTemplate, &m_rendererTemplate, &m_components);
61
 
    }
62
 
 
63
56
    void setCategory(scopes::Category::SCPtr const& category)
64
57
    {
65
58
        m_category = category;
211
204
        return 0;
212
205
    }
213
206
 
214
 
    bool isSpecial() const
215
 
    {
216
 
        return m_isSpecial;
217
 
    }
218
 
 
219
207
    static bool parseTemplate(std::string const& raw_template, QJsonValue* renderer, QJsonValue* components)
220
208
    {
221
209
        // lazy init of the defaults
260
248
    QJsonValue m_components;
261
249
    QSharedPointer<ResultsModel> m_resultsModel;
262
250
    QPointer<QObject> m_countObject;
263
 
    bool m_isSpecial;
264
251
 
265
252
    static QJsonValue mergeOverrides(QJsonValue const& defaultVal, QJsonValue const& overrideVal)
266
253
    {
299
286
QJsonValue* CategoryData::DEFAULTS = nullptr;
300
287
 
301
288
Categories::Categories(QObject* parent)
302
 
    : unity::shell::scopes::CategoriesInterface(parent)
 
289
    : unity::shell::scopes::CategoriesInterface(parent),
 
290
    m_categoryIndex(0)
303
291
{
304
292
}
305
293
 
333
321
    return -1;
334
322
}
335
323
 
336
 
int Categories::getFirstEmptyCategoryIndex() const
337
 
{
338
 
    for (int i = 0; i < m_categories.size(); i++) {
339
 
        if (m_categories[i]->isSpecial()) {
340
 
            continue;
341
 
        }
342
 
        if (m_categories[i]->resultsModelCount() == 0) {
343
 
            return i;
344
 
        }
345
 
    }
346
 
 
347
 
    return m_categories.size();
348
 
}
349
 
 
350
324
void Categories::registerCategory(const scopes::Category::SCPtr& category, QSharedPointer<ResultsModel> resultsModel)
351
325
{
352
326
    // do we already have a category with this id?
 
327
    if (m_registeredCategories.find(category->id()) != m_registeredCategories.end()) {
 
328
        return;
 
329
    }
 
330
    m_registeredCategories.insert(category->id());
 
331
 
353
332
    int index = getCategoryIndex(QString::fromStdString(category->id()));
354
 
    int emptyIndex = getFirstEmptyCategoryIndex();
 
333
    int emptyIndex = m_categoryIndex++;
355
334
    if (index >= 0) {
356
335
        // re-registering an existing category will move it after the first non-empty category
357
336
        if (emptyIndex < index) {
374
353
            m_categories.insert(emptyIndex, catData);
375
354
            endInsertRows();
376
355
        } else {
 
356
            // the category has already been registered for current search,
 
357
            // check if any attributes of the category changed
377
358
            QSharedPointer<CategoryData> catData = m_categories[index];
378
 
            // check if any attributes of the category changed
379
359
            QVector<int> changedRoles(catData->updateAttributes(category));
380
360
 
381
361
            if (changedRoles.size() > 0) {
463
443
    dataChanged(changeStart, changeEnd, roles);
464
444
}
465
445
 
 
446
void Categories::markNewSearch()
 
447
{
 
448
    m_categoryIndex = 0;
 
449
    m_registeredCategories.clear();
 
450
    for (auto model: m_categoryResults) {
 
451
        model->markNewSearch();
 
452
    }
 
453
}
 
454
 
 
455
void Categories::purgeResults()
 
456
{
 
457
    QVector<int> roles;
 
458
    roles.append(RoleCount);
 
459
 
 
460
    for (auto it = m_categoryResults.begin(); it != m_categoryResults.end(); it++) {
 
461
        auto model = it.value();
 
462
        if (model->needsPurging()) {
 
463
            model->clearResults();
 
464
 
 
465
            QModelIndex idx(index(getCategoryIndex(QString::fromStdString(it.key()))));
 
466
            Q_EMIT dataChanged(idx, idx, roles);
 
467
        }
 
468
    }
 
469
}
466
470
 
467
471
bool Categories::parseTemplate(std::string const& raw_template, QJsonValue* renderer, QJsonValue* components)
468
472
{
494
498
    return false;
495
499
}
496
500
 
497
 
void Categories::addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject)
498
 
{
499
 
    int index = getCategoryIndex(categoryId);
500
 
    if (index >= 0) {
501
 
        qWarning("ERROR! Category with id \"%s\" already exists!", categoryId.toStdString().c_str());
502
 
    } else {
503
 
        QSharedPointer<CategoryData> catData(new CategoryData(categoryId, name, icon, rawTemplate, countObject));
504
 
        // prepend the category
505
 
        beginInsertRows(QModelIndex(), 0, 0);
506
 
        m_categories.prepend(catData);
507
 
        endInsertRows();
508
 
 
509
 
        if (countObject) {
510
 
            m_countObjects[countObject] = categoryId;
511
 
            QObject::connect(countObject, SIGNAL(countChanged()), this, SLOT(countChanged()));
512
 
        }
513
 
    }
514
 
}
515
 
 
516
501
void Categories::countChanged()
517
502
{
518
503
    QObject* countObject = sender();
530
515
    }
531
516
}
532
517
 
 
518
void Categories::updateResult(unity::scopes::Result const& result, QString const& categoryId, unity::scopes::Result const& updated_result)
 
519
{
 
520
    for (auto catData: m_categories) {
 
521
        if (catData->categoryId() == categoryId) {
 
522
            catData->resultsModel()->updateResult(result, updated_result);
 
523
            break;
 
524
        }
 
525
    }
 
526
}
 
527
 
533
528
QVariant
534
529
Categories::data(const QModelIndex& index, int role) const
535
530
{