IntelliPhoto 0.7

IntelliToolPolygon.cpp
Go to the documentation of this file.
1 #include "IntelliToolPolygon.h"
2 #include "Layer/PaintingArea.h"
3 #include <QCursor>
4 #include <QInputDialog>
5 #include <QDebug>
6 #include <cmath>
7
8 IntelliToolPolygon::IntelliToolPolygon(PaintingArea* Area, IntelliColorPicker* colorPicker, IntelliToolsettings* Toolsettings, bool isSettingPolygon)
9 : IntelliTool(Area, colorPicker, Toolsettings){
10 isPointNearStart = false;
11 drawingOfPolygon = false;
12 isInside = false;
13 this->isSettingPolygon = isSettingPolygon;
14 if(isSettingPolygon) {
15 Toolsettings->setLineWidth(5);
16 }
17 this->ActiveType = Tooltype::POLYGON;
18 }
19
20 IntelliToolPolygon::~IntelliToolPolygon(){
21 if(drawingOfPolygon) {
22 IntelliTool::onMouseRightPressed(0,0);
23 }
24 }
25
26 void IntelliToolPolygon::onMouseLeftPressed(int x, int y){
27 if(!drawingOfPolygon && Area->getTypeOfImageActiveLayer() == ImageType::SHAPEDIMAGE && x > 0 && y > 0 && x<Area->getWidthOfActive() && y<Area->getHeightOfActive()) {
28 if(Area->getPolygonDataOfActiveLayer().size()>2) {
29 std::vector<Triangle> Triangles = IntelliTriangulation::calculateTriangles(Area->getPolygonDataOfActiveLayer());
30 QPoint Point(x,y);
31 isInside = IntelliTriangulation::isInPolygon(Triangles,Point);
32 }
33 else{
34 isInside = true;
35 }
36 if(isSettingPolygon) {
37 isInside = true;
38 }
39 }
40 else if(!drawingOfPolygon && Area->getTypeOfImageActiveLayer() == ImageType::RASTERIMAGE && x >= 0 && y >= 0 && x<Area->getWidthOfActive() && y<Area->getHeightOfActive()) {
41 isInside = true;
42 }
43
44 if(isInside && !drawingOfPolygon) {
45 IntelliTool::onMouseLeftPressed(x,y);
46 QPoint drawingPoint = QPoint(x,y);
47
48 drawingOfPolygon = true;
49 QPointList.push_back(drawingPoint);
50
51 this->Canvas->image->drawPoint(QPointList.back(), colorPicker->getFirstColor(), Toolsettings->getLineWidth());
52 if(!isSettingPolygon) {
53 this->Canvas->image->calculateVisiblity();
54 }
55 }
56 else if(drawingOfPolygon && QPointList.size() > 0 && isNearStart(x,y,QPointList.front())) {
57 if(QPointList.size() > 2) {
58 isPointNearStart = true;
59 this->Canvas->image->drawLine(QPointList.back(), QPointList.front(), colorPicker->getFirstColor(), Toolsettings->getLineWidth());
60 if(!isSettingPolygon) {
61 this->Canvas->image->calculateVisiblity();
62 }
63 }
64 else{
65 isInside = false;
66 drawingOfPolygon = false;
67 QPointList.clear();
68 IntelliTool::onMouseRightPressed(x,y);
69 IntelliTool::onMouseRightReleased(x,y);
70 }
71
72 }
73 else if(drawingOfPolygon) {
74 QPoint drawingPoint(x,y);
75 QPointList.push_back(drawingPoint);
76 this->Canvas->image->drawLine(QPointList[QPointList.size() - 2], QPointList[QPointList.size() - 1], colorPicker->getFirstColor(), Toolsettings->getLineWidth());
77 if(!isSettingPolygon) {
78 this->Canvas->image->calculateVisiblity();
79 }
80 }
81 }
82
83 void IntelliToolPolygon::onMouseRightPressed(int x, int y){
84 drawingOfPolygon = false;
85 isInside = false;
86 isPointNearStart = false;
87 QPointList.clear();
88 IntelliTool::onMouseRightPressed(x,y);
89 }
90
91 void IntelliToolPolygon::onMouseLeftReleased(int x, int y){
92 if(isPointNearStart) {
93 isInside = false;
94 isPointNearStart = false;
95 drawingOfPolygon = false;
96 if(!isSettingPolygon) {
97 std::vector<Triangle> Triangles = IntelliTriangulation::calculateTriangles(QPointList);
98 QPoint Point;
99 QColor colorTwo(colorPicker->getSecondColor());
100 colorTwo.setAlpha(Toolsettings->getInnerAlpha());
101 for(int i = 0; i < activeLayer->width; i++) {
102 for(int j = 0; j < activeLayer->height; j++) {
103 Point = QPoint(i,j);
104 if(IntelliTriangulation::isInPolygon(Triangles,Point)) {
105 this->Canvas->image->drawPixel(Point, colorTwo);
106 }
107 }
108 }
109 for(int i = 0; i<static_cast<int>(QPointList.size()); i++) {
110 int next = static_cast<int>((i + static_cast<int>(1)) % static_cast<int>(QPointList.size()));
111 this->Canvas->image->drawLine(QPointList[static_cast<unsigned long long>(i)], QPointList[static_cast<unsigned long long>(next)], colorPicker->getFirstColor(), Toolsettings->getLineWidth());
112 }
113
114 }
115 else{
116 Canvas->image->setPolygon(QPointList);
117 Canvas->image->setImageData(Area->getImageDataOfActiveLayer());
118 }
119 IntelliTool::onMouseLeftReleased(x,y);
120 QPointList.clear();
121 }
122 }
123
124 void IntelliToolPolygon::onMouseRightReleased(int x, int y){
125 IntelliTool::onMouseRightReleased(x,y);
126 }
127
128 void IntelliToolPolygon::onWheelScrolled(int value){
129 if(!isSettingPolygon) {
130 IntelliTool::onWheelScrolled(value);
131 if(!isDrawing) {
132 Toolsettings->setLineWidth(Toolsettings->getLineWidth() + value);
133 }
134 }
135 }
136
137 void IntelliToolPolygon::onMouseMoved(int x, int y){
138 if(!isSettingPolygon) {
139 IntelliTool::onMouseMoved(x,y);
140 }
141 }
142
143 bool IntelliToolPolygon::isNearStart(int x, int y, QPoint Startpoint){
144 int StartX = Startpoint.x();
145 int StartY = Startpoint.y();
146 int valueToNear = 5;
147
148 float euklid = sqrt(pow(static_cast<float>(StartX - x),2.f) + pow(static_cast<float>(StartY - y),2.f));
149
150 return static_cast<int>(euklid)<valueToNear;
151 }
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
IntelliColorPicker::getFirstColor
QColor getFirstColor() const
A function to read the primary selected color.
Definition: IntelliColorPicker.cpp:15
ImageType::RASTERIMAGE
@ RASTERIMAGE
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
IntelliToolsettings::getLineWidth
int getLineWidth() const
getLineWidth returns the width attribute of the line.
Definition: IntelliToolsettings.cpp:14
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
IntelliToolPolygon.h
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
IntelliTool::Toolsettings
IntelliToolsettings * Toolsettings
Toolsettings a refrence to the tool settings.
Definition: IntelliTool.h:69
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
PaintingArea::getPolygonDataOfActiveLayer
std::vector< QPoint > getPolygonDataOfActiveLayer()
getPolygonDataOfActiveLayer get the polygon data of the active Layer.
Definition: PaintingArea.cpp:302
IntelliTriangulation::calculateTriangles
std::vector< Triangle > calculateTriangles(std::vector< QPoint > polyPoints)
A function to split a polygon in its spanning traingles by using Meisters Theorem of graph theory by ...
Definition: IntelliTriangulation.cpp:7
IntelliToolsettings::getInnerAlpha
int getInnerAlpha() const
getInnerAlpha returns the inner alpha value.
Definition: IntelliToolsettings.cpp:28
IntelliTool::Area
PaintingArea * Area
A pointer to the general PaintingArea to interact with.
Definition: IntelliTool.h:54
IntelliToolPolygon::onMouseMoved
virtual void onMouseMoved(int x, int y) override
A function managing the mouse moved event.
Definition: IntelliToolPolygon.cpp:137
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
IntelliColorPicker::getSecondColor
QColor getSecondColor() const
A function to read the secondary selected color.
Definition: IntelliColorPicker.cpp:19
IntelliImage::drawPoint
virtual void drawPoint(const QPoint &p1, const QColor &color, const int &penWidth)
A function that draws a point between on a given point in a given color.
Definition: IntelliImage.cpp:78
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
IntelliToolPolygon::IntelliToolPolygon
IntelliToolPolygon(PaintingArea *Area, IntelliColorPicker *colorPicker, IntelliToolsettings *Toolsettings, bool isSettingPolygon=false)
A constructor setting the general paintingArea and colorPicker.
Definition: IntelliToolPolygon.cpp:8
IntelliTool::Tooltype::POLYGON
@ POLYGON
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
IntelliTool
An abstract class that manages the basic events, like mouse clicks or scrolls events.
Definition: IntelliTool.h:17
IntelliImage::setImageData
virtual void setImageData(const QImage &newData)
setImageData overwrites the old imageData the new imageData.
Definition: IntelliImage.cpp:143
ImageType::SHAPEDIMAGE
@ SHAPEDIMAGE
PaintingArea::getTypeOfImageActiveLayer
ImageType getTypeOfImageActiveLayer()
getTypeOfImageActiveLayer get the type of the active Layer.
Definition: PaintingArea.cpp:298
IntelliToolPolygon::onMouseRightPressed
virtual void onMouseRightPressed(int x, int y) override
A function managing the right click pressed of a mouse. Resetting the current fill.
Definition: IntelliToolPolygon.cpp:83
PaintingArea.h
LayerObject::height
int height
height - Stores the height of a layer in pixels.
Definition: PaintingArea.h:35
PaintingArea::getImageDataOfActiveLayer
QImage getImageDataOfActiveLayer()
getImageDataOfActiveLayer used to get the currents active imageData (if there isn't any active layer ...
Definition: PaintingArea.cpp:476
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
IntelliToolPolygon::~IntelliToolPolygon
~IntelliToolPolygon() override
A Destructor.
Definition: IntelliToolPolygon.cpp:20
LayerObject::image
IntelliImage * image
image - Stores the imageData of the current LayerObject.
Definition: PaintingArea.h:27
IntelliToolsettings::setLineWidth
void setLineWidth(int LineWidth)
setLineWidth sets the width attribute of the line.
Definition: IntelliToolsettings.cpp:18
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
IntelliToolPolygon::onMouseLeftPressed
virtual void onMouseLeftPressed(int x, int y) override
A function managing the left click pressed of a mouse. Setting polygon points.
Definition: IntelliToolPolygon.cpp:26
IntelliToolPolygon::onMouseRightReleased
virtual void onMouseRightReleased(int x, int y) override
A function managing the right click released of a mouse.
Definition: IntelliToolPolygon.cpp:124
IntelliToolPolygon::onMouseLeftReleased
virtual void onMouseLeftReleased(int x, int y) override
A function managing the left click released of a mouse. Merging the fill to the active layer.
Definition: IntelliToolPolygon.cpp:91
IntelliImage::calculateVisiblity
virtual void calculateVisiblity()=0
An abstract function that calculates the visiblity of the Image data if needed.
IntelliTriangulation::isInPolygon
bool isInPolygon(const std::vector< Triangle > &triangles, QPoint &point)
A function to check if a point lies in a polygon by checking its spanning triangles.
Definition: IntelliTriangulation.cpp:116
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
IntelliToolPolygon::onWheelScrolled
virtual void onWheelScrolled(int value) override
A function managing the scroll event. CHanging the lineWidth relative to value.
Definition: IntelliToolPolygon.cpp:128
IntelliImage::setPolygon
virtual void setPolygon(const std::vector< QPoint > &polygonData)=0
An abstract function that sets the data of the visible Polygon, if needed.