Beispiel #1
0
    def _createSnippetButtons(self, snippetButtonsSizer, parent):
        # Insert variable
        self.insertVariableBtn = PopupButton(parent,
                                             bitmap=wx.Bitmap(
                                                 os.path.join(
                                                     self._imagesPath,
                                                     "variables-menu.png")))
        self.insertVariableBtn.SetToolTipString(_(u"Insert variable"))

        for menuitem in self._varMenuItems:
            data = u'{{' + menuitem[1] + u'}}'
            title = u'{var} - {title}'.format(var=data, title=menuitem[0])
            self.insertVariableBtn.appendMenuItem(title, data)

        self.insertVariableBtn.appendMenuItem(_(u'Other variable...'), None)

        snippetButtonsSizer.Add(self.insertVariableBtn, flag=wx.ALL, border=0)

        # Insert block
        self.insertBlockBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "block-menu.png")))
        self.insertBlockBtn.SetToolTipString(_(u"Insert block"))

        for menuitem in self._blocksMenuItems:
            data = menuitem[1]
            title = menuitem[0]
            self.insertBlockBtn.appendMenuItem(title, data)

        snippetButtonsSizer.Add(self.insertBlockBtn, flag=wx.ALL, border=0)
Beispiel #2
0
    def _createSnippetButtons(self, snippetButtonsSizer, parent):
        # Insert variable
        self.insertVariableBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath,
                                          "variables-menu.png"))
        )
        self.insertVariableBtn.SetToolTip(_(u"Insert variable"))

        for menuitem in self._varMenuItems:
            data = u'{{' + menuitem[1] + u'}}'
            title = u'{var} - {title}'.format(var=data, title=menuitem[0])
            self.insertVariableBtn.appendMenuItem(title, data)

        self.insertVariableBtn.appendMenuItem(_(u'Other variable...'), None)

        snippetButtonsSizer.Add(self.insertVariableBtn, flag=wx.ALL, border=0)

        # Insert block
        self.insertBlockBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "block-menu.png"))
        )
        self.insertBlockBtn.SetToolTip(_(u"Insert block"))

        for menuitem in self._blocksMenuItems:
            data = menuitem[1]
            title = menuitem[0]
            self.insertBlockBtn.appendMenuItem(title, data)

        snippetButtonsSizer.Add(self.insertBlockBtn, flag=wx.ALL, border=0)
