Krita Source Code Documentation
Loading...
Searching...
No Matches
KisAnimAutoKey.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022 Emmet O'Neill <emmetoneill.pdx@gmail.com>
3 * SPDX-FileCopyrightText: 2022 Eoin O'Neill <eoinoneill1991@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "KisAnimAutoKey.h"
9
10#include <QReadWriteLock>
11
12#include <QApplication>
13#include <QThread>
14
15#include "kis_image_config.h"
17
18#include "kis_transaction.h"
20
21
22namespace KisAutoKey
23{
24
25class AutoKeyFrameStateHolder : public QObject
26{
27 Q_OBJECT
28public:
29
31 KIS_SAFE_ASSERT_RECOVER_NOOP(qApp->thread() == QThread::currentThread());
32
33 connect(KisImageConfigNotifier::instance(), SIGNAL(autoKeyFrameConfigurationChanged()),
34 this, SLOT(slotAutoKeyFrameSettingChanged()));
36 }
37
38 Mode mode() const {
39 QReadLocker l(&m_lock);
40 return m_mode;
41 }
42
44 QWriteLocker l(&m_lock);
45 m_mode = mode;
46 }
47
48private Q_SLOTS:
50 QWriteLocker l(&m_lock);
51
52 KisImageConfig cfg(true);
53
54 if (cfg.autoKeyEnabled()) {
57 } else {
59 }
60 }
61
62private:
63 mutable QReadWriteLock m_lock;
65};
66
67QScopedPointer<AutoKeyFrameStateHolder> s_holder;
68void initHolder () {
69 if (!s_holder) {
71 }
72}
73Q_COREAPP_STARTUP_FUNCTION(initHolder)
74
76{
77 return s_holder->mode();
78}
79
80KUndo2Command* tryAutoCreateDuplicatedFrame(KisPaintDeviceSP device, AutoCreateKeyframeFlags flags) {
81 KUndo2Command* undo = nullptr;
83
84 const bool isLodNMode = device->defaultBounds()->currentLevelOfDetail() > 0;
85
86 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!isLodNMode || flags & SupportsLod, undo);
87
88 const KisAutoKey::Mode autoKeyMode = KisAutoKey::activeMode();
89 if (autoKeyMode == KisAutoKey::NONE) return undo;
90
91 KisRasterKeyframeChannel *channel = device->keyframeChannel();
92 if (!channel) return undo;
93
94 const int activeKeyframe = channel->activeKeyframeTime();
95 const int targetKeyframe = device->defaultBounds()->currentTime();
96
97 if (activeKeyframe == targetKeyframe) return undo;
98
99 if (isLodNMode) {
100 if (flags & AllowBlankMode && autoKeyMode == KisAutoKey::BLANK) {
101 const QRect originalDirtyRect = device->exactBounds();
102 KisTransaction transaction(device);
103 device->clear();
104 device->setDirty(originalDirtyRect);
105 undo = transaction.endAndTake();
106 }
107 // for KisAutoKey::DUPLICATE mode we do nothing and just reuse
108 // the LoDN device
109 } else {
110 undo = new KUndo2Command;
111
112 if (flags & AllowBlankMode && autoKeyMode == KisAutoKey::BLANK) {
113 channel->addKeyframe(targetKeyframe, undo);
114
117
118 KisKeyframeSP newKeyframe = channel->keyframeAt(targetKeyframe);
119 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(newKeyframe, undo);
120
121 KisKeyframeSP oldKeyframe = channel->keyframeAt(activeKeyframe);
122 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(oldKeyframe, undo);
123
124 newKeyframe->setColorLabel(oldKeyframe->colorLabel());
125 } else {
126 channel->copyKeyframe(activeKeyframe, targetKeyframe, undo);
127 }
128 }
129
130 return undo;
131}
132
134{
135 s_holder->testingSetActiveMode(mode);
136}
137
138
139} // namespace KisAutoKey
140
141#include "KisAnimAutoKey.moc"
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
virtual int currentLevelOfDetail() const =0
virtual int currentTime() const =0
static KisImageConfigNotifier * instance()
bool autoKeyEnabled(bool requestDefault=false) const
bool autoKeyModeDuplicate(bool requestDefault=false) const
KisKeyframeSP keyframeAt(int time) const
Get a keyframe at specified time. Used primarily when the value of a given keyframe is needed.
static void copyKeyframe(const KisKeyframeChannel *sourceChannel, int sourceTime, KisKeyframeChannel *targetChannel, int targetTime, KUndo2Command *parentUndoCmd=nullptr)
Copy a keyframe across channel(s) at the specified times.
void addKeyframe(int time, KUndo2Command *parentUndoCmd=nullptr)
Add a new keyframe to the channel at the specified time.
int activeKeyframeTime(int time) const
Get the time of the active keyframe. Useful for snapping any time to that of the most recent keyframe...
KisRasterKeyframeChannel * keyframeChannel() const
void setDirty(const QRect &rc)
virtual void clear()
QRect exactBounds() const
KisDefaultBoundsBaseSP defaultBounds() const
The KisRasterKeyframeChannel is a concrete KisKeyframeChannel subclass that stores and manages KisRas...
KUndo2Command * endAndTake()
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
QScopedPointer< AutoKeyFrameStateHolder > s_holder
void testingSetActiveMode(Mode mode)
KUndo2Command * tryAutoCreateDuplicatedFrame(KisPaintDeviceSP device, AutoCreateKeyframeFlags flags)
create a new duplicated keyframe if auto-keyframe mode is on