File indexing completed on 2025-01-19 03:59:30

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-08-19
0007  * Description : image quality sorter maintenance tool
0008  *
0009  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2013-2014 by Gowtham Ashok <gwty93 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2021-2022 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "imagequalitysorter.h"
0018 
0019 // Qt includes
0020 
0021 #include <QString>
0022 #include <QIcon>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "digikam_globals.h"
0031 #include "dimg.h"
0032 #include "coredb.h"
0033 #include "albummanager.h"
0034 #include "coredbaccess.h"
0035 #include "tagscache.h"
0036 #include "maintenancethread.h"
0037 
0038 namespace Digikam
0039 {
0040 
0041 class Q_DECL_HIDDEN ImageQualitySorter::Private
0042 {
0043 public:
0044 
0045     explicit Private()
0046       : mode  (ImageQualitySorter::NonAssignedItems),
0047         thread(nullptr)
0048     {
0049     }
0050 
0051     QualityScanMode       mode;
0052 
0053     ImageQualityContainer quality;
0054 
0055     QStringList           allPicturesPath;
0056 
0057     AlbumList             albumList;
0058 
0059     MaintenanceThread*    thread;
0060 };
0061 
0062 ImageQualitySorter::ImageQualitySorter(QualityScanMode mode,
0063                                        const AlbumList& list,
0064                                        const ImageQualityContainer& quality,
0065                                        ProgressItem* const parent)
0066     : MaintenanceTool(QLatin1String("ImageQualitySorter"), parent),
0067       d(new Private)
0068 {
0069     d->mode       = mode;
0070     d->albumList  = list;
0071     d->quality    = quality;
0072     d->thread     = new MaintenanceThread(this);
0073 
0074     connect(d->thread, SIGNAL(signalCompleted()),
0075             this, SLOT(slotDone()));
0076 
0077     connect(d->thread, SIGNAL(signalAdvance(QImage)),
0078             this, SLOT(slotAdvance(QImage)));
0079 }
0080 
0081 ImageQualitySorter::~ImageQualitySorter()
0082 {
0083     delete d;
0084 }
0085 
0086 void ImageQualitySorter::setUseMultiCoreCPU(bool b)
0087 {
0088     d->thread->setUseMultiCore(b);
0089 }
0090 
0091 void ImageQualitySorter::slotCancel()
0092 {
0093     d->thread->cancel();
0094     MaintenanceTool::slotCancel();
0095 }
0096 
0097 void ImageQualitySorter::slotStart()
0098 {
0099     MaintenanceTool::slotStart();
0100 
0101     setLabel(i18n("Image Quality Sorter"));
0102 
0103     ProgressManager::addProgressItem(this);
0104 
0105     if (d->albumList.isEmpty())
0106     {
0107         d->albumList = AlbumManager::instance()->allPAlbums();
0108     }
0109 
0110     // Get all item in DB which do not have any Pick Label assigned.
0111 
0112     QStringList dirty = CoreDbAccess().db()->getItemsURLsWithTag(TagsCache::instance()->tagForPickLabel(NoPickLabel));
0113 
0114     // Get all digiKam albums collection pictures path, depending of d->rebuildAll flag.
0115 
0116     for (AlbumList::ConstIterator it = d->albumList.constBegin() ;
0117          !canceled() && (it != d->albumList.constEnd()) ; ++it)
0118     {
0119         QStringList aPaths;
0120 
0121         if      ((*it)->type() == Album::PHYSICAL)
0122         {
0123             Q_FOREACH (const QString& path, CoreDbAccess().db()->getItemURLsInAlbum((*it)->id()))
0124             {
0125                 if (!aPaths.contains(path))
0126                 {
0127                     aPaths << path;
0128                 }
0129             }
0130         }
0131         else if ((*it)->type() == Album::TAG)
0132         {
0133             Q_FOREACH (const QString& path, CoreDbAccess().db()->getItemURLsInTag((*it)->id()))
0134             {
0135                 if (!aPaths.contains(path))
0136                 {
0137                     aPaths << path;
0138                 }
0139             }
0140         }
0141 
0142         if (d->mode == NonAssignedItems)
0143         {
0144             Q_FOREACH (const QString& path, aPaths)
0145             {
0146                 if (dirty.contains(path))
0147                 {
0148                     d->allPicturesPath += path;
0149                 }
0150             }
0151         }
0152         else  // AllItems
0153         {
0154             d->allPicturesPath += aPaths;
0155         }
0156     }
0157 
0158     if (d->allPicturesPath.isEmpty())
0159     {
0160         slotDone();
0161 
0162         return;
0163     }
0164 
0165     setTotalItems(d->allPicturesPath.count());
0166 
0167     d->thread->sortByImageQuality(d->allPicturesPath, d->quality);
0168     d->thread->start();
0169 }
0170 
0171 void ImageQualitySorter::slotAdvance(const QImage& img)
0172 {
0173     setThumbnail(QIcon(QPixmap::fromImage(img)));
0174     advance(1);
0175 }
0176 
0177 } // namespace Digikam
0178 
0179 #include "moc_imagequalitysorter.cpp"