From 8692ccd8a392035853168f21ea5a65dd8efa840f Mon Sep 17 00:00:00 2001 From: Jonas Mucke Date: Thu, 23 Jan 2020 20:13:43 +0100 Subject: [PATCH] project load and export --- src/GUI/IntelliPhotoGui.cpp | 4 +- src/Image/IntelliImage.h | 16 ++--- src/Image/IntelliRasterImage.cpp | 8 ++- src/Image/IntelliRasterImage.h | 6 ++ src/Image/IntelliShapedImage.cpp | 8 ++- src/Image/IntelliShapedImage.h | 8 +++ src/IntelliHelper/IntelliDatamanager.cpp | 77 ++++++++++++++++++++---- src/Layer/PaintingArea.cpp | 31 ++++++++-- src/Layer/PaintingArea.h | 23 +++++-- src/Tool/IntelliToolPolygon.cpp | 4 +- 10 files changed, 146 insertions(+), 39 deletions(-) diff --git a/src/GUI/IntelliPhotoGui.cpp b/src/GUI/IntelliPhotoGui.cpp index 4b38c18..f470822 100644 --- a/src/GUI/IntelliPhotoGui.cpp +++ b/src/GUI/IntelliPhotoGui.cpp @@ -102,7 +102,7 @@ void IntelliPhotoGui::slotCreateNewRasterLayer(){ // Create New Layer if (ok1&&ok2) { - paintingArea->addLayer(width,height,0,0,IntelliImage::ImageType::RASTERIMAGE); + paintingArea->addLayer(width,height,0,0,255,ImageType::RASTERIMAGE); UpdateGui(); } } @@ -121,7 +121,7 @@ void IntelliPhotoGui::slotCreateNewShapedLayer(){ // Create New Layer if (ok1&&ok2) { - paintingArea->addLayer(width, height, 0, 0, IntelliImage::ImageType::SHAPEDIMAGE); + paintingArea->addLayer(width, height, 0, 0,255, ImageType::SHAPEDIMAGE); UpdateGui(); } } diff --git a/src/Image/IntelliImage.h b/src/Image/IntelliImage.h index 5c19186..732fc34 100644 --- a/src/Image/IntelliImage.h +++ b/src/Image/IntelliImage.h @@ -16,6 +16,14 @@ class UnitTest; class IntelliTool; +/*! + * \brief The Types, which an Image can be. + */ +enum class ImageType { + RASTERIMAGE, + SHAPEDIMAGE +}; + /*! * \brief An abstract class which manages the basic IntelliImage operations. */ @@ -24,14 +32,6 @@ friend UnitTest; friend IntelliTool; public: -/*! - * \brief The Types, which an Image can be. - */ -enum class ImageType { - RASTERIMAGE, - SHAPEDIMAGE -}; - protected: void resizeImage(QImage*image, const QSize &newSize); diff --git a/src/Image/IntelliRasterImage.cpp b/src/Image/IntelliRasterImage.cpp index 3106e36..9e92ad9 100644 --- a/src/Image/IntelliRasterImage.cpp +++ b/src/Image/IntelliRasterImage.cpp @@ -5,7 +5,7 @@ IntelliRasterImage::IntelliRasterImage(int width, int height, bool fastRendererOn) : IntelliImage(width, height, fastRendererOn){ - TypeOfImage = IntelliImage::ImageType::RASTERIMAGE; + TypeOfImage = ImageType::RASTERIMAGE; this->fastRenderering = fastRendererOn; } @@ -16,7 +16,7 @@ IntelliRasterImage::~IntelliRasterImage(){ IntelliImage* IntelliRasterImage::getDeepCopy(){ IntelliRasterImage* raster = new IntelliRasterImage(imageData.width(), imageData.height(), false); raster->imageData.fill(Qt::transparent); - raster->TypeOfImage = IntelliImage::ImageType::RASTERIMAGE; + raster->TypeOfImage = ImageType::RASTERIMAGE; return raster; } @@ -49,3 +49,7 @@ QImage IntelliRasterImage::getDisplayable(const QSize& displaySize, int alpha){ void IntelliRasterImage::setPolygon(const std::vector& polygonData){ return; } + +std::vector IntelliRasterImage::getPolygon(){ + return std::vector(); +} diff --git a/src/Image/IntelliRasterImage.h b/src/Image/IntelliRasterImage.h index a67472a..c97ec8c 100644 --- a/src/Image/IntelliRasterImage.h +++ b/src/Image/IntelliRasterImage.h @@ -57,6 +57,12 @@ virtual IntelliImage* getDeepCopy() override; * \param polygonData - The Vertices of the Polygon. Nothing happens. */ virtual void setPolygon(const std::vector& polygonData) override; + +/*! + * \brief getPolygon + * \return returns the points of the polygon + */ +virtual std::vector getPolygon(); }; #endif diff --git a/src/Image/IntelliShapedImage.cpp b/src/Image/IntelliShapedImage.cpp index 14c7b71..3ba9ac2 100644 --- a/src/Image/IntelliShapedImage.cpp +++ b/src/Image/IntelliShapedImage.cpp @@ -6,7 +6,7 @@ IntelliShapedImage::IntelliShapedImage(int width, int height, bool fastRendererOn) : IntelliRasterImage(width, height, fastRendererOn){ - TypeOfImage = IntelliImage::ImageType::SHAPEDIMAGE; + TypeOfImage = ImageType::SHAPEDIMAGE; this->fastRenderering = fastRendererOn; } @@ -22,7 +22,7 @@ IntelliImage* IntelliShapedImage::getDeepCopy(){ IntelliShapedImage* shaped = new IntelliShapedImage(imageData.width(), imageData.height(), false); shaped->setPolygon(this->polygonData); shaped->imageData.fill(Qt::transparent); - shaped->TypeOfImage = IntelliImage::ImageType::SHAPEDIMAGE; + shaped->TypeOfImage = ImageType::SHAPEDIMAGE; return shaped; } @@ -111,3 +111,7 @@ void IntelliShapedImage::setPolygon(const std::vector& polygonData){ calculateVisiblity(); return; } + +std::vector IntelliShapedImage::getPolygon(){ + return polygonData; +} diff --git a/src/Image/IntelliShapedImage.h b/src/Image/IntelliShapedImage.h index 32c4c9f..781ece5 100644 --- a/src/Image/IntelliShapedImage.h +++ b/src/Image/IntelliShapedImage.h @@ -77,6 +77,14 @@ virtual std::vector getPolygonData() override { * \param polygonData - The Vertices of the Polygon. Just Planar Polygons are allowed. */ virtual void setPolygon(const std::vector& polygonData) override; + + +/*! + * \brief getPolygon + * \return returns the data of the polygon as points + */ +virtual std::vector getPolygon() override; }; + #endif diff --git a/src/IntelliHelper/IntelliDatamanager.cpp b/src/IntelliHelper/IntelliDatamanager.cpp index 936dbf3..bce95f6 100644 --- a/src/IntelliHelper/IntelliDatamanager.cpp +++ b/src/IntelliHelper/IntelliDatamanager.cpp @@ -5,27 +5,37 @@ bool IntelliDatamanager::saveProject(PaintingArea* Canvas, QString filePath){ QFile openFile(filePath); if(openFile.open(QIODevice::WriteOnly)){ + QTextStream out(&openFile); std::vector* layerBundle = Canvas->getLayerBundle(); size_t numberOfLayers = layerBundle->size(); - - out << "Format: idf\n" << "Version: 0.7\n" << endl; - out << "Resolution: " << Canvas->getMaxWidth() << "x" << Canvas->getMaxHeight() << endl; - out << "Layers: " << numberOfLayers << endl; + out << 7 << endl; //version tag + out << Canvas->getRenderSettings() << " "; + out << Canvas->getMaxWidth() << " " << Canvas->getMaxHeight() << endl; //dimensions of canvas + out << numberOfLayers << endl; //number of layers for(size_t i = 0; iat(i).width; int height = layerBundle->at(i).height; - out << "width: " << width << endl; - out << "height: " << height << endl; - out << "xoffset: " << layerBundle->at(i).widthOffset << endl; - out << "yoffset: " << layerBundle->at(i).heightOffset << endl; - out << "alpha: " << layerBundle->at(i).alpha << endl; + out << width << endl; //width + out << height << endl; //height + out << layerBundle->at(i).widthOffset << endl; //widthOffset + out << layerBundle->at(i).heightOffset << endl; //HeightOffset + out << layerBundle->at(i).alpha << endl; //alpha of layer + if(layerBundle->at(i).image->getTypeOfImage() == ImageType::RASTERIMAGE){ + out << 0 << " "; + }else{ + out << 1 << " "; + } + std::vector points = layerBundle->at(i).image->getPolygonData(); + out << points.size() << " "; + for(size_t j = 0; jat(i).image->getImageData().pixelColor(j,k); - out << pixColor.red() << " " << pixColor.green() << " " << pixColor.blue() << " " << pixColor.alpha() << ":"; + out << pixColor.red() << " " << pixColor.green() << " " << pixColor.blue() << " " << pixColor.alpha() << " "; } - out << endl; } } @@ -39,9 +49,50 @@ bool IntelliDatamanager::saveProject(PaintingArea* Canvas, QString filePath){ bool IntelliDatamanager::loadProject(PaintingArea* Canvas, QString filePath){ QFile openFile(filePath); - + Canvas->deleteAllLayers(); if(openFile.open(QIODevice::ReadOnly)){ - qDebug() << openFile.readLine(); + QTextStream in(&openFile); + + float version; + int rendersetting; + int widthCanvas, heightCanvas, numberOffLayers; + in >> version; + in >> rendersetting; + in >> widthCanvas >> heightCanvas; + in >> numberOffLayers; + + Canvas->setLayerDimensions(widthCanvas, heightCanvas); + for(int i=0; i> width >> height >> widthOffset >> heightOffset >> alpha; + + int typeFlag; + size_t numberOfPoints; + std::vector polyPoints; + + in >> typeFlag >> numberOfPoints; + if(typeFlag==0){ + Canvas->addLayer(width, height, widthOffset, heightOffset, alpha, ImageType::RASTERIMAGE); + }else{ + Canvas->addLayer(width, height, widthOffset, heightOffset, alpha, ImageType::SHAPEDIMAGE); + } + polyPoints.reserve(numberOfPoints); + for(size_t j=0; j> x >> y; + polyPoints.push_back(QPoint(x,y)); + } + Canvas->setPolygonDataToActive(polyPoints); + + for(int j=0; j> red >> green >> blue >> alpha; + Canvas->setPixelToActive(QColor(red, green, blue, alpha), QPoint(j, k)); + } + } + } + Canvas->setRenderSettings(static_cast(rendersetting)); openFile.close(); return true; } diff --git a/src/Layer/PaintingArea.cpp b/src/Layer/PaintingArea.cpp index a2813af..97a72cd 100644 --- a/src/Layer/PaintingArea.cpp +++ b/src/Layer/PaintingArea.cpp @@ -49,6 +49,10 @@ void PaintingArea::setRenderSettings(bool isFastRenderingOn){ } } +bool PaintingArea::getRenderSettings(){ + return this->renderSettings.isFastRenderering(); +} + void PaintingArea::setLayerDimensions(int maxWidth, int maxHeight){ //set standart parameter this->maxWidth = maxWidth; @@ -60,19 +64,27 @@ void PaintingArea::setLayerDimensions(int maxWidth, int maxHeight){ } -int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset, IntelliImage::ImageType type){ +void PaintingArea::setPixelToActive(QColor color, QPoint point){ + layerBundle[static_cast(activeLayer)].image->drawPixel(point, color); +} + +void PaintingArea::setPolygonDataToActive(std::vector points){ + layerBundle[static_cast(activeLayer)].image->setPolygon(points); +} + +int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset,int alpha, ImageType type){ LayerObject newLayer; updateTools(); newLayer.width = width; newLayer.height = height; newLayer.widthOffset = widthOffset; newLayer.heightOffset = heightOffset; - if(type==IntelliImage::ImageType::RASTERIMAGE) { + newLayer.alpha = alpha; + if(type==ImageType::RASTERIMAGE) { newLayer.image = new IntelliRasterImage(width,height,renderSettings.isFastRenderering()); - }else if(type==IntelliImage::ImageType::SHAPEDIMAGE) { + }else if(type==ImageType::SHAPEDIMAGE) { newLayer.image = new IntelliShapedImage(width, height, renderSettings.isFastRenderering()); } - newLayer.alpha = 255; this->layerBundle.push_back(newLayer); activeLayer = static_cast(layerBundle.size()) - 1; return activeLayer; @@ -117,7 +129,7 @@ void PaintingArea::setLayerAlpha(int idx, int alpha){ } void PaintingArea::setPolygon(int idx){ if(idx>=0&&idx(layerBundle.size())) { - if(layerBundle[static_cast(idx)].image->getTypeOfImage()==IntelliImage::ImageType::SHAPEDIMAGE) { + if(layerBundle[static_cast(idx)].image->getTypeOfImage()==ImageType::SHAPEDIMAGE) { delete this->Tool; this->Tool = new IntelliToolPolygon(this,&colorPicker,&Toolsettings, true); isSettingPolygon = true; @@ -138,6 +150,13 @@ bool PaintingArea::open(const QString &filePath){ return open; } +void PaintingArea::deleteAllLayers(){ + for(auto layer: layerBundle){ + delete layer.image; + } + layerBundle.clear(); +} + // Save the current image bool PaintingArea::save(const QString &filePath, const char*fileFormat){ if(layerBundle.size()==0) { @@ -249,7 +268,7 @@ int PaintingArea::getMaxHeight(){ return this->maxHeight; } -IntelliImage::ImageType PaintingArea::getTypeOfImageRealLayer(){ +ImageType PaintingArea::getTypeOfImageRealLayer(){ return this->layerBundle[static_cast(activeLayer)].image->getTypeOfImage(); } diff --git a/src/Layer/PaintingArea.h b/src/Layer/PaintingArea.h index 74e8eb5..264dafa 100644 --- a/src/Layer/PaintingArea.h +++ b/src/Layer/PaintingArea.h @@ -78,6 +78,12 @@ PaintingArea(int maxWidth = 600, int maxHeight = 600, QWidget*parent = nullptr); */ void setRenderSettings(bool isFastRenderingOn); +/*! + * \brief getRenderSettings updates all Images to the new Rendersetting. + * \param isFastRenderingOn is the new given flag for the FastRenderer. + */ +bool getRenderSettings(); + /*! * \brief The open method is used for loading a picture into the current layer. * \param filePath - Path and Name which are used to determine where the to-be-opened file is stored. @@ -92,16 +98,21 @@ bool open(const QString &filePath); */ bool save(const QString &filePath, const char*fileFormat); +/*! + * \brief deleteAllLayers deletes all layers + */ +void deleteAllLayers(); /*! * \brief The addLayer adds a layer to the current project/ painting area * \param width - Width of the layer in pixles * \param height - Height of the layer in pixles * \param widthOffset - Offset of the layer measured to the left border of the painting area in pixles * \param heightOffset - Offset of the layer measured to the top border of the painting area in pixles + * \param alpha - Transparence of the layer * \param type - Defining the ImageType of the new layer * \return Returns the number of layers in the project */ -int addLayer(int width, int height, int widthOffset = 0, int heightOffset = 0, IntelliImage::ImageType type = IntelliImage::ImageType::RASTERIMAGE); +int addLayer(int width, int height, int widthOffset = 0, int heightOffset = 0, int alpha=255, ImageType type = ImageType::RASTERIMAGE); /*! * \brief The addLayerAt adds a layer to the current project/ painting area at a specific position in the layer stack * \param idx - Index of the position the new layer should be added @@ -112,7 +123,7 @@ int addLayer(int width, int height, int widthOffset = 0, int heightOffset = 0, I * \param type - Defining the ImageType of the new layer * \return Returns the id of the layer position */ -int addLayerAt(int idx, int width, int height, int widthOffset = 0, int heightOffset = 0, IntelliImage::ImageType type = IntelliImage::ImageType::RASTERIMAGE); +int addLayerAt(int idx, int width, int height, int widthOffset = 0, int heightOffset = 0, ImageType type = ImageType::RASTERIMAGE); /*! * \brief The deleteLayer method removes a layer at a given idx * \param idx - The index of the layer to be removed @@ -183,7 +194,7 @@ int getMaxWidth(); int getMaxHeight(); -IntelliImage::ImageType getTypeOfImageRealLayer(); +ImageType getTypeOfImageRealLayer(); std::vector getPolygonDataOfRealLayer(); @@ -206,6 +217,11 @@ std::vector* getLayerBundle(); IntelliToolsettings Toolsettings; IntelliColorPicker colorPicker; +void setLayerDimensions(int maxWidth, int maxHeight); + +void setPixelToActive(QColor color, QPoint point); + +void setPolygonDataToActive(std::vector points); public slots: /*! * \brief The slotActivateLayer method handles the event of selecting one layer as active @@ -227,7 +243,6 @@ void wheelEvent(QWheelEvent*event) override; void paintEvent(QPaintEvent*event) override; private: -void setLayerDimensions(int maxWidth, int maxHeight); void selectLayerUp(); void selectLayerDown(); IntelliTool* copyActiveTool(); diff --git a/src/Tool/IntelliToolPolygon.cpp b/src/Tool/IntelliToolPolygon.cpp index 507ffb7..d8dc357 100644 --- a/src/Tool/IntelliToolPolygon.cpp +++ b/src/Tool/IntelliToolPolygon.cpp @@ -24,7 +24,7 @@ IntelliToolPolygon::~IntelliToolPolygon(){ } void IntelliToolPolygon::onMouseLeftPressed(int x, int y){ - if(!drawingOfPolygon && Area->getTypeOfImageRealLayer() == IntelliImage::ImageType::SHAPEDIMAGE && x > 0 && y > 0 && xgetWidthOfActive() && ygetHeightOfActive()) { + if(!drawingOfPolygon && Area->getTypeOfImageRealLayer() == ImageType::SHAPEDIMAGE && x > 0 && y > 0 && xgetWidthOfActive() && ygetHeightOfActive()) { if(Area->getPolygonDataOfRealLayer().size()>2) { std::vector Triangles = IntelliTriangulation::calculateTriangles(Area->getPolygonDataOfRealLayer()); QPoint Point(x,y); @@ -37,7 +37,7 @@ void IntelliToolPolygon::onMouseLeftPressed(int x, int y){ isInside = true; } } - else if(!drawingOfPolygon && Area->getTypeOfImageRealLayer() == IntelliImage::ImageType::RASTERIMAGE && x >= 0 && y >= 0 && xgetWidthOfActive() && ygetHeightOfActive()) { + else if(!drawingOfPolygon && Area->getTypeOfImageRealLayer() == ImageType::RASTERIMAGE && x >= 0 && y >= 0 && xgetWidthOfActive() && ygetHeightOfActive()) { isInside = true; }