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