2
# -*- coding: utf-8 -*-
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
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.
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.
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/>.
14
import wx, os, pickle, locale
17
class LorzeDlgHelper():
18
# load and save dialogs for LORZE ersandcad
20
def __init__(self, parent):
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
40
def OpenDialog(self, label, filedir, filename, wildcard):
41
# dialog for open files, show dialog, return path or ''
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)
48
if dlg.ShowModal()== wx.ID_OK:
49
# clicked ok, set path, destroy dialog, return path
55
# not clicked ok, destroy dialog, return ''
60
def SaveDialog(self, label, filedir, filename, wildcard, defext):
61
# dialog for save files with extension & overwrite control, show dialog
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)
70
if dlg.ShowModal()== wx.ID_OK:
71
# clicked ok, set path, destroy dialog
75
if path.endswith(defext)== False:
76
# check extension, correct extension, syntax for the os
77
path= os.path.normpath(path+ defext)
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()
85
# do not overwrite, return ''
88
# overwrite, return path
91
# file dont exist, all ok
96
# not clicked ok, destroy dialog
101
def ColorDialog(self, default):
102
# dialog to select color, return dialog in html format or ''
103
# default= default color in html syntax
106
defaultdata= wx.ColourData()
107
defaultdata.SetColour(default)
110
dlg = wx.ColourDialog(self.__parent, data= defaultdata)
111
dlg.GetColourData().SetChooseFull(True)
114
if dlg.ShowModal() == wx.ID_OK:
116
data = dlg.GetColourData()
117
return(data.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
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)
131
if dlg.ShowModal() == wx.ID_OK:
142
def ListSelection(self, titel, choicelist, default):
143
# show combobox dialogue
144
dlg= DlgListSel(self.__parent, titel, choicelist, default)
147
# get choice, close dialogue, return choice, '' for cancel
148
choice= dlg.GetChoice()
153
class DlgListSel(wx.Dialog):
154
# dialog with a listbox choice
156
def __init__(self, parent, titel, choicelist, default):
158
wx.Dialog.__init__(self, parent, wx.ID_ANY, titel, size=(200, 300), style= wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
160
# choicelist and choiceindex
161
self.__choicelist= choicelist
162
self.__choiceindex= None
165
self.__list= wx.ListBox(self, size= (-1, -1), choices= self.__choicelist)
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)
172
buttonok= wx.Button(self, label= u'OK')
173
buttoncancel= wx.Button(self, label= u'Cancel')
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)
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)
192
def OnOk(self, event):
193
self.__choiceindex= self.__list.GetSelection()
197
def OnCancel(self, event):
198
self.__choiceindex= None
203
if self.__choiceindex== None:
206
return(self.__choicelist[self.__choiceindex])
209
class TestFrame(wx.Frame):
212
wx.Frame.__init__(self, None, title= u'Test Dialog Load/ Save Object')
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')
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)
226
self.__dlg= LorzeDlgHelper(self)
228
vbox= wx.BoxSizer(wx.VERTICAL)
231
vbox.Add(buttoncolour)
240
def OnLoad(self, event):
241
print(self.__dlg.OpenDialog('Load dialog', '/', 'Test.txt',
242
'Text (*.txt)|*.txt|All (*.*)|*.*'))
245
def OnSave(self, event):
246
print(self.__dlg.SaveDialog('Save dialog', '/', 'Test.txt',
247
'Text (*.txt)|*.txt|All (*.*)|*.*', '.txt'))
250
def OnColour(self, event):
251
print(self.__dlg.ColorDialog('#FFFFFF'))
254
def OnText(self, event):
255
print(self.__dlg.TextDialog('Test', 'Testeingabe ..', '123'))
258
def OnList(self, event):
259
print(self.__dlg.ListSelection('List-Test', ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], 'e'))
262
if __name__== '__main__':
265
# internationalization
267
wxloc.AddCatalogLookupPathPrefix('./in18')
268
# get system language ('xx_XX', 'CHARSET')
269
wxlang= locale.getdefaultlocale()
270
wxlang= wxlang[0][:2]
271
# select translations
273
wxloc.AddCatalog('LORZE_de')
274
# name for translations texts