Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
eyedroppertool.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "eyedroppertool.h"
19 #include <QPainter>
20 #include <QPixmap>
21 #include <QBitmap>
22 
23 #include "pencilsettings.h"
24 #include "layer.h"
25 #include "layervector.h"
26 #include "layerbitmap.h"
27 #include "colormanager.h"
28 #include "object.h"
29 #include "editor.h"
30 #include "layermanager.h"
31 #include "scribblearea.h"
32 
33 
34 EyedropperTool::EyedropperTool(QObject *parent) :
35  BaseTool(parent)
36 {
37 }
38 
39 void EyedropperTool::loadSettings()
40 {
41  properties.width = -1;
42  properties.feather = -1;
43  properties.useFeather = false;
44  properties.useAA = -1;
45 }
46 
47 QCursor EyedropperTool::cursor()
48 {
49  if ( mEditor->preference()->isOn( SETTING::TOOL_CURSOR ) )
50  {
51  return QCursor(QPixmap(":icons/eyedropper.png"), 0, 15);
52  }
53  else
54  {
55  return Qt::CrossCursor;
56  }
57 }
58 
59 QCursor EyedropperTool::cursor(const QColor colour)
60 {
61  QPixmap icon(":icons/eyedropper.png");
62 
63  QPixmap pixmap(32, 32);
64  pixmap.fill(Qt::transparent);
65 
66  QPainter painter(&pixmap);
67  painter.drawPixmap(0, 0, icon);
68  painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
69  painter.setBrush(colour);
70  painter.drawRect(16, 16, 15, 15);
71  painter.end();
72 
73  return QCursor(pixmap, 0, 15);
74 }
75 
76 void EyedropperTool::mousePressEvent(QMouseEvent *event)
77 {
78  Q_UNUSED(event);
79 }
80 
81 void EyedropperTool::mouseReleaseEvent(QMouseEvent *event)
82 {
83  Layer* layer = mEditor->layers()->currentLayer();
84  if (layer == NULL) { return; }
85 
86  if (event->button() == Qt::LeftButton)
87  {
88  if (layer->type() == Layer::BITMAP)
89  {
90  BitmapImage* targetImage = ((LayerBitmap *)layer)->getLastBitmapImageAtFrame( mEditor->currentFrame(), 0);
91  //QColor pickedColour = targetImage->pixel(getLastPoint().x(), getLastPoint().y());
92  QColor pickedColour;
93  pickedColour.setRgba( targetImage->pixel( getLastPoint().x(), getLastPoint().y() ) );
94  int transp = 255 - pickedColour.alpha();
95  pickedColour.setRed( pickedColour.red() + transp );
96  pickedColour.setGreen( pickedColour.green() + transp );
97  pickedColour.setBlue( pickedColour.blue() + transp );
98  if (pickedColour.alpha() != 0)
99  {
100  mEditor->color()->setColor(pickedColour);
101  }
102  }
103  else if (layer->type() == Layer::VECTOR)
104  {
105  VectorImage *vectorImage = ((LayerVector *)layer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
106  int colourNumber = vectorImage->getColourNumber(getLastPoint());
107  if (colourNumber != -1)
108  {
109  mEditor->color()->setColorNumber(colourNumber);
110  }
111  }
112  }
113 }
114 
115 void EyedropperTool::mouseMoveEvent(QMouseEvent *event)
116 {
117  Q_UNUSED(event);
118 
119  Layer* layer = mEditor->layers()->currentLayer();
120  if (layer == NULL) { return; }
121 
122  if (layer->type() == Layer::BITMAP)
123  {
124  BitmapImage *targetImage = ((LayerBitmap *)layer)->getLastBitmapImageAtFrame(mEditor->currentFrame(), 0);
125  if (targetImage->contains(getCurrentPoint()))
126  {
127  QColor pickedColour;
128  //pickedColour.setRgba(targetImage->pixel(getCurrentPoint().x(), getCurrentPoint().y()));
129  pickedColour.setRgba( targetImage->pixel( getCurrentPoint().x(), getCurrentPoint().y() ) );
130  int transp = 255 - pickedColour.alpha();
131  pickedColour.setRed( pickedColour.red() + transp );
132  pickedColour.setGreen( pickedColour.green() + transp );
133  pickedColour.setBlue( pickedColour.blue() + transp );
134  if (pickedColour.alpha() != 0)
135  {
136  mScribbleArea->setCursor(cursor(pickedColour));
137  }
138  else
139  {
140  mScribbleArea->setCursor(cursor());
141  }
142  }
143  else
144  {
145  mScribbleArea->setCursor(cursor());
146  }
147  }
148  if (layer->type() == Layer::VECTOR)
149  {
150  VectorImage *vectorImage = ((LayerVector *)layer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
151  int colourNumber = vectorImage->getColourNumber(getCurrentPoint());
152  if (colourNumber != -1)
153  {
154  mScribbleArea->setCursor(cursor(mEditor->object()->getColour(colourNumber).colour));
155  }
156  else
157  {
158  mScribbleArea->setCursor(cursor());
159  }
160  }
161 }
Definition: layer.h:32