RadonMetrics/MaintainabilityIndexDialog.py

Wed, 16 Sep 2015 20:07:48 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 16 Sep 2015 20:07:48 +0200
changeset 8
d02708288a22
child 9
7f6e04213998
permissions
-rw-r--r--

Continued implementing the maintainability index stuff.

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

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

"""
Module implementing a dialog to show maintainability indexes.
"""

from __future__ import unicode_literals

try:
    str = unicode       # __IGNORE_EXCEPTION __IGNORE_WARNING__
except NameError:
    pass

import os
import fnmatch

from PyQt5.QtCore import pyqtSlot, qVersion, Qt, QTimer, QLocale
from PyQt5.QtWidgets import (
    QDialog, QDialogButtonBox, QAbstractButton, QHeaderView, QTreeWidgetItem,
    QApplication
)

from .Ui_MaintainabilityIndexDialog import Ui_MaintainabilityIndexDialog
from E5Gui.E5Application import e5App

import Preferences
import Utilities


class MaintainabilityIndexDialog(QDialog, Ui_MaintainabilityIndexDialog):
    """
    Class implementing a dialog to show maintainability indexes.
    """
    def __init__(self, radonService, parent=None):
        """
        Constructor
        
        @param radonService reference to the service
        @type RadonMetricsPlugin
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MaintainabilityIndexDialog, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt.Window)
        
        self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
        self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
        
        self.resultList.headerItem().setText(self.resultList.columnCount(), "")
        
        self.radonService = radonService
        self.radonService.metricsDone.connect(self.__processResult)
        self.radonService.metricsError.connect(self.__processError)
        self.radonService.batchFinished.connect(self.__batchFinished)
        
        self.cancelled = False
        
        self.__project = e5App().getObject("Project")
        self.__locale = QLocale()
        
        self.__fileList = []
        self.filterFrame.setVisible(False)
        
        self.explanationLabel.setText(self.tr(
            "<table>"
            "<tr><td colspan=2><b>Ranking:</b></td></tr>"
            "<tr><td><b>A</b></td><td>score &gt; 19</td></tr>"
            "<tr><td><b>B</b></td><td>9 &lt; score &le; 19</td></tr>"
            "<tr><td><b>C</b></td><td>score &le; 9</td></tr>"
            "</table>"
        ))
    
    def __resizeResultColumns(self):
        """
        Private method to resize the list columns.
        """
        self.resultList.header().resizeSections(QHeaderView.ResizeToContents)
        self.resultList.header().setStretchLastSection(True)
    
    def __createResultItem(self, filename, values):
        """
        Private slot to create a new item in the result list.
        
        @param filename name of the file
        @type str
        @param values values to be displayed
        @type dict
        """
        data = [self.__project.getRelativePath(filename)]
        try:
            data.append(self.__locale.toString(float(values["mi"]), "f", 2))
        except ValueError:
            data.append(values["mi"])
        data.append(values["rank"])
        itm = QTreeWidgetItem(self.resultList, data)
        itm.setTextAlignment(1, Qt.Alignment(Qt.AlignRight))
        itm.setTextAlignment(2, Qt.Alignment(Qt.AlignHCenter))
    
    def __createErrorItem(self, filename, message):
        """
        Private slot to create a new error item in the result list.
        
        @param filename name of the file
        @type str
        @param message error message
        @type str
        """
        itm = QTreeWidgetItem(self.resultList, [
            "{0} ({1})".format(self.__project.getRelativePath(filename),
                               message)])
        itm.setFirstColumnSpanned(True)
        font = itm.font(0)
        font.setItalic(True)
        itm.setFont(0, font)
    
    def prepare(self, fileList, project):
        """
        Public method to prepare the dialog with a list of filenames.
        
        @param fileList list of filenames
        @type list of str
        @param project reference to the project object
        @type Project
        """
        self.__fileList = fileList[:]
        self.__project = project
        
        self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
        self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
        self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
        
        self.filterFrame.setVisible(True)
        
        self.__data = self.__project.getData(
            "OTHERTOOLSPARMS", "RadonCodeMetrics")
        if self.__data is None or "ExcludeFiles" not in self.__data:
            self.__data = {"ExcludeFiles": ""}
        self.excludeFilesEdit.setText(self.__data["ExcludeFiles"])
    
    def start(self, fn):
        """
        Public slot to start the maintainability index determination.
        
        @param fn file or list of files or directory to show
            the maintainability index for
        @type str or list of str
        """
        self.cancelled = False
        self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
        self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True)
        self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
        QApplication.processEvents()
        
        if isinstance(fn, list):
            self.files = fn
        elif os.path.isdir(fn):
            self.files = []
            extensions = set(Preferences.getPython("PythonExtensions") +
                             Preferences.getPython("Python3Extensions"))
            for ext in extensions:
                self.files.extend(
                    Utilities.direntries(fn, True, '*{0}'.format(ext), 0))
        else:
            self.files = [fn]
        self.files.sort()
        # check for missing files
        for f in self.files[:]:
            if not os.path.exists(f):
                self.files.remove(f)
        
        self.__summary = {"files": 0}
        for key in ['loc', 'sloc', 'lloc', 'comments', 'multi', 'blank']:
            self.__summary[key] = 0
        
        if len(self.files) > 0:
            # disable updates of the list for speed
            self.resultList.setUpdatesEnabled(False)
            self.resultList.setSortingEnabled(False)
            
            self.checkProgress.setMaximum(len(self.files))
            self.checkProgress.setVisible(len(self.files) > 1)
            self.checkProgressLabel.setVisible(len(self.files) > 1)
            QApplication.processEvents()
            
            # now go through all the files
            self.progress = 0
            if len(self.files) == 1 or not self.radonService.hasBatch:
                self.__batch = False
                self.maintainabilityIndex()
            else:
                self.__batch = True
                self.maintainabilityIndexBatch()

eric ide

mercurial