File indexing completed on 2025-01-05 03:51:06

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-04-27
0007  * Description : a folder view for date albums.
0008  *
0009  * SPDX-FileCopyrightText: 2005      by Renchi Raju <renchi dot raju at gmail dot com>
0010  * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2009-2010 by Johannes Wienke <languitar at semipol dot de>
0012  * SPDX-FileCopyrightText: 2014      by Michael G. Hansen <mike at mghansen dot de>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "datefolderview.h"
0019 
0020 // KDE includes
0021 
0022 #include <kconfiggroup.h>
0023 
0024 // Local includes
0025 
0026 #include "digikam_debug.h"
0027 #include "album.h"
0028 #include "coredb.h"
0029 #include "applicationsettings.h"
0030 #include "datetreeview.h"
0031 #include "monthwidget.h"
0032 
0033 namespace Digikam
0034 {
0035 
0036 class Q_DECL_HIDDEN DateFolderView::Private
0037 {
0038 public:
0039 
0040     explicit Private()
0041       : active      (false),
0042         dateTreeView(nullptr),
0043         monthview   (nullptr)
0044     {
0045     }
0046 
0047     bool          active;
0048 
0049     DateTreeView* dateTreeView;
0050     MonthWidget*  monthview;
0051 };
0052 
0053 DateFolderView::DateFolderView(QWidget* const parent, DateAlbumModel* const dateAlbumModel)
0054     : DVBox            (parent),
0055       StateSavingObject(this),
0056       d                (new Private)
0057 {
0058     setObjectName(QLatin1String("DateFolderView"));
0059 
0060     d->dateTreeView = new DateTreeView(this);
0061     d->dateTreeView->setAlbumModel(dateAlbumModel);
0062     d->dateTreeView->setAlbumManagerCurrentAlbum(true);
0063     d->monthview    = new MonthWidget(this);
0064 
0065     connect(d->dateTreeView, SIGNAL(currentAlbumChanged(Album*)),
0066             this, SLOT(slotSelectionChanged(Album*)));
0067 
0068     // Loading of DAlbums may take longer that setting up the gui. Therefore
0069     // the first call to setActive may not set the current album in the album
0070     // manager as it is not yet loaded. To achieve this, we wait for loading
0071     // DAlbums and set the active album in the album manager if this tab is
0072     // active
0073 
0074     connect(AlbumManager::instance(), SIGNAL(signalAllDAlbumsLoaded()),
0075             this, SLOT(slotAllAlbumsLoaded()));
0076 }
0077 
0078 DateFolderView::~DateFolderView()
0079 {
0080     saveState();
0081 }
0082 
0083 void DateFolderView::setItemModel(ItemFilterModel* const model)
0084 {
0085     d->monthview->setItemModel(model);
0086 }
0087 
0088 void DateFolderView::setActive(const bool val)
0089 {
0090     if (d->active == val)
0091     {
0092         return;
0093     }
0094 
0095     d->active = val;
0096 
0097     if (d->active)
0098     {
0099         AlbumManager::instance()->setCurrentAlbums(QList<Album*>()
0100                                                    << d->dateTreeView->currentAlbum());
0101         slotSelectionChanged(d->dateTreeView->currentAlbum());
0102     }
0103     else
0104     {
0105         d->monthview->setActive(false);
0106     }
0107 }
0108 
0109 void DateFolderView::slotSelectionChanged(Album* selectedAlbum)
0110 {
0111     if (!d->active)
0112     {
0113         qCDebug(DIGIKAM_GENERAL_LOG) << "Not active, returning without action";
0114         return;
0115     }
0116 
0117     d->monthview->setActive(false);
0118 
0119     DAlbum* const dalbum = dynamic_cast<DAlbum*> (selectedAlbum);
0120 
0121     if (!dalbum)
0122     {
0123         return;
0124     }
0125 
0126     if (dalbum->range() == DAlbum::Month)
0127     {
0128         QDate date = dalbum->date();
0129         d->monthview->setActive(true);
0130         d->monthview->setYearMonth(date.year(), date.month());
0131     }
0132 
0133     if (d->active)
0134     {
0135         AlbumManager::instance()->setCurrentAlbums(QList<Album*>() << dalbum);
0136     }
0137 }
0138 
0139 void DateFolderView::slotAllAlbumsLoaded()
0140 {
0141     if (d->active)
0142     {
0143         QList<Album*> albums({d->dateTreeView->currentAlbum()});
0144 
0145         if (AlbumManager::instance()->currentAlbums() != albums)
0146         {
0147             AlbumManager::instance()->setCurrentAlbums(albums);
0148             slotSelectionChanged(d->dateTreeView->currentAlbum());
0149         }
0150     }
0151 
0152     // Workaround for bug 447874
0153 
0154     d->dateTreeView->setSortingEnabled(true);
0155 }
0156 
0157 void DateFolderView::setConfigGroup(const KConfigGroup& group)
0158 {
0159     StateSavingObject::setConfigGroup(group);
0160     d->dateTreeView->setConfigGroup(group);
0161 }
0162 
0163 void DateFolderView::doLoadState()
0164 {
0165     d->dateTreeView->loadState();
0166 }
0167 
0168 void DateFolderView::doSaveState()
0169 {
0170     d->dateTreeView->saveState();
0171 }
0172 
0173 void DateFolderView::gotoDate(const QDate& dt)
0174 {
0175     qCDebug(DIGIKAM_GENERAL_LOG) << "Going to date " << dt;
0176 
0177     QModelIndex dateIndex = d->dateTreeView->albumModel()->monthIndexForDate(dt);
0178 
0179     if (!dateIndex.isValid())
0180     {
0181         qCDebug(DIGIKAM_GENERAL_LOG) << "Cannot find an album for date " << dt;
0182 
0183         return;
0184     }
0185 
0186     DAlbum* const dateAlbum = d->dateTreeView->albumModel()->albumForIndex(dateIndex);
0187 
0188     if (!dateAlbum)
0189     {
0190         qCWarning(DIGIKAM_GENERAL_LOG) << "Could not retrieve an album for index " << dateIndex;
0191 
0192         return;
0193     }
0194 
0195     qCDebug(DIGIKAM_GENERAL_LOG) << "Got date album " << dateAlbum;
0196 
0197     d->dateTreeView->setCurrentAlbums(QList<Album*>() << dateAlbum);
0198 
0199 }
0200 
0201 void DateFolderView::changeAlbumFromHistory(DAlbum* const album)
0202 {
0203     d->dateTreeView->setCurrentAlbums(QList<Album*>() << album);
0204 }
0205 
0206 AlbumPointer<DAlbum> DateFolderView::currentAlbum() const
0207 {
0208     return AlbumPointer<DAlbum>(d->dateTreeView->currentAlbum());
0209 }
0210 
0211 } // namespace Digikam
0212 
0213 #include "moc_datefolderview.cpp"