| Total Complexity | 43 | 
| Total Lines | 191 | 
| Duplicated Lines | 6.81 % | 
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like pygameui.Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # | ||
| 2 | # Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] | ||
| 3 | # | ||
| 4 | # This file is part of Pygame.UI. | ||
| 5 | # | ||
| 6 | # Pygame.UI is free software; you can redistribute it and/or modify | ||
| 7 | # it under the terms of the Lesser GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 2.1 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | # | ||
| 11 | # Pygame.UI is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # Lesser GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the Lesser GNU General Public License | ||
| 17 | # along with Pygame.UI; if not, write to the Free Software | ||
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | # | ||
| 20 | |||
| 21 | """ | ||
| 22 | Member variables naming convention: | ||
| 23 | _ non watched variable (not changing visual representation of widget) | ||
| 24 | * 'watched' variable (change of this variable will force widget update) | ||
| 25 | """ | ||
| 26 | |||
| 27 | import pygame | ||
| 28 | |||
| 29 | import Const | ||
| 30 | import types | ||
| 31 | |||
| 32 | class DataHolder: | ||
| 33 | pass | ||
| 34 | |||
| 35 | class Widget: | ||
| 36 | def __init__(self, parent, **kwargs): | ||
| 37 | # set attributes | ||
| 38 | # bypass change detection | ||
| 39 | self.__dict__['_changeReported'] = 0 | ||
| 40 | self.__dict__['parent'] = parent | ||
| 41 | self.__dict__['metaType'] = Const.TYPE_WIDGET | ||
| 42 | self.__dict__['app'] = parent.getApp() | ||
| 43 | self.__dict__['theme'] = self.app.theme | ||
| 44 | self.__dict__['foreground'] = None | ||
| 45 | self.__dict__['background'] = None | ||
| 46 | self.__dict__['style'] = None | ||
| 47 | self.__dict__['font'] = None | ||
| 48 | self.__dict__['align'] = Const.ALIGN_NONE | ||
| 49 | self.__dict__['tooltip'] = None | ||
| 50 | self.__dict__['tooltipTitle'] = None | ||
| 51 | self.__dict__['statustip'] = None | ||
| 52 | self.__dict__['visible'] = 0 | ||
| 53 | self.__dict__['enabled'] = 1 | ||
| 54 | self.__dict__['focused'] = 0 | ||
| 55 | self.__dict__['mouseOver'] = 0 | ||
| 56 | self.__dict__['focusable'] = 1 | ||
| 57 | self.__dict__['dragSource'] = 0 | ||
| 58 | self.__dict__['dragTarget'] = 0 | ||
| 59 | self.__dict__['layout'] = None | ||
| 60 | self.__dict__['tags'] = [] | ||
| 61 | self.__dict__['id'] = None | ||
| 62 | self.__dict__['orderNo'] = 0 | ||
| 63 | self.__dict__['rect'] = pygame.Rect((0, 0, 0, 0)) | ||
| 64 |         self.__dict__['_handleMap'] = {'*': []} | ||
| 65 | self.__dict__['data'] = DataHolder() | ||
| 66 | # notify parent | ||
| 67 | self.visible = 1 | ||
| 68 | self.processKWArguments(kwargs) | ||
| 69 | |||
| 70 | def processKWArguments(self, kwargs): | ||
| 71 | # process keyword arguments | ||
| 72 | for key in kwargs: | ||
| 73 | if hasattr(self, key): | ||
| 74 | self.__dict__[key] = kwargs[key] | ||
| 75 | else: | ||
| 76 | raise AttributeError(key) | ||
| 77 | |||
| 78 | def subscribeAction(self, actionName, handler): | ||
| 79 | handleList = self._handleMap.get(actionName, []) | ||
| 80 | handleList.append(handler) | ||
| 81 | self._handleMap[actionName] = handleList | ||
| 82 | |||
| 83 | def processAction(self, actionName, data = None, widget = None): | ||
| 84 | if not actionName: return | ||
| 85 | if not widget: widget = self | ||
| 86 | handlers = self._handleMap.get(actionName, self._handleMap['*']) | ||
| 87 | if handlers: | ||
| 88 | for handler in handlers: | ||
| 89 | if type(handler) == types.InstanceType: | ||
| 90 | handler = getattr(handler, actionName) | ||
| 91 | handler(widget, actionName, data) | ||
| 92 | else: | ||
| 93 | self.parent.processAction(actionName, data, widget) | ||
| 94 | |||
| 95 | def draw(self, surface): | ||
| 96 |         raise NotImplementedError('%s.draw' % self.__class__) | ||
| 97 | |||
| 98 | def getSize(self): | ||
| 99 | raise NotImplementedError | ||
| 100 | |||
| 101 | def onFocusLost(self): | ||
| 102 | self.focused = 0 | ||
| 103 | |||
| 104 | def onFocusGained(self): | ||
| 105 | self.focused = 1 | ||
| 106 | |||
| 107 | def onMouseOut(self): | ||
| 108 | self.mouseOver = 0 | ||
| 109 | |||
| 110 | def onMouseOver(self): | ||
| 111 | self.mouseOver = 1 | ||
| 112 | |||
| 113 | def onCursorChanged(self): | ||
| 114 | return | ||
| 115 | |||
| 116 | def processMB1Down(self, evt): | ||
| 117 | return Const.NoEvent | ||
| 118 | |||
| 119 | def processMB1Up(self, evt): | ||
| 120 | return Const.NoEvent | ||
| 121 | |||
| 122 | def processMB1UpMissed(self, evt): | ||
| 123 | return Const.NoEvent | ||
| 124 | |||
| 125 | def processMB2Down(self, evt): | ||
| 126 | return Const.NoEvent | ||
| 127 | |||
| 128 | def processMB2Up(self, evt): | ||
| 129 | return Const.NoEvent | ||
| 130 | |||
| 131 | def processMB2UpMissed(self, evt): | ||
| 132 | return Const.NoEvent | ||
| 133 | |||
| 134 | def processMB3Down(self, evt): | ||
| 135 | return Const.NoEvent | ||
| 136 | |||
| 137 | def processMB3Up(self, evt): | ||
| 138 | return Const.NoEvent | ||
| 139 | |||
| 140 | def processMB3UpMissed(self, evt): | ||
| 141 | return Const.NoEvent | ||
| 142 | |||
| 143 | def processMWUp(self, evt): | ||
| 144 | return Const.NoEvent | ||
| 145 | |||
| 146 | def processMWDown(self, evt): | ||
| 147 | return Const.NoEvent | ||
| 148 | |||
| 149 | def processMMotion(self, evt): | ||
| 150 | return Const.NoEvent | ||
| 151 | |||
| 152 | def processKeyDown(self, evt): | ||
| 153 | return evt | ||
| 154 | |||
| 155 | def processKeyUp(self, evt): | ||
| 156 | return evt | ||
| 157 | |||
| 158 | def getApp(self): | ||
| 159 | return self.parent.getApp() | ||
| 160 | |||
| 161 | def redraw(self): | ||
| 162 | self.parent.redraw(self) | ||
| 163 | self.__dict__['_changeReported'] = 1 | ||
| 164 | |||
| 165 | View Code Duplication | def __setattr__(self, name, value): | |
|  | |||
| 166 | #@name = intern(name) | ||
| 167 | dict = self.__dict__ | ||
| 168 | if value == dict.get(name, Const.NoValue): | ||
| 169 | return | ||
| 170 | dict[name] = value | ||
| 171 | if name == 'visible' and self.parent: | ||
| 172 | self.parent.redraw(self, 1) | ||
| 173 | dict['_changeReported'] = 1 | ||
| 174 | #@print 'set', self, name , value | ||
| 175 | elif not self._changeReported and name[0] != '_' and self.parent: | ||
| 176 | self.parent.redraw(self) | ||
| 177 | dict['_changeReported'] = 1 | ||
| 178 | #@print 'set', self, name , value | ||
| 179 | |||
| 180 | def set(self, **kwargs): | ||
| 181 | self.processKWArguments(kwargs) | ||
| 182 | |||
| 183 | ## | ||
| 184 | ## All elements are registered here | ||
| 185 | ## | ||
| 186 | |||
| 187 | widgets = {} | ||
| 188 | |||
| 189 | def registerWidget(widget, name): | ||
| 190 | widgets[name] = widget | ||
| 191 |