Skip to content

Commit

Permalink
Fix vector layer color import (#1798)
Browse files Browse the repository at this point in the history
* Get rid of Object pointers in layer and vector classes

* Import colors of imported vector layers

* Fix tests
  • Loading branch information
J5lx authored Nov 25, 2023
1 parent a7ab2a6 commit 18c5494
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 211 deletions.
51 changes: 33 additions & 18 deletions app/src/importlayersdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ GNU General Public License for more details.
#include "soundmanager.h"
#include "layer.h"
#include "layersound.h"
#include "layervector.h"
#include "soundclip.h"


Expand Down Expand Up @@ -83,29 +84,43 @@ void ImportLayersDialog::importLayers()
int currentFrame = mEditor->currentFrame();
Q_ASSERT(ui->lwLayers->count() == mImportObject->getLayerCount());

for (int i = 0; i < ui->lwLayers->count(); i++ )
QMap<int, int> importedColors;

for (const QListWidgetItem* item : ui->lwLayers->selectedItems())
{
QListWidgetItem* item = ui->lwLayers->item(i);
if (item->isSelected())
{
int layerId = item->data(Qt::UserRole).toInt();
mImportLayer = mImportObject->takeLayer(item->data(Qt::UserRole).toInt());
mImportLayer->setName(mEditor->layers()->nameSuggestLayer(item->text()));
loadKeyFrames(mImportLayer); // all keyframes of this layer must be in memory

mImportLayer = mImportObject->takeLayer(layerId);
mImportLayer->setName(mEditor->layers()->nameSuggestLayer(item->text()));
loadKeyFrames(mImportLayer); // all keyframes of this layer must be in memory
object->addLayer(mImportLayer);

object->addLayer(mImportLayer);
if (mImportLayer->type() == Layer::VECTOR)
{
LayerVector* layerVector = static_cast<LayerVector*>(mImportLayer);
for (int i = 0; i < mImportObject->getColorCount(); i++) {
if (!layerVector->usesColor(i)) {
continue;
}

if (!importedColors.contains(i)) {
const ColorRef color = mImportObject->getColor(i);
object->addColor(color);
importedColors[i] = object->getColorCount() - 1;
}

layerVector->moveColor(i, importedColors[i]);
}
}

if (mImportLayer->type() == Layer::SOUND)
if (mImportLayer->type() == Layer::SOUND)
{
LayerSound* layerSound = static_cast<LayerSound*>(mImportLayer);
layerSound->foreachKeyFrame([this](KeyFrame* key)
{
LayerSound* layerSound = static_cast<LayerSound*>(mImportLayer);
layerSound->foreachKeyFrame([this](KeyFrame* key)
{
SoundClip* clip = dynamic_cast<SoundClip*>(key);
Status st = mEditor->sound()->loadSound(clip, clip->fileName());
Q_ASSERT(st.ok());
});
}
SoundClip* clip = dynamic_cast<SoundClip*>(key);
Status st = mEditor->sound()->loadSound(clip, clip->fileName());
Q_ASSERT(st.ok());
});
}
}
mEditor->object()->modification();
Expand Down
4 changes: 2 additions & 2 deletions core_lib/src/canvaspainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void CanvasPainter::paintVectorOnionSkinFrame(QPainter& painter, const QRect& bl
QPainter onionSkinPainter;
initializePainter(onionSkinPainter, mOnionSkinPixmap, blitRect);

vectorImage->paintImage(onionSkinPainter, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
vectorImage->paintImage(onionSkinPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
paintOnionSkinFrame(painter, onionSkinPainter, nFrame, colorize, vectorImage->getOpacity());
}

Expand Down Expand Up @@ -344,7 +344,7 @@ void CanvasPainter::paintCurrentVectorFrame(QPainter& painter, const QRect& blit
const bool isDrawing = mTiledBuffer->isValid();

// Paint existing vector image to the painter
vectorImage->paintImage(currentVectorPainter, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);
vectorImage->paintImage(currentVectorPainter, *mObject, mOptions.bOutlines, mOptions.bThinLines, mOptions.bAntiAlias);

if (isCurrentLayer) {
if (isDrawing) {
Expand Down
22 changes: 11 additions & 11 deletions core_lib/src/graphics/vector/beziercurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BezierCurve::BezierCurve()
BezierCurve::BezierCurve(const QList<QPointF>& pointList, bool smooth)
{
QList<qreal> pressureList;
for (int i = 0; i < pointList.size(); i++)
for (int i = 0; i < pointList.size(); i++)
{
pressureList << 0.5; // default pressure
}
Expand Down Expand Up @@ -297,13 +297,13 @@ BezierCurve BezierCurve::transformed(QTransform transformation) const
if (isSelected(-1)) { newOrigin = transformation.map(newOrigin); }
transformedCurve.setOrigin( newOrigin );
for(int i=0; i< vertex.size(); i++) {
QPointF newC1 = c1.at(i);
QPointF newC2 = c2.at(i);
QPointF newVertex = vertex.at(i);
if (isSelected(i-1)) { newC1 = transformation.map(newC1); }
if (isSelected(i)) { newC2 = transformation.map(newC2); newVertex = transformation.map(newVertex); }
transformedCurve.appendCubic( newC1, newC2, newVertex, pressure.at(i) );
if (isSelected(i)) { transformedCurve.setSelected(i, true); }
QPointF newC1 = c1.at(i);
QPointF newC2 = c2.at(i);
QPointF newVertex = vertex.at(i);
if (isSelected(i-1)) { newC1 = transformation.map(newC1); }
if (isSelected(i)) { newC2 = transformation.map(newC2); newVertex = transformation.map(newVertex); }
transformedCurve.appendCubic( newC1, newC2, newVertex, pressure.at(i) );
if (isSelected(i)) { transformedCurve.setSelected(i, true); }
}
transformedCurve.setWidth( width);
transformedCurve.setVariableWidth( variableWidth );
Expand Down Expand Up @@ -429,9 +429,9 @@ void BezierCurve::removeVertex(int i)
}
}

void BezierCurve::drawPath(QPainter& painter, Object* object, QTransform transformation, bool simplified, bool showThinLines )
void BezierCurve::drawPath(QPainter& painter, const Object& object, QTransform transformation, bool simplified, bool showThinLines )
{
QColor color = object->getColor(colorNumber).color;
QColor color = object.getColor(colorNumber).color;

BezierCurve myCurve;
if (isPartlySelected()) { myCurve = (transformed(transformation)); }
Expand Down Expand Up @@ -869,7 +869,7 @@ bool BezierCurve::findIntersection(BezierCurve curve1, int i1, BezierCurve curve
//if (L2.intersect(L1, intersection) == QLineF::BoundedIntersection) {
//qDebug() << " FOUND rectangle intersection ";
//if (intersectionPoint != curve1.getVertex(i1-1) && intersectionPoint != curve1.getVertex(i1)) {
// qDebug() << " it's not one of the points ";
// qDebug() << " it's not one of the points ";
// find the cubic intersection
int nSteps = 24;
P1 = curve1.getVertex(i1-1);
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/graphics/vector/beziercurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class BezierCurve
QPainterPath getStrokedPath(qreal width, bool pressure);
QRectF getBoundingRect();

void drawPath(QPainter& painter, Object* object, QTransform transformation, bool simplified, bool showThinLines );
void drawPath(QPainter& painter, const Object& object, QTransform transformation, bool simplified, bool showThinLines );
void createCurve(const QList<QPointF>& pointList, const QList<qreal>& pressureList , bool smooth);
void smoothCurve();

Expand Down
18 changes: 4 additions & 14 deletions core_lib/src/graphics/vector/vectorimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ VectorImage::VectorImage()
VectorImage::VectorImage(const VectorImage& v2) : KeyFrame(v2)
{
deselectAll();
mObject = v2.mObject;
mCurves = v2.mCurves;
mArea = v2.mArea;
mOpacity = v2.mOpacity;
Expand All @@ -52,7 +51,6 @@ VectorImage& VectorImage::operator=(const VectorImage& a) {

deselectAll();
KeyFrame::operator=(a);
mObject = a.mObject;
mCurves = a.mCurves;
mArea = a.mArea;
mOpacity = a.mOpacity;
Expand Down Expand Up @@ -1090,16 +1088,6 @@ void VectorImage::paste(VectorImage& vectorImage)
modification();
}

/**
* @brief VectorImage::getColor
* @param colorNumber: the color number which is referred to in the palette
* @return QColor
*/
QColor VectorImage::getColor(int colorNumber)
{
return mObject->getColor(colorNumber).color;
}

/**
* @brief VectorImage::getColorNumber
* @param point: The QPoint of the BezierArea
Expand Down Expand Up @@ -1194,11 +1182,13 @@ void VectorImage::moveColor(int start, int end)
/**
* @brief VectorImage::paintImage
* @param painter: QPainter&
* @param object: const Object&
* @param simplified: bool
* @param showThinCurves: bool
* @param antialiasing: bool
*/
void VectorImage::paintImage(QPainter& painter,
const Object& object,
bool simplified,
bool showThinCurves,
bool antialiasing)
Expand All @@ -1220,7 +1210,7 @@ void VectorImage::paintImage(QPainter& painter,
updateArea(mArea[i]); // to do: if selected

// --- fill areas ---- //
QColor color = getColor(mArea[i].mColorNumber);
QColor color = object.getColor(mArea[i].mColorNumber).color;

painter.save();
painter.setWorldMatrixEnabled(false);
Expand All @@ -1246,7 +1236,7 @@ void VectorImage::paintImage(QPainter& painter,
// ---- draw curves ----
for (BezierCurve curve : mCurves)
{
curve.drawPath(painter, mObject, mSelectionTransformation, simplified, showThinCurves);
curve.drawPath(painter, object, mSelectionTransformation, simplified, showThinCurves);
painter.setClipping(false);
}
painter.restore();
Expand Down
6 changes: 1 addition & 5 deletions core_lib/src/graphics/vector/vectorimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class VectorImage : public KeyFrame

VectorImage* clone() const override;

void setObject(Object* pObj) { mObject = pObj; }

bool read(QString filePath);
Status write(QString filePath, QString format);

Expand Down Expand Up @@ -85,15 +83,14 @@ class VectorImage : public KeyFrame

void paste(VectorImage&);

QColor getColor(int i);
int getColorNumber(QPointF point);
bool usesColor(int index);
void removeColor(int index);
int getCurvesColor(int curve);
bool isCurveVisible(int curve);
void moveColor(int start, int end);

void paintImage(QPainter& painter, bool simplified, bool showThinCurves, bool antialiasing);
void paintImage(QPainter& painter, const Object& object, bool simplified, bool showThinCurves, bool antialiasing);

void clear();
void clean();
Expand Down Expand Up @@ -165,7 +162,6 @@ class VectorImage : public KeyFrame
private:
QList<BezierCurve> mCurves;

Object* mObject = nullptr;
QRectF mSelectionRect;
QTransform mSelectionTransformation;
QSize mSize;
Expand Down
51 changes: 0 additions & 51 deletions core_lib/src/managers/soundmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,57 +68,6 @@ Status SoundManager::save(Object*)
return Status::OK;
}

Status SoundManager::loadSound(Layer* soundLayer, int frameNumber, QString soundFilePath)
{
Q_ASSERT(soundLayer);
if (soundLayer->type() != Layer::SOUND)
{
return Status::ERROR_INVALID_LAYER_TYPE;
}

if (frameNumber < 0)
{
return Status::ERROR_INVALID_FRAME_NUMBER;
}

if (!QFile::exists(soundFilePath))
{
return Status::FILE_NOT_FOUND;
}

KeyFrame* key = soundLayer->getKeyFrameAt(frameNumber);
if (key == nullptr)
{
key = new SoundClip;
soundLayer->addKeyFrame(frameNumber, key);
}

if (!key->fileName().isEmpty())
{
// file path should be empty.
// we can only load a audio clip to an empty key!
return Status::FAIL;
}

QString strCopyFile = soundLayer->object()->copyFileToDataFolder(soundFilePath);
Q_ASSERT(!strCopyFile.isEmpty());

QString sOriginalName = QFileInfo(soundFilePath).fileName();

SoundClip* soundClip = dynamic_cast<SoundClip*>(key);
soundClip->init(strCopyFile);
soundClip->setSoundClipName(sOriginalName);

Status st = createMediaPlayer(soundClip);
if (!st.ok())
{
delete soundClip;
return st;
}

return Status::OK;
}

Status SoundManager::loadSound(SoundClip* soundClip, QString strSoundFile)
{
Q_ASSERT(soundClip);
Expand Down
1 change: 0 additions & 1 deletion core_lib/src/managers/soundmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SoundManager : public BaseManager
Status load(Object*) override;
Status save(Object*) override;

Status loadSound(Layer* soundLayer, int frameNumber, QString strSoundFile);
Status loadSound(SoundClip* soundClip, QString strSoundFile);
Status processSound(SoundClip* soundClip);

Expand Down
15 changes: 3 additions & 12 deletions core_lib/src/structure/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ GNU General Public License for more details.
#include <QPainter>
#include <QDomElement>
#include "keyframe.h"
#include "object.h"

// Used to sort the selected frames list
bool sortAsc(int left, int right)
{
return left < right;
}

Layer::Layer(Object* object, LAYER_TYPE eType)
Layer::Layer(int id, LAYER_TYPE eType)
{
Q_ASSERT(eType != UNDEFINED);

mObject = object;
meType = eType;
mName = QString(tr("Undefined Layer"));

mId = object->getUniqueLayerID();
mId = id;
}

Layer::~Layer()
Expand All @@ -51,13 +49,6 @@ Layer::~Layer()
mKeyFrames.clear();
}

void Layer::setObject(Object* obj)
{
Q_ASSERT(obj);
mObject = obj;
mId = mObject->getUniqueLayerID();
}

void Layer::foreachKeyFrame(std::function<void(KeyFrame*)> action) const
{
for (auto pair : mKeyFrames)
Expand Down Expand Up @@ -180,7 +171,7 @@ bool Layer::addNewKeyFrameAt(int position)
{
if (position <= 0) return false;

KeyFrame* key = createKeyFrame(position, mObject);
KeyFrame* key = createKeyFrame(position);
return addKeyFrame(position, key);
}

Expand Down
Loading

0 comments on commit 18c5494

Please sign in to comment.