From 1e49112e2a11d246ee44e42e28c12d96e044975d Mon Sep 17 00:00:00 2001 From: Rodney Date: Jun 23 2021 15:00:46 +0000 Subject: Merge pull request #3989 from shun-iwasawa/g/paste_style_color_to_colorfield Enable to Paste Style's Color Into Color Field --- diff --git a/toonz/sources/include/toonzqt/colorfield.h b/toonz/sources/include/toonzqt/colorfield.h index 9d10a03..4e44902 100644 --- a/toonz/sources/include/toonzqt/colorfield.h +++ b/toonz/sources/include/toonzqt/colorfield.h @@ -181,12 +181,15 @@ protected: void mousePressEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; void hideEvent(QHideEvent *) override; + void contextMenuEvent(QContextMenuEvent *event) override; protected slots: void onRedChannelChanged(int value, bool isDragging); void onGreenChannelChanged(int value, bool isDragging); void onBlueChannelChanged(int value, bool isDragging); void onAlphaChannelChanged(int value, bool isDragging); + void onPasteColor(); + void onCopyColor(); signals: void editingChanged(const TPixel32 &, bool isEditing); diff --git a/toonz/sources/toonzqt/colorfield.cpp b/toonz/sources/toonzqt/colorfield.cpp index 236366b..52bc928 100644 --- a/toonz/sources/toonzqt/colorfield.cpp +++ b/toonz/sources/toonzqt/colorfield.cpp @@ -10,7 +10,11 @@ #include "tcolorstyles.h" #include "trop.h" #include "toonzqt/lutcalibrator.h" +#include "styledata.h" +// Qt includes +#include +#include #include #include #include @@ -18,6 +22,31 @@ using namespace DVGui; +namespace { + +void drawChessBoard(QPainter &p) { + for (int x = 0; x < 4; x++) { + for (int y = 0; y < 4; y++) { + QColor col((x + y) % 2 == 0 ? Qt::black : Qt::white); + p.fillRect(x * 4, y * 4, 4, 4, col); + } + } +} + +QPixmap getIconPm(const QColor &color) { + QPixmap retPm(16, 16); + if (color.alpha() == 255) { + retPm.fill(color); + return retPm; + } + QPainter p(&retPm); + drawChessBoard(p); + p.fillRect(0, 0, 16, 16, color); + return retPm; +} + +} // namespace + //============================================================================= /*! \class DVGui::StyleSample \brief The StyleSample class provides to view a square color. @@ -505,6 +534,51 @@ void ColorField::hideEvent(QHideEvent *) { } //----------------------------------------------------------------------------- + +void ColorField::contextMenuEvent(QContextMenuEvent *event) { + bool hasColor = QApplication::clipboard()->mimeData()->hasColor(); + const StyleData *data = + dynamic_cast(QApplication::clipboard()->mimeData()); + + QMenu menu(this); + if (hasColor) { // pasting QColor + QColor color = qvariant_cast( + QApplication::clipboard()->mimeData()->colorData()); + QAction *action = new QAction(tr("Paste Color"), this); + action->setIcon(QIcon(getIconPm(color))); + action->setData(color); + + connect(action, SIGNAL(triggered()), this, SLOT(onPasteColor())); + menu.addAction(action); + menu.addSeparator(); + } else if (data && data->getStyleCount() > 0) { // pasting styles colors + // show 10 styles in maximum + int styleCount = std::min(10, data->getStyleCount()); + for (int i = 0; i < styleCount; i++) { + QString styleName = QString::fromStdWString(data->getStyle(i)->getName()); + TPixel32 color = data->getStyle(i)->getMainColor(); + QColor _color(color.r, color.g, color.b, color.m); + + QAction *action = + new QAction(tr("Paste Color of %1").arg(styleName), this); + action->setIcon(QIcon(getIconPm(_color))); + action->setData(_color); + + connect(action, SIGNAL(triggered()), this, SLOT(onPasteColor())); + menu.addAction(action); + } + menu.addSeparator(); + } + + QAction *copyAction = new QAction(tr("Copy Color"), this); + connect(copyAction, SIGNAL(triggered()), this, SLOT(onCopyColor())); + menu.addAction(copyAction); + + menu.exec(event->globalPos()); + event->accept(); +} + +//----------------------------------------------------------------------------- /*! If current red channel value of color is different from \b value set it, change \b StyleSample color and emit signal \b colorChanged(const TPixel32 &). @@ -566,6 +640,28 @@ void ColorField::onAlphaChannelChanged(int value, bool isDragging) { //----------------------------------------------------------------------------- +void ColorField::onPasteColor() { + QColor color = qobject_cast(sender())->data().value(); + + m_color = TPixel32(color.red(), color.green(), color.blue(), color.alpha()); + if (!m_alphaChannel->isVisible()) m_color.m = 255; + m_colorSample->setColor(m_color); + updateChannels(); + emit colorChanged(m_color, false); +} + +//----------------------------------------------------------------------------- + +void ColorField::onCopyColor() { + QColor color(m_color.r, m_color.g, m_color.b, m_color.m); + + QMimeData *data = new QMimeData(); + data->setColorData(color); + QApplication::clipboard()->setMimeData(data); +} + +//----------------------------------------------------------------------------- + void ColorField::setChessboardColors(const TPixel32 &col1, const TPixel32 &col2) { m_colorSample->setChessboardColors(col1, col2);