1#include "layeropacitydialog.h"
2#include "ui_layeropacitydialog.h"
4#include "layermanager.h"
5#include "playbackmanager.h"
7#include "layerbitmap.h"
8#include "bitmapimage.h"
9#include "layervector.h"
10#include "vectorimage.h"
13LayerOpacityDialog::LayerOpacityDialog(
QWidget *parent) :
20LayerOpacityDialog::~LayerOpacityDialog()
25void LayerOpacityDialog::setCore(
Editor *editor)
28 mLayerManager = mEditor->layers();
29 mPlayBack = mEditor->playback();
32void LayerOpacityDialog::initUI()
37 connect(ui->chooseOpacitySpinBox, spinboxValueChanged,
this, &LayerOpacityDialog::opacitySpinboxChanged);
47 connect(mEditor, &Editor::objectLoaded,
this, &LayerOpacityDialog::onObjectLoaded);
49 connect(mPlayBack, &PlaybackManager::playStateChanged,
this, &LayerOpacityDialog::onPlayStateChanged);
50 connect(mLayerManager, &LayerManager::currentLayerChanged,
this, &LayerOpacityDialog::onCurrentLayerChanged);
51 connect(mEditor, &Editor::selectedFramesChanged,
this, &LayerOpacityDialog::onSelectedFramesChanged);
56void LayerOpacityDialog::updateUI()
58 Layer* currentLayer = mLayerManager->currentLayer();
59 if (currentLayer ==
nullptr) {
return; }
61 ui->labLayerInfo->setText(
tr(
"Layer: %1").arg(currentLayer->name()));
62 if (currentLayer->type() != Layer::BITMAP && currentLayer->type() != Layer::VECTOR) {
63 setCanAdjust(
false,
false);
67 bool canAdjust =
false;
68 if (ui->rbActiveKeyframe->isChecked()) {
69 KeyFrame* keyframe = currentLayer->getLastKeyFrameAtPosition(mEditor->currentFrame());
70 canAdjust = keyframe !=
nullptr;
71 }
else if (ui->rbSelectedKeyframes->isChecked()) {
72 canAdjust = !currentLayer->getSelectedFramesByPos().
isEmpty();
73 }
else if (ui->rbActiveLayer->isChecked()) {
77 updateSelectedFramesUI();
79 ui->chooseOpacitySlider->setEnabled(canAdjust);
80 ui->chooseOpacitySpinBox->setEnabled(canAdjust);
83void LayerOpacityDialog::onObjectLoaded()
85 Layer* currentLayer = mLayerManager->currentLayer();
86 if (currentLayer ==
nullptr) {
return; }
88 if (currentLayer->type() != Layer::BITMAP && currentLayer->type() != Layer::VECTOR) {
return; }
90 KeyFrame* keyframe = currentLayer->getLastKeyFrameAtPosition(mEditor->currentFrame());
93 updateValues(getOpacityForKeyFrame(currentLayer, keyframe));
101qreal LayerOpacityDialog::getOpacityForKeyFrame(
Layer* layer,
const KeyFrame* keyframe)
const
103 if (layer->type() == Layer::BITMAP) {
105 return bitmap->getOpacity();
106 }
else if (layer->type() == Layer::VECTOR) {
108 return vector->getOpacity();
114void LayerOpacityDialog::setOpacityForKeyFrame(
Layer* layer,
KeyFrame* keyframe, qreal opacity)
116 if (layer->type() == Layer::BITMAP) {
118 bitmap->setOpacity(opacity);
120 }
else if (layer->type() == Layer::VECTOR) {
122 vector->setOpacity(opacity);
127void LayerOpacityDialog::opacitySliderChanged(
int value)
129 ui->chooseOpacitySpinBox->setValue(value * mSpinBoxMultiplier);
130 opacityValueChanged();
133void LayerOpacityDialog::opacitySpinboxChanged(
double value)
135 ui->chooseOpacitySlider->setValue(
static_cast<int>(value * 5.0));
136 opacityValueChanged();
139void LayerOpacityDialog::fade(OpacityFadeType fadeType)
144 Layer* currentLayer = mLayerManager->currentLayer();
145 if (currentLayer ==
nullptr) {
return; }
147 if (currentLayer->type() != Layer::BITMAP && currentLayer->type() != Layer::VECTOR) {
return; }
149 QList<int> selectedKeys = currentLayer->getSelectedFramesByPos();
151 if (selectedKeys.
count() < mMinSelectedFrames) {
return; }
153 int fadeFromPos = selectedKeys.
first();
154 KeyFrame* keyframe = currentLayer->getLastKeyFrameAtPosition(fadeFromPos);
155 if (keyframe ==
nullptr) {
return; }
157 qreal initialOpacity = getOpacityForKeyFrame(currentLayer, keyframe);
159 qreal imageCount =
static_cast<qreal
>(selectedKeys.
count() - 1);
161 qreal opacityStepper = 0.0;
163 case OpacityFadeType::IN:
166 if (initialOpacity >= 1.0) {
167 initialOpacity = 0.0;
169 opacityStepper = (1.0 - initialOpacity) / imageCount;
172 case OpacityFadeType::OUT:
175 if (initialOpacity <= 0) {
176 initialOpacity = 1.0;
178 opacityStepper = initialOpacity / imageCount;
183 for (
int i = 0; i < selectedKeys.
count(); i++) {
184 keyframe = currentLayer->getLastKeyFrameAtPosition(selectedKeys.
at(i));
185 if (keyframe ==
nullptr) {
continue; }
187 qreal newOpacity = 0;
190 case OpacityFadeType::IN: {
191 newOpacity = initialOpacity + (i * opacityStepper);
194 case OpacityFadeType::OUT: {
195 newOpacity = initialOpacity - (i * opacityStepper);
199 setOpacityForKeyFrame(currentLayer, keyframe, newOpacity);
202 keyframe = currentLayer->getLastKeyFrameAtPosition(mEditor->currentFrame());
205 qreal imageOpacity = getOpacityForKeyFrame(currentLayer, keyframe);
206 updateValues(imageOpacity);
212void LayerOpacityDialog::fadeInPressed()
214 fade(OpacityFadeType::IN);
217void LayerOpacityDialog::fadeOutPressed()
219 fade(OpacityFadeType::OUT);
222void LayerOpacityDialog::onCurrentLayerChanged(
int)
224 onCurrentFrameChanged(mEditor->currentFrame());
228void LayerOpacityDialog::onCurrentFrameChanged(
int frame)
230 if (mPlayerIsPlaying) {
return; }
232 Layer* currentLayer = mLayerManager->currentLayer();
233 if (currentLayer ==
nullptr) {
return; }
235 if (currentLayer->type() != Layer::BITMAP && currentLayer->type() != Layer::VECTOR) {
236 setCanAdjust(
false,
false);
240 KeyFrame* keyframe = currentLayer->getLastKeyFrameAtPosition(frame);
242 updateValues(getOpacityForKeyFrame(currentLayer, keyframe));
247void LayerOpacityDialog::onSelectedFramesChanged()
252void LayerOpacityDialog::updateSelectedFramesUI()
254 Layer* currentLayer = mLayerManager->currentLayer();
255 if (currentLayer ==
nullptr) {
return; }
257 QList<int> frames = currentLayer->getSelectedFramesByPos();
259 int minSelectedFrames = frames.
count() >= mMinSelectedFrames;
260 ui->groupBoxFade->setEnabled(minSelectedFrames);
261 ui->rbSelectedKeyframes->setEnabled(minSelectedFrames);
264void LayerOpacityDialog::onPlayStateChanged(
bool isPlaying)
266 mPlayerIsPlaying = isPlaying;
268 if (!mPlayerIsPlaying) {
269 onCurrentFrameChanged(mEditor->currentFrame());
274void LayerOpacityDialog::updateValues(qreal opacity)
279 int newOpacity =
static_cast<int>(opacity * mMultiplier);
280 ui->chooseOpacitySlider->setValue(newOpacity);
281 ui->chooseOpacitySpinBox->setValue(newOpacity * mSpinBoxMultiplier);
284void LayerOpacityDialog::opacityValueChanged()
286 if (ui->rbActiveKeyframe->isChecked()) {
287 setOpacityForCurrentKeyframe();
288 }
else if (ui->rbActiveLayer->isChecked()) {
289 setOpacityForLayer();
290 }
else if (ui->rbSelectedKeyframes->isChecked()) {
291 setOpacityForSelectedKeyframes();
295void LayerOpacityDialog::setOpacityForCurrentKeyframe()
297 Layer* currentLayer = mLayerManager->currentLayer();
298 if (currentLayer ==
nullptr) {
return; }
300 if (currentLayer->type() != Layer::BITMAP && currentLayer->type() != Layer::VECTOR) {
return; }
302 KeyFrame* keyframe = currentLayer->getLastKeyFrameAtPosition(mEditor->currentFrame());
303 if (keyframe ==
nullptr) {
return; }
305 qreal opacity = ui->chooseOpacitySlider->value() / mMultiplier;
306 setOpacityForKeyFrame(currentLayer, keyframe, opacity);
311void LayerOpacityDialog::setOpacityForSelectedKeyframes()
313 Layer* currentLayer = mLayerManager->currentLayer();
314 if (currentLayer ==
nullptr) {
return; }
316 QList<int> frames = currentLayer->getSelectedFramesByPos();
318 if (frames.
isEmpty()) {
return; }
320 qreal opacity =
static_cast<qreal
>(ui->chooseOpacitySlider->value()) / mMultiplier;
322 for (
int pos : frames)
324 KeyFrame* keyframe = currentLayer->getKeyFrameAt(
pos);
327 setOpacityForKeyFrame(currentLayer, keyframe, opacity);
333void LayerOpacityDialog::setOpacityForLayer()
335 Layer* currentLayer = mLayerManager->currentLayer();
336 if (currentLayer ==
nullptr) {
return; }
338 qreal opacity =
static_cast<qreal
>(ui->chooseOpacitySlider->value()) / mMultiplier;
340 currentLayer->foreachKeyFrame([
this, currentLayer, opacity](
KeyFrame* keyframe) {
343 setOpacityForKeyFrame(currentLayer, keyframe, opacity);
349void LayerOpacityDialog::setCanAdjust(
bool opacity,
bool fade)
351 ui->groupBoxFade->setEnabled(fade);
352 ui->chooseOpacitySlider->setEnabled(opacity);
353 ui->chooseOpacitySpinBox->setEnabled(opacity);
void framesModified()
This should be emitted after modifying multiple frames.
void scrubbed(int frameNumber)
This should be emitted after scrubbing.
void markFrameAsDirty(const int frameNumber)
Mark the frame position as dirty.
void valueChanged(int value)
void finished(int result)
void valueChanged(double d)
const T & at(int i) const const
int count(const T &value) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString tr(const char *sourceText, const char *disambiguation, int n)