File indexing completed on 2024-05-19 05:13:58
0001 /* 0002 SPDX-FileCopyrightText: 2005 Reinhold Kainhofer <reinhold@kainhofer.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0005 */ 0006 0007 #include "helper.h" 0008 #include "calendarview_debug.h" 0009 #include "prefs.h" 0010 0011 #include <Akonadi/Collection> 0012 #include <Akonadi/Item> 0013 0014 #include <Akonadi/CollectionColorAttribute> 0015 #include <Akonadi/CollectionModifyJob> 0016 0017 #include <QIcon> 0018 #include <QPixmap> 0019 0020 bool EventViews::isColorDark(const QColor &c) 0021 { 0022 double luminance = (c.red() * 0.299) + (c.green() * 0.587) + (c.blue() * 0.114); 0023 return (luminance < 128.0) ? true : false; 0024 } 0025 0026 QColor EventViews::getTextColor(const QColor &c) 0027 { 0028 return (!isColorDark(c)) ? QColor(0, 0, 0) : QColor(255, 255, 255); 0029 } 0030 0031 void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences) 0032 { 0033 if (!coll.isValid() || !color.isValid()) { 0034 return; 0035 } 0036 0037 const QString id = QString::number(coll.id()); 0038 0039 // Save the color in akonadi (so the resource can even save it server-side) 0040 Akonadi::Collection collection = coll; 0041 auto colorAttr = collection.attribute<Akonadi::CollectionColorAttribute>(Akonadi::Collection::AddIfMissing); 0042 if (colorAttr) { 0043 colorAttr->setColor(color); 0044 auto job = new Akonadi::CollectionModifyJob(collection, nullptr); 0045 QObject::connect(job, &Akonadi::CollectionModifyJob::result, [job]() { 0046 if (job->error()) { 0047 qCWarning(CALENDARVIEW_LOG) << "Failed to set CollectionColorAttribute:" << job->errorString(); 0048 } 0049 }); 0050 } 0051 0052 // Also save the color in eventviewsrc (mostly historical) 0053 preferences->setResourceColor(id, color); 0054 } 0055 0056 QColor EventViews::resourceColor(const Akonadi::Collection &coll, const PrefsPtr &preferences) 0057 { 0058 if (!coll.isValid()) { 0059 return {}; 0060 } 0061 const QString id = QString::number(coll.id()); 0062 // Color stored in eventviewsrc (and in memory) 0063 QColor color = preferences->resourceColorKnown(id); 0064 if (color.isValid()) { 0065 return color; 0066 } 0067 // Color stored in akonadi 0068 if (coll.hasAttribute<Akonadi::CollectionColorAttribute>()) { 0069 const auto colorAttr = coll.attribute<Akonadi::CollectionColorAttribute>(); 0070 if (colorAttr && colorAttr->color().isValid()) { 0071 return colorAttr->color(); 0072 } 0073 } 0074 // Generate new color and store it in eventsviewsrc (and in memory) 0075 return preferences->resourceColor(id); 0076 } 0077 0078 QColor EventViews::resourceColor(const Akonadi::Item &item, const PrefsPtr &preferences) 0079 { 0080 if (!item.isValid()) { 0081 return {}; 0082 } 0083 return resourceColor(item.parentCollection(), preferences); 0084 } 0085 0086 int EventViews::yearDiff(QDate start, QDate end) 0087 { 0088 return end.year() - start.year(); 0089 } 0090 0091 QPixmap EventViews::cachedSmallIcon(const QString &name) 0092 { 0093 return QIcon::fromTheme(name).pixmap(16, 16); 0094 }