def OnContextMenu(self, event): """Show context menu. So far offers only copying layer list to clipboard """ if len(self._layerList) < 1: event.Skip() return menu = Menu() llist = [layer.name for layer in self._layerList] texts = [",".join(llist), ",".join(reversed(llist))] labels = [ _("Copy map names to clipboard (top to bottom)"), _("Copy map names to clipboard (bottom to top)"), ] for label, text in zip(labels, texts): id = NewId() self.Bind(wx.EVT_MENU, lambda evt, t=text, id=id: self._copyText(t), id=id) menu.Append(id, label) # show the popup menu self.PopupMenu(menu) menu.Destroy() event.Skip()
def OnToolMenu(self, event): """Menu for additional tools""" toolMenu = Menu() for label, itype, handler, desc in ( ( _("Switch orientation"), wx.ITEM_NORMAL, self.parent.OnSwitchOrientation, "switchOrientation", ), ( _("Switch maps"), wx.ITEM_NORMAL, self.parent.OnSwitchWindows, "switchMaps", ), ): # Add items to the menu item = wx.MenuItem( parentMenu=toolMenu, id=wx.ID_ANY, text=label, kind=itype ) toolMenu.AppendItem(item) self.parent.GetWindow().Bind(wx.EVT_MENU, handler, item) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.parent.GetWindow().PopupMenu(toolMenu) toolMenu.Destroy()
def OnContextMenu(self, event): """Popup context menu enabling to choose a widget that will be shown in statusbar.""" def setSbItemProperty(idx): self.mapFrame.mapWindowProperties.sbItem = idx def getSbItemProperty(): return self.mapFrame.mapWindowProperties.sbItem menu = Menu() for i, label in enumerate(self.GetItemLabels()): wxid = NewId() self.statusbar.Bind( wx.EVT_MENU, lambda evt, idx=i: setSbItemProperty(idx), id=wxid, ) menu.Append(wxid, label, kind=wx.ITEM_RADIO) item = menu.FindItem(wxid)[0] if i == getSbItemProperty(): item.Check(item.IsChecked() is False) if label in (self.GetDisabledItemLabels()): item.Enable(enable=False) # show the popup menu self.statusbar.PopupMenu(menu) menu.Destroy() event.Skip()
def _popupMenuEmpty(self): """Create empty popup when multiple different types of items are selected""" menu = Menu() item = wx.MenuItem(menu, wx.ID_ANY, _("No available options")) menu.AppendItem(item) item.Enable(False) self.PopupMenu(menu) menu.Destroy()
def ShowContextMenu(self, node): """Show context menu. Menu for copying distinguishes single and multiple selection. """ nodes = self.tree.GetSelected() if not nodes: return menu = Menu() texts = [] if len(nodes) > 1: values = [] for node in nodes: values.append( (node.label, node.data[ self._colNames[1]] if node.data else '')) col1 = '\n'.join([val[1] for val in values if val[1]]) col2 = '\n'.join([val[0] for val in values if val[0]]) table = '\n'.join([val[0] + ': ' + val[1] for val in values]) texts.append( (_("Copy from '%s' column") % self._colNames[1], col1)) texts.append( (_("Copy from '%s' column") % self._colNames[0], col2)) texts.append((_("Copy selected lines"), table)) else: label1 = nodes[0].label texts.append((_("Copy '%s'" % self._cutLabel(label1)), label1)) if nodes[0].data and nodes[0].data[self._colNames[1]]: label2 = nodes[0].data[self._colNames[1]] texts.insert( 0, (_( "Copy '%s'" % self._cutLabel(label2)), label2)) texts.append((_("Copy line"), label1 + ': ' + label2)) ids = [] for text in texts: id = NewId() ids.append(id) self.Bind( wx.EVT_MENU, lambda evt, t=text[1], id=id: self._copyText(t), id=id) menu.Append(id, text[0]) # show the popup menu self.PopupMenu(menu) menu.Destroy() for id in ids: self.Unbind(wx.EVT_MENU, id=id)
def _onMenu(self, data): """Toolbar pop-up menu""" menu = Menu() for icon, handler in data: item = wx.MenuItem(menu, wx.ID_ANY, icon.GetLabel()) item.SetBitmap(icon.GetBitmap(self.parent.iconsize)) menu.AppendItem(item) self.Bind(wx.EVT_MENU, handler, item) self.PopupMenu(menu) menu.Destroy()
def _popupMenuElement(self): """Create popup menu for elements""" menu = Menu() item = wx.MenuItem(menu, wx.ID_ANY, _("&Paste")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnPasteMap, item) genv = gisenv() currentLocation, currentMapset = self._isCurrent(genv) if not (currentMapset and self.copy_layer): item.Enable(False) self.PopupMenu(menu) menu.Destroy()
def OnZoomMenu(self, event): """Popup Zoom menu""" point = wx.GetMousePosition() zoommenu = Menu() # Add items to the menu zoomwind = wx.MenuItem( zoommenu, wx.ID_ANY, _("Zoom to computational region (set with g.region)")) zoommenu.AppendItem(zoomwind) self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind) zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to default region")) zoommenu.AppendItem(zoomdefault) self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault) zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to saved region")) zoommenu.AppendItem(zoomsaved) self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved) savewind = wx.MenuItem(zoommenu, wx.ID_ANY, _("Set computational region from display")) zoommenu.AppendItem(savewind) self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind) savezoom = wx.MenuItem(zoommenu, wx.ID_ANY, _("Save display geometry to named region")) zoommenu.AppendItem(savezoom) self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.PopupMenu(zoommenu) zoommenu.Destroy()
def PrintMenu(self, event): """Print options and output menu """ point = wx.GetMousePosition() printmenu = Menu() for title, handler in ((_("Page setup"), self.OnPageSetup), (_("Print preview"), self.OnPrintPreview), (_("Print display"), self.OnDoPrint)): item = wx.MenuItem(printmenu, wx.ID_ANY, title) printmenu.AppendItem(item) self.Bind(wx.EVT_MENU, handler, item) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.PopupMenu(printmenu) printmenu.Destroy()
def __init__(self, app): wx.Frame.__init__(self, None, title="d.rast.edit (%s)" % app.inmap) self.app = app self.Bind(wx.EVT_CLOSE, self.OnClose) filemenu = Menu() filemenu.Append(wx.ID_SAVE, "&Save", "Save changes") filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate the program") menubar = wx.MenuBar() menubar.Append(filemenu, "&File") self.SetMenuBar(menubar) self.Bind(wx.EVT_MENU, self.OnSave) self.Bind(wx.EVT_MENU, self.OnExit) sizer = wx.BoxSizer(orient=wx.VERTICAL) self.canvas = Canvas(app, parent=self) sizer.Add(self.canvas, proportion=1, flag=wx.EXPAND) tools = wx.BoxSizer(wx.HORIZONTAL) l = wx.StaticText(parent=self, label="New Value:") tools.Add(l, flag=wx.ALIGN_CENTER_VERTICAL) tools.AddSpacer(5) self.newval = wx.TextCtrl(parent=self, style=wx.TE_PROCESS_ENTER) tools.Add(self.newval, flag=wx.ALIGN_CENTER_VERTICAL) l = wx.StaticText(parent=self, label="Color:") tools.Add(l, flag=wx.ALIGN_CENTER_VERTICAL) tools.AddSpacer(5) self.color = ColorPanel(parent=self, size=(30, 5)) tools.Add(self.color, flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND) self.Bind(wx.EVT_TEXT_ENTER, self.OnReturn, source=self.newval) sizer.Add(tools, proportion=0, flag=wx.EXPAND) self.SetSizerAndFit(sizer) self.SetSize((app.width, app.height)) self.status = self.CreateStatusBar(6) self.brush_update()
def PlotOptionsMenu(self, event): """Popup menu for plot and text options""" point = wx.GetMousePosition() popt = Menu() # Add items to the menu settext = wx.MenuItem(popt, wx.ID_ANY, _("Text settings")) popt.AppendItem(settext) self.Bind(wx.EVT_MENU, self.PlotText, settext) setgrid = wx.MenuItem(popt, wx.ID_ANY, _("Plot settings")) popt.AppendItem(setgrid) self.Bind(wx.EVT_MENU, self.PlotOptions, setgrid) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.PopupMenu(popt) popt.Destroy()
def OnAdditionalToolMenu(self, event): """Menu for additional tools""" point = wx.GetMousePosition() toolMenu = Menu() for label, itype, handler, desc in ( (_('Break selected lines/boundaries at intersection'), wx.ITEM_CHECK, self.OnBreak, "breakLine"), (_('Connect selected lines/boundaries'), wx.ITEM_CHECK, self.OnConnect, "connectLine"), (_('Copy categories'), wx.ITEM_CHECK, self.OnCopyCats, "copyCats"), (_('Copy features from (background) vector map'), wx.ITEM_CHECK, self.OnCopy, "copyLine"), (_('Copy attributes'), wx.ITEM_CHECK, self.OnCopyAttrb, "copyAttrs"), (_('Feature type conversion'), wx.ITEM_CHECK, self.OnTypeConversion, "typeConv"), (_('Flip selected lines/boundaries'), wx.ITEM_CHECK, self.OnFlip, "flipLine"), (_('Merge selected lines/boundaries'), wx.ITEM_CHECK, self.OnMerge, "mergeLine"), (_('Snap selected lines/boundaries (only to nodes)'), wx.ITEM_CHECK, self.OnSnap, "snapLine"), (_('Split line/boundary'), wx.ITEM_CHECK, self.OnSplitLine, "splitLine"), (_('Query features'), wx.ITEM_CHECK, self.OnQuery, "queryLine"), (_('Z bulk-labeling of 3D lines'), wx.ITEM_CHECK, self.OnZBulk, "zbulkLine")): # Add items to the menu item = wx.MenuItem(parentMenu=toolMenu, id=wx.ID_ANY, text=label, kind=itype) toolMenu.AppendItem(item) self.MapWindow.Bind(wx.EVT_MENU, handler, item) if self.action['desc'] == desc: item.Check(True) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.MapWindow.PopupMenu(toolMenu) toolMenu.Destroy() if self.action['desc'] == 'addPoint': self.ToggleTool(self.additionalTools, False)
def _createMenu(self, node): """Creates menu""" menu = MenuWidget() for child in node.children: if child.children: label = child.label subMenu = self._createMenu(child) menu.AppendMenu(wx.ID_ANY, label, subMenu) else: data = child.data.copy() data.pop("label") self._createMenuItem(menu, label=child.label, **data) self.parent.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight) return menu
def OnContextMenu(self, node): if not hasattr(self, "popupID"): self.popupID = dict() for key in ("install", "help"): self.popupID[key] = NewId() data = node.data if data and "command" in data: self.popupMenu = Menu() self.popupMenu.Append(self.popupID["install"], _("Install")) self.Bind(wx.EVT_MENU, self.OnInstall, id=self.popupID["install"]) self.popupMenu.AppendSeparator() self.popupMenu.Append(self.popupID["help"], _("Show manual page")) self.Bind(wx.EVT_MENU, self.OnItemHelp, id=self.popupID["help"]) self.PopupMenu(self.popupMenu) self.popupMenu.Destroy()
def OnContextMenu(self, node): if not hasattr(self, "popupID"): self.popupID = dict() for key in ('install', 'help'): self.popupID[key] = wx.NewId() data = node.data if data and 'command' in data: self.popupMenu = Menu() self.popupMenu.Append(self.popupID['install'], text=_("Install")) self.Bind(wx.EVT_MENU, self.OnInstall, id=self.popupID['install']) self.popupMenu.AppendSeparator() self.popupMenu.Append(self.popupID['help'], text=_("Show manual page")) self.Bind(wx.EVT_MENU, self.OnItemHelp, id=self.popupID['help']) self.PopupMenu(self.popupMenu) self.popupMenu.Destroy()
def ContexMenu(self, event): if not event.inaxes: return if event.button == 3: menu = Menu() menu_items = [[ "zoom_to_extend", _("Zoom to scatter plot extend"), lambda event: self.plot.ZoomToExtend() ]] for item in menu_items: item_id = NewId() menu.Append(item_id, item[1]) menu.Bind(wx.EVT_MENU, item[2], id=item_id) wx.CallAfter(self.ShowMenu, menu)
def OnClassRightUp(self, event): """Show context menu on right click""" item, flags = self.HitTest((event.GetX(), event.GetY())) if item != wx.NOT_FOUND and flags & wx.LIST_HITTEST_ONITEM: self.rightClickedItemIdx = item if not hasattr(self, "popupZoomtoAreas"): self.popupZoomtoAreas = NewId() self.Bind(wx.EVT_MENU, self.OnZoomToAreasByCat, id=self.popupZoomtoAreas) # generate popup-menu menu = Menu() menu.Append( self.popupZoomtoAreas, _("Zoom to training areas of selected class") ) self.PopupMenu(menu) menu.Destroy()
def _popupMenuMapset(self): """Create popup menu for mapsets""" menu = Menu() genv = gisenv() currentLocation, currentMapset = self._isCurrent(genv) item = wx.MenuItem(menu, wx.ID_ANY, _("&Paste")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnPasteMap, item) if not (currentMapset and self.copy_layer): item.Enable(False) item = wx.MenuItem(menu, wx.ID_ANY, _("&Switch mapset")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnSwitchLocationMapset, item) if (self.selected_location[0].label == genv['LOCATION_NAME'] and self.selected_mapset[0].label == genv['MAPSET']): item.Enable(False) self.PopupMenu(menu) menu.Destroy()
def OnRightUp(self, event): """Mouse right button up""" if not hasattr(self, "popupID1"): self.popupID1 = NewId() self.popupID2 = NewId() self.popupID3 = NewId() self.Bind(wx.EVT_MENU, self.OnItemDelete, id=self.popupID1) self.Bind(wx.EVT_MENU, self.OnItemDeleteAll, id=self.popupID2) self.Bind(wx.EVT_MENU, self.OnReload, id=self.popupID3) # generate popup-menu menu = Menu() menu.Append(self.popupID1, _("Delete selected")) if self.list.GetFirstSelected() == -1: menu.Enable(self.popupID1, False) menu.Append(self.popupID2, _("Delete all")) menu.AppendSeparator() menu.Append(self.popupID3, _("Reload")) self.PopupMenu(menu) menu.Destroy()
def OnRightUp(self, event): """Mouse right button up""" if self.disablePopup: return if not hasattr(self, "popupId"): self.popupID = dict() self.popupID['remove'] = NewId() self.popupID['reload'] = NewId() self.Bind(wx.EVT_MENU, self.OnRemove, id=self.popupID['remove']) self.Bind(wx.EVT_MENU, self.OnReload, id=self.popupID['reload']) # generate popup-menu menu = Menu() menu.Append(self.popupID['remove'], _("Delete selected")) if self.GetFirstSelected() == -1: menu.Enable(self.popupID['remove'], False) menu.AppendSeparator() menu.Append(self.popupID['reload'], _("Reload")) self.PopupMenu(menu) menu.Destroy()
def PrintMenu(self, event): """Print options and output menu""" point = wx.GetMousePosition() printmenu = Menu() # Add items to the menu setup = wx.MenuItem(printmenu, id=wx.ID_ANY, text=_("Page setup")) printmenu.AppendItem(setup) self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup) preview = wx.MenuItem(printmenu, id=wx.ID_ANY, text=_("Print preview")) printmenu.AppendItem(preview) self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview) doprint = wx.MenuItem(printmenu, id=wx.ID_ANY, text=_("Print display")) printmenu.AppendItem(doprint) self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint) # Popup the menu. If an item is selected then its handler # will be called before PopupMenu returns. self.PopupMenu(printmenu) printmenu.Destroy()
def _popupMenuLayer(self): """Create popup menu for layers""" menu = Menu() genv = gisenv() currentLocation, currentMapset = self._isCurrent(genv) item = wx.MenuItem(menu, wx.NewId(), _("&Cut")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnMoveMap, item) if not currentMapset: item.Enable(False) item = wx.MenuItem(menu, wx.NewId(), _("&Copy")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnCopyMap, item) item = wx.MenuItem(menu, wx.NewId(), _("Copy &name")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnCopyName, item) item = wx.MenuItem(menu, wx.NewId(), _("&Paste")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnPasteMap, item) if not (currentMapset and self.copy_layer): item.Enable(False) item = wx.MenuItem(menu, wx.NewId(), _("&Delete")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnDeleteMap, item) item.Enable(currentMapset) item = wx.MenuItem(menu, wx.NewId(), _("&Rename")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnRenameMap, item) item.Enable(currentMapset) menu.AppendSeparator() if not isinstance(self._giface, StandaloneGrassInterface) and \ self.selected_location.label == genv['LOCATION_NAME']: item = wx.MenuItem(menu, wx.NewId(), _("&Display layer")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnDisplayLayer, item) item = wx.MenuItem(menu, wx.NewId(), _("Show &metadata")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnMetadata, item) self.PopupMenu(menu) menu.Destroy()
class InstallExtensionWindow(wx.Frame): def __init__( self, parent, giface, id=wx.ID_ANY, title=_("Fetch & install extension from GRASS Addons"), **kwargs, ): self.parent = parent self._giface = giface self.options = dict() # list of options wx.Frame.__init__(self, parent=parent, id=id, title=title, **kwargs) self.SetIcon( wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)) self.panel = wx.Panel(parent=self, id=wx.ID_ANY) # self.repoBox = StaticBox( # parent=self.panel, id=wx.ID_ANY, label=" %s " % # _("Repository (leave empty to use the official one)")) self.treeBox = StaticBox( parent=self.panel, id=wx.ID_ANY, label=" %s " % _("List of extensions - double-click to install"), ) # self.repo = TextCtrl(parent=self.panel, id=wx.ID_ANY) # modelBuilder loads data into tree model self.modelBuilder = ExtensionTreeModelBuilder() # tree view displays model data self.tree = CTreeView(parent=self.panel, model=self.modelBuilder.GetModel()) self.search = SearchCtrl(self.panel) self.search.SetDescriptiveText(_("Search")) self.search.ShowCancelButton(True) # load data in different thread self.thread = gThread() self.optionBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Options")) task = gtask.parse_interface("g.extension") ignoreFlags = ["l", "c", "g", "a", "f", "t", "help", "quiet"] if sys.platform == "win32": ignoreFlags.append("d") ignoreFlags.append("i") for f in task.get_options()["flags"]: name = f.get("name", "") desc = f.get("label", "") if not desc: desc = f.get("description", "") if not name and not desc: continue if name in ignoreFlags: continue self.options[name] = wx.CheckBox(parent=self.panel, id=wx.ID_ANY, label=desc) # defaultUrl = '' # default/official one will be used when option empty # self.repo.SetValue( # task.get_param( # value='url').get( # 'default', # defaultUrl)) self.statusbar = self.CreateStatusBar(number=1) # self.btnFetch = Button(parent=self.panel, id=wx.ID_ANY, # label=_("&Fetch")) # self.btnFetch.SetToolTip(_("Fetch list of available modules " # "from GRASS Addons repository")) self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE) self.btnInstall = Button(parent=self.panel, id=wx.ID_ANY, label=_("&Install")) self.btnInstall.SetToolTip(_("Install selected add-ons GRASS module")) self.btnInstall.Enable(False) self.btnHelp = Button(parent=self.panel, id=wx.ID_HELP) self.btnHelp.SetToolTip(_("Show g.extension manual page")) self.btnClose.Bind(wx.EVT_BUTTON, lambda evt: self.Close()) # self.btnFetch.Bind(wx.EVT_BUTTON, self.OnFetch) self.btnInstall.Bind(wx.EVT_BUTTON, self.OnInstall) self.btnHelp.Bind(wx.EVT_BUTTON, self.OnHelp) self.search.Bind(wx.EVT_TEXT, lambda evt: self.Filter(evt.GetString())) self.search.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.Filter("")) self.tree.selectionChanged.connect(self.OnItemSelected) self.tree.itemActivated.connect(self.OnItemActivated) self.tree.contextMenu.connect(self.OnContextMenu) wx.CallAfter(self._fetch) self._layout() def _layout(self): """Do layout""" sizer = wx.BoxSizer(wx.VERTICAL) # repoSizer = wx.StaticBoxSizer(self.repoBox, wx.VERTICAL) # repo1Sizer = wx.BoxSizer(wx.HORIZONTAL) # repo1Sizer.Add(self.repo, proportion=1, # flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=1) # repo1Sizer.Add(self.btnFetch, proportion=0, # flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=1) # repoSizer.Add(repo1Sizer, # flag=wx.EXPAND) sizer.Add(self.search, proportion=0, flag=wx.EXPAND | wx.ALL, border=3) treeSizer = wx.StaticBoxSizer(self.treeBox, wx.HORIZONTAL) treeSizer.Add(self.tree, proportion=1, flag=wx.ALL | wx.EXPAND, border=1) # options optionSizer = wx.StaticBoxSizer(self.optionBox, wx.VERTICAL) for key in self.options.keys(): optionSizer.Add(self.options[key], proportion=0) btnSizer = wx.BoxSizer(wx.HORIZONTAL) btnSizer.Add(self.btnHelp, proportion=0) btnSizer.AddStretchSpacer() btnSizer.Add(self.btnClose, proportion=0, flag=wx.RIGHT, border=5) btnSizer.Add(self.btnInstall, proportion=0) # sizer.Add(repoSizer, proportion=0, # flag=wx.ALL | wx.EXPAND, border=3) sizer.Add( treeSizer, proportion=1, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3, ) sizer.Add( optionSizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3, ) sizer.Add(btnSizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=5) self.panel.SetSizer(sizer) sizer.Fit(self.panel) self.Layout() def _getCmd(self): item = self.tree.GetSelected() if not item or "command" not in item[0].data: GError(_("Extension not defined"), parent=self) return name = item[0].data["command"] flags = list() for key in self.options.keys(): if self.options[key].IsChecked(): if len(key) == 1: flags.append("-%s" % key) else: flags.append("--%s" % key) # 'url=' + self.repo.GetValue().strip()] return ["g.extension"] + flags + ["extension={}".format(name)] def OnFetch(self, event): """Fetch list of available extensions""" self._fetch() def _fetch(self): """Fetch list of available extensions""" wx.BeginBusyCursor() self.SetStatusText( _("Fetching list of modules from GRASS-Addons (be patient)..."), 0) try: self.thread.Run( callable=self.modelBuilder.Load, url="", # self.repo.GetValue().strip(), ondone=lambda event: self._fetchDone(), ) except GException as error: self._fetchDone() GError(str(error), parent=self, showTraceback=False) def _fetchDone(self): self.tree.RefreshItems() nitems = len(self.modelBuilder.GetModel().SearchNodes(key="command", value="*")) self.SetStatusText(_("%d extensions loaded") % nitems, 0) wx.EndBusyCursor() def Filter(self, text): model = self.modelBuilder.GetModel() if text: model = model.Filtered(key=["command", "keywords", "description"], value=text) self.tree.SetModel(model) self.tree.ExpandAll() else: self.tree.SetModel(model) def OnContextMenu(self, node): if not hasattr(self, "popupID"): self.popupID = dict() for key in ("install", "help"): self.popupID[key] = NewId() data = node.data if data and "command" in data: self.popupMenu = Menu() self.popupMenu.Append(self.popupID["install"], _("Install")) self.Bind(wx.EVT_MENU, self.OnInstall, id=self.popupID["install"]) self.popupMenu.AppendSeparator() self.popupMenu.Append(self.popupID["help"], _("Show manual page")) self.Bind(wx.EVT_MENU, self.OnItemHelp, id=self.popupID["help"]) self.PopupMenu(self.popupMenu) self.popupMenu.Destroy() def OnItemActivated(self, node): data = node.data if data and "command" in data: self.OnInstall(event=None) def OnInstall(self, event): """Install selected extension""" log = self.parent.GetLogWindow() cmd = self._getCmd() if cmd: log.RunCmd(cmd, onDone=self.OnDone) def OnDone(self, event): if event.returncode == 0: if not os.getenv("GRASS_ADDON_BASE"): SetAddOnPath(key="BASE") globalvar.UpdateGRASSAddOnCommands() toolboxesOutdated() def OnItemHelp(self, event): item = self.tree.GetSelected() if not item or "command" not in item[0].data: return self._giface.Help(entry=item[0].data["command"], online=True) def OnHelp(self, event): self._giface.Help(entry="g.extension") def OnItemSelected(self, node): """Item selected""" data = node.data if data is None: self.SetStatusText("", 0) self.btnInstall.Enable(False) else: self.SetStatusText(data.get("description", ""), 0) self.btnInstall.Enable(True)
class InstallExtensionWindow(wx.Frame): def __init__(self, parent, giface, id=wx.ID_ANY, title=_("Fetch & install extension from GRASS Addons"), **kwargs): self.parent = parent self._giface = giface self.options = dict() # list of options wx.Frame.__init__(self, parent=parent, id=id, title=title, **kwargs) self.SetIcon( wx.Icon(os.path.join(globalvar.ICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO)) self.panel = wx.Panel(parent=self, id=wx.ID_ANY) # self.repoBox = StaticBox( # parent=self.panel, id=wx.ID_ANY, label=" %s " % # _("Repository (leave empty to use the official one)")) self.treeBox = StaticBox( parent=self.panel, id=wx.ID_ANY, label=" %s " % _("List of extensions - double-click to install")) # self.repo = TextCtrl(parent=self.panel, id=wx.ID_ANY) # modelBuilder loads data into tree model self.modelBuilder = ExtensionTreeModelBuilder() # tree view displays model data self.tree = CTreeView(parent=self.panel, model=self.modelBuilder.GetModel()) self.search = SearchModuleWidget(parent=self.panel, model=self.modelBuilder.GetModel(), showChoice=False) self.search.showSearchResult.connect( lambda result: self.tree.Select(result)) # show text in statusbar when notification appears self.search.showNotification.connect( lambda message: self.SetStatusText(message)) # load data in different thread self.thread = gThread() self.optionBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Options")) task = gtask.parse_interface('g.extension') ignoreFlags = ['l', 'c', 'g', 'a', 'f', 't', 'help', 'quiet'] if sys.platform == 'win32': ignoreFlags.append('d') ignoreFlags.append('i') for f in task.get_options()['flags']: name = f.get('name', '') desc = f.get('label', '') if not desc: desc = f.get('description', '') if not name and not desc: continue if name in ignoreFlags: continue self.options[name] = wx.CheckBox(parent=self.panel, id=wx.ID_ANY, label=desc) # defaultUrl = '' # default/official one will be used when option empty # self.repo.SetValue( # task.get_param( # value='url').get( # 'default', # defaultUrl)) self.statusbar = self.CreateStatusBar(number=1) # self.btnFetch = Button(parent=self.panel, id=wx.ID_ANY, # label=_("&Fetch")) # self.btnFetch.SetToolTip(_("Fetch list of available modules " # "from GRASS Addons repository")) self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE) self.btnInstall = Button(parent=self.panel, id=wx.ID_ANY, label=_("&Install")) self.btnInstall.SetToolTip(_("Install selected add-ons GRASS module")) self.btnInstall.Enable(False) self.btnHelp = Button(parent=self.panel, id=wx.ID_HELP) self.btnHelp.SetToolTip(_("Show g.extension manual page")) self.btnClose.Bind(wx.EVT_BUTTON, lambda evt: self.Close()) # self.btnFetch.Bind(wx.EVT_BUTTON, self.OnFetch) self.btnInstall.Bind(wx.EVT_BUTTON, self.OnInstall) self.btnHelp.Bind(wx.EVT_BUTTON, self.OnHelp) self.tree.selectionChanged.connect(self.OnItemSelected) self.tree.itemActivated.connect(self.OnItemActivated) self.tree.contextMenu.connect(self.OnContextMenu) wx.CallAfter(self._fetch) self._layout() def _layout(self): """Do layout""" sizer = wx.BoxSizer(wx.VERTICAL) # repoSizer = wx.StaticBoxSizer(self.repoBox, wx.VERTICAL) # repo1Sizer = wx.BoxSizer(wx.HORIZONTAL) # repo1Sizer.Add(self.repo, proportion=1, # flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=1) # repo1Sizer.Add(self.btnFetch, proportion=0, # flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=1) # repoSizer.Add(repo1Sizer, # flag=wx.EXPAND) findSizer = wx.BoxSizer(wx.HORIZONTAL) findSizer.Add(self.search, proportion=1) treeSizer = wx.StaticBoxSizer(self.treeBox, wx.HORIZONTAL) treeSizer.Add(self.tree, proportion=1, flag=wx.ALL | wx.EXPAND, border=1) # options optionSizer = wx.StaticBoxSizer(self.optionBox, wx.VERTICAL) for key in self.options.keys(): optionSizer.Add(self.options[key], proportion=0) btnSizer = wx.BoxSizer(wx.HORIZONTAL) btnSizer.Add(self.btnHelp, proportion=0) btnSizer.AddStretchSpacer() btnSizer.Add(self.btnClose, proportion=0, flag=wx.RIGHT, border=5) btnSizer.Add(self.btnInstall, proportion=0) # sizer.Add(repoSizer, proportion=0, # flag=wx.ALL | wx.EXPAND, border=3) sizer.Add(findSizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=3) sizer.Add(treeSizer, proportion=1, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3) sizer.Add(optionSizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, border=3) sizer.Add(btnSizer, proportion=0, flag=wx.ALIGN_RIGHT | wx.ALL | wx.EXPAND, border=5) self.panel.SetSizer(sizer) sizer.Fit(self.panel) self.Layout() def _getCmd(self): item = self.tree.GetSelected() if not item or 'command' not in item[0].data: GError(_("Extension not defined"), parent=self) return name = item[0].data['command'] flags = list() for key in self.options.keys(): if self.options[key].IsChecked(): if len(key) == 1: flags.append('-%s' % key) else: flags.append('--%s' % key) # 'url=' + self.repo.GetValue().strip()] return ['g.extension'] + flags + ['extension={}'.format(name)] def OnFetch(self, event): """Fetch list of available extensions""" self._fetch() def _fetch(self): """Fetch list of available extensions""" wx.BeginBusyCursor() self.SetStatusText( _("Fetching list of modules from GRASS-Addons (be patient)..."), 0) try: self.thread.Run( callable=self.modelBuilder.Load, url='', # self.repo.GetValue().strip(), ondone=lambda event: self._fetchDone()) except GException as e: self._fetchDone() GError(unicode(e), parent=self, showTraceback=False) def _fetchDone(self): self.tree.RefreshItems() nitems = len(self.modelBuilder.GetModel().SearchNodes(key='command', value='*')) self.SetStatusText(_("%d extensions loaded") % nitems, 0) wx.EndBusyCursor() def OnContextMenu(self, node): if not hasattr(self, "popupID"): self.popupID = dict() for key in ('install', 'help'): self.popupID[key] = NewId() data = node.data if data and 'command' in data: self.popupMenu = Menu() self.popupMenu.Append(self.popupID['install'], text=_("Install")) self.Bind(wx.EVT_MENU, self.OnInstall, id=self.popupID['install']) self.popupMenu.AppendSeparator() self.popupMenu.Append(self.popupID['help'], text=_("Show manual page")) self.Bind(wx.EVT_MENU, self.OnItemHelp, id=self.popupID['help']) self.PopupMenu(self.popupMenu) self.popupMenu.Destroy() def OnItemActivated(self, node): data = node.data if data and 'command' in data: self.OnInstall(event=None) def OnInstall(self, event): """Install selected extension""" log = self.parent.GetLogWindow() cmd = self._getCmd() if cmd: log.RunCmd(cmd, onDone=self.OnDone) def OnDone(self, event): if event.returncode == 0: if not os.getenv('GRASS_ADDON_BASE'): SetAddOnPath(key='BASE') globalvar.UpdateGRASSAddOnCommands() toolboxesOutdated() def OnItemHelp(self, event): item = self.tree.GetSelected() if not item or 'command' not in item[0].data: return self._giface.Help(entry=item[0].data['command'], online=True) def OnHelp(self, event): self._giface.Help(entry='g.extension') def OnItemSelected(self, node): """Item selected""" data = node.data if data is None: self.SetStatusText('', 0) self.btnInstall.Enable(False) else: self.SetStatusText(data.get('description', ''), 0) self.btnInstall.Enable(True)
def OnCategoryRightUp(self, event): """Show context menu on right click""" item, flags = self.HitTest((event.GetX(), event.GetY())) if item != wx.NOT_FOUND and flags & wx.LIST_HITTEST_ONITEM: self.rightClickedItemIdx = item # generate popup-menu cat_idx = self.rightClickedItemIdx cats = self.cats_mgr.GetCategories() cat_id = cats[cat_idx] showed = self.cats_mgr.GetCategoryAttrs(cat_id)['show'] menu = Menu() item = menu.Append(wx.ID_ANY, _("Rename class")) self.Bind(wx.EVT_MENU, self.OnRename, item) item = menu.Append(wx.ID_ANY, _("Set color")) self.Bind(wx.EVT_MENU, self.OnSetColor, item) item = menu.Append(item_id, _("Change opacity level")) self.Bind(wx.EVT_MENU, self.OnPopupOpacityLevel, item) if showed: text = _("Hide") else: text = _("Show") item = menu.Append(wx.ID_ANY, text) self.Bind( wx.EVT_MENU, lambda event: self._setCatAttrs(cat_id=cat_id, attrs={'show': not showed}), item) menu.AppendSeparator() item = menu.Append(wx.ID_ANY, _("Move to top")) self.Bind(wx.EVT_MENU, self.OnMoveTop, item) if cat_idx == 0: menu.Enable(item.GetId(), False) item = menu.Append(wx.ID_ANY, _("Move to bottom")) self.Bind(wx.EVT_MENU, self.OnMoveBottom, item) if cat_idx == len(cats) - 1: menu.Enable(item.GetId(), False) menu.AppendSeparator() item = menu.Append(wx.ID_ANY, _("Move category up")) self.Bind(wx.EVT_MENU, self.OnMoveUp, item) if cat_idx == 0: menu.Enable(item.GetId(), False) item = menu.Append(wx.ID_ANY, _("Move category down")) self.Bind(wx.EVT_MENU, self.OnMoveDown, item) if cat_idx == len(cats) - 1: menu.Enable(item.GetId(), False) menu.AppendSeparator() item = menu.Append(wx.ID_ANY, _("Export class raster")) self.Bind(wx.EVT_MENU, self.OnExportCatRast, item) self.PopupMenu(menu) menu.Destroy()
def OnImportMenu(self, event): """Create popup menu for other import options""" # create submenu subMenu = Menu() subitem = wx.MenuItem(subMenu, wx.ID_ANY, _("Link external raster data [r.external]")) subMenu.AppendItem(subitem) self.Bind(wx.EVT_MENU, self.OnLinkGdalLayers, subitem) subitem = wx.MenuItem(subMenu, wx.ID_ANY, _("Link external vector data [v.external]")) subMenu.AppendItem(subitem) self.Bind(wx.EVT_MENU, self.OnLinkOgrLayers, subitem) subMenu.AppendSeparator() subitem = wx.MenuItem(subMenu, wx.ID_ANY, _("Set raster output format [r.external.out]")) subMenu.AppendItem(subitem) self.Bind(wx.EVT_MENU, self.OnRasterOutputFormat, subitem) subitem = wx.MenuItem(subMenu, wx.ID_ANY, _("Set vector output format [v.external.out]")) subMenu.AppendItem(subitem) self.Bind(wx.EVT_MENU, self.OnVectorOutputFormat, subitem) # create menu menu = Menu() item = wx.MenuItem(menu, wx.ID_ANY, _("Unpack GRASS raster map [r.unpack]")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, lambda evt: self.GuiParseCommand("r.unpack"), item) item = wx.MenuItem(menu, wx.ID_ANY, _("Unpack GRASS vector map [v.unpack]")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, lambda evt: self.GuiParseCommand("v.unpack"), item) menu.AppendSeparator() item = wx.MenuItem(menu, wx.ID_ANY, _("Create raster map from x,y,z data [r.in.xyz]")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, lambda evt: self.GuiParseCommand("r.in.xyz"), item) item = wx.MenuItem( menu, wx.ID_ANY, _("Create vector map from x,y,z data [v.in.ascii]")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, lambda evt: self.GuiParseCommand("v.in.ascii"), item) menu.AppendSeparator() menu.AppendMenu(wx.ID_ANY, _("Link external data"), subMenu) menu.AppendSeparator() item = wx.MenuItem(menu, wx.ID_ANY, _("More options...")) menu.AppendItem(item) self.Bind(wx.EVT_MENU, self.OnMoreOptions, item) self.PopupMenu(menu) menu.Destroy()