22from random import randint
33
44from PyQt5 .QtCore import QPoint , Qt
5- from PyQt5 .QtGui import QColor , QCursor , QIcon , QImage , QPainter , QPen , QPixmap
5+ from PyQt5 .QtGui import QColor , QCursor , QIcon , QImage , QPainter , QPen , QPixmap ,\
6+ QBrush
67from PyQt5 .QtWidgets import (QAction , QApplication , QFileDialog ,
7- QMainWindow , QPushButton )
8+ QMainWindow , QPushButton , )
89
910# QBrush
1011
@@ -23,7 +24,6 @@ def paintEvent(self, event):
2324
2425class ColorBox (QMainWindow ):
2526 """ """
26-
2727 def __init__ (self , parent = None ):
2828 super ().__init__ (parent )
2929 self .toolsX = 5
@@ -55,11 +55,10 @@ def closeEvent(self, *args, **kwargs):
5555 self .exists = False
5656 print ("Closing color pallet window" )
5757
58-
5958class Tool :
6059 def __init__ (self , toolName , brushSize , color ,
6160 paintPattern , PaintBoard , iconPath , shortcut , statusTip ,
62- isDipped = False , toolDead = False
61+ duration , isDipped = False , toolDead = False
6362 ):
6463 """class for creating drawing tools
6564
@@ -84,6 +83,7 @@ def __init__(self, toolName, brushSize, color,
8483 self .iconPath = iconPath
8584 self .shortcut = shortcut
8685 self .statusTip = statusTip
86+ self .duration = duration
8787 self .toolDead = toolDead
8888
8989 self .create_button ()
@@ -98,61 +98,50 @@ def create_button(self):
9898 tool_btn .setShortcut (self .shortcut )
9999 tool_btn .setStatusTip (self .statusTip )
100100 tool_btn .triggered .connect (
101- lambda : self .PaintBoard .connectTool (self ,
102- self .color ,
103- self .paintPattern )
101+ lambda : self .PaintBoard .connectTool (self )
104102 )
105103 self .PaintBoard .toolbar .addAction (tool_btn )
106104
107-
108105class PalletteButton :
109106 """Class for color pallete; allows for mixing color"""
110-
111107 def __init__ (self ):
112108 self .r = randint (0 , 255 )
113109 self .g = randint (0 , 255 )
114110 self .b = randint (0 , 255 )
115- self .alpha = 1.0
116- self .color = self .r , self .g , self .b , self .alpha
111+ self .alpha = 255
112+ self .palletteColor = self .r , self .g , self .b , self .alpha
117113 # otherwise it won't be possible to display color
118114 # in pallettes down below
119115
120116 def mixColor (self , tool ):
121117 if tool is None :
122- return
118+ return # make it not crash
123119
124- print (tool .color .green (), tool .color )
120+ print (tool .color .red (), tool . color . green (), tool .color . blue () )
125121 # TODO: pointy pen & no tool crashes upon clicking here with it
126- if tool .toolName in ["A Bucket" , "Straggly Paintbrush" , "Solid Brush" ]:
127- # tool[r,g,b,a]
128- # if not(sum([tool.color.red(),
129- # tool.color.green(), tool.color.blue()])
130- # and tool.color.alpha()):
131- # tool.color = QColor(self.r, self.g, self.b, self.alpha)
132- # print(tool.color)
133- # else: # perhaps don't divide by 4
134- mixedColor = (
135- self .r - (max (self .r , tool .color .red () // 4 ) -
136- min (self .r , tool .color .red () // 4 )
137- ),
138- self .g - (max (self .g , tool .color .green () // 4 ) -
139- min (self .g , tool .color .green () // 4 )
140- ),
141- self .b - (max (self .b , tool .color .blue () // 4 ) -
142- min (self .b , tool .color .blue () // 4 )
143- ),
144- 1.0
145- )
146- self .r = mixedColor [0 ]
147- self .b = mixedColor [1 ]
148- self .g = mixedColor [2 ]
149- self .alpha = mixedColor [3 ]
150- self .color = mixedColor
151- tool .color = QColor (mixedColor [0 ], mixedColor [1 ],
152- mixedColor [2 ], mixedColor [3 ])
153- if tool .toolName in ["Straggly Paintbrush" ,
154- "Solid Brush" ]:
155- tool .isDipped = True
122+ if tool .toolName in ["A Bucket" , "Straggly Paintbrush" ,
123+ "Solid Brush" ]:
124+ # tool[r,b,g]
125+ if not (sum ([tool .color .red (), tool .color .green (),
126+ tool .color .blue ()]) and tool .color .alpha ())\
127+ and self .alpha :
128+ # self.alpha so that color pallette is not empty
129+ tool .color = QColor (self .r , self .g , self .b , 255 )
130+ else :
131+ mixedColor = (
132+ (self .r + tool .color .red ())// 2 ,
133+ (self .g + tool .color .green ())// 2 ,
134+ (self .b + tool .color .blue ())// 2 ,
135+ 255
136+ )
137+ self .r = mixedColor [0 ]
138+ self .g = mixedColor [1 ]
139+ self .b = mixedColor [2 ]
140+ self .palletteColor = (self .r , self .g , self .b )
141+ tool .color = QColor (self .r ,self .g ,self .b ) # the error is caused by this
142+ if tool .toolName in ["Straggly Paintbrush" ,
143+ "Solid Brush" ]:
144+ tool .isDipped = True
156145 # perhaps colorBox.update()
157146
158147
@@ -171,7 +160,7 @@ def Setup(self):
171160 self .connectTool ()
172161 self .painter = QPainter (self .canvas )
173162
174- # TODO: custom paintPatterns
163+ # TODO: custom paintPatterns
175164
176165 mainMenu = self .menuBar ()
177166
@@ -208,52 +197,63 @@ def Setup(self):
208197 self .toolbar = self .addToolBar ("Toolbar" )
209198 self .toolbar .setStyleSheet ('background-color: white' )
210199
211- self .pointy_pen = Tool ("Pointy Pen" , 1 , QColor () ,
212- [randint (4 , 8 ), randint (5 , 10 ),
213- randint (1 , 5 ), randint ( 0 , 5 )], self ,
200+ self .pointy_pen = Tool ("Pointy Pen" , 1 , Qt . black ,
201+ [randint (1 , 4 ), randint ( 1 , 2 ), randint (0 , 3 ),
202+ randint (0 , 5 )], self ,
214203 "Design/icons/Pointy Pen.png" ,
215- "CTRL+P" , "A very pointy pen"
204+ "CTRL+P" , "A very pointy pen" ,
205+ randint (1 , 15 )
216206 )
217- # they shouldn't have any color in the beggining
218- # alpha decrease -=1 ; tuple required
219- self .fill = Tool ("A Bucket" , 2000 , QColor (),
207+
208+ #they shouldn't have any color in the beggining
209+ #alpha decrease -=1 ; tuple required
210+ self .fill = Tool ("A Bucket" , 300 , QColor (0 ,0 ,0 ,0.0 ),
220211 [1 , 1 , 1 , 1 ], self ,
221212 'Design/icons/A bucket.png' ,
222- "CTRL+B" , "A bucket"
213+ "CTRL+B" , "A bucket" ,
214+ 1
223215 )
224216
225217 self .straggly_paintbrush = Tool ("Straggly Paintbrush" ,
226- 10 , QColor (),
227- [randint (4 , 8 ), randint (5 , 10 ),
228- randint (2 , 5 ), randint (0 , 5 )],
218+ 10 , QColor (0 , 0 , 0 , 0.0 ),
219+ [randint (1 , 4 ), randint (1 , 2 ),
220+ randint (0 , 3 ), randint (0 , 5 )],
229221 self ,
230222 "Design/icons/Straggly Paintbrush.png" ,
231- "CTRL+A" , "A very Straggly Paintbrush."
223+ "CTRL+A" , "A very Straggly Paintbrush." ,
224+ 9999 #randint(5,30)
232225 )
233226
234- self .solidifed_brush = Tool ("Solid Brush" , 10 , QColor (),
235- [randint (1 , 4 ), randint (5 , 10 ),
236- randint (2 , 5 ), randint (0 , 5 )], self ,
227+ self .solidifed_brush = Tool ("Solid Brush" , 10 , QColor (0 , 0 , 0 , 0.0 ),
228+ [randint (1 , 4 ), randint (1 , 2 ),
229+ randint (0 , 3 ), randint (0 , 5 )], self ,
237230 'Design/icons/Solid Brush.png' ,
238231 "CTRL+J" , "Gosh, that is a hard tip" ,
232+ 1
239233 )
240234
241235 self .eraser = Tool ("Eraser" , 10 , Qt .white ,
242236 [0 , 0 , 0 , 0.0 ], self , "" , "Ctrl+F" ,
243237 "Erase Your Mistakes, Kid!" , True )
244- # duration will have to be infinte here
238+ # duration will have to be infinte here
245239
246240 self .show ()
247241
248242 self .drawing = False
249243 self .lastPoint = QPoint ()
250244
251- def connectTool (self , curTool = None , color = None , paintPattern = [ 4 , 5 , 6 , 2 ] ):
245+ def connectTool (self , curTool = None ):
252246
253247 self .currentTool = curTool
254- self .currentToolDuration = randint (1 , 10 )
255- self .currentBrushColor = color
256- self .currentBrushPattern = paintPattern
248+
249+ """if not self.currentTool.duration:
250+ if self.currentTool.toolName == "Pointy Pen":
251+ self.currentTool.duration = randint(0, 15)
252+ elif self.currentTool.toolName == "Straggly Paintbrush":
253+ self.currentTool.duration = randint(5, 30)
254+ else:
255+ self.currenTool.duration = 1
256+ self.currentTool.toolDead = False"""
257257
258258 self .setCursor (QCursor (
259259 QPixmap ("Design/icons/{}.png" .format (self .currentTool .toolName
@@ -282,67 +282,63 @@ def colorBoxRun(self):
282282 c6 = PalletteButton ()
283283
284284 p1 .setStyleSheet ("background-color: rgba{0}; border-radius:50px"
285- .format (c1 .color ))
285+ .format (c1 .palletteColor ))
286286 p1 .clicked .connect (lambda : c1 .mixColor (self .currentTool ))
287287 colorBox .addPallette (p1 )
288288 p2 .setStyleSheet ("background-color: rgba{0}"
289- .format (c2 .color ))
289+ .format (c2 .palletteColor ))
290290 p2 .clicked .connect (lambda : c2 .mixColor (self .currentTool ))
291291 colorBox .addPallette (p2 )
292292 p3 .setStyleSheet ("background-color: rgba{0}"
293- .format (c3 .color ))
293+ .format (c3 .palletteColor ))
294294 p3 .clicked .connect (lambda : c3 .mixColor (self .currentTool ))
295295 colorBox .addPallette (p3 )
296296 p4 .setStyleSheet ("background-color: rgba{0};"
297- .format (c4 .color ))
297+ .format (c4 .palletteColor ))
298298 p4 .clicked .connect (lambda : c4 .mixColor (self .currentTool ))
299299 colorBox .addPallette (p4 )
300300 p5 .setStyleSheet ("background-color: rgba{0}"
301- .format (c5 .color ))
301+ .format (c5 .palletteColor ))
302302 p5 .clicked .connect (lambda : c5 .mixColor (self .currentTool ))
303303 colorBox .addPallette (p5 )
304304 p6 .setStyleSheet ("background-color: rgba{0}"
305- .format (c6 .color ))
305+ .format (c6 .palletteColor ))
306306 p6 .clicked .connect (lambda : c6 .mixColor (self .currentTool ))
307307 colorBox .addPallette (p6 )
308308
309309 # showing toolBox
310310 colorBox .showColorBox ()
311311
312312 def mousePressEvent (self , event ):
313- if event .button () == Qt .LeftButton and self . currentTool is not None :
313+ if event .button () == Qt .LeftButton :
314314 self .drawing = True
315315 if self .currentTool .toolName == "A Bucket" :
316- Pen = QPen ()
316+ Pen = QPen (self . currentTool . color )
317317 Pen .setWidth (self .currentTool .brushSize )
318- Pen .setColor (self .currentTool .color )
318+ # Pen.setColor(self.currentTool.color)
319319 self .painter .setPen (Pen )
320320 # self.painter.drawLine(0, 0, 1000, 500)
321- self .painter .drawLine ( 0 , 0 , 100 , 150 )
321+ self .painter .drawEllipse ( event . pos () , 100 , 150 )
322322 self .lastPoint = event .pos ()
323323 self .update ()
324324
325325 def mouseMoveEvent (self , event ):
326326 if (event .buttons () and Qt .LeftButton ) and \
327- self .drawing and self .currentTool is not None :
327+ self .drawing and self .currentTool . toolName is not None :
328328
329329 Pen = QPen ()
330- if self .currentTool .toolName != "Eraser" and \
331- self .currentTool .toolName != "A Bucket" :
332- if self .currentToolDuration <= 0.0 :
333- self .currentTool .toolDead = True
330+ if self .currentTool .toolName != "Eraser" :
331+ if self .currentTool .duration <= 0.0 :
332+ self .toolDead = True
334333 print ('Tools Died' )
335- self .currentToolDuration = 0
334+ self .currentTool . duration = 0
336335 Pen .setDashPattern ([0 , 0 , 0 , 0 ])
337336 self .drawing = False
338337 else :
339- self .currentTool . toolDead = False
340- self .currentToolDuration -= 0.05
338+ self .toolDead = False
339+ self .currentTool . duration -= 0.1
341340 # print(self.currentToolDuration)
342341
343- if self .currentToolDuration < 2 :
344- Pen .setDashPattern (self .currentBrushPattern )
345-
346342 if self .currentTool .toolName == "Pointy Pen" :
347343 Pen .setCapStyle (Qt .RoundCap )
348344 Pen .setJoinStyle (Qt .BevelJoin )
@@ -351,15 +347,14 @@ def mouseMoveEvent(self, event):
351347 Pen .setCapStyle (Qt .SquareCap )
352348 Pen .setJoinStyle (Qt .MiterJoin )
353349
354- Pen .setColor (self .currentBrushColor )
350+ Pen .setColor (self .currentTool . color )
355351 Pen .setWidth (self .currentTool .brushSize )
356352 self .painter .setPen (Pen )
357- if self .currentTool . toolDead is True :
353+ if self .toolDead is True :
358354 if self .currentTool .toolName == "Pointy Pen" :
359355 self .setCursor (QCursor (
360356 QPixmap ("Design/icons/Pointy Pen Broken.png" )))
361- # if event.pos().y() > 53 and
362- # self.currentTool.toolName is not None:
357+ # if event.pos().y() > 53 and self.currentTool.toolName is not None:
363358 self .painter .drawLine (self .lastPoint , event .pos ())
364359 self .lastPoint = event .pos ()
365360 self .update ()
@@ -371,7 +366,7 @@ def mouseRealeaseEvent(self, event):
371366 def paintEvent (self , event ):
372367 canvas_painter = QPainter (self )
373368 canvas_painter .drawImage (self .rect (),
374- self .canvas , self .canvas .rect ())
369+ self .canvas , self .canvas .rect ())
375370
376371 def newCanvas (self ):
377372 # TODO: Add New Canvas
0 commit comments