# -*- python -*-
#
# OpenAlea.OALab: Multi-Paradigm GUI
#
# Copyright 2013-2015 INRIA - CIRAD - INRA
#
# File author(s): Julien Coste <julien.coste@inria.fr>
# Guillaume Baty <guillaume.baty@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
#
###############################################################################
"""
TODO:
- use project known categories instead of hard coded 'model', 'src', ...
"""
from openalea.vpltk.qt import QtGui
from openalea.core.service.data import DataClass, MimeType
from openalea.core.service.plugin import plugin_instance_exists, plugin_instance
from openalea.oalab.paradigm.creator import ParadigmCreator, ParadigmInfoSelector
from openalea.oalab.project.projectbrowser import ProjectBrowserWidget, ProjectBrowserView
from openalea.oalab.utils import ModalDialog
from openalea.oalab.widget import resources_rc
[docs]class ProjectEditorView(ProjectBrowserView):
def __init__(self):
ProjectBrowserView.__init__(self)
self.paradigm_container = None
self.contextual_creator = ParadigmCreator(self)
self.contextual_creator.paradigm_clicked.connect(self.new_contextual_paradigm)
self.paradigm = ParadigmCreator(self)
self.paradigm.paradigm_clicked.connect(self.new_paradigm)
[docs] def add_new_file_actions(self, menu, paradigms=None):
if paradigms is None:
menu.addActions(self.contextual_creator.actions())
else:
for paradigm in paradigms:
menu.addAction(self.contextual_creator.action(paradigm))
menu.addSeparator()
def _new_paradigm(self, project, category=None, dtype=None, name=None):
klass = DataClass(MimeType(name=dtype))
if name is None:
# Builtin default name for some categories
if category in ['startup', 'lib']:
d = {'startup': 'start.py', 'lib': 'algo.py'}
name = d[category]
else:
# If category defined, use it in name
if category:
name = '%s_%s.%s' % (klass.default_name.lower(), category.lower(), klass.extension)
else:
name = 'file.%s' % klass.extension
# Change extension to fit dtype (case name is given with wrong extension)
parts = name.split('.')
parts[-1] = klass.extension
default_name = '.'.join(parts)
if category:
categories = [category]
else:
categories = project.categories.keys()
# Show dialog
w = ParadigmInfoSelector(default_name, categories, [dtype],
project=project)
dialog = ModalDialog(w)
w.validity_changed.connect(dialog.set_valid)
dialog.set_valid(w.is_valid())
if dialog.exec_():
name = w.name()
category = w.category()
dtype = w.dtype()
p = project.path / category / name
if p.exists():
project.add(category, path=p)
else:
project.add(category, filename=name, content="", dtype=dtype)
self.item_added.emit(project, category, name)
[docs] def new_contextual_paradigm(self, dtype):
project, category, data = self.selected_data()
name = None
if category == 'category':
category = data
elif category in project.categories:
name = data
else:
category = None
self._new_paradigm(project, category, dtype, name)
[docs] def new_paradigm(self, dtype):
project = self.project()
self._new_paradigm(project, dtype=dtype)