def __init__(self, parent, handlerObj, giface, model, id=wx.ID_ANY, **kwargs): self.parent = parent self._handlerObj = handlerObj self._giface = giface self.showNotification = Signal('SearchModuleWindow.showNotification') wx.Panel.__init__(self, parent=parent, id=id, **kwargs) # tree self._tree = CTreeView(model=model, parent=self) self._tree.SetToolTip( _("Double-click or Ctrl-Enter to run selected module")) # self._dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY, # label = " %s " % _("Module tree")) # search widget self._search = SearchModuleWidget(parent=self, model=model, showChoice=False) self._search.showSearchResult.connect( lambda result: self._tree.Select(result)) self._search.showNotification.connect(self.showNotification) self._helpText = StaticText( parent=self, id=wx.ID_ANY, label="Press Enter for next match, Ctrl+Enter to run command") self._helpText.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_GRAYTEXT)) # buttons self._btnRun = Button(self, id=wx.ID_OK, label=_("&Run")) self._btnRun.SetToolTip(_("Run selected module from the tree")) self._btnHelp = Button(self, id=wx.ID_ANY, label=_("H&elp")) self._btnHelp.SetToolTip( _("Show manual for selected module from the tree")) self._btnAdvancedSearch = Button(self, id=wx.ID_ANY, label=_("Adva&nced search...")) self._btnAdvancedSearch.SetToolTip( _("Do advanced search using %s module") % 'g.search.module') # bindings self._btnRun.Bind(wx.EVT_BUTTON, lambda evt: self.Run()) self._btnHelp.Bind(wx.EVT_BUTTON, lambda evt: self.Help()) self._btnAdvancedSearch.Bind(wx.EVT_BUTTON, lambda evt: self.AdvancedSearch()) self.Bind(wx.EVT_KEY_UP, self.OnKeyUp) self._tree.selectionChanged.connect(self.OnItemSelected) self._tree.itemActivated.connect(lambda node: self.Run(node)) self._layout() self._search.SetFocus()
def __init__(self, parent, model, id = wx.ID_ANY, **kwargs): self.parent = parent # LayerManager self.showNotification = Signal('SearchModuleWindow.showNotification') wx.Panel.__init__(self, parent = parent, id = id, **kwargs) # tree self._tree = CTreeView(model=model, parent=self) self._tree.SetToolTipString(_("Double-click or Ctrl-Enter to run selected module")) # self._dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY, # label = " %s " % _("Module tree")) # search widget self._search = SearchModuleWidget(parent=self, model=model, showChoice=False) self._search.showSearchResult.connect(lambda result: self._tree.Select(result)) self._search.showNotification.connect(self.showNotification) self._helpText = wx.StaticText(parent=self, id=wx.ID_ANY, label="Press Enter for next match, Ctrl+Enter to run command") self._helpText.SetForegroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_GRAYTEXT)) # buttons self._btnRun = wx.Button(self, id=wx.ID_OK, label=_("&Run")) self._btnRun.SetToolTipString(_("Run selected module from the tree")) # bindings self._btnRun.Bind(wx.EVT_BUTTON, lambda evt: self.Run()) self.Bind(wx.EVT_KEY_UP, self.OnKeyUp) self._tree.selectionChanged.connect(self.OnItemSelected) self._tree.itemActivated.connect(lambda node: self.Run(node)) self._layout() self._search.SetFocus()
def MakeSearchPaneContent(self, pane, model): """Create search pane""" border = wx.BoxSizer(wx.VERTICAL) self.search = SearchModuleWidget(parent=pane, model=model) self.search.showNotification.connect(self.showNotification) border.Add(self.search, proportion=0, flag=wx.EXPAND | wx.ALL, border=1) pane.SetSizer(border) border.Fit(pane)
def __init__(self, parent, title=_("Add GRASS command to the model"), style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs): """Graphical modeler module search window :param parent: parent window :param id: window id :param title: window title :param kwargs: wx.Dialogs' arguments """ self.parent = parent wx.Dialog.__init__( self, parent=parent, id=wx.ID_ANY, title=title, **kwargs) self.SetName("ModelerDialog") self.SetIcon( wx.Icon( os.path.join( globalvar.ICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO)) self._command = None self.panel = wx.Panel(parent=self, id=wx.ID_ANY) self.cmdBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Command")) self.labelBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Label and comment")) # menu data for search widget and prompt menuModel = LayerManagerMenuData() self.cmd_prompt = GPromptSTC( parent=self, menuModel=menuModel.GetModel()) self.cmd_prompt.promptRunCmd.connect(self.OnCommand) self.cmd_prompt.commandSelected.connect( lambda command: self.label.SetValue(command)) self.search = SearchModuleWidget(parent=self.panel, model=menuModel.GetModel(), showTip=True) self.search.moduleSelected.connect( lambda name: self.cmd_prompt.SetTextAndFocus(name + ' ')) wx.CallAfter(self.cmd_prompt.SetFocus) self.label = TextCtrl(parent=self.panel, id=wx.ID_ANY) self.comment = TextCtrl( parent=self.panel, id=wx.ID_ANY, style=wx.TE_MULTILINE) self.btnCancel = Button(self.panel, wx.ID_CANCEL) self.btnOk = Button(self.panel, wx.ID_OK) self.btnOk.SetDefault() self.Bind(wx.EVT_BUTTON, self.OnOk, self.btnOk) self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel) self._layout() self.SetSize((500, -1))
class ModelSearchDialog(wx.Dialog): def __init__(self, parent, title=_("Add GRASS command to the model"), style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs): """Graphical modeler module search window :param parent: parent window :param id: window id :param title: window title :param kwargs: wx.Dialogs' arguments """ self.parent = parent wx.Dialog.__init__( self, parent=parent, id=wx.ID_ANY, title=title, **kwargs) self.SetName("ModelerDialog") self.SetIcon( wx.Icon( os.path.join( globalvar.ICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO)) self._command = None self.panel = wx.Panel(parent=self, id=wx.ID_ANY) self.cmdBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Command")) self.labelBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Label and comment")) # menu data for search widget and prompt menuModel = LayerManagerMenuData() self.cmd_prompt = GPromptSTC( parent=self, menuModel=menuModel.GetModel()) self.cmd_prompt.promptRunCmd.connect(self.OnCommand) self.cmd_prompt.commandSelected.connect( lambda command: self.label.SetValue(command)) self.search = SearchModuleWidget(parent=self.panel, model=menuModel.GetModel(), showTip=True) self.search.moduleSelected.connect( lambda name: self.cmd_prompt.SetTextAndFocus(name + ' ')) wx.CallAfter(self.cmd_prompt.SetFocus) self.label = TextCtrl(parent=self.panel, id=wx.ID_ANY) self.comment = TextCtrl( parent=self.panel, id=wx.ID_ANY, style=wx.TE_MULTILINE) self.btnCancel = Button(self.panel, wx.ID_CANCEL) self.btnOk = Button(self.panel, wx.ID_OK) self.btnOk.SetDefault() self.Bind(wx.EVT_BUTTON, self.OnOk, self.btnOk) self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel) self._layout() self.SetSize((500, -1)) def _layout(self): cmdSizer = wx.StaticBoxSizer(self.cmdBox, wx.VERTICAL) cmdSizer.Add(self.cmd_prompt, proportion=1, flag=wx.EXPAND) labelSizer = wx.StaticBoxSizer(self.labelBox, wx.VERTICAL) gridSizer = wx.GridBagSizer(hgap=5, vgap=5) gridSizer.Add(StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Label:")), flag=wx.ALIGN_CENTER_VERTICAL, pos=(0, 0)) gridSizer.Add(self.label, pos=(0, 1), flag=wx.EXPAND) gridSizer.Add(StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Comment:")), flag=wx.ALIGN_CENTER_VERTICAL, pos=(1, 0)) gridSizer.Add(self.comment, pos=(1, 1), flag=wx.EXPAND) gridSizer.AddGrowableRow(1) gridSizer.AddGrowableCol(1) labelSizer.Add(gridSizer, proportion=1, flag=wx.EXPAND) btnSizer = wx.StdDialogButtonSizer() btnSizer.AddButton(self.btnCancel) btnSizer.AddButton(self.btnOk) btnSizer.Realize() mainSizer = wx.BoxSizer(wx.VERTICAL) mainSizer.Add(self.search, proportion=0, flag=wx.EXPAND | wx.ALL, border=3) mainSizer.Add(cmdSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, border=3) mainSizer.Add(labelSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, border=3) mainSizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5) self.panel.SetSizer(mainSizer) mainSizer.Fit(self) self.Layout() def GetPanel(self): """Get dialog panel""" return self.panel def _getCmd(self): line = self.cmd_prompt.GetCurLine()[0].strip() if len(line) == 0: cmd = list() else: try: cmd = utils.split(str(line)) except UnicodeError: cmd = utils.split(EncodeString((line))) return cmd def GetCmd(self): """Get command""" return self._command def GetLabel(self): """Get label and comment""" return self.label.GetValue(), self.comment.GetValue() def ValidateCmd(self, cmd): if len(cmd) < 1: GError(parent=self, message=_("Command not defined.\n\n" "Unable to add new action to the model.")) return False if cmd[0] not in globalvar.grassCmd: GError( parent=self, message=_( "'%s' is not a GRASS module.\n\n" "Unable to add new action to the model.") % cmd[0]) return False return True def OnCommand(self, cmd): """Command in prompt confirmed""" if self.ValidateCmd(cmd): self._command = cmd self.EndModal(wx.ID_OK) def OnOk(self, event): """Button 'OK' pressed""" cmd = self._getCmd() if self.ValidateCmd(cmd): self._command = cmd self.EndModal(wx.ID_OK) def OnCancel(self, event): """Cancel pressed, close window""" self.Hide() def Reset(self): """Reset dialog""" self.search.Reset() self.label.SetValue('') self.comment.SetValue('') self.cmd_prompt.OnCmdErase(None) self.cmd_prompt.SetFocus()
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 = wx.StaticBox( parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Repository (leave empty to use the official one)")) self.treeBox = wx.StaticBox( parent=self.panel, id=wx.ID_ANY, label=" %s " % _("List of extensions - double-click to install")) self.repo = wx.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 = wx.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 = wx.Button(parent=self.panel, id=wx.ID_ANY, label=_("&Fetch")) self.btnFetch.SetToolTipString( _("Fetch list of available modules " "from GRASS Addons SVN repository")) self.btnClose = wx.Button(parent=self.panel, id=wx.ID_CLOSE) self.btnInstall = wx.Button(parent=self.panel, id=wx.ID_ANY, label=_("&Install")) self.btnInstall.SetToolTipString( _("Install selected add-ons GRASS module")) self.btnInstall.Enable(False) self.btnHelp = wx.Button(parent=self.panel, id=wx.ID_HELP) self.btnHelp.SetToolTipString(_("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()
class SearchModuleWindow(wx.Panel): """Menu tree and search widget for searching modules. Signal: showNotification - attribute 'message' """ def __init__(self, parent, handlerObj, giface, model, id=wx.ID_ANY, **kwargs): self.parent = parent self._handlerObj = handlerObj self._giface = giface self.showNotification = Signal('SearchModuleWindow.showNotification') wx.Panel.__init__(self, parent=parent, id=id, **kwargs) # tree self._tree = CTreeView(model=model, parent=self) self._tree.SetToolTip( _("Double-click or Ctrl-Enter to run selected module")) # self._dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY, # label = " %s " % _("Module tree")) # search widget self._search = SearchModuleWidget(parent=self, model=model, showChoice=False) self._search.showSearchResult.connect( lambda result: self._tree.Select(result)) self._search.showNotification.connect(self.showNotification) self._helpText = StaticText( parent=self, id=wx.ID_ANY, label="Press Enter for next match, Ctrl+Enter to run command") self._helpText.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_GRAYTEXT)) # buttons self._btnRun = Button(self, id=wx.ID_OK, label=_("&Run")) self._btnRun.SetToolTip(_("Run selected module from the tree")) self._btnHelp = Button(self, id=wx.ID_ANY, label=_("H&elp")) self._btnHelp.SetToolTip( _("Show manual for selected module from the tree")) self._btnAdvancedSearch = Button(self, id=wx.ID_ANY, label=_("Adva&nced search...")) self._btnAdvancedSearch.SetToolTip( _("Do advanced search using %s module") % 'g.search.module') # bindings self._btnRun.Bind(wx.EVT_BUTTON, lambda evt: self.Run()) self._btnHelp.Bind(wx.EVT_BUTTON, lambda evt: self.Help()) self._btnAdvancedSearch.Bind(wx.EVT_BUTTON, lambda evt: self.AdvancedSearch()) self.Bind(wx.EVT_KEY_UP, self.OnKeyUp) self._tree.selectionChanged.connect(self.OnItemSelected) self._tree.itemActivated.connect(lambda node: self.Run(node)) self._layout() self._search.SetFocus() def _layout(self): """Do dialog layout""" sizer = wx.BoxSizer(wx.VERTICAL) # body dataSizer = wx.BoxSizer(wx.HORIZONTAL) dataSizer.Add(self._tree, proportion=1, flag=wx.EXPAND) # buttons btnSizer = wx.BoxSizer(wx.HORIZONTAL) btnSizer.Add(self._btnAdvancedSearch, proportion=0) btnSizer.AddStretchSpacer() btnSizer.Add(self._btnHelp, proportion=0) btnSizer.Add(self._btnRun, proportion=0) sizer.Add(dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5) sizer.Add(self._search, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5) sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5) sizer.Add(self._helpText, proportion=0, flag=wx.EXPAND | wx.LEFT, border=5) sizer.Fit(self) sizer.SetSizeHints(self) self.SetSizer(sizer) self.Fit() self.SetAutoLayout(True) self.Layout() def _GetSelectedNode(self): selection = self._tree.GetSelected() if not selection: return None return selection[0] def Run(self, node=None): """Run selected command. :param node: a tree node associated with the module or other item """ if not node: node = self._GetSelectedNode() # nothing selected if not node: return data = node.data # non-leaf nodes if not data: return # extract name of the handler and create a new call handler = 'self._handlerObj.' + data['handler'].lstrip('self.') if data['command']: eval(handler)(event=None, cmd=data['command'].split()) else: eval(handler)(event=None) def Help(self, node=None): """Show documentation for a module""" if not node: node = self._GetSelectedNode() # nothing selected if not node: return data = node.data # non-leaf nodes if not data: return if not data['command']: # showing nothing for non-modules return # strip parameters from command if present name = data['command'].split()[0] self._giface.Help(name) self.showNotification.emit( message=_("Documentation for %s is now open in the web browser") % name) def AdvancedSearch(self): """Show advanced search window""" self._handlerObj.RunMenuCmd(cmd=['g.search.modules']) def OnKeyUp(self, event): """Key or key combination pressed""" if event.ControlDown() and \ event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER): self.Run() def OnItemSelected(self, node): """Item selected""" data = node.data if not data or 'command' not in data: return if data['command']: label = data['command'] if data['description']: label += ' -- ' + data['description'] else: label = data['description'] self.showNotification.emit(message=label)
class SearchModuleWindow(wx.Panel): """Menu tree and search widget for searching modules. Signal: showNotification - attribute 'message' """ def __init__(self, parent, handlerObj, model, id=wx.ID_ANY, **kwargs): self.parent = parent self.handlerObj = handlerObj self.showNotification = Signal('SearchModuleWindow.showNotification') wx.Panel.__init__(self, parent=parent, id=id, **kwargs) # tree self._tree = CTreeView(model=model, parent=self) self._tree.SetToolTipString( _("Double-click or Ctrl-Enter to run selected module")) # self._dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY, # label = " %s " % _("Module tree")) # search widget self._search = SearchModuleWidget(parent=self, model=model, showChoice=False) self._search.showSearchResult.connect( lambda result: self._tree.Select(result)) self._search.showNotification.connect(self.showNotification) self._helpText = wx.StaticText( parent=self, id=wx.ID_ANY, label="Press Enter for next match, Ctrl+Enter to run command") self._helpText.SetForegroundColour( wx.SystemSettings_GetColour(wx.SYS_COLOUR_GRAYTEXT)) # buttons self._btnRun = wx.Button(self, id=wx.ID_OK, label=_("&Run")) self._btnRun.SetToolTipString(_("Run selected module from the tree")) # bindings self._btnRun.Bind(wx.EVT_BUTTON, lambda evt: self.Run()) self.Bind(wx.EVT_KEY_UP, self.OnKeyUp) self._tree.selectionChanged.connect(self.OnItemSelected) self._tree.itemActivated.connect(lambda node: self.Run(node)) self._layout() self._search.SetFocus() def _layout(self): """Do dialog layout""" sizer = wx.BoxSizer(wx.VERTICAL) # body dataSizer = wx.BoxSizer(wx.HORIZONTAL) dataSizer.Add(item=self._tree, proportion=1, flag=wx.EXPAND) # buttons btnSizer = wx.BoxSizer(wx.HORIZONTAL) btnSizer.Add(item=self._btnRun, proportion=0) sizer.Add(item=dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5) sizer.Add(item=self._search, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5) sizer.Add(item=btnSizer, proportion=0, flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, border=5) sizer.Add(item=self._helpText, proportion=0, flag=wx.EXPAND | wx.LEFT, border=5) sizer.Fit(self) sizer.SetSizeHints(self) self.SetSizer(sizer) self.Fit() self.SetAutoLayout(True) self.Layout() def Run(self, module=None): """Run selected command. :param module: module (represented by tree node) """ if module is None: if not self._tree.GetSelected(): return module = self._tree.GetSelected()[0] data = module.data if not data: return handler = 'self.handlerObj.' + data['handler'].lstrip('self.') if data['command']: eval(handler)(event=None, cmd=data['command'].split()) else: eval(handler)(event=None) def OnKeyUp(self, event): """Key or key combination pressed""" if event.ControlDown() and \ event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER): self.Run() def OnItemSelected(self, node): """Item selected""" data = node.data if not data or 'command' not in data: return if data['command']: label = data['command'] if data['description']: label += ' -- ' + data['description'] else: label = data['description'] self.showNotification.emit(message=label)