示例#1
0
 def open_project(self, name=False, path=None):
     """
     If name==false, display a widget to choose project to open.
     Then open project.
     """
     self.proj_selector = ProjectExplorer()
     dialog = ModalDialog(self.proj_selector)
     dialog.resize(800, 600)
     dialog.setContentsMargins(3, 3, 3, 3)
     if dialog.exec_():
         project = self.proj_selector.item()
         if project:
             self.set_project(project)
             set_active_project(project)
示例#2
0
 def open_project(self, name=False, path=None):
     """
     If name==false, display a widget to choose project to open.
     Then open project.
     """
     self.proj_selector = ProjectExplorer()
     dialog = ModalDialog(self.proj_selector)
     dialog.resize(800, 600)
     dialog.setContentsMargins(3, 3, 3, 3)
     if dialog.exec_():
         project = self.proj_selector.item()
         if project:
             self.set_project(project)
             set_active_project(project)
示例#3
0
class ProjectBrowserView(QtGui.QTreeView, AbstractListener):
    item_added = QtCore.Signal(object, str, str) # project, category, name
    item_clicked = QtCore.Signal(object, str, str) # project, category, name
    item_double_clicked = QtCore.Signal(object, str, str) # project, category, name
    item_removed = QtCore.Signal(object, str, object) # project, category, item
    project_closed = QtCore.Signal(object) # old project
    project_open = QtCore.Signal(object) # new project

    def __init__(self):
        QtGui.QTreeView.__init__(self)
        AbstractListener.__init__(self)

        self._model = ProjectModel()

        self.setModel(self._model)
        self._model.dataChanged.connect(self._on_model_changed)

        self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.connect(self, QtCore.SIGNAL('doubleClicked(const QModelIndex&)'), self.open)

        self.setHeaderHidden(True)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setAcceptDrops(True)

        self._create_actions()

    def _create_actions(self):
        self._actions = []

        self.actionEditMeta = QtGui.QAction(
            qicon("adwaita_accessories-dictionary.png"),
            "Edit Project Information",
            self)
        self.actionEditMeta.triggered.connect(self.edit_metadata)

        self.actionCloseProj = QtGui.QAction(qicon("close_project.png"), "Close project", self)
        self.actionCloseProj.triggered.connect(self.close)
        self.actionCloseProj.setShortcut(self.tr("Ctrl+Shift+W"))

        self.actionOpenProj = QtGui.QAction(qicon("open_project.png"), "Open Project", self)
        self.actionOpenProj.triggered.connect(self.open_project)
        self.actionOpenProj.setShortcut(self.tr('Ctrl+Shift+O'))

        self.action_save_proj = QtGui.QAction(qicon("save_project.png"), "Save Project", self)
        self.action_save_proj.triggered.connect(self.save_project)
        self.action_save_proj.setShortcut(self.tr('Ctrl+Shift+S'))

    #  API
    def notify(self, sender, event=None):
        signal, data = event
        if signal == 'project_changed':
            self.refresh()

    def set_project(self, project):
        old_proj = self._model.project()
        if old_proj:
            old_proj.unregister_listener(self)
        if project:
            project.register_listener(self)

        # TODO: Dirty hack to remove asap. Close project selector if widget has been created
        if hasattr(self, "proj_selector"):
            del self.proj_selector

        self._model.set_project(project)

        if project:
            self.project_closed.emit(old_proj)
            self.project_open.emit(project)
        else:
            self.project_closed.emit(old_proj)

    def refresh(self):
        self._model.refresh()

    #  Convenience methods

    def getItem(self):
        index = self.getIndex()
        if index:
            return self._model.itemFromIndex(index)

    def getIndex(self):
        indices = self.selectedIndexes()
        for index in indices:
            return index

    def project(self):
        return self._model.project()

    def selected_data(self):
        index = self.getIndex()
        project = self.project()
        data = self._model.projectdata(index)
        if project is None:
            return (None, None, None)
        elif project and data is None:
            return project, None, None
        else:
            category, name = data
            return project, category, name

    #  Slots

    def _on_model_changed(self):
        self.expandAll()

    #  Contextual menu

    def create_menu(self):
        menu = QtGui.QMenu(self)
        project, category, obj = self.selected_data()

        if project and category is None:
            menu.addAction(self.actionEditMeta)
            menu.addAction(self.actionCloseProj)

        elif category == 'category' and obj == 'data':
            import_data = QtGui.QAction(qicon('import.png'), 'Import data', self)
            import_data.triggered.connect(self.open)
            menu.addAction(import_data)

        if category in ['model', 'src', 'startup', 'doc', 'data', 'lib']:
            editAction = QtGui.QAction(qicon('open.png'), 'Open "%s"' % obj, self)
            menu.addAction(editAction)
            editAction.triggered.connect(self.open)

            rename = QtGui.QAction(qicon('Crystal_Clear_action_editcopy.png'), 'Rename', self)
            rename.triggered.connect(self.rename)
            menu.addAction(rename)

            remove = QtGui.QAction(qicon('Crystal_Clear_action_edit_remove.png'), 'Remove', self)
            remove.triggered.connect(self.remove)
            menu.addAction(remove)

            menu.addSeparator()

            deleteAction = QtGui.QAction(qicon('Crystal_Clear_action_stop.png'), 'Delete', self)
            menu.addAction(deleteAction)
            deleteAction.triggered.connect(self.delete)

        return menu

    def contextMenuEvent(self, event):
        if self.project() is None:
            return
        menu = self.create_menu()
        menu.exec_(event.globalPos())

    #  Action's slots

    def edit_metadata(self):
        project = self.project()
        rvalue = edit_metadata(project)
        if rvalue:
            self.refresh()

    def open_project(self, name=False, path=None):
        """
        If name==false, display a widget to choose project to open.
        Then open project.
        """
        self.proj_selector = ProjectExplorer()
        dialog = ModalDialog(self.proj_selector)
        dialog.resize(800, 600)
        dialog.setContentsMargins(3, 3, 3, 3)
        if dialog.exec_():
            project = self.proj_selector.item()
            if project:
                self.set_project(project)
                set_active_project(project)

    def open(self):
        project, category, name = self.selected_data()
        if project and category:
            if category == 'category' and name == 'data':
                p = QtGui.QFileDialog.getOpenFileName(self, 'Select File to open', project.path, "All (*)")
                if p:
                    p = path(p)
                    project.add(name, path=p)
            elif category == 'category':
                pass
            elif category == 'project':
                pass
                # self.open_all_scripts_from_project(project)
            elif category == 'data':
                from openalea.file.files import start
                start(project.get(category, name).path)
            else:
                self.item_double_clicked.emit(project, category, name)

    def save_project(self):
        project = self.project()
        if project:
            project.save()

    def _rename(self, project, category, name):
        if category in project.categories:
            rename_model(project, category, name)
        elif category == 'project':
            self.edit_metadata()

    def rename(self):
        project, category, name = self.selected_data()
        if project and category:
            self._rename(project, category, name)

    def remove(self):
        project, category, name = self.selected_data()
        if project and category:
            project.remove(category, filename=name)

    def delete(self):
        project, category, name = self.selected_data()
        if project:
            if category in project.categories:
                data = project.get(category, name)

                confirm = QtGui.QLabel('Remove %s ?' % data.path)
                dialog = ModalDialog(confirm)
                if dialog.exec_():
                    project.remove(category, data)
                    data.path.remove()
                    self.item_removed.emit(project, category, data)

    def close(self):
        self.set_project(None)

    def import_file(self):
        print 'import_file'