Beispiel #3
0
class EditSnippetsDialog(TestedDialog):
    '''
    Dialog to create, edit and remove snippets and folders.
    '''
    def __init__(self, parent):
        super(EditSnippetsDialog,
              self).__init__(parent,
                             style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
        global _
        _ = get_()

        self.ICON_WIDTH = 16
        self.ICON_HEIGHT = 16

        self._imagesPath = getImagesPath()
        self._dirImageId = None
        self._snippetImageId = None

        self.addGroupBtn = None
        self.addSnippetBtn = None

        self._varMenuItems = [
            (_(u'Selected text'), defines.VAR_SEL_TEXT),
            (_(u'Current date'), defines.VAR_DATE),
            (_(u'Page title'), defines.VAR_TITLE),
            (_(u'Page type'), defines.VAR_PAGE_TYPE),
            (_(u'Page tags list'), defines.VAR_TAGS),
            (_(u'Attachments path'), defines.VAR_ATTACH),
            (_(u'Path to page'), defines.VAR_FOLDER),
            (_(u'Relative page path'), defines.VAR_SUBPATH),
            (_(u'Page creation date'), defines.VAR_DATE_CREATING),
            (_(u'Page modification date'), defines.VAR_DATE_EDITIND),
            (_(u'Page Id'), defines.VAR_PAGE_ID),
            (_(u'Attachments list'), defines.VAR_ATTACHLIST),
            (_(u'Child pages'), defines.VAR_CHILDLIST),
        ]

        self._blocksMenuItems = [
            (_('{% if %}'), (u'{% if %}', '{% elif %}{% else %}{% endif %}')),
            (_('{% include %}'), (u"{% include '", u"' %}")),
            (_('{# comment #}'), (u'{# ', ' #}')),
        ]

        self._createGUI()
        self.SetTitle(_(u'Snippets management'))
        self.disableSnippetEditor()

    def disableSnippetEditor(self):
        self.snippetEditor.SetText(u'')
        self._snippetPanel.Disable()
        self.runSnippetBtn.Disable()

    def enableSnippetEditor(self):
        self._snippetPanel.Enable()
        self.runSnippetBtn.Enable()

    def appendDirTreeItem(self, parentItem, name, data):
        itemData = wx.TreeItemData(data)
        if parentItem is not None:
            newItemId = self.snippetsTree.AppendItem(parentItem,
                                                     name,
                                                     self._dirImageId,
                                                     data=itemData)
        else:
            newItemId = self.snippetsTree.AddRoot(name,
                                                  self._dirImageId,
                                                  data=itemData)
        return newItemId

    def appendSnippetTreeItem(self, parentItem, name, data):
        itemData = wx.TreeItemData(data)
        newItemId = self.snippetsTree.AppendItem(parentItem,
                                                 name,
                                                 self._snippetImageId,
                                                 data=itemData)
        return newItemId

    def _createTreeButtons(self, groupButtonsSizer):
        # Add a group button
        self.addGroupBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "folder_add.png")))
        self.addGroupBtn.SetToolTipString(_(u"Add new snippets group"))
        groupButtonsSizer.Add(self.addGroupBtn, flag=wx.ALL, border=0)

        # Add a snippet button
        self.addSnippetBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath,
                                          "snippet_add.png")))
        self.addSnippetBtn.SetToolTipString(_(u"Create new snippet"))
        groupButtonsSizer.Add(self.addSnippetBtn, flag=wx.ALL, border=0)

        # Rename group or snippet button
        self.renameBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "rename.png")))
        self.renameBtn.SetToolTipString(_(u"Rename"))
        groupButtonsSizer.Add(self.renameBtn, flag=wx.ALL, border=0)

        # Remove group or snippet button
        self.removeBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "remove.png")))
        self.removeBtn.SetToolTipString(_(u"Remove"))
        groupButtonsSizer.Add(self.removeBtn, flag=wx.ALL, border=0)

        # Run snippet
        self.runSnippetBtn = wx.BitmapButton(
            self, bitmap=wx.Bitmap(os.path.join(self._imagesPath, "run.png")))
        self.runSnippetBtn.SetToolTipString(_(u"Run snippet"))
        groupButtonsSizer.Add(self.runSnippetBtn, flag=wx.ALL, border=0)

        # Open help
        self.openHelpBtn = wx.BitmapButton(
            self, bitmap=wx.Bitmap(os.path.join(self._imagesPath, "help.png")))
        self.openHelpBtn.SetToolTipString(_(u"Open help..."))
        groupButtonsSizer.Add(self.openHelpBtn, flag=wx.ALL, border=0)

    def _createImagesList(self):
        self._imagelist = SafeImageList(self.ICON_WIDTH, self.ICON_HEIGHT)

        self._dirImageId = self._imagelist.Add(
            wx.Bitmap(os.path.join(self._imagesPath, u'folder.png')))

        self._snippetImageId = self._imagelist.Add(
            wx.Bitmap(os.path.join(self._imagesPath, u'snippet.png')))

    def _createTreePanel(self, mainSizer):
        self._createImagesList()

        self.snippetsTree = wx.TreeCtrl(self,
                                        style=wx.TR_HAS_BUTTONS
                                        | wx.TR_EDIT_LABELS | wx.SUNKEN_BORDER)
        self.snippetsTree.SetMinSize((200, 200))

        self.snippetsTree.AssignImageList(self._imagelist)

        # Buttons for the snippets tree
        groupButtonsSizer = wx.BoxSizer(wx.HORIZONTAL)
        self._createTreeButtons(groupButtonsSizer)

        # TreeSizer
        treeSizer = wx.FlexGridSizer(cols=1)
        treeSizer.AddGrowableRow(1)
        treeSizer.AddGrowableCol(0)
        treeSizer.Add(groupButtonsSizer, 1, wx.EXPAND, border=2)
        treeSizer.Add(self.snippetsTree, 1, wx.EXPAND, border=2)

        mainSizer.Add(treeSizer, 1, wx.ALL | wx.EXPAND, border=2)

    def _createSnippetButtons(self, snippetButtonsSizer, parent):
        # Insert variable
        self.insertVariableBtn = PopupButton(parent,
                                             bitmap=wx.Bitmap(
                                                 os.path.join(
                                                     self._imagesPath,
                                                     "variables-menu.png")))
        self.insertVariableBtn.SetToolTipString(_(u"Insert variable"))

        for menuitem in self._varMenuItems:
            data = u'{{' + menuitem[1] + u'}}'
            title = u'{var} - {title}'.format(var=data, title=menuitem[0])
            self.insertVariableBtn.appendMenuItem(title, data)

        self.insertVariableBtn.appendMenuItem(_(u'Other variable...'), None)

        snippetButtonsSizer.Add(self.insertVariableBtn, flag=wx.ALL, border=0)

        # Insert block
        self.insertBlockBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "block-menu.png")))
        self.insertBlockBtn.SetToolTipString(_(u"Insert block"))

        for menuitem in self._blocksMenuItems:
            data = menuitem[1]
            title = menuitem[0]
            self.insertBlockBtn.appendMenuItem(title, data)

        snippetButtonsSizer.Add(self.insertBlockBtn, flag=wx.ALL, border=0)

    def _createSnippetPanel(self, mainSizer):
        self._snippetPanel = wx.Panel(self)

        # Snippet editor
        self.snippetEditor = SnippetEditor(self._snippetPanel)

        # Buttons for snippet
        snippetButtonsSizer = wx.BoxSizer(wx.HORIZONTAL)
        self._createSnippetButtons(snippetButtonsSizer, self._snippetPanel)

        # Errors messages
        self.errorsTextCtrl = wx.TextCtrl(self._snippetPanel,
                                          style=wx.TE_MULTILINE
                                          | wx.TE_READONLY)
        self.errorsTextCtrl.SetMinSize((-1, 100))

        # SnippetSizer
        snippetSizer = wx.FlexGridSizer(cols=1)
        snippetSizer.AddGrowableRow(1)
        snippetSizer.AddGrowableCol(0)

        snippetSizer.Add(snippetButtonsSizer, 1, wx.EXPAND, border=2)
        snippetSizer.Add(self.snippetEditor, 1, wx.EXPAND, border=2)
        snippetSizer.Add(self.errorsTextCtrl, 1, wx.EXPAND, border=2)

        self._snippetPanel.SetSizer(snippetSizer)
        mainSizer.Add(self._snippetPanel, 1, wx.ALL | wx.EXPAND, border=2)

    def _createBottomButtons(self, mainSizer):
        mainSizer.AddStretchSpacer()
        self.closeBtn = wx.Button(self, id=wx.ID_CLOSE, label=_(u'Close'))
        mainSizer.Add(self.closeBtn, flag=wx.ALL | wx.ALIGN_RIGHT, border=2)
        self.SetEscapeId(wx.ID_CLOSE)

    def _createGUI(self):
        # Main Sizer
        mainSizer = wx.FlexGridSizer(cols=2)
        mainSizer.AddGrowableCol(0, 1)
        mainSizer.AddGrowableCol(1, 3)
        mainSizer.AddGrowableRow(0)

        self._createTreePanel(mainSizer)
        self._createSnippetPanel(mainSizer)
        self._createBottomButtons(mainSizer)

        self.SetSizer(mainSizer)
        self.Layout()

    @property
    def currentSnippet(self):
        return self.snippetEditor.GetText()

    def setError(self, text):
        self.errorsTextCtrl.SetValue(text)
