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

« back to all changes in this revision

Viewing changes to eric/Graphics/ModuleWidget.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) 2004 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
4
 
#
5
 
 
6
 
"""
7
 
Module implementing a module widget.
8
 
"""
9
 
 
10
 
from qt import *
11
 
from qtcanvas import *
12
 
 
13
 
from UMLWidget import UMLWidget
14
 
 
15
 
class ModuleModel:
16
 
    """
17
 
    Class implementing the module model.
18
 
    """
19
 
    def __init__(self, name, classlist=[]):
20
 
        """
21
 
        Constructor
22
 
        
23
 
        @param name the module name (string)
24
 
        @param classlist list of class names (list of strings)
25
 
        """
26
 
        self.name = name
27
 
        self.classlist = classlist
28
 
        
29
 
    def addClass(self, classname):
30
 
        """
31
 
        Method to add a class to the module model.
32
 
        
33
 
        @param classname class name to be added (string)
34
 
        """
35
 
        self.classlist.append(classname)
36
 
        
37
 
    def getClasses(self):
38
 
        """
39
 
        Method to retrieve the classes of the module.
40
 
        
41
 
        @return list of class names (list of strings)
42
 
        """
43
 
        return self.classlist[:]
44
 
        
45
 
    def getName(self):
46
 
        """
47
 
        Method to retrieve the module name.
48
 
        
49
 
        @return module name (string)
50
 
        """
51
 
        return self.name
52
 
        
53
 
class ModuleWidget(UMLWidget):
54
 
    """
55
 
    Class implementing a module widget.
56
 
    """
57
 
    def __init__(self, canvas, model=None, x=0, y=0, rounded=0):
58
 
        """
59
 
        Constructor
60
 
        
61
 
        @param canvas canvas containing the class widget (QCanvas)
62
 
        @param model module model containing the module data (ModuleModel)
63
 
        @param x x-coordinate (integer)
64
 
        @param y y-coordinate (integer)
65
 
        @param rounded flag indicating a rounded corner (boolean)
66
 
        """
67
 
        UMLWidget.__init__(self, canvas, x, y, rounded)
68
 
        self.model = model
69
 
        
70
 
        if self.model:
71
 
            self.calculateSize()
72
 
        
73
 
        self.update()
74
 
        
75
 
    def calculateSize(self):
76
 
        """
77
 
        Method to calculate the size of the module widget.
78
 
        """
79
 
        if self.model is None:
80
 
            return
81
 
            
82
 
        classes = self.model.getClasses()
83
 
        
84
 
        font = QFont(self.font)
85
 
        font.setBold(1)
86
 
        font.setItalic(1)
87
 
        fm = QFontMetrics(font)
88
 
        
89
 
        # calculate height
90
 
        fontHeight = fm.lineSpacing()
91
 
        lines = 1
92
 
        if classes:
93
 
            lines += len(classes)
94
 
        else:
95
 
            lines += 1
96
 
        height = lines * fontHeight
97
 
        
98
 
        # calculate width
99
 
        width = fm.width(self.model.getName())
100
 
        
101
 
        for cls in classes:
102
 
            w = fm.width(cls)
103
 
            width = max(w, width)
104
 
        
105
 
        width += self.margin * 2
106
 
        width += 4  # a little bit more for strange fonts
107
 
        height += self.margin * 4
108
 
        
109
 
        self.setSize(width, height)
110
 
    
111
 
    def setModel(self, model):
112
 
        """
113
 
        Method to set the module model.
114
 
        
115
 
        @param model module model containing the module data (ModuleModel)
116
 
        """
117
 
        self.model = model
118
 
        self.calculateSize()
119
 
        self.update()
120
 
        
121
 
    def drawShape(self, painter):
122
 
        """
123
 
        Overriden method to draw the shape.
124
 
        
125
 
        @param painter painter the shape is drawn to (QPainter)
126
 
        """
127
 
        painter.setPen(Qt.black)
128
 
        offsetX = self.x()
129
 
        offsetY = self.y()
130
 
        w = self.width()
131
 
        h = self.height()
132
 
        fm = QFontMetrics(self.font)
133
 
        fontHeight = fm.lineSpacing() + 1 # one for the baseline itself
134
 
        
135
 
        painter.drawRect(offsetX, offsetY, w, h)
136
 
        
137
 
        y = self.margin
138
 
        self.font.setBold(1)
139
 
        painter.setFont(self.font)
140
 
        painter.drawText(offsetX + self.margin,
141
 
                         offsetY + y,
142
 
                         w - self.margin * 2,
143
 
                         fontHeight,
144
 
                         Qt.AlignHCenter,
145
 
                         self.model.getName())
146
 
        y += fontHeight + self.margin
147
 
        self.font.setBold(0)
148
 
        painter.setFont(self.font)
149
 
        painter.drawLine(offsetX, offsetY + y,
150
 
                         offsetX + w - 1, offsetY + y)
151
 
        y += self.margin
152
 
        
153
 
        classes = self.model.getClasses()
154
 
        if classes:
155
 
            for cls in classes:
156
 
                painter.drawText(offsetX + self.margin,
157
 
                                 offsetY + y,
158
 
                                 w - self.margin * 2,
159
 
                                 fontHeight,
160
 
                                 Qt.AlignHCenter,
161
 
                                 cls)
162
 
                y += fontHeight
163
 
        else:
164
 
            painter.drawText(offsetX + self.margin,
165
 
                             offsetY + y,
166
 
                             w - self.margin * 2,
167
 
                             fontHeight,
168
 
                             Qt.AlignHCenter,
169
 
                             "")
170
 
            y += fontHeight
171
 
        
172
 
        if self.isSelected():
173
 
            self.drawSelected(painter, offsetX, offsetY)