# -*- python -*-
#
# OpenAlea.OALab: Multi-Paradigm GUI
#
# Copyright 2013 INRIA - CIRAD - INRA
#
# File author(s): Julien Coste <julien.coste@inria.fr>
#
# File contributor(s):
#
# Distributed under the Cecill-C License.
# See accompanying file LICENSE.txt or copy at
# http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html
#
# OpenAlea WebSite : http://openalea.gforge.inria.fr
#
###############################################################################
"""
Display a tree view of the project in oalab
"""
__revision__ = "$Id: "
from openalea.vpltk.qt import QtGui, QtCore
from openalea.core.path import path
from openalea.core import settings
from openalea.oalab.gui import resources_rc
import os
[docs]class ProjectLabel(QtGui.QLabel):
"""
Widget to display the name of the current project.
"""
def __init__(self, session, controller, parent=None):
super(ProjectLabel, self).__init__(parent=None)
self.session = session
self.update()
[docs] def update(self):
if self.session.current_is_project():
label = self.session.project.name
else:
label = ""
self.setText(label)
[docs]class ProjectTreeView(QtGui.QTreeView):
"""
Widget to display Tree View of project.
"""
def __init__(self, session, controller, parent=None):
super(ProjectTreeView, self).__init__(parent)
#self.setIconSize(QtCore.QSize(30,30))
self.session = session
self.controller = controller
if self.session.current_is_project():
self.project = self.session.project
else:
self.project = None
self.projectview = QtGui.QWidget()
# project tree view
self.proj_model = PrjctModel(session, controller, self.project)
self.setHeaderHidden(True)
self.setModel(self.proj_model)
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
QtCore.QObject.connect(self,QtCore.SIGNAL('customContextMenuRequested(const QPoint&)'),self.showMenu)
[docs] def update(self):
self.reinit_treeview()
[docs] def reinit_treeview(self):
""" Reinitialise project view """
if self.session.current_is_project():
self.project = self.session.project
else:
self.project = None
self.proj_model.set_proj(self.project)
self.expandAll()
[docs] def hasSelection(self):
"""function hasSelection: check if an object is selected, return True in this case"""
return self.selectionModel().hasSelection()
[docs] def hasParent(self):
if self.hasSelection():
index = self.selectedIndexes()[0]
item = index.model().itemFromIndex(index)
parent = item.parent()
return bool(parent)
else:
return False
[docs] def mainMenu(self):
"""
:return: Name of menu tab to automatically set current when current widget
begin current.
"""
return "Project"
[docs]class PrjctModel(QtGui.QStandardItemModel):
"""
Item model to use TreeView with a project.
Use:
# Project to display
project = ...
# Model to transform a project into a tree
proj_model = PrjctModel(project)
# Create tree view and set model
treeView = QtGui.QTreeView()
treeView.setModel(proj_model)
# Display
treeView.show()
"""
def __init__(self, session, controller, project, parent=None):
super(PrjctModel, self).__init__(parent)
# Use it to store evrything to compare with new when a change occure
self.controller = controller
self.old_models = list()
self.old_control = list()
self.old_scene = list()
self.proj = None
self.set_proj(project)
#QtCore.QObject.connect(self,QtCore.SIGNAL('dataChanged( const QModelIndex &, const QModelIndex &)'),self.renamed)
self.icons = dict()
for applet in self.controller.applet_container.paradigms.values():
self.icons[applet.extension] = applet.icon
[docs] def renamed(self,x,y):
if self.proj is not None:
if self.proj.is_project():
# Get item and his parent
parent = self.item(x.parent().row())
# Check if you have the right to rename
if parent:
item = parent.child(x.row())
# List brothers of item
children = list()
raw = parent.rowCount()
for i in range(raw):
child = parent.child(i)
children.append(child.text())
# Search which is the old_item which was changed and rename it
if parent.text() == "Models":
for i in self.old_models:
if i not in children:
self.proj.rename(category=parent.text(), old_name=i, new_name= item.text())
if parent.text() == "Control":
for i in children:
if i not in self.old_control:
self.proj.rename(category=parent.text(), old_name=i, new_name= item.text())
if parent.text() == "Scene":
for i in children:
if i not in self.old_scene:
self.proj.rename(category=parent.text(), old_name=i, new_name= item.text())
# Save project
self.proj.save()
[docs] def set_proj(self, proj=None):
self.clear()
if proj is not None:
self.proj = proj
self._set_levels()
def _set_levels(self):
icon_project = QtGui.QIcon(":/images/resources/openalea_icon2.png")
icon_src = QtGui.QIcon(":/images/resources/filenew.png")
icon_control = QtGui.QIcon(":/images/resources/node.png")
icon_scene = QtGui.QIcon(":/images/resources/plant.png")
icon_startup = QtGui.QIcon(":/images/resources/editredo.png")
icon_data = QtGui.QIcon(":/images/resources/fileopen.png")
icon_doc = QtGui.QIcon(":/images/resources/book.png")
icon_cache = QtGui.QIcon(":/images/resources/editcopy.png")
project = self.proj
name = project.name
parentItem = self.invisibleRootItem()
item = QtGui.QStandardItem(name)
files = project.files
# Propose icon by default.
# If project have another one, use it
icon = icon_project
if hasattr(project, "icon"):
icon_name = project.icon
if len(icon_name):
if icon_name[0] is not ":":
#local icon
icon_name = path(project.path)/name/icon_name
#else native icon from oalab.gui.resources
icon = QtGui.QIcon(icon_name)
item.setIcon(icon)
parentItem.appendRow(item)
categories = files.keys()
for category in categories:
if hasattr(project, category):
cat = getattr(project, category)
if len(cat) > 0:
item2 = QtGui.QStandardItem(category)
item.appendRow(item2)
try:
icon = eval(str("icon_"+category))
except NameError:
icon = QtGui.QIcon()
item2.setIcon(icon)
else:
# hide name of category if we don't have object of this category
pass
if isinstance(cat,dict):
for obj in cat.keys():
item3 = QtGui.QStandardItem(obj)
if category == "src":
ext = obj.split(".")[-1]
if ext in self.icons.keys():
item3.setIcon(QtGui.QIcon(self.icons[ext]))
item2.appendRow(item3)
else:
# Useful for category "localized" which store a bool and not a list
item3 = QtGui.QStandardItem(cat)
item2.appendRow(item3)
[docs]class PrjctManagerModel(QtGui.QStandardItemModel):
"""
Item model to use TreeView with the project manager.
Use:
from openalea.vpltk.project.manager import ProjectManager
import sys
app = QtGui.QApplication(sys.argv)
project_manager = ProjectManager()
project_manager.discover()
# Model to transform a project into a tree
proj_model = PrjctManagerModel(project_manager)
# Create tree view and set model
treeView = QtGui.QTreeView()
treeView.setModel(proj_model)
treeView.expandAll()
# Display
treeView.show()
app.exec_()
"""
def __init__(self, project_manager, parent=None):
super(PrjctManagerModel, self).__init__(parent)
self.projects = []
self.set_proj_manag(project_manager)
[docs] def set_proj_manag(self, project_manager):
self.clear()
project_manager.discover()
self.projects = project_manager.projects
self._set_levels()
def _set_levels(self):
icon_project = QtGui.QIcon(":/images/resources/openalea_icon2.png")
icon_src = QtGui.QIcon(":/images/resources/filenew.png")
icon_control = QtGui.QIcon(":/images/resources/node.png")
icon_scene = QtGui.QIcon(":/images/resources/plant.png")
icon_startup = QtGui.QIcon(":/images/resources/editredo.png")
icon_data = QtGui.QIcon(":/images/resources/fileopen.png")
icon_doc = QtGui.QIcon(":/images/resources/book.png")
icon_cache = QtGui.QIcon(":/images/resources/editcopy.png")
for project in self.projects:
name = project.name
parentItem = self.invisibleRootItem()
item = QtGui.QStandardItem(name)
files = project.files
# Propose icon by default.
# If project have another one, use it
icon = icon_project
if hasattr(project, "icon"):
icon_name = project.icon
if len(icon_name):
if icon_name[0] is not ":":
#local icon
icon_name = path(project.path)/name/icon_name
#else native icon from oalab.gui.resources
icon = QtGui.QIcon(icon_name)
item.setIcon(icon)
parentItem.appendRow(item)
categories = files.keys()
for category in categories:
if hasattr(project, category):
cat = getattr(project, category)
if len(cat) > 0:
item2 = QtGui.QStandardItem(category)
item.appendRow(item2)
try:
icon = eval(str("icon_"+category))
except NameError:
icon = QtGui.QIcon()
item2.setIcon(icon)
else:
# hide name of category if we don't have object of this category
pass
if isinstance(cat,dict):
for obj in cat.keys():
item3 = QtGui.QStandardItem(obj)
# ext = obj.split(".")[-1]
# if ext in self.icons.keys():
# item.setIcon(QtGui.QIcon(self.icons[ext]))
item2.appendRow(item3)
else:
# Useful for category "localized" which store a bool and not a list
item3 = QtGui.QStandardItem(cat)
item2.appendRow(item3)
def _set_level_0_only(self):
"""
Use it if you just want to see projects path
"""
for name in self.projects:
parentItem = self.invisibleRootItem()
item = QtGui.QStandardItem(name)
parentItem.appendRow(item)
[docs]def main():
from openalea.vpltk.project.manager import ProjectManager
import sys
app = QtGui.QApplication(sys.argv)
project_manager = ProjectManager()
# Model to transform a project into a tree
proj_model = PrjctManagerModel(project_manager)
# Create tree view and set model
treeView = QtGui.QTreeView()
treeView.setModel(proj_model)
#treeView.expandAll()
# Display
treeView.show()
app.exec_()
if __name__ == "__main__":
main()