Coverage for pyilper/penconfig.py: 89%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

133 statements  

1#!/usr/bin/python3 

2# -*- coding: utf-8 -*- 

3# penconfig for pyILPER 

4# 

5# (c) 2016 Joachim Siebold 

6# 

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

8# modify it under the terms of the GNU General Public License 

9# as published by the Free Software Foundation; either version 2 

10# of the License, or (at your option) any later version. 

11# 

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

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

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

15# GNU General Public License for more details. 

16# 

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

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

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

20# 

21# penconfig class ------------------------------------------- 

22# 

23# Changelog 

24# 17.10.2016 jsi: 

25# - first version (merged) 

26# 12.02.2018 jsi: 

27# - added the clean parameter to the open method 

28# 10.08.2018 jsi: 

29# - cls_PenConfigWindow moved from pilplotter.py 

30# 12.12.2021 jsi: 

31# - add configversion parameter to open method 

32# 

33import copy 

34from PySide6 import QtCore, QtGui, QtWidgets 

35from .userconfig import cls_userconfig, ConfigError 

36# 

37# Plotter pen table model class -------------------------------------------- 

38# 

39class PenTableModel(QtCore.QAbstractTableModel): 

40 def __init__(self, datain, parent = None): 

41 super().__init__() 

42 self.arraydata = datain 

43 

44 def rowCount(self, parent): 

45 return len(self.arraydata) 

46 

47 def columnCount(self, parent): 

48 return len(self.arraydata[0]) 

49 

50 def data(self, index, role): 

51 if not index.isValid(): 

52 return None 

53 elif role != QtCore.Qt.DisplayRole: 

54 return None 

55 return (self.arraydata[index.row()][index.column()]) 

56 

57 def setData(self, index, value,role): 

58 if index.column()==0: 

59 self.arraydata[index.row()][index.column()] = value 

60 else: 

61 self.arraydata[index.row()][index.column()] = int(value) 

62 self.dataChanged.emit(index,index) # this updates the edited cell 

63 return True 

64 

65 def flags(self, index): 

66 return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable 

67 

68 def headerData(self,section,orientation,role): 

69 if role != QtCore.Qt.DisplayRole: 

70 return None 

71 if (orientation == QtCore.Qt.Horizontal): 

72 if section==0: 

73 return("Description") 

74 elif section==1: 

75 return("R") 

76 elif section==2: 

77 return("G") 

78 elif section==3: 

79 return("B") 

80 elif section==3: 

81 return("A") 

82 elif section==4: 

83 return("Alpha") 

84 elif section==5: 

85 return("Width") 

86 else: 

87 return("") 

88 

89 def getTable(self): 

90 return self.arraydata 

91 

92 def setAll(self,penconfig): 

93 self.arraydata=penconfig 

94 self.layoutChanged.emit() # this updates all cells 

95 

96# 

97# Custom class with input validators --------------------------------------- 

98# 

99class PenDelegate(QtWidgets.QItemDelegate): 

100 

101 def createEditor(self, parent, option, index): 

102 editor= super(PenDelegate,self).createEditor(parent,option,index) 

103 if index.column() > 0 and index.column()< 5: 

104 editor.setValidator(QtGui.QIntValidator(0,255)) 

105 elif index.column() == 5: 

106 editor.setValidator(QtGui.QDoubleValidator(0.0,5.0,1)) 

107 return(editor) 

108 

109 def setEditorData(self, editor, index): 

110 # Gets display text if edit data hasn't been set. 

111 text = index.data(QtCore.Qt.EditRole) or index.data(QtCore.Qt.DisplayRole) 

112 editor.setText(str(text)) 

113 

114# 

115# Plotter pen configuration class ----------------------------------- 

116# 

117class cls_PenConfigWindow(QtWidgets.QDialog): 

118 

119 def __init__(self): 

120 super().__init__() 

121 self.setWindowTitle('Plotter pen config') 

122 self.vlayout = QtWidgets.QVBoxLayout() 

123# 

124# table widget 

125# 

126 self.tablemodel=PenTableModel(PENCONFIG.get_all()) 

127 self.tableview= QtWidgets.QTableView() 

128 self.tableview.setModel(self.tablemodel) 

129 self.tableview.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) 

130 self.tableview.verticalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) 

131 self.delegate= PenDelegate() 

132 self.tableview.setItemDelegate(self.delegate) 

133 self.vlayout.addWidget(self.tableview) 

134# 

135# ok/cancel button box 

136#  

137 self.buttonBox = QtWidgets.QDialogButtonBox() 

138 self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Reset| QtWidgets.QDialogButtonBox.Ok) 

