~lorzeteam/lorze/trunk

« back to all changes in this revision

Viewing changes to Scripts/lrzdlghelper.py

  • Committer: Andreas Ulrich
  • Date: 2012-12-13 20:54:19 UTC
  • Revision ID: ulrich3110@gmail.com-20121213205419-aucgdskqtqmyrj10
new file structure, new object structure, about dialogue, help dialogue, hep pages in english and german, german translation, ponton5h installer, documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
# LORZE erasandcad, a 2D CAD with an intuitive user interface, simple and easy.
5
 
# http://erasand.jimdo.com/python-programme/lorze/
6
 
# (C) 2012, Andreas Ulrich
7
 
 
8
 
# This program 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.
9
 
 
10
 
# This program 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.
11
 
 
12
 
# You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
13
 
 
14
 
import wx, os, pickle, locale
15
 
 
16
 
 
17
 
class LorzeDlgHelper():
18
 
    # load and save dialogs for LORZE ersandcad
19
 
 
20
 
    def __init__(self, parent):
21
 
 
22
 
        # parent class
23
 
        self.__parent= parent
24
 
 
25
 
        # attributes for load & save dialog
26
 
        # path: string, whole path
27
 
        # data: container, data to save or loaded data
28
 
        # filename & filedir: strings, name and directory of path
29
 
        # label: string, caption of dialog
30
 
        # wildcard: string, at example: 'Text (*.txt)|*.txt|All (*.*)|*.*'
31
 
        # defext: string, default extension
32
 
        self.__path= None
33
 
        self.__data= None
34
 
        self.__filename= ''
35
 
        self.__filedir= ''
36
 
        self.__label= ''
37
 
        self.__wildcard= ''
38
 
        self.__defext= ''
39
 
 
40
 
    def OpenDialog(self, label, filedir, filename, wildcard):
41
 
        # dialog for open files, show dialog, return path or ''
42
 
        # parent: wx.Frame
43
 
        # label: string, title of dialog
44
 
        # filedir & filename: strings, default filename and directory
45
 
        # wildcard: string, at example: 'Text (*.txt)|*.txt|All (*.*)|*.*'
46
 
        dlg= wx.FileDialog(self.__parent, label, filedir, filename, wildcard, wx.FD_OPEN)
47
 
 
48
 
        if dlg.ShowModal()== wx.ID_OK:
49
 
            # clicked ok, set path, destroy dialog, return path
50
 
            path= dlg.GetPath()
51
 
            dlg.Destroy()
52
 
            return(path)
53
 
 
54
 
        else:
55
 
            # not clicked ok, destroy dialog, return ''
56
 
            dlg.Destroy()
57
 
            return('')
58
 
 
59
 
 
60
 
    def SaveDialog(self, label, filedir, filename, wildcard, defext):
61
 
        # dialog for save files with extension & overwrite control, show dialog
62
 
        # return path or ''
63
 
        # parent: wx.Frame
64
 
        # label: string, title of dialog
65
 
        # filedir & filename: strings, default filename and directory
66
 
        # wildcard: string, at example: 'Text (*.txt)|*.txt|All (*.*)|*.*'
67
 
        # defext: string, extension must have, at example '.txt'
68
 
        dlg= wx.FileDialog(self.__parent, label, filedir,filename, wildcard, wx.FD_SAVE)
69
 
 
70
 
        if dlg.ShowModal()== wx.ID_OK:
71
 
            # clicked ok, set path, destroy dialog
72
 
            path= dlg.GetPath()
73
 
            dlg.Destroy()
74
 
 
75
 
            if path.endswith(defext)== False:
76
 
                # check extension, correct extension, syntax for the os
77
 
                path= os.path.normpath(path+ defext)
78
 
 
79
 
            if os.path.exists(path):
80
 
                # file already exist, overwrite?, show dialog and get answer
81
 
                question= wx.MessageDialog(None, _(u'The file already exists: ')+ path+ _(u'. Do you want to overwrite ?'), _(u'Confirmation'), wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION)
82
 
                answer= question.ShowModal()
83
 
 
84
 
                if answer== wx.ID_NO:
