-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.py
281 lines (240 loc) · 10.5 KB
/
Image.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#***************************************************************************
#* Copyright (c) 2019 Gabriel Antao <[email protected]> *
#* *
#* This file is part of the FreeCAD CAx development system. *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* FreeCAD is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Lesser General Public License for more details. *
#* *
#* You should have received a copy of the GNU General Public License *
#* along with Dimensioning FreeCAD Workbench. *
#* If not, see <https://www.gnu.org/licenses/> *
#* *
#***************************************************************************/
"""
Insert svg image into drawing.
"""
from PySide import QtGui, QtCore, QtSvg
import FreeCAD, FreeCADGui
from Utils import getGraphicsView
class ImageTask:
"""Create and handle image task dialog"""
def __init__(self, graphics_view):
self.form = FreeCADGui.PySideUic.loadUi(":/ui/task_image.ui")
self.image = None
self.createFileDialog()
# Handle scene objects
self.graphics_view = graphics_view
self.scene = self.graphics_view.scene()
## DIALOG METHODS ##
def isAllowedAlterSelection(self):
return True
def isAllowedAlterView(self):
return True
def isAllowedAlterDocument(self):
return True
def reject(self):
self.scene.removeItem(self.image)
return True #close dialog
def accept(self):
if self.image:
self.image.setEditMode(False)
self.image.update()
document = self.graphics_view.getDocument()
image = document.addObject("App::FeaturePython", "Image")
Image(image)
ImageView(image.ViewObject, self.image)
page = self.graphics_view.getPage()
page.addObject(image)
FreeCAD.ActiveDocument.recompute()
FreeCAD.Console.PrintMessage("Svg image created.\n")
return True #close dialog
return False
## SLOTS ##
def fileDialogAccepted(self, filepath):
self.form.path.clear()
self.form.path.insert(filepath)
self.createImage(filepath)
## METHODS ##
def connectSlots(self):
"""Connect all slot functions to dialog widgets"""
self.form.scale.valueChanged.connect(self.image.setScale)
self.form.rotation.valueChanged.connect(self.image.setRotation)
self.form.opacity.valueChanged.connect(self.image.setOpacity)
def disconnectSlots(self):
"""Disconnect all slot functions to dialog widgets"""
self.form.scale.valueChanged.disconnect(self.image.setScale)
self.form.rotation.valueChanged.disconnect(self.image.setRotation)
self.form.opacity.valueChanged.disconnect(self.image.setOpacity)
# NOTE: initial path should use QStandardPaths::PicturesLocation
# Does it work on all systems (Unix, Windows, OSX) ?
# (using expanduser)
def createFileDialog(self):
import os
self.dialog = QtGui.QFileDialog(self.form,
"Open svg image",
os.path.expanduser("~/Pictures"),
"Scalable Vector Graphics (*.svg)")
self.form.search.clicked.connect(self.dialog.show)
self.dialog.fileSelected.connect(self.fileDialogAccepted)
def createImage(self, filepath):
"""Create and add a svg image."""
pos = QtCore.QPointF(0, 0)
if self.image:
pos = self.image.pos()
self.scene.removeItem(self.image)
self.disconnectSlots()
self.image = ImageItem(filepath)
self.connectSlots()
scale = self.form.scale.value()
rotation = self.form.rotation.value()
opacity = self.form.opacity.value()
self.image.setPos(pos)
self.image.setScale(scale)
self.image.setRotation(rotation)
self.image.setOpacity(opacity)
center = self.image.boundingRect().center()
self.image.setTransformOriginPoint(center)
self.scene.addItem(self.image)
class ImageItem(QtSvg.QGraphicsSvgItem):
def __init__(self, filepath):
super(ImageItem, self).__init__(filepath)
self.editModeOn = True
self.filepath = filepath
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
self.setCursor(QtCore.Qt.OpenHandCursor)
def opacity(self):
return int(super(ImageItem, self).opacity()*100)
def setOpacity(self, opacity):
"""Change opacity."""
super(ImageItem, self).setOpacity(opacity/100.0)
def setEditMode(self, value):
"""Edit mode, true when editing (or creating) the image."""
self.editModeOn = value
def paint(self, painter, option, widget=None):
rect = self.boundingRect()
# Draw rect
# FIXME: rect should not be affected by scale nor opacity
if self.editModeOn:
pen = QtGui.QPen()
pen.setStyle(QtCore.Qt.DotLine)
pen.setColor(QtCore.Qt.gray)
pen.setWidthF(2)
painter.setPen(pen)
painter.drawRect(0, 0, rect.width(), rect.height())
super(ImageItem, self).paint(painter, option, widget)
class Image:
"""Feature for a svg image in draw."""
def __init__(self, obj):
obj.addProperty("App::PropertyFile", "File", "General", "File path", 1)
obj.addProperty("App::PropertyFloat", "Scale", "General",
"Image scale")
obj.addProperty("App::PropertyAngle", "Rotation", "General",
"Image rotation")
obj.addProperty("App::PropertyPercent", "Opacity", "General",
"Image opacity")
obj.addProperty("App::PropertyFloat", "Zvalue", "General",
"Image Z-value")
obj.Proxy = self
def onChanged(self, fp, prop):
pass # Let ImageView handle this
def execute(self, obj):
pass
class ImageView:
"""View for a svg image in draw."""
def __init__(self, vobj, graphics_item):
self.image = graphics_item
vobj.Proxy = self
def attach(self, vp):
# NOTE: it must be done to show coulored icon in tree view
# https://forum.freecadweb.org/viewtopic.php?t=12139
from pivy import coin
vp.addDisplayMode(coin.SoGroup(), "Standard")
# Set feature properties
feature = vp.Object
feature.File = self.image.filepath
feature.Scale = self.image.scale()
feature.Rotation = self.image.rotation()
feature.Opacity = self.image.opacity()
feature.Zvalue = self.image.zValue()
def getGraphicsView(self, vp):
page = vp.Object.getParentGroup() #feature
page_view = page.ViewObject.Proxy
return page_view.graphics_view
# def setEdit(self, mode):
# #https://www.freecadweb.org/wiki/Std_Edit
# """Enter in task dialog to edit annotation."""
# FreeCADGui.Control.showDialog(AnnotationTask(getGraphicsView()))
# return True
def onDelete(self, vp, subname):
"""Delete the annotation itens."""
graphics_view = self.getGraphicsView(vp)
scene = graphics_view.scene()
scene.removeItem(self.image)
return True
def doubleClicked(self, vp):
"""Called when double click in object in treeview."""
page_view = self.getGraphicsView(vp)
page_view.graphics_view.setActive()
return True
def onChanged(self, vp, prop):
"""Called when ImageView property changes"""
if prop == "Visibility":
visibility = vp.getPropertyByName("Visibility")
self.image.setVisible(visibility)
def updateData(self, fp, prop):
"""Called when Image property changes"""
if prop == "Scale":
scale = fp.getPropertyByName("Scale")
if scale < 0:
scale = -scale
fp.Scale = scale
self.image.setScale(scale)
elif prop == "Rotation":
rotation = fp.getPropertyByName("Rotation")
self.image.setRotation(rotation)
elif prop == "Opacity":
opacity = fp.getPropertyByName("Opacity")
self.image.setOpacity(opacity)
elif prop == "Zvalue":
zvalue = fp.getPropertyByName("Zvalue")
if zvalue < 0:
zvalue = 0
fp.Zvalue = 0
self.image.setZValue(zvalue)
def getIcon(self):
return ":/icons/image.svg"
def getDisplayModes(self,obj):
"""Return a list of display modes."""
return ["Standard"]
def getDefaultDisplayMode(self):
"""Return the name of the default display mode.
It must be defined in getDisplayModes."""
return "Standard"
class ImageCommand:
"""Command for creating svg image."""
def IsActive(self):
if FreeCADGui.ActiveDocument == None:
return False
return not FreeCADGui.Control.activeDialog()
def Activated(self):
graphics_view = getGraphicsView()
if not graphics_view: # page is not active
return
FreeCADGui.Control.showDialog(ImageTask(graphics_view))
def GetResources(self):
return {"Pixmap" : ":/icons/image.svg",
"Accel" : "Shift+I",
"MenuText": "Image",
"ToolTip": "Insert a svg image."}
FreeCADGui.addCommand("Dimensioning_Image", ImageCommand())