|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2022 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 PyQt6.QtCore import Qt |
|
13 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem |
|
14 |
|
15 from .Ui_CodeStyleCodeSelectionDialog import Ui_CodeStyleCodeSelectionDialog |
|
16 |
|
17 from .translations import getMessageCodes, getTranslatedMessage |
|
18 |
|
19 from . import CodeStyleCheckerUtilities |
|
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 msgCode in sorted(selectableCodes): |
|
62 message = getTranslatedMessage(msgCode, [], example=True) |
|
63 if message is None: |
|
64 # try with extension |
|
65 for ext in ("L", "M", "H", "1"): |
|
66 message = getTranslatedMessage( |
|
67 "{0}.{1}".format(msgCode, ext), [], example=True |
|
68 ) |
|
69 if message is not None: |
|
70 break |
|
71 else: |
|
72 continue |
|
73 itm = QTreeWidgetItem(self.codeTable, [ |
|
74 msgCode, "\n".join(textWrapper.wrap(message))]) |
|
75 CodeStyleCheckerUtilities.setItemIcon(itm, 0, msgCode) |
|
76 itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable) |
|
77 if msgCode in codeList: |
|
78 itm.setCheckState(0, Qt.CheckState.Checked) |
|
79 codeList.remove(msgCode) |
|
80 else: |
|
81 itm.setCheckState(0, Qt.CheckState.Unchecked) |
|
82 self.codeTable.resizeColumnToContents(0) |
|
83 self.codeTable.resizeColumnToContents(1) |
|
84 self.codeTable.header().setStretchLastSection(True) |
|
85 |
|
86 self.__extraCodes = codeList[:] |
|
87 |
|
88 def getSelectedCodes(self): |
|
89 """ |
|
90 Public method to get a comma separated list of codes selected. |
|
91 |
|
92 @return comma separated list of selected codes |
|
93 @rtype str |
|
94 """ |
|
95 selectedCodes = [] |
|
96 |
|
97 for index in range(self.codeTable.topLevelItemCount()): |
|
98 itm = self.codeTable.topLevelItem(index) |
|
99 if itm.checkState(0) == Qt.CheckState.Checked: |
|
100 selectedCodes.append(itm.text(0)) |
|
101 |
|
102 return ", ".join(self.__extraCodes + selectedCodes) |