~ubuntu-branches/ubuntu/karmic/eric/karmic

« back to all changes in this revision

Viewing changes to eric/DataViews/CodeMetricsDialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2008-01-28 18:02:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080128180225-6nrox6yrworh2c4v
Tags: 4.0.4-1ubuntu1
* Add python-qt3 to build-depends becuase that's where Ubuntu puts 
  pyqtconfig
* Change maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright (c) 2003 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
 
4
#
 
5
 
 
6
"""
 
7
Module implementing a code metrics dialog.
 
8
"""
 
9
 
 
10
import sys
 
11
import os
 
12
import types
 
13
 
 
14
from PyQt4.QtCore import *
 
15
from PyQt4.QtGui import *
 
16
 
 
17
from Ui_CodeMetricsDialog import Ui_CodeMetricsDialog
 
18
import CodeMetrics
 
19
import Utilities
 
20
 
 
21
class CodeMetricsDialog(QDialog, Ui_CodeMetricsDialog):
 
22
    """
 
23
    Class implementing a dialog to display the code metrics.
 
24
    """
 
25
    def __init__(self, parent = None):
 
26
        """
 
27
        Constructor
 
28
        
 
29
        @param parent parent widget (QWidget)
 
30
        """
 
31
        QDialog.__init__(self, parent)
 
32
        self.setupUi(self)
 
33
        
 
34
        self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
 
35
        self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
 
36
        
 
37
        self.summaryList.header().resizeSection(0, 200)
 
38
        self.summaryList.header().resizeSection(1, 100)
 
39
        
 
40
        self.cancelled = False
 
41
        
 
42
    def __resizeResultColumns(self):
 
43
        """
 
44
        Private method to resize the list columns.
 
45
        """
 
46
        self.resultList.header().resizeSections(QHeaderView.ResizeToContents)
 
47
        self.resultList.header().setStretchLastSection(True)
 
48
        
 
49
    def __createResultItem(self, parent, strings):
 
50
        """
 
51
        Private slot to create a new item in the result list.
 
52
        
 
53
        @param parent parent of the new item (QTreeWidget or QTreeWidgetItem)
 
54
        @param strings strings to be displayed (QStringList)
 
55
        @return the generated item
 
56
        """
 
57
        itm = QTreeWidgetItem(parent, strings)
 
58
        for col in range(1,7):
 
59
            itm.setTextAlignment(col, Qt.Alignment(Qt.AlignRight))
 
60
        return itm
 
61
        
 
62
    def __resizeSummaryColumns(self):
 
63
        """
 
64
        Private method to resize the list columns.
 
65
        """
 
66
        self.summaryList.doItemsLayout()
 
67
        self.summaryList.header().resizeSections(QHeaderView.ResizeToContents)
 
68
        self.summaryList.header().setStretchLastSection(True)
 
69
        
 
70
    def __createSummaryItem(self, col0, col1):
 
71
        """
 
72
        Private slot to create a new item in the summary list.
 
73
        
 
74
        @param col0 string for column 0 (string or QString)
 
75
        @param col1 string for column 1 (string or QString)
 
76
        """
 
77
        itm = QTreeWidgetItem(self.summaryList, QStringList() << col0 << col1)
 
78
        itm.setTextAlignment(1, Qt.Alignment(Qt.AlignRight))
 
79
        
 
80
    def start(self, fn):
 
81
        """
 
82
        Public slot to start the code metrics determination.
 
83
        
 
84
        @param fn file or list of files or directory to be show
 
85
                the code metrics for (string or list of strings)
 
86
        """
 
87
        loc = QLocale()
 
88
        if type(fn) is types.ListType:
 
89
            files = fn
 
90
        elif os.path.isdir(fn):
 
91
            files = Utilities.direntries(fn, True, '*.py', False)
 
92
        else:
 
93
            files = [fn]
 
94
        files.sort()
 
95
        # check for missing files
 
96
        for f in files[:]:
 
97
            if not os.path.exists(f):
 
98
                files.remove(f)
 
99
        
 
100
        self.checkProgress.setMaximum(len(files))
 
101
        QApplication.processEvents()
 
102
        
 
103
        total = {}
 
104
        CodeMetrics.summarize(total, 'files', len(files))
 
105
        
 
106
        progress = 0
 
107
        
 
108
        try:
 
109
            # disable updates of the list for speed
 
110
            self.resultList.setUpdatesEnabled(False)
 
111
            self.resultList.setSortingEnabled(False)
 
112
            
 
113
            # now go through all the files
 
114
            for file in files:
 
115
                if self.cancelled:
 
116
                    return
 
117
                
 
118
                fitm = self.__createResultItem(self.resultList, QStringList(file))
 
119
                
 
120
                stats = CodeMetrics.analyze(file, total)
 
121
                
 
122
                identifiers = stats.identifiers + ['TOTAL ']
 
123
                for id in identifiers:
 
124
                    counters = stats.counters.get(id, {})
 
125
                    v = []
 
126
                    for key in ('start', 'end', 'lines', 'nloc', 'comments', 'empty'):
 
127
                        if counters.get(key, 0):
 
128
                            v.append(loc.toString(counters[key]))
 
129
                        else:
 
130
                            v.append('')
 
131
                    
 
132
                    self.__createResultItem(fitm, 
 
133
                        QStringList() << id << v[0] << v[1] << v[2] << v[3] << v[4] << v[5])
 
134
                self.resultList.expandItem(fitm)
 
135
                
 
136
                progress += 1
 
137
                self.checkProgress.setValue(progress)
 
138
                QApplication.processEvents()
 
139
        finally:
 
140
            # reenable updates of the list
 
141
            self.resultList.setSortingEnabled(True)
 
142
            self.resultList.setUpdatesEnabled(True)
 
143
        self.__resizeResultColumns()
 
144
        
 
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()
 
159
        self.__finish()
 
160
        
 
161
    def __finish(self):
 
162
        """
 
163
        Private slot called when the action finished or the user pressed the button.
 
164
        """
 
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)
 
169
        
 
170
        self.resultList.header().setResizeMode(QHeaderView.Interactive)
 
171
        self.summaryList.header().setResizeMode(QHeaderView.Interactive)
 
172
        
 
173
    def on_buttonBox_clicked(self, button):
 
174
        """
 
175
        Private slot called by a button of the button box clicked.
 
176
        
 
177
        @param button button that was clicked (QAbstractButton)
 
178
        """
 
179
        if button == self.buttonBox.button(QDialogButtonBox.Close):
 
180
            self.close()
 
181
        elif button == self.buttonBox.button(QDialogButtonBox.Cancel):
 
182
            self.__finish()