Commit fb9cec68 authored by Jorn Bruggeman's avatar Jorn Bruggeman
Browse files

python: moved treeview class to pyfabm, hide models that are automatically created

parent 572a0490
......@@ -35,7 +35,7 @@ fabm.get_variable_metadata_ptr.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c
fabm.get_variable_long_path.argtypes = [ctypes.c_void_p,ctypes.c_int,ctypes.c_char_p]
fabm.get_parameter_metadata.argtypes = [ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int)]
fabm.get_dependency_metadata.argtypes = [ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p]
fabm.get_model_metadata.argtypes = [ctypes.c_char_p,ctypes.c_int,ctypes.c_char_p]
fabm.get_model_metadata.argtypes = [ctypes.c_char_p,ctypes.c_int,ctypes.c_char_p,ctypes.POINTER(ctypes.c_int)]
fabm.get_coupling.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_void_p),ctypes.POINTER(ctypes.c_void_p)]
fabm.get_real_parameter.argtypes = [ctypes.c_int,ctypes.c_int]
fabm.get_real_parameter.restype = ctypes.c_double
......@@ -265,15 +265,21 @@ class Coupling(Variable):
value = property(getValue, setValue)
class SubModel(object):
def __init__(self,name):
strlong_name = ctypes.create_string_buffer(ATTRIBUTE_LENGTH)
iuser = ctypes.c_int()
fabm.get_model_metadata(name,ATTRIBUTE_LENGTH,strlong_name,iuser)
self.long_name = strlong_name.value
self.user_created = iuser.value!=0
class Model(object):
def __init__(self,path='fabm.yaml'):
fabm.initialize(path)
self.updateConfiguration()
def getModelLongName(self,name):
strname = ctypes.create_string_buffer(ATTRIBUTE_LENGTH)
fabm.get_model_metadata(name,ATTRIBUTE_LENGTH,strname)
return strname.value
def getSubModel(self,name):
return SubModel(name)
def saveSettings(self):
environment = dict([(dependency.name,dependency.value) for dependency in self.dependencies])
......
......@@ -99,13 +99,20 @@ class ItemModel(QtCore.QAbstractItemModel):
self.value = None
self.long_name = long_name
def processNode(n,path=()):
childprefix = path
if isinstance(n.object,basestring) and path:
if path[-1] in ('parameters','initialization','environment','coupling'):
childprefix = path[:-1]
else:
n.object = Submodel(self.model.getModelLongName('/'.join(path)))
for child in n.children: processNode(child,childprefix+(child.name,))
for i in range(len(n.children)-1,-1,-1):
child = n.children[i]
childpath = path + (child.name,)
if isinstance(child.object,basestring):
if childpath[-1] in ('parameters','initialization','environment','coupling'):
childpath = childpath[:-1]
else:
submodel = self.model.getSubModel('/'.join(childpath))
if submodel.user_created:
child.object = Submodel(submodel.long_name)
else:
n.removeChild(i)
child = None
if child is not None: processNode(child,childpath)
processNode(root)
if self.root is not None:
......@@ -227,3 +234,27 @@ class ItemModel(QtCore.QAbstractItemModel):
def headerData(self,section,orientation,role):
if orientation==QtCore.Qt.Horizontal and role==QtCore.Qt.DisplayRole and section>=0 and section<4:
return ('name','value','units','symbol')[section]
class TreeView(QtGui.QTreeView):
def __init__(self,model,parent):
QtGui.QTreeView.__init__(self,parent)
itemmodel = pyfabm.gui_qt.ItemModel(model,parent)
self.setItemDelegate(Delegate(parent))
self.setModel(itemmodel)
self.setUniformRowHeights(True)
self.expandAll()
def onTreeViewContextMenu(pos):
index = self.indexAt(pos)
if index.isValid() and index.column()==1:
data = index.internalPointer().object
if isinstance(data,pyfabm.Parameter) and data.value!=data.default and data.default is not None:
def reset():
data.reset()
itemmodel.rebuild()
contextMenu = QtGui.QMenu(self)
contextMenu.addAction('Reset to default (%s)' % data.default,reset)
contextMenu.exec_(self.mapToGlobal(pos))
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(onTreeViewContextMenu)
for i in range(3): self.resizeColumnToContents(i)
......@@ -123,8 +123,9 @@
! Re-create original models
node => model%root%children%first
do while (associated(node))
if (node%model%type_name/='') then
if (node%model%user_created) then
call factory%create(node%model%type_name,childmodel)
childmodel%user_created = .true.
call newmodel%root%add_child(childmodel,node%model%name,node%model%long_name,configunit=-1)
end if
node => node%next
......@@ -361,11 +362,12 @@
call copy_to_c_string(variable%long_name,long_name)
end subroutine get_variable_metadata_ptr
subroutine get_model_metadata(name,length,long_name) bind(c)
subroutine get_model_metadata(name,length,long_name,user_created) bind(c)
!DIR$ ATTRIBUTES DLLEXPORT :: get_model_metadata
character(kind=c_char),intent(in), target :: name(*)
integer(c_int), intent(in), value :: length
character(kind=c_char),intent(out) :: long_name(length)
integer(c_int), intent(out) :: user_created
character(len=attribute_length),pointer :: pname
class (type_base_model), pointer :: found_model
......@@ -374,6 +376,7 @@
found_model => model%root%find_model(pname(:index(pname,C_NULL_CHAR)-1))
if (.not.associated(found_model)) call driver%fatal_error('get_model_metadata','model "'//pname(:index(pname,C_NULL_CHAR)-1)//'" not found.')
call copy_to_c_string(found_model%long_name,long_name)
user_created = found_model%user_created
end subroutine
subroutine link_dependency_data(index,value) bind(c)
......
......@@ -32,27 +32,7 @@ app = QtGui.QApplication([' '])
dialog = QtGui.QDialog()
layout = QtGui.QHBoxLayout()
tree = QtGui.QTreeView()
itemmodel = pyfabm.gui_qt.ItemModel(model,dialog)
tree.setItemDelegate(pyfabm.gui_qt.Delegate(dialog))
tree.setModel(itemmodel)
tree.setUniformRowHeights(True)
tree.expandAll()
def onTreeViewContextMenu(pos):
index = tree.indexAt(pos)
if index.isValid() and index.column()==1:
data = index.internalPointer().object
if isinstance(data,pyfabm.Parameter) and data.value!=data.default:
def reset():
data.reset()
itemmodel.rebuild()
contextMenu = QtGui.QMenu(tree)
contextMenu.addAction('Reset to default (%s)' % data.default,reset)
contextMenu.exec_(tree.mapToGlobal(pos))
tree.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
tree.customContextMenuRequested.connect(onTreeViewContextMenu)
for i in range(3): tree.resizeColumnToContents(i)
tree = pyfabm.gui_qt.TreeView(model,dialog)
layout.addWidget(tree)
dialog.setLayout(layout)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment