pronterface_widgets.py 9.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# This file is part of the Printrun suite.
#
# Printrun is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Printrun is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Printrun.  If not, see <http://www.gnu.org/licenses/>.

import wx
17
import re
18

19
class MacroEditor(wx.Dialog):
20
    """Really simple editor to edit macro definitions"""
21

22
    def __init__(self, macro_name, definition, callback, gcode = False):
23
        self.indent_chars = "  "
24
        title = "  macro %s"
25
        if gcode:
26 27 28
            title = "  %s"
        self.gcode = gcode
        wx.Dialog.__init__(self, None, title = title % macro_name, style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
29
        self.callback = callback
30 31
        self.panel = wx.Panel(self,-1)
        titlesizer = wx.BoxSizer(wx.HORIZONTAL)
32
        titletext = wx.StaticText(self.panel,-1, "              _")  #title%macro_name)
33 34
        #title.SetFont(wx.Font(11, wx.NORMAL, wx.NORMAL, wx.BOLD))
        titlesizer.Add(titletext, 1)
35
        self.findb = wx.Button(self.panel,  -1, _("Find"), style = wx.BU_EXACTFIT)  #New button for "Find" (Jezmy)
36
        self.findb.Bind(wx.EVT_BUTTON,  self.find)
37
        self.okb = wx.Button(self.panel, -1, _("Save"), style = wx.BU_EXACTFIT)
38 39 40 41
        self.okb.Bind(wx.EVT_BUTTON, self.save)
        self.Bind(wx.EVT_CLOSE, self.close)
        titlesizer.Add(self.findb)
        titlesizer.Add(self.okb)
42
        self.cancelb = wx.Button(self.panel, -1, _("Cancel"), style = wx.BU_EXACTFIT)
43 44
        self.cancelb.Bind(wx.EVT_BUTTON, self.close)
        titlesizer.Add(self.cancelb)
45
        topsizer = wx.BoxSizer(wx.VERTICAL)
46
        topsizer.Add(titlesizer, 0, wx.EXPAND)
47
        self.e = wx.TextCtrl(self.panel, style = wx.HSCROLL|wx.TE_MULTILINE|wx.TE_RICH2, size = (400, 400))
48 49 50 51
        if not self.gcode:
            self.e.SetValue(self.unindent(definition))
        else:
            self.e.SetValue("\n".join(definition))
52
        topsizer.Add(self.e, 1, wx.ALL+wx.EXPAND)
53 54 55 56 57
        self.panel.SetSizer(topsizer)
        topsizer.Layout()
        topsizer.Fit(self)
        self.Show()
        self.e.SetFocus()
58

59
    def find(self, ev):
60 61 62 63
        # Ask user what to look for, find it and point at it ...  (Jezmy)
        S = self.e.GetStringSelection()
        if not S :
            S = "Z"
64
        FindValue = wx.GetTextFromUser('Please enter a search string:', caption = "Search", default_value = S, parent = None)
65 66
        somecode = self.e.GetValue()
        numLines = len(somecode)
67
        position = somecode.find(FindValue,  self.e.GetInsertionPoint())
68 69
        if position == -1 :
         #   ShowMessage(self,-1,  "Not found!")
70
            titletext = wx.TextCtrl(self.panel,-1, "Not Found!")
71 72 73
        else:
        # self.title.SetValue("Position : "+str(position))

74
            titletext = wx.TextCtrl(self.panel,-1, str(position))
75 76

            # ananswer = wx.MessageBox(str(numLines)+" Lines detected in file\n"+str(position), "OK")
77
            self.e.SetFocus()
78
            self.e.SetInsertionPoint(position)
79 80
            self.e.SetSelection(position,  position + len(FindValue))
            self.e.ShowPosition(position)
81

82 83 84 85 86
    def ShowMessage(self, ev , message):
        dlg = wxMessageDialog(self, message,
                              "Info!", wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()
87

88
    def save(self, ev):
89 90 91 92 93
        self.Destroy()
        if not self.gcode:
            self.callback(self.reindent(self.e.GetValue()))
        else:
            self.callback(self.e.GetValue().split("\n"))
94

95
    def close(self, ev):
96
        self.Destroy()
97

98
    def unindent(self, text):
99
        self.indent_chars = text[:len(text)-len(text.lstrip())]
100 101
        if len(self.indent_chars) == 0:
            self.indent_chars = "  "
102
        unindented = ""
103
        lines = re.split(r"(?:\r\n?|\n)", text)
104 105 106 107 108 109 110 111 112
        #print lines
        if len(lines) <= 1:
            return text
        for line in lines:
            if line.startswith(self.indent_chars):
                unindented += line[len(self.indent_chars):] + "\n"
            else:
                unindented += line + "\n"
        return unindented
113
    def reindent(self, text):
114
        lines = re.split(r"(?:\r\n?|\n)", text)
115 116 117 118 119 120 121 122 123 124
        if len(lines) <= 1:
            return text
        reindented = ""
        for line in lines:
            if line.strip() != "":
                reindented += self.indent_chars + line + "\n"
        return reindented

class options(wx.Dialog):
    """Options editor"""
125 126 127 128
    def __init__(self, pronterface):
        wx.Dialog.__init__(self, None, title = _("Edit settings"), style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
        topsizer = wx.BoxSizer(wx.VERTICAL)
        vbox = wx.StaticBoxSizer(wx.StaticBox(self, label = _("Defaults")) ,wx.VERTICAL)
129
        topsizer.Add(vbox, 1, wx.ALL+wx.EXPAND)
130
        grid = wx.FlexGridSizer(rows = 0, cols = 2, hgap = 8, vgap = 2)
131 132 133
        grid.SetFlexibleDirection( wx.BOTH )
        grid.AddGrowableCol( 1 )
        grid.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
134
        vbox.Add(grid, 0, wx.EXPAND)
135
        ctrls = {}
136 137 138
        for k, v in sorted(pronterface.settings._all_settings().items()):
            ctrls[k, 0] = wx.StaticText(self,-1, k)
            ctrls[k, 1] = wx.TextCtrl(self,-1, str(v))
139
            if k in pronterface.helpdict:
140 141
                ctrls[k, 0].SetToolTipString(pronterface.helpdict.get(k))
                ctrls[k, 1].SetToolTipString(pronterface.helpdict.get(k))
142 143 144
            grid.Add(ctrls[k, 0], 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.ALIGN_RIGHT)
            grid.Add(ctrls[k, 1], 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL|wx.EXPAND)
        topsizer.Add(self.CreateSeparatedButtonSizer(wx.OK+wx.CANCEL), 0, wx.EXPAND)
145 146 147
        self.SetSizer(topsizer)
        topsizer.Layout()
        topsizer.Fit(self)
148 149 150 151
        if self.ShowModal() == wx.ID_OK:
            for k, v in pronterface.settings._all_settings().items():
                if ctrls[k, 1].GetValue() != str(v):
                    pronterface.set(k, str(ctrls[k, 1].GetValue()))
152 153 154 155
        self.Destroy()

class ButtonEdit(wx.Dialog):
    """Custom button edit dialog"""
156
    def __init__(self, pronterface):
157
        wx.Dialog.__init__(self, None, title = _("Custom button"), style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
158 159 160 161
        self.pronterface = pronterface
        topsizer = wx.BoxSizer(wx.VERTICAL)
        grid = wx.FlexGridSizer(rows = 0, cols = 2, hgap = 4, vgap = 2)
        grid.AddGrowableCol(1, 1)
162
        grid.Add(wx.StaticText(self,-1, _("Button title")), 0, wx.BOTTOM|wx.RIGHT)
163 164
        self.name = wx.TextCtrl(self,-1, "")
        grid.Add(self.name, 1, wx.EXPAND)
165
        grid.Add(wx.StaticText(self, -1, _("Command")), 0, wx.BOTTOM|wx.RIGHT)
166
        self.command = wx.TextCtrl(self,-1, "")
167
        xbox = wx.BoxSizer(wx.HORIZONTAL)
168
        xbox.Add(self.command, 1, wx.EXPAND)
169
        self.command.Bind(wx.EVT_TEXT, self.macrob_enabler)
170
        self.macrob = wx.Button(self,-1, "..", style = wx.BU_EXACTFIT)
171 172
        self.macrob.Bind(wx.EVT_BUTTON, self.macrob_handler)
        xbox.Add(self.macrob, 0)
173 174 175 176 177 178 179
        grid.Add(xbox, 1, wx.EXPAND)
        grid.Add(wx.StaticText(self,-1, _("Color")), 0, wx.BOTTOM|wx.RIGHT)
        self.color = wx.TextCtrl(self,-1, "")
        grid.Add(self.color, 1, wx.EXPAND)
        topsizer.Add(grid, 0, wx.EXPAND)
        topsizer.Add( (0, 0), 1)
        topsizer.Add(self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL), 0, wx.ALIGN_CENTER)
180 181
        self.SetSizer(topsizer)

182
    def macrob_enabler(self, e):
183 184 185 186 187 188 189
        macro = self.command.GetValue()
        valid = False
        try:
            if macro == "":
                valid = True
            elif self.pronterface.macros.has_key(macro):
                valid = True
190
            elif hasattr(self.pronterface.__class__, u"do_"+macro):
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                valid = False
            elif len([c for c in macro if not c.isalnum() and c != "_"]):
                valid = False
            else:
                valid = True
        except:
            if macro == "":
                valid = True
            elif self.pronterface.macros.has_key(macro):
                valid = True
            elif len([c for c in macro if not c.isalnum() and c != "_"]):
                valid = False
            else:
                valid = True
        self.macrob.Enable(valid)

207
    def macrob_handler(self, e):
208 209 210 211 212 213
        macro = self.command.GetValue()
        macro = self.pronterface.edit_macro(macro)
        self.command.SetValue(macro)
        if self.name.GetValue()=="":
            self.name.SetValue(macro)

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
class SpecialButton(object):

    label = None
    command = None
    background = None
    pos = None
    span = None
    tooltip = None
    custom = None

    def __init__(self, label, command, background = None, pos = None, span = None, tooltip = None, custom = False):
        self.label = label
        self.command = command
        self.pos = pos
        self.background = background
        self.span = span
        self.tooltip = tooltip
        self.custom = custom