File indexing completed on 2024-06-02 04:15:13

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-03-22
0007  * Description : Qt Model for Albums
0008  *
0009  * SPDX-FileCopyrightText: 2008-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "albummodel_p.h"
0016 
0017 namespace Digikam
0018 {
0019 
0020 DateAlbumModel::DateAlbumModel(QObject* const parent)
0021     : AbstractCountingAlbumModel(Album::DATE,
0022                                  AlbumManager::instance()->findDAlbum(0),
0023                                  IgnoreRootAlbum, parent)
0024 {
0025     m_columnHeader = i18n("Dates");
0026 
0027     connect(AlbumManager::instance(), SIGNAL(signalDAlbumsDirty(QMap<YearMonth,int>)),
0028             this, SLOT(setYearMonthMap(QMap<YearMonth,int>)));
0029 
0030     setYearMonthMap(AlbumManager::instance()->getDAlbumsCount());
0031 
0032     setup();
0033 }
0034 
0035 DAlbum* DateAlbumModel::albumForIndex(const QModelIndex& index) const
0036 {
0037     return (static_cast<DAlbum*>(AbstractCountingAlbumModel::albumForIndex(index)));
0038 }
0039 
0040 QModelIndex DateAlbumModel::monthIndexForDate(const QDate& date) const
0041 {
0042     // iterate over all years
0043 
0044     for (int yearIndex = 0 ; yearIndex < rowCount() ; ++yearIndex)
0045     {
0046         QModelIndex year        = index(yearIndex, 0);
0047         DAlbum* const yearAlbum = albumForIndex(year);
0048 
0049         // do not search through months if we are sure, that the year already
0050         // does not match
0051 
0052         if (yearAlbum                            &&
0053             (yearAlbum->range() == DAlbum::Year) &&
0054             (yearAlbum->date().year() != date.year()))
0055         {
0056             continue;
0057         }
0058 
0059         // search the album with the correct month
0060 
0061         for (int monthIndex = 0 ; monthIndex < rowCount(year) ; ++monthIndex)
0062         {
0063             QModelIndex month        = index(monthIndex, 0, year);
0064             DAlbum* const monthAlbum = albumForIndex(month);
0065 
0066             if (monthAlbum                                  &&
0067                 (monthAlbum->range() == DAlbum::Month)      &&
0068                 (monthAlbum->date().year() == date.year())  &&
0069                 (monthAlbum->date().month() == date.month()))
0070             {
0071                 return month;
0072             }
0073         }
0074 
0075     }
0076 
0077     return QModelIndex();
0078 }
0079 
0080 void DateAlbumModel::setPixmaps(const QPixmap& forYearAlbums, const QPixmap& forMonthAlbums)
0081 {
0082     m_yearPixmap  = forYearAlbums;
0083     m_monthPixmap = forMonthAlbums;
0084 }
0085 
0086 QString DateAlbumModel::albumName(Album* album) const
0087 {
0088     DAlbum* const dalbum = static_cast<DAlbum*>(album);
0089 
0090     if (dalbum->range() == DAlbum::Year)
0091     {
0092         return QString::number(dalbum->date().year());
0093     }
0094     else
0095     {
0096         return QLocale().standaloneMonthName(dalbum->date().month(), QLocale::LongFormat);
0097     }
0098 }
0099 
0100 QVariant DateAlbumModel::decorationRoleData(Album* album) const
0101 {
0102     DAlbum* const dalbum = static_cast<DAlbum*>(album);
0103 
0104     if (dalbum->range() == DAlbum::Year)
0105     {
0106         return m_yearPixmap;
0107     }
0108     else
0109     {
0110         return m_monthPixmap;
0111     }
0112 }
0113 
0114 QVariant DateAlbumModel::sortRoleData(Album* a) const
0115 {
0116     DAlbum* const dalbum = static_cast<DAlbum*>(a);
0117 
0118     if (dalbum)
0119     {
0120         return dalbum->date();
0121     }
0122 
0123     qCDebug(DIGIKAM_GENERAL_LOG) << "There must be a data album.";
0124 
0125     return QDate();
0126 }
0127 
0128 Album* DateAlbumModel::albumForId(int id) const
0129 {
0130     return AlbumManager::instance()->findDAlbum(id);
0131 }
0132 
0133 void DateAlbumModel::setYearMonthMap(const QMap<YearMonth, int>& yearMonthMap)
0134 {
0135     AlbumIterator it(rootAlbum());
0136 
0137     QHash<int, int> albumToCountHash;
0138 
0139     while (it.current())
0140     {
0141         DAlbum* const dalbum = static_cast<DAlbum*>(*it);
0142         QDate date           = dalbum->date();
0143 
0144         switch (dalbum->range())
0145         {
0146             case DAlbum::Month:
0147             {
0148                 QMap<YearMonth, int>::const_iterator it2 = yearMonthMap.constFind(YearMonth(date.year(), date.month()));
0149 
0150                 if (it2 != yearMonthMap.constEnd())
0151                 {
0152                     albumToCountHash.insert((*it)->id(), it2.value());
0153                 }
0154 
0155                 break;
0156             }
0157 
0158             case DAlbum::Year:
0159             {
0160                 // a year itself cannot contain images and therefore always has count 0
0161 
0162                 albumToCountHash.insert((*it)->id(), 0);
0163                 break;
0164             }
0165 
0166             default:
0167             {
0168                 qCDebug(DIGIKAM_GENERAL_LOG) << "Untreated DAlbum range " << dalbum->range();
0169                 albumToCountHash.insert((*it)->id(), 0);
0170                 break;
0171             }
0172         }
0173 
0174         ++it;
0175     }
0176 
0177     setCountHash(albumToCountHash);
0178 }
0179 
0180 } // namespace Digikam