85
 
                    # do not overwrite, return ''
86
 
                    return('')
87
 
                else:
88
 
                    # overwrite, return path
89
 
                    return(path)
90
 
 
91
 
            # file dont exist, all ok
92
 
            else:
93
 
                return(path)
94
 
 
95
 
        else:
96
 
            # not clicked ok, destroy dialog
97
 
            dlg.Destroy()
98
 
            return('')
99
 
 
100
 
 
101
 
    def ColorDialog(self, default):
102
 
        # dialog to select color, return dialog in html format or ''
103
 
        # default= default color in html syntax
104
 
 
105
 
        # default color
106
 
        defaultdata= wx.ColourData()
107
 
        defaultdata.SetColour(default)
108
 
 
109
 
        # dialog
110
 
        dlg = wx.ColourDialog(self.__parent, data= defaultdata)
111
 
        dlg.GetColourData().SetChooseFull(True)
112
 
 
113
 
        # show dialog
114
 
        if dlg.ShowModal() == wx.ID_OK:
115
 
            # clicked ok
116
 
            data = dlg.GetColourData()
117
 
            return(data.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
118
 
        else:
119
 
            # not clicked ok
120
 
            return('')
121
 
 
122
 
 
123
 
    def TextDialog(self, titel, label, default):
124
 
        # dialog to entry text, return string or ''
125
 
        # title= titel of the dialogue
126
 
        # label= description for the text input
127
 
        # default= detaulf text
128
 
        dlg= wx.TextEntryDialog(self.__parent, label, titel)
129
 
        dlg.SetValue(default)
130
 
 
131
 
        if dlg.ShowModal() == wx.ID_OK:
132
 
            # clicked ok
133
 
            text= dlg.GetValue()
134
 
        else:
135
 
            # not clicked ok
136
 
            text= ''
137
 
 
138
 
        dlg.Destroy()
139
 
        return(text)
140
 
 
141
 
 
142
 
    def ListSelection(self, titel, choicelist, default):
143
 
        # show combobox dialogue
144
 
        dlg= DlgListSel(self.__parent, titel, choicelist, default)
145
 
        dlg.ShowModal()
146
 
 
147
 
        # get choice, close dialogue, return choice, '' for cancel
148
 
        choice= dlg.GetChoice()
149
 
        dlg.Destroy()
150
 
        return(choice)
151
 
 
152
 
 
153
 
class DlgListSel(wx.Dialog):
154
 
    # dialog with a listbox choice
155
 
 
156
 
    def __init__(self, parent, titel, choicelist, default):
157
 
        # subclass
158
 
        wx.Dialog.__init__(self, parent, wx.ID_ANY, titel, size=(200, 300), style= wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
159
 
 
160
 
        # choicelist and choiceindex
161
 
        self.__choicelist= choicelist
162
 
        self.__choiceindex= None
163
 
 
164
 
        # listbox
165
 
        self.__list= wx.ListBox(self, size= (-1, -1), choices= self.__choicelist)
166
 
 
167
 
        # search index of default and select default
168
 
        for i in range(len(self.__choicelist)):
169
 
            if self.__choicelist[i]== default:
170
 
                self.__list.SetSelection(i)
171
 
 
172
 
        buttonok= wx.Button(self, label= u'OK')
173
 
        buttoncancel= wx.Button(self, label= u'Cancel')
174
 
 
175
 
        # bindings
176
 
        buttonok.Bind(wx.EVT_BUTTON, self.OnOk)
177
 
        buttoncancel.Bind(wx.EVT_BUTTON, self.OnCancel)
178
 
        self.__list.Bind(wx.EVT_LISTBOX_DCLICK, self.OnOk)
179
 
 
180
 
        # layout
181
 
        vbox= wx.BoxSizer(wx.VERTICAL)
182
 
        vbox.Add(self.__list, 1, wx.EXPAND|wx.ALL, 5)
183
 
        hbox= wx.BoxSizer(wx.HORIZONTAL)
184
 
        hbox.Add(buttonok, 1, wx.EXPAND|wx.ALL, 5)
185
 
        hbox.Add(buttoncancel, 1, wx.EXPAND|wx.ALL, 5)
186
 
        vbox.Add(hbox, 0, wx.EXPAND)
187
 
 
188
 
        self.SetSizer(vbox)
189
 
        self.Centre()
190
 
 
191
 
 
192
 
    def OnOk(self, event):
193
 
        self.__choiceindex= self.__list.GetSelection()
194
 
        self.Close()
195
 
 
196
 
 
197
 
    def OnCancel(self, event):
198
 
        self.__choiceindex= None
199
 
        self.Close()
200
 
 
201
 
 
202
 
    def GetChoice(self):
203
 
        if self.__choiceindex== None:
204
 
            return('')
205
 
        else:
206
 
            return(self.__choicelist[self.__choiceindex])
207
 
 
208
 
 
209
 
class TestFrame(wx.Frame):
210
 
    # object for testing
211
 
    def __init__(self):
212
 
        wx.Frame.__init__(self, None, title= u'Test Dialog Load/ Save Object')
213
 
 
214
 
        buttonload= wx.Button(self, label= u'Load dialogue')
215
 
        buttonsave= wx.Button(self, label= u'Save dialogue')
216
 
        buttoncolour= wx.Button(self, label= u'Color dialogue')
217
 
        buttontext= wx.Button(self, label= u'Text dialogue')
218
 
        buttonlist= wx.Button(self, label= u'List dialogue')
219
 
 
220
 
        buttonload.Bind(wx.EVT_BUTTON, self.OnLoad)
221
 
        buttonsave.Bind(wx.EVT_BUTTON, self.OnSave)
222
 
        buttoncolour.Bind(wx.EVT_BUTTON, self.OnColour)
223
 
        buttontext.Bind(wx.EVT_BUTTON, self.OnText)
224
 
        buttonlist.Bind(wx.EVT_BUTTON, self.OnList)
225
 
 
226
 
        self.__dlg= LorzeDlgHelper(self)
227
 
 
228
 
        vbox= wx.BoxSizer(wx.VERTICAL)
229
 
        vbox.Add(buttonload)
230
 
        vbox.Add(buttonsave)
231
 
        vbox.Add(buttoncolour)
232
 
        vbox.Add(buttontext)
233
 
        vbox.Add(buttonlist)
234
 
 
235
 
        self.SetSizer(vbox)
236
 
        self.Center()
237
 
        self.Show()
238
 
 
239
 
 
240
 
    def OnLoad(self, event):
241
 
        print(self.__dlg.OpenDialog('Load dialog', '/', 'Test.txt',
242
 
         'Text (*.txt)|*.txt|All (*.*)|*.*'))
243
 
 
244
 
 
245
 
    def OnSave(self, event):
246
 
        print(self.__dlg.SaveDialog('Save dialog', '/', 'Test.txt',
247
 
         'Text (*.txt)|*.txt|All (*.*)|*.*', '.txt'))
248
 
 
249
 
 
250
 
    def OnColour(self, event):
251
 
        print(self.__dlg.ColorDialog('#FFFFFF'))
252
 
 
253
 
 
254
 
    def OnText(self, event):
255
 
        print(self.__dlg.TextDialog('Test', 'Testeingabe ..', '123'))
256
 
 
257
 
 
258
 
    def OnList(self, event):
259
 
        print(self.__dlg.ListSelection('List-Test', ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], 'e'))
260
 
 
261
 
 
262
 
if __name__== '__main__':
263
 
    app= wx.App()
264
 
 
265
 
    # internationalization
266
 
    wxloc= wx.Locale()
267
 
    wxloc.AddCatalogLookupPathPrefix('./in18')
268
 
    # get system language ('xx_XX', 'CHARSET')
269
 
    wxlang= locale.getdefaultlocale()
270
 
    wxlang= wxlang[0][:2]
271
 
    # select translations
272
 
    if wxlang== 'de':
273
 
        wxloc.AddCatalog('LORZE_de')
274
 
    # name for translations texts
275
 
    _= wx.GetTranslation
276
 
 
277
 
    frame= TestFrame()
278
 
    app.MainLoop()
279