1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
SPDX-FileCopyrightText: 2009 Jan Lepper <krusader@users.sourceforge.net>
SPDX-FileCopyrightText: 2009-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "krpreviewjob.h"
#include "../FileSystem/fileitem.h"
#include "../defaults.h"
#include "PanelView/krview.h"
#include "PanelView/krviewitem.h"
#include "krpreviews.h"
#include <stdio.h>
// QtWidgets
#include <QWidget>
#define ASSERT(what) \
if (!(what)) \
abort();
// how much items to process by a single job
// view becomes unresponsive during load if set too high
#define MAX_CHUNK_SIZE 50
KrPreviewJob::KrPreviewJob(KrPreviews *parent)
: _job(nullptr)
, _parent(parent)
{
_timer.setSingleShot(true);
_timer.setInterval(0);
connect(&_timer, &QTimer::timeout, this, &KrPreviewJob::slotStartJob);
}
KrPreviewJob::~KrPreviewJob()
{
doKill();
}
void KrPreviewJob::scheduleItem(KrViewItem *item)
{
if (!_scheduled.contains(item)) {
_scheduled.append(item);
setTotalAmount(KJob::Files, totalAmount(KJob::Files) + 1);
}
if (!_job)
_timer.start();
}
void KrPreviewJob::removeItem(KrViewItem *item)
{
setTotalAmount(KJob::Files, totalAmount(KJob::Files) - _scheduled.removeAll(item));
if (_job) {
doKill();
if (!_scheduled.isEmpty())
_timer.start();
}
if (_scheduled.isEmpty())
emitResult();
}
void KrPreviewJob::slotFailed(const KFileItem &item)
{
slotGotPreview(item, QPixmap());
}
void KrPreviewJob::slotGotPreview(const KFileItem &item, const QPixmap &preview)
{
KrViewItem *vi = _hash[item];
ASSERT(vi);
_scheduled.removeOne(vi);
const FileItem *file = vi->getFileItem();
_parent->addPreview(file, preview);
vi->redraw();
setProcessedAmount(KJob::Files, processedAmount(KJob::Files) + 1);
emitPercent(processedAmount(KJob::Files), totalAmount(KJob::Files));
}
void KrPreviewJob::slotStartJob()
{
ASSERT(_job == nullptr);
ASSERT(!_scheduled.isEmpty());
_hash.clear();
sort();
int size = _parent->_view->fileIconSize();
KFileItemList list;
for (int i = 0; i < _scheduled.count() && i < MAX_CHUNK_SIZE; i++) {
KFileItem fi(_scheduled[i]->getFileItem()->getUrl(), nullptr, 0);
list.append(fi);
_hash.insert(fi, _scheduled[i]);
}
QStringList allPlugins = KIO::PreviewJob::availablePlugins();
_job = new KIO::PreviewJob(list, QSize(size, size), &allPlugins);
_job->setScaleType(KIO::PreviewJob::ScaledAndCached);
connect(_job, &KIO::PreviewJob::gotPreview, this, &KrPreviewJob::slotGotPreview);
connect(_job, &KIO::PreviewJob::failed, this, &KrPreviewJob::slotFailed);
connect(_job, &KIO::PreviewJob::result, this, &KrPreviewJob::slotJobResult);
}
void KrPreviewJob::slotJobResult(KJob *job)
{
(void)job;
if (!disconnect(_job, nullptr, this, nullptr))
abort();
_job = nullptr;
_hash.clear();
if (_scheduled.isEmpty())
emitResult();
else
_timer.start();
}
// move currently visible items to beginning of the list
void KrPreviewJob::sort()
{
for (int i = 0, visible_end = 0; i < _scheduled.count(); i++) {
KrViewItem *item = _scheduled[i];
if (_parent->_view->widget()->rect().intersects(item->itemRect())) {
if (i != visible_end)
_scheduled.move(i, visible_end);
visible_end++;
}
}
}
bool KrPreviewJob::doKill()
{
_timer.stop();
if (_job) {
if (!disconnect(_job, nullptr, this, nullptr))
abort();
if (!_job->kill())
abort();
_job = nullptr;
}
_hash.clear();
return true;
}
|