IntelliToolPolygon.cpp
Go to the documentation of this file.
#include "IntelliToolPolygon.h"
#include "Layer/PaintingArea.h"
#include <QCursor>
#include <QInputDialog>
#include <QDebug>
#include <cmath>
IntelliToolPolygon::IntelliToolPolygon(PaintingArea* Area, IntelliColorPicker* colorPicker, IntelliToolsettings* Toolsettings, bool isSettingPolygon)
: IntelliTool(Area, colorPicker, Toolsettings){
isPointNearStart = false;
drawingOfPolygon = false;
isInside = false;
this->isSettingPolygon = isSettingPolygon;
if(isSettingPolygon) {
Toolsettings->setLineWidth(5);
}
this->ActiveType = Tooltype::POLYGON;
}
IntelliToolPolygon::~IntelliToolPolygon(){
if(drawingOfPolygon) {
IntelliTool::onMouseRightPressed(0,0);
}
}
void IntelliToolPolygon::onMouseLeftPressed(int x, int y){
if(!drawingOfPolygon && Area->getTypeOfImageActiveLayer() == ImageType::SHAPEDIMAGE && x > 0 && y > 0 && x<Area->getWidthOfActive() && y<Area->getHeightOfActive()) {
if(Area->getPolygonDataOfActiveLayer().size()>2) {
std::vector<Triangle> Triangles = IntelliTriangulation::calculateTriangles(Area->getPolygonDataOfActiveLayer());
QPoint Point(x,y);
isInside = IntelliTriangulation::isInPolygon(Triangles,Point);
}
else{
isInside = true;
}
if(isSettingPolygon) {
isInside = true;
}
}
else if(!drawingOfPolygon && Area->getTypeOfImageActiveLayer() == ImageType::RASTERIMAGE && x >= 0 && y >= 0 && x<Area->getWidthOfActive() && y<Area->getHeightOfActive()) {
isInside = true;
}
if(isInside && !drawingOfPolygon) {
IntelliTool::onMouseLeftPressed(x,y);
QPoint drawingPoint = QPoint(x,y);
drawingOfPolygon = true;
QPointList.push_back(drawingPoint);
this->Canvas->image->drawPoint(QPointList.back(), colorPicker->getFirstColor(), Toolsettings->getLineWidth());
if(!isSettingPolygon) {
this->Canvas->image->calculateVisiblity();
}
}
else if(drawingOfPolygon && QPointList.size() > 0 && isNearStart(x,y,QPointList.front())) {
if(QPointList.size() > 2) {
isPointNearStart = true;
this->Canvas->image->drawLine(QPointList.back(), QPointList.front(), colorPicker->getFirstColor(), Toolsettings->getLineWidth());
if(!isSettingPolygon) {
this->Canvas->image->calculateVisiblity();
}
}
else{
isInside = false;
drawingOfPolygon = false;
QPointList.clear();
IntelliTool::onMouseRightPressed(x,y);
IntelliTool::onMouseRightReleased(x,y);
}
}
else if(drawingOfPolygon) {
QPoint drawingPoint(x,y);
QPointList.push_back(drawingPoint);
this->Canvas->image->drawLine(QPointList[QPointList.size() - 2], QPointList[QPointList.size() - 1], colorPicker->getFirstColor(), Toolsettings->getLineWidth());
if(!isSettingPolygon) {
this->Canvas->image->calculateVisiblity();
}
}
}
void IntelliToolPolygon::onMouseRightPressed(int x, int y){
drawingOfPolygon = false;
isInside = false;
isPointNearStart = false;
QPointList.clear();
IntelliTool::onMouseRightPressed(x,y);
}
void IntelliToolPolygon::onMouseLeftReleased(int x, int y){
if(isPointNearStart) {
isInside = false;
isPointNearStart = false;
drawingOfPolygon = false;
if(!isSettingPolygon) {
std::vector<Triangle> Triangles = IntelliTriangulation::calculateTriangles(QPointList);
QPoint Point;
QColor colorTwo(colorPicker->getSecondColor());
colorTwo.setAlpha(Toolsettings->getInnerAlpha());
for(int i = 0; i < activeLayer->width; i++) {
for(int j = 0; j < activeLayer->height; j++) {
Point = QPoint(i,j);
if(IntelliTriangulation::isInPolygon(Triangles,Point)) {
this->Canvas->image->drawPixel(Point, colorTwo);
}
}
}
for(int i = 0; i<static_cast<int>(QPointList.size()); i++) {
int next = static_cast<int>((i + static_cast<int>(1)) % static_cast<int>(QPointList.size()));
this->Canvas->image->drawLine(QPointList[static_cast<unsigned long long>(i)], QPointList[static_cast<unsigned long long>(next)], colorPicker->getFirstColor(), Toolsettings->getLineWidth());
}
}
else{
Canvas->image->setPolygon(QPointList);
Canvas->image->setImageData(Area->getImageDataOfActiveLayer());
}
IntelliTool::onMouseLeftReleased(x,y);
QPointList.clear();
}
}
void IntelliToolPolygon::onMouseRightReleased(int x, int y){
IntelliTool::onMouseRightReleased(x,y);
}
void IntelliToolPolygon::onWheelScrolled(int value){
if(!isSettingPolygon) {
IntelliTool::onWheelScrolled(value);
if(!isDrawing) {
Toolsettings->setLineWidth(Toolsettings->getLineWidth() + value);
}
}
}
void IntelliToolPolygon::onMouseMoved(int x, int y){
if(!isSettingPolygon) {
IntelliTool::onMouseMoved(x,y);
}
}
bool IntelliToolPolygon::isNearStart(int x, int y, QPoint Startpoint){
int StartX = Startpoint.x();
int StartY = Startpoint.y();
int valueToNear = 5;
float euklid = sqrt(pow(static_cast<float>(StartX - x),2.f) + pow(static_cast<float>(StartY - y),2.f));
return static_cast<int>(euklid)<valueToNear;
}
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:48
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:53
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.