Coverage for tatlin/lib/ui/dialogs.py: 76%

92 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 05:56 +0000

1# -*- coding: utf-8 -*- 

2# Copyright (C) 2011 Denis Kobozev 

3# 

4# This program is free software; you can redistribute it and/or modify 

5# it under the terms of the GNU General Public License as published by 

6# the Free Software Foundation; either version 2 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program; if not, write to the Free Software Foundation, 

16# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

17 

18 

19import wx 

20import wx.adv 

21 

22 

23class OpenDialog(wx.FileDialog): 

24 ftypes = (None, "gcode", "stl") 

25 

26 def __init__(self, parent, directory=None): 

27 super(OpenDialog, self).__init__( 

28 parent, 

29 "Open", 

30 wildcard="G-code and STL files (*.gcode;*.nc;*.stl)|*.gcode;*.nc;*.stl|G-code files (*.*)|*.*|STL files (*.*)|*.*", 

31 style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST, 

32 ) 

33 

34 if directory is not None: 

35 self.SetDirectory(directory) 

36 

37 def get_path(self): 

38 if self.ShowModal() == wx.ID_CANCEL: 

39 return None 

40 return self.GetPath() 

41 

42 def get_type(self): 

43 return self.ftypes[self.GetFilterIndex()] 

44 

45 

46class OpenErrorAlert(wx.MessageDialog): 

47 def __init__(self, fpath, error): 

48 msg = "Error opening file %s: %s" % (fpath, error) 

49 super(OpenErrorAlert, self).__init__(None, msg, "Error", wx.OK | wx.ICON_ERROR) 

50 

51 def show(self): 

52 self.ShowModal() 

53 

54 

55class SaveDialog(wx.FileDialog): 

56 def __init__(self, parent, directory=None): 

57 super(SaveDialog, self).__init__( 

58 parent, 

59 "Save As", 

60 wildcard="STL files (*.stl)|*.stl", 

61 style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT, 

62 ) 

63 if directory is not None: 

64 self.SetDirectory(directory) 

65 

66 def get_path(self): 

67 if self.ShowModal() == wx.ID_CANCEL: 

68 return None 

69 return self.GetPath() 

70 

71 

72class QuitDialog(wx.Dialog): 

73 RESPONSE_CANCEL = -1 

74 RESPONSE_DISCARD = 0 

75 RESPONSE_SAVE_AS = 1 

76 RESPONSE_SAVE = 2 

77 

78 def __init__(self, parent): 

79 super(QuitDialog, self).__init__(parent, title="Save changes?") 

80 

81 label = wx.StaticText(self, label="Save changes to the file before closing?") 

82 

83 self.btn_discard = wx.Button(self, label="Discard") 

84 self.btn_cancel = wx.Button(self, wx.ID_CANCEL) 

85 self.btn_save_as = wx.Button(self, wx.ID_SAVEAS) 

86 self.btn_save = wx.Button(self, wx.ID_SAVE) 

87 

88 hbox = wx.BoxSizer(wx.HORIZONTAL) 

89 hbox.Add(self.btn_discard, 0) 

90 hbox.Add(self.btn_cancel, 0, wx.LEFT, border=5) 

91 hbox.Add(self.btn_save_as, 0, wx.LEFT, border=5) 

92 hbox.Add(self.btn_save, 0, wx.LEFT, border=5) 

93 

94 vbox = wx.BoxSizer(wx.VERTICAL) 

95 vbox.Add(label, 0, wx.EXPAND | wx.ALL, border=5) 

96 vbox.Add(hbox, -1, wx.EXPAND | wx.RIGHT | wx.BOTTOM | wx.LEFT, border=5) 

97 

98 self.SetSizer(vbox) 

99 self.SetSize((399, 90)) 

100 

101 self.btn_discard.Bind(wx.EVT_BUTTON, self.on_discard) 

102 self.btn_cancel.Bind(wx.EVT_BUTTON, self.on_cancel) 

103 self.btn_save_as.Bind(wx.EVT_BUTTON, self.on_save_as) 

104 self.btn_save.Bind(wx.EVT_BUTTON, self.on_save) 

105 

106 def on_discard(self, event): 

107 self.EndModal(self.RESPONSE_DISCARD) 

108 

109 def on_cancel(self, event): 

110 self.EndModal(self.RESPONSE_CANCEL) 

111 

112 def on_save_as(self, event): 

113 self.EndModal(self.RESPONSE_SAVE_AS) 

114 

115 def on_save(self, event): 

116 self.EndModal(self.RESPONSE_SAVE) 

117 

118 def show(self): 

119 return self.ShowModal() 

120 

121 

122class ProgressDialog(wx.ProgressDialog): 

123 def __init__(self): 

124 super(ProgressDialog, self).__init__("Loading", "", 100) 

125 

126 self.value = -1 

127 

128 def stage(self, message): 

129 self.Show() 

130 self.Update(0, message) 

131 

132 def step(self, count, limit): 

133 self.value = max(-1, min(int(count / limit * 100), 100)) 

134 self.Update(self.value) 

135 

136 def hide(self): 

137 self.Hide() 

138 

139 def destroy(self): 

140 self.Destroy() 

141 

142 

143class AboutDialog(object): 

144 def __init__(self, version, icon, license): 

145 from datetime import datetime 

146 

147 info = wx.adv.AboutDialogInfo() 

148 

149 info.SetName("Tatlin") 

150 info.SetVersion("v%s" % version) 

151 info.SetIcon(icon) 

152 info.SetDescription("Gcode and STL viewer for 2D printing") 

153 info.SetCopyright( 

154 "(C) 2010-%s Denis Kobozev" % datetime.strftime(datetime.now(), "%Y") 

155 ) 

156 info.SetWebSite("https://tatlin2d.com/") 

157 info.AddDeveloper("Denis Kobozev <d.v.kobozev@gmail.com>") 

158 info.SetLicence(license) 

159 

160 wx.adv.AboutBox(info)