예제 #1
0
파일: umbra.py 프로젝트: rustychris/umbra
    def dockwidget_show(self):
            # dockwidget may not exist if:
            #    first run of plugin
            #    removed on close (see self.onClosePlugin method)
            if self.dockwidget == None:
                # Create the dockwidget (after translation) and keep reference
                self.dockwidget = UmbraDockWidget()

                # connect to provide cleanup on closing of dockwidget
                # self.dockwidget.closingDockWidget.connect(self.on_close_dockwidget)

                # show the dockwidget
                self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget)
            self.dockwidget.show()
            print "Done starting"
예제 #2
0
파일: umbra.py 프로젝트: rustychris/umbra
class Umbra(Boiler):
    """  
    Core more specific to the Umbra plugin
    """ 
    def __init__(self, iface):
        global the_umbra
        the_umbra=self
        
        self.log=log
        super(Umbra,self).__init__(iface)
        self.log.info('Firing up Umbra')
        
        self.canvas=self.iface.mapCanvas()
        self.gridlayers=[]

    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""

        print "** initGui"

        icon_path = ':/plugins/Umbra/icon.png'
        # self.add_action(
        #     icon_path,
        #     text=self.tr(u'Umbra'),
        #     callback=self.run,
        #     parent=self.iface.mainWindow())

        self.editor_tool = umbra_editor_tool.UmbraEditorTool(self.iface,umbra=self)

        self.add_action(icon_path,text='Open Umbra layer',
                        callback=self.open_layer,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)
        
        self.add_action(icon_path,text='New Umbra layer',
                        callback=self.new_layer,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)
        
        self.add_action(icon_path,text='Save Umbra layer',
                        callback=self.save_layer,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)
        self.add_action(icon_path,text='Renumber nodes/edges/cells',
                        callback=self.renumber_layer,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)

        self.add_action(icon_path,text='Delete nodes by polygon',
                        callback=self.delete_nodes_by_polygon,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)

        self.add_action(icon_path,text='Show Umbra panel',
                        callback=self.dockwidget_show,
                        parent=self.iface.mainWindow(),
                        add_to_menu=True,
                        add_to_toolbar=False)
        
        self.add_action(icon_path,text='Mesh Edit',
                        callback=self.enable_tool,
                        parent=self.iface.mainWindow(),
                        add_to_menu=False,
                        add_to_toolbar=True)

    #--------------------------------------------------------------------------

    # def on_close_dockwidget(self):
    #     """
    #     record that the dockwidget was closed
    #     """
    # 
    #     log.info("** cleaning up dockwidget")
    # 
    #     # disconnects
    #     self.dockwidget.closingDockWidget.disconnect(self.on_close_dockwidget)
    #     self.dockwidget=None
    #     log.info("disconnected")

    def unload(self):
        """Removes the plugin menu item and icon from QGIS GUI."""

        print "** UNLOAD Umbra"

        try:
            for action in self.actions:
                self.iface.removePluginMenu(
                    self.tr(u'&Umbra'),
                    action)
                self.iface.removeToolBarIcon(action)
        except Exception as exc:
            print "While removing toolbaricon"
            print exc
        
        # remove the toolbar(?)
        self.toolbar=None

        self.dockwidget_hide() # ideally really remove it, but maybe good enough to just hide.
        
        # remove any umbra layers - doesn't seem to be working.
        for gridlayer in self.gridlayers:
            gridlayer.remove_all_qlayers()

    #--------------------------------------------------------------------------

    def activate(self):
        if not self.pluginIsActive:
            self.pluginIsActive = True

            print "** STARTING Umbra"
            self.dockwidget_show()

            # if this is omitted, be sure to also skip over
            # the disconnects.
            # this doesn't seem to really work, and leads to a bunch
            # of callbacks lying around...
            #li=self.iface.legendInterface()
            #li.currentLayerChanged.connect(self.on_layer_changed)

    def dockwidget_show(self):
            # dockwidget may not exist if:
            #    first run of plugin
            #    removed on close (see self.onClosePlugin method)
            if self.dockwidget == None:
                # Create the dockwidget (after translation) and keep reference
                self.dockwidget = UmbraDockWidget()

                # connect to provide cleanup on closing of dockwidget
                # self.dockwidget.closingDockWidget.connect(self.on_close_dockwidget)

                # show the dockwidget
                self.iface.addDockWidget(Qt.LeftDockWidgetArea, self.dockwidget)
            self.dockwidget.show()
            print "Done starting"

    def dockwidget_hide(self):
        if self.dockwidget is not None:
            self.dockwidget.hide()

    def current_layer_is_umbra(self,clayer=None):
        # moved from tool
        if clayer is None:
            clayer = self.canvas.currentLayer()
            if clayer is None:
                # NB: possible that a group is selected here, but we
                # have no way of checking for that.
                return False
        for gridlayer in self.gridlayers:
            self.log.info('Checking for layers in %s'%gridlayer)
            for layer in gridlayer.layers:
                if clayer==layer:
                    return True
        return False

    def current_grid(self):
        gridlayer=self.active_gridlayer()
        if gridlayer is not None:
            return gridlayer.grid
        else:
            log.warning("request for current grid, but umbra didn't find one")
            return None

    def open_layer(self):
        self.activate()
        dialog=umbra_openlayer.UmbraOpenLayer(parent=self.iface.mainWindow(),
                                              iface=self.iface,
                                              umbra=self)
        dialog.exec_()

    def new_layer(self):
        self.activate()
        dialog=umbra_newlayer.UmbraNewLayer(parent=self.iface.mainWindow(),
                                            iface=self.iface,
                                            umbra=self)
        dialog.exec_()
        
    def active_gridlayer(self):
        if not self.pluginIsActive:
            log.info("active_gridlayer: plugin not active")
            return None
        
        clayer = self.canvas.currentLayer()
        if clayer is None:
            return None
        log.info("Searching for layer " + str(clayer))

        for gridlayer in self.gridlayers:
            if gridlayer.match_to_qlayer(clayer):
                return gridlayer
        return None

    def save_layer(self):
        glayer = self.active_gridlayer()
        if glayer is None:
            return

        # TODO: probably this gets routed through UmbraLayer?
        dialog=umbra_savelayer.UmbraSaveLayer(parent=self.iface.mainWindow(),
                                              iface=self.iface,
                                              umbra=self)
        dialog.exec_()
    
    def renumber_layer(self):
        glayer = self.active_gridlayer()
        if glayer is None:
            return
        glayer.grid.renumber()

    def delete_nodes_by_polygon(self):
        pass

    def enable_tool(self):
        log.info("Enabled umbra mapTool")
        self.iface.mapCanvas().setMapTool(self.editor_tool)

    def run(self):
        """Run method that loads and starts the plugin"""
        print "** Call to run"
        self.activate()

    def register_grid(self,ul):
        self.gridlayers.append(ul)
        
    def on_layer_changed(self,layer):
        if layer is not None:
            print "on_layer_changed"
            self.log.info('on layer changed')
            print "layer is ",layer
            # Not sure why, but using layer.id() was triggering an error
            # about the layer being deleted.  Hmm - this is still somehow
            # related to an error about the layer being deleted.
            if self.current_layer_is_umbra(layer):
                self.log.info("Setting map tool to ours")
                self.iface.mapCanvas().setMapTool(self.editor_tool)
                self.log.info("Done with setting map tool to ours")