Eventviews

helper.cpp
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"
9#include "prefs.h"
10
11#include <Akonadi/Collection>
12#include <Akonadi/Item>
13
14#include <Akonadi/CollectionColorAttribute>
15#include <Akonadi/CollectionModifyJob>
16
17#include <QIcon>
18#include <QPixmap>
19
21{
22 double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114);
23 return (luminance < 128.0) ? true : false;
24}
25
27{
28 return (!isColorDark(c)) ? QColor(0, 0, 0) : QColor(255, 255, 255);
29}
30
31void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences)
32{
33 if (!coll.isValid() || !color.isValid()) {
34 return;
35 }
36
37 const QString id = QString::number(coll.id());
38
39 // Save the color in akonadi (so the resource can even save it server-side)
40 Akonadi::Collection collection = coll;
42 if (colorAttr) {
43 colorAttr->setColor(color);
44 auto job = new Akonadi::CollectionModifyJob(collection, nullptr);
46 if (job->error()) {
47 qCWarning(CALENDARVIEW_LOG) << "Failed to set CollectionColorAttribute:" << job->errorString();
48 }
49 });
50 }
51
52 // Also save the color in eventviewsrc (mostly historical)
53 preferences->setResourceColor(id, color);
54}
55
57{
58 if (!coll.isValid()) {
59 return {};
60 }
61
62 // Color stored in akonadi
63 const auto colorAttr = coll.attribute<Akonadi::CollectionColorAttribute>();
64 if (colorAttr != nullptr && colorAttr->color().isValid()) {
65 return colorAttr->color();
66 }
67
68 const QString id = QString::number(coll.id());
69 // Color stored in eventviewsrc (and in memory)
70 QColor color = preferences->resourceColorKnown(id);
71 if (color.isValid()) {
72 return color;
73 }
74 // Generate new color and store it in eventsviewsrc (and in memory)
75 return preferences->resourceColor(id);
76}
77
79{
80 return end.year() - start.year();
81}
82
84{
85 return QIcon::fromTheme(name).pixmap(16, 16);
86}
bool isValid() const
const T * attribute() const
void result(KJob *job)
Q_SCRIPTABLE Q_NOREPLY void start()
bool isColorDark(const QColor &color)
Determines if the color is "dark" or "light" by looking at its luminance.
Definition helper.cpp:20
EVENTVIEWS_EXPORT QColor resourceColor(const Akonadi::Collection &collection, const PrefsPtr &preferences)
This method returns the proper resource / subresource color for the view.
Definition helper.cpp:56
QPixmap cachedSmallIcon(const QString &name)
Equivalent to SmallIcon( name ), but uses QPixmapCache.
Definition helper.cpp:83
int yearDiff(QDate start, QDate end)
Returns the number of years between the start QDate and the end QDate (i.e.
Definition helper.cpp:78
EVENTVIEWS_EXPORT void setResourceColor(const Akonadi::Collection &collection, const QColor &color, const PrefsPtr &preferences)
This method sets the resource color as an Akonadi collection attribute and in the local preferences.
Definition helper.cpp:31
QColor getTextColor(const QColor &c)
Returns a nice QColor for text, give the input color &c.
Definition helper.cpp:26
int blue() const const
int green() const const
bool isValid() const const
int red() const const
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.