|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select code style message codes. |
|
8 """ |
|
9 |
|
10 import textwrap |
|
11 |
|
12 from PyQt5.QtCore import Qt |
|
13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem |
|
14 |
|
15 from .Ui_CodeStyleCodeSelectionDialog import Ui_CodeStyleCodeSelectionDialog |
|
16 |
|
17 from .translations import getMessageCodes, getTranslatedMessage |
|
18 |
|
19 import UI.PixmapCache |
|
20 |
|
21 |
|
22 class CodeStyleCodeSelectionDialog(QDialog, Ui_CodeStyleCodeSelectionDialog): |
|
23 """ |
|
24 Class implementing a dialog to select code style message codes. |
|
25 """ |
|
26 def __init__(self, codes, categories, showFixCodes, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param codes comma separated list of selected codes |
|
31 @type str |
|
32 @param categories list of message categories to omit |
|
33 @type list of str |
|
34 @param showFixCodes flag indicating to show a list of fixable |
|
35 issues |
|
36 @type bool |
|
37 @param parent reference to the parent widget |
|
38 @type QWidget |
|
39 """ |
|
40 super().__init__(parent) |
|
41 self.setupUi(self) |
|
42 |
|
43 textWrapper = textwrap.TextWrapper(width=60) |
|
44 |
|
45 self.codeTable.headerItem().setText(self.codeTable.columnCount(), "") |
|
46 codeList = [code.strip() for code in codes.split(",") if code.strip()] |
|
47 if categories: |
|
48 codeList = [code for code in codeList if code[0] not in categories] |
|
49 |
|
50 if showFixCodes: |
|
51 from .CodeStyleFixer import FixableCodeStyleIssues |
|
52 selectableCodes = FixableCodeStyleIssues |
|
53 else: |
|
54 selectableCodes = ( |
|
55 [x for x in getMessageCodes() if not x.startswith('FIX')] |
|
56 ) |
|
57 if categories: |
|
58 # filter by category |
|
59 selectableCodes = [x for x in selectableCodes |
|
60 if x[0] not in categories] |
|
61 for code in sorted(selectableCodes): |
|
62 message = getTranslatedMessage(code, [], example=True) |
|
63 if message is None: |
|
64 # try with extension |
|
65 for ext in ("L", "M", "H", "1"): |
|
66 message = getTranslatedMessage("{0}.{1}".format(code, ext), |
|
67 [], example=True) |
|
68 if message is not None: |
|
69 break |
|
70 else: |
|
71 continue |
|
72 itm = QTreeWidgetItem(self.codeTable, [ |
|
73 code, "\n".join(textWrapper.wrap(message))]) |
|
74 if code.startswith(("W", "C", "M")): |
|
75 itm.setIcon(0, UI.PixmapCache.getIcon("warning")) |
|
76 elif code.startswith("E"): |
|
77 itm.setIcon(0, UI.PixmapCache.getIcon("syntaxError")) |
|
78 elif code.startswith(("A", "N")): |
|
79 itm.setIcon(0, UI.PixmapCache.getIcon("namingError")) |
|
80 elif code.startswith("D"): |
|
81 itm.setIcon(0, UI.PixmapCache.getIcon("docstringError")) |
|
82 elif code.startswith("S"): |
|
83 itm.setIcon(0, UI.PixmapCache.getIcon("securityLow")) |
|
84 elif code.startswith("P"): |
|
85 itm.setIcon(0, UI.PixmapCache.getIcon("dirClosed")) |
|
86 elif code.startswith("Y"): |
|
87 itm.setIcon(0, UI.PixmapCache.getIcon("filePython")) |
|
88 else: |
|
89 # unknown category prefix => warning |
|
90 itm.setIcon(0, UI.PixmapCache.getIcon("warning")) |
|
91 itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable) |
|
92 if code in codeList: |
|
93 itm.setCheckState(0, Qt.CheckState.Checked) |
|
94 codeList.remove(code) |
|
95 else: |
|
96 itm.setCheckState(0, Qt.CheckState.Unchecked) |
|
97 self.codeTable.resizeColumnToContents(0) |
|
98 self.codeTable.resizeColumnToContents(1) |
|
99 self.codeTable.header().setStretchLastSection(True) |
|
100 |
|
101 self.__extraCodes = codeList[:] |
|
102 |
|
103 def getSelectedCodes(self): |
|
104 """ |
|
105 Public method to get a comma separated list of codes selected. |
|
106 |
|
107 @return comma separated list of selected codes |
|
108 @rtype str |
|
109 """ |
|
110 selectedCodes = [] |
|
111 |
|
112 for index in range(self.codeTable.topLevelItemCount()): |
|
113 itm = self.codeTable.topLevelItem(index) |
|
114 if itm.checkState(0) == Qt.CheckState.Checked: |
|
115 selectedCodes.append(itm.text(0)) |
|
116 |
|
117 return ", ".join(self.__extraCodes + selectedCodes) |