Commit 1ec2e08a authored by Olivier Churlaud's avatar Olivier Churlaud

Event view can use the CollectionColorAttribute color

This introduces 3 changes in Helper:
 - 3 Helper functions are now exported
 - resourceColor() checks if a color is set in the preferences files. If yes, it uses it. If not, it checks if there is a CollectionColorAttribute in the collection. If yes, it uses it else it call the resourceColor() from prefs (that can create a new color or return an invalid one)
 - setResourceColor() set a color in the preferences files ONLY IF it's not the same as the CollectionColor Attribute.

Bug: 328862

Differential Revision: https://phabricator.kde.org/D7500
parent 46845833
cmake_minimum_required(VERSION 3.0)
set(PIM_VERSION "5.6.40")
set(PIM_VERSION "5.6.41")
project(eventviews VERSION ${PIM_VERSION})
......
......@@ -115,6 +115,7 @@ install(TARGETS
ecm_generate_headers(eventviews_CamelCase_HEADERS
HEADER_NAMES
Helper
Prefs
EventView
REQUIRED_HEADERS eventviews_CamelCase_HEADERS
......
......@@ -26,6 +26,8 @@
#include <Collection>
#include <Item>
#include <AkonadiCore/CollectionColorAttribute>
#include <QIcon>
#include <QPixmap>
......@@ -35,13 +37,41 @@ QColor EventViews::getTextColor(const QColor &c)
return (luminance > 128.0) ? QColor(0, 0, 0) : QColor(255, 255, 255);
}
void EventViews::setResourceColor(const Akonadi::Collection &coll, const QColor &color, const PrefsPtr &preferences)
{
if (!coll.isValid()) {
return;
}
const QString id = QString::number(coll.id());
if (coll.hasAttribute<Akonadi::CollectionColorAttribute>()) {
Akonadi::CollectionColorAttribute *colorAttr = coll.attribute<Akonadi::CollectionColorAttribute>();
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);
}
QColor EventViews::resourceColor(const Akonadi::Collection &coll, const PrefsPtr &preferences)
{
if (!coll.isValid()) {
return QColor();
}
const QString id = QString::number(coll.id());
return preferences->resourceColor(id);
QColor color = preferences->resourceColorKnown(id);
if (!color.isValid() && coll.hasAttribute<Akonadi::CollectionColorAttribute>()) {
Akonadi::CollectionColorAttribute *colorAttr = coll.attribute<Akonadi::CollectionColorAttribute>();
if (colorAttr && colorAttr->color().isValid()) {
color = colorAttr->color();
} else {
return preferences->resourceColor(id);
}
}
return color;
}
QColor EventViews::resourceColor(const Akonadi::Item &item, const PrefsPtr &preferences)
......@@ -50,7 +80,17 @@ QColor EventViews::resourceColor(const Akonadi::Item &item, const PrefsPtr &pref
return QColor();
}
const QString id = QString::number(item.parentCollection().id());
return preferences->resourceColor(id);
QColor color = preferences->resourceColorKnown(id);
if (!color.isValid() && item.parentCollection().hasAttribute<Akonadi::CollectionColorAttribute>()) {
Akonadi::CollectionColorAttribute *colorAttr = item.parentCollection().attribute<Akonadi::CollectionColorAttribute>();
if (colorAttr && colorAttr->color().isValid()) {
color = colorAttr->color();
} else {
return preferences->resourceColor(id);
}
}
return color;
}
int EventViews::yearDiff(const QDate &start, const QDate &end)
......
......@@ -22,6 +22,9 @@
#ifndef EVENTVIEWS_HELPER_H
#define EVENTVIEWS_HELPER_H
#include "eventviews_export.h"
#include <QColor>
#include <QSharedPointer>
......@@ -49,18 +52,34 @@ QColor getTextColor(const QColor &c);
/**
This method returns the proper resource / subresource color for the view.
If a value is stored in the preferences, we use it, else we try to find a
CollectionColorAttribute in the collection. If everything else fails, a
random color can be set.
It is prefered to use this function instead of the
EventViews::Prefs::resourceColor function.
@return The resource color for the incidence. If the incidence belongs
to a subresource, the color for the subresource is returned (if set).
@param calendar the calendar for which the resource color should be obtained
@param incidence the incidence for which the color is needed (to
determine which subresource needs to be used)
*/
QColor resourceColor(const Akonadi::Item &incidence,
EVENTVIEWS_EXPORT QColor resourceColor(const Akonadi::Item &incidence,
const PrefsPtr &preferences);
QColor resourceColor(const Akonadi::Collection &collection,
EVENTVIEWS_EXPORT QColor resourceColor(const Akonadi::Collection &collection,
const PrefsPtr &preferences);
/**
This method sets the resource color in the preferences, only if it is
different from the CollectionColorAttribute. It is prefered to use this
instead of the EventViews::Prefs::setResourceColor function.
@param collection the collection for which the resource color should be stored
@param color the color to stored
@param preferences a pointer to the EventViews::Prefs to use
*/
EVENTVIEWS_EXPORT void setResourceColor(const Akonadi::Collection &collection,
const QColor &color, const PrefsPtr &preferences);
/**
Returns the number of years between the @p start QDate and the @p end QDate
(i.e. the difference in the year number of both dates)
......
......@@ -30,6 +30,9 @@
#include "calendarview_debug.h"
#include <QFontDatabase>
#include <AkonadiCore/AttributeFactory>
#include <AkonadiCore/CollectionColorAttribute>
using namespace EventViews;
QSet<EventViews::EventView::ItemIcon> iconArrayToSet(const QByteArray &array)
......@@ -499,10 +502,14 @@ QFont Prefs::Private::getFont(const KConfigSkeleton::ItemFont *baseConfigItem) c
Prefs::Prefs() : d(new Private(this))
{
// necessary to use CollectionColorAttribute in the EventViews::resourceColor and EventViews::setResourceColor
Akonadi::AttributeFactory::registerAttribute<Akonadi::CollectionColorAttribute>();
}
Prefs::Prefs(KCoreConfigSkeleton *appConfig) : d(new Private(this, appConfig))
{
// necessary to use CollectionColorAttribute in the EventViews::resourceColor and EventViews::setResourceColor
Akonadi::AttributeFactory::registerAttribute<Akonadi::CollectionColorAttribute>();
}
Prefs::~Prefs()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment