-
Notifications
You must be signed in to change notification settings - Fork 8
/
freehandApp.py
executable file
·131 lines (94 loc) · 3.73 KB
/
freehandApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
Qt app demonstrating freehand drawing tool.
Copyright 2012 Lloyd Konneker
This is free software, covered by the GNU General Public License.
'''
try:
# Set PyQt API version to 2
import sip
API_NAMES = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
API_VERSION = 2
for name in API_NAMES:
sip.setapi(name, API_VERSION)
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
import sys
from freehandTool.pointerEvent import PointerEvent
from freehandTool.freehand import FreehandTool
# from freehandTool.ghostLine import PointerTrackGhost
from freehandTool.freehandHead import PointerTrackGhost
from freehandTool.segmentString.segmentString import SegmentString
class DiagramScene(QGraphicsScene):
def __init__(self, *args):
QGraphicsScene.__init__(self, *args)
self.addItem(QGraphicsTextItem("Freehand drawing with pointer"))
class GraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(GraphicsView, self).__init__(parent)
assert self.dragMode() == QGraphicsView.NoDrag
self.setRenderHint(QPainter.Antialiasing)
self.setRenderHint(QPainter.TextAntialiasing)
self.freehandTool = FreehandTool(view=self)
self.setMouseTracking(True); # Enable mouseMoveEvent
''' Delegate events to FreehandTool. '''
def mouseMoveEvent(self, event):
''' Tell freehandTool to update its SegmentString. '''
pointerEvent = PointerEvent()
pointerEvent.makeFromEvent(mapper=self, event=event)
self.freehandTool.pointerMoveEvent(pointerEvent)
def mousePressEvent(self, event):
'''
On mouse button down, create a new (infinitesmal) SegmentString and PointerTrackGhost.
freehandTool remembers and updates SegmentString.
'''
pointerEvent = PointerEvent()
pointerEvent.makeFromEvent(mapper=self, event=event)
'''
freehandCurve as QGraphicsItem positioned at event in scene.
It keeps its internal data in its local CS
'''
freehandCurve = SegmentString()
self.scene().addItem(freehandCurve)
freehandCurve.setPos(pointerEvent.scenePos)
'''
headGhost at (0,0) in scene
it keeps its local data in CS equivalent to scene
'''
headGhost = PointerTrackGhost()
self.scene().addItem(headGhost)
self.freehandTool.setSegmentString(segmentString=freehandCurve,
pathHeadGhost=headGhost,
scenePosition=pointerEvent.scenePos)
self.freehandTool.pointerPressEvent(pointerEvent)
def mouseReleaseEvent(self, event):
pointerEvent = PointerEvent()
pointerEvent.makeFromEvent(mapper=self, event=event)
self.freehandTool.pointerReleaseEvent(pointerEvent)
def keyPressEvent(self, event):
if event.modifiers() & Qt.ControlModifier:
alternateMode = True
else:
alternateMode = False
self.freehandTool.testControlPoint(event, alternateMode)
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.scene = DiagramScene()
self.view = GraphicsView(self.scene)
rect = QRectF(-500, -500, 500, 500)
self.view.fitInView(rect)
self.view.setSceneRect(rect)
self.setCentralWidget(self.view)
def main(args):
app = QApplication(args)
app.setStyle(QStyleFactory.create("Fusion")) # fixes gtk assertion errors
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 500, 500)
mainWindow.show()
sys.exit(app.exec_()) # Qt Main loop
if __name__ == "__main__":
main(sys.argv)