Preferences/ConfigurationPages/EditorHighlightingStylesPage.py

branch
sub_styles
changeset 6860
50069cc0ee49
parent 6855
4d80c8cc99a1
child 6862
6f4237ccf576
equal deleted inserted replaced
6859:951aa89d367d 6860:50069cc0ee49
128 self.styleGroup.setEnabled(True) 128 self.styleGroup.setEnabled(True)
129 for description, styleNo, subStyleNo in self.lexer.getStyles(): 129 for description, styleNo, subStyleNo in self.lexer.getStyles():
130 itm = QListWidgetItem(description, self.styleElementList) 130 itm = QListWidgetItem(description, self.styleElementList)
131 itm.setData(self.StyleRole, styleNo) 131 itm.setData(self.StyleRole, styleNo)
132 itm.setData(self.SubstyleRole, subStyleNo) 132 itm.setData(self.SubstyleRole, subStyleNo)
133 # TODO: add substyle handling
134 self.__styleAllItems() 133 self.__styleAllItems()
135 self.styleElementList.setCurrentRow(0) 134 self.styleElementList.setCurrentRow(0)
136
137 # TODO: remove this once done
138 def __styleForRow(self, row):
139 """
140 Private method to get the style number of the item of a given row.
141
142 @param row row number
143 @type int
144 @return style number
145 @rtype int
146 """
147 itm = self.styleElementList.item(row)
148 return itm.data(self.StyleRole)
149 135
150 def __stylesForRow(self, row): 136 def __stylesForRow(self, row):
151 """ 137 """
152 Private method to get the style and sub-style number of the item of 138 Private method to get the style and sub-style number of the item of
153 a given row. 139 a given row.
154 140
155 @param row row number 141 @param row row number
156 @type int 142 @type int
157 @return tuple containing the style and sub-style numbers 143 @return tuple containing the style and sub-style numbers
158 @rtype tuple of (int,int) 144 @rtype tuple of (int, int)
159 """ 145 """
160 itm = self.styleElementList.item(row) 146 itm = self.styleElementList.item(row)
147 return self.__stylesForItem(itm)
148
149 def __stylesForItem(self, itm):
150 """
151 Private method to get the style and sub-style number of the given item.
152
153 @param itm reference to the item to extract the styles from
154 @type QListWidgetItem
155 @return tuple containing the style and sub-style numbers
156 @rtype tuple of (int, int)
157 """
161 style = itm.data(self.StyleRole) 158 style = itm.data(self.StyleRole)
162 substyle = itm.data(self.SubstyleRole) 159 substyle = itm.data(self.SubstyleRole)
163 160
164 return (style, substyle) 161 return (style, substyle)
162
163 def __currentStyles(self):
164 """
165 Private method to get the styles of the current item.
166
167 @return tuple containing the style and sub-style numbers
168 @rtype tuple of (int, int)
169 """
170 itm = self.styleElementList.currentItem()
171 if itm is None:
172 styles = (0, -1) # return default style
173 else:
174 styles = self.__stylesForItem(itm)
175
176 return styles
177
178 def __styleOneItem(self, item, style, substyle):
179 """
180 Private method to style one item of the style element list.
181
182 @param item reference to the item to be styled
183 @type QListWidgetItem
184 @param style base style number
185 @type int
186 @param substyle sub-style number
187 @type int
188 """
189 colour = self.lexer.color(style, substyle)
190 paper = self.lexer.paper(style, substyle)
191 font = self.lexer.font(style, substyle)
192 eolfill = self.lexer.eolFill(style, substyle)
193
194 item.setFont(font)
195 item.setBackground(paper)
196 item.setForeground(colour)
197 if eolfill:
198 item.setCheckState(Qt.Checked)
199 else:
200 item.setCheckState(Qt.Unchecked)
165 201
166 def __styleAllItems(self): 202 def __styleAllItems(self):
167 """ 203 """
168 Private method to style all items of the style element list. 204 Private method to style all items of the style element list.
169 """ 205 """
170 for row in range(self.styleElementList.count()): 206 for row in range(self.styleElementList.count()):
171 style, substyle = self.__stylesForRow(row) 207 style, substyle = self.__stylesForRow(row)
172 colour = self.lexer.color(style, substyle)
173 paper = self.lexer.paper(style, substyle)
174 font = self.lexer.font(style, substyle)
175 eolfill = self.lexer.eolFill(style, substyle)
176
177 itm = self.styleElementList.item(row) 208 itm = self.styleElementList.item(row)
178 itm.setFont(font) 209 self.__styleOneItem(itm, style, substyle)
179 itm.setBackground(paper) 210
180 itm.setForeground(colour) 211 def on_styleElementList_currentItemChanged(self, current, previous):
181 if eolfill:
182 itm.setCheckState(Qt.Checked)
183 else:
184 itm.setCheckState(Qt.Unchecked)
185
186 def on_styleElementList_currentRowChanged(self, row):
187 """ 212 """
188 Private method to handle a change of the current row. 213 Private method to handle a change of the current row.
189 214
190 @param row current row number 215 @param current reference to the current item
191 @type int 216 @type QListWidgetItem
192 """ 217 @param previous reference to the previous item
193 if row < 0: 218 @type QListWidgetItem
194 return 219 """
195 220 if current is None:
196 self.style = self.__styleForRow(row) # TODO: get rid of self.style 221 return
197 colour = self.lexer.color(self.style) 222
198 paper = self.lexer.paper(self.style) 223 style, substyle = self.__stylesForItem(current)
199 eolfill = self.lexer.eolFill(self.style) 224 colour = self.lexer.color(style, substyle)
200 font = self.lexer.font(self.style) 225 paper = self.lexer.paper(style, substyle)
226 eolfill = self.lexer.eolFill(style, substyle)
227 font = self.lexer.font(style, substyle)
201 228
202 self.sampleText.setFont(font) 229 self.sampleText.setFont(font)
203 pl = self.sampleText.palette() 230 pl = self.sampleText.palette()
204 pl.setColor(QPalette.Text, colour) 231 pl.setColor(QPalette.Text, colour)
205 pl.setColor(QPalette.Base, paper) 232 pl.setColor(QPalette.Base, paper)
211 def on_foregroundButton_clicked(self): 238 def on_foregroundButton_clicked(self):
212 """ 239 """
213 Private method used to select the foreground colour of the selected 240 Private method used to select the foreground colour of the selected
214 style and lexer. 241 style and lexer.
215 """ 242 """
216 colour = QColorDialog.getColor(self.lexer.color(self.style)) 243 style, substyle = self.__currentStyles()
244 colour = QColorDialog.getColor(self.lexer.color(style, substyle))
217 if colour.isValid(): 245 if colour.isValid():
218 pl = self.sampleText.palette() 246 pl = self.sampleText.palette()
219 pl.setColor(QPalette.Text, colour) 247 pl.setColor(QPalette.Text, colour)
220 self.sampleText.setPalette(pl) 248 self.sampleText.setPalette(pl)
221 self.sampleText.repaint() 249 self.sampleText.repaint()
222 if len(self.styleElementList.selectedItems()) > 1: 250 for selItem in self.styleElementList.selectedItems():
223 for selItem in self.styleElementList.selectedItems(): 251 style, substyle = self.__stylesForItem(selItem)
224 style = self.__styleForRow( 252 self.lexer.setColor(colour, style, substyle)
225 self.styleElementList.row(selItem)) 253 selItem.setForeground(colour)
226 self.lexer.setColor(colour, style)
227 selItem.setForeground(colour)
228 else:
229 self.lexer.setColor(colour, self.style)
230 self.styleElementList.currentItem().setForeground(colour)
231 254
232 @pyqtSlot() 255 @pyqtSlot()
233 def on_backgroundButton_clicked(self): 256 def on_backgroundButton_clicked(self):
234 """ 257 """
235 Private method used to select the background colour of the selected 258 Private method used to select the background colour of the selected
236 style and lexer. 259 style and lexer.
237 """ 260 """
238 colour = QColorDialog.getColor(self.lexer.paper(self.style)) 261 style, substyle = self.__currentStyles()
262 colour = QColorDialog.getColor(self.lexer.paper(style, substyle))
239 if colour.isValid(): 263 if colour.isValid():
240 pl = self.sampleText.palette() 264 pl = self.sampleText.palette()
241 pl.setColor(QPalette.Base, colour) 265 pl.setColor(QPalette.Base, colour)
242 self.sampleText.setPalette(pl) 266 self.sampleText.setPalette(pl)
243 self.sampleText.repaint() 267 self.sampleText.repaint()
244 if len(self.styleElementList.selectedItems()) > 1: 268 for selItem in self.styleElementList.selectedItems():
245 for selItem in self.styleElementList.selectedItems(): 269 style, substyle = self.__stylesForItem(selItem)
246 style = self.__styleForRow( 270 self.lexer.setPaper(colour, style, substyle)
247 self.styleElementList.row(selItem)) 271 selItem.setBackground(colour)
248 self.lexer.setPaper(colour, style)
249 selItem.setBackground(colour)
250 else:
251 self.lexer.setPaper(colour, self.style)
252 self.styleElementList.currentItem().setBackground(colour)
253 272
254 @pyqtSlot() 273 @pyqtSlot()
255 def on_allBackgroundColoursButton_clicked(self): 274 def on_allBackgroundColoursButton_clicked(self):
256 """ 275 """
257 Private method used to select the background colour of all styles of a 276 Private method used to select the background colour of all styles of a
258 selected lexer. 277 selected lexer.
259 """ 278 """
260 colour = QColorDialog.getColor(self.lexer.paper(self.style)) 279 style, substyle = self.__currentStyles()
280 colour = QColorDialog.getColor(self.lexer.paper(style, substyle))
261 if colour.isValid(): 281 if colour.isValid():
262 pl = self.sampleText.palette() 282 pl = self.sampleText.palette()
263 pl.setColor(QPalette.Base, colour) 283 pl.setColor(QPalette.Base, colour)
264 self.sampleText.setPalette(pl) 284 self.sampleText.setPalette(pl)
265 self.sampleText.repaint() 285 self.sampleText.repaint()
266 for row in range(self.styleElementList.count()): 286 for row in range(self.styleElementList.count()):
267 style = self.__styleForRow(row) 287 style, substyle = self.__stylesForRow(row)
268 self.lexer.setPaper(colour, style) 288 self.lexer.setPaper(colour, style, substyle)
269 self.__styleAllItems() 289 self.__styleAllItems()
270 290
271 def __changeFont(self, doAll, familyOnly, sizeOnly): 291 def __changeFont(self, doAll, familyOnly, sizeOnly):
272 """ 292 """
273 Private slot to change the highlighter font. 293 Private slot to change the highlighter font.
275 @param doAll flag indicating to change the font for all styles 295 @param doAll flag indicating to change the font for all styles
276 (boolean) 296 (boolean)
277 @param familyOnly flag indicating to set the font family only (boolean) 297 @param familyOnly flag indicating to set the font family only (boolean)
278 @param sizeOnly flag indicating to set the font size only (boolean 298 @param sizeOnly flag indicating to set the font size only (boolean
279 """ 299 """
280 def setFont(font, style, familyOnly, sizeOnly): 300 def setFont(font, style, substyle, familyOnly, sizeOnly):
281 """ 301 """
282 Local function to set the font. 302 Local function to set the font.
283 303
284 @param font font to be set (QFont) 304 @param font font to be set
285 @param style style to set the font for (integer) 305 @type QFont
306 @param style style number
307 @type int
308 @param substyle sub-style number
309 @type int
286 @param familyOnly flag indicating to set the font family only 310 @param familyOnly flag indicating to set the font family only
287 (boolean) 311 @type bool
288 @param sizeOnly flag indicating to set the font size only (boolean 312 @param sizeOnly flag indicating to set the font size only
313 @type bool
289 """ 314 """
290 if familyOnly or sizeOnly: 315 if familyOnly or sizeOnly:
291 newFont = QFont(self.lexer.font(style)) 316 newFont = QFont(self.lexer.font(style))
292 if familyOnly: 317 if familyOnly:
293 newFont.setFamily(font.family()) 318 newFont.setFamily(font.family())
294 if sizeOnly: 319 if sizeOnly:
295 newFont.setPointSize(font.pointSize()) 320 newFont.setPointSize(font.pointSize())
296 self.lexer.setFont(newFont, style) 321 self.lexer.setFont(newFont, style, substyle)
297 else: 322 else:
298 self.lexer.setFont(font, style) 323 self.lexer.setFont(font, style, substyle)
299 324
300 def setSampleFont(font, familyOnly, sizeOnly): 325 def setSampleFont(font, familyOnly, sizeOnly):
301 """ 326 """
302 Local function to set the font of the sample text. 327 Local function to set the font of the sample text.
303 328
305 @param familyOnly flag indicating to set the font family only 330 @param familyOnly flag indicating to set the font family only
306 (boolean) 331 (boolean)
307 @param sizeOnly flag indicating to set the font size only (boolean 332 @param sizeOnly flag indicating to set the font size only (boolean
308 """ 333 """
309 if familyOnly or sizeOnly: 334 if familyOnly or sizeOnly:
310 newFont = QFont(self.lexer.font(self.style)) 335 style, substyle = self.__currentStyles()
336 newFont = QFont(self.lexer.font(style, substyle))
311 if familyOnly: 337 if familyOnly:
312 newFont.setFamily(font.family()) 338 newFont.setFamily(font.family())
313 if sizeOnly: 339 if sizeOnly:
314 newFont.setPointSize(font.pointSize()) 340 newFont.setPointSize(font.pointSize())
315 self.sampleText.setFont(newFont) 341 self.sampleText.setFont(newFont)
316 else: 342 else:
317 self.sampleText.setFont(font) 343 self.sampleText.setFont(font)
318 344
345 style, substyle = self.__currentStyles()
319 if self.monospacedButton.isChecked(): 346 if self.monospacedButton.isChecked():
320 options = MonospacedFontsOption 347 options = MonospacedFontsOption
321 else: 348 else:
322 options = NoFontsOption 349 options = NoFontsOption
323 font, ok = QFontDialog.getFont(self.lexer.font(self.style), self, 350 font, ok = QFontDialog.getFont(self.lexer.font(style, substyle), self,
324 "", options) 351 "", options)
325 if ok: 352 if ok:
326 setSampleFont(font, familyOnly, sizeOnly) 353 setSampleFont(font, familyOnly, sizeOnly)
327 if doAll: 354 if doAll:
328 for row in range(self.styleElementList.count()): 355 for row in range(self.styleElementList.count()):
329 style = self.__styleForRow(row) 356 style, substyle = self.__stylesForRow(row)
330 setFont(font, style, familyOnly, sizeOnly) 357 setFont(font, style, substyle, familyOnly, sizeOnly)
331 self.__styleAllItems() 358 self.__styleAllItems()
332 elif len(self.styleElementList.selectedItems()) > 1: 359 else:
333 for selItem in self.styleElementList.selectedItems(): 360 for selItem in self.styleElementList.selectedItems():
334 style = self.__styleForRow( 361 style, substyle = self.__stylesForItem(selItem)
335 self.styleElementList.row(selItem)) 362 setFont(font, style, substyle, familyOnly, sizeOnly)
336 setFont(font, style, familyOnly, sizeOnly) 363 itmFont = self.lexer.font(style, substyle)
337 itmFont = self.lexer.font(style)
338 selItem.setFont(itmFont) 364 selItem.setFont(itmFont)
339 else:
340 setFont(font, self.style, familyOnly, sizeOnly)
341 itmFont = self.lexer.font(self.style)
342 self.styleElementList.currentItem().setFont(itmFont)
343 365
344 def __fontButtonMenuTriggered(self, act): 366 def __fontButtonMenuTriggered(self, act):
345 """ 367 """
346 Private slot used to select the font of the selected style and lexer. 368 Private slot used to select the font of the selected style and lexer.
347 369
372 Private method used to set the eolfill for the selected style and 394 Private method used to set the eolfill for the selected style and
373 lexer. 395 lexer.
374 396
375 @param on flag indicating enabled or disabled state (boolean) 397 @param on flag indicating enabled or disabled state (boolean)
376 """ 398 """
399 style, substyle = self.__currentStyles()
377 checkState = Qt.Checked if on else Qt.Unchecked 400 checkState = Qt.Checked if on else Qt.Unchecked
378 if len(self.styleElementList.selectedItems()) > 1: 401 for selItem in self.styleElementList.selectedItems():
379 for selItem in self.styleElementList.selectedItems(): 402 style, substyle = self.__stylesForItem(selItem)
380 style = self.__styleForRow( 403 self.lexer.setEolFill(on, style, substyle)
381 self.styleElementList.row(selItem)) 404 selItem.setCheckState(checkState)
382 self.lexer.setEolFill(on, style)
383 selItem.setCheckState(checkState)
384 else:
385 self.lexer.setEolFill(on, self.style)
386 self.styleElementList.currentItem().setCheckState(checkState)
387 405
388 @pyqtSlot() 406 @pyqtSlot()
389 def on_allEolFillButton_clicked(self): 407 def on_allEolFillButton_clicked(self):
390 """ 408 """
391 Private method used to set the eolfill for all styles of a selected 409 Private method used to set the eolfill for all styles of a selected
401 0, False) 419 0, False)
402 if ok: 420 if ok:
403 enabled = selection == on 421 enabled = selection == on
404 self.eolfillCheckBox.setChecked(enabled) 422 self.eolfillCheckBox.setChecked(enabled)
405 for row in range(self.styleElementList.count()): 423 for row in range(self.styleElementList.count()):
406 style = self.__styleForRow(row) 424 style, substyle = self.__stylesForRow(row)
407 self.lexer.setEolFill(enabled, style) 425 self.lexer.setEolFill(enabled, style, substyle)
408 self.__styleAllItems() 426 self.__styleAllItems()
409 427
410 @pyqtSlot() 428 @pyqtSlot()
411 def on_defaultButton_clicked(self): 429 def on_defaultButton_clicked(self):
412 """ 430 """
413 Private method to set the current style to its default values. 431 Private method to set the current style to its default values.
414 """ 432 """
415 if len(self.styleElementList.selectedItems()) > 1: 433 for selItem in self.styleElementList.selectedItems():
416 for selItem in self.styleElementList.selectedItems(): 434 style, substyle = self.__stylesForItem(selItem)
417 style = self.__styleForRow( 435 self.__setToDefault(style, substyle)
418 self.styleElementList.row(selItem)) 436 self.on_styleElementList_currentItemChanged(
419 self.__setToDefault(style) 437 self.styleElementList.currentItem(), None)
420 else:
421 self.__setToDefault(self.style)
422 self.on_styleElementList_currentRowChanged(
423 self.styleElementList.currentRow())
424 self.__styleAllItems() 438 self.__styleAllItems()
425 439
426 @pyqtSlot() 440 @pyqtSlot()
427 def on_allDefaultButton_clicked(self): 441 def on_allDefaultButton_clicked(self):
428 """ 442 """
429 Private method to set all styles to their default values. 443 Private method to set all styles to their default values.
430 """ 444 """
431 for row in range(self.styleElementList.count()): 445 for row in range(self.styleElementList.count()):
432 style = self.__styleForRow(row) 446 style, substyle = self.__stylesForRow(row)
433 self.__setToDefault(style) 447 self.__setToDefault(style, substyle)
434 self.on_styleElementList_currentRowChanged( 448 self.on_styleElementList_currentItemChanged(
435 self.styleElementList.currentRow()) 449 self.styleElementList.currentItem(), None)
436 self.__styleAllItems() 450 self.__styleAllItems()
437 451
438 def __setToDefault(self, style): 452 def __setToDefault(self, style, substyle):
439 """ 453 """
440 Private method to set a specific style to its default values. 454 Private method to set a specific style to its default values.
441 455
442 @param style style to be reset (integer) 456 @param style style number
443 """ 457 @type int
444 self.lexer.setColor(self.lexer.defaultColor(style), style) 458 @param substyle sub-style number
445 self.lexer.setPaper(self.lexer.defaultPaper(style), style) 459 @type int
446 self.lexer.setFont(self.lexer.defaultFont(style), style) 460 """
447 self.lexer.setEolFill(self.lexer.defaultEolFill(style), style) 461 self.lexer.setColor(self.lexer.defaultColor(style, substyle),
462 style, substyle)
463 self.lexer.setPaper(self.lexer.defaultPaper(style, substyle),
464 style, substyle)
465 self.lexer.setFont(self.lexer.defaultFont(style, substyle),
466 style, substyle)
467 self.lexer.setEolFill(self.lexer.defaultEolFill(style, substyle),
468 style, substyle)
448 469
449 @pyqtSlot() 470 @pyqtSlot()
450 def on_importCurrentButton_clicked(self): 471 def on_importCurrentButton_clicked(self):
451 """ 472 """
452 Private slot to import the styles of the current lexer. 473 Private slot to import the styles of the current lexer.
498 fn += ex 519 fn += ex
499 520
500 f = QFile(fn) 521 f = QFile(fn)
501 if f.open(QIODevice.WriteOnly): 522 if f.open(QIODevice.WriteOnly):
502 from E5XML.HighlightingStylesWriter import HighlightingStylesWriter 523 from E5XML.HighlightingStylesWriter import HighlightingStylesWriter
524 # TODO: sub-styles
503 HighlightingStylesWriter(f, lexers).writeXML() 525 HighlightingStylesWriter(f, lexers).writeXML()
504 f.close() 526 f.close()
505 else: 527 else:
506 E5MessageBox.critical( 528 E5MessageBox.critical(
507 self, 529 self,
532 return 554 return
533 555
534 f = QFile(fn) 556 f = QFile(fn)
535 if f.open(QIODevice.ReadOnly): 557 if f.open(QIODevice.ReadOnly):
536 from E5XML.HighlightingStylesReader import HighlightingStylesReader 558 from E5XML.HighlightingStylesReader import HighlightingStylesReader
559 # TODO: sub-styles
537 reader = HighlightingStylesReader(f, lexers) 560 reader = HighlightingStylesReader(f, lexers)
538 reader.readXML() 561 reader.readXML()
539 f.close() 562 f.close()
540 else: 563 else:
541 E5MessageBox.critical( 564 E5MessageBox.critical(
547 .format(fn, f.errorString()) 570 .format(fn, f.errorString())
548 ) 571 )
549 return 572 return
550 573
551 if self.lexer: 574 if self.lexer:
552 colour = self.lexer.color(self.style) 575 style, substyle = self.__currentStyles()
553 paper = self.lexer.paper(self.style) 576 colour = self.lexer.color(style, substyle)
554 eolfill = self.lexer.eolFill(self.style) 577 paper = self.lexer.paper(style, substyle)
555 font = self.lexer.font(self.style) 578 eolfill = self.lexer.eolFill(style, substyle)
579 font = self.lexer.font(style, substyle)
556 580
557 self.sampleText.setFont(font) 581 self.sampleText.setFont(font)
558 pl = self.sampleText.palette() 582 pl = self.sampleText.palette()
559 pl.setColor(QPalette.Text, colour) 583 pl.setColor(QPalette.Text, colour)
560 pl.setColor(QPalette.Base, paper) 584 pl.setColor(QPalette.Base, paper)

eric ide

mercurial