Recht unschöne Modularität der Tools

This commit is contained in:
Sonaion
2019-12-10 12:20:15 +01:00
parent 62e144abd4
commit 0aa3b17b8a
10 changed files with 147 additions and 14 deletions

View File

@@ -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

View File

@@ -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){

View File

@@ -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<QPoint>& polygonData) override;

View File

@@ -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;

View File

@@ -4,7 +4,7 @@
#include"Image/IntelliImage.h"
class IntelliShapedImage : public IntelliImage{
friend IntelliTool;
protected:
virtual void calculateVisiblity() override;
std::vector<QPoint> 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<QPoint>& polygonData) override;
};

View File

@@ -22,6 +22,7 @@ SOURCES += \
Image/IntelliShapedImage.cpp \
IntelliHelper/IntelliHelper.cpp \
Layer/PaintingArea.cpp \
Tool/IntelliTool.cpp \
main.cpp
HEADERS += \

View File

@@ -58,6 +58,7 @@ int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOff
return static_cast<int>(layerBundle.size())-1;
}
void PaintingArea::deleteLayer(int index){
if(index<static_cast<int>(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);
}
}

View File

@@ -5,10 +5,12 @@
#include <QColor>
#include <QImage>
#include"Image/IntelliImage.h"
#include"Tool/IntelliTool.h"
#include <QPoint>
#include <QWidget>
#include <QList>
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

View File

@@ -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; y<Active->hight; y++){
for(int x=0; x<Active->width; x++){
clr_0=Active->image->imageData.pixelColor(x,y);
clr_1=Canvas->image->imageData.pixelColor(x,y);
float t = static_cast<float>(clr_1.alpha())/255.f;
int r =static_cast<int>(static_cast<float>(clr_1.red())*(t)+static_cast<float>(clr_0.red())*(1.f-t)+0.5f);
int g =static_cast<int>(static_cast<float>(clr_1.green())*(t)+static_cast<float>(clr_0.green())*(1.f-t)+0.5f);
int b =static_cast<int>(static_cast<float>(clr_1.blue())*(t)+static_cast<float>(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;
}

View File

@@ -1,18 +1,30 @@
#ifndef Intelli_Tool_H
#define Intelli_Tool_H
#include"Layer/PaintingArea.h"
#include<vector>
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