Krita Source Code Documentation
Loading...
Searching...
No Matches
recorder_export_preprocessor.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Luna Lovecraft <ciubix8514@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8#include "recorder_const.h"
9#include "recorder_format.h"
10#include <QRegularExpression>
11#include <algorithm>
12
27
29 : d(new Private(directory, format))
30{
31}
32
36
38{
39 d->directory = directory;
40 d->format = format;
41}
42
43// Double check that the files are sequential and have no holes, if there are any, fill them in
45{
46 if (!d->directory.exists()) {
47 return;
48 }
49
50 QStringList fileNames = d->directory.entryList({"*." % RecorderFormatInfo::fileExtension(d->format)});
51
52 QRegularExpression fileRegex = RecorderConst::snapshotFilePatternFor(RecorderFormatInfo::fileExtension(d->format));
53 QHash<int, QString> files;
54 QVector<int> indecies;
55
56 Q_FOREACH (QString file, fileNames) {
57 QRegularExpressionMatch match = fileRegex.match(file);
58
59 if (match.hasMatch()) {
60 int index = match.captured(1).toInt();
61 files.insert(index, file);
62 indecies.push_back(index);
63 }
64 }
65
66 // It should be sorted, but just in case
67 std::sort(indecies.begin(), indecies.end());
68
69 QHash<int, int> holes;
70 int prevIndex = 0;
71
72 for (auto it = indecies.begin(); it != indecies.end(); ++it) {
73 int curIndex = *it;
74 int diff = curIndex - prevIndex;
75
76 if (diff > 1) {
77 holes.insert(curIndex, diff - 1);
78 }
79
80 prevIndex = curIndex;
81 }
82
83 // No holes, so we don't need to do anything
84 if (holes.isEmpty()) {
85 return;
86 }
87
88 int offset = 0;
89 for (auto it = indecies.begin(); it != indecies.end(); ++it) {
90 int ind = *it;
91
92 if (holes.contains(ind)) {
93 offset += holes[ind];
94 }
95
96 if (offset == 0) {
97 continue;
98 }
99
100 QString oldName = files[ind];
101 QString newName = QString("%2.%3").arg(QString("%1").arg(ind - offset, 7, 10, QLatin1Char('0')),
103
104 // Hopefully nothing should go wrong
105 d->directory.rename(oldName, newName);
106 }
107}
RecorderExportPreprocessor(QDir directory, RecorderFormat format)
void updateSettings(QDir directory, RecorderFormat format)
QRegularExpression snapshotFilePatternFor(const QString &extension)
QLatin1String fileExtension(RecorderFormat format)
RecorderFormat
Private(QDir directory, RecorderFormat format)