Conditions | 8 |
Total Lines | 275 |
Code Lines | 182 |
Lines | 0 |
Ratio | 0 % |
Changes | 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:
1 | #!/usr/bin/env python |
||
106 | def init(self, root): |
||
107 | """Initialize and return the main frame.""" |
||
108 | # pylint: disable=attribute-defined-outside-init |
||
109 | |||
110 | # Shared arguments |
||
111 | width_text = 30 |
||
112 | height_text = 10 |
||
113 | height_ext = 5 |
||
114 | |||
115 | # Shared keyword arguments |
||
116 | kw_f = {'padding': 5} # constructor arguments for frames |
||
117 | kw_gp = {'padx': 2, 'pady': 2} # grid arguments for padded widgets |
||
118 | kw_gs = {'sticky': tk.NSEW} # grid arguments for sticky widgets |
||
119 | kw_gsp = dict( |
||
120 | chain(kw_gs.items(), kw_gp.items()) |
||
121 | ) # grid arguments for sticky padded widgets |
||
122 | |||
123 | root.bind_all("<Control-minus>", lambda arg: widget.adjustFontSize(-1)) |
||
124 | root.bind_all("<Control-equal>", lambda arg: widget.adjustFontSize(1)) |
||
125 | root.bind_all("<Control-0>", lambda arg: widget.resetFontSize()) |
||
126 | |||
127 | # Configure grid |
||
128 | frame = ttk.Frame(root, **kw_f) |
||
129 | frame.rowconfigure(0, weight=0) |
||
130 | frame.rowconfigure(1, weight=1) |
||
131 | frame.columnconfigure(0, weight=2) |
||
132 | frame.columnconfigure(1, weight=1) |
||
133 | frame.columnconfigure(2, weight=1) |
||
134 | frame.columnconfigure(3, weight=2) |
||
135 | |||
136 | # Create widgets |
||
137 | def frame_project(root): |
||
138 | """Frame for the current project.""" |
||
139 | # Configure grid |
||
140 | frame = ttk.Frame(root, **kw_f) |
||
141 | frame.rowconfigure(0, weight=1) |
||
142 | frame.columnconfigure(0, weight=0) |
||
143 | frame.columnconfigure(1, weight=1) |
||
144 | |||
145 | # Place widgets |
||
146 | widget.Label(frame, text="Project:").grid(row=0, column=0, **kw_gp) |
||
147 | widget.Entry(frame, textvariable=self.stringvar_project).grid( |
||
148 | row=0, column=1, **kw_gsp |
||
149 | ) |
||
150 | |||
151 | return frame |
||
152 | |||
153 | def frame_tree(root): |
||
154 | """Frame for the current document.""" |
||
155 | # Configure grid |
||
156 | frame = ttk.Frame(root, **kw_f) |
||
157 | frame.rowconfigure(0, weight=1) |
||
158 | frame.columnconfigure(0, weight=0) |
||
159 | frame.columnconfigure(1, weight=1) |
||
160 | |||
161 | # Place widgets |
||
162 | widget.Label(frame, text="Document:").grid(row=0, column=0, **kw_gp) |
||
163 | self.combobox_documents = widget.Combobox( |
||
164 | frame, textvariable=self.stringvar_document, state="readonly" |
||
165 | ) |
||
166 | self.combobox_documents.grid(row=0, column=1, **kw_gsp) |
||
167 | |||
168 | return frame |
||
169 | |||
170 | def frame_document(root): |
||
171 | """Frame for current document's outline and items.""" |
||
172 | # Configure grid |
||
173 | frame = ttk.Frame(root, **kw_f) |
||
174 | frame.rowconfigure(0, weight=0) |
||
175 | frame.rowconfigure(1, weight=5) |
||
176 | frame.rowconfigure(2, weight=0) |
||
177 | frame.rowconfigure(3, weight=0) |
||
178 | frame.columnconfigure(0, weight=0) |
||
179 | frame.columnconfigure(1, weight=0) |
||
180 | frame.columnconfigure(2, weight=0) |
||
181 | frame.columnconfigure(3, weight=0) |
||
182 | frame.columnconfigure(4, weight=1) |
||
183 | frame.columnconfigure(5, weight=1) |
||
184 | |||
185 | @_log |
||
186 | def treeview_outline_treeviewselect(event): |
||
187 | """Handle selecting an item in the tree view.""" |
||
188 | if self.ignore: |
||
189 | return |
||
190 | thewidget = event.widget |
||
191 | curselection = thewidget.selection() |
||
192 | if curselection: |
||
193 | uid = curselection[0] |
||
194 | self.stringvar_item.set(uid) |
||
195 | |||
196 | @_log |
||
197 | def treeview_outline_delete(event): # pylint: disable=W0613 |
||
198 | """Handle deleting an item in the tree view.""" |
||
199 | if self.ignore: |
||
200 | return |
||
201 | self.remove() |
||
202 | |||
203 | # Place widgets |
||
204 | widget.Label(frame, text="Outline:").grid( |
||
205 | row=0, column=0, columnspan=4, sticky=tk.W, **kw_gp |
||
206 | ) |
||
207 | widget.Label(frame, text="Items:").grid( |
||
208 | row=0, column=4, columnspan=2, sticky=tk.W, **kw_gp |
||
209 | ) |
||
210 | c_columnId = ("Id",) |
||
211 | self.treeview_outline = widget.TreeView(frame, columns=c_columnId) |
||
212 | for col in c_columnId: |
||
213 | self.treeview_outline.heading(col, text=col) |
||
214 | |||
215 | # Add a Vertical scrollbar to the Treeview Outline |
||
216 | treeview_outline_verticalScrollBar = widget.ScrollbarV( |
||
217 | frame, command=self.treeview_outline.yview |
||
218 | ) |
||
219 | treeview_outline_verticalScrollBar.grid( |
||
220 | row=1, column=0, columnspan=1, **kw_gs |
||
221 | ) |
||
222 | self.treeview_outline.configure( |
||
223 | yscrollcommand=treeview_outline_verticalScrollBar.set |
||
224 | ) |
||
225 | |||
226 | self.treeview_outline.bind( |
||
227 | "<<TreeviewSelect>>", treeview_outline_treeviewselect |
||
228 | ) |
||
229 | self.treeview_outline.bind("<Delete>", treeview_outline_delete) |
||
230 | self.treeview_outline.grid(row=1, column=1, columnspan=3, **kw_gsp) |
||
231 | self.text_items = widget.noUserInput_init( |
||
232 | widget.Text(frame, width=width_text, wrap=tk.WORD) |
||
233 | ) |
||
234 | self.text_items.grid(row=1, column=4, columnspan=2, **kw_gsp) |
||
235 | self.text_items_hyperlink = utilTkinter.HyperlinkManager(self.text_items) |
||
236 | widget.Button(frame, text="<", width=0, command=self.left).grid( |
||
237 | row=2, column=0, sticky=tk.EW, padx=(2, 0) |
||
238 | ) |
||
239 | widget.Button(frame, text="v", width=0, command=self.down).grid( |
||
240 | row=2, column=1, sticky=tk.EW |
||
241 | ) |
||
242 | widget.Button(frame, text="^", width=0, command=self.up).grid( |
||
243 | row=2, column=2, sticky=tk.EW |
||
244 | ) |
||
245 | widget.Button(frame, text=">", width=0, command=self.right).grid( |
||
246 | row=2, column=3, sticky=tk.EW, padx=(0, 2) |
||
247 | ) |
||
248 | widget.Button(frame, text="Add Item", command=self.add).grid( |
||
249 | row=2, column=4, sticky=tk.W, **kw_gp |
||
250 | ) |
||
251 | widget.Button(frame, text="Remove Selected Item", command=self.remove).grid( |
||
252 | row=2, column=5, sticky=tk.E, **kw_gp |
||
253 | ) |
||
254 | |||
255 | return frame |
||
256 | |||
257 | def frame_item(root): |
||
258 | """Frame for the currently selected item.""" |
||
259 | # Configure grid |
||
260 | frame = ttk.Frame(root, **kw_f) |
||
261 | frame.rowconfigure(0, weight=0) |
||
262 | frame.rowconfigure(1, weight=4) |
||
263 | frame.rowconfigure(2, weight=0) |
||
264 | frame.rowconfigure(3, weight=1) |
||
265 | frame.rowconfigure(4, weight=1) |
||
266 | frame.rowconfigure(5, weight=1) |
||
267 | frame.rowconfigure(6, weight=1) |
||
268 | frame.rowconfigure(7, weight=0) |
||
269 | frame.rowconfigure(8, weight=0) |
||
270 | frame.rowconfigure(9, weight=0) |
||
271 | frame.rowconfigure(10, weight=0) |
||
272 | frame.rowconfigure(11, weight=4) |
||
273 | frame.columnconfigure(0, weight=1, pad=kw_f['padding'] * 2) |
||
274 | frame.columnconfigure(1, weight=1) |
||
275 | |||
276 | @_log |
||
277 | def text_focusin(_): |
||
278 | """Handle entering a text field.""" |
||
279 | self.ignore = True |
||
280 | |||
281 | @_log |
||
282 | def text_item_focusout(event): |
||
283 | """Handle updated text text.""" |
||
284 | self.ignore = False |
||
285 | thewidget = event.widget |
||
286 | value = thewidget.get('1.0', tk.END) |
||
287 | self.stringvar_text.set(value) |
||
288 | |||
289 | @_log |
||
290 | def text_extendedvalue_focusout(event): |
||
291 | """Handle updated extended attributes.""" |
||
292 | self.ignore = False |
||
293 | thewidget = event.widget |
||
294 | value = thewidget.get('1.0', tk.END) |
||
295 | self.stringvar_extendedvalue.set(value) |
||
296 | |||
297 | # Selected Item |
||
298 | widget.Label(frame, text="Selected Item:").grid( |
||
299 | row=0, column=0, columnspan=3, sticky=tk.W, **kw_gp |
||
300 | ) |
||
301 | self.text_item = widget.Text( |
||
302 | frame, width=width_text, height=height_text, wrap=tk.WORD |
||
303 | ) |
||
304 | self.text_item.bind('<FocusIn>', text_focusin) |
||
305 | self.text_item.bind('<FocusOut>', text_item_focusout) |
||
306 | self.text_item.grid(row=1, column=0, columnspan=3, **kw_gsp) |
||
307 | |||
308 | # Column: Properties |
||
309 | self.create_properties_widget(frame).grid( |
||
310 | row=2, rowspan=2, column=0, columnspan=2, sticky=tk.NSEW, **kw_gp |
||
311 | ) |
||
312 | |||
313 | # Column: Links |
||
314 | self.create_links_widget(frame).grid( |
||
315 | row=4, rowspan=3, column=0, columnspan=2, sticky=tk.NSEW, **kw_gp |
||
316 | ) |
||
317 | |||
318 | # External Reference |
||
319 | self.create_reference_widget(frame).grid( |
||
320 | row=7, rowspan=2, column=0, columnspan=2, sticky=tk.NSEW, **kw_gp |
||
321 | ) |
||
322 | |||
323 | widget.Label(frame, text="Extended Attributes:").grid( |
||
324 | row=9, column=0, columnspan=3, sticky=tk.W, **kw_gp |
||
325 | ) |
||
326 | self.combobox_extended = widget.Combobox( |
||
327 | frame, textvariable=self.stringvar_extendedkey |
||
328 | ) |
||
329 | self.combobox_extended.grid(row=10, column=0, columnspan=3, **kw_gsp) |
||
330 | self.text_extendedvalue = widget.Text( |
||
331 | frame, width=width_text, height=height_ext, wrap=tk.WORD |
||
332 | ) |
||
333 | self.text_extendedvalue.bind('<FocusIn>', text_focusin) |
||
334 | self.text_extendedvalue.bind('<FocusOut>', text_extendedvalue_focusout) |
||
335 | self.text_extendedvalue.grid(row=11, column=0, columnspan=3, **kw_gsp) |
||
336 | |||
337 | return frame |
||
338 | |||
339 | def frame_family(root): |
||
340 | """Frame for the parent and child document items.""" |
||
341 | # Configure grid |
||
342 | frame = ttk.Frame(root, **kw_f) |
||
343 | frame.rowconfigure(0, weight=0) |
||
344 | frame.rowconfigure(1, weight=1) |
||
345 | frame.rowconfigure(2, weight=0) |
||
346 | frame.rowconfigure(3, weight=1) |
||
347 | frame.columnconfigure(0, weight=1) |
||
348 | |||
349 | # Place widgets |
||
350 | widget.Label(frame, text="Linked To:").grid( |
||
351 | row=0, column=0, sticky=tk.W, **kw_gp |
||
352 | ) |
||
353 | self.text_parents = widget.noUserInput_init( |
||
354 | widget.Text(frame, width=width_text, wrap=tk.WORD) |
||
355 | ) |
||
356 | self.text_parents_hyperlink = utilTkinter.HyperlinkManager( |
||
357 | self.text_parents |
||
358 | ) |
||
359 | self.text_parents.grid(row=1, column=0, **kw_gsp) |
||
360 | widget.Label(frame, text="Linked From:").grid( |
||
361 | row=2, column=0, sticky=tk.W, **kw_gp |
||
362 | ) |
||
363 | self.text_children = widget.noUserInput_init( |
||
364 | widget.Text(frame, width=width_text, wrap=tk.WORD) |
||
365 | ) |
||
366 | self.text_children_hyperlink = utilTkinter.HyperlinkManager( |
||
367 | self.text_children |
||
368 | ) |
||
369 | self.text_children.grid(row=3, column=0, **kw_gsp) |
||
370 | |||
371 | return frame |
||
372 | |||
373 | # Place widgets |
||
374 | frame_project(frame).grid(row=0, column=0, columnspan=2, **kw_gs) |
||
375 | frame_tree(frame).grid(row=0, column=2, columnspan=2, **kw_gs) |
||
376 | frame_document(frame).grid(row=1, column=0, **kw_gs) |
||
377 | frame_item(frame).grid(row=1, column=1, columnspan=2, **kw_gs) |
||
378 | frame_family(frame).grid(row=1, column=3, **kw_gs) |
||
379 | |||
380 | return frame |
||
381 | |||
836 |