From 9cbd0e8743e7067f89dcf90121c2ae8fc1e6f0b9 Mon Sep 17 00:00:00 2001 From: shun_iwasawa Date: Aug 26 2016 11:59:54 +0000 Subject: organize_palette 4 --- diff --git a/toonz/sources/common/tvrender/tpalette.cpp b/toonz/sources/common/tvrender/tpalette.cpp index 3f7f9c0..902a439 100644 --- a/toonz/sources/common/tvrender/tpalette.cpp +++ b/toonz/sources/common/tvrender/tpalette.cpp @@ -1115,3 +1115,15 @@ void TPalette::setShortcutValue(int key, int styleId) { m_shortcuts[key] = styleId; } } + +//------------------------------------------------------------------- +// Returns true if there is at least one style with picked pos value +//------------------------------------------------------------------- + +bool TPalette::hasPickedPosStyle() { + for (int i = 0; i < getStyleCount(); ++i) { + TColorStyleP style = m_styles[i].second; + if (style->getPickedPosition() != TPoint()) return true; + } + return false; +} \ No newline at end of file diff --git a/toonz/sources/include/toonz/palettecmd.h b/toonz/sources/include/toonz/palettecmd.h index da63518..ada5ee4 100644 --- a/toonz/sources/include/toonz/palettecmd.h +++ b/toonz/sources/include/toonz/palettecmd.h @@ -92,6 +92,13 @@ DVAPI void renamePaletteStyle(TPaletteHandle *paletteHandle, DVAPI void organizePaletteStyle(TPaletteHandle *paletteHandle, int styleId, const TPoint &point); +/* +called in ColorModelViewer::repickFromColorModel(). +Pick color from the img for all styles with "picked position" value. +*/ +DVAPI void pickColorByUsingPickedPosition(TPaletteHandle *paletteHandle, + TImageP img); + } // namespace #endif diff --git a/toonz/sources/include/tpalette.h b/toonz/sources/include/tpalette.h index bf16794..a1085ce 100644 --- a/toonz/sources/include/tpalette.h +++ b/toonz/sources/include/tpalette.h @@ -417,6 +417,9 @@ between RGBA color components. m_isLocked = lock; } + bool hasPickedPosStyle(); // Returns true if there is at least one style with + // picked pos value + public: // Deprecated functions diff --git a/toonz/sources/toonz/colormodelviewer.cpp b/toonz/sources/toonz/colormodelviewer.cpp index 6636000..9ff95ab 100644 --- a/toonz/sources/toonz/colormodelviewer.cpp +++ b/toonz/sources/toonz/colormodelviewer.cpp @@ -223,11 +223,28 @@ void ColorModelViewer::contextMenuEvent(QContextMenuEvent *event) { connect(loadCurrentFrame, SIGNAL(triggered()), SLOT(loadCurrentFrame())); menu.addAction(loadCurrentFrame); + if (!m_imageViewer->getImage()) { + menu.exec(event->globalPos()); + return; + } + QAction *removeColorModel = new QAction(QString(tr("Remove Color Model")), this); connect(removeColorModel, SIGNAL(triggered()), SLOT(removeColorModel())); menu.addAction(removeColorModel); + /* If there is at least one style with "picked pos" parameter, then enable + * repick command */ + TRasterImageP ri = m_imageViewer->getImage(); + if (ri && currentPalette->hasPickedPosStyle()) { + menu.addSeparator(); + QAction *repickFromColorModelAct = new QAction( + QString(tr("Update Colors by Using Picked Positions")), this); + connect(repickFromColorModelAct, SIGNAL(triggered()), + SLOT(repickFromColorModel())); + menu.addAction(repickFromColorModelAct); + } + menu.addSeparator(); QString shortcut = QString::fromStdString( @@ -595,6 +612,17 @@ void ColorModelViewer::onRefImageNotFound() { "level.")); } +//----------------------------------------------------------------------------- + +void ColorModelViewer::repickFromColorModel() { + TImageP img = m_imageViewer->getImage(); + if (!img) return; + TPaletteHandle *ph = + TApp::instance()->getPaletteController()->getCurrentLevelPalette(); + + PaletteCmd::pickColorByUsingPickedPosition(ph, img); +} + //============================================================================= OpenFloatingPanel openColorModelCommand(MI_OpenColorModel, "ColorModel", diff --git a/toonz/sources/toonz/colormodelviewer.h b/toonz/sources/toonz/colormodelviewer.h index 3a4a00f..833a62d 100644 --- a/toonz/sources/toonz/colormodelviewer.h +++ b/toonz/sources/toonz/colormodelviewer.h @@ -63,6 +63,8 @@ protected slots: * -*/ void changePickType(); + void repickFromColorModel(); + signals: void refImageNotFound(); }; diff --git a/toonz/sources/toonzlib/palettecmd.cpp b/toonz/sources/toonzlib/palettecmd.cpp index 3cc28a1..c23f12e 100644 --- a/toonz/sources/toonzlib/palettecmd.cpp +++ b/toonz/sources/toonzlib/palettecmd.cpp @@ -33,6 +33,7 @@ #include "tstroke.h" #include "tregion.h" #include "tlevel_io.h" +#include "tpixelutils.h" #include "historytypes.h" @@ -45,6 +46,8 @@ #include #include +#include + //=================================================================== void findPaletteLevels(set &levels, int &rowIndex, @@ -1240,3 +1243,105 @@ void PaletteCmd::organizePaletteStyle(TPaletteHandle *paletteHandle, TUndoManager::manager()->endBlock(); } + +//============================================================================= +// called in ColorModelViewer::repickFromColorModel(). +// Pick color from the img for all styles with "picked position" value. +//----------------------------------------------------------------------------- +namespace { + +class pickColorByUsingPickedPositionUndo final : public TUndo { + TPaletteHandle *m_paletteHandle; // Used in undo and redo to notify change + TPaletteP m_palette; + QHash> m_styleList; + +public: + pickColorByUsingPickedPositionUndo( + TPaletteHandle *paletteHandle, + QHash> styleList) + : m_paletteHandle(paletteHandle), m_styleList(styleList) { + m_palette = paletteHandle->getPalette(); + assert(m_palette); + } + void undo() const override { + QHash>::const_iterator i = + m_styleList.constBegin(); + while (i != m_styleList.constEnd()) { + TColorStyle *style = m_palette->getStyle(i.key()); + assert(style); + style->setMainColor(i.value().first); + ++i; + } + m_paletteHandle->notifyColorStyleChanged(false); + } + void redo() const override { + QHash>::const_iterator i = + m_styleList.constBegin(); + while (i != m_styleList.constEnd()) { + TColorStyle *style = m_palette->getStyle(i.key()); + assert(style); + style->setMainColor(i.value().second); + ++i; + } + m_paletteHandle->notifyColorStyleChanged(false); + } + int getSize() const override { return sizeof *this; } + QString getHistoryString() override { + return QObject::tr("Update Colors by Using Picked Positions in Palette %1") + .arg(QString::fromStdWString(m_palette->getPaletteName())); + } + int getHistoryType() override { return HistoryType::Palette; } +}; + +TPixel32 pickColor(TRasterImageP ri, const TPoint &rasterPoint) { + TRasterP raster; + raster = ri->getRaster(); + + if (!TRect(raster->getSize()).contains(rasterPoint)) + return TPixel32::Transparent; + + TRaster32P raster32 = raster; + if (raster32) return raster32->pixels(rasterPoint.y)[rasterPoint.x]; + + TRasterGR8P rasterGR8 = raster; + if (rasterGR8) + return toPixel32(rasterGR8->pixels(rasterPoint.y)[rasterPoint.x]); + + return TPixel32::Transparent; +} +} + +void PaletteCmd::pickColorByUsingPickedPosition(TPaletteHandle *paletteHandle, + TImageP img) { + TRasterImageP ri = img; + if (!ri) return; + + TPalette *currentPalette = paletteHandle->getPalette(); + if (!currentPalette) return; + + TDimension imgSize = ri->getRaster()->getSize(); + QHash> styleList; + + // For all styles (starting from #1 as #0 is reserved for the transparent) + for (int sId = 1; sId < currentPalette->getStyleCount(); sId++) { + TColorStyle *style = currentPalette->getStyle(sId); + TPoint pp = style->getPickedPosition(); + // If style has a valid picked position + if (pp != TPoint() && pp.x >= 0 && pp.x < imgSize.lx && pp.y >= 0 && + pp.y < imgSize.ly && style->hasMainColor()) { + TPixel32 beforeColor = style->getMainColor(); + TPixel32 afterColor = pickColor(ri, pp); + style->setMainColor(afterColor); + //... store the style in the list for undo + styleList.insert(sId, QPair(beforeColor, afterColor)); + } + } + + // if some style has been changed, then register undo and notify changes + if (!styleList.isEmpty()) { + pickColorByUsingPickedPositionUndo *undo = + new pickColorByUsingPickedPositionUndo(paletteHandle, styleList); + TUndoManager::manager()->add(undo); + paletteHandle->notifyColorStyleChanged(false, true); // set dirty flag here + } +} \ No newline at end of file diff --git a/toonz/sources/toonzqt/paletteviewergui.cpp b/toonz/sources/toonzqt/paletteviewergui.cpp index fcd489b..fcf7105 100644 --- a/toonz/sources/toonzqt/paletteviewergui.cpp +++ b/toonz/sources/toonzqt/paletteviewergui.cpp @@ -454,6 +454,10 @@ void PageViewer::drawColorName(QPainter &p, QRect &nameRect, TColorStyle *style, name += " " + toQString(g.first) + ":" + QString::number(g.second); if (style->getFlags() != 0) name += "(autopaint)"; + TPoint pickedPos = style->getPickedPosition(); + if (pickedPos != TPoint()) + name += QString(" (%1,%2)").arg(pickedPos.x).arg(pickedPos.y); + p.drawText(nameRect.adjusted(6, 4, -6, -4), name); QColor borderCol(getTextColor()); @@ -772,10 +776,23 @@ void PageViewer::paintEvent(QPaintEvent *e) { p.drawText(indexRect, Qt::AlignCenter, QString().setNum(styleIndex)); // draw "Autopaint for lines" indicator + int offset = 0; if (style->getFlags() != 0) { QRect aflRect(chipRect.bottomLeft() + QPoint(0, -14), QSize(12, 15)); p.drawRect(aflRect); p.drawText(aflRect, Qt::AlignCenter, "A"); + offset += 12; + } + + // draw "Picked Position" indicator (not show on small chip mode) + if (style->getPickedPosition() != TPoint() && m_viewMode != SmallChips) { + QRect ppRect(chipRect.bottomLeft() + QPoint(offset, -14), + QSize(12, 15)); + p.drawRect(ppRect); + QPoint markPos = ppRect.center() + QPoint(1, 0); + p.drawEllipse(markPos, 3, 3); + p.drawLine(markPos - QPoint(5, 0), markPos + QPoint(5, 0)); + p.drawLine(markPos - QPoint(0, 5), markPos + QPoint(0, 5)); } // revert font set diff --git a/toonz/sources/toonzqt/styleeditor.cpp b/toonz/sources/toonzqt/styleeditor.cpp index 107db3e..361b879 100644 --- a/toonz/sources/toonzqt/styleeditor.cpp +++ b/toonz/sources/toonzqt/styleeditor.cpp @@ -3060,9 +3060,13 @@ void StyleEditor::onStyleSwitched() { QString::fromStdWString(L" Palette : " + palette->getPaletteName()); // style name - statusText += QString::fromStdWString(L" | Style#"); + statusText += QString::fromStdWString(L" | #"); statusText += QString::number(styleIndex); statusText += QString::fromStdWString(L" : " + m_editedStyle->getName()); + TPoint pickedPos = m_editedStyle->getPickedPosition(); + if (pickedPos != TPoint()) + statusText += + QString(" (Picked from %1,%2)").arg(pickedPos.x).arg(pickedPos.y); m_statusLabel->setText(statusText); } else