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