Beispiel #4
0
class EditSnippetsDialog(wx.Frame):
    '''
    Dialog to create, edit and remove snippets and folders.
    '''
    def __init__(self, parent):
        super().__init__(
            parent,
            style=wx.CAPTION | wx.CLOSE | wx.SYSTEM_MENU | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.CLOSE_BOX | wx.RESIZE_BORDER | wx.FRAME_TOOL_WINDOW | wx.FRAME_NO_TASKBAR | wx.FRAME_FLOAT_ON_PARENT
        )
        global _
        _ = get_()

        self.ICON_WIDTH = 16
        self.ICON_HEIGHT = 16

        self._imagesPath = getImagesPath()
        self._dirImageId = None
        self._snippetImageId = None

        self.addGroupBtn = None
        self.addSnippetBtn = None

        self._varMenuItems = [
            (_(u'Selected text'), defines.VAR_SEL_TEXT),
            (_(u'Current date'), defines.VAR_DATE),
            (_(u'Page title'), defines.VAR_TITLE),
            (_(u'Page type'), defines.VAR_PAGE_TYPE),
            (_(u'Page tags list'), defines.VAR_TAGS),
            (_(u'Attachments path'), defines.VAR_ATTACH),
            (_(u'Path to page'), defines.VAR_FOLDER),
            (_(u'Relative page path'), defines.VAR_SUBPATH),
            (_(u'Page creation date'), defines.VAR_DATE_CREATING),
            (_(u'Page modification date'), defines.VAR_DATE_EDITIND),
            (_(u'Page Id'), defines.VAR_PAGE_ID),
            (_(u'Attachments list'), defines.VAR_ATTACHLIST),
            (_(u'Child pages'), defines.VAR_CHILDLIST),
        ]

        self._blocksMenuItems = [
            (_('{% if %}'), (u'{% if %}', '{% elif %}{% else %}{% endif %}')),
            (_('{% include %}'), (u"{% include '", u"' %}")),
            (_('{# comment #}'), (u'{# ', ' #}')),
        ]

        self._createGUI()
        self.SetTitle(_(u'Snippets management'))
        self.disableSnippetEditor()

    def disableSnippetEditor(self):
        self.snippetEditor.SetText(u'')
        self._snippetPanel.Disable()
        self.runSnippetBtn.Disable()

    def enableSnippetEditor(self):
        self._snippetPanel.Enable()
        self.runSnippetBtn.Enable()

    def appendDirTreeItem(self, parentItem, name, data):
        itemData = data
        if parentItem is not None:
            newItemId = self.snippetsTree.AppendItem(parentItem,
                                                     name,
                                                     self._dirImageId,
                                                     data=itemData)
        else:
            newItemId = self.snippetsTree.AddRoot(name,
                                                  self._dirImageId,
                                                  data=itemData)
        return newItemId

    def appendSnippetTreeItem(self, parentItem, name, data):
        itemData = data
        newItemId = self.snippetsTree.AppendItem(parentItem,
                                                 name,
                                                 self._snippetImageId,
                                                 data=itemData)
        return newItemId

    def _createTreeButtons(self, groupButtonsSizer):
        # Add a group button
        self.addGroupBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "folder_add.png"))
        )
        self.addGroupBtn.SetToolTip(_(u"Add new snippets group"))
        groupButtonsSizer.Add(self.addGroupBtn, flag=wx.ALL, border=0)

        # Add a snippet button
        self.addSnippetBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "snippet_add.png"))
        )
        self.addSnippetBtn.SetToolTip(_(u"Create new snippet"))
        groupButtonsSizer.Add(self.addSnippetBtn, flag=wx.ALL, border=0)

        # Rename group or snippet button
        self.renameBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "rename.png"))
        )
        self.renameBtn.SetToolTip(_(u"Rename"))
        groupButtonsSizer.Add(self.renameBtn, flag=wx.ALL, border=0)

        # Remove group or snippet button
        self.removeBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "remove.png"))
        )
        self.removeBtn.SetToolTip(_(u"Remove"))
        groupButtonsSizer.Add(self.removeBtn, flag=wx.ALL, border=0)

        # Run snippet
        self.runSnippetBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "run.png"))
        )
        self.runSnippetBtn.SetToolTip(_(u"Run snippet"))
        groupButtonsSizer.Add(self.runSnippetBtn, flag=wx.ALL, border=0)

        # Open help
        self.openHelpBtn = wx.BitmapButton(
            self,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "help.png"))
        )
        self.openHelpBtn.SetToolTip(_(u"Open help..."))
        groupButtonsSizer.Add(self.openHelpBtn, flag=wx.ALL, border=0)

    def _createImagesList(self):
        self._imagelist = SafeImageList(self.ICON_WIDTH, self.ICON_HEIGHT)

        self._dirImageId = self._imagelist.Add(
            wx.Bitmap(os.path.join(self._imagesPath, u'folder.png'))
        )

        self._snippetImageId = self._imagelist.Add(
            wx.Bitmap(os.path.join(self._imagesPath, u'snippet.png'))
        )

    def _createTreePanel(self, mainSizer):
        self._createImagesList()

        self.snippetsTree = wx.TreeCtrl(
            self,
            style=wx.TR_HAS_BUTTONS | wx.TR_EDIT_LABELS | wx.SUNKEN_BORDER)
        self.snippetsTree.SetMinSize((200, 200))

        self.snippetsTree.AssignImageList(self._imagelist)

        # Buttons for the snippets tree
        groupButtonsSizer = wx.BoxSizer(wx.HORIZONTAL)
        self._createTreeButtons(groupButtonsSizer)

        # TreeSizer
        treeSizer = wx.FlexGridSizer(cols=1)
        treeSizer.AddGrowableRow(1)
        treeSizer.AddGrowableCol(0)
        treeSizer.Add(groupButtonsSizer, 1, wx.EXPAND, border=2)
        treeSizer.Add(self.snippetsTree, 1, wx.EXPAND, border=2)

        mainSizer.Add(treeSizer, 1, wx.ALL | wx.EXPAND, border=2)

    def _createSnippetButtons(self, snippetButtonsSizer, parent):
        # Insert variable
        self.insertVariableBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath,
                                          "variables-menu.png"))
        )
        self.insertVariableBtn.SetToolTip(_(u"Insert variable"))

        for menuitem in self._varMenuItems:
            data = u'{{' + menuitem[1] + u'}}'
            title = u'{var} - {title}'.format(var=data, title=menuitem[0])
            self.insertVariableBtn.appendMenuItem(title, data)

        self.insertVariableBtn.appendMenuItem(_(u'Other variable...'), None)

        snippetButtonsSizer.Add(self.insertVariableBtn, flag=wx.ALL, border=0)

        # Insert block
        self.insertBlockBtn = PopupButton(
            parent,
            bitmap=wx.Bitmap(os.path.join(self._imagesPath, "block-menu.png"))
        )
        self.insertBlockBtn.SetToolTip(_(u"Insert block"))

        for menuitem in self._blocksMenuItems:
            data = menuitem[1]
            title = menuitem[0]
            self.insertBlockBtn.appendMenuItem(title, data)

        snippetButtonsSizer.Add(self.insertBlockBtn, flag=wx.ALL, border=0)

    def _createSnippetPanel(self, mainSizer):
        self._snippetPanel = wx.Panel(self)

        # Snippet editor
        self.snippetEditor = SnippetEditor(self._snippetPanel)

        # Buttons for snippet
        snippetButtonsSizer = wx.BoxSizer(wx.HORIZONTAL)
        self._createSnippetButtons(snippetButtonsSizer, self._snippetPanel)

        # Errors messages
        self.errorsTextCtrl = wx.TextCtrl(
            self._snippetPanel,
            style=wx.TE_MULTILINE | wx.TE_READONLY
        )
        self.errorsTextCtrl.SetMinSize((-1, 100))

        # SnippetSizer
        snippetSizer = wx.FlexGridSizer(cols=1)
        snippetSizer.AddGrowableRow(1)
        snippetSizer.AddGrowableCol(0)

        snippetSizer.Add(snippetButtonsSizer, 1, wx.EXPAND, border=2)
        snippetSizer.Add(self.snippetEditor, 1, wx.EXPAND, border=2)
        snippetSizer.Add(self.errorsTextCtrl, 1, wx.EXPAND, border=2)

        self._snippetPanel.SetSizer(snippetSizer)
        mainSizer.Add(self._snippetPanel, 1, wx.ALL | wx.EXPAND, border=2)

    def _createBottomButtons(self, mainSizer):
        mainSizer.AddStretchSpacer()
        self.closeBtn = wx.Button(self, id=wx.ID_CLOSE, label=_(u'Close'))
        mainSizer.Add(self.closeBtn, flag=wx.ALL | wx.ALIGN_RIGHT, border=2)

    def _createMenu(self):
        self._menuBar = wx.MenuBar()
        editMenu = self._createEditMenu()
        fileMenu = self._createFileMenu()
        helpMenu = self._createHelpMenu()
        self._menuBar.Append(fileMenu, _('File'))
        self._menuBar.Append(editMenu, _('Edit'))
        self._menuBar.Append(helpMenu, _('Help'))
        self.SetMenuBar(self._menuBar)

    def _createFileMenu(self):
        menu = wx.Menu()
        menu.Append(self.addGroupBtn.GetId(),
                    _(u"Add new snippets group") + '\tCtrl+Shift+N')
        menu.Append(self.addSnippetBtn.GetId(),
                    _(u"Create new snippet") + '\tCtrl+N')
        menu.Append(self.renameBtn.GetId(), _(u"Rename") + '\tF2')
        menu.Append(self.removeBtn.GetId(), _(u"Remove") + '\tCtrl+Del')
        menu.Append(self.runSnippetBtn.GetId(), _(u"Run snippet") + '\tF5')
        return menu

    def _createHelpMenu(self):
        menu = wx.Menu()
        menu.Append(self.openHelpBtn.GetId(), _(u"Open help...") + '\tF1')
        return menu

    def _createEditMenu(self):
        menu = wx.Menu()
        menu.Append(wx.ID_UNDO, _(u"Undo") + "\tCtrl+Z")
        menu.Append(wx.ID_REDO, _(u"Redo") + "\tCtrl+Y")
        menu.AppendSeparator()
        menu.Append(wx.ID_CUT, _(u"Cut") + "\tCtrl+X")
        menu.Append(wx.ID_COPY, _(u"Copy") + "\tCtrl+C")
        menu.Append(wx.ID_PASTE, _(u"Paste") + "\tCtrl+V")
        menu.AppendSeparator()
        menu.Append(wx.ID_SELECTALL, _(u"Select All") + "\tCtrl+A")
        return menu

    def _createGUI(self):
        # Main Sizer
        mainSizer = wx.FlexGridSizer(cols=2)
        mainSizer.AddGrowableCol(0, 1)
        mainSizer.AddGrowableCol(1, 3)
        mainSizer.AddGrowableRow(0)

        self._createTreePanel(mainSizer)
        self._createSnippetPanel(mainSizer)
        self._createBottomButtons(mainSizer)
        self._createMenu()

        self.SetSizer(mainSizer)
        self.Layout()

    @property
    def currentSnippet(self):
        return self.snippetEditor.GetText()

    def setError(self, text):
        self.errorsTextCtrl.SetValue(text)