diff --git a/.gitignore b/.gitignore index e431211..e8e6a7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Build folders -/build-*/ +build-*/ # QT Creator Files *.creator.user* diff --git a/src/GUI/IntelliPhotoGui.cpp b/src/GUI/IntelliPhotoGui.cpp index ce6af70..2969434 100644 --- a/src/GUI/IntelliPhotoGui.cpp +++ b/src/GUI/IntelliPhotoGui.cpp @@ -17,7 +17,6 @@ IntelliPhotoGui::IntelliPhotoGui(){ createMenus(); //set style of the gui setIntelliStyle(); - // Size the app resize(600,600); showMaximized(); @@ -31,7 +30,6 @@ void IntelliPhotoGui::closeEvent(QCloseEvent *event){ if (maybeSave()) { event->accept(); } else { - // If there have been changes ignore the event event->ignore(); } @@ -48,7 +46,7 @@ void IntelliPhotoGui::slotOpen(){ // tr sets the window title to Open File // QDir opens the current dirctory QString fileName = QFileDialog::getOpenFileName(this, - tr("Open File"), QDir::currentPath()); + tr("Open File"), QDir::currentPath(), nullptr, nullptr, QFileDialog::DontUseNativeDialog); // If we have a file name load the image and place // it in the paintingArea @@ -229,7 +227,7 @@ void IntelliPhotoGui::slotCreateLineTool(){ void IntelliPhotoGui::slotAboutDialog(){ // Window title and text to display QMessageBox::about(this, tr("About Painting"), - tr("

IntelliPhoto Some nice ass looking software

")); + tr("

IntelliPhotoPretty basic editor.

