Skip to content

Commit

Permalink
Merge pull request #1283 from davidlamhauge/drag_palette
Browse files Browse the repository at this point in the history
#944 Reorder palette swatches with drag and drop
  • Loading branch information
candyface authored Dec 8, 2019
2 parents 91951c5 + 137c455 commit 526a0e4
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 24 deletions.
100 changes: 78 additions & 22 deletions app/src/colorpalettewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ GNU General Public License for more details.
#include <QPushButton>
#include <QSettings>
#include <QMenu>
#include <QAbstractItemModel>

// Project
#include "colourref.h"
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}

Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()
Expand All @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/colorpalettewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ namespace Ui
class ColorPalette;
}


class ColorPaletteWidget : public BaseDockWidget
{
Q_OBJECT
Expand All @@ -47,6 +46,7 @@ class ColorPaletteWidget : public BaseDockWidget

void initUI() override;
void updateUI() override;
void setCore(Editor* editor);

int currentColourNumber();

Expand All @@ -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();
Expand Down Expand Up @@ -106,6 +107,9 @@ private slots:
bool mIsColorDialog = false;
bool mMultipleSelected = false;

Editor* mEditor = nullptr;
Object* mObject = nullptr;

};

#endif
Expand Down
1 change: 1 addition & 0 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions core_lib/src/graphics/vector/vectorimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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&
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/graphics/vector/vectorimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/interface/scribblearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ void ScribbleArea::setPrevTool()

void ScribbleArea::paletteColorChanged(QColor color)
{
Q_UNUSED(color);
Q_UNUSED(color)
updateAllVectorLayersAtCurrentFrame();
}

Expand Down
9 changes: 9 additions & 0 deletions core_lib/src/structure/layervector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ void LayerVector::removeColour(int colorIndex)
});
}

void LayerVector::moveColor(int start, int end)
{
foreachKeyFrame( [=] (KeyFrame* pKeyFrame)
{
auto pVecImage = static_cast<VectorImage*>(pKeyFrame);
pVecImage->moveColor(start, end);
});
}

void LayerVector::loadImageAtFrame(QString path, int frameNumber)
{
if (keyExists(frameNumber))
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/structure/layervector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions core_lib/src/structure/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LayerVector*>(layer)->moveColor(start, end);
}
}
}

void Object::addColourAtIndex(int index, ColourRef newColour)
{
mPalette.insert(index, newColour);
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/structure/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 526a0e4

Please sign in to comment.