示例#4
0
class ProjectBrowserView(QtGui.QTreeView, AbstractListener):
    item_added = QtCore.Signal(object, str, str)  # project, category, name
    item_clicked = QtCore.Signal(object, str, str)  # project, category, name
    item_double_clicked = QtCore.Signal(object, str,
                                        str)  # project, category, name
    item_removed = QtCore.Signal(object, str,
                                 object)  # project, category, item
    project_closed = QtCore.Signal(object)  # old project
    project_open = QtCore.Signal(object)  # new project

    def __init__(self):
        QtGui.QTreeView.__init__(self)
        AbstractListener.__init__(self)

        self._model = ProjectModel()

        self.setModel(self._model)
        self._model.dataChanged.connect(self._on_model_changed)

        self.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.connect(self, QtCore.SIGNAL('doubleClicked(const QModelIndex&)'),
                     self.open)

        self.setHeaderHidden(True)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setAcceptDrops(True)

        self._create_actions()

    def _create_actions(self):
        self._actions = []

        self.actionEditMeta = QtGui.QAction(
            qicon("adwaita_accessories-dictionary.png"),
            "Edit Project Information", self)
        self.actionEditMeta.triggered.connect(self.edit_metadata)

        self.actionCloseProj = QtGui.QAction(qicon("close_project.png"),
                                             "Close project", self)
        self.actionCloseProj.triggered.connect(self.close)
        self.actionCloseProj.setShortcut(self.tr("Ctrl+Shift+W"))

        self.actionOpenProj = QtGui.QAction(qicon("open_project.png"),
                                            "Open Project", self)
        self.actionOpenProj.triggered.connect(self.open_project)
        self.actionOpenProj.setShortcut(self.tr('Ctrl+Shift+O'))

        self.action_save_proj = QtGui.QAction(qicon("save_project.png"),
                                              "Save Project", self)
        self.action_save_proj.triggered.connect(self.save_project)
        self.action_save_proj.setShortcut(self.tr('Ctrl+Shift+S'))

    #  API
    def notify(self, sender, event=None):
        signal, data = event
        if signal == 'project_changed':
            self.refresh()

    def set_project(self, project):
        old_proj = self._model.project()
        if old_proj:
            old_proj.unregister_listener(self)
        if project:
            project.register_listener(self)

        # TODO: Dirty hack to remove asap. Close project selector if widget has been created
        if hasattr(self, "proj_selector"):
            del self.proj_selector

        self._model.set_project(project)

        if project:
            self.project_closed.emit(old_proj)
            self.project_open.emit(project)
        else:
            self.project_closed.emit(old_proj)

    def refresh(self):
        self._model.refresh()

    #  Convenience methods

    def getItem(self):
        index = self.getIndex()
        if index:
            return self._model.itemFromIndex(index)

    def getIndex(self):
        indices = self.selectedIndexes()
        for index in indices:
            return index

    def project(self):
        return self._model.project()

    def selected_data(self):
        index = self.getIndex()
        project = self.project()
        data = self._model.projectdata(index)
        if project is None:
            return (None, None, None)
        elif project and data is None:
            return project, None, None
        else:
            category, name = data
            return project, category, name

    #  Slots

    def _on_model_changed(self):
        self.expandAll()

    #  Contextual menu

    def create_menu(self):
        menu = QtGui.QMenu(self)
        project, category, obj = self.selected_data()

        if project and category is None:
            menu.addAction(self.actionEditMeta)
            menu.addAction(self.actionCloseProj)

        elif category == 'category' and obj == 'data':
            import_data = QtGui.QAction(qicon('import.png'), 'Import data',
                                        self)
            import_data.triggered.connect(self.open)
            menu.addAction(import_data)

        if category in ['model', 'src', 'startup', 'doc', 'data', 'lib']:
            editAction = QtGui.QAction(qicon('open.png'), 'Open "%s"' % obj,
                                       self)
            menu.addAction(editAction)
            editAction.triggered.connect(self.open)

            rename = QtGui.QAction(qicon('Crystal_Clear_action_editcopy.png'),
                                   'Rename', self)
            rename.triggered.connect(self.rename)
            menu.addAction(rename)

            remove = QtGui.QAction(
                qicon('Crystal_Clear_action_edit_remove.png'), 'Remove', self)
            remove.triggered.connect(self.remove)
            menu.addAction(remove)

            menu.addSeparator()

            deleteAction = QtGui.QAction(
                qicon('Crystal_Clear_action_stop.png'), 'Delete', self)
            menu.addAction(deleteAction)
            deleteAction.triggered.connect(self.delete)

        return menu

    def contextMenuEvent(self, event):
        if self.project() is None:
            return
        menu = self.create_menu()
        menu.exec_(event.globalPos())

    #  Action's slots

    def edit_metadata(self):
        project = self.project()
        rvalue = edit_metadata(project)
        if rvalue:
            self.refresh()

    def open_project(self, name=False, path=None):
        """
        If name==false, display a widget to choose project to open.
        Then open project.
        """
        self.proj_selector = ProjectExplorer()
        dialog = ModalDialog(self.proj_selector)
        dialog.resize(800, 600)
        dialog.setContentsMargins(3, 3, 3, 3)
        if dialog.exec_():
            project = self.proj_selector.item()
            if project:
                self.set_project(project)
                set_active_project(project)

    def open(self):
        project, category, name = self.selected_data()
        if project and category:
            if category == 'category' and name == 'data':
                p = QtGui.QFileDialog.getOpenFileName(self,
                                                      'Select File to open',
                                                      project.path, "All (*)")
                if p:
                    p = path(p)
                    project.add(name, path=p)
            elif category == 'category':
                pass
            elif category == 'project':
                pass
                # self.open_all_scripts_from_project(project)
            elif category == 'data':
                from openalea.file.files import start
                start(project.get(category, name).path)
            else:
                self.item_double_clicked.emit(project, category, name)

    def save_project(self):
        project = self.project()
        if project:
            project.save()

    def _rename(self, project, category, name):
        if category in project.categories:
            rename_model(project, category, name)
        elif category == 'project':
            self.edit_metadata()

    def rename(self):
        project, category, name = self.selected_data()
        if project and category:
            self._rename(project, category, name)

    def remove(self):
        project, category, name = self.selected_data()
        if project and category:
            project.remove(category, filename=name)

    def delete(self):
        project, category, name = self.selected_data()
        if project:
            if category in project.categories:
                data = project.get(category, name)

                confirm = QtGui.QLabel('Remove %s ?' % data.path)
                dialog = ModalDialog(confirm)
                if dialog.exec_():
                    project.remove(category, data)
                    data.path.remove()
                    self.item_removed.emit(project, category, data)

    def close(self):
        self.set_project(None)

    def import_file(self):
        print 'import_file'