Krita Source Code Documentation
Loading...
Searching...
No Matches
KisAnimationImporter Class Reference

#include <kis_animation_importer.h>

+ Inheritance diagram for KisAnimationImporter:

Classes

struct  Private
 

Public Member Functions

KisImportExportErrorCode import (QStringList files, int firstFrame, int step, bool autoAddHoldframes=false, bool startfrom0=false, int isAscending=0, bool assignDocumentProfile=false, QList< int > optionalKeyframeTimeList={})
 
 KisAnimationImporter (KisDocument *document)
 
 KisAnimationImporter (KisImageSP image, KoUpdaterPtr updater=0)
 
 ~KisAnimationImporter () override
 

Private Slots

void cancel ()
 

Private Member Functions

QPair< KisPaintLayerSP, class KisRasterKeyframeChannel * > initializePaintLayer (QScopedPointer< KisDocument > &doc, class KisUndoAdapter *undoAdapter)
 

Private Attributes

QScopedPointer< Privatem_d
 

Detailed Description

Definition at line 19 of file kis_animation_importer.h.

Constructor & Destructor Documentation

◆ KisAnimationImporter() [1/2]

KisAnimationImporter::KisAnimationImporter ( KisImageSP image,
KoUpdaterPtr updater = 0 )

Definition at line 34 of file kis_animation_importer.cpp.

35 : m_d(new Private())
36{
37 m_d->document = 0;
38 m_d->image = image;
39 m_d->stop = false;
40 m_d->updater = updater;
41}
QScopedPointer< Private > m_d

References m_d.

◆ KisAnimationImporter() [2/2]

KisAnimationImporter::KisAnimationImporter ( KisDocument * document)

Definition at line 43 of file kis_animation_importer.cpp.

44 : m_d(new Private())
45{
46 m_d->document= document;
47 m_d->image = document->image();
48 m_d->stop = false;
49}

References m_d.

◆ ~KisAnimationImporter()

KisAnimationImporter::~KisAnimationImporter ( )
override

Definition at line 51 of file kis_animation_importer.cpp.

52{}

Member Function Documentation

◆ cancel

void KisAnimationImporter::cancel ( )
privateslot

Definition at line 216 of file kis_animation_importer.cpp.

217{
218 m_d->stop = true;
219}

References m_d.

◆ import()

KisImportExportErrorCode KisAnimationImporter::import ( QStringList files,
int firstFrame,
int step,
bool autoAddHoldframes = false,
bool startfrom0 = false,
int isAscending = 0,
bool assignDocumentProfile = false,
QList< int > optionalKeyframeTimeList = {} )

Definition at line 54 of file kis_animation_importer.cpp.

