diff --git a/app/src/colorpalettewidget.cpp b/app/src/colorpalettewidget.cpp index 0c54fe349..8fb1e0569 100644 --- a/app/src/colorpalettewidget.cpp +++ b/app/src/colorpalettewidget.cpp @@ -31,6 +31,7 @@ GNU General Public License for more details. #include #include #include +#include // Project #include "colourref.h" @@ -80,6 +81,7 @@ void ColorPaletteWidget::initUI() palettePreferences(); connect(ui->colorListWidget, &QListWidget::itemClicked, this, &ColorPaletteWidget::clickColorListItem); + connect(ui->colorListWidget->model(), &QAbstractItemModel::rowsMoved, this, &ColorPaletteWidget::onRowsMoved); connect(ui->colorListWidget, &QListWidget::itemDoubleClicked, this, &ColorPaletteWidget::changeColourName); connect(ui->colorListWidget, &QListWidget::itemChanged, this, &ColorPaletteWidget::onItemChanged); @@ -96,6 +98,12 @@ void ColorPaletteWidget::updateUI() updateGridUI(); } +void ColorPaletteWidget::setCore(Editor *editor) +{ + mEditor = editor; + mObject = mEditor->object(); +} + void ColorPaletteWidget::showContextMenu(const QPoint &pos) { QPoint globalPos = ui->colorListWidget->mapToGlobal(pos); @@ -111,14 +119,14 @@ void ColorPaletteWidget::showContextMenu(const QPoint &pos) void ColorPaletteWidget::addItem() { QSignalBlocker b(ui->colorListWidget); - QColor newColour = editor()->color()->frontColor(); + QColor newColour = mEditor->color()->frontColor(); // add in front of selected color int colorIndex = ui->colorListWidget->currentRow()+1; ColourRef ref(newColour); - editor()->object()->addColourAtIndex(colorIndex, ref); + mObject->addColourAtIndex(colorIndex, ref); refreshColorList(); } @@ -127,7 +135,7 @@ void ColorPaletteWidget::replaceItem() QSignalBlocker b(ui->colorListWidget); int index = ui->colorListWidget->currentRow(); - QColor newColour = editor()->color()->frontColor(); + QColor newColour = mEditor->color()->frontColor(); if (index >= 0) { @@ -189,11 +197,11 @@ void ColorPaletteWidget::refreshColorList() borderHighlight.setColor(QColor(255, 255, 255, 200)); borderHighlight.setDashOffset(4); - int colourCount = editor()->object()->getColourCount(); + int colourCount = mObject->getColourCount(); for (int i = 0; i < colourCount; i++) { - const ColourRef colourRef = editor()->object()->getColour(i); + const ColourRef colourRef = mObject->getColour(i); QListWidgetItem* colourItem = new QListWidgetItem(ui->colorListWidget); if (ui->colorListWidget->viewMode() != QListView::IconMode) @@ -222,7 +230,7 @@ void ColorPaletteWidget::refreshColorList() colourItem->setIcon(swatchIcon); swatchPainter.end(); - colourItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); + colourItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDragEnabled); ui->colorListWidget->addItem(colourItem); } @@ -244,11 +252,11 @@ void ColorPaletteWidget::changeColourName(QListWidgetItem* item) tr("Colour name"), tr("Colour name"), QLineEdit::Normal, - editor()->object()->getColour(colorNumber).name, + mObject->getColour(colorNumber).name, &ok); if (ok && !text.isEmpty()) { - editor()->object()->renameColour(colorNumber, text); + mObject->renameColour(colorNumber, text); refreshColorList(); } } @@ -259,7 +267,55 @@ void ColorPaletteWidget::onItemChanged(QListWidgetItem* item) { int index = ui->colorListWidget->row(item); QString newColorName = item->text(); - editor()->object()->renameColour(index, newColorName); + mObject->renameColour(index, newColorName); +} + +void ColorPaletteWidget::onRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row) +{ + Q_UNUSED(parent) + Q_UNUSED(destination) + Q_UNUSED(end) + + int startIndex, endIndex; + if (start < row) + { + row -= 1; // TODO: Is this a bug? + if (start == row) { return; } + + startIndex = start; + endIndex = row; + + mObject->movePaletteColor(startIndex, endIndex); + + mObject->addColour(mObject->getColour(startIndex)); + mObject->moveVectorColor(startIndex, mObject->getColourCount() - 1); + for (int i = startIndex; i < endIndex; i++) + { + mObject->moveVectorColor(i + 1, i); + } + mObject->moveVectorColor(mObject->getColourCount() - 1, endIndex); + } + else + { + if (start == row) { return; } + + startIndex = start; + endIndex = row; + + mObject->movePaletteColor(startIndex, endIndex); + + mObject->addColour(mObject->getColour(startIndex)); + mObject->moveVectorColor(startIndex, mObject->getColourCount() - 1); + for (int i = startIndex; i > endIndex; i--) + { + mObject->moveVectorColor(i - 1, i); + } + mObject->moveVectorColor(mObject->getColourCount() - 1, endIndex); + } + + mObject->removeColour(mObject->getColourCount() - 1); + + refreshColorList(); } void ColorPaletteWidget::clickColorListItem(QListWidgetItem* currentItem) @@ -320,7 +376,7 @@ void ColorPaletteWidget::palettePreferences() void ColorPaletteWidget::setListMode() { ui->colorListWidget->setViewMode(QListView::ListMode); - ui->colorListWidget->setMovement(QListView::Static); + ui->colorListWidget->setDragDropMode(QAbstractItemView::InternalMove); ui->colorListWidget->setGridSize(QSize(-1, -1)); updateUI(); @@ -424,21 +480,21 @@ void ColorPaletteWidget::clickAddColorButton() if (mIsColorDialog) newColour = QColorDialog::getColor(prevColor.rgba(), this, QString(), QColorDialog::ShowAlphaChannel); else - newColour = editor()->color()->frontColor(); + newColour = mEditor->color()->frontColor(); if (!newColour.isValid()) { return; // User canceled operation } - int colorIndex = editor()->object()->getColourCount(); + int colorIndex = mObject->getColourCount(); ColourRef ref(newColour); - editor()->object()->addColour(ref); + mObject->addColour(ref); refreshColorList(); - editor()->color()->setColorNumber(colorIndex); - editor()->color()->setColor(ref.colour); + mEditor->color()->setColorNumber(colorIndex); + mEditor->color()->setColor(ref.colour); } void ColorPaletteWidget::clickRemoveColorButton() @@ -450,28 +506,28 @@ void ColorPaletteWidget::clickRemoveColorButton() // items are not deleted by qt, it has to be done manually // delete should happen before removing the color from from palette // as the palette will be one ahead and crash otherwise - if (editor()->object()->isColourInUse(index)) + if (mObject->isColourInUse(index)) { bool accepted = false; if (!mMultipleSelected) accepted = showPaletteWarning(); - if ((accepted || mMultipleSelected) && editor()->object()->getColourCount() > 1) + if ((accepted || mMultipleSelected) && mObject->getColourCount() > 1) { delete item; - editor()->object()->removeColour(index); + mObject->removeColour(index); } } - else if (editor()->object()->getColourCount() > 1) + else if (mObject->getColourCount() > 1) { delete item; - editor()->object()->removeColour(index); + mObject->removeColour(index); } - else if (editor()->object()->getColourCount() == 1) + else if (mObject->getColourCount() == 1) { showPaletteReminder(); } - editor()->updateCurrentFrame(); + mEditor->updateCurrentFrame(); } mMultipleSelected = false; } diff --git a/app/src/colorpalettewidget.h b/app/src/colorpalettewidget.h index 8d074d052..0c38a5aa8 100644 --- a/app/src/colorpalettewidget.h +++ b/app/src/colorpalettewidget.h @@ -35,7 +35,6 @@ namespace Ui class ColorPalette; } - class ColorPaletteWidget : public BaseDockWidget { Q_OBJECT @@ -47,6 +46,7 @@ class ColorPaletteWidget : public BaseDockWidget void initUI() override; void updateUI() override; + void setCore(Editor* editor); int currentColourNumber(); @@ -67,6 +67,7 @@ private slots: void clickColorListItem(QListWidgetItem*); void changeColourName(QListWidgetItem*); void onItemChanged(QListWidgetItem* item); + void onRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row); void clickAddColorButton(); void clickColorDialogButton(); void clickRemoveColorButton(); @@ -106,6 +107,9 @@ private slots: bool mIsColorDialog = false; bool mMultipleSelected = false; + Editor* mEditor = nullptr; + Object* mObject = nullptr; + }; #endif diff --git a/app/src/mainwindow2.cpp b/app/src/mainwindow2.cpp index caa3d217a..9887a5e6f 100644 --- a/app/src/mainwindow2.cpp +++ b/app/src/mainwindow2.cpp @@ -152,6 +152,7 @@ void MainWindow2::createDockWidgets() mColorInspector->setObjectName("Color Inspector"); mColorPalette = new ColorPaletteWidget(this); + mColorPalette->setCore(mEditor); mColorPalette->setObjectName("ColorPalette"); mDisplayOptionWidget = new DisplayOptionWidget(this); diff --git a/core_lib/src/graphics/vector/vectorimage.cpp b/core_lib/src/graphics/vector/vectorimage.cpp index a66648d5c..5ab3fc677 100644 --- a/core_lib/src/graphics/vector/vectorimage.cpp +++ b/core_lib/src/graphics/vector/vectorimage.cpp @@ -1111,6 +1111,18 @@ void VectorImage::removeColour(int index) } } +void VectorImage::moveColor(int start, int end) +{ + for(int i=0; i< mArea.size(); i++) + { + if (mArea[i].getColourNumber() == start) mArea[i].setColourNumber(end); + } + for(int i=0; i< mCurves.size(); i++) + { + if (mCurves[i].getColourNumber() == start) mCurves[i].setColourNumber(end); + } +} + /** * @brief VectorImage::paintImage * @param painter: QPainter& diff --git a/core_lib/src/graphics/vector/vectorimage.h b/core_lib/src/graphics/vector/vectorimage.h index 7ec14be6e..c9a244c7f 100644 --- a/core_lib/src/graphics/vector/vectorimage.h +++ b/core_lib/src/graphics/vector/vectorimage.h @@ -86,6 +86,7 @@ class VectorImage : public KeyFrame int getColourNumber(QPointF point); bool usesColour(int index); void removeColour(int index); + void moveColor(int start, int end); void paintImage(QPainter& painter, bool simplified, bool showThinCurves, bool antialiasing); void outputImage(QImage* image, QTransform myView, bool simplified, bool showThinCurves, bool antialiasing); // uses paintImage diff --git a/core_lib/src/interface/scribblearea.cpp b/core_lib/src/interface/scribblearea.cpp index 9388a4f51..cd8c66bfc 100644 --- a/core_lib/src/interface/scribblearea.cpp +++ b/core_lib/src/interface/scribblearea.cpp @@ -1571,7 +1571,7 @@ void ScribbleArea::setPrevTool() void ScribbleArea::paletteColorChanged(QColor color) { - Q_UNUSED(color); + Q_UNUSED(color) updateAllVectorLayersAtCurrentFrame(); } diff --git a/core_lib/src/structure/layervector.cpp b/core_lib/src/structure/layervector.cpp index 7df584e9e..a6033f072 100644 --- a/core_lib/src/structure/layervector.cpp +++ b/core_lib/src/structure/layervector.cpp @@ -50,6 +50,15 @@ void LayerVector::removeColour(int colorIndex) }); } +void LayerVector::moveColor(int start, int end) +{ + foreachKeyFrame( [=] (KeyFrame* pKeyFrame) + { + auto pVecImage = static_cast(pKeyFrame); + pVecImage->moveColor(start, end); + }); +} + void LayerVector::loadImageAtFrame(QString path, int frameNumber) { if (keyExists(frameNumber)) diff --git a/core_lib/src/structure/layervector.h b/core_lib/src/structure/layervector.h index 0779fcf55..a7300426e 100644 --- a/core_lib/src/structure/layervector.h +++ b/core_lib/src/structure/layervector.h @@ -41,6 +41,7 @@ class LayerVector : public Layer bool usesColour(int index); void removeColour(int index); + void moveColor(int start, int end); protected: Status saveKeyFrameFile(KeyFrame*, QString path) override; diff --git a/core_lib/src/structure/object.cpp b/core_lib/src/structure/object.cpp index 676f386c6..e5a3f1c59 100644 --- a/core_lib/src/structure/object.cpp +++ b/core_lib/src/structure/object.cpp @@ -316,6 +316,23 @@ void Object::addColour(QColor colour) addColour(ColourRef(colour, "Colour " + QString::number(mPalette.size()))); } +void Object::movePaletteColor(int start, int end) +{ + mPalette.move(start, end); +} + +void Object::moveVectorColor(int start, int end) +{ + for (int i = 0; i < getLayerCount(); i++) + { + Layer* layer = getLayer(i); + if (layer->type() == Layer::VECTOR) + { + static_cast(layer)->moveColor(start, end); + } + } +} + void Object::addColourAtIndex(int index, ColourRef newColour) { mPalette.insert(index, newColour); diff --git a/core_lib/src/structure/object.h b/core_lib/src/structure/object.h index 053d8a600..6345a1f12 100644 --- a/core_lib/src/structure/object.h +++ b/core_lib/src/structure/object.h @@ -87,6 +87,8 @@ class Object : public QObject void setColour(int index, QColor newColour); void setColourRef(int index, ColourRef newColourRef); void addColour(QColor); + void movePaletteColor(int start, int end); + void moveVectorColor(int start, int end); void addColour(ColourRef newColour) { mPalette.append(newColour); } void addColourAtIndex(int index, ColourRef newColour);