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()
class ModelSearchDialog(wx.Dialog): def __init__(self, parent, id=wx.ID_ANY, title=_("Add new GRASS module 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=id, title=title, **kwargs) self.SetName("ModelerDialog") self.SetIcon( wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO)) self.panel = wx.Panel(parent=self, id=wx.ID_ANY) self.cmdBox = wx.StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Command")) self.cmd_prompt = GPromptSTC(parent=self) self.search = SearchModuleWindow(parent=self.panel, cmdPrompt=self.cmd_prompt, showTip=True) wx.CallAfter(self.cmd_prompt.SetFocus) # get commands items = self.cmd_prompt.GetCommandItems() self.btnCancel = wx.Button(self.panel, wx.ID_CANCEL) self.btnOk = wx.Button(self.panel, wx.ID_OK) self.btnOk.SetDefault() self.btnOk.Enable(False) self.cmd_prompt.Bind(wx.EVT_KEY_UP, self.OnText) self.search.searchChoice.Bind(wx.EVT_CHOICE, self.OnText) self.Bind(wx.EVT_BUTTON, self.OnOk, self.btnOk) self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel) self._layout() self.SetSize((500, 275)) def _layout(self): cmdSizer = wx.StaticBoxSizer(self.cmdBox, wx.VERTICAL) cmdSizer.Add(item=self.cmd_prompt, 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(item=self.search, proportion=0, flag=wx.EXPAND | wx.ALL, border=3) mainSizer.Add(item=cmdSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, border=3) mainSizer.Add(item=btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5) self.panel.SetSizer(mainSizer) mainSizer.Fit(self.panel) self.Layout() def GetPanel(self): """!Get dialog panel""" return self.panel def GetCmd(self): """!Get command""" line = self.cmd_prompt.GetCurLine()[0].strip() if len(line) == 0: list() try: cmd = utils.split(str(line)) except UnicodeError: cmd = utils.split(utils.EncodeString((line))) return cmd def OnOk(self, event): """!Button 'OK' pressed""" # hide autocomplete if self.cmd_prompt.AutoCompActive(): self.cmd_prompt.AutoCompCancel() self.btnOk.SetFocus() cmd = self.GetCmd() if len(cmd) < 1: GError(parent=self, message=_("Command not defined.\n\n" "Unable to add new action to the model.")) return 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 self.EndModal(wx.ID_OK) def OnCancel(self, event): """Cancel pressed, close window""" # hide autocomplete if self.cmd_prompt.AutoCompActive(): self.cmd_prompt.AutoCompCancel() self.Hide() def OnText(self, event): """!Text in prompt changed""" if self.cmd_prompt.AutoCompActive(): event.Skip() return if isinstance(event, wx.KeyEvent): entry = self.cmd_prompt.GetTextLeft() elif isinstance(event, wx.stc.StyledTextEvent): entry = event.GetText() else: entry = event.GetString() if entry: self.btnOk.Enable() else: self.btnOk.Enable(False) event.Skip() def Reset(self): """!Reset dialog""" self.search.Reset() self.cmd_prompt.OnCmdErase(None) self.btnOk.Enable(False) self.cmd_prompt.SetFocus()