File size: 18,999 Bytes
985c397 | 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2017 sliptonic <shopinthewoods@gmail.com> *
# * *
# * 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. *
# * *
# * This program 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
import FreeCADGui
import Path
import Path.Base.Util as PathUtil
from PySide import QtGui, QtCore
from PySide import QtCore, QtGui
__title__ = "CAM UI helper and utility functions"
__author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecad.org"
__doc__ = "A collection of helper and utility functions for the CAM GUI."
if False:
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
Path.Log.trackModule(Path.Log.thisModule())
else:
Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
def populateCombobox(form, enumTups, comboBoxesPropertyMap):
"""populateCombobox(form, enumTups, comboBoxesPropertyMap) ... populate comboboxes with translated enumerations
** comboBoxesPropertyMap will be unnecessary if UI files use strict combobox naming protocol.
Args:
form = UI form
enumTups = list of (translated_text, data_string) tuples
comboBoxesPropertyMap = list of (translated_text, data_string) tuples
"""
Path.Log.track(enumTups)
# Load appropriate enumerations in each combobox
for cb, prop in comboBoxesPropertyMap:
box = getattr(form, cb) # Get the combobox
box.clear() # clear the combobox
for text, data in enumTups[prop]: # load enumerations
box.addItem(text, data)
def updateInputField(obj, prop, widget, onBeforeChange=None):
"""updateInputField(obj, prop, widget) ... update obj's property prop with the value of widget.
The property's value is only assigned if the new value differs from the current value.
This prevents onChanged notifications where the value didn't actually change.
Gui::InputField and Gui::QuantitySpinBox widgets are supported - and the property can
be of type Quantity or Float.
If onBeforeChange is specified it is called before a new value is assigned to the property.
Returns True if a new value was assigned, False otherwise (new value is the same as the current).
"""
value = widget.property("rawValue")
Path.Log.track("value: {}".format(value))
attr = PathUtil.getProperty(obj, prop)
attrValue = attr.Value if hasattr(attr, "Value") else attr
isDiff = False
if not Path.Geom.isRoughly(attrValue, value):
isDiff = True
else:
if hasattr(obj, "ExpressionEngine"):
exprSet = False
for prp, expr in obj.ExpressionEngine:
if prp == prop:
exprSet = True
Path.Log.debug('prop = "expression": {} = "{}"'.format(prp, expr))
value = FreeCAD.Units.Quantity(obj.evalExpression(expr)).Value
if not Path.Geom.isRoughly(attrValue, value):
isDiff = True
break
if exprSet:
widget.setReadOnly(True)
widget.setProperty("exprSet", "true")
widget.style().unpolish(widget)
widget.ensurePolished()
else:
widget.setReadOnly(False)
widget.setProperty("exprSet", "false")
widget.style().unpolish(widget)
widget.ensurePolished()
widget.update()
if isDiff:
Path.Log.debug("updateInputField(%s, %s): %.2f -> %.2f" % (obj.Label, prop, attr, value))
if onBeforeChange:
onBeforeChange(obj)
PathUtil.setProperty(obj, prop, value)
return True
return False
class QuantitySpinBox(QtCore.QObject):
"""Controller class to interface a Gui::QuantitySpinBox.
The spin box gets bound to a given property and supports update in both directions.
QuatitySpinBox(widget, obj, prop, onBeforeChange=None)
widget ... expected to be reference to a Gui::QuantitySpinBox
obj ... document object
prop ... canonical name of the (sub-) property
onBeforeChange ... an optional callback being executed before the value of the property is changed
"""
def __init__(self, widget, obj, prop, onBeforeChange=None):
super().__init__()
Path.Log.track(widget)
self.widget = widget
self.onBeforeChange = onBeforeChange
self.prop = None
self.obj = obj
self.lastWidgetText = self.widget.text()
self.attachTo(obj, prop)
self.widget.installEventFilter(self)
# Connect local class method as slot
self.widget.textChanged.connect(self.onWidgetValueChanged)
# Connect to showFormulaDialog signal to track dialog open/close
try:
self.widget.showFormulaDialog.connect(self.onFormulaDialogStateChanged)
except AttributeError:
# Widget may not have this signal
pass
def onFormulaDialogStateChanged(self, isOpen):
"""
Slot called when the formula dialog is opened or closed.
When the dialog closes (isOpen=False), refresh the widget.
"""
if not isOpen:
# Dialog has closed, update the widget to reflect any changes
self.updateWidget()
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Type.FocusIn:
self.updateWidget()
return False
def onWidgetValueChanged(self):
"""
Slot method for determining if a change
in widget value is a result of an expression edit, or a simple spinbox change.
If the former, emit a manual `editingFinished` signal because the Expression editor
window returned a value to the base widget, leaving it in read-only mode,
and finishing the editing of the value. Otherwise, due nothing if the value
has not changed, or there is no active expression for the property.
If the user closes the Expression editor to cancel the edit, the value will not
be changed, and this manual signal will not be emitted."""
if self._hasExpression() and self.widget.text() != self.lastWidgetText:
self.widget.editingFinished.emit()
def attachTo(self, obj, prop=None):
"""use an existing editor for the given object and property"""
Path.Log.track(self.prop, prop)
self.obj = obj
self.prop = prop
if obj and prop:
attr = PathUtil.getProperty(obj, prop)
if attr is not None:
if hasattr(attr, "Value"):
self.widget.setProperty("unit", attr.getUserPreferred()[2])
self.widget.setProperty("binding", "%s.%s" % (obj.Name, prop))
self.valid = True
else:
Path.Log.warning("Cannot find property {} of {}".format(prop, obj.Label))
self.valid = False
else:
self.valid = False
def expression(self):
"""returns the expression if one is bound to the property"""
Path.Log.track(self.prop, self.valid)
if self.valid:
return self.widget.property("expression")
return ""
def setMinimum(self, quantity):
"""set the minimum"""
Path.Log.track(self.prop, self.valid)
if self.valid:
value = quantity.Value if hasattr(quantity, "Value") else quantity
self.widget.setProperty("setMinimum", value)
def updateWidget(self, quantity=None):
"""
update the display value of the spin box.
If no value is provided the value of the bound property is used.
quantity can be of type Quantity or Float."""
Path.Log.track(self.prop, self.valid, quantity)
if self.valid:
expr = self._hasExpression()
if quantity is None:
if expr:
quantity = FreeCAD.Units.Quantity(self.obj.evalExpression(expr))
else:
quantity = PathUtil.getProperty(self.obj, self.prop)
value = quantity.Value if hasattr(quantity, "Value") else quantity
self.widget.setProperty("rawValue", value)
self.lastWidgetText = self.widget.text() # update last widget value
if expr:
self.widget.setReadOnly(True)
self.widget.setProperty("exprSet", "true")
self.widget.style().unpolish(self.widget)
self.widget.ensurePolished()
else:
self.widget.setReadOnly(False)
self.widget.setProperty("exprSet", "false")
self.widget.style().unpolish(self.widget)
self.widget.ensurePolished()
def updateProperty(self):
"""updateProperty() ... update the bound property with the value from the spin box"""
Path.Log.track(self.prop, self.valid)
if self.valid:
return updateInputField(self.obj, self.prop, self.widget, self.onBeforeChange)
return None
def _hasExpression(self):
for prop, exp in self.obj.ExpressionEngine:
if prop == self.prop:
return exp
return None
class PropertyComboBox(QtCore.QObject):
"""Base controller class for properties represented as QComboBox."""
def __init__(self, widget, obj, prop, onBeforeChange=None):
super().__init__()
Path.Log.track(widget)
self.widget = widget
self.onBeforeChange = onBeforeChange
self.prop = None
self.obj = obj
self.valid = False
self.attachTo(obj, prop)
self.widget.currentIndexChanged.connect(self.updateProperty)
def attachTo(self, obj, prop=None):
"""use an existing editor for the given object and property"""
Path.Log.track(self.prop, prop)
self.obj = obj
self.prop = prop
if obj and prop:
attr = PathUtil.getProperty(obj, prop)
if attr is not None:
self.valid = True
self._populateComboBox()
self.updateWidget()
else:
Path.Log.warning("Cannot find property {} of {}".format(prop, obj.Label))
self.valid = False
else:
self.valid = False
def _populateComboBox(self):
"""To be implemented by subclasses"""
raise NotImplementedError
def updateWidget(self, value=None):
"""update the display value of the combo box."""
Path.Log.track(self.prop, self.valid, value)
if self.valid:
if value is None:
value = PathUtil.getProperty(self.obj, self.prop)
index = (
self.widget.findData(value)
if hasattr(self.widget, "findData")
else self.widget.findText(str(value))
)
if index >= 0:
self.widget.setCurrentIndex(index)
def updateProperty(self):
"""update the bound property with the value from the combo box"""
Path.Log.track(self.prop, self.valid)
if self.valid and self.prop:
if self.onBeforeChange:
self.onBeforeChange()
current_value = PathUtil.getProperty(self.obj, self.prop)
new_value = (
self.widget.currentData()
if hasattr(self.widget, "currentData")
else self.widget.currentText()
)
if str(new_value) != str(current_value):
setattr(self.obj, self.prop, new_value)
return True
return False
class IntegerSpinBox(QtCore.QObject):
"""Controller class for integer properties represented as QSpinBox.
IntegerSpinBox(widget, obj, prop, onBeforeChange=None)
widget ... expected to be reference to a QSpinBox
obj ... document object
prop ... canonical name of the (sub-) property
onBeforeChange ... optional callback before property change
"""
def __init__(self, widget, obj, prop, onBeforeChange=None):
super().__init__()
self.widget = widget
self.onBeforeChange = onBeforeChange
self.prop = None
self.obj = obj
self.valid = False
# Configure spin box defaults
self.widget.setMinimum(-2147483647) # Qt's minimum for spin boxes
self.widget.setMaximum(2147483647) # Qt's maximum for spin boxes
self.attachTo(obj, prop)
self.widget.valueChanged.connect(self.updateProperty)
def attachTo(self, obj, prop=None):
"""bind to the given object and property"""
self.obj = obj
self.prop = prop
if obj and prop:
try:
prop_value = PathUtil.getProperty(obj, prop)
if prop_value is not None:
self.valid = True
self.updateWidget()
else:
Path.Log.warning(f"Cannot get value for property {prop} of {obj.Label}")
self.valid = False
except Exception as e:
Path.Log.error(f"Error attaching to property {prop}: {str(e)}")
self.valid = False
else:
self.valid = False
def updateWidget(self, value=None):
"""update the spin box value"""
if self.valid:
try:
if value is None:
value = PathUtil.getProperty(self.obj, self.prop)
# Handle both direct values and Quantity objects
if hasattr(value, "Value"): # For Quantity properties
value = int(value.Value)
self.widget.setValue(int(value))
except Exception as e:
Path.Log.error(f"Error updating spin box: {str(e)}")
def updateProperty(self):
"""update the bound property with the spin box value"""
if self.valid and self.prop:
if self.onBeforeChange:
self.onBeforeChange()
new_value = self.widget.value()
current_value = PathUtil.getProperty(self.obj, self.prop)
# Handle Quantity properties
if hasattr(current_value, "Value"):
if new_value != current_value.Value:
current_value.Value = new_value
return True
elif new_value != current_value:
setattr(self.obj, self.prop, new_value)
return True
return False
def setRange(self, min_val, max_val):
"""set minimum and maximum values"""
self.widget.setMinimum(min_val)
self.widget.setMaximum(max_val)
def setSingleStep(self, step):
"""setSingleStep(step) ... set the step size"""
self.widget.setSingleStep(step)
class BooleanComboBox(PropertyComboBox):
"""Controller class for boolean properties represented as QComboBox."""
def _populateComboBox(self):
self.widget.clear()
self.widget.addItem("True", True)
self.widget.addItem("False", False)
class EnumerationComboBox(PropertyComboBox):
"""Controller class for enumeration properties represented as QComboBox."""
def _populateComboBox(self):
self.widget.clear()
enums = self.obj.getEnumerationsOfProperty(self.prop)
for item in enums:
self.widget.addItem(item, item)
class PropertyLabel(QtCore.QObject):
"""Controller class for read-only property display as QLabel."""
def __init__(self, widget, obj, prop, onBeforeChange=None):
super().__init__()
self.widget = widget
self.obj = obj
self.prop = prop
self.valid = False
self.attachTo(obj, prop)
def attachTo(self, obj, prop=None):
"""bind to the given object and property"""
self.obj = obj
self.prop = prop
if obj and prop:
attr = PathUtil.getProperty(obj, prop)
if attr is not None:
self.valid = True
self.updateWidget()
else:
Path.Log.warning(f"Cannot find property {prop} of {obj.Label}")
self.valid = False
else:
self.valid = False
def updateWidget(self, value=None):
"""update the label text"""
if self.valid:
if value is None:
value = PathUtil.getProperty(self.obj, self.prop)
self.widget.setText(str(value))
def getDocNode():
doc = FreeCADGui.ActiveDocument.Document.Name
tws = FreeCADGui.getMainWindow().findChildren(QtGui.QTreeWidget)
for tw in tws:
if tw.topLevelItemCount() != 1 or tw.topLevelItem(0).text(0) != "Application":
continue
toptree = tw.topLevelItem(0)
for i in range(0, toptree.childCount()):
docitem = toptree.child(i)
if docitem.text(0) == doc:
return docitem
return None
def disableItem(item):
Dragflag = QtCore.Qt.ItemFlag.ItemIsDragEnabled
Dropflag = QtCore.Qt.ItemFlag.ItemIsDropEnabled
item.setFlags(item.flags() & ~Dragflag)
item.setFlags(item.flags() & ~Dropflag)
for idx in range(0, item.childCount()):
disableItem(item.child(idx))
def findItem(docitem, objname):
print(docitem.text(0))
for i in range(0, docitem.childCount()):
if docitem.child(i).text(0) == objname:
return docitem.child(i)
res = findItem(docitem.child(i), objname)
if res:
return res
return None
|