139 self.buttonBox.setCenterButtons(True) 

140 self.buttonBox.accepted.connect(self.do_ok) 

141 self.buttonBox.rejected.connect(self.do_cancel) 

142 self.buttonBox.button(QtWidgets.QDialogButtonBox.Reset).clicked.connect(self.do_reset) 

143 self.vlayout.addWidget(self.buttonBox) 

144 self.setLayout(self.vlayout) 

145 

146 def do_ok(self): 

147 PENCONFIG.set_all(self.tablemodel.getTable()) 

148 super().accept() 

149 

150 def do_cancel(self): 

151 super().reject() 

152# 

153# reset populates table with the default configuration 

154# 

155 def do_reset(self): 

156 self.tablemodel.setAll(PENCONFIG.default_config()) 

157 

158 @staticmethod 

159 def getPenConfig(): 

160 dialog= cls_PenConfigWindow() 

161 dialog.resize(650,600) 

162 result= dialog.exec() 

163 if result== QtWidgets.QDialog.Accepted: 

164 return True 

165 else: 

166 return False 

167class PenConfigError(Exception): 

168 def __init__(self,msg,add_msg= None): 

169 self.msg= msg 

170 self.add_msg = add_msg 

171 

172 

173class cls_penconfig: 

174# 

175# initialize: set penconfig to the default configuration 

176# 

177 def __init__(self): 

178 self.__penconfig__= None 

179 self.__userconfig__ = None 

180 return 

181# 

182# default config 

183# 

184 def default_config(self): 

185 return [ 

186 ["Black 0.3 mm", 0x00,0x00,0x00,0xff,1.0], 

187 ["Red 0.3 mm", 0xff,0x00,0x00,0xff,1.0], 

188 ["Green 0.3 mm", 0x00,0xff,0x00,0xff,1.0], 

189 ["Blue 0.3 mm", 0x00,0x00,0xff,0xff,1.0], 

190 ["Yellow 0.3 mm", 0xff,0xff,0x00,0xff,1.0], 

191 ["Cyan 0.3 mm", 0x00,0xff,0xff,0xff,1.0], 

192 ["Magenta 0.3 mm", 0xff,0x00,0xff,0xff,1.0], 

193 ["Black 0.7 mm", 0x00,0x00,0x00,0xff,2.0], 

194 ["Red 0.7 mm", 0xff,0x00,0x00,0xff,2.0], 

195 ["Green 0.7 mm", 0x00,0xff,0x00,0xff,2.0], 

196 ["Blue 0.7 mm", 0x00,0x00,0xff,0xff,2.0], 

197 ["Yellow 0.7 mm", 0x00,0xff,0xff,0xff,2.0], 

198 ["Cyan 0.7 mm", 0x00,0xff,0xff,0xff,2.0], 

199 ["Magenta 0.7 mm", 0xff,0x00,0xff,0xff,2.0], 

200 ["Custom1 0.3 mm", 0x00,0x00,0x00,0xff,1.0], 

201 ["Custom2 0.3 mm", 0x00,0x00,0x00,0xff,1.0]] 

202 

203 

204# 

205# open: read in the pen configuration. If the configuration file does not  

206# exist, the default configuration is written to the pen config file 

207# If clean is true do not read an existing config file 

208# 

209 def open(self,name,configversion,instance,production,clean): 

210 self.__userconfig__= cls_userconfig(name,"penconfig",configversion,instance,production) 

211 if clean: 

212 return 

213 try: 

214 self.__penconfig__= self.__userconfig__.read(self.default_config()) 

215 except ConfigError as e: 

216 raise PenConfigError(e.msg,e.add_msg) 

217# 

218# Get the list of pens 

219# 

220 def get_penlist(self): 

221 penlist= [] 

222 for p in self.__penconfig__: 

223 penlist.append(p[0]) 

224 return penlist 

225# 

226# Get the config of the nth pen  

227# 

228 def get_pen(self,n): 

229 return(self.__penconfig__[n][1:]) 

230# 

231# Get the whole table as a copy 

232# 

233 def get_all(self): 

234 return copy.deepcopy(self.__penconfig__) 

235 

236# 

237# Populate the whole table 

238# 

239 def set_all(self,newlist): 

240 self.__penconfig__= [] 

241 self.__penconfig__= copy.deepcopy(newlist) 

242 

243# 

244# Save the penconfig to the configuration file 

245# 

246 def save(self): 

247 try: 

248 self.__userconfig__.write(self.__penconfig__) 

249 except ConfigError as e: 

250 raise PenConfigError(e.msg,e.add_msg) 

251# 

252PENCONFIG= cls_penconfig()