File indexing completed on 2024-04-28 17:06:21

0001 /*
0002     SPDX-FileCopyrightText: 2009 Jan Lepper <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2009-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "krpreviews.h"
0009 #include "krpreviewjob.h"
0010 
0011 #include "../FileSystem/fileitem.h"
0012 #include "../defaults.h"
0013 #include "PanelView/krview.h"
0014 #include "PanelView/krviewitem.h"
0015 #include "krcolorcache.h"
0016 
0017 #include <stdio.h>
0018 
0019 #define ASSERT(what)                                                                                                                                           \
0020     if (!(what))                                                                                                                                               \
0021         abort();
0022 
0023 KrPreviews::KrPreviews(KrView *view)
0024     : _job(nullptr)
0025     , _view(view)
0026 {
0027     _dim = KrColorCache::getColorCache().getDimSettings(_dimColor, _dimFactor);
0028     connect(&KrColorCache::getColorCache(), &KrColorCache::colorsRefreshed, this, &KrPreviews::slotRefreshColors);
0029 }
0030 
0031 KrPreviews::~KrPreviews()
0032 {
0033     clear();
0034 }
0035 
0036 void KrPreviews::clear()
0037 {
0038     if (_job) {
0039         _job->kill(KJob::EmitResult);
0040         _job = nullptr;
0041     }
0042     _previews.clear();
0043     _previewsInactive.clear();
0044 }
0045 
0046 void KrPreviews::update()
0047 {
0048     if (_job)
0049         return;
0050     for (KrViewItem *it = _view->getFirst(); it != nullptr; it = _view->getNext(it)) {
0051         if (!_previews.contains(it->getFileItem()))
0052             updatePreview(it);
0053     }
0054 }
0055 
0056 void KrPreviews::deletePreview(KrViewItem *item)
0057 {
0058     if (_job) {
0059         _job->removeItem(item);
0060     }
0061     removePreview(item->getFileItem());
0062 }
0063 
0064 void KrPreviews::updatePreview(KrViewItem *item)
0065 {
0066     if (!_job) {
0067         _job = new KrPreviewJob(this);
0068         connect(_job, &KrPreviewJob::result, this, &KrPreviews::slotJobResult);
0069         _view->op()->emitPreviewJobStarted(_job);
0070     }
0071     _job->scheduleItem(item);
0072 }
0073 
0074 bool KrPreviews::getPreview(const FileItem *file, QPixmap &pixmap, bool active)
0075 {
0076     if (active || !_dim)
0077         pixmap = _previews.value(file);
0078     else
0079         pixmap = _previewsInactive.value(file);
0080 
0081     return !pixmap.isNull();
0082 }
0083 
0084 void KrPreviews::slotJobResult(KJob *job)
0085 {
0086     (void)job;
0087     _job = nullptr;
0088 }
0089 
0090 void KrPreviews::slotRefreshColors()
0091 {
0092     clear();
0093     _dim = KrColorCache::getColorCache().getDimSettings(_dimColor, _dimFactor);
0094     update();
0095 }
0096 
0097 void KrPreviews::addPreview(const FileItem *file, const QPixmap &preview)
0098 {
0099     QPixmap active, inactive;
0100 
0101     if (preview.isNull()) {
0102         active = KrView::getIcon(const_cast<FileItem *>(file), true, _view->fileIconSize());
0103         if (_dim)
0104             inactive = KrView::getIcon(const_cast<FileItem *>(file), false, _view->fileIconSize());
0105     } else {
0106         active = KrView::processIcon(preview, false, _dimColor, _dimFactor, file->isSymLink());
0107         if (_dim)
0108             inactive = KrView::processIcon(preview, true, _dimColor, _dimFactor, file->isSymLink());
0109     }
0110 
0111     _previews.insert(file, active);
0112     if (_dim)
0113         _previewsInactive.insert(file, inactive);
0114 }
0115 
0116 void KrPreviews::removePreview(const FileItem *file)
0117 {
0118     _previews.remove(file);
0119     _previewsInactive.remove(file);
0120 }