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#include <QRegion>
13#include <KSharedConfig>
14#include <KConfigGroup>
15
20
26
28 {
29 }
30
31 void getTail()
32 {
33 auto config = KSharedConfig ::openConfig(directory.filePath(RecorderConst::recorderMetaDataName));
34 KConfigGroup cfg = KConfigGroup(config, "");
35
36 tails = cfg.readEntry("tails", QStringList());
37 }
38
39 void saveTail()
40 {
41 auto config = KSharedConfig ::openConfig(directory.filePath(RecorderConst::recorderMetaDataName));
42 KConfigGroup cfg = KConfigGroup(config, "");
43
44 cfg.writeEntry("tails", tails);
45
46 tails.clear();
47 }
48};
49
51 : d(new Private(directory, format))
52{
53}
54
58
60{
61 d->directory = directory;
62 d->format = format;
63}
64
65// Double check that the files are sequential and have no holes, if there are any, fill them in
67{
68 if (!d->directory.exists()) {
69 return;
70 }
71
72 d->getTail();
73
74 QStringList fileNames = d->directory.entryList({"*." % RecorderFormatInfo::fileExtension(d->format)});
75
76 QRegularExpression fileRegex = RecorderConst::snapshotFilePatternFor(RecorderFormatInfo::fileExtension(d->format));
77 QHash<int, QString> files;
78 QVector<int> indecies;
79
80 Q_FOREACH (QString file, fileNames) {
81 QRegularExpressionMatch match = fileRegex.match(file);
82
83 if (match.hasMatch()) {
84 int index = match.captured(1).toInt();
85 files.insert(index, file);
86 indecies.push_back(index);
87 }
88 }
89
90 // It should be sorted, but just in case
91 std::sort(indecies.begin(), indecies.end());
92
93 QHash<int, int> holes;
94 int prevIndex = 0;
95
96 for (auto it = indecies.begin(); it != indecies.end(); ++it) {
97 int curIndex = *it;
98 int diff = curIndex - prevIndex;
99
100 if (diff > 1) {
101 holes.insert(curIndex, diff - 1);
102 }
103
104 prevIndex = curIndex;
105 }
106
107 // No holes, so we don't need to do anything
108 if (holes.isEmpty()) {
109 return;
110 }
111
112 int offset = 0;
113 QStringList new_tails = d->tails;
114
115 for (auto it = indecies.begin(); it != indecies.end(); ++it) {
116 int ind = *it;
117
118 if (holes.contains(ind)) {
119 offset += holes[ind];
120 }
121
122 if (offset == 0) {
123 continue;
124 }
125
126 QString oldName = files[ind];
127 QString newName = QString("%2.%3").arg(QString("%1").arg(ind - offset, 7, 10, QLatin1Char('0')),
129
130 int tailIndex = d->tails.indexOf(oldName);
131 if (tailIndex != -1) {
132 new_tails[tailIndex] = newName;
133 }
134
135 // Hopefully nothing should go wrong
136 d->directory.rename(oldName, newName);
137 }
138
139 if (d->tails != new_tails) {
140 d->tails = new_tails;
141 d->saveTail();
142 }
143}
QList< QString > QStringList
RecorderExportPreprocessor(QDir directory, RecorderFormat format)
void updateSettings(QDir directory, RecorderFormat format)
void doPreprocessing()
Checks if there are no holes in the frames, and shifts the files to fill them.
const QString recorderMetaDataName
QRegularExpression snapshotFilePatternFor(const QString &extension)
QLatin1String fileExtension(RecorderFormat format)
RecorderFormat
Private(QDir directory, RecorderFormat format)