Skip to content

Commit

Permalink
add custom cursor for eraser
Browse files Browse the repository at this point in the history
  • Loading branch information
rsusik committed May 13, 2021
1 parent ff63fc4 commit 6810494
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
compileone:
pyinstaller --onefile -F --add-data "./utils/*:utils" screenpen.pyw
pyinstaller --onefile -F --add-data "./screenpen/utils/*:utils" screenpen/screenpen.py

clean:
rm -rf dist build screenpen.egg-info

setup: clean
python setup.py sdist bdist_wheel

pipinstall:
rm -rf screenpen.egg-info && python -m pip install -e .

pipwheel:
rm -rf screenpen.egg-info && python -m pip wheel install -e . -w wheels

setuppip: clean
rm -rf dist build && python setup.py sdist bdist_wheel && twine upload dist/*

test: clean
pip uninstall -y screenpen && pip install -e . && python -m screenpen
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PyQt5
matplotlib==3.2
numpy
matplotlib>=3.2
numpy>=1.8
63 changes: 57 additions & 6 deletions screenpen/screenpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtCore import QPoint, Qt, QSize
from PyQt5.QtWidgets import QToolBar, QAction, QDialog, QToolButton, QMenu, QColorDialog
from PyQt5.QtGui import (
QIcon, QScreen, QPalette, QColor, QCursor,
Expand Down Expand Up @@ -120,13 +120,19 @@ def __init__(self, screen, screen_geom, pixmap: QtGui.QPixmap = None): # app: QA
self.sc_redo = QShortcut(QKeySequence('Ctrl+Y'), self)
self.sc_redo.activated.connect(self.redo)

def _setCursor(self, cursor):
def _setCursor(self, cursor, hotx = None, hoty = None):
if hotx is None:
hotx = 2
if hoty is None:
hoty = 2
if type(cursor) == str:
pixm = QtGui.QPixmap.fromImage(QtGui.QImage.fromData(bytes(self._applySvgConfig(self._icons[cursor], None), encoding='utf-8')))
pixm = pixm.scaled(QSize(32, 32))
self.setCursor(QCursor(pixm, 2, 2))
self.setCursor(QCursor(pixm, hotx, hoty))
elif type(cursor) == Qt.CursorShape:
self.setCursor(QCursor(cursor))
elif type(cursor) == QPixmap:
self.setCursor(QCursor(cursor, hotx, hoty))

def keyPressEvent(self, event):
if event.isAutoRepeat():
Expand Down Expand Up @@ -238,6 +244,36 @@ def _setColor():
self._setupTools()
return _setColor

def _getEraserPen(self, color=Qt.transparent, size=30):
pen = QtGui.QPen()
pen.setBrush(QtGui.QBrush(color))
pen.setStyle(Qt.SolidLine) ; pen.setCapStyle(Qt.RoundCap)
pen.setJoinStyle(Qt.RoundJoin) ; pen.setWidth(size)
return pen

def setEraser(self):
def _setEraser():
pix = QPixmap()
img = QtGui.QImage(QSize(32, 32), QtGui.QImage.Format_ARGB32)
img.fill(Qt.transparent)
qp = QtGui.QPainter(img)
qp.setPen(self._getEraserPen(QColor('#7acfe6'), 30))
path = QtGui.QPainterPath()
path.moveTo(QPoint(16, 16))
path.cubicTo(QPoint(16, 17), QPoint(16, 16), QPoint(16, 16))
qp.drawPath(path)
qp.setPen(self._getEraserPen(QColor('#eccdec'), 26))
path = QtGui.QPainterPath()
path.moveTo(QPoint(16, 16))
path.cubicTo(QPoint(16, 17), QPoint(16, 16), QPoint(16, 16))
qp.drawPath(path)
qp.end()
pix = pix.fromImage(img)
self.setAction('drawEraser')()
self._setCursor(pix, 16, 16)
self._setupTools()
return _setEraser

def setStyle(self, style):
def _setStyle():
self.curr_style = style
Expand All @@ -250,9 +286,11 @@ def _setWidth():
self._setupTools()
return _setWidth

def setAction(self, action):
def setAction(self, action, cursor=None):
def _setAction():
self.curr_method = action
if cursor is None:
self._setCursor(Qt.ArrowCursor)
return _setAction

class ChartDialog(QDialog):
Expand Down Expand Up @@ -316,6 +354,7 @@ def code_clicked(c):

def showChart(self):
def _showChart():
self._setCursor(Qt.ArrowCursor)
dlg = self.ChartDialog(self)
if dlg.exec_():
self.curr_method = 'drawChart'
Expand Down Expand Up @@ -393,7 +432,7 @@ def _createToolBars(self):
)

penToolBar.addAction(self.addAction(f'Color Picker', self._getIcon('color_picker'), self.colorPicker()))
penToolBar.addAction(self.addAction(f'Eraser', self._getIcon('eraser'), self.setColor(Qt.transparent)))
penToolBar.addAction(self.addAction(f'Eraser', self._getIcon('eraser'), self.setEraser()))

actionBar.addAction(self.addAction("Path", self._getIcon('path'), self.setAction('drawPath')))
actionBar.addAction(self.addAction("Rect", self._getIcon('rect'), self.setAction('drawRect')))
Expand Down Expand Up @@ -500,6 +539,17 @@ def paintEvent(self, event):
self.update()
qp.setBrush(self.curr_br)

elif self.curr_method in ['drawEraser']:
if self.lastPoint != self.end:
qp.setBrush(Qt.NoBrush)
qp.setPen(self._getEraserPen(Qt.transparent))
self.path.cubicTo(self.end, self.end, self.end)
self.curr_args = [self.path]
getattr(qp, 'drawPath')(*self.curr_args)
self.lastPoint = self.end
self.update()
qp.setBrush(self.curr_br)

qp.setCompositionMode (QtGui.QPainter.CompositionMode_SourceOver)
qp.end()

Expand All @@ -523,7 +573,7 @@ def mousePressEvent(self, event):
self.begin = self.scaleCoords(event.pos())
self.end = self.scaleCoords(event.pos())

elif self.curr_method in ['drawPath']:
elif self.curr_method in ['drawPath', 'drawEraser']:
self.path = QtGui.QPainterPath()
self.begin = self.scaleCoords(event.pos())
self.end = self.scaleCoords(event.pos())
Expand Down Expand Up @@ -599,6 +649,7 @@ def mouseReleaseEvent(self, event):
p.convertFromImage(self.imageDraw)
self.history.append(p)


def setupBoard(self, color):
def _setupBoard():
self.background.fill(color)
Expand Down
2 changes: 1 addition & 1 deletion screenpen/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.7"
__version__ = "0.1.8"

0 comments on commit 6810494

Please sign in to comment.