eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCodeSelectionDialog.py

Sun, 12 Apr 2020 19:07:49 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 12 Apr 2020 19:07:49 +0200
changeset 7533
88261c96484b
parent 7360
9190402e4505
child 7609
d5aff4fd0ef8
permissions
-rw-r--r--

Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.

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

# Copyright (c) 2011 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to select code style message codes.
"""


from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QTreeWidgetItem

from .Ui_CodeStyleCodeSelectionDialog import Ui_CodeStyleCodeSelectionDialog

import UI.PixmapCache


class CodeStyleCodeSelectionDialog(QDialog, Ui_CodeStyleCodeSelectionDialog):
    """
    Class implementing a dialog to select code style message codes.
    """
    def __init__(self, codes, showFixCodes, parent=None):
        """
        Constructor
        
        @param codes comma separated list of selected codes (string)
        @param showFixCodes flag indicating to show a list of fixable
            issues (boolean)
        @param parent reference to the parent widget (QWidget)
        """
        super(CodeStyleCodeSelectionDialog, self).__init__(parent)
        self.setupUi(self)
        
        self.codeTable.headerItem().setText(self.codeTable.columnCount(), "")
        codeList = [code.strip() for code in codes.split(",") if code.strip()]
        
        from .translations import _messages, _messages_sample_args
        
        if showFixCodes:
            from .CodeStyleFixer import FixableCodeStyleIssues
            selectableCodes = FixableCodeStyleIssues
        else:
            selectableCodes = [x for x in list(_messages.keys())
                               if not x.startswith('F')]
        for code in sorted(selectableCodes):
            if code in _messages_sample_args:
                message = _messages[code].format(*_messages_sample_args[code])
            elif code in _messages:
                message = _messages[code]
            else:
                continue
            itm = QTreeWidgetItem(self.codeTable, [code, message])
            if code.startswith(("W", "C", "M")):
                itm.setIcon(0, UI.PixmapCache.getIcon("warning"))
            elif code.startswith("E"):
                itm.setIcon(0, UI.PixmapCache.getIcon("syntaxError"))
            elif code.startswith("N"):
                itm.setIcon(0, UI.PixmapCache.getIcon("namingError"))
            elif code.startswith("D"):
                itm.setIcon(0, UI.PixmapCache.getIcon("docstringError"))
            else:
                # unknown category prefix => warning
                itm.setIcon(0, UI.PixmapCache.getIcon("warning"))
            itm.setFlags(itm.flags() | Qt.ItemIsUserCheckable)
            if code in codeList:
                itm.setCheckState(0, Qt.Checked)
                codeList.remove(code)
            else:
                itm.setCheckState(0, Qt.Unchecked)
        self.codeTable.resizeColumnToContents(0)
        self.codeTable.resizeColumnToContents(1)
        self.codeTable.header().setStretchLastSection(True)
        
        self.__extraCodes = codeList[:]
    
    def getSelectedCodes(self):
        """
        Public method to get a comma separated list of codes selected.
        
        @return comma separated list of selected codes (string)
        """
        selectedCodes = []
        
        for index in range(self.codeTable.topLevelItemCount()):
            itm = self.codeTable.topLevelItem(index)
            if itm.checkState(0) == Qt.Checked:
                selectedCodes.append(itm.text(0))
        
        return ", ".join(self.__extraCodes + selectedCodes)

eric ide

mercurial