IntelliPhoto  0.4
PaintingArea.cpp
Go to the documentation of this file.
1 // ---------- PaintingArea.cpp ----------
2 #include "string.h"
3 
4 #include <vector>
5 
6 #include <QtWidgets>
7 #include <QPoint>
8 #include <QRect>
9 
10 #include "PaintingArea.h"
13 #include "Tool/IntelliToolPen.h"
14 #include "Tool/IntelliToolPlain.h"
15 #include "Tool/IntelliToolLine.h"
16 #include "Tool/IntelliToolCircle.h"
19 
20 PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
21  :QWidget(parent){
22  //test yout tool here and reset after accomplished test
23  this->Tool = new IntelliToolFloodFill(this, &colorPicker);
24  this->setUp(maxWidth, maxHeight);
25  //tetsing
26  this->addLayer(200,200,0,0,ImageType::Shaped_Image);
27  layerBundle[0].image->drawPlain(QColor(255,0,0,255));
28  std::vector<QPoint> polygon;
29  polygon.push_back(QPoint(100,000));
30  polygon.push_back(QPoint(200,100));
31  polygon.push_back(QPoint(100,200));
32  polygon.push_back(QPoint(000,100));
33  layerBundle[0].image->setPolygon(polygon);
34 
35  this->addLayer(200,200,150,150);
36  layerBundle[1].image->drawPlain(QColor(0,255,0,255));
37  layerBundle[1].alpha=200;
38 
39  activeLayer=1;
40 }
41 
43  delete Tool;
44 }
45 
46 void PaintingArea::setUp(int maxWidth, int maxHeight){
47  //set standart parameter
48  this->maxWidth = maxWidth;
49  this->maxHeight = maxHeight;
50  Canvas = new QImage(maxWidth,maxHeight, QImage::Format_ARGB32);
51 
52  // Roots the widget to the top left even if resized
53  setAttribute(Qt::WA_StaticContents);
54 
55 }
56 
57 int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset, ImageType type){
58  LayerObject newLayer;
59  newLayer.width = width;
60  newLayer.hight = height;
61  newLayer.widthOffset = widthOffset;
62  newLayer.hightOffset = heightOffset;
63  if(type==ImageType::Raster_Image){
64  newLayer.image = new IntelliRasterImage(width,height);
65  }else if(type==ImageType::Shaped_Image){
66  newLayer.image = new IntelliShapedImage(width, height);
67  }
68  newLayer.alpha = 255;
69  this->layerBundle.push_back(newLayer);
70  return static_cast<int>(layerBundle.size())-1;
71 }
72 
73 
74 void PaintingArea::deleteLayer(int index){
75  if(index<static_cast<int>(layerBundle.size())){
76  this->layerBundle.erase(layerBundle.begin()+index);
77  if(activeLayer>=index){
78  activeLayer--;
79  }
80  }
81 }
82 
84  if(activeLayer>=0 && activeLayer < static_cast<int>(layerBundle.size())){
85  this->layerBundle.erase(layerBundle.begin()+activeLayer);
86  activeLayer--;
87  }
88 }
89 
91  if(index>=0&&index<static_cast<int>(layerBundle.size())){
92  this->activeLayer=index;
93  }
94 }
95 
96 void PaintingArea::setAlphaOfLayer(int index, int alpha){
97  if(index>=0&&index<static_cast<int>(layerBundle.size())){
98  layerBundle[static_cast<size_t>(index)].alpha=alpha;
99  }
100 }
101 
102 // Used to load the image and place it in the widget
103 bool PaintingArea::open(const QString &fileName){
104  if(this->activeLayer==-1){
105  return false;
106  }
107  IntelliImage* active = layerBundle[static_cast<size_t>(activeLayer)].image;
108  bool open = active->loadImage(fileName);
109  active->calculateVisiblity();
110  update();
111  return open;
112 }
113 
114 // Save the current image
115 bool PaintingArea::save(const QString &fileName, const char *fileFormat){
116  if(layerBundle.size()==0){
117  return false;
118  }
119  this->assembleLayers(true);
120 
121  if(!strcmp(fileFormat,"PNG")){
122  QImage visibleImage = Canvas->convertToFormat(QImage::Format_Indexed8);
123  fileFormat = "png";
124  if (visibleImage.save(fileName, fileFormat)) {
125  return true;
126  } else {
127  return false;
128  }
129  }
130 
131  if (Canvas->save(fileName, fileFormat)) {
132  return true;
133  } else {
134  return false;
135  }
136 }
137 
138 // Color the image area with white
139 void PaintingArea::floodFill(int r, int g, int b, int a){
140  if(this->activeLayer==-1){
141  return;
142  }
143  IntelliImage* active = layerBundle[static_cast<size_t>(activeLayer)].image;
144  active->drawPlain(QColor(r, g, b, a));
145  update();
146 }
147 
149  layerBundle[static_cast<size_t>(activeLayer)].widthOffset += x;
150  layerBundle[static_cast<size_t>(activeLayer)].hightOffset += y;
151 }
152 
154  if(idx==1){
155  this->activateUpperLayer();
156  }else if(idx==-1){
157  this->activateLowerLayer();
158  }
159 }
160 
162  if(a>=0 && a < static_cast<int>(layerBundle.size())){
163  this->setLayerToActive(a);
164  }
165 }
166 
168  QColor clr = QColorDialog::getColor(colorPicker.getFirstColor(), nullptr, "Main Color", QColorDialog::DontUseNativeDialog);
169  this->colorPicker.setFirstColor(clr);
170 }
171 
173  QColor clr = QColorDialog::getColor(colorPicker.getSecondColor(), nullptr, "Secondary Color", QColorDialog::DontUseNativeDialog);
174  this->colorPicker.setSecondColor(clr);
175 }
176 
178  this->colorPicker.switchColors();
179 }
180 
182  delete this->Tool;
183  Tool = new IntelliToolPen(this, &colorPicker);
184 }
185 
187  delete this->Tool;
188  Tool = new IntelliToolPlainTool(this, &colorPicker);
189 }
190 
192  delete this->Tool;
193  Tool = new IntelliToolLine(this, &colorPicker);
194 }
195 
196 // If a mouse button is pressed check if it was the
197 // left button and if so store the current position
198 // Set that we are currently drawing
199 void PaintingArea::mousePressEvent(QMouseEvent *event){
200  if(Tool == nullptr)
201  return;
202  int x = event->x()-layerBundle[activeLayer].widthOffset;
203  int y = event->y()-layerBundle[activeLayer].hightOffset;
204  if(event->button() == Qt::LeftButton){
205  Tool->onMouseLeftPressed(x, y);
206  }else if(event->button() == Qt::RightButton){
207  Tool->onMouseRightPressed(x, y);
208  }
209  update();
210 }
211 
212 // When the mouse moves if the left button is clicked
213 // we call the drawline function which draws a line
214 // from the last position to the current
215 void PaintingArea::mouseMoveEvent(QMouseEvent *event){
216  if(Tool == nullptr)
217  return;
218  int x = event->x()-layerBundle[activeLayer].widthOffset;
219  int y = event->y()-layerBundle[activeLayer].hightOffset;
220  Tool->onMouseMoved(x, y);
221  update();
222 }
223 
224 // If the button is released we set variables to stop drawing
225 void PaintingArea::mouseReleaseEvent(QMouseEvent *event){
226  if(Tool == nullptr)
227  return;
228  int x = event->x()-layerBundle[activeLayer].widthOffset;
229  int y = event->y()-layerBundle[activeLayer].hightOffset;
230  if(event->button() == Qt::LeftButton){
231  Tool->onMouseLeftReleased(x, y);
232  }else if(event->button() == Qt::RightButton){
233  Tool->onMouseRightReleased(x, y);
234  }
235  update();
236 }
237 
238 void PaintingArea::wheelEvent(QWheelEvent *event){
239  QPoint numDegrees = event->angleDelta() / 8;
240  if(!numDegrees.isNull()){
241  QPoint numSteps = numDegrees / 15;
242  Tool->onWheelScrolled(numSteps.y()*-1);
243  }
244 }
245 
246 // QPainter provides functions to draw on the widget
247 // The QPaintEvent is sent to widgets that need to
248 // update themselves
249 void PaintingArea::paintEvent(QPaintEvent *event){
250  this->assembleLayers();
251 
252  QPainter painter(this);
253  QRect dirtyRec = event->rect();
254  painter.drawImage(dirtyRec, *Canvas, dirtyRec);
255  update();
256 }
257 
258 // Resize the image to slightly larger then the main window
259 // to cut down on the need to resize the image
260 void PaintingArea::resizeEvent(QResizeEvent *event){
261  //TODO wait till tool works
262  update();
263 }
264 
265 void PaintingArea::resizeImage(QImage *image_res, const QSize &newSize){
266  //TODO implement
267 }
268 
269 void PaintingArea::activateUpperLayer(){
270  if(activeLayer!=-1 && activeLayer<layerBundle.size()-1){
271  std::swap(layerBundle[activeLayer], layerBundle[activeLayer+1]);
272  activeLayer++;
273  }
274 }
275 
276 void PaintingArea::activateLowerLayer(){
277  if(activeLayer!=-1 && activeLayer>0){
278  std::swap(layerBundle[activeLayer], layerBundle[activeLayer-1]);
279  activeLayer--;
280  }
281 }
282 
283 void PaintingArea::assembleLayers(bool forSaving){
284  if(forSaving){
285  Canvas->fill(Qt::GlobalColor::transparent);
286  }else{
287  Canvas->fill(Qt::GlobalColor::black);
288  }
289  for(size_t i=0; i<layerBundle.size(); i++){
290  LayerObject layer = layerBundle[i];
291  QImage cpy = layer.image->getDisplayable(layer.alpha);
292  QColor clr_0;
293  QColor clr_1;
294  for(int y=0; y<layer.hight; y++){
295  if(layer.hightOffset+y<0) continue;
296  if(layer.hightOffset+y>=maxHeight) break;
297  for(int x=0; x<layer.width; x++){
298  if(layer.widthOffset+x<0) continue;
299  if(layer.widthOffset+x>=maxWidth) break;
300  clr_0=Canvas->pixelColor(layer.widthOffset+x, layer.hightOffset+y);
301  clr_1=cpy.pixelColor(x,y);
302  float t = static_cast<float>(clr_1.alpha())/255.f;
303  int r =static_cast<int>(static_cast<float>(clr_1.red())*(t)+static_cast<float>(clr_0.red())*(1.f-t)+0.5f);
304  int g =static_cast<int>(static_cast<float>(clr_1.green())*(t)+static_cast<float>(clr_0.green())*(1.f-t)+0.5f);
305  int b =static_cast<int>(static_cast<float>(clr_1.blue())*(t)+static_cast<float>(clr_0.blue()*(1.f-t))+0.5f);
306  int a =std::min(clr_0.alpha()+clr_1.alpha(), 255);
307  clr_0.setRed(r);
308  clr_0.setGreen(g);
309  clr_0.setBlue(b);
310  clr_0.setAlpha(a);
311 
312  Canvas->setPixelColor(layer.widthOffset+x, layer.hightOffset+y, clr_0);
313  }
314  }
315  }
316 }
317 
318 void PaintingArea::createTempLayerAfter(int idx){
319  if(idx>=0){
320  LayerObject newLayer;
321  newLayer.alpha = 255;
322  newLayer.hight = layerBundle[idx].hight;
323  newLayer.width = layerBundle[idx].width;
324  newLayer.hightOffset = layerBundle[idx].hightOffset;
325  newLayer.widthOffset = layerBundle[idx].widthOffset;
326  newLayer.image = layerBundle[idx].image->getDeepCopy();
327  layerBundle.insert(layerBundle.begin()+idx+1,newLayer);
328  }
329 }
IntelliTool::onMouseRightPressed
virtual void onMouseRightPressed(int x, int y)
A function managing the right click Pressed of a Mouse. Constructing the Canvas to draw on....
Definition: IntelliTool.cpp:14
IntelliTool::onMouseLeftReleased
virtual void onMouseLeftReleased(int x, int y)
A function managing the left click Released of a Mouse. Call this in child classes!
Definition: IntelliTool.cpp:32
ImageType
ImageType
The Types, which an Image can be.
Definition: IntelliImage.h:14
PaintingArea::addLayer
int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type=ImageType::Raster_Image)
Definition: PaintingArea.cpp:57
PaintingArea::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *event) override
Definition: PaintingArea.cpp:225
IntelliTool::onMouseLeftPressed
virtual void onMouseLeftPressed(int x, int y)
A function managing the left click Pressed of a Mouse. Resetting the current draw....
Definition: IntelliTool.cpp:25
IntelliShapedImage.h
PaintingArea::open
bool open(const QString &fileName)
Definition: PaintingArea.cpp:103
LayerObject::widthOffset
int widthOffset
Definition: PaintingArea.h:21
IntelliImage::loadImage
virtual bool loadImage(const QString &fileName)
A function that loads and sclaes an image to the fitting dimensions.
Definition: IntelliImage.cpp:14
PaintingArea::setLayerToActive
void setLayerToActive(int index)
Definition: PaintingArea.cpp:90
PaintingArea::floodFill
void floodFill(int r, int g, int b, int a)
Definition: PaintingArea.cpp:139
IntelliToolPlainTool
Definition: IntelliToolPlain.h:7
IntelliColorPicker::setSecondColor
void setSecondColor(QColor Color)
A function to set the secondary color.
Definition: IntelliColorPicker.cpp:28
IntelliShapedImage
The IntelliShapedImage manages a Shapedimage.
Definition: IntelliShapedImage.h:11
IntelliColorPicker::getSecondColor
QColor getSecondColor()
A function to read the secondary selected color.
Definition: IntelliColorPicker.cpp:20
PaintingArea::save
bool save(const QString &fileName, const char *fileFormat)
Definition: PaintingArea.cpp:115
IntelliColorPicker::switchColors
void switchColors()
A function switching primary and secondary color.
Definition: IntelliColorPicker.cpp:12
IntelliToolFloodFill.h
IntelliImage::getDisplayable
virtual QImage getDisplayable(const QSize &displaySize, int alpha)=0
A function returning the displayable ImageData in a requested transparence and size.
PaintingArea::createPlainTool
void createPlainTool()
Definition: PaintingArea.cpp:186
PaintingArea::wheelEvent
void wheelEvent(QWheelEvent *event) override
Definition: PaintingArea.cpp:238
LayerObject
Definition: PaintingArea.h:17
PaintingArea::deleteLayer
void deleteLayer(int index)
Definition: PaintingArea.cpp:74
PaintingArea::createPenTool
void createPenTool()
Definition: PaintingArea.cpp:181
IntelliToolPlain.h
PaintingArea::mousePressEvent
void mousePressEvent(QMouseEvent *event) override
Definition: PaintingArea.cpp:199
ImageType::Raster_Image
IntelliRasterImage.h
LayerObject::alpha
int alpha
Definition: PaintingArea.h:23
PaintingArea::createLineTool
void createLineTool()
Definition: PaintingArea.cpp:191
IntelliToolLine.h
IntelliToolPen
Definition: IntelliToolPen.h:8
PaintingArea::colorPickerSetSecondColor
void colorPickerSetSecondColor()
Definition: PaintingArea.cpp:172
IntelliTool::onMouseRightReleased
virtual void onMouseRightReleased(int x, int y)
A function managing the right click Released of a Mouse. Merging the Canvas to Active....
Definition: IntelliTool.cpp:21
PaintingArea::colorPickerSetFirstColor
void colorPickerSetFirstColor()
Definition: PaintingArea.cpp:167
PaintingArea::colorPickerSwitchColor
void colorPickerSwitchColor()
Definition: PaintingArea.cpp:177
LayerObject::width
int width
Definition: PaintingArea.h:19
IntelliToolRectangle.h
PaintingArea::~PaintingArea
~PaintingArea() override
Definition: PaintingArea.cpp:42
PaintingArea::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *event) override
Definition: PaintingArea.cpp:215
IntelliColorPicker::setFirstColor
void setFirstColor(QColor Color)
A function to set the primary color.
Definition: IntelliColorPicker.cpp:24
PaintingArea::slotDeleteActiveLayer
void slotDeleteActiveLayer()
Definition: PaintingArea.cpp:83
ImageType::Shaped_Image
PaintingArea::moveActiveLayer
void moveActiveLayer(int idx)
Definition: PaintingArea.cpp:153
PaintingArea::PaintingArea
PaintingArea(int maxWidth=600, int maxHeight=600, QWidget *parent=nullptr)
Definition: PaintingArea.cpp:20
PaintingArea.h
IntelliColorPicker::getFirstColor
QColor getFirstColor()
A function to read the primary selected color.
Definition: IntelliColorPicker.cpp:16
LayerObject::hight
int hight
Definition: PaintingArea.h:20
PaintingArea::slotActivateLayer
void slotActivateLayer(int a)
Definition: PaintingArea.cpp:161
PaintingArea::paintEvent
void paintEvent(QPaintEvent *event) override
Definition: PaintingArea.cpp:249
PaintingArea::setAlphaOfLayer
void setAlphaOfLayer(int index, int alpha)
Definition: PaintingArea.cpp:96
LayerObject::image
IntelliImage * image
Definition: PaintingArea.h:18
PaintingArea::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: PaintingArea.cpp:260
IntelliToolFloodFill
Definition: IntelliToolFloodFill.h:7
IntelliToolPen.h
PaintingArea::movePositionActive
void movePositionActive(int x, int y)
Definition: PaintingArea.cpp:148
IntelliImage
An abstract class which manages the basic IntelliImage operations.
Definition: IntelliImage.h:24
IntelliTool::onMouseMoved
virtual void onMouseMoved(int x, int y)
A function managing the mouse moved event. Call this in child classes!
Definition: IntelliTool.cpp:41
LayerObject::hightOffset
int hightOffset
Definition: PaintingArea.h:22
IntelliImage::calculateVisiblity
virtual void calculateVisiblity()=0
An abstract function that calculates the visiblity of the Image data if needed.
IntelliTool::onWheelScrolled
virtual void onWheelScrolled(int value)
A function managing the scroll event. A Positive Value means scrolling outwards. Call this in child c...
Definition: IntelliTool.cpp:46
IntelliRasterImage
The IntelliRasterImage manages a Rasterimage.
Definition: IntelliRasterImage.h:9
IntelliToolCircle.h
IntelliImage::drawPlain
virtual void drawPlain(const QColor &color)
A function that clears the whole image in a given Color.
Definition: IntelliImage.cpp:77
IntelliToolLine
Definition: IntelliToolLine.h:12