helper.cpp 2.8 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofer.com>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5
6
7
*/

#include "helper.h"
8
#include "calendarview_debug.h"
Laurent Montel's avatar
Laurent Montel committed
9
#include "prefs.h"
10
11
12
13

#include <Collection>
#include <Item>

14
#include <AkonadiCore/CollectionColorAttribute>
15
#include <AkonadiCore/CollectionModifyJob>
16

17
18
19
#include <QIcon>
#include <QPixmap>

20
bool EventViews::isColorDark(const QColor &c)
21
22
{
    double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
23
24
25
26
27
28
    return (luminance < 128.0) ? true : false;
}

QColor EventViews::getTextColor(const QColor &c)
{
    return (!isColorDark(c)) ? QColor(0, 0, 0) : QColor(255, 255, 255);
29
30
}

Laurent Montel's avatar
Laurent Montel committed
31
void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences)
32
{
33
    if (!coll.isValid() || !color.isValid()) {
34
35
36
37
        return;
    }

    const QString id = QString::number(coll.id());
38
39
40

    // Save the color in akonadi (so the resource can even save it server-side)
    Akonadi::Collection collection = coll;
Laurent Montel's avatar
Laurent Montel committed
41
    auto colorAttr = collection.attribute<Akonadi::CollectionColorAttribute>(Akonadi::Collection::AddIfMissing);
42
43
44
45
46
47
48
49
50
    if (colorAttr) {
        colorAttr->setColor(color);
        auto job = new Akonadi::CollectionModifyJob(collection, nullptr);
        QObject::connect(job, &Akonadi::CollectionModifyJob::result, [=]() {
            if (job->error()) {
                qCWarning(CALENDARVIEW_LOG) << "Failed to set CollectionColorAttribute:" << job->errorString();
            }
        });
    }
51
52

    // Also save the color in eventviewsrc (mostly historical)
53
54
55
    preferences->setResourceColor(id, color);
}

56
57
58
QColor EventViews::resourceColor(const Akonadi::Collection &coll, const PrefsPtr &preferences)
{
    if (!coll.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
59
        return {};
60
61
    }
    const QString id = QString::number(coll.id());
62
    // Color stored in eventviewsrc (and in memory)
63
    QColor color = preferences->resourceColorKnown(id);
64
65
66
67
68
    if (color.isValid()) {
        return color;
    }
    // Color stored in akonadi
    if (coll.hasAttribute<Akonadi::CollectionColorAttribute>()) {
Laurent Montel's avatar
Laurent Montel committed
69
        const auto colorAttr = coll.attribute<Akonadi::CollectionColorAttribute>();
70
        if (colorAttr && colorAttr->color().isValid()) {
71
            return colorAttr->color();
72
73
        }
    }
74
75
    // Generate new color and store it in eventsviewsrc (and in memory)
    return preferences->resourceColor(id);
76
77
78
79
80
}

QColor EventViews::resourceColor(const Akonadi::Item &item, const PrefsPtr &preferences)
{
    if (!item.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
81
        return {};
82
    }
83
    return resourceColor(item.parentCollection(), preferences);
84
85
}

Laurent Montel's avatar
Laurent Montel committed
86
int EventViews::yearDiff(QDate start, QDate end)
87
88
89
90
91
92
93
94
{
    return end.year() - start.year();
}

QPixmap EventViews::cachedSmallIcon(const QString &name)
{
    return QIcon::fromTheme(name).pixmap(16, 16);
}