1
# -*- coding: utf-8 -*-
3
# Copyright (c) 2003 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
7
Module implementing a code metrics dialog.
14
from PyQt4.QtCore import *
15
from PyQt4.QtGui import *
17
from Ui_CodeMetricsDialog import Ui_CodeMetricsDialog
21
class CodeMetricsDialog(QDialog, Ui_CodeMetricsDialog):
23
Class implementing a dialog to display the code metrics.
25
def __init__(self, parent = None):
29
@param parent parent widget (QWidget)
31
QDialog.__init__(self, parent)
34
self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
35
self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
37
self.summaryList.header().resizeSection(0, 200)
38
self.summaryList.header().resizeSection(1, 100)
40
self.cancelled = False
42
def __resizeResultColumns(self):
44
Private method to resize the list columns.
46
self.resultList.header().resizeSections(QHeaderView.ResizeToContents)
47
self.resultList.header().setStretchLastSection(True)
49
def __createResultItem(self, parent, strings):
51
Private slot to create a new item in the result list.
53
@param parent parent of the new item (QTreeWidget or QTreeWidgetItem)
54
@param strings strings to be displayed (QStringList)
55
@return the generated item
57
itm = QTreeWidgetItem(parent, strings)
58
for col in range(1,7):
59
itm.setTextAlignment(col, Qt.Alignment(Qt.AlignRight))
62
def __resizeSummaryColumns(self):
64
Private method to resize the list columns.
66
self.summaryList.doItemsLayout()
67
self.summaryList.header().resizeSections(QHeaderView.ResizeToContents)
68
self.summaryList.header().setStretchLastSection(True)
70
def __createSummaryItem(self, col0, col1):
72
Private slot to create a new item in the summary list.
74
@param col0 string for column 0 (string or QString)
75
@param col1 string for column 1 (string or QString)
77
itm = QTreeWidgetItem(self.summaryList, QStringList() << col0 << col1)
78
itm.setTextAlignment(1, Qt.Alignment(Qt.AlignRight))
82
Public slot to start the code metrics determination.
84
@param fn file or list of files or directory to be show
85
the code metrics for (string or list of strings)
88
if type(fn) is types.ListType:
90
elif os.path.isdir(fn):
91
files = Utilities.direntries(fn, True, '*.py', False)
95
# check for missing files
97
if not os.path.exists(f):
100
self.checkProgress.setMaximum(len(files))
101
QApplication.processEvents()
104
CodeMetrics.summarize(total, 'files', len(files))
109
# disable updates of the list for speed
110
self.resultList.setUpdatesEnabled(False)
111
self.resultList.setSortingEnabled(False)
113
# now go through all the files
118
fitm = self.__createResultItem(self.resultList, QStringList(file))
120
stats = CodeMetrics.analyze(file, total)
122
identifiers = stats.identifiers + ['TOTAL ']
123
for id in identifiers:
124
counters = stats.counters.get(id, {})
126
for key in ('start', 'end', 'lines', 'nloc', 'comments', 'empty'):
127
if counters.get(key, 0):
128
v.append(loc.toString(counters[key]))
132
self.__createResultItem(fitm,
133
QStringList() << id << v[0] << v[1] << v[2] << v[3] << v[4] << v[5])
134
self.resultList.expandItem(fitm)
137
self.checkProgress.setValue(progress)
138
QApplication.processEvents()
140
# reenable updates of the list
141
self.resultList.setSortingEnabled(True)
142
self.resultList.setUpdatesEnabled(True)
143
self.__resizeResultColumns()
145
# now do the summary stuff
146
docstrings = total['lines'] - total['comments'] - \
147
total['empty lines'] - total['non-commentary lines']
148
self.__createSummaryItem(self.trUtf8("files"), loc.toString(total['files']))
149
self.__createSummaryItem(self.trUtf8("lines"), loc.toString(total['lines']))
150
self.__createSummaryItem(self.trUtf8("bytes"), loc.toString(total['bytes']))
151
self.__createSummaryItem(self.trUtf8("comments"), loc.toString(total['comments']))
152
self.__createSummaryItem(self.trUtf8("empty lines"),
153
loc.toString(total['empty lines']))
154
self.__createSummaryItem(self.trUtf8("non-commentary lines"),
155
loc.toString(total['non-commentary lines']))
156
self.__createSummaryItem(self.trUtf8("documentation lines"),
157
loc.toString(docstrings))
158
self.__resizeSummaryColumns()
163
Private slot called when the action finished or the user pressed the button.
165
self.cancelled = True
166
self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
167
self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
168
self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
170
self.resultList.header().setResizeMode(QHeaderView.Interactive)
171
self.summaryList.header().setResizeMode(QHeaderView.Interactive)
173
def on_buttonBox_clicked(self, button):
175
Private slot called by a button of the button box clicked.
177
@param button button that was clicked (QAbstractButton)
179
if button == self.buttonBox.button(QDialogButtonBox.Close):
181
elif button == self.buttonBox.button(QDialogButtonBox.Cancel):