extensionservice.py
来自「Wxpython Implemented on Windows CE, Sou」· Python 代码 · 共 445 行 · 第 1/2 页
PY
445 行
self.Bind(wx.EVT_BUTTON, self.OnMoveUp, self._moveUpButton)
buttonSizer.Add(self._moveUpButton, 1, wx.EXPAND)
self._moveDownButton = wx.Button(self, -1, _("Move Down"))
self.Bind(wx.EVT_BUTTON, self.OnMoveDown, self._moveDownButton)
buttonSizer.Add(self._moveDownButton, 1, wx.EXPAND)
self._addButton = wx.Button(self, wx.ID_ADD)
self.Bind(wx.EVT_BUTTON, self.OnAdd, self._addButton)
buttonSizer.Add(self._addButton, 1, wx.EXPAND)
self._deleteButton = wx.Button(self, wx.ID_DELETE, label=_("Delete")) # get rid of accelerator for letter d in "&Delete"
self.Bind(wx.EVT_BUTTON, self.OnDelete, self._deleteButton)
buttonSizer.Add(self._deleteButton, 1, wx.EXPAND)
extCtrlSizer.Add(buttonSizer, 0, wx.ALIGN_CENTER)
extOptionsPanelSizer.Add(extCtrlSizer, 0, wx.EXPAND)
self._extDetailPanel = wx.Panel(self)
staticBox = wx.StaticBox(self, label=_("Selected External Tool"))
staticBoxSizer = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
extDetailSizer = wx.FlexGridSizer(cols=2, vgap=5, hgap=5)
extDetailSizer.AddGrowableCol(1,1)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("Menu Item Name:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._menuItemNameTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
extDetailSizer.Add(self._menuItemNameTextCtrl, 0, wx.EXPAND)
self.Bind(wx.EVT_TEXT, self.SaveCurrentItem, self._menuItemNameTextCtrl)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("Menu Item Description:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._menuItemDescTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
extDetailSizer.Add(self._menuItemDescTextCtrl, 0, wx.EXPAND)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("Command Path:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._commandTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
findFileButton = wx.Button(self._extDetailPanel, -1, _("Browse..."))
def OnBrowseButton(event):
fileDlg = wx.FileDialog(self, _("Choose an Executable:"), style=wx.OPEN|wx.FILE_MUST_EXIST|wx.HIDE_READONLY|wx.CHANGE_DIR)
path = self._commandTextCtrl.GetValue()
if path:
fileDlg.SetPath(path)
# fileDlg.CenterOnParent() # wxBug: caused crash with wx.FileDialog
if fileDlg.ShowModal() == wx.ID_OK:
self._commandTextCtrl.SetValue(fileDlg.GetPath())
self._commandTextCtrl.SetInsertionPointEnd()
self._commandTextCtrl.SetToolTipString(fileDlg.GetPath())
fileDlg.Destroy()
wx.EVT_BUTTON(findFileButton, -1, OnBrowseButton)
hsizer = wx.BoxSizer(wx.HORIZONTAL)
hsizer.Add(self._commandTextCtrl, 1, wx.EXPAND)
hsizer.Add(findFileButton, 0, wx.LEFT, HALF_SPACE)
extDetailSizer.Add(hsizer, 0, wx.EXPAND)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("Command Pre Args:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._commandPreArgsTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
extDetailSizer.Add(self._commandPreArgsTextCtrl, 0, wx.EXPAND)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("Command Post Args:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._commandPostArgsTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
extDetailSizer.Add(self._commandPostArgsTextCtrl, 0, wx.EXPAND)
extDetailSizer.Add(wx.StaticText(self._extDetailPanel, -1, _("File Extensions:")), flag=wx.ALIGN_CENTER_VERTICAL)
self._fileExtTextCtrl = wx.TextCtrl(self._extDetailPanel, -1, size = (-1, -1))
self._fileExtTextCtrl.SetToolTipString(_("""For example: "txt, text" (comma separated) or "*" for all files"""))
extDetailSizer.Add(self._fileExtTextCtrl, 0, wx.EXPAND)
self._selFileCtrl = wx.CheckBox(self._extDetailPanel, -1, _("Operate on Selected File"))
extDetailSizer.Add(self._selFileCtrl, 0, wx.ALIGN_CENTER_VERTICAL|wx.TOP, SPACE)
self._selFileCtrl.SetToolTipString(_("If focus is in the project, instead of operating on the project file, operate on the selected file."))
self._extDetailPanel.SetSizer(extDetailSizer)
staticBoxSizer.Add(self._extDetailPanel, 1, wx.ALL|wx.EXPAND, SPACE)
extOptionsPanelSizer.Add(staticBoxSizer, 1, wx.LEFT|wx.EXPAND, SPACE)
extOptionsPanelBorderSizer.Add(extOptionsPanelSizer, 1, wx.ALL|wx.EXPAND, SPACE)
self.SetSizer(extOptionsPanelBorderSizer)
if self.PopulateItems():
self._extListBox.SetSelection(0)
self.OnListBoxSelect()
self.Layout()
parent.AddPage(self, _("External Tools"))
def OnOK(self, optionsDialog):
self.SaveCurrentItem()
extensionsService = wx.GetApp().GetService(ExtensionService)
extensionsService.SetExtensions(self._extensions)
extensionsService.SaveExtensions()
if extensionsService.CheckSumExtensions() != self._oldExtensions: # see PopulateItems() note about self._oldExtensions
msgTitle = wx.GetApp().GetAppName()
if not msgTitle:
msgTitle = _("Document Options")
wx.MessageBox(_("Extension changes will not appear until the application is restarted."),
msgTitle,
wx.OK | wx.ICON_INFORMATION,
self.GetParent())
def PopulateItems(self):
extensionsService = wx.GetApp().GetService(ExtensionService)
import copy
self._extensions = copy.deepcopy(extensionsService.GetExtensions())
self._oldExtensions = extensionsService.CheckSumExtensions() # wxBug: need to make a copy now since the deepcopy reorders fields, so we must compare the prestine copy with the modified copy
for extension in self._extensions:
self._extListBox.Append(extension.menuItemName, extension)
self._currentItem = None
self._currentItemIndex = -1
return len(self._extensions)
def OnListBoxSelect(self, event=None):
self.SaveCurrentItem()
if self._extListBox.GetSelection() == wx.NOT_FOUND:
self._currentItemIndex = -1
self._currentItem = None
self._deleteButton.Enable(False)
self._moveUpButton.Enable(False)
self._moveDownButton.Enable(False)
else:
self._currentItemIndex = self._extListBox.GetSelection()
self._currentItem = self._extListBox.GetClientData(self._currentItemIndex)
self._deleteButton.Enable()
self._moveUpButton.Enable(self._extListBox.GetCount() > 1 and self._currentItemIndex > 0)
self._moveDownButton.Enable(self._extListBox.GetCount() > 1 and self._currentItemIndex < self._extListBox.GetCount() - 1)
self.LoadItem(self._currentItem)
def SaveCurrentItem(self, event=None):
extension = self._currentItem
if extension:
if extension.menuItemName != self._menuItemNameTextCtrl.GetValue():
extension.menuItemName = self._menuItemNameTextCtrl.GetValue()
self._extListBox.SetString(self._currentItemIndex, extension.menuItemName)
extension.menuItemDesc = self._menuItemDescTextCtrl.GetValue()
extension.command = self._commandTextCtrl.GetValue()
extension.commandPreArgs = self._commandPreArgsTextCtrl.GetValue()
extension.commandPostArgs = self._commandPostArgsTextCtrl.GetValue()
fileExt = self._fileExtTextCtrl.GetValue().replace(' ','')
if not fileExt:
extension.fileExt = None
else:
extension.fileExt = fileExt.split(',')
extension.opOnSelectedFile = self._selFileCtrl.GetValue()
def LoadItem(self, extension):
if extension:
self._menuItemDescTextCtrl.SetValue(extension.menuItemDesc or '')
self._commandTextCtrl.SetValue(extension.command or '')
self._commandTextCtrl.SetToolTipString(extension.command or '')
self._commandPreArgsTextCtrl.SetValue(extension.commandPreArgs or '')
self._commandPostArgsTextCtrl.SetValue(extension.commandPostArgs or '')
if extension.fileExt:
list = ""
for ext in extension.fileExt:
if list:
list = list + ", "
list = list + ext
self._fileExtTextCtrl.SetValue(list)
else:
self._fileExtTextCtrl.SetValue('')
self._selFileCtrl.SetValue(extension.opOnSelectedFile)
self._menuItemNameTextCtrl.SetValue(extension.menuItemName or '') # Do the name last since it triggers the write event that updates the entire item
self._extDetailPanel.Enable()
else:
self._menuItemNameTextCtrl.SetValue('')
self._menuItemDescTextCtrl.SetValue('')
self._commandTextCtrl.SetValue('')
self._commandTextCtrl.SetToolTipString(_("Path to executable"))
self._commandPreArgsTextCtrl.SetValue('')
self._commandPostArgsTextCtrl.SetValue('')
self._fileExtTextCtrl.SetValue('')
self._selFileCtrl.SetValue(True)
self._extDetailPanel.Enable(False)
def OnAdd(self, event):
self.SaveCurrentItem()
name = _("Untitled")
count = 1
while self._extListBox.FindString(name) != wx.NOT_FOUND:
count = count + 1
name = _("Untitled%s") % count
extension = Extension(name)
self._extensions.append(extension)
self._extListBox.Append(extension.menuItemName, extension)
self._extListBox.SetStringSelection(extension.menuItemName)
self.OnListBoxSelect()
self._menuItemNameTextCtrl.SetFocus()
self._menuItemNameTextCtrl.SetSelection(-1, -1)
def OnDelete(self, event):
self._extListBox.Delete(self._currentItemIndex)
self._extensions.remove(self._currentItem)
self._currentItemIndex = min(self._currentItemIndex, self._extListBox.GetCount() - 1)
if self._currentItemIndex > -1:
self._extListBox.SetSelection(self._currentItemIndex)
self._currentItem = None # Don't update it since it no longer exists
self.OnListBoxSelect()
def OnMoveUp(self, event):
itemAboveString = self._extListBox.GetString(self._currentItemIndex - 1)
itemAboveData = self._extListBox.GetClientData(self._currentItemIndex - 1)
self._extListBox.Delete(self._currentItemIndex - 1)
self._extListBox.Insert(itemAboveString, self._currentItemIndex)
self._extListBox.SetClientData(self._currentItemIndex, itemAboveData)
self._currentItemIndex = self._currentItemIndex - 1
self.OnListBoxSelect() # Reset buttons
def OnMoveDown(self, event):
itemBelowString = self._extListBox.GetString(self._currentItemIndex + 1)
itemBelowData = self._extListBox.GetClientData(self._currentItemIndex + 1)
self._extListBox.Delete(self._currentItemIndex + 1)
self._extListBox.Insert(itemBelowString, self._currentItemIndex)
self._extListBox.SetClientData(self._currentItemIndex, itemBelowData)
self._currentItemIndex = self._currentItemIndex + 1
self.OnListBoxSelect() # Reset buttons
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?