Property editor for enums.
This commit is contained in:
@@ -22,8 +22,6 @@
|
||||
# * *
|
||||
# ***************************************************************************
|
||||
|
||||
import Draft
|
||||
import DraftVecUtils
|
||||
import FreeCAD
|
||||
import FreeCADGui
|
||||
import PathScripts.PathGui as PathGui
|
||||
@@ -31,10 +29,16 @@ import PathScripts.PathIconViewProvider as PathIconViewProvider
|
||||
import PathScripts.PathLog as PathLog
|
||||
import PathScripts.PathSetupSheet as PathSetupSheet
|
||||
import PathScripts.PathSetupSheetOpPrototype as PathSetupSheetOpPrototype
|
||||
import PathScripts.PathSetupSheetOpPrototypeGui as PathSetupSheetOpPrototypeGui
|
||||
import PathScripts.PathUtil as PathUtil
|
||||
|
||||
from PySide import QtCore, QtGui
|
||||
|
||||
__title__ = "Setup Sheet Editor"
|
||||
__author__ = "sliptonic (Brad Collette)"
|
||||
__url__ = "http://www.freecadweb.org"
|
||||
__doc__ = "Task panel editor for a SetupSheet"
|
||||
|
||||
# Qt tanslation handling
|
||||
def translate(context, text, disambig=None):
|
||||
return QtCore.QCoreApplication.translate(context, text, disambig)
|
||||
@@ -94,43 +98,40 @@ class ViewProvider:
|
||||
def doubleClicked(self, vobj):
|
||||
self.setEdit(vobj)
|
||||
|
||||
class PropertyEditorDelegate(QtGui.QStyledItemDelegate):
|
||||
class Delegate(QtGui.QStyledItemDelegate):
|
||||
PropertyRole = QtCore.Qt.UserRole + 1
|
||||
EditorRole = QtCore.Qt.UserRole + 2
|
||||
|
||||
def paint(self, painter, option, index):
|
||||
#PathLog.track(index.column(), type(option))
|
||||
if False and 2 == index.column():
|
||||
PathLog.track(index.row(), index.data().toString())
|
||||
painter.drawText(option.rect, index.data().toString())
|
||||
else:
|
||||
QtGui.QStyledItemDelegate.paint(self, painter, option, index)
|
||||
|
||||
#def paint(self, painter, option, index):
|
||||
# #PathLog.track(index.column(), type(option))
|
||||
# if False and 2 == index.column():
|
||||
# PathLog.track(index.row(), index.data().toString())
|
||||
# option.text = index.data().toString()
|
||||
# painter.drawText(option.rect, index.data().toString(), option)
|
||||
# else:
|
||||
# if 2 == index.column():
|
||||
# PathLog.track(index.row(), type(index.data(self.PropertyRole)))
|
||||
# QtGui.QStyledItemDelegate.paint(self, painter, option, index)
|
||||
|
||||
def createEditor(self, parent, option, index):
|
||||
PathLog.track(index.row(), index.column())
|
||||
if 0 == index.column():
|
||||
return QtGui.QStyledItemDelegate.createEditor(self, parent, option, index)
|
||||
return None
|
||||
if index.data(self.EditorRole) is None:
|
||||
editor = PathSetupSheetOpPrototypeGui.Editor(index.data(self.PropertyRole))
|
||||
index.model().setData(index, editor, self.EditorRole)
|
||||
return index.data(self.EditorRole).widget(parent)
|
||||
|
||||
def setEditorData(self, widget, index):
|
||||
PathLog.track(index.row(), index.column())
|
||||
if 0 == index.column():
|
||||
QtGui.QStyledItemDelegate.setEditorData(self, widget, index)
|
||||
isset = widget.isChecked()
|
||||
index.model().item(index.row(), 1).setEnabled(isset)
|
||||
index.model().item(index.row(), 2).setEnabled(isset)
|
||||
print("setEditorData(%s)" % widget)
|
||||
index.data(self.EditorRole).setEditorData(widget)
|
||||
|
||||
def setModelData(self, widget, model, index):
|
||||
PathLog.track(index.row(), index.column())
|
||||
if 0 == index.column():
|
||||
return QtGui.QStyledItemDelegate.setModelData(self, widget, model, index)
|
||||
print("setModelData(%s)" % widget)
|
||||
editor = index.data(self.EditorRole)
|
||||
editor.setModelData(widget)
|
||||
index.model().setData(index, editor.prop.toString(), QtCore.Qt.DisplayRole)
|
||||
|
||||
def updateEditorGeometry(self, widget, option, index):
|
||||
#print("is this even called")
|
||||
if widget:
|
||||
widget.setGeometry(option.rect)
|
||||
else:
|
||||
print("so sad")
|
||||
widget.setGeometry(option.rect)
|
||||
|
||||
class OpTaskPanel:
|
||||
|
||||
@@ -155,7 +156,7 @@ class OpTaskPanel:
|
||||
def setupUi(self):
|
||||
PathLog.track()
|
||||
|
||||
self.delegate = PropertyEditorDelegate(self.form)
|
||||
self.delegate = Delegate(self.form)
|
||||
self.model = QtGui.QStandardItemModel(len(self.props), 3, self.form)
|
||||
self.model.setHorizontalHeaderLabels(['Set', 'Property', 'Value'])
|
||||
|
||||
@@ -167,7 +168,7 @@ class OpTaskPanel:
|
||||
|
||||
self.model.setData(self.model.index(i, 0), isset, QtCore.Qt.EditRole)
|
||||
self.model.setData(self.model.index(i, 1), name, QtCore.Qt.EditRole)
|
||||
self.model.setData(self.model.index(i, 2), prop, QtCore.Qt.EditRole)
|
||||
self.model.setData(self.model.index(i, 2), prop, Delegate.PropertyRole)
|
||||
self.model.setData(self.model.index(i, 2), prop.toString(), QtCore.Qt.DisplayRole)
|
||||
|
||||
self.model.item(i, 0).setCheckable(True)
|
||||
@@ -178,7 +179,7 @@ class OpTaskPanel:
|
||||
self.model.item(i, 2).setEnabled(False)
|
||||
|
||||
self.form.table.setModel(self.model)
|
||||
self.form.table.setItemDelegate(self.delegate)
|
||||
self.form.table.setItemDelegateForColumn(2, self.delegate)
|
||||
self.form.table.resizeColumnsToContents()
|
||||
|
||||
self.model.dataChanged.connect(self.updateData)
|
||||
|
||||
@@ -32,6 +32,12 @@ __url__ = "http://www.freecadweb.org"
|
||||
__doc__ = "Prototype objects to allow extraction of setup sheet values and editing."
|
||||
|
||||
|
||||
if False:
|
||||
PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
|
||||
PathLog.trackModule(PathLog.thisModule())
|
||||
else:
|
||||
PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
|
||||
|
||||
class Property(object):
|
||||
'''Base class for all prototype properties'''
|
||||
def __init__(self, name, category, info):
|
||||
@@ -39,7 +45,7 @@ class Property(object):
|
||||
self.category = category
|
||||
self.info = info
|
||||
self.editorMode = 0
|
||||
self.value = ''
|
||||
self.value = None
|
||||
|
||||
def setValue(self, value):
|
||||
self.value = value
|
||||
@@ -50,7 +56,7 @@ class Property(object):
|
||||
self.editorMode = mode
|
||||
|
||||
def toString(self):
|
||||
if self.value:
|
||||
if not self.value is None:
|
||||
return str(self.value)
|
||||
t = self.typeString()
|
||||
p = 'an' if t[0] in ['A', 'E', 'I', 'O', 'U'] else 'a'
|
||||
@@ -95,14 +101,14 @@ class PropertyString(Property):
|
||||
class OpPrototype(object):
|
||||
|
||||
PropertyType = {
|
||||
'App::PropertyEnumeration': PropertyEnumeration,
|
||||
'App::PropertyDistance': PropertyDistance,
|
||||
'App::PropertyPercent': PropertyPercent,
|
||||
'App::PropertyFloat': PropertyFloat,
|
||||
'App::PropertyBool': PropertyBool,
|
||||
'App::PropertyString': PropertyString,
|
||||
'App::PropertyLinkSubListGlobal': Property,
|
||||
'App::PropertyDistance': PropertyDistance,
|
||||
'App::PropertyEnumeration': PropertyEnumeration,
|
||||
'App::PropertyFloat': PropertyFloat,
|
||||
'App::PropertyLink': Property,
|
||||
'App::PropertyLinkSubListGlobal': Property,
|
||||
'App::PropertyPercent': PropertyPercent,
|
||||
'App::PropertyString': PropertyString,
|
||||
'App::PropertyVectorDistance': Property,
|
||||
'Part::PropertyPartShape': Property,
|
||||
}
|
||||
@@ -111,6 +117,11 @@ class OpPrototype(object):
|
||||
self.properties = {}
|
||||
self.DoNotSetDefaultValues = True
|
||||
|
||||
def __setattr__(self, name, val):
|
||||
if name in ['properties', 'DoNotSetDefaultValues']:
|
||||
return super(self.__class__, self).__setattr__(name, val)
|
||||
self.properties[name].setValue(val)
|
||||
|
||||
def addProperty(self, typeString, name, category, info = None):
|
||||
prop = self.PropertyType[typeString](name, category, info)
|
||||
self.properties[name] = prop
|
||||
|
||||
92
src/Mod/Path/PathScripts/PathSetupSheetOpPrototypeGui.py
Normal file
92
src/Mod/Path/PathScripts/PathSetupSheetOpPrototypeGui.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ***************************************************************************
|
||||
# * *
|
||||
# * Copyright (c) 2018 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 PathScripts.PathGui as PathGui
|
||||
import PathScripts.PathIconViewProvider as PathIconViewProvider
|
||||
import PathScripts.PathLog as PathLog
|
||||
import PathScripts.PathSetupSheet as PathSetupSheet
|
||||
import PathScripts.PathSetupSheetOpPrototype as PathSetupSheetOpPrototype
|
||||
import PathScripts.PathUtil as PathUtil
|
||||
|
||||
from PySide import QtCore, QtGui
|
||||
|
||||
__title__ = "Setup Sheet Editor"
|
||||
__author__ = "sliptonic (Brad Collette)"
|
||||
__url__ = "http://www.freecadweb.org"
|
||||
__doc__ = "Task panel editor for a SetupSheet"
|
||||
|
||||
# Qt tanslation handling
|
||||
def translate(context, text, disambig=None):
|
||||
return QtCore.QCoreApplication.translate(context, text, disambig)
|
||||
|
||||
if True:
|
||||
PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
|
||||
PathLog.trackModule(PathLog.thisModule())
|
||||
else:
|
||||
PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
|
||||
|
||||
class _PropertyEditor(object):
|
||||
def __init__(self, prop):
|
||||
self.prop = prop
|
||||
|
||||
class _PropertyEnumEditor(_PropertyEditor):
|
||||
|
||||
def widget(self, parent):
|
||||
PathLog.track(self.prop.name, self.prop.getEnumValues())
|
||||
return QtGui.QComboBox(parent);
|
||||
|
||||
def setEditorData(self, widget):
|
||||
widget.clear()
|
||||
widget.addItems(self.prop.getEnumValues())
|
||||
if self.prop.getValue():
|
||||
index = widget.findText(self.prop.getValue(), QtCore.Qt.MatchFixedString)
|
||||
if index >= 0:
|
||||
widget.setCurrentIndex(index)
|
||||
|
||||
def setModelData(self, widget):
|
||||
self.prop.setValue(widget.currentText())
|
||||
|
||||
|
||||
_EditorFactory = {
|
||||
PathSetupSheetOpPrototype.Property: None,
|
||||
PathSetupSheetOpPrototype.PropertyBool: None,
|
||||
PathSetupSheetOpPrototype.PropertyDistance: None,
|
||||
PathSetupSheetOpPrototype.PropertyEnumeration: _PropertyEnumEditor,
|
||||
PathSetupSheetOpPrototype.PropertyFloat: None,
|
||||
PathSetupSheetOpPrototype.PropertyPercent: None,
|
||||
PathSetupSheetOpPrototype.PropertyString: None,
|
||||
}
|
||||
|
||||
X = []
|
||||
|
||||
def Editor(prop):
|
||||
'''Returns an editor class to be used for that property.'''
|
||||
global X
|
||||
X.append(prop)
|
||||
factory = _EditorFactory[prop.__class__]
|
||||
if factory:
|
||||
return factory(prop)
|
||||
return None
|
||||
Reference in New Issue
Block a user