RadonMetrics/MaintainabilityIndexDialog.py

changeset 8
d02708288a22
child 9
7f6e04213998
equal deleted inserted replaced
7:3eb5fb0ae72c 8:d02708288a22
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to show maintainability indexes.
8 """
9
10 from __future__ import unicode_literals
11
12 try:
13 str = unicode # __IGNORE_EXCEPTION __IGNORE_WARNING__
14 except NameError:
15 pass
16
17 import os
18 import fnmatch
19
20 from PyQt5.QtCore import pyqtSlot, qVersion, Qt, QTimer, QLocale
21 from PyQt5.QtWidgets import (
22 QDialog, QDialogButtonBox, QAbstractButton, QHeaderView, QTreeWidgetItem,
23 QApplication
24 )
25
26 from .Ui_MaintainabilityIndexDialog import Ui_MaintainabilityIndexDialog
27 from E5Gui.E5Application import e5App
28
29 import Preferences
30 import Utilities
31
32
33 class MaintainabilityIndexDialog(QDialog, Ui_MaintainabilityIndexDialog):
34 """
35 Class implementing a dialog to show maintainability indexes.
36 """
37 def __init__(self, radonService, parent=None):
38 """
39 Constructor
40
41 @param radonService reference to the service
42 @type RadonMetricsPlugin
43 @param parent reference to the parent widget
44 @type QWidget
45 """
46 super(MaintainabilityIndexDialog, self).__init__(parent)
47 self.setupUi(self)
48 self.setWindowFlags(Qt.Window)
49
50 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
51 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
52
53 self.resultList.headerItem().setText(self.resultList.columnCount(), "")
54
55 self.radonService = radonService
56 self.radonService.metricsDone.connect(self.__processResult)
57 self.radonService.metricsError.connect(self.__processError)
58 self.radonService.batchFinished.connect(self.__batchFinished)
59
60 self.cancelled = False
61
62 self.__project = e5App().getObject("Project")
63 self.__locale = QLocale()
64
65 self.__fileList = []
66 self.filterFrame.setVisible(False)
67
68 self.explanationLabel.setText(self.tr(
69 "<table>"
70 "<tr><td colspan=2><b>Ranking:</b></td></tr>"
71 "<tr><td><b>A</b></td><td>score &gt; 19</td></tr>"
72 "<tr><td><b>B</b></td><td>9 &lt; score &le; 19</td></tr>"
73 "<tr><td><b>C</b></td><td>score &le; 9</td></tr>"
74 "</table>"
75 ))
76
77 def __resizeResultColumns(self):
78 """
79 Private method to resize the list columns.
80 """
81 self.resultList.header().resizeSections(QHeaderView.ResizeToContents)
82 self.resultList.header().setStretchLastSection(True)
83
84 def __createResultItem(self, filename, values):
85 """
86 Private slot to create a new item in the result list.
87
88 @param filename name of the file
89 @type str
90 @param values values to be displayed
91 @type dict
92 """
93 data = [self.__project.getRelativePath(filename)]
94 try:
95 data.append(self.__locale.toString(float(values["mi"]), "f", 2))
96 except ValueError:
97 data.append(values["mi"])
98 data.append(values["rank"])
99 itm = QTreeWidgetItem(self.resultList, data)
100 itm.setTextAlignment(1, Qt.Alignment(Qt.AlignRight))
101 itm.setTextAlignment(2, Qt.Alignment(Qt.AlignHCenter))
102
103 def __createErrorItem(self, filename, message):
104 """
105 Private slot to create a new error item in the result list.
106
107 @param filename name of the file
108 @type str
109 @param message error message
110 @type str
111 """
112 itm = QTreeWidgetItem(self.resultList, [
113 "{0} ({1})".format(self.__project.getRelativePath(filename),
114 message)])
115 itm.setFirstColumnSpanned(True)
116 font = itm.font(0)
117 font.setItalic(True)
118 itm.setFont(0, font)
119
120 def prepare(self, fileList, project):
121 """
122 Public method to prepare the dialog with a list of filenames.
123
124 @param fileList list of filenames
125 @type list of str
126 @param project reference to the project object
127 @type Project
128 """
129 self.__fileList = fileList[:]
130 self.__project = project
131
132 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
133 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
134 self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
135
136 self.filterFrame.setVisible(True)
137
138 self.__data = self.__project.getData(
139 "OTHERTOOLSPARMS", "RadonCodeMetrics")
140 if self.__data is None or "ExcludeFiles" not in self.__data:
141 self.__data = {"ExcludeFiles": ""}
142 self.excludeFilesEdit.setText(self.__data["ExcludeFiles"])
143
144 def start(self, fn):
145 """
146 Public slot to start the maintainability index determination.
147
148 @param fn file or list of files or directory to show
149 the maintainability index for
150 @type str or list of str
151 """
152 self.cancelled = False
153 self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
154 self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True)
155 self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
156 QApplication.processEvents()
157
158 if isinstance(fn, list):
159 self.files = fn
160 elif os.path.isdir(fn):
161 self.files = []
162 extensions = set(Preferences.getPython("PythonExtensions") +
163 Preferences.getPython("Python3Extensions"))
164 for ext in extensions:
165 self.files.extend(
166 Utilities.direntries(fn, True, '*{0}'.format(ext), 0))
167 else:
168 self.files = [fn]
169 self.files.sort()
170 # check for missing files
171 for f in self.files[:]:
172 if not os.path.exists(f):
173 self.files.remove(f)
174
175 self.__summary = {"files": 0}
176 for key in ['loc', 'sloc', 'lloc', 'comments', 'multi', 'blank']:
177 self.__summary[key] = 0
178
179 if len(self.files) > 0:
180 # disable updates of the list for speed
181 self.resultList.setUpdatesEnabled(False)
182 self.resultList.setSortingEnabled(False)
183
184 self.checkProgress.setMaximum(len(self.files))
185 self.checkProgress.setVisible(len(self.files) > 1)
186 self.checkProgressLabel.setVisible(len(self.files) > 1)
187 QApplication.processEvents()
188
189 # now go through all the files
190 self.progress = 0
191 if len(self.files) == 1 or not self.radonService.hasBatch:
192 self.__batch = False
193 self.maintainabilityIndex()
194 else:
195 self.__batch = True
196 self.maintainabilityIndexBatch()

eric ide

mercurial