55{
56 //TODO: We should clean up this code --
57 // There are a lot of actions here that we should break into individual methods
58 // so that we can better control code flow, and I'd prefer to use multiple import
59 // calls to better handle all of these different options!
60 // Additionally, we might prefer to use flags for multiple booleans to improve
61 // legibility of calls.
62 Q_ASSERT(step > 0);
63
64 KisUndoAdapter *undo = m_d->image->undoAdapter();
65 undo->beginMacro(kundo2_i18n("Import animation"));
66
67 QScopedPointer<KisDocument> importDoc(KisPart::instance()->createDocument());
68 importDoc->setFileBatchMode(true);
69
70 const bool usingPredefinedTimes = !optionalKeyframeTimeList.isEmpty() && !autoAddHoldframes;
71 QQueue<int> predefinedFrameQueue;
72 predefinedFrameQueue.append(optionalKeyframeTimeList);
73
75 int frame = usingPredefinedTimes ? predefinedFrameQueue.dequeue() : firstFrame;
76 int filesProcessed = 0;
77
78 if (usingPredefinedTimes) {
79 KIS_ASSERT(files.count() == optionalKeyframeTimeList.count());
80 }
81
82 if (m_d->updater) {
83 m_d->updater->setRange(0, files.size());
84 }
85
86 QPair<KisPaintLayerSP, KisRasterKeyframeChannel*> layerRasterChannelPair;
87
88 const QRegExp rx(QLatin1String("(\\d+)")); //regex for extracting numbers
89 QStringList fileNumberRxList;
90
91 int pos = 0;
92
93 while ((pos = rx.indexIn(files.at(0), pos)) != -1) {
94 fileNumberRxList << rx.cap(1);
95 pos += rx.matchedLength();
96 }
97
98 int firstFrameNumber = 0;
99 bool ok;
100
101 if (!fileNumberRxList.isEmpty()) {
102 fileNumberRxList.last().toInt(&ok); // selects the last number of file name of the first frame (useful for descending order)
103 // Note to self -- ^^ uh.... This isn't doing anything?? Shouldn't this assign `firstFrameNumber`?
104 }
105
106 if (firstFrameNumber == 0){
107 startfrom0 = false; // if enabled, the zeroth frame will be places in -1 slot, leading to an error
108 }
109
110 fileNumberRxList.clear();
111 const int offset = (startfrom0 ? 1 : 0); //offset added to consider file numbering starts from 1 instead of 0
112 int autoframe = 0;
113
114 KisConfig cfg(true);
115
116 Q_FOREACH(QString file, files) {
117 bool successfullyLoaded = importDoc->openPath(file, KisDocument::DontAddToRecent);
119
120 if ( (!usingPredefinedTimes && frame == firstFrame)
121 || (usingPredefinedTimes && frame == optionalKeyframeTimeList.first()) ) {
122 layerRasterChannelPair = initializePaintLayer(importDoc, undo);
123 }
124
125 if (m_d->updater) {
126 if (m_d->updater->interrupted()) {
127 m_d->stop = true;
128 } else {
129 m_d->updater->setValue(filesProcessed + 1);
130
131 // the updater doesn't call that automatically,
132 // it is "threaded" by default
133 qApp->processEvents();
134 }
135 }
136
137 if (m_d->stop) {
139 break;
140 }
141
142 if (cfg.trimFramesImport()) {
143 importDoc->image()->projection()->crop(m_d->image->bounds());
144 }
145 importDoc->image()->projection()->purgeDefaultPixels();
146
148
149 if (!autoAddHoldframes) {
150 layerRasterChannelPair.second->importFrame(frame, importDoc->image()->projection(), NULL); // as first frame added will go to second slot i.e #1 instead of #0
151 } else {
152 pos = 0;
153
154 while ((pos = rx.indexIn(file, pos)) != -1) {
155 fileNumberRxList << rx.cap(1);
156 pos += rx.matchedLength();
157 }
158
159 int filenum = fileNumberRxList.last().toInt(&ok);
160
161 if (isAscending == 0) {
162 autoframe = firstFrame + filenum - offset;
163 } else {
164 autoframe = firstFrame + (firstFrameNumber - filenum); //places the first frame #0 (or #1) slot, and later frames are added as per the difference
165 }
166
167 if (ok) {
168 layerRasterChannelPair.second->importFrame(autoframe , importDoc->image()->projection(), NULL);
169 } else {
170 // if it fails to extract a number, the next frame will simply be added to next slot
171 layerRasterChannelPair.second->importFrame(autoframe + 1, importDoc->image()->projection(), NULL);
172 }
173 fileNumberRxList.clear();
174 }
175
176 if (usingPredefinedTimes && predefinedFrameQueue.count()) {
177 frame = predefinedFrameQueue.dequeue();
178 } else {
179 frame += step;
180 }
181
182 filesProcessed++;
183 }
184
185 if (layerRasterChannelPair.first && assignDocumentProfile) {
186
187 if (layerRasterChannelPair.first->colorSpace()->colorModelId() == m_d->image->colorSpace()->colorModelId()) {
188
189 const KoColorSpace *srcColorSpace = layerRasterChannelPair.first->colorSpace();
191 srcColorSpace->colorModelId().id()
192 , srcColorSpace->colorDepthId().id()
193 , m_d->image->colorSpace()->profile());
194
195 KisAssignProfileProcessingVisitor *visitor = new KisAssignProfileProcessingVisitor(srcColorSpace, dstColorSpace);
196 visitor->visit(layerRasterChannelPair.first.data(), undo);
197 }
198 }
199
200 undo->endMacro();
201
202 return status;
203}
QPair< KisPaintLayerSP, class KisRasterKeyframeChannel * > initializePaintLayer(QScopedPointer< KisDocument > &doc, class KisUndoAdapter *undoAdapter)
void visit(KisTransformMask *mask, KisUndoAdapter *undoAdapter) override
static KisPart * instance()
Definition KisPart.cpp:131
virtual KoID colorModelId() const =0
virtual KoID colorDepthId() const =0
QString id() const
Definition KoID.cpp:63
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
KisDocument * createDocument(QList< KisNodeSP > nodes, KisImageSP srcImage, const QRect &copiedBounds)
KUndo2MagicString kundo2_i18n(const char *text)
QAction * undo(const QObject *recvr, const char *slot, QObject *parent)
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()

References ImportExportCodes::Cancelled, KoColorSpace::colorDepthId(), KoColorSpace::colorModelId(), KoColorSpaceRegistry::colorSpace(), createDocument(), KisDocument::DontAddToRecent, KoID::id(), initializePaintLayer(), KoColorSpaceRegistry::instance(), KisPart::instance(), ImportExportCodes::InternalError, KIS_ASSERT, KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE, kundo2_i18n(), m_d, ImportExportCodes::OK, KisConfig::trimFramesImport(), and KisAssignProfileProcessingVisitor::visit().

◆ initializePaintLayer()

QPair< KisPaintLayerSP, KisRasterKeyframeChannel * > KisAnimationImporter::initializePaintLayer ( QScopedPointer< KisDocument > & doc,
class KisUndoAdapter * undoAdapter )
private

Definition at line 205 of file kis_animation_importer.cpp.

206{
207 const KoColorSpace *cs = doc->image()->colorSpace();
208 KisPaintLayerSP paintLayer = new KisPaintLayer(m_d->image, m_d->image->nextLayerName(), OPACITY_OPAQUE_U8, cs);
209 undoAdapter->addCommand(new KisImageLayerAddCommand(m_d->image, paintLayer, m_d->image->rootLayer(), m_d->image->rootLayer()->childCount()));
210
211 paintLayer->enableAnimation();
212 KisRasterKeyframeChannel* contentChannel = qobject_cast<KisRasterKeyframeChannel*>(paintLayer->getKeyframeChannel(KisKeyframeChannel::Raster.id(), true));
213 return QPair<KisPaintLayerSP, KisRasterKeyframeChannel*>(paintLayer, contentChannel);
214}
const quint8 OPACITY_OPAQUE_U8
The command for adding a layer.
static const KoID Raster
The KisRasterKeyframeChannel is a concrete KisKeyframeChannel subclass that stores and manages KisRas...
KisKeyframeChannel * getKeyframeChannel(const QString &id, bool create)
void enableAnimation()

References KisUndoAdapter::addCommand(), KisNode::childCount(), KisBaseNode::enableAnimation(), KisBaseNode::getKeyframeChannel(), KoID::id(), KisBaseNode::image, m_d, OPACITY_OPAQUE_U8, KisKeyframeChannel::Raster, and KisImage::rootLayer().

Member Data Documentation

◆ m_d

QScopedPointer<Private> KisAnimationImporter::m_d
private

Definition at line 45 of file kis_animation_importer.h.


The documentation for this class was generated from the following files: