mirror of
https://github.com/creyD/intelliphoto.git
synced 2026-04-16 21:30:31 +02:00
Restore of old painting
This commit is contained in:
@@ -1,34 +1,30 @@
|
||||
// ---------- PaintingArea.cpp ----------
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QRect>
|
||||
#include<QRect>
|
||||
#include "PaintingArea.h"
|
||||
#include "Image/IntelliRasterImage.h"
|
||||
#include "Image/IntelliShapedImage.h"
|
||||
|
||||
#include <vector>
|
||||
#include <QPoint>
|
||||
#include<vector>
|
||||
#include<QPoint>
|
||||
|
||||
PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
|
||||
:QWidget(parent){
|
||||
this->setUp(maxWidth, maxHeight);
|
||||
PaintingArea::PaintingArea(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
//create standart image
|
||||
this->image = new IntelliRasterImage(400,400);
|
||||
std::vector<QPoint> poly;
|
||||
poly.push_back(QPoint(200,0));
|
||||
poly.push_back(QPoint(400,300));
|
||||
poly.push_back(QPoint(0,300));
|
||||
poly.push_back(QPoint(200,0));
|
||||
image->setPolygon(poly);
|
||||
|
||||
//tetsing
|
||||
this->addLayer(200,200,0,0);
|
||||
this->addLayer(200,200,201,201);
|
||||
this->setUp();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PaintingArea::setUp(int maxWidth, int maxHeight){
|
||||
|
||||
//set standart parameter
|
||||
this->maxWidth = maxWidth;
|
||||
this->maxHeight = maxHeight;
|
||||
Canvas = new QImage(maxWidth,maxHeight, QImage::Format_ARGB32);
|
||||
Canvas->fill(Qt::GlobalColor::white);
|
||||
|
||||
void PaintingArea::setUp(){
|
||||
// Roots the widget to the top left even if resized
|
||||
setAttribute(Qt::WA_StaticContents);
|
||||
|
||||
@@ -36,52 +32,27 @@ void PaintingArea::setUp(int maxWidth, int maxHeight){
|
||||
scribbling = false;
|
||||
myPenWidth = 1;
|
||||
myPenColor = Qt::blue;
|
||||
|
||||
}
|
||||
|
||||
void PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset, ImageType type){
|
||||
LayerObject newLayer;
|
||||
newLayer.width = width;
|
||||
newLayer.height = height;
|
||||
newLayer.widthOffset = widthOffset;
|
||||
newLayer.heightOffset = heightOffset;
|
||||
PaintingArea::PaintingArea(int width, int height, ImageType type, QWidget *parent)
|
||||
: QWidget(parent){
|
||||
if(type==ImageType::Raster_Image){
|
||||
newLayer.image = new IntelliRasterImage(width,height);
|
||||
this->image = new IntelliRasterImage(width, height);
|
||||
}else if(type==ImageType::Shaped_Image){
|
||||
newLayer.image = new IntelliShapedImage(width, height);
|
||||
this->image = new IntelliShapedImage(width, height);
|
||||
}else{
|
||||
qDebug() << "No valid Image type error";
|
||||
return;
|
||||
}
|
||||
newLayer.alpha = 255;
|
||||
this->layerStructure.push_back(newLayer);
|
||||
this->setUp();
|
||||
}
|
||||
|
||||
void PaintingArea::deleteLayer(int index){
|
||||
if(index<layerStructure.size()){
|
||||
this->layerStructure.erase(layerStructure.begin()+index);
|
||||
if(activeLayer>=index){
|
||||
activeLayer--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PaintingArea::setLayerToActive(int index) {
|
||||
if(index<layerStructure.size()){
|
||||
this->activeLayer=index;
|
||||
}
|
||||
}
|
||||
|
||||
void PaintingArea::setAlphaToLayer(int index, int alpha){
|
||||
if(index<layerStructure.size()){
|
||||
layerStructure[index].alpha=alpha;
|
||||
}
|
||||
}
|
||||
|
||||
// Used to load the image and place it in the widget
|
||||
bool PaintingArea::openImage(const QString &fileName)
|
||||
{
|
||||
if(this->activeLayer==-1){
|
||||
return false;
|
||||
}
|
||||
IntelliImage* active = layerStructure[activeLayer].image;
|
||||
bool open = active->loadImage(fileName);
|
||||
bool open = image->loadImage(fileName);
|
||||
update();
|
||||
return open;
|
||||
}
|
||||
@@ -89,22 +60,14 @@ bool PaintingArea::openImage(const QString &fileName)
|
||||
// Save the current image
|
||||
bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
|
||||
{
|
||||
if(this->activeLayer==-1){
|
||||
// Created to hold the image
|
||||
QImage visibleImage = image->getDisplayable();
|
||||
|
||||
if (visibleImage.save(fileName, fileFormat)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
// Created to hold the image
|
||||
|
||||
for(size_t i=0; i<layerStructure.size(); i++){
|
||||
LayerObject layer;
|
||||
QImage cpy = layer.image->getDisplayable(layer.alpha);
|
||||
//TODO draw cpy to CANVAS
|
||||
}
|
||||
|
||||
//if (Canvas.save(fileName, fileFormat)) {
|
||||
// return true;
|
||||
//} else {
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
|
||||
// Used to change the pen color
|
||||
@@ -122,13 +85,8 @@ void PaintingArea::setPenWidth(int newWidth)
|
||||
// Color the image area with white
|
||||
void PaintingArea::clearImage()
|
||||
{
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
IntelliImage* active = layerStructure[activeLayer].image;
|
||||
active->floodFill(qRgb(255, 255, 255));
|
||||
|
||||
update();
|
||||
image->floodFill(qRgb(255, 255, 255));
|
||||
update();
|
||||
}
|
||||
|
||||
// If a mouse button is pressed check if it was the
|
||||
@@ -137,14 +95,8 @@ void PaintingArea::clearImage()
|
||||
void PaintingArea::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
IntelliImage* active = layerStructure[activeLayer].image;
|
||||
|
||||
int x = static_cast<int>(event->x()*static_cast<float>(active->x())/static_cast<float>(size().width()));
|
||||
int y = static_cast<int>(event->y()*static_cast<float>(active->y())/static_cast<float>(size().height()));
|
||||
//TODO CALCULATE LAST POINT
|
||||
int x = event->x()*(float)image->x()/(float)size().width();
|
||||
int y = event->y()*(float)image->y()/(float)size().height();
|
||||
lastPoint=QPoint(x,y);
|
||||
scribbling = true;
|
||||
}
|
||||
@@ -156,17 +108,9 @@ void PaintingArea::mousePressEvent(QMouseEvent *event)
|
||||
// from the last position to the current
|
||||
void PaintingArea::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
|
||||
if ((event->buttons() & Qt::LeftButton) && scribbling){
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
IntelliImage* active = layerStructure[activeLayer].image;
|
||||
|
||||
int x = static_cast<int>(event->x()*static_cast<float>(active->x())/static_cast<float>(size().width()));
|
||||
int y = static_cast<int>(event->y()*static_cast<float>(active->y())/static_cast<float>(size().height()));
|
||||
|
||||
//TODO CALCULATE NEW POINT
|
||||
int x = event->x()*(float)image->x()/(float)size().width();
|
||||
int y = event->y()*(float)image->y()/(float)size().height();
|
||||
drawLineTo(QPoint(x,y));
|
||||
update();
|
||||
}
|
||||
@@ -176,15 +120,8 @@ void PaintingArea::mouseMoveEvent(QMouseEvent *event)
|
||||
void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && scribbling) {
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
IntelliImage* active = layerStructure[activeLayer].image;
|
||||
|
||||
int x = static_cast<int>(event->x()*static_cast<float>(active->x())/static_cast<float>(size().width()));
|
||||
int y = static_cast<int>(event->y()*static_cast<float>(active->y())/static_cast<float>(size().height()));
|
||||
|
||||
//TODO CALCULATE NEW POINT
|
||||
int x = event->x()*(float)image->x()/(float)size().width();
|
||||
int y = event->y()*(float)image->y()/(float)size().height();
|
||||
drawLineTo(QPoint(x,y));
|
||||
update();
|
||||
scribbling = false;
|
||||
@@ -196,14 +133,9 @@ void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
|
||||
// update themselves
|
||||
void PaintingArea::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
LayerObject active = layerStructure[activeLayer];
|
||||
|
||||
QPainter painter(this);
|
||||
QRect dirtyRec = event->rect();
|
||||
painter.drawImage(dirtyRec, active.image->getDisplayable(dirtyRec.size(), active.alpha), dirtyRec);
|
||||
painter.drawImage(dirtyRec, image->getDisplayable(dirtyRec.size()), dirtyRec);
|
||||
update();
|
||||
}
|
||||
|
||||
@@ -211,26 +143,17 @@ void PaintingArea::paintEvent(QPaintEvent *event)
|
||||
// to cut down on the need to resize the image
|
||||
void PaintingArea::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
LayerObject active = layerStructure[activeLayer];
|
||||
|
||||
QPainter painter(this);
|
||||
QRect dirtyRec(QPoint(0,0), event->size());
|
||||
painter.drawImage(dirtyRec, active.image->getDisplayable(event->size(), active.alpha), dirtyRec);
|
||||
painter.drawImage(dirtyRec, image->getDisplayable(event->size()), dirtyRec);
|
||||
update();
|
||||
//QWidget::resizeEvent(event);
|
||||
}
|
||||
|
||||
void PaintingArea::drawLineTo(const QPoint &endPoint)
|
||||
{
|
||||
//// Used to draw on the widget
|
||||
if(this->activeLayer==-1){
|
||||
return;
|
||||
}
|
||||
LayerObject active = layerStructure[activeLayer];
|
||||
|
||||
active.image->drawLine(lastPoint, endPoint,myPenColor, myPenWidth);
|
||||
// Used to draw on the widget
|
||||
image->drawLine(lastPoint, endPoint,myPenColor, myPenWidth);
|
||||
lastPoint = endPoint;
|
||||
update();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include"Image/IntelliImage.h"
|
||||
#include <QPoint>
|
||||
#include <QWidget>
|
||||
#include <QList>
|
||||
|
||||
class PaintingArea : public QWidget
|
||||
{
|
||||
@@ -17,24 +16,19 @@ class PaintingArea : public QWidget
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PaintingArea(int maxWidth=1000, int maxHeight=800, QWidget *parent = nullptr);
|
||||
//create raster image 400*200
|
||||
PaintingArea(QWidget *parent = nullptr);
|
||||
PaintingArea(int width, int height, ImageType type, QWidget *parent = nullptr);
|
||||
|
||||
// Handles all events
|
||||
bool openImage(const QString &fileName);
|
||||
bool saveImage(const QString &fileName, const char *fileFormat);
|
||||
|
||||
void addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type = ImageType::Raster_Image);
|
||||
void deleteLayer(int index);
|
||||
void setLayerToActive(int index);
|
||||
void setAlphaToLayer(int index, int alpha);
|
||||
void setPenColor(const QColor &newColor);
|
||||
void setPenWidth(int newWidth);
|
||||
|
||||
// Has the image been modified since last save
|
||||
bool isModified() const { return modified; }
|
||||
|
||||
void setPenColor(const QColor &newColor);
|
||||
QColor penColor() const { return myPenColor; }
|
||||
|
||||
void setPenWidth(int newWidth);
|
||||
int penWidth() const { return myPenWidth; }
|
||||
|
||||
public slots:
|
||||
@@ -43,7 +37,7 @@ public slots:
|
||||
void clearImage();
|
||||
|
||||
//void setUp helper for konstruktor
|
||||
void setUp(int maxWidth, int maxHeight);
|
||||
void setUp();
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseMoveEvent(QMouseEvent *event) override;
|
||||
@@ -57,22 +51,6 @@ protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private:
|
||||
struct LayerObject{
|
||||
IntelliImage* image;
|
||||
int width;
|
||||
int height;
|
||||
int widthOffset;
|
||||
int heightOffset;
|
||||
int alpha=255;
|
||||
};
|
||||
|
||||
QImage* Canvas;
|
||||
int maxWidth;
|
||||
int maxHeight;
|
||||
|
||||
std::vector<LayerObject> layerStructure;
|
||||
int activeLayer=-1;
|
||||
|
||||
void drawLineTo(const QPoint &endPoint);
|
||||
void resizeImage(QImage *image_res, const QSize &newSize);
|
||||
|
||||
@@ -89,6 +67,7 @@ private:
|
||||
QColor myPenColor;
|
||||
|
||||
// Stores the image being drawn
|
||||
IntelliImage* image;
|
||||
|
||||
// Stores the location at the current mouse event
|
||||
QPoint lastPoint;
|
||||
|
||||
Reference in New Issue
Block a user