From c0d30fae55ce89d957f8a2e9cf18a07d9bc9a83e Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@qt.io>
Date: Mon, 21 Oct 2019 16:43:21 +0200
Subject: [PATCH] pixeltool: Add context menu option to copy the color

Add an option to copy the color via QColor::name(),
bound to the 'c' key.

Change-Id: Ib33538e5ea5523e5ec5881d75e0b17ab675078e3
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
---
 src/pixeltool/qpixeltool.cpp | 11 ++++++++++-
 src/pixeltool/qpixeltool.h   |  1 +
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/pixeltool/qpixeltool.cpp b/src/pixeltool/qpixeltool.cpp
index f699ec828..81923d1ed 100644
--- a/src/pixeltool/qpixeltool.cpp
+++ b/src/pixeltool/qpixeltool.cpp
@@ -307,8 +307,10 @@ void QPixelTool::keyPressEvent(QKeyEvent *e)
         break;
 #if QT_CONFIG(clipboard)
     case Qt::Key_C:
-        if (e->modifiers() & Qt::ControlModifier)
+        if (e->modifiers().testFlag(Qt::ControlModifier))
             copyToClipboard();
+        else
+            copyColorToClipboard();
         break;
 #endif // QT_CONFIG(clipboard)
     case Qt::Key_S:
@@ -448,6 +450,8 @@ void QPixelTool::contextMenuEvent(QContextMenuEvent *e)
 #if QT_CONFIG(clipboard)
     menu.addAction(QLatin1String("Copy to clipboard"),
                    this, &QPixelTool::copyToClipboard, QKeySequence::Copy);
+    menu.addAction(QLatin1String("Copy color value to clipboard"),
+                   this, &QPixelTool::copyColorToClipboard, Qt::Key_C);
 #endif // QT_CONFIG(clipboard)
 
     menu.addSeparator();
@@ -640,6 +644,11 @@ void QPixelTool::copyToClipboard()
 {
     QGuiApplication::clipboard()->setPixmap(m_buffer);
 }
+
+void QPixelTool::copyColorToClipboard()
+{
+    QGuiApplication::clipboard()->setText(QColor(m_currentColor).name());
+}
 #endif // QT_CONFIG(clipboard)
 
 void QPixelTool::saveToFile()
diff --git a/src/pixeltool/qpixeltool.h b/src/pixeltool/qpixeltool.h
index beb786fd2..3fa9f1e7e 100644
--- a/src/pixeltool/qpixeltool.h
+++ b/src/pixeltool/qpixeltool.h
@@ -53,6 +53,7 @@ public slots:
     void setZoomVisible(bool visible);
 #if QT_CONFIG(clipboard)
     void copyToClipboard();
+    void copyColorToClipboard();
 #endif
     void saveToFile();
     void increaseGridSize() { setGridSize(m_gridSize + 1); }
-- 
GitLab