diff --git a/calendarviews/helper.cpp b/calendarviews/helper.cpp --- a/calendarviews/helper.cpp +++ b/calendarviews/helper.cpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -42,6 +43,10 @@ if ( !coll.isValid() ) { return QColor(); } + Akonadi::CollectionColorAttribute *colorAttribute = coll.attribute(); + if (colorAttribute) { + return colorAttribute->color(); + } const QString id = QString::number( coll.id() ); return preferences->resourceColor( id ); } @@ -51,8 +56,7 @@ if ( !item.isValid() ) { return QColor(); } - const QString id = QString::number( item.parentCollection().id() ); - return preferences->resourceColor( id ); + return resourceColor(item.parentCollection(), preferences); } int EventViews::yearDiff( const QDate &start, const QDate &end ) diff --git a/calendarviews/viewcalendar.cpp b/calendarviews/viewcalendar.cpp --- a/calendarviews/viewcalendar.cpp +++ b/calendarviews/viewcalendar.cpp @@ -187,7 +187,8 @@ QColor AkonadiViewCalendar::resourceColor(const KCalCore::Incidence::Ptr &incidence) const { - return EventViews::resourceColor( item(incidence), mEventView->preferences() ); + // We need the uptodate parent collection and not an old version of it. + return EventViews::resourceColor(mCalendar->collection(item(incidence).parentCollection().id()), mEventView->preferences() ); } QString AkonadiViewCalendar::uid(const KCalCore::Incidence::Ptr &incidence) const diff --git a/korganizer/akonadicollectionview.h b/korganizer/akonadicollectionview.h --- a/korganizer/akonadicollectionview.h +++ b/korganizer/akonadicollectionview.h @@ -112,6 +112,7 @@ void onSearchIsActive(bool); void onAction(const QModelIndex &index, int action); void allButtonClicked(bool); + void calendarColorChanged(const QModelIndex &topleft, const QModelIndex &buttomDown); private: Akonadi::EntityTreeModel *entityTreeModel() const; diff --git a/korganizer/akonadicollectionview.cpp b/korganizer/akonadicollectionview.cpp --- a/korganizer/akonadicollectionview.cpp +++ b/korganizer/akonadicollectionview.cpp @@ -719,6 +719,8 @@ mEnableAction->setIcon(KIconLoader().loadIcon(QLatin1String("bookmarks"), KIconLoader::Small)); connect( mDefaultCalendar, SIGNAL(triggered(bool)), this, SLOT(setDefaultCalendar()) ); + + connect(collectionFilter, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(calendarColorChanged(QModelIndex,QModelIndex))); } } @@ -757,34 +759,43 @@ void AkonadiCollectionView::assignColor() { - QModelIndex index = mCollectionView->selectionModel()->currentIndex(); //selectedRows() + const QModelIndex index = mCollectionView->selectionModel()->currentIndex(); //selectedRows() Q_ASSERT( index.isValid() ); - const Akonadi::Collection collection = CalendarSupport::collectionFromIndex( index ); + Akonadi::Collection collection = CalendarSupport::collectionFromIndex( index ); Q_ASSERT( collection.isValid() ); - const QString identifier = QString::number( collection.id() ); - const QColor defaultColor = KOPrefs::instance()->resourceColor( identifier ); + const QColor defaultColor = KOHelper::resourceColor(collection); QColor myColor; const int result = KColorDialog::getColor( myColor, defaultColor ); if ( result == KColorDialog::Accepted && myColor != defaultColor ) { - KOPrefs::instance()->setResourceColor( identifier, myColor ); - emit colorsChanged(); + KOHelper::setResourceColor(collection, myColor); + + mCollectionView->model()->setData(index, QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole); updateMenu(); updateView(); } } +void AkonadiCollectionView::calendarColorChanged(const QModelIndex &topLeft, const QModelIndex &buttomRight) +{ + //We may want to have here a logic to detect only color changes + Q_UNUSED(topLeft); + Q_UNUSED(buttomRight); + Akonadi::Collection collection = CalendarSupport::collectionFromIndex(topLeft); + Q_ASSERT( collection.isValid() ); + emit colorsChanged(); +} + void AkonadiCollectionView::disableColor() { QModelIndex index = mCollectionView->selectionModel()->currentIndex(); //selectedRows() Q_ASSERT( index.isValid() ); - const Akonadi::Collection collection = CalendarSupport::collectionFromIndex( index ); + Akonadi::Collection collection = CalendarSupport::collectionFromIndex( index ); Q_ASSERT( collection.isValid() ); - const QString identifier = QString::number( collection.id() ); - KOPrefs::instance()->setResourceColor( identifier, QColor() ); + KOHelper::setResourceColor(collection, QColor()); + mCollectionView->model()->setData(index, QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole); updateMenu(); updateView(); - emit colorsChanged(); } void AkonadiCollectionView::setCollectionSelectionProxyModel( KCheckableProxyModel *m ) @@ -837,8 +848,7 @@ const Akonadi::Collection collection = CalendarSupport::collectionFromIndex( index ); if ( collection.isValid() && !collection.contentMimeTypes().isEmpty() ) { - const QString identifier = QString::number( collection.id() ); - const QColor defaultColor = KOPrefs::instance()->resourceColor( identifier ); + const QColor defaultColor = KOHelper::resourceColor(collection); enableAction = enableAction && defaultColor.isValid(); mDisableColor->setEnabled( enableAction ); mDefaultCalendar->setEnabled( !KOHelper::isStandardCalendar( collection.id() ) && diff --git a/korganizer/kohelper.h b/korganizer/kohelper.h --- a/korganizer/kohelper.h +++ b/korganizer/kohelper.h @@ -59,7 +59,7 @@ KORGANIZERPRIVATE_EXPORT QColor resourceColor( const Akonadi::Collection &collection ); KORGANIZERPRIVATE_EXPORT QColor resourceColorKnown( const Akonadi::Collection &collection ); - KORGANIZERPRIVATE_EXPORT void setResourceColor( const Akonadi::Collection &collection, const QColor &color ); + KORGANIZERPRIVATE_EXPORT void setResourceColor( Akonadi::Collection &collection, const QColor &color ); /** Returns the number of years between the @p start QDate and the @p end QDate diff --git a/korganizer/kohelper.cpp b/korganizer/kohelper.cpp --- a/korganizer/kohelper.cpp +++ b/korganizer/kohelper.cpp @@ -26,6 +26,7 @@ #include "koprefs.h" #include +#include #include @@ -41,6 +42,11 @@ return QColor(); } + Akonadi::CollectionColorAttribute *colorAttribute = coll.attribute(); + if (colorAttribute) { + return colorAttribute->color(); + } + const QString id = QString::number( coll.id() ); return KOPrefs::instance()->resourceColor( id ); } @@ -51,27 +57,26 @@ return QColor(); } - const QString id = QString::number( coll.id() ); - return KOPrefs::instance()->resourceColorKnown( id ); + Akonadi::CollectionColorAttribute *colorAttribute = coll.attribute(); + if (colorAttribute) { + return colorAttribute->color(); + } } -void KOHelper::setResourceColor(const Akonadi::Collection &collection, const QColor &color) +void KOHelper::setResourceColor(Akonadi::Collection &collection, const QColor &color) { if ( collection.isValid() ) { - const QString id = QString::number( collection.id() ); - return KOPrefs::instance()->setResourceColor(id, color); + collection.attribute(Akonadi::Collection::AddIfMissing)->setColor(color); } } - QColor KOHelper::resourceColor( const Akonadi::Item &item ) { if ( !item.isValid() ) { return QColor(); } - const QString id = QString::number( item.storageCollectionId() ); - return KOPrefs::instance()->resourceColor( id ); + return resourceColor(item.parentCollection()); } int KOHelper::yearDiff( const QDate &start, const QDate &end ) diff --git a/korganizer/koprefsdialog.h b/korganizer/koprefsdialog.h --- a/korganizer/koprefsdialog.h +++ b/korganizer/koprefsdialog.h @@ -85,7 +85,7 @@ Akonadi::CollectionComboBox *mResourceCombo; KColorButton *mResourceButton; - QHash mResourceDict; + QHash mResourceDict; }; class KCM_KORGANIZER_EXPORT KOPrefsDialogGroupScheduling : public KPIM::KPrefsModule diff --git a/korganizer/koprefsdialog.cpp b/korganizer/koprefsdialog.cpp --- a/korganizer/koprefsdialog.cpp +++ b/korganizer/koprefsdialog.cpp @@ -27,11 +27,13 @@ #include "kitemiconcheckcombo.h" #include "kocore.h" #include "koglobals.h" +#include "kohelper.h" #include "koprefs.h" #include "ui_kogroupwareprefspage.h" #include #include +#include #include #include @@ -43,6 +45,7 @@ #include #include #include +#include #include #include @@ -965,18 +968,26 @@ void KOPrefsDialogColorsAndFonts::usrWriteConfig() { - QHash::const_iterator i = mCategoryDict.constBegin(); - while ( i != mCategoryDict.constEnd() ) { - CalendarSupport::KCalPrefs::instance()->setCategoryColor( i.key(), i.value() ); - ++i; + { + QHash::const_iterator i = mCategoryDict.constBegin(); + while ( i != mCategoryDict.constEnd() ) { + CalendarSupport::KCalPrefs::instance()->setCategoryColor( i.key(), i.value() ); + ++i; + } } - i = mResourceDict.constBegin(); - while ( i != mResourceDict.constEnd() ) { - KOPrefs::instance()->setResourceColor( i.key(), i.value() ); - ++i; + { + QHash::const_iterator i = mResourceDict.constBegin(); + while ( i != mResourceDict.constEnd() ) { + const QModelIndex index = i.key(); + if (index.isValid()) { + Akonadi::Collection col = CalendarSupport::collectionFromIndex(index); + KOHelper::setResourceColor(col, i.value()); + mResourceCombo->model()->setData(index, QVariant::fromValue(col), Akonadi::EntityTreeModel::CollectionRole); + } + ++i; + } } - //mCalendarViewsPrefs->writeConfig(); } @@ -1018,33 +1029,25 @@ void KOPrefsDialogColorsAndFonts::setResourceColor() { - bool ok; - const QString id = - QString::number( mResourceCombo->itemData( - mResourceCombo->currentIndex(), - Akonadi::CollectionModel::CollectionIdRole ).toLongLong( &ok ) ); - if( ! ok ) { - return; + const QModelIndex index = mResourceCombo->model()->index(mResourceCombo->currentIndex(), 0); + if (!index.isValid()) { + return; } - mResourceDict.insert( id, mResourceButton->color() ); + + mResourceDict.insert(QPersistentModelIndex(index), mResourceButton->color()); slotWidChanged(); } void KOPrefsDialogColorsAndFonts::updateResourceColor() { - bool ok; - const QString id = - QString::number( mResourceCombo->itemData( - mResourceCombo->currentIndex(), - Akonadi::CollectionModel::CollectionIdRole ).toLongLong( &ok ) ); - if ( !ok ) { - return; + const QModelIndex index = mResourceCombo->model()->index(mResourceCombo->currentIndex(), 0); + if (!index.isValid()) { + return; } - kDebug() << id << mResourceCombo->itemText( mResourceCombo->currentIndex() ); - QColor color = mResourceDict.value( id ); + QColor color = mResourceDict.value(QPersistentModelIndex(index)); if ( ! color.isValid() ) { - color = KOPrefs::instance()->resourceColor( id ); + color = KOHelper::resourceColor(mResourceCombo->currentCollection()); } mResourceButton->setColor( color ); } diff --git a/korganizer/views/collectionview/calendardelegate.cpp b/korganizer/views/collectionview/calendardelegate.cpp --- a/korganizer/views/collectionview/calendardelegate.cpp +++ b/korganizer/views/collectionview/calendardelegate.cpp @@ -153,7 +153,7 @@ { Q_ASSERT(index.isValid()); - const Akonadi::Collection col = CalendarSupport::collectionFromIndex(index); + Akonadi::Collection col = CalendarSupport::collectionFromIndex(index); //We display the toolbuttons while hovering const bool showButtons = option.state & QStyle::State_MouseOver; // const bool enabled = col.shouldList(Akonadi::Collection::ListDisplay); @@ -199,7 +199,6 @@ const Akonadi::Collection parentCol = personCollection(index); if (parentCol.isValid()) { color = KOHelper::resourceColor(parentCol); - KOHelper::setResourceColor(col, color); } else { color = KOHelper::resourceColor(col); }