File indexing completed on 2025-01-05 03:57:54

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-08-27
0007  * Description : Showfoto folder view model.
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotofolderviewmodel.h"
0016 
0017 // Qt includes
0018 
0019 #include <QPainter>
0020 #include <QIODevice>
0021 #include <QModelIndex>
0022 #include <QMimeDatabase>
0023 #include <QFileInfo>
0024 
0025 // Local includes
0026 
0027 #include "digikam_debug.h"
0028 #include "digikam_globals.h"
0029 #include "drawdecoder.h"
0030 #include "thumbnailloadthread.h"
0031 #include "showfotofolderviewlist.h"
0032 
0033 using namespace Digikam;
0034 
0035 namespace ShowFoto
0036 {
0037 
0038 class Q_DECL_HIDDEN ShowfotoFolderViewModel::Private
0039 {
0040 
0041 public:
0042 
0043     explicit Private()
0044         : thumbnailThread (nullptr),
0045           view            (nullptr)
0046     {
0047     }
0048 
0049     ThumbnailLoadThread*    thumbnailThread;
0050     ShowfotoFolderViewList* view;
0051 
0052     static const int        s_maxSize;         ///< Max icon size.
0053 };
0054 
0055 const int ShowfotoFolderViewModel::Private::s_maxSize = 256;
0056 
0057 ShowfotoFolderViewModel::ShowfotoFolderViewModel(ShowfotoFolderViewList* const view)
0058     : QFileSystemModel(view),
0059       d               (new Private)
0060 {
0061     d->view               = view;
0062 
0063     setObjectName(QLatin1String("ShowfotoFolderViewModel"));
0064 
0065     // --- Populate the model
0066 
0067     setRootPath(QDir::rootPath());
0068 
0069     // If an item fails the filter, hide it
0070 
0071     setNameFilterDisables(false);
0072 
0073     d->thumbnailThread = new ThumbnailLoadThread;
0074     d->thumbnailThread->setSendSurrogatePixmap(false);
0075     d->thumbnailThread->setThumbnailSize(Private::s_maxSize);
0076 
0077     connect(d->thumbnailThread, SIGNAL(signalThumbnailLoaded(LoadingDescription,QPixmap)),
0078             this, SLOT(refreshThumbnails(LoadingDescription,QPixmap)));
0079 }
0080 
0081 ShowfotoFolderViewModel::~ShowfotoFolderViewModel()
0082 {
0083     d->thumbnailThread->stopAllTasks();
0084 
0085     delete d->thumbnailThread;
0086     delete d;
0087 }
0088 
0089 int ShowfotoFolderViewModel::maxIconSize()
0090 {
0091     return (Private::s_maxSize);
0092 }
0093 
0094 QVariant ShowfotoFolderViewModel::data(const QModelIndex& index, int role) const
0095 {
0096     if ((role == Qt::DecorationRole) && (index.column() == 0))
0097     {
0098         QString path = fileInfo(index).absoluteFilePath();
0099 
0100         if (isReadableImageFile(path))
0101         {
0102             QPixmap pix;
0103 
0104             if (d->thumbnailThread->find(ThumbnailIdentifier(path), pix))
0105             {
0106                 pix = pix.scaled(d->view->iconSize(), Qt::KeepAspectRatio,
0107                                                       Qt::FastTransformation);
0108 
0109                 QPixmap icon(d->view->iconSize());
0110                 icon.fill(Qt::transparent);
0111                 QPainter p(&icon);
0112                 p.drawPixmap((icon.width()  - pix.width() ) / 2,
0113                              (icon.height() - pix.height()) / 2,
0114                              pix);
0115 
0116                 return icon;
0117             }
0118         }
0119     }
0120 
0121     return QFileSystemModel::data(index, role);
0122 }
0123 
0124 void ShowfotoFolderViewModel::refreshThumbnails(const LoadingDescription& desc, const QPixmap& pix)
0125 {
0126     QModelIndex current = index(desc.filePath);
0127 
0128     if (current.isValid() && !pix.isNull())
0129     {
0130         Q_EMIT dataChanged(current, current);
0131     }
0132 }
0133 
0134 QStringList ShowfotoFolderViewModel::currentFilesPath() const
0135 {
0136     QStringList paths;
0137     QModelIndex idx = index(rootPath());
0138     QModelIndex child;
0139     QString file;
0140 
0141     for (int i = 0 ; i < rowCount(idx) ; ++i)
0142     {
0143         child = index(i, idx.column(), idx);
0144         file  = filePath(child);
0145 
0146         if (QFileInfo(file).isFile())
0147         {
0148             paths << file;
0149         }
0150     }
0151 
0152     return paths;
0153 }
0154 
0155 } // namespace ShowFoto
0156 
0157 #include "moc_showfotofolderviewmodel.cpp"