mirror of
https://github.com/creyD/intelliphoto.git
synced 2026-04-14 12:20:32 +02:00
Merge branch 'Code_Quality' into 'dev'
Code quality See merge request creyd/intelliphoto!16
This commit is contained in:
@@ -73,7 +73,8 @@ private:
|
|||||||
bool saveFile(const QByteArray &fileFormat);
|
bool saveFile(const QByteArray &fileFormat);
|
||||||
|
|
||||||
// What we'll draw on
|
// What we'll draw on
|
||||||
PaintingArea *paintingArea;
|
PaintingArea* paintingArea;
|
||||||
|
|
||||||
|
|
||||||
// The menu widgets
|
// The menu widgets
|
||||||
QMenu *saveAsMenu;
|
QMenu *saveAsMenu;
|
||||||
|
|||||||
@@ -71,11 +71,3 @@ void IntelliImage::floodFill(const QColor& color){
|
|||||||
imageData.fill(color);
|
imageData.fill(color);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int IntelliImage::x(){
|
|
||||||
return imageData.size().width();
|
|
||||||
}
|
|
||||||
|
|
||||||
int IntelliImage::y(){
|
|
||||||
return imageData.size().height();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -13,20 +13,26 @@ enum class ImageType{
|
|||||||
Shaped_Image
|
Shaped_Image
|
||||||
};
|
};
|
||||||
|
|
||||||
class IntelliImage{
|
class IntelliTool;
|
||||||
|
|
||||||
|
class IntelliImage{
|
||||||
|
friend IntelliTool;
|
||||||
protected:
|
protected:
|
||||||
void resizeImage(QImage *image, const QSize &newSize);
|
void resizeImage(QImage *image, const QSize &newSize);
|
||||||
|
virtual void calculateVisiblity()=0;
|
||||||
|
|
||||||
QImage imageData;
|
QImage imageData;
|
||||||
|
|
||||||
|
//calculate with polygon
|
||||||
public:
|
public:
|
||||||
IntelliImage(int weight, int height);
|
IntelliImage(int weight, int height);
|
||||||
virtual ~IntelliImage() = 0;
|
virtual ~IntelliImage() = 0;
|
||||||
|
|
||||||
//start on top left
|
//start on top left
|
||||||
virtual void drawPixel(const QPoint &p1, const QColor& color);
|
//TODO give tool refrence of image, -> funtkions will not be needed
|
||||||
virtual void drawLine(const QPoint &p1, const QPoint& p2, const QColor& color, const int& penWidth);
|
virtual void drawPixel(const QPoint &p1, const QColor& color);
|
||||||
virtual void floodFill(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
|
//returns the filtered output
|
||||||
virtual QImage getDisplayable(const QSize& displaySize, int alpha)=0;
|
virtual QImage getDisplayable(const QSize& displaySize, int alpha)=0;
|
||||||
@@ -37,10 +43,9 @@ public:
|
|||||||
//sets the data for the visible image
|
//sets the data for the visible image
|
||||||
virtual void setPolygon(const std::vector<QPoint>& polygonData)=0;
|
virtual void setPolygon(const std::vector<QPoint>& polygonData)=0;
|
||||||
|
|
||||||
virtual bool loadImage(const QString &fileName);
|
|
||||||
|
|
||||||
int x();
|
//loads an image to the ColorBuffer
|
||||||
int y();
|
virtual bool loadImage(const QString &fileName);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ IntelliRasterImage::~IntelliRasterImage(){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntelliRasterImage::calculateVisiblity(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
QImage IntelliRasterImage::getDisplayable(int alpha){
|
QImage IntelliRasterImage::getDisplayable(int alpha){
|
||||||
return getDisplayable(imageData.size(), alpha);
|
return getDisplayable(imageData.size(), alpha);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,8 @@
|
|||||||
#include"Image/IntelliImage.h"
|
#include"Image/IntelliImage.h"
|
||||||
|
|
||||||
class IntelliRasterImage : public IntelliImage{
|
class IntelliRasterImage : public IntelliImage{
|
||||||
|
protected:
|
||||||
|
virtual void calculateVisiblity() override;
|
||||||
public:
|
public:
|
||||||
IntelliRasterImage(int weight, int height);
|
IntelliRasterImage(int weight, int height);
|
||||||
virtual ~IntelliRasterImage() override;
|
virtual ~IntelliRasterImage() override;
|
||||||
|
|||||||
@@ -13,59 +13,66 @@ IntelliShapedImage::~IntelliShapedImage(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
QImage IntelliShapedImage::getDisplayable(int alpha){
|
QImage IntelliShapedImage::getDisplayable(int alpha){
|
||||||
return getDisplayable(imageData.size());
|
return getDisplayable(imageData.size(),alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntelliShapedImage::calculateVisiblity(){
|
||||||
|
if(polygonData.size()<=2){
|
||||||
|
QColor clr;
|
||||||
|
for(int y=0; y<imageData.height(); y++){
|
||||||
|
for(int x=0; x<imageData.width(); x++){
|
||||||
|
clr = imageData.pixel(x,y);
|
||||||
|
clr.setAlpha(255);
|
||||||
|
imageData.setPixelColor(x,y,clr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QPoint A = polygonData[0];
|
||||||
|
QColor clr;
|
||||||
|
for(int y=0; y<imageData.height(); y++){
|
||||||
|
for(int x=0; x<imageData.width(); x++){
|
||||||
|
int cutNumeber=0;
|
||||||
|
for(int i=1; i<static_cast<int>(polygonData.size()-1); i++){
|
||||||
|
QPoint B = polygonData[static_cast<size_t>(i)];
|
||||||
|
QPoint C = polygonData[static_cast<size_t>(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);
|
||||||
|
clr.setAlpha(255);
|
||||||
|
imageData.setPixelColor(x,y,clr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage IntelliShapedImage::getDisplayable(const QSize& displaySize, int alpha){
|
QImage IntelliShapedImage::getDisplayable(const QSize& displaySize, int alpha){
|
||||||
if(polygonData.size()==0){
|
|
||||||
QImage copy = imageData;
|
|
||||||
for(int y = 0; y<copy.height(); y++){
|
|
||||||
for(int x = 0; x<copy.width(); x++){
|
|
||||||
QColor clr = copy.pixelColor(x,y);
|
|
||||||
clr.setAlpha(alpha);
|
|
||||||
copy.setPixelColor(x,y, clr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
|
|
||||||
}
|
|
||||||
QImage copy = imageData;
|
QImage copy = imageData;
|
||||||
QPoint startPoint;
|
|
||||||
QPoint extrem(0,copy.width()+1);
|
|
||||||
for(int y = 0; y<copy.height(); y++){
|
for(int y = 0; y<copy.height(); y++){
|
||||||
extrem.setY(y);
|
for(int x = 0; x<copy.width(); x++){
|
||||||
startPoint.setY(y);
|
QColor clr = copy.pixelColor(x,y);
|
||||||
//traverse through x dircetion
|
clr.setAlpha(std::min(alpha,clr.alpha()));
|
||||||
for(int x=0; x<copy.width(); x++){
|
copy.setPixelColor(x,y, clr);
|
||||||
startPoint.setX(x);
|
|
||||||
//traverse all edges
|
|
||||||
int cutNumberX = 0;
|
|
||||||
for(size_t i=0; i<polygonData.size()-1; i++){
|
|
||||||
QPoint& start = polygonData[i];
|
|
||||||
QPoint& end = polygonData[i+1];
|
|
||||||
cutNumberX+=IntelliHelper::hasIntersection(startPoint, extrem, start, end);
|
|
||||||
}
|
|
||||||
//check if zhe cutNumber is Even -> not in Polygon
|
|
||||||
if(!(cutNumberX&1)){
|
|
||||||
QColor tmpColor(0,0,0);
|
|
||||||
tmpColor.setAlpha(0);
|
|
||||||
copy.setPixelColor(startPoint,tmpColor);
|
|
||||||
}else{
|
|
||||||
QColor clr = copy.pixelColor(x,y);
|
|
||||||
clr.setAlpha(alpha);
|
|
||||||
copy.setPixelColor(x,y,clr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
|
return copy.scaled(displaySize,Qt::IgnoreAspectRatio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
|
void IntelliShapedImage::setPolygon(const std::vector<QPoint>& polygonData){
|
||||||
if(polygonData.size()<3){
|
if(polygonData.size()<3){
|
||||||
return;
|
this->polygonData.clear();
|
||||||
}
|
}else{
|
||||||
for(auto element:polygonData){
|
this->polygonData.clear();
|
||||||
this->polygonData.push_back(QPoint(element.x(), element.y()));
|
for(auto element:polygonData){
|
||||||
|
this->polygonData.push_back(QPoint(element.x(), element.y()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
calculateVisiblity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
class IntelliShapedImage : public IntelliImage{
|
class IntelliShapedImage : public IntelliImage{
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
virtual void calculateVisiblity() override;
|
||||||
std::vector<QPoint> polygonData;
|
std::vector<QPoint> polygonData;
|
||||||
public:
|
public:
|
||||||
IntelliShapedImage(int weight, int height);
|
IntelliShapedImage(int weight, int height);
|
||||||
|
|||||||
@@ -1,39 +1,4 @@
|
|||||||
#include"IntelliHelper.h"
|
#include"IntelliHelper.h"
|
||||||
#include<algorithm>
|
#include<algorithm>
|
||||||
|
|
||||||
int IntelliHelper::orientation(QPoint& p, QPoint& q, QPoint& r){
|
|
||||||
int value = (q.y()-p.y())*(r.x()-q.x())-
|
|
||||||
(q.x()-p.x())*(r.y()-q.y());
|
|
||||||
if(value==0) return 0;
|
|
||||||
return (value>0)?1:2;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntelliHelper::onSegment(QPoint& p1, QPoint& q, QPoint& p2){
|
|
||||||
return (q.x() >= std::min(p1.x(),p2.x()) && q.x() <= std::max(p1.x(), p2.x()) &&
|
|
||||||
q.y() >= std::min(p1.y(),p2.y()) && q.y() <= std::max(p1.y(), p2.y()));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IntelliHelper::hasIntersection(QPoint& p1, QPoint& q1, QPoint& p2, QPoint& q2){
|
|
||||||
int o1 = IntelliHelper::orientation(p1,q1,p2);
|
|
||||||
int o2 = IntelliHelper::orientation(p1,q1,q2);
|
|
||||||
int o3 = IntelliHelper::orientation(p2,q2,p1);
|
|
||||||
int o4 = IntelliHelper::orientation(p2,q2,q1);
|
|
||||||
|
|
||||||
// General case
|
|
||||||
if (o1 != o2 && o3 != o4)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
|
|
||||||
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
|
|
||||||
|
|
||||||
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
|
|
||||||
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
|
|
||||||
|
|
||||||
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
|
|
||||||
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
|
|
||||||
|
|
||||||
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
|
|
||||||
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
|
|
||||||
|
|
||||||
return false; // Doesn't fall in any of the above cases
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,18 +5,27 @@
|
|||||||
|
|
||||||
|
|
||||||
class IntelliHelper{
|
class IntelliHelper{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//checks for orientation:
|
|
||||||
// 0 - colinear
|
|
||||||
// 1 - clockwise
|
|
||||||
// 2 - counter clockwise
|
|
||||||
static int orientation(QPoint& p1, QPoint& p2, QPoint& p3);
|
|
||||||
|
|
||||||
//checks if q is on segment p1-p2
|
static inline float sign(QPoint& p1, QPoint& p2, QPoint& p3){
|
||||||
static bool onSegment(QPoint& p1, QPoint& q, QPoint& p2);
|
return (p1.x()-p3.x())*(p2.y()-p3.y())-(p2.x()-p3.x())*(p1.y()-p3.y());
|
||||||
|
}
|
||||||
|
|
||||||
//cheks if p1-q1 intersects with p2-q2
|
|
||||||
static bool hasIntersection(QPoint& p1, QPoint& q1, QPoint& p2, QPoint& q2);
|
static inline bool isInTriangle(QPoint& A, QPoint& B, QPoint& C, QPoint& P){
|
||||||
|
float val1, val2, val3;
|
||||||
|
bool neg, pos;
|
||||||
|
|
||||||
|
val1 = IntelliHelper::sign(P,A,B);
|
||||||
|
val2 = IntelliHelper::sign(P,B,C);
|
||||||
|
val3 = IntelliHelper::sign(P,C,A);
|
||||||
|
|
||||||
|
neg = (val1<0.f) || (val2<0.f) || (val3<0.f);
|
||||||
|
pos = (val1>0.f) || (val2>0.f) || (val3>0.f);
|
||||||
|
|
||||||
|
return !(neg && pos);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,26 +10,18 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <QPoint>
|
#include <QPoint>
|
||||||
|
|
||||||
#define EXPORT
|
|
||||||
|
|
||||||
PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
|
PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
|
||||||
:QWidget(parent){
|
:QWidget(parent){
|
||||||
this->setUp(maxWidth, maxHeight);
|
this->setUp(maxWidth, maxHeight);
|
||||||
|
|
||||||
#ifdef EXPORT
|
|
||||||
this->addLayer(maxWidth, maxHeight);
|
|
||||||
layerStructure[0].image->floodFill(QColor(255,255,255,255));
|
|
||||||
activeLayer=0;
|
|
||||||
#endif
|
|
||||||
#ifndef EXPORT
|
|
||||||
//tetsing
|
//tetsing
|
||||||
this->addLayer(200,200,0,0,ImageType::Shaped_Image);
|
this->addLayer(200,200,0,0,ImageType::Shaped_Image);
|
||||||
layerStructure[0].image->floodFill(QColor(255,0,0,255));
|
layerStructure[0].image->floodFill(QColor(255,0,0,255));
|
||||||
std::vector<QPoint> polygon;
|
std::vector<QPoint> polygon;
|
||||||
polygon.push_back(QPoint(100,0));
|
polygon.push_back(QPoint(100,000));
|
||||||
polygon.push_back(QPoint(200,200));
|
polygon.push_back(QPoint(200,100));
|
||||||
polygon.push_back(QPoint(0,200));
|
polygon.push_back(QPoint(100,200));
|
||||||
polygon.push_back(QPoint(100,0));
|
polygon.push_back(QPoint(000,100));
|
||||||
layerStructure[0].image->setPolygon(polygon);
|
layerStructure[0].image->setPolygon(polygon);
|
||||||
|
|
||||||
this->addLayer(200,200,150,150);
|
this->addLayer(200,200,150,150);
|
||||||
@@ -37,7 +29,6 @@ PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
|
|||||||
layerStructure[1].alpha=200;
|
layerStructure[1].alpha=200;
|
||||||
|
|
||||||
activeLayer=1;
|
activeLayer=1;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::setUp(int maxWidth, int maxHeight){
|
void PaintingArea::setUp(int maxWidth, int maxHeight){
|
||||||
@@ -45,7 +36,6 @@ void PaintingArea::setUp(int maxWidth, int maxHeight){
|
|||||||
this->maxWidth = maxWidth;
|
this->maxWidth = maxWidth;
|
||||||
this->maxHeight = maxHeight;
|
this->maxHeight = maxHeight;
|
||||||
Canvas = new QImage(maxWidth,maxHeight, QImage::Format_ARGB32);
|
Canvas = new QImage(maxWidth,maxHeight, QImage::Format_ARGB32);
|
||||||
Canvas->fill(Qt::GlobalColor::white);
|
|
||||||
|
|
||||||
// Roots the widget to the top left even if resized
|
// Roots the widget to the top left even if resized
|
||||||
setAttribute(Qt::WA_StaticContents);
|
setAttribute(Qt::WA_StaticContents);
|
||||||
@@ -69,11 +59,11 @@ int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOff
|
|||||||
}
|
}
|
||||||
newLayer.alpha = 255;
|
newLayer.alpha = 255;
|
||||||
this->layerStructure.push_back(newLayer);
|
this->layerStructure.push_back(newLayer);
|
||||||
return layerStructure.size()-1;
|
return static_cast<int>(layerStructure.size())-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::deleteLayer(int index){
|
void PaintingArea::deleteLayer(int index){
|
||||||
if(index<layerStructure.size()){
|
if(index<static_cast<int>(layerStructure.size())){
|
||||||
this->layerStructure.erase(layerStructure.begin()+index);
|
this->layerStructure.erase(layerStructure.begin()+index);
|
||||||
if(activeLayer>=index){
|
if(activeLayer>=index){
|
||||||
activeLayer--;
|
activeLayer--;
|
||||||
@@ -82,26 +72,24 @@ void PaintingArea::deleteLayer(int index){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::deleteActiveLayer(){
|
void PaintingArea::deleteActiveLayer(){
|
||||||
|
if(activeLayer>=0 && activeLayer < static_cast<int>(layerStructure.size())){
|
||||||
this->layerStructure.erase(layerStructure.begin()+activeLayer);
|
this->layerStructure.erase(layerStructure.begin()+activeLayer);
|
||||||
activeLayer--;
|
activeLayer--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::setLayerToActive(int index) {
|
void PaintingArea::setLayerToActive(int index) {
|
||||||
if(index<layerStructure.size()){
|
if(index>=0&&index<static_cast<int>(layerStructure.size())){
|
||||||
this->activeLayer=index;
|
this->activeLayer=index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::setAlphaToLayer(int index, int alpha){
|
void PaintingArea::setAlphaToLayer(int index, int alpha){
|
||||||
if(index<layerStructure.size()){
|
if(index>=0&&index<static_cast<int>(layerStructure.size())){
|
||||||
layerStructure[index].alpha=alpha;
|
layerStructure[static_cast<size_t>(index)].alpha=alpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap PaintingArea::getAsPixmap(){
|
|
||||||
assembleLayers();
|
|
||||||
return QPixmap::fromImage(*Canvas);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used to load the image and place it in the widget
|
// Used to load the image and place it in the widget
|
||||||
bool PaintingArea::openImage(const QString &fileName)
|
bool PaintingArea::openImage(const QString &fileName)
|
||||||
@@ -109,7 +97,7 @@ bool PaintingArea::openImage(const QString &fileName)
|
|||||||
if(this->activeLayer==-1){
|
if(this->activeLayer==-1){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
IntelliImage* active = layerStructure[activeLayer].image;
|
IntelliImage* active = layerStructure[static_cast<size_t>(activeLayer)].image;
|
||||||
bool open = active->loadImage(fileName);
|
bool open = active->loadImage(fileName);
|
||||||
update();
|
update();
|
||||||
return open;
|
return open;
|
||||||
@@ -133,7 +121,6 @@ bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (Canvas->save(fileName, fileFormat)) {
|
if (Canvas->save(fileName, fileFormat)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@@ -144,13 +131,13 @@ bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
|
|||||||
// Used to change the pen color
|
// Used to change the pen color
|
||||||
void PaintingArea::setPenColor(const QColor &newColor)
|
void PaintingArea::setPenColor(const QColor &newColor)
|
||||||
{
|
{
|
||||||
myPenColor = newColor;
|
//TODO give to Tool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to change the pen width
|
// Used to change the pen width
|
||||||
void PaintingArea::setPenWidth(int newWidth)
|
void PaintingArea::setPenWidth(int newWidth)
|
||||||
{
|
{
|
||||||
myPenWidth = newWidth;
|
//TODO give to Tool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color the image area with white
|
// Color the image area with white
|
||||||
@@ -158,48 +145,50 @@ void PaintingArea::clearImage(int r, int g, int b){
|
|||||||
if(this->activeLayer==-1){
|
if(this->activeLayer==-1){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
IntelliImage* active = layerStructure[activeLayer].image;
|
IntelliImage* active = layerStructure[static_cast<size_t>(activeLayer)].image;
|
||||||
active->floodFill(QColor(r, g, b, 255));
|
active->floodFill(QColor(r, g, b, 255));
|
||||||
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::activate(int a){
|
void PaintingArea::activate(int a){
|
||||||
this->setLayerToActive(a);
|
if(a>=0 && a < static_cast<int>(layerStructure.size())){
|
||||||
|
this->setLayerToActive(a);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::setAlpha(int a){
|
void PaintingArea::setAlpha(int a){
|
||||||
if(activeLayer>=0){
|
if(activeLayer>=0){
|
||||||
layerStructure[activeLayer].alpha=a;
|
layerStructure[static_cast<size_t>(activeLayer)].alpha=a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveUp(int a){
|
void PaintingArea::getMoveUp(int a){
|
||||||
layerStructure[activeLayer].heightOffset-=a;
|
layerStructure[static_cast<size_t>(activeLayer)].heightOffset-=a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveDown(int a){
|
void PaintingArea::getMoveDown(int a){
|
||||||
layerStructure[activeLayer].heightOffset+=a;
|
layerStructure[static_cast<size_t>(activeLayer)].heightOffset+=a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveRight(int a){
|
void PaintingArea::getMoveRight(int a){
|
||||||
layerStructure[activeLayer].widthOffset+=a;
|
layerStructure[static_cast<size_t>(activeLayer)].widthOffset+=a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveLeft(int a){
|
void PaintingArea::getMoveLeft(int a){
|
||||||
layerStructure[activeLayer].widthOffset-=a;
|
layerStructure[static_cast<size_t>(activeLayer)].widthOffset-=a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveLayerUp(){
|
void PaintingArea::getMoveLayerUp(){
|
||||||
if(activeLayer<layerStructure.size()-1 && activeLayer>=0){
|
if(activeLayer<static_cast<int>(layerStructure.size()-1) && activeLayer>=0){
|
||||||
std::swap(layerStructure[activeLayer], layerStructure[activeLayer+1]);
|
std::swap(layerStructure[static_cast<size_t>(activeLayer)], layerStructure[static_cast<size_t>(activeLayer+1)]);
|
||||||
activeLayer++;
|
activeLayer++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::getMoveLayerDown(){
|
void PaintingArea::getMoveLayerDown(){
|
||||||
if(activeLayer>0){
|
if(activeLayer>0){
|
||||||
std::swap(layerStructure[activeLayer], layerStructure[activeLayer-1]);
|
std::swap(layerStructure[static_cast<size_t>(activeLayer)], layerStructure[static_cast<size_t>(activeLayer-1)]);
|
||||||
activeLayer--;
|
activeLayer--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -209,18 +198,7 @@ void PaintingArea::getMoveLayerDown(){
|
|||||||
// Set that we are currently drawing
|
// Set that we are currently drawing
|
||||||
void PaintingArea::mousePressEvent(QMouseEvent *event)
|
void PaintingArea::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
if (event->button() == Qt::LeftButton) {
|
//TODO implement in tool
|
||||||
if(this->activeLayer==-1){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LayerObject& active = layerStructure[activeLayer];
|
|
||||||
|
|
||||||
int x = event->x()-active.widthOffset;
|
|
||||||
int y = event->y()-active.heightOffset;
|
|
||||||
//TODO CALCULATE LAST POINT
|
|
||||||
lastPoint=QPoint(x,y);
|
|
||||||
scribbling = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -229,38 +207,13 @@ void PaintingArea::mousePressEvent(QMouseEvent *event)
|
|||||||
// from the last position to the current
|
// from the last position to the current
|
||||||
void PaintingArea::mouseMoveEvent(QMouseEvent *event)
|
void PaintingArea::mouseMoveEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
if ((event->buttons() & Qt::LeftButton) && scribbling){
|
//TODO implement in Tool
|
||||||
if(this->activeLayer==-1){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LayerObject& active = layerStructure[activeLayer];
|
|
||||||
|
|
||||||
int x = event->x()-active.widthOffset;
|
|
||||||
int y = event->y()-active.heightOffset;
|
|
||||||
|
|
||||||
//TODO CALCULATE NEW POINT
|
|
||||||
drawLineTo(QPoint(x,y));
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the button is released we set variables to stop drawing
|
// If the button is released we set variables to stop drawing
|
||||||
void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
|
void PaintingArea::mouseReleaseEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
if (event->button() == Qt::LeftButton && scribbling) {
|
//TODO implement in tool
|
||||||
if(this->activeLayer==-1){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LayerObject& active = layerStructure[activeLayer];
|
|
||||||
|
|
||||||
int x = event->x()-active.widthOffset;
|
|
||||||
int y = event->y()-active.heightOffset;
|
|
||||||
|
|
||||||
//TODO CALCULATE NEW POINT
|
|
||||||
drawLineTo(QPoint(x,y));
|
|
||||||
update();
|
|
||||||
scribbling = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QPainter provides functions to draw on the widget
|
// QPainter provides functions to draw on the widget
|
||||||
@@ -280,32 +233,17 @@ void PaintingArea::paintEvent(QPaintEvent *event)
|
|||||||
// to cut down on the need to resize the image
|
// to cut down on the need to resize the image
|
||||||
void PaintingArea::resizeEvent(QResizeEvent *event)
|
void PaintingArea::resizeEvent(QResizeEvent *event)
|
||||||
{
|
{
|
||||||
if(this->activeLayer==-1){
|
//TODO wait till tool works
|
||||||
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);
|
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::drawLineTo(const QPoint &endPoint)
|
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);
|
|
||||||
lastPoint = endPoint;
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::resizeImage(QImage *image_res, const QSize &newSize){
|
void PaintingArea::resizeImage(QImage *image_res, const QSize &newSize){
|
||||||
image_res->scaled(newSize,Qt::IgnoreAspectRatio);
|
//TODO implement
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintingArea::assembleLayers(bool forSaving){
|
void PaintingArea::assembleLayers(bool forSaving){
|
||||||
@@ -314,20 +252,23 @@ void PaintingArea::assembleLayers(bool forSaving){
|
|||||||
}else{
|
}else{
|
||||||
Canvas->fill(Qt::GlobalColor::black);
|
Canvas->fill(Qt::GlobalColor::black);
|
||||||
}
|
}
|
||||||
//TODO interpolation of alpha for saving
|
|
||||||
for(size_t i=0; i<layerStructure.size(); i++){
|
for(size_t i=0; i<layerStructure.size(); i++){
|
||||||
LayerObject layer = layerStructure[i];
|
LayerObject layer = layerStructure[i];
|
||||||
QImage cpy = layer.image->getDisplayable(layer.alpha);
|
QImage cpy = layer.image->getDisplayable(layer.alpha);
|
||||||
QColor clr_0;
|
QColor clr_0;
|
||||||
QColor clr_1;
|
QColor clr_1;
|
||||||
for(int y=0; y<layer.height; y++){
|
for(int y=0; y<layer.height; y++){
|
||||||
|
if(layer.heightOffset+y<0) continue;
|
||||||
|
if(layer.heightOffset+y>=maxHeight) break;
|
||||||
for(int x=0; x<layer.width; x++){
|
for(int x=0; x<layer.width; x++){
|
||||||
|
if(layer.widthOffset+x<0) continue;
|
||||||
|
if(layer.heightOffset+y>=maxWidth) break;
|
||||||
clr_0=Canvas->pixelColor(layer.widthOffset+x, layer.heightOffset+y);
|
clr_0=Canvas->pixelColor(layer.widthOffset+x, layer.heightOffset+y);
|
||||||
clr_1=cpy.pixelColor(x,y);
|
clr_1=cpy.pixelColor(x,y);
|
||||||
float t = (float)clr_1.alpha()/255.f;
|
float t = static_cast<float>(clr_1.alpha())/255.f;
|
||||||
int r =(float)clr_1.red()*(t)+(float)clr_0.red()*(1.-t);
|
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 =(float)clr_1.green()*(t)+(float)clr_0.green()*(1.-t);
|
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 =(float)clr_1.blue()*(t)+(float)clr_0.blue()*(1.-t);
|
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);
|
int a =std::min(clr_0.alpha()+clr_1.alpha(), 255);
|
||||||
clr_0.setRed(r);
|
clr_0.setRed(r);
|
||||||
clr_0.setGreen(g);
|
clr_0.setGreen(g);
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ public:
|
|||||||
int penWidth() const { return myPenWidth; }
|
int penWidth() const { return myPenWidth; }
|
||||||
|
|
||||||
|
|
||||||
QPixmap getAsPixmap();
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
// Events to handle
|
// Events to handle
|
||||||
|
|||||||
Reference in New Issue
Block a user