| Conditions | 16 |
| Total Lines | 146 |
| Code Lines | 103 |
| 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:
Complex classes like LM.ListMenuClass.message_window() 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 | import os |
||
| 37 | def message_window(self, event=None): |
||
| 38 | """ツリービューを右クリックしたときの処理 |
||
| 39 | |||
| 40 | ・子アイテムならば削除ダイアログを表示する。 |
||
| 41 | 親アイテムならば追加を行う。 |
||
| 42 | |||
| 43 | Args: |
||
| 44 | event (instance): tkinter.Event のインスタンス |
||
| 45 | |||
| 46 | """ |
||
| 47 | curItem = self.APP.tree.focus() # 選択アイテムの認識番号取得 |
||
| 48 | parentItem = self.APP.tree.parent(curItem) # 親アイテムの認識番号取得 |
||
| 49 | # 親アイテムをクリックしたとき |
||
| 50 | if self.APP.tree.item(curItem)["text"] == self.tree_folder[4][1]: |
||
| 51 | # imageタグを選択したとき |
||
| 52 | fTyp = [(u'小説エディタ', '*.gif')] |
||
| 53 | iDir = os.path.abspath(os.path.dirname(__file__)) |
||
| 54 | filepath = filedialog.askopenfilename( |
||
| 55 | filetypes=fTyp, |
||
| 56 | initialdir=iDir |
||
| 57 | ) |
||
| 58 | # ファイル名があるとき |
||
| 59 | if not filepath == "": |
||
| 60 | file_name = os.path.splitext(os.path.basename(filepath))[0] |
||
| 61 | path = "./{0}/{1}.gif".format( |
||
| 62 | self.tree_folder[4][0], |
||
| 63 | file_name |
||
| 64 | ) |
||
| 65 | shutil.copy2(filepath, path) |
||
| 66 | self.APP.cwc.frame_image() |
||
| 67 | path = "./{0}/{1}.txt".format( |
||
| 68 | self.tree_folder[4][0], |
||
| 69 | file_name |
||
| 70 | ) |
||
| 71 | tree = self.APP.tree.insert( |
||
| 72 | self.tree_folder[4][0], |
||
| 73 | 'end', |
||
| 74 | text=file_name |
||
| 75 | ) |
||
| 76 | self.APP.tree.see(tree) |
||
| 77 | self.APP.tree.selection_set(tree) |
||
| 78 | self.APP.tree.focus(tree) |
||
| 79 | self.select_list_item = file_name |
||
| 80 | self.APP.fmc.now_path = path |
||
| 81 | f = open(path, 'w', encoding='utf-8') |
||
| 82 | self.APP.sfc.zoom = 100 |
||
| 83 | f.write(str(self.APP.sfc.zoom)) |
||
| 84 | f.close() |
||
| 85 | self.APP.cwc.frame_image() |
||
| 86 | self.path_read_image( |
||
| 87 | self.tree_folder[4][0], |
||
| 88 | file_name, |
||
| 89 | 0 |
||
| 90 | ) |
||
| 91 | |||
| 92 | else: |
||
| 93 | if str( |
||
| 94 | self.APP.tree.item(curItem)["text"] |
||
| 95 | ) and (not str( |
||
| 96 | self.APP.tree.item(parentItem)["text"] |
||
| 97 | ) |
||
| 98 | ): |
||
| 99 | # サブダイヤログを表示する |
||
| 100 | title = u'{0}に挿入'.format(self.APP.tree.item(curItem)["text"]) |
||
| 101 | dialog = MD.MyDialog(self.APP, "挿入", True, title, False) |
||
| 102 | self.ROOT.wait_window(dialog.sub_name_win) |
||
| 103 | file_name = dialog.txt |
||
| 104 | del dialog |
||
| 105 | if not file_name == "": |
||
| 106 | self.APP.fmc.open_file_save(self.APP.fmc.now_path) |
||
| 107 | curItem = self.APP.tree.focus() |
||
| 108 | text = self.APP.tree.item(curItem)["text"] |
||
| 109 | path = "" |
||
| 110 | # 選択されているフォルダを見つける |
||
| 111 | for val in self.tree_folder: |
||
| 112 | if text == val[1]: |
||
| 113 | if val[0] == self.tree_folder[0][0]: |
||
| 114 | self.APP.cwc.frame_character() |
||
| 115 | self.APP.txt_yobi_name.insert( |
||
| 116 | tk.END, |
||
| 117 | file_name |
||
| 118 | ) |
||
| 119 | else: |
||
| 120 | self.APP.cwc.frame() |
||
| 121 | |||
| 122 | path = "./{0}/{1}.txt".format(val[0], file_name) |
||
| 123 | tree = self.APP.tree.insert( |
||
| 124 | val[0], |
||
| 125 | 'end', |
||
| 126 | text=file_name |
||
| 127 | ) |
||
| 128 | self.APP.fmc.now_path = path |
||
| 129 | break |
||
| 130 | |||
| 131 | # パスが存在すれば新規作成する |
||
| 132 | if not path == "": |
||
| 133 | f = open(path, 'w', encoding='utf-8') |
||
| 134 | f.write("") |
||
| 135 | f.close() |
||
| 136 | # ツリービューを選択状態にする |
||
| 137 | self.APP.tree.see(tree) |
||
|
|
|||
| 138 | self.APP.tree.selection_set(tree) |
||
| 139 | self.APP.tree.focus(tree) |
||
| 140 | self.select_list_item = file_name |
||
| 141 | self.APP.winfo_toplevel().title( |
||
| 142 | u"小説エディタ\\{0}\\{1}" |
||
| 143 | .format(text, file_name) |
||
| 144 | ) |
||
| 145 | self.APP.text.focus() |
||
| 146 | # テキストを読み取り専用を解除する |
||
| 147 | self.APP.text.configure(state='normal') |
||
| 148 | self.APP.hpc.create_tags() |
||
| 149 | # 子アイテムを右クリックしたとき |
||
| 150 | else: |
||
| 151 | if str(self.APP.tree.item(curItem)["text"]): |
||
| 152 | # 項目を削除する |
||
| 153 | file_name = self.APP.tree.item(curItem)["text"] |
||
| 154 | text = self.APP.tree.item(parentItem)["text"] |
||
| 155 | # OK、キャンセルダイアログを表示し、OKを押したとき |
||
| 156 | if messagebox.askokcancel( |
||
| 157 | u"項目削除", |
||
| 158 | "{0}を削除しますか?".format(file_name) |
||
| 159 | ): |
||
| 160 | # パスを取得する |
||
| 161 | for val in self.tree_folder: |
||
| 162 | if text == val[1]: |
||
| 163 | path = "./{0}/{1}.txt".format( |
||
| 164 | val[0], |
||
| 165 | file_name |
||
| 166 | ) |
||
| 167 | image_path = "./{0}/{1}.gif".format( |
||
| 168 | val[0], |
||
| 169 | file_name |
||
| 170 | ) |
||
| 171 | self.APP.tree.delete(curItem) |
||
| 172 | self.APP.fmc.now_path = "" |
||
| 173 | break |
||
| 174 | # imageパスが存在したとき |
||
| 175 | if os.path.isfile(image_path): |
||
| 176 | os.remove(image_path) |
||
| 177 | |||
| 178 | # パスが存在したとき |
||
| 179 | if not path == "": |
||
| 180 | os.remove(path) |
||
| 181 | self.APP.cwc.frame() |
||
| 182 | self.APP.text.focus() |
||
| 183 | |||
| 366 |