|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select code style message codes. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import Qt |
|
13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem |
|
14 |
|
15 from .Ui_CodeStyleCodeSelectionDialog import Ui_CodeStyleCodeSelectionDialog |
|
16 |
|
17 import UI.PixmapCache |
|
18 |
|
19 |
|
20 class CodeStyleCodeSelectionDialog(QDialog, Ui_CodeStyleCodeSelectionDialog): |
|
21 """ |
|
22 Class implementing a dialog to select code style message codes. |
|
23 """ |
|
24 def __init__(self, codes, showFixCodes, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param codes comma separated list of selected codes (string) |
|
29 @param showFixCodes flag indicating to show a list of fixable |
|
30 issues (boolean) |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super(CodeStyleCodeSelectionDialog, self).__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.codeTable.headerItem().setText(self.codeTable.columnCount(), "") |
|
37 codeList = [code.strip() for code in codes.split(",") if code.strip()] |
|
38 |
|
39 from .translations import _messages, _messages_sample_args |
|
40 |
|
41 if showFixCodes: |
|
42 from .CodeStyleFixer import FixableCodeStyleIssues |
|
43 selectableCodes = FixableCodeStyleIssues |
|
44 else: |
|
45 selectableCodes = [x for x in list(_messages.keys()) |
|
46 if not x.startswith('F')] |
|
47 for code in sorted(selectableCodes): |
|
48 if code in _messages_sample_args: |
|
49 message = _messages[code].format(*_messages_sample_args[code]) |
|
50 elif code in _messages: |
|
51 message = _messages[code] |
|
52 else: |
|
53 continue |
|
54 itm = QTreeWidgetItem(self.codeTable, [code, message]) |
|
55 if code.startswith(("W", "C", "M")): |
|
56 itm.setIcon(0, UI.PixmapCache.getIcon("warning.png")) |
|
57 elif code.startswith("E"): |
|
58 itm.setIcon(0, UI.PixmapCache.getIcon("syntaxError.png")) |
|
59 elif code.startswith("N"): |
|
60 itm.setIcon(0, UI.PixmapCache.getIcon("namingError.png")) |
|
61 elif code.startswith("D"): |
|
62 itm.setIcon(0, UI.PixmapCache.getIcon("docstringError.png")) |
|
63 itm.setFlags(itm.flags() | Qt.ItemIsUserCheckable) |
|
64 if code in codeList: |
|
65 itm.setCheckState(0, Qt.Checked) |
|
66 codeList.remove(code) |
|
67 else: |
|
68 itm.setCheckState(0, Qt.Unchecked) |
|
69 self.codeTable.resizeColumnToContents(0) |
|
70 self.codeTable.resizeColumnToContents(1) |
|
71 self.codeTable.header().setStretchLastSection(True) |
|
72 |
|
73 self.__extraCodes = codeList[:] |
|
74 |
|
75 def getSelectedCodes(self): |
|
76 """ |
|
77 Public method to get a comma separated list of codes selected. |
|
78 |
|
79 @return comma separated list of selected codes (string) |
|
80 """ |
|
81 selectedCodes = [] |
|
82 |
|
83 for index in range(self.codeTable.topLevelItemCount()): |
|
84 itm = self.codeTable.topLevelItem(index) |
|
85 if itm.checkState(0) == Qt.Checked: |
|
86 selectedCodes.append(itm.text(0)) |
|
87 |
|
88 return ", ".join(self.__extraCodes + selectedCodes) |