Conditions | 31 |
Total Lines | 139 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Orange.widgets.utils.plot.OWAxis.update() 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 | """ |
||
141 | def update(self, zoom_only=False): |
||
142 | self.update_ticks() |
||
143 | line_color = self.plot.color(OWPalette.Axis) |
||
144 | text_color = self.plot.color(OWPalette.Text) |
||
145 | if not self.graph_line or not self.scene(): |
||
146 | return |
||
147 | self.line_item.setLine(self.graph_line) |
||
148 | self.line_item.setPen(line_color) |
||
149 | if self.title: |
||
150 | self.title_item.setHtml('<b>' + self.title + '</b>') |
||
151 | self.title_item.setDefaultTextColor(text_color) |
||
152 | if self.title_location == AxisMiddle: |
||
153 | title_p = 0.5 |
||
154 | elif self.title_location == AxisEnd: |
||
155 | title_p = 0.95 |
||
156 | else: |
||
157 | title_p = 0.05 |
||
158 | title_pos = self.graph_line.pointAt(title_p) |
||
159 | v = self.graph_line.normalVector().unitVector() |
||
160 | |||
161 | dense_text = False |
||
162 | if hasattr(self, 'title_margin'): |
||
163 | offset = self.title_margin |
||
164 | elif self._ticks: |
||
165 | if self.should_be_expanded(): |
||
166 | offset = 55 |
||
167 | dense_text = True |
||
168 | else: |
||
169 | offset = 35 |
||
170 | else: |
||
171 | offset = 10 |
||
172 | |||
173 | if self.title_above: |
||
174 | title_pos += (v.p2() - v.p1()) * (offset + QFontMetrics(self.title_item.font()).height()) |
||
175 | else: |
||
176 | title_pos -= (v.p2() - v.p1()) * offset |
||
177 | ## TODO: Move it according to self.label_pos |
||
178 | self.title_item.setVisible(self.show_title) |
||
179 | self.title_item.setRotation(-self.graph_line.angle()) |
||
180 | c = self.title_item.mapToParent(self.title_item.boundingRect().center()) |
||
181 | tl = self.title_item.mapToParent(self.title_item.boundingRect().topLeft()) |
||
182 | self.title_item.setPos(title_pos - c + tl) |
||
183 | |||
184 | ## Arrows |
||
185 | if not zoom_only: |
||
186 | if self.start_arrow_item: |
||
187 | self.scene().removeItem(self.start_arrow_item) |
||
188 | self.start_arrow_item = None |
||
189 | if self.end_arrow_item: |
||
190 | self.scene().removeItem(self.end_arrow_item) |
||
191 | self.end_arrow_item = None |
||
192 | |||
193 | if self.arrows & AxisStart: |
||
194 | if not zoom_only or not self.start_arrow_item: |
||
195 | self.start_arrow_item = QGraphicsPathItem(self.arrow_path, self) |
||
196 | self.start_arrow_item.setPos(self.graph_line.p1()) |
||
197 | self.start_arrow_item.setRotation(-self.graph_line.angle() + 180) |
||
198 | self.start_arrow_item.setBrush(line_color) |
||
199 | self.start_arrow_item.setPen(line_color) |
||
200 | if self.arrows & AxisEnd: |
||
201 | if not zoom_only or not self.end_arrow_item: |
||
202 | self.end_arrow_item = QGraphicsPathItem(self.arrow_path, self) |
||
203 | self.end_arrow_item.setPos(self.graph_line.p2()) |
||
204 | self.end_arrow_item.setRotation(-self.graph_line.angle()) |
||
205 | self.end_arrow_item.setBrush(line_color) |
||
206 | self.end_arrow_item.setPen(line_color) |
||
207 | |||
208 | ## Labels |
||
209 | |||
210 | n = len(self._ticks) |
||
211 | resize_plot_item_list(self.label_items, n, QGraphicsTextItem, self) |
||
212 | resize_plot_item_list(self.label_bg_items, n, QGraphicsRectItem, self) |
||
213 | resize_plot_item_list(self.tick_items, n, QGraphicsLineItem, self) |
||
214 | |||
215 | test_rect = QRectF(self.graph_line.p1(), self.graph_line.p2()).normalized() |
||
216 | test_rect.adjust(-1, -1, 1, 1) |
||
217 | |||
218 | n_v = self.graph_line.normalVector().unitVector() |
||
219 | if self.title_above: |
||
220 | n_p = n_v.p2() - n_v.p1() |
||
221 | else: |
||
222 | n_p = n_v.p1() - n_v.p2() |
||
223 | l_v = self.graph_line.unitVector() |
||
224 | l_p = l_v.p2() - l_v.p1() |
||
225 | for i in range(n): |
||
226 | pos, text, size, step = self._ticks[i] |
||
227 | hs = 0.5 * step |
||
228 | tick_pos = self.map_to_graph(pos) |
||
229 | if not test_rect.contains(tick_pos): |
||
230 | self.tick_items[i].setVisible(False) |
||
231 | self.label_items[i].setVisible(False) |
||
232 | continue |
||
233 | item = self.label_items[i] |
||
234 | item.setVisible(True) |
||
235 | if not zoom_only: |
||
236 | if self.id in XAxes or getattr(self, 'is_horizontal', False): |
||
237 | item.setHtml('<center>' + Qt.escape(text.strip()) + '</center>') |
||
238 | else: |
||
239 | item.setHtml(Qt.escape(text.strip())) |
||
240 | |||
241 | item.setTextWidth(-1) |
||
242 | text_angle = 0 |
||
243 | if dense_text: |
||
244 | w = min(item.boundingRect().width(), self.max_text_width) |
||
245 | item.setTextWidth(w) |
||
246 | if self.title_above: |
||
247 | label_pos = tick_pos + n_p * (w + self.text_margin) + l_p * item.boundingRect().height() / 2 |
||
248 | else: |
||
249 | label_pos = tick_pos + n_p * self.text_margin + l_p * item.boundingRect().height() / 2 |
||
250 | text_angle = -90 if self.title_above else 90 |
||
251 | else: |
||
252 | w = min(item.boundingRect().width(), |
||
253 | QLineF(self.map_to_graph(pos - hs), self.map_to_graph(pos + hs)).length()) |
||
254 | label_pos = tick_pos + n_p * self.text_margin + l_p * item.boundingRect().height() / 2 |
||
255 | item.setTextWidth(w) |
||
256 | |||
257 | if not self.always_horizontal_text: |
||
258 | if self.title_above: |
||
259 | item.setRotation(-self.graph_line.angle() - text_angle) |
||
260 | else: |
||
261 | item.setRotation(self.graph_line.angle() - text_angle) |
||
262 | |||
263 | item.setPos(label_pos) |
||
264 | item.setDefaultTextColor(text_color) |
||
265 | |||
266 | self.label_bg_items[i].setRect(item.boundingRect()) |
||
267 | self.label_bg_items[i].setPen(QPen(Qt.NoPen)) |
||
268 | self.label_bg_items[i].setBrush(self.plot.color(OWPalette.Canvas)) |
||
269 | |||
270 | item = self.tick_items[i] |
||
271 | item.setVisible(True) |
||
272 | tick_line = QLineF(v) |
||
273 | tick_line.translate(-tick_line.p1()) |
||
274 | tick_line.setLength(size) |
||
275 | if self.title_above: |
||
276 | tick_line.setAngle(tick_line.angle() + 180) |
||
277 | item.setLine(tick_line) |
||
278 | item.setPen(line_color) |
||
279 | item.setPos(self.map_to_graph(pos)) |
||
280 | |||
371 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.