helper.cpp 3.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
  Copyright (C) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

  As a special exception, permission is given to link this program
  with any edition of Qt, and distribute the resulting executable,
  without including the source code for Qt in the source distribution.
*/

#include "helper.h"
#include "prefs.h"

#include <Collection>
#include <Item>

29
30
#include <AkonadiCore/CollectionColorAttribute>

31
32
33
#include <QIcon>
#include <QPixmap>

34
bool EventViews::isColorDark(const QColor &c)
35
36
{
    double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
37
38
39
40
41
42
    return (luminance < 128.0) ? true : false;
}

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

Laurent Montel's avatar
Laurent Montel committed
45
void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences)
46
47
48
49
50
51
52
{
    if (!coll.isValid()) {
        return;
    }

    const QString id = QString::number(coll.id());
    if (coll.hasAttribute<Akonadi::CollectionColorAttribute>()) {
53
        const Akonadi::CollectionColorAttribute *colorAttr
Laurent Montel's avatar
Laurent Montel committed
54
            = coll.attribute<Akonadi::CollectionColorAttribute>();
55
56
57
58
59
60
61
62
63
        if (colorAttr && colorAttr->color().isValid() && (colorAttr->color() == color)) {
            // It's the same color: we save an invalid color
            preferences->setResourceColor(id, QColor());
        }
    }
    // in all other cases, we save the resourceColor
    preferences->setResourceColor(id, color);
}

64
65
66
67
68
69
QColor EventViews::resourceColor(const Akonadi::Collection &coll, const PrefsPtr &preferences)
{
    if (!coll.isValid()) {
        return QColor();
    }
    const QString id = QString::number(coll.id());
70
    // Color stored in eventviewsrc (and in memory)
71
    QColor color = preferences->resourceColorKnown(id);
72
73
74
75
76
    if (color.isValid()) {
        return color;
    }
    // Color stored in akonadi
    if (coll.hasAttribute<Akonadi::CollectionColorAttribute>()) {
77
        const Akonadi::CollectionColorAttribute *colorAttr
Laurent Montel's avatar
Laurent Montel committed
78
            = coll.attribute<Akonadi::CollectionColorAttribute>();
79
        if (colorAttr && colorAttr->color().isValid()) {
80
            return colorAttr->color();
81
82
        }
    }
83
84
    // Generate new color and store it in eventsviewsrc (and in memory)
    return preferences->resourceColor(id);
85
86
87
88
89
90
91
}

QColor EventViews::resourceColor(const Akonadi::Item &item, const PrefsPtr &preferences)
{
    if (!item.isValid()) {
        return QColor();
    }
92
    return resourceColor(item.parentCollection(), preferences);
93
94
95
96
97
98
99
100
101
102
103
}

int EventViews::yearDiff(const QDate &start, const QDate &end)
{
    return end.year() - start.year();
}

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