diff --git a/src/Painting/Image/IntelliImage.h b/src/Painting/Image/IntelliImage.h index 894093c..55ea479 100644 --- a/src/Painting/Image/IntelliImage.h +++ b/src/Painting/Image/IntelliImage.h @@ -29,15 +29,17 @@ public: virtual ~IntelliImage() = 0; //start on top left - //TODO give tool refrence of image, -> funtkions will not be needed - virtual void drawPixel(const QPoint &p1, const QColor& color); - virtual void drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth); - virtual void floodFill(const QColor& color); + virtual void drawPixel(const QPoint &p1, const QColor& color); + virtual void drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth); + virtual void floodFill(const QColor& color); //returns the filtered output virtual QImage getDisplayable(const QSize& displaySize, int alpha)=0; virtual QImage getDisplayable(int alpha=255)=0; + //gets a copy of the image !allocated + virtual IntelliImage* getDeepCopy()=0; + //returns the filtered output //sets the data for the visible image diff --git a/src/Painting/Image/IntelliRasterImage.cpp b/src/Painting/Image/IntelliRasterImage.cpp index 3cedbeb..6e4bc15 100644 --- a/src/Painting/Image/IntelliRasterImage.cpp +++ b/src/Painting/Image/IntelliRasterImage.cpp @@ -12,8 +12,14 @@ IntelliRasterImage::~IntelliRasterImage(){ } -void IntelliRasterImage::calculateVisiblity(){ +IntelliImage* IntelliRasterImage::getDeepCopy(){ + IntelliRasterImage* raster = new IntelliRasterImage(imageData.width(), imageData.height()); + raster->imageData.fill(Qt::transparent); + return raster; +} +void IntelliRasterImage::calculateVisiblity(){ + //not used in raster image } QImage IntelliRasterImage::getDisplayable(int alpha){ diff --git a/src/Painting/Image/IntelliRasterImage.h b/src/Painting/Image/IntelliRasterImage.h index 2d6d809..bf709e6 100644 --- a/src/Painting/Image/IntelliRasterImage.h +++ b/src/Painting/Image/IntelliRasterImage.h @@ -4,6 +4,7 @@ #include"Image/IntelliImage.h" class IntelliRasterImage : public IntelliImage{ + friend IntelliTool; protected: virtual void calculateVisiblity() override; public: @@ -14,6 +15,8 @@ public: virtual QImage getDisplayable(const QSize& displaySize,int alpha) override; virtual QImage getDisplayable(int alpha=255) override; + //gets a copy of the image !allocated + virtual IntelliImage* getDeepCopy() override; //sets the data for the visible image virtual void setPolygon(const std::vector& polygonData) override; diff --git a/src/Painting/Image/IntelliShapedImage.cpp b/src/Painting/Image/IntelliShapedImage.cpp index 7baf81c..72480f6 100644 --- a/src/Painting/Image/IntelliShapedImage.cpp +++ b/src/Painting/Image/IntelliShapedImage.cpp @@ -16,6 +16,13 @@ QImage IntelliShapedImage::getDisplayable(int alpha){ return getDisplayable(imageData.size(),alpha); } +IntelliImage* IntelliShapedImage::getDeepCopy(){ + IntelliShapedImage* shaped = new IntelliShapedImage(imageData.width(), imageData.height()); + shaped->setPolygon(this->polygonData); + shaped->imageData.fill(Qt::transparent); + return shaped; +} + void IntelliShapedImage::calculateVisiblity(){ if(polygonData.size()<=2){ QColor clr; diff --git a/src/Painting/Image/IntelliShapedImage.h b/src/Painting/Image/IntelliShapedImage.h index 0039c3b..e3ef5bc 100644 --- a/src/Painting/Image/IntelliShapedImage.h +++ b/src/Painting/Image/IntelliShapedImage.h @@ -4,7 +4,7 @@ #include"Image/IntelliImage.h" class IntelliShapedImage : public IntelliImage{ - + friend IntelliTool; protected: virtual void calculateVisiblity() override; std::vector polygonData; @@ -16,6 +16,9 @@ public: virtual QImage getDisplayable(const QSize& displaySize, int alpha=255) override; virtual QImage getDisplayable(int alpha=255) override; + //gets a copy of the image !allocated + virtual IntelliImage* getDeepCopy() override; + //sets the data for the visible image virtual void setPolygon(const std::vector& polygonData) override; }; diff --git a/src/Painting/IntelliPhoto.pro b/src/Painting/IntelliPhoto.pro index 9cb20dc..cddc449 100644 --- a/src/Painting/IntelliPhoto.pro +++ b/src/Painting/IntelliPhoto.pro @@ -22,6 +22,7 @@ SOURCES += \ Image/IntelliShapedImage.cpp \ IntelliHelper/IntelliHelper.cpp \ Layer/PaintingArea.cpp \ + Tool/IntelliTool.cpp \ main.cpp HEADERS += \ diff --git a/src/Painting/Layer/PaintingArea.cpp b/src/Painting/Layer/PaintingArea.cpp index 044820a..e8b7a94 100644 --- a/src/Painting/Layer/PaintingArea.cpp +++ b/src/Painting/Layer/PaintingArea.cpp @@ -58,6 +58,7 @@ int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOff return static_cast(layerBundle.size())-1; } + void PaintingArea::deleteLayer(int index){ if(index(layerBundle.size())){ this->layerBundle.erase(layerBundle.begin()+index); @@ -217,6 +218,7 @@ void PaintingArea::activateLowerLayer(){ } } + void PaintingArea::assembleLayers(bool forSaving){ if(forSaving){ Canvas->fill(Qt::GlobalColor::transparent); @@ -251,3 +253,16 @@ void PaintingArea::assembleLayers(bool forSaving){ } } } + +void PaintingArea::createTempLayerAfter(int idx){ + if(idx>=0){ + LayerObject newLayer; + newLayer.alpha = layerBundle[idx].alpha; + newLayer.hight = layerBundle[idx].hight; + newLayer.width = layerBundle[idx].width; + newLayer.hightOffset = layerBundle[idx].hightOffset; + newLayer.widthOffset = layerBundle[idx].widthOffset; + newLayer.image = layerBundle[idx].image->getDeepCopy(); + layerBundle.insert(layerBundle.begin()+idx+1,newLayer); + } +} diff --git a/src/Painting/Layer/PaintingArea.h b/src/Painting/Layer/PaintingArea.h index 7ebec54..ead05da 100644 --- a/src/Painting/Layer/PaintingArea.h +++ b/src/Painting/Layer/PaintingArea.h @@ -5,10 +5,12 @@ #include #include #include"Image/IntelliImage.h" +#include"Tool/IntelliTool.h" #include #include #include + struct LayerObject{ IntelliImage* image; int width; @@ -16,6 +18,8 @@ struct LayerObject{ int widthOffset; int hightOffset; int alpha=255; + + }; class PaintingArea : public QWidget @@ -24,7 +28,7 @@ class PaintingArea : public QWidget // for all Qt objects // QObjects handle events Q_OBJECT - + friend IntelliTool; public: PaintingArea(int maxWidth=600, int maxHeight=600, QWidget *parent = nullptr); @@ -33,6 +37,7 @@ public: bool save(const QString &fileName, const char *fileFormat); int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type = ImageType::Raster_Image); + int addLayerAt(int idx, int width, int height, int widthOffset=0, int heightOffset=0, ImageType type = ImageType::Raster_Image); void deleteLayer(int index); void setLayerToActive(int index); void setAlphaOfLayer(int index, int alpha); @@ -43,6 +48,7 @@ public: // Has the image been modified since last save bool isModified() const { return modified; } + public slots: // Events to handle @@ -66,6 +72,7 @@ private: void activateUpperLayer(); void activateLowerLayer(); + QImage* Canvas; int maxWidth; int maxHeight; @@ -80,6 +87,9 @@ private: // Will be marked true or false depending on if // we have saved after a change bool modified=false; + + //Helper for Tool + void createTempLayerAfter(int idx); }; #endif diff --git a/src/Painting/Tool/IntelliTool.cpp b/src/Painting/Tool/IntelliTool.cpp new file mode 100644 index 0000000..046e7be --- /dev/null +++ b/src/Painting/Tool/IntelliTool.cpp @@ -0,0 +1,74 @@ +#include"IntelliTool.h" +#include"Layer/PaintingArea.h" + +IntelliTool::IntelliTool(PaintingArea* Area){ + this->Area=Area; +} + + +IntelliTool::~IntelliTool(){ + +} + + +void IntelliTool::onMouseRightPressed(int x, int y){ + this->drawing=true; + //create drawing layer + this->createToolLayer(); +} + +void IntelliTool::onMouseRightReleased(int x, int y){ + if(drawing){ + drawing=false; + this->mergeToolLayer(); + this->deleteToolLayer(); + } +} + +void IntelliTool::onMouseLeftPressed(int x, int y){ + if(drawing){ + drawing=false; + this->deleteToolLayer(); + } +} + +void IntelliTool::onMouseLeftReleased(int x, int y){ + //optional for tool +} + +void IntelliTool::onMouseMoved(int x, int y){ + //optional for tool +} + +void IntelliTool::createToolLayer(){ + Area->createTempLayerAfter(Area->activeLayer); + this->Active=&Area->layerBundle[Area->activeLayer]; + this->Canvas=&Area->layerBundle[Area->activeLayer+1]; +} + +void IntelliTool::mergeToolLayer(){ + QColor clr_0; + QColor clr_1; + for(int y=0; yhight; y++){ + for(int x=0; xwidth; x++){ + clr_0=Active->image->imageData.pixelColor(x,y); + clr_1=Canvas->image->imageData.pixelColor(x,y); + float t = static_cast(clr_1.alpha())/255.f; + int r =static_cast(static_cast(clr_1.red())*(t)+static_cast(clr_0.red())*(1.f-t)+0.5f); + int g =static_cast(static_cast(clr_1.green())*(t)+static_cast(clr_0.green())*(1.f-t)+0.5f); + int b =static_cast(static_cast(clr_1.blue())*(t)+static_cast(clr_0.blue()*(1.f-t))+0.5f); + int a =std::min(clr_0.alpha()+clr_1.alpha(), 255); + clr_0.setRed(r); + clr_0.setGreen(g); + clr_0.setBlue(b); + clr_0.setAlpha(a); + + Active->image->imageData.setPixelColor(x, y, clr_0); + } + } +} + +void IntelliTool::deleteToolLayer(){ + Area->deleteLayer(Area->activeLayer+1); + this->Canvas=nullptr; +} diff --git a/src/Painting/Tool/IntelliTool.h b/src/Painting/Tool/IntelliTool.h index b30b007..7c17366 100644 --- a/src/Painting/Tool/IntelliTool.h +++ b/src/Painting/Tool/IntelliTool.h @@ -1,18 +1,30 @@ #ifndef Intelli_Tool_H #define Intelli_Tool_H -#include"Layer/PaintingArea.h" +#include + +class LayerObject; +class PaintingArea; class IntelliTool{ private: - LayerObject* DataValue; - LayerObject* Preview; + void createToolLayer(); + void mergeToolLayer(); + void deleteToolLayer(); +protected: + PaintingArea* Area; + LayerObject* Active; + LayerObject* Canvas; + bool drawing = false; public: - IntelliTool(LayerObject* DataValue, LayerObject* Preview); + IntelliTool(PaintingArea* Area); virtual ~IntelliTool() = 0; - virtual void onMousePressed(QMouseEvent *event) = 0; - virtual void onMouseMoved(QMouseEvent* event)=0; - virtual void onMouseReleased(QMouseEvent* event)=0; + virtual void onMouseRightPressed(int x, int y); + virtual void onMouseRightReleased(int x, int y); + virtual void onMouseLeftPressed(int x, int y); + virtual void onMouseLeftReleased(int x, int y); + + virtual void onMouseMoved(int x, int y); }; #endif