")); } // Define menu actions that call functions @@ -271,6 +269,7 @@ void IntelliPhotoGui::createActions(){ // Create New Layer action and tie to IntelliPhotoGui::newLayer() actionCreateNewLayer = new QAction(tr("&New Layer..."), this); + actionCreateNewLayer->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); connect(actionCreateNewLayer, SIGNAL(triggered()), this, SLOT(slotCreateNewLayer())); // Delete New Layer action and tie to IntelliPhotoGui::deleteLayer() @@ -457,7 +456,7 @@ bool IntelliPhotoGui::saveFile(const QByteArray &fileFormat){ initialPath, tr("%1 Files (*.%2);;All Files (*)") .arg(QString::fromLatin1(fileFormat.toUpper())) - .arg(QString::fromLatin1(fileFormat))); + .arg(QString::fromLatin1(fileFormat)), nullptr, QFileDialog::DontUseNativeDialog); // If no file do nothing if (fileName.isEmpty()) { diff --git a/src/Image/IntelliShapedImage.cpp b/src/Image/IntelliShapedImage.cpp index 0dbc807..dbf6e48 100644 --- a/src/Image/IntelliShapedImage.cpp +++ b/src/Image/IntelliShapedImage.cpp @@ -35,26 +35,18 @@ void IntelliShapedImage::calculateVisiblity(){ } return; } - QPoint A = polygonData[0]; QColor clr; for(int y=0; y(polygonData.size()-1); i++){ - QPoint B = polygonData[static_cast(i)]; - QPoint C = polygonData[static_cast(i+1)]; - QPoint P(x,y); - cutNumeber+=IntelliHelper::isInTriangle(A,B,C,P); - } - if(cutNumeber%2==0){ - clr = imageData.pixelColor(x,y); - clr.setAlpha(0); - imageData.setPixelColor(x,y,clr); - }else{ - clr = imageData.pixelColor(x,y); + QPoint ptr(x,y); + clr = imageData.pixelColor(x,y); + bool isInPolygon = IntelliHelper::isInPolygon(triangles, ptr); + if(isInPolygon){ clr.setAlpha(std::min(255, clr.alpha())); - imageData.setPixelColor(x,y,clr); + }else{ + clr.setAlpha(0); } + imageData.setPixelColor(x,y,clr); } } } @@ -79,6 +71,7 @@ void IntelliShapedImage::setPolygon(const std::vector& polygonData){ for(auto element:polygonData){ this->polygonData.push_back(QPoint(element.x(), element.y())); } + triangles = IntelliHelper::calculateTriangles(polygonData); } calculateVisiblity(); return; diff --git a/src/Image/IntelliShapedImage.h b/src/Image/IntelliShapedImage.h index 0a3598e..9e9518f 100644 --- a/src/Image/IntelliShapedImage.h +++ b/src/Image/IntelliShapedImage.h @@ -2,9 +2,13 @@ #define INTELLISHAPE_H #include"Image/IntelliRasterImage.h" +#include +#include"IntelliHelper/IntelliHelper.h" class IntelliShapedImage : public IntelliRasterImage{ friend IntelliTool; +private: + std::vector triangles; protected: std::vector polygonData; diff --git a/src/IntelliHelper/IntelliHelper.cpp b/src/IntelliHelper/IntelliHelper.cpp index cb5d6a8..42b8aab 100644 --- a/src/IntelliHelper/IntelliHelper.cpp +++ b/src/IntelliHelper/IntelliHelper.cpp @@ -1,2 +1,123 @@ #include"IntelliHelper.h" #include +#include +#include + + +std::vector IntelliHelper::calculateTriangles(std::vector polyPoints){ + //helper for managing the triangle vertices and their state + struct TriangleHelper{ + QPoint vertex; + float interiorAngle; + int index; + bool isTip; + }; + + //calculates the inner angle of 'point' + auto calculateInner = [](QPoint& point, QPoint& prev, QPoint& post){ + QPoint AP(point.x()-prev.x(), point.y()-prev.y()); + QPoint BP(point.x()-post.x(), point.y()-post.y()); + + float topSclar = AP.x()*BP.x()+AP.y()*BP.y(); + float absolute = sqrt(pow(AP.x(),2.)+pow(AP.y(),2.))*sqrt(pow(BP.x(),2.)+pow(BP.y(),2.)); + return acos(topSclar/absolute); + }; + + //gets the first element of vec for which element.isTip == true holds + auto getTip= [](const std::vector& vec){ + for(auto element:vec){ + if(element.isTip){ + return element; + } + } + return vec[0]; + }; + + //get the vertex Index bevor index in relation to the container length + auto getPrev = [](int index, int length){ + return (index-1)>0?(index-1):(length-1); + }; + + //get the vertex Index after index in relation to the container lenght + auto getPost = [](int index, int length){ + return (index+1)%length; + }; + + //return if the vertex is a tip + auto isTip = [](float angle){ + return angle<180.f; + }; + + std::vector Vertices; + std::vector Triangles; + + //set up all vertices and calculate intirior angle + for(int i=0; i(polyPoints.size()); i++){ + TriangleHelper helper; + int prev = getPrev(i, static_cast(polyPoints.size())); + int post = getPost(i, static_cast(polyPoints.size())); + + helper.vertex = polyPoints[static_cast(i)]; + helper.index = i; + + helper.interiorAngle = calculateInner(polyPoints[static_cast(i)], + polyPoints[static_cast(prev)], + polyPoints[static_cast(post)]); + helper.isTip = isTip(helper.interiorAngle); + Vertices.push_back(helper); + } + + //search triangles based on the intirior angles of each vertey + while(Triangles.size() != polyPoints.size()-2){ + Triangle tri; + TriangleHelper smallest = getTip(Vertices); + int prev = getPrev(smallest.index, static_cast(Vertices.size())); + int post = getPost(smallest.index, static_cast(Vertices.size())); + + //set triangle and push it + tri.A = Vertices[static_cast(prev)].vertex; + tri.B = Vertices[static_cast(smallest.index)].vertex; + tri.C = Vertices[static_cast(post)].vertex; + Triangles.push_back(tri); + + //update Vertice array + Vertices.erase(Vertices.begin()+smallest.index); + for(size_t i=static_cast(smallest.index); i(Vertices.size())); + int postOfPrev = getPost(prev, static_cast(Vertices.size())); + + int prevOfPost = getPrev(post, static_cast(Vertices.size())); + int postOfPost = getPost(post, static_cast(Vertices.size())); + + //update vertices with interior angles + //updtae prev + Vertices[static_cast(prev)].interiorAngle = calculateInner(Vertices[static_cast(prev)].vertex, + Vertices[static_cast(prevOfPrev)].vertex, + Vertices[static_cast(postOfPrev)].vertex); + Vertices[static_cast(prev)].isTip = isTip(Vertices[static_cast(prev)].interiorAngle); + //update post + Vertices[static_cast(post)].interiorAngle = calculateInner(Vertices[static_cast(post)].vertex, + Vertices[static_cast(prevOfPost)].vertex, + Vertices[static_cast(postOfPost)].vertex); + Vertices[static_cast(post)].isTip = isTip(Vertices[static_cast(post)].interiorAngle); + + } + return Triangles; +} + +bool IntelliHelper::isInPolygon(std::vector &triangles, QPoint &point){ + for(auto triangle : triangles){ + if(IntelliHelper::isInTriangle(triangle.A ,triangle.B, triangle.C, point)){ + return true; + } + } + return false; +} diff --git a/src/IntelliHelper/IntelliHelper.h b/src/IntelliHelper/IntelliHelper.h index 1bad2b9..e01aff9 100644 --- a/src/IntelliHelper/IntelliHelper.h +++ b/src/IntelliHelper/IntelliHelper.h @@ -2,17 +2,20 @@ #define INTELLIHELPER_H #include +#include + +struct Triangle{ + QPoint A,B,C; +}; + +namespace IntelliHelper { -class IntelliHelper{ - -public: - - static inline float sign(QPoint& p1, QPoint& p2, QPoint& p3){ + inline float sign(QPoint& p1, QPoint& p2, QPoint& p3){ return (p1.x()-p3.x())*(p2.y()-p3.y())-(p2.x()-p3.x())*(p1.y()-p3.y()); } - static inline bool isInTriangle(QPoint& A, QPoint& B, QPoint& C, QPoint& P){ + inline bool isInTriangle(QPoint& A, QPoint& B, QPoint& C, QPoint& P){ float val1, val2, val3; bool neg, pos; @@ -25,6 +28,11 @@ public: return !(neg && pos); } -}; + + std::vector calculateTriangles(std::vector polyPoints); + + bool isInPolygon(std::vector &triangles, QPoint &point); + +} #endif diff --git a/src/IntelliPhoto.pro b/src/IntelliPhoto.pro index 21210e8..37df2ff 100644 --- a/src/IntelliPhoto.pro +++ b/src/IntelliPhoto.pro @@ -29,7 +29,7 @@ SOURCES += \ Tool/IntelliToolPen.cpp \ Tool/IntelliToolPlain.cpp \ Tool/IntelliToolPolygon.cpp \ - Tool/IntelliToolRechteck.cpp \ + Tool/IntelliToolRectangle.cpp \ main.cpp HEADERS += \ @@ -46,7 +46,7 @@ HEADERS += \ Tool/IntelliToolPen.h \ Tool/IntelliToolPlain.h \ Tool/IntelliToolPolygon.h \ - Tool/IntelliToolRechteck.h + Tool/IntelliToolRectangle.h FORMS += \ mainwindow.ui diff --git a/src/Layer/PaintingArea.cpp b/src/Layer/PaintingArea.cpp index bca41f0..d4b0700 100644 --- a/src/Layer/PaintingArea.cpp +++ b/src/Layer/PaintingArea.cpp @@ -14,7 +14,7 @@ #include "Tool/IntelliToolPlain.h" #include "Tool/IntelliToolLine.h" #include "Tool/IntelliToolCircle.h" -#include "Tool/IntelliToolRechteck.h" +#include "Tool/IntelliToolRectangle.h" #include "Tool/IntelliToolPolygon.h" PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent) @@ -165,12 +165,12 @@ void PaintingArea::slotActivateLayer(int a){ } void PaintingArea::colorPickerSetFirstColor(){ - QColor clr = QColorDialog::getColor(colorPicker.getFirstColor(), nullptr, "Main Color"); + QColor clr = QColorDialog::getColor(colorPicker.getFirstColor(), nullptr, "Main Color", QColorDialog::DontUseNativeDialog); this->colorPicker.setFirstColor(clr); } void PaintingArea::colorPickerSetSecondColor(){ - QColor clr = QColorDialog::getColor(colorPicker.getSecondColor(), nullptr, "Secondary Color"); + QColor clr = QColorDialog::getColor(colorPicker.getSecondColor(), nullptr, "Secondary Color", QColorDialog::DontUseNativeDialog); this->colorPicker.setSecondColor(clr); } diff --git a/src/Layer/PaintingArea.h b/src/Layer/PaintingArea.h index 547b6f3..d7c9282 100644 --- a/src/Layer/PaintingArea.h +++ b/src/Layer/PaintingArea.h @@ -21,8 +21,6 @@ struct LayerObject{ int widthOffset; int hightOffset; int alpha=255; - - }; class PaintingArea : public QWidget @@ -87,7 +85,6 @@ private: void activateUpperLayer(); void activateLowerLayer(); - QImage* Canvas; int maxWidth; int maxHeight; @@ -108,4 +105,3 @@ private: }; #endif - diff --git a/src/Tool/IntelliToolRechteck.cpp b/src/Tool/IntelliToolRectangle.cpp similarity index 73% rename from src/Tool/IntelliToolRechteck.cpp rename to src/Tool/IntelliToolRectangle.cpp index 05b1e79..0c89762 100644 --- a/src/Tool/IntelliToolRechteck.cpp +++ b/src/Tool/IntelliToolRectangle.cpp @@ -1,18 +1,18 @@ -#include"IntelliToolRechteck.h" +#include"IntelliToolRectangle.h" #include "Layer/PaintingArea.h" #include "QInputDialog" -IntelliToolRechteck::IntelliToolRechteck(PaintingArea* Area, IntelliColorPicker* colorPicker) +IntelliToolRectangle::IntelliToolRectangle(PaintingArea* Area, IntelliColorPicker* colorPicker) :IntelliTool(Area, colorPicker){ this->alphaInner = QInputDialog::getInt(nullptr,"Inner Alpha Value", "Value:", 0,0,255,1); this->edgeWidth = QInputDialog::getInt(nullptr,"Outer edge width", "Value:", 0,1,255,1); } -IntelliToolRechteck::~IntelliToolRechteck(){ +IntelliToolRectangle::~IntelliToolRectangle(){ } -void IntelliToolRechteck::drawRechteck(QPoint otherCornor){ +void IntelliToolRectangle::drawRectangle(QPoint otherCornor){ int xMin = std::min(originCornor.x(), otherCornor.x()); int xMax = std::max(originCornor.x(), otherCornor.x()); @@ -30,36 +30,36 @@ void IntelliToolRechteck::drawRechteck(QPoint otherCornor){ this->Canvas->image->drawLine(QPoint(xMax, yMax),QPoint(xMax, yMin), this->colorPicker->getFirstColor(), edgeWidth); } -void IntelliToolRechteck::onMouseRightPressed(int x, int y){ +void IntelliToolRectangle::onMouseRightPressed(int x, int y){ IntelliTool::onMouseRightPressed(x,y); } -void IntelliToolRechteck::onMouseRightReleased(int x, int y){ +void IntelliToolRectangle::onMouseRightReleased(int x, int y){ IntelliTool::onMouseRightReleased(x,y); } -void IntelliToolRechteck::onMouseLeftPressed(int x, int y){ +void IntelliToolRectangle::onMouseLeftPressed(int x, int y){ IntelliTool::onMouseLeftPressed(x,y); this->originCornor=QPoint(x,y); - drawRechteck(originCornor); + drawRectangle(originCornor); Canvas->image->calculateVisiblity(); } -void IntelliToolRechteck::onMouseLeftReleased(int x, int y){ +void IntelliToolRectangle::onMouseLeftReleased(int x, int y){ IntelliTool::onMouseLeftReleased(x,y); } -void IntelliToolRechteck::onMouseMoved(int x, int y){ +void IntelliToolRectangle::onMouseMoved(int x, int y){ IntelliTool::onMouseMoved(x,y); if(this->drawing){ this->Canvas->image->drawPlain(Qt::transparent); QPoint next(x,y); - drawRechteck(next); + drawRectangle(next); } IntelliTool::onMouseMoved(x,y); } -void IntelliToolRechteck::onWheelScrolled(int value){ +void IntelliToolRectangle::onWheelScrolled(int value){ IntelliTool::onWheelScrolled(value); this->edgeWidth+=value; if(this->edgeWidth<=0){ diff --git a/src/Tool/IntelliToolRechteck.h b/src/Tool/IntelliToolRectangle.h similarity index 61% rename from src/Tool/IntelliToolRechteck.h rename to src/Tool/IntelliToolRectangle.h index f3db883..2b59629 100644 --- a/src/Tool/IntelliToolRechteck.h +++ b/src/Tool/IntelliToolRectangle.h @@ -1,20 +1,20 @@ -#ifndef INTELLIRECHTECKTOOL_H -#define INTELLIRECHTECKTOOL_H +#ifndef INTELLIRECTANGLETOOL_H +#define INTELLIRECTANGLETOOL_H #include "IntelliTool.h" #include "QColor" #include "QPoint" -class IntelliToolRechteck : public IntelliTool{ - void drawRechteck(QPoint otherCornor); +class IntelliToolRectangle : public IntelliTool{ + void drawRectangle(QPoint otherCornor); QPoint originCornor; int alphaInner; int edgeWidth; public: - IntelliToolRechteck(PaintingArea* Area, IntelliColorPicker* colorPicker); - virtual ~IntelliToolRechteck() override; + IntelliToolRectangle(PaintingArea* Area, IntelliColorPicker* colorPicker); + virtual ~IntelliToolRectangle() override; virtual void onMouseRightPressed(int x, int y) override; virtual void onMouseRightReleased(int x, int y) override; @@ -26,4 +26,4 @@ public: virtual void onMouseMoved(int x, int y) override; }; -#endif // INTELLIRECHTECKTOOL_H +#endif // INTELLIRECTANGLETOOL_H diff --git a/src/main.cpp b/src/main.cpp index 771a872..c8e501c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include "GUI/IntelliPhotoGui.h" #include #include +#include "IntelliHelper/IntelliHelper.h" +#include int main(int argc, char *argv[]){ // The main application