IntelliPhoto 0.7

IntelliToolGradient.cpp
Go to the documentation of this file.
1 #include "IntelliToolGradient.h"
2 #include "Layer/PaintingArea.h"
3 #include "math.h"
4 #include <QDebug>
5
6 IntelliToolGradient::IntelliToolGradient(PaintingArea* Area, IntelliColorPicker* colorPicker, IntelliToolsettings* Toolsettings)
7 : IntelliTool(Area, colorPicker, Toolsettings){
8 this->ActiveType = Tooltype::GRADIENT;
9 this->LineColor = QColor(0,0,0,255);
10 this->hasMoved = false;
11 }
12
13 IntelliToolGradient::~IntelliToolGradient(){
14 IntelliTool::onMouseRightPressed(0,0);
15 }
16
17 void IntelliToolGradient::onMouseLeftPressed(int x, int y){
18 IntelliTool::onMouseLeftPressed(x,y);
19 doubleStartPoint[0] = static_cast<double>(x);
20 doubleStartPoint[1] = static_cast<double>(y);
21 startPoint = QPoint(x,y);
22 endPoint = QPoint(x,y);
23 VectorStartEnd[0] = 0;
24 VectorStartEnd[1] = 0;
25 Canvas->image->drawPixel(startPoint,LineColor);
26 }
27
28 void IntelliToolGradient::onMouseRightPressed(int x, int y){
29 IntelliTool::onMouseRightPressed(x,y);
30 }
31
32 void IntelliToolGradient::onMouseLeftReleased(int x, int y){
33 if(hasMoved && this->isDrawing) {
34 computeGradientLayer();
35 IntelliTool::onMouseLeftReleased(x,y);
36 }
37 }
38
39 void IntelliToolGradient::onMouseRightReleased(int x, int y){
40 IntelliTool::onMouseRightReleased(x,y);
41 }
42
43 void IntelliToolGradient::onMouseMoved(int x, int y){
44 if(this->isDrawing) {
45 hasMoved = true;
46 endPoint = QPoint(x,y);
47 VectorStartEnd[0] = static_cast<float>(endPoint.x() - startPoint.x());
48 VectorStartEnd[1] = static_cast<float>(endPoint.y() - startPoint.y());
49 NormalVector[0] = VectorStartEnd[1];
50 NormalVector[1] = (-1 * VectorStartEnd[0]);
51 NormalDotNormal = dotProduct(NormalVector,NormalVector);
52 this->Canvas->image->drawPlain(Qt::transparent);
53 computeGradientLayer();
54 Canvas->image->drawLine(startPoint,endPoint,LineColor,1);
55 }
56 IntelliTool::onMouseMoved(x,y);
57 }
58
59 void IntelliToolGradient::onWheelScrolled(int value){
60 IntelliTool::onWheelScrolled(value);
61 }
62
63 void IntelliToolGradient::computeAndDrawPixelColor(QPoint Point){
64 double doublePoint[2];
65 doublePoint[0] = static_cast<double>(Point.x());
66 doublePoint[1] = static_cast<double>(Point.y());
67 double doublePointSubA[2];
68 doublePointSubA[0] = doublePoint[0] - doubleStartPoint[0];
69 doublePointSubA[1] = doublePoint[1] - doubleStartPoint[1];
70 double Perpendicular[2];
71 double PointSubADotNormal = dotProduct(doublePointSubA,NormalVector);
72 Perpendicular[0] = doublePoint[0] - (PointSubADotNormal / NormalDotNormal) * NormalVector[0];
73 Perpendicular[1] = doublePoint[1] - (PointSubADotNormal / NormalDotNormal) * NormalVector[1];
74 double VectorAPoint[2];
75 VectorAPoint[0] = static_cast<double>(Perpendicular[0] - doubleStartPoint[0]);
76 VectorAPoint[1] = static_cast<double>(Perpendicular[1] - doubleStartPoint[1]);
77 double ratio;
78 if(((VectorAPoint[0] < 0 && VectorStartEnd[0] < 0) || (VectorAPoint[0] > 0 && VectorStartEnd[0] > 0)) && ((VectorAPoint[1] < 0 && VectorStartEnd[1] < 0) || (VectorAPoint[1] > 0 && VectorStartEnd[1] > 0)))
79 ratio = lenghtVector(VectorAPoint) / lenghtVector(VectorStartEnd);
80 else{
81 ratio = -1;
82 }
83 QColor computedColor;
84 if(ratio < 0) {
85 computedColor = colorPicker->getFirstColor();
86 }
87 else if(ratio > 1) {
88 computedColor = colorPicker->getSecondColor();
89 }
90 else{
91 int red;
92 int green;
93 int blue;
94 int alpha;
95 int red2;
96 int green2;
97 int blue2;
98 int alpha2;
99 colorPicker->getFirstColor().getRgb(&red,&green,&blue,&alpha);
100 colorPicker->getSecondColor().getRgb(&red2,&green2,&blue2,&alpha2);
101 computedColor.setRed(static_cast<int>(ratio * red2 + (1 - ratio) * red));
102 computedColor.setGreen(static_cast<int>(ratio * green2 + (1 - ratio) * green));
103 computedColor.setBlue(static_cast<int>(ratio * blue2 + (1 - ratio) * blue));
104 computedColor.setAlpha(static_cast<int>(ratio * alpha2 + (1 - ratio) * alpha));
105 }
106 Canvas->image->drawPixel(Point,computedColor);
107 }
108
109 double IntelliToolGradient::dotProduct(double Vector1[2], double Vector2[2]){
110 return static_cast<double>(Vector1[0] * Vector2[0] + Vector1[1] * Vector2[1]);
111 }
112
113 double IntelliToolGradient::lenghtVector(double Vector[2]){
114 return static_cast<double>((sqrt(Vector[0] * Vector[0] + Vector[1] * Vector[1])));
115 }
116
117 void IntelliToolGradient::computeGradientLayer(){
118 bool switched = false;
119 if(Canvas->image->isFastRendering()) {
120 switched = true;
121 Canvas->image->updateRendererSetting(false);
122 }
123 for(int i = 0; i < activeLayer->height; i++) {
124 for(int j = 0; j < activeLayer->width; j++) {
125 computeAndDrawPixelColor(QPoint(j,i));
126 }
127 }
128 if(switched) {
129 Canvas->image->updateRendererSetting(true);
130 }
131 }
IntelliToolGradient::onMouseLeftReleased
virtual void onMouseLeftReleased(int x, int y) override
A function managing the left click Released of a Mouse. Call this in child classes!
Definition: IntelliToolGradient.cpp:32
IntelliTool::onMouseRightPressed
virtual void onMouseRightPressed(int x, int y)
A function managing the right click Pressed of a Mouse. Constructing the Canvas to draw on....
Definition: IntelliTool.cpp:19
IntelliToolGradient::onMouseRightReleased
virtual void onMouseRightReleased(int x, int y) override
A function managing the right click Released of a Mouse. Merging the Canvas to Active....
Definition: IntelliToolGradient.cpp:39
IntelliColorPicker::getFirstColor
QColor getFirstColor() const
A function to read the primary selected color.
Definition: IntelliColorPicker.cpp:15
IntelliTool::onMouseLeftReleased
virtual void onMouseLeftReleased(int x, int y)
A function managing the left click Released of a Mouse. Call this in child classes!
Definition: IntelliTool.cpp:37
IntelliTool::colorPicker
IntelliColorPicker * colorPicker
A pointer to the IntelliColorPicker of the PaintingArea to interact with, and get the colors.
Definition: IntelliTool.h:64
IntelliImage::drawLine
virtual void drawLine(const QPoint &p1, const QPoint &p2, const QColor &color, const int &penWidth)
A function that draws a line between two given points in a given color.
Definition: IntelliImage.cpp:97
IntelliTool::onMouseLeftPressed
virtual void onMouseLeftPressed(int x, int y)
A function managing the left click Pressed of a Mouse. Resetting the current draw....
Definition: IntelliTool.cpp:30
IntelliImage::drawPixel
virtual void drawPixel(const QPoint &p1, const QColor &color)
A funtcion used to draw a pixel on the Image with the given Color.
Definition: IntelliImage.cpp:58
IntelliImage::isFastRendering
virtual bool isFastRendering() const
isFastRendering returns if the Image is in fast rendering mode.
Definition: IntelliImage.cpp:171
IntelliToolGradient::~IntelliToolGradient
virtual ~IntelliToolGradient() override
~IntelliToolGradient basic destructor.
Definition: IntelliToolGradient.cpp:13
IntelliToolGradient::onMouseRightPressed
virtual void onMouseRightPressed(int x, int y) override
A function managing the right click Pressed of a Mouse. Constructing the Canvas to draw on....
Definition: IntelliToolGradient.cpp:28
PaintingArea
The PaintingArea class manages the methods and stores information about the current painting area,...
Definition: PaintingArea.h:57
IntelliTool::isDrawing
bool isDrawing
A flag checking if the user is currently drawing or not.
Definition: IntelliTool.h:84
IntelliToolGradient::onWheelScrolled
virtual void onWheelScrolled(int value) override
A function managing the scroll event. A positive value means scrolling outwards. Call this in child c...
Definition: IntelliToolGradient.cpp:59
IntelliColorPicker::getSecondColor
QColor getSecondColor() const
A function to read the secondary selected color.
Definition: IntelliColorPicker.cpp:19
IntelliToolGradient::IntelliToolGradient
IntelliToolGradient(PaintingArea *Area, IntelliColorPicker *colorPicker, IntelliToolsettings *Toolsettings)
IntelliToolGradient basic constructor of the gradient tool.
Definition: IntelliToolGradient.cpp:6
IntelliTool::onMouseRightReleased
virtual void onMouseRightReleased(int x, int y)
A function managing the right click Released of a Mouse. Merging the Canvas to Active....
Definition: IntelliTool.cpp:26
IntelliTool::activeLayer
LayerObject * activeLayer
A pointer to the underlying active Layer, do not work on this. This is used for data grabbing or prev...
Definition: IntelliTool.h:74
IntelliImage::updateRendererSetting
virtual void updateRendererSetting(bool fastRendererOn)
updateRendererSetting updates the existing image format to the new format.
Definition: IntelliImage.cpp:153
IntelliTool::Canvas
LayerObject * Canvas
A pointer to the drawing canvas of the tool, work on this.
Definition: IntelliTool.h:79
LayerObject::width
int width
width - Stores the width of a layer in pixels.
Definition: PaintingArea.h:31
IntelliToolGradient::onMouseLeftPressed
virtual void onMouseLeftPressed(int x, int y) override
A function managing the left click Pressed of a Mouse. Resetting the current draw....
Definition: IntelliToolGradient.cpp:17
IntelliTool
An abstract class that manages the basic events, like mouse clicks or scrolls events.
Definition: IntelliTool.h:17
IntelliToolGradient::onMouseMoved
virtual void onMouseMoved(int x, int y) override
A function managing the mouse moved event. Call this in child classes!
Definition: IntelliToolGradient.cpp:43
PaintingArea.h
LayerObject::height
int height
height - Stores the height of a layer in pixels.
Definition: PaintingArea.h:35
IntelliTool::ActiveType
Tooltype ActiveType
ActiveType the type of the active tool.
Definition: IntelliTool.h:59
IntelliColorPicker
The IntelliColorPicker manages the selected colors for one whole project.
Definition: IntelliColorPicker.h:14
LayerObject::image
IntelliImage * image
image - Stores the imageData of the current LayerObject.
Definition: PaintingArea.h:27
IntelliTool::Tooltype::GRADIENT
@ GRADIENT
IntelliTool::onMouseMoved
virtual void onMouseMoved(int x, int y)
A function managing the mouse moved event. Call this in child classes!
Definition: IntelliTool.cpp:47
IntelliTool::onWheelScrolled
virtual void onWheelScrolled(int value)
A function managing the scroll event. A positive value means scrolling outwards. Call this in child c...
Definition: IntelliTool.cpp:52
IntelliToolsettings
The IntelliToolsettings class managing the settings of the tools, independent of an existing tool.
Definition: IntelliToolsettings.h:9
IntelliToolGradient.h
IntelliImage::drawPlain
virtual void drawPlain(const QColor &color)
A function that clears the whole image in a given Color.
Definition: IntelliImage.cpp:117