Passed
Push — master ( 3731d5...59a60b )
by Yoshihiro
02:04
created

LM   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 374
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
eloc 236
dl 0
loc 374
ccs 171
cts 180
cp 0.95
rs 9.76
c 0
b 0
f 0
wmc 33

6 Methods

Rating   Name   Duplication   Size   Complexity  
A ListMenuClass.path_read_text() 0 41 4
A ListMenuClass.on_name_click() 0 39 4
A ListMenuClass.__init__() 0 6 1
B ListMenuClass.on_double_click() 0 58 5
F ListMenuClass.message_window() 0 151 16
A ListMenuClass.path_read_image() 0 44 3
1
#!/usr/bin/env python3
2 1
import os
3 1
import shutil
4 1
import tkinter as tk
5 1
import tkinter.filedialog as filedialog
6 1
import tkinter.messagebox as messagebox
7 1
import xml.etree.ElementTree as ET
8
9 1
from PIL import Image, ImageTk
10
11 1
import MD
12
13
14 1
class ListMenuClass():
15
    """リストメニューバーのクラス.
16
17
    ・リストメニューバーにあるプログラム群
18
19
    Args:
20
        app (instance): MainProcessingClassインスタンス
21
        master (instance): toplevelインスタンス
22
        tree_folder (str): ツリーフォルダの配列
23
24
    Attributes:
25
        text_text (str): 現在入力中の初期テキスト
26
        self.select_list_item (str): 選択中のリストボックスアイテム名
27
28
    """
29 1
    def __init__(self, app, master, tree_folder):
30 1
        self.text_text = ""
31 1
        self.select_list_item = ""
32 1
        self.APP = app
33 1
        self.MASTER = master
34 1
        self.tree_folder = tree_folder
35
36 1
    def message_window(self, event=None):
37
        """ツリービューを右クリックしたときの処理.
38
39
        ・子アイテムならば削除ダイアログを表示する。
40
        親アイテムならば追加を行う。
41
42
        Args:
43
            event (instance): tkinter.Event のインスタンス
44
45
        """
46
        # 選択アイテムの認識番号取得
47 1
        curItem = self.APP.tree.focus()
48
        # 親アイテムの認識番号取得
49 1
        parentItem = self.APP.tree.parent(curItem)
50
        # 親アイテムをクリックしたとき
51 1
        if self.APP.tree.item(curItem)["text"] == self.tree_folder[4][1]:
52
            # imageタグを選択したとき
53 1
            fTyp = [(u'小説エディタ', '*.gif')]
54 1
            iDir = os.path.abspath(os.path.dirname(__file__))
55 1
            filepath = filedialog.askopenfilename(
56
                filetypes=fTyp,
57
                initialdir=iDir
58
            )
59
            # ファイル名があるとき
60 1
            if not filepath == "":
61 1
                file_name = os.path.splitext(os.path.basename(filepath))[0]
62 1
                path = "./{0}/{1}.gif".format(
63
                    self.tree_folder[4][0],
64
                    file_name
65
                )
66 1
                shutil.copy2(filepath, path)
67 1
                self.APP.cwc.frame_image()
68 1
                path = "./{0}/{1}.txt".format(
69
                    self.tree_folder[4][0],
70
                    file_name
71
                )
72 1
                tree = self.APP.tree.insert(
73
                    self.tree_folder[4][0],
74
                    'end',
75
                    text=file_name
76
                )
77 1
                self.APP.tree.see(tree)
78 1
                self.APP.tree.selection_set(tree)
79 1
                self.APP.tree.focus(tree)
80 1
                self.select_list_item = file_name
81 1
                self.APP.fmc.now_path = path
82 1
                f = open(path, 'w', encoding='utf-8')
83 1
                self.APP.sfc.zoom = 100
84 1
                f.write(str(self.APP.sfc.zoom))
85 1
                f.close()
86 1
                self.APP.cwc.frame_image()
87 1
                self.path_read_image(
88
                    self.tree_folder[4][0],
89
                    file_name,
90
                    0
91
                )
92
93
        else:
94 1
            if str(
95
                self.APP.tree.item(curItem)["text"]
96
            ) and (not str(
97
                    self.APP.tree.item(parentItem)["text"]
98
                )
99
            ):
100
                # サブダイヤログを表示する
101 1
                title = u'{0}に挿入'.format(self.APP.tree.item(curItem)["text"])
102 1
                dialog = MD.MyDialogClass(self.APP, "挿入", True, title, False)
103 1
                self.MASTER.wait_window(dialog.sub_name_win)
104 1
                file_name = dialog.txt
105 1
                del dialog
106 1
                if not file_name == "":
107 1
                    self.APP.fmc.open_file_save(self.APP.fmc.now_path)
108 1
                    curItem = self.APP.tree.focus()
109 1
                    text = self.APP.tree.item(curItem)["text"]
110 1
                    path = ""
111 1
                    tree = ""
112
                    # 選択されているフォルダを見つける
113 1
                    for val in self.tree_folder:
114 1
                        if text == val[1]:
115 1
                            if val[0] == self.tree_folder[0][0]:
116 1
                                self.APP.cwc.frame_character()
117 1
                                self.APP.txt_yobi_name.insert(
118
                                    tk.END,
119
                                    file_name
120
                                )
121
                            else:
122 1
                                self.APP.cwc.frame()
123
124 1
                            path = "./{0}/{1}.txt".format(val[0], file_name)
125 1
                            tree = self.APP.tree.insert(
126
                                val[0],
127
                                'end',
128
                                text=file_name
129
                            )
130 1
                            self.APP.fmc.now_path = path
131 1
                            break
132
133
                    # パスが存在すれば新規作成する
134 1
                    if not path == "":
135 1
                        f = open(path, 'w', encoding='utf-8')
136 1
                        f.write("")
137 1
                        f.close()
138
                        # ツリービューを選択状態にする
139 1
                        self.APP.tree.see(tree)
140 1
                        self.APP.tree.selection_set(tree)
141 1
                        self.APP.tree.focus(tree)
142 1
                        self.select_list_item = file_name
143 1
                        self.APP.winfo_toplevel().title(
144
                            u"小説エディタ\\{0}\\{1}"
145
                            .format(text, file_name)
146
                        )
147 1
                        self.APP.text.focus()
148
                        # テキストを読み取り専用を解除する
149 1
                        self.APP.text.configure(state='normal')
150 1
                        self.APP.hpc.create_tags()
151
            # 子アイテムを右クリックしたとき
152
            else:
153 1
                if str(self.APP.tree.item(curItem)["text"]):
154
                    # 項目を削除する
155 1
                    file_name = self.APP.tree.item(curItem)["text"]
156 1
                    text = self.APP.tree.item(parentItem)["text"]
157
                    # OK、キャンセルダイアログを表示し、OKを押したとき
158 1
                    if messagebox.askokcancel(
159
                        u"項目削除",
160
                        "{0}を削除しますか?".format(file_name)
161
                    ):
162 1
                        image_path = ""
163 1
                        path = ""
164
                        # パスを取得する
165 1
                        for val in self.tree_folder:
166 1
                            if text == val[1]:
167 1
                                path = "./{0}/{1}.txt".format(
168
                                    val[0],
169
                                    file_name
170
                                )
171 1
                                image_path = "./{0}/{1}.gif".format(
172
                                    val[0],
173
                                    file_name
174
                                )
175 1
                                self.APP.tree.delete(curItem)
176 1
                                self.APP.fmc.now_path = ""
177 1
                                break
178
                        # imageパスが存在したとき
179 1
                        if os.path.isfile(image_path):
180 1
                            os.remove(image_path)
181
182
                        # パスが存在したとき
183 1
                        if not path == "":
184 1
                            os.remove(path)
185 1
                            self.APP.cwc.frame()
186 1
                            self.APP.text.focus()
187
188 1
    def on_name_click(self, event=None):
189
        """名前の変更.
190
191
        ・リストボックスの名前を変更する。
192
193
        Args:
194
            event (instance): tkinter.Event のインスタンス
195
196
        """
197
        # 選択アイテムの認識番号取得
198 1
        curItem = self.APP.tree.focus()
199
        # 親アイテムの認識番号取得
200 1
        parentItem = self.APP.tree.parent(curItem)
201 1
        text = self.APP.tree.item(parentItem)["text"]
202 1
        if not text == "":
203 1
            sub_text = self.APP.tree.item(curItem)["text"]
204 1
            title = u'{0}の名前を変更'.format(sub_text)
205 1
            dialog2 = MD.MyDialogClass(self.APP, u"変更", True, title, sub_text)
206 1
            self.MASTER.wait_window(dialog2.sub_name_win)
207
            # テキストを読み取り専用を解除する
208 1
            self.APP.text.configure(state='normal')
209 1
            co_text = dialog2.txt
210 1
            del dialog2
211 1
            for val in self.tree_folder:
212 1
                if text == val[1]:
213 1
                    path1 = "./{0}/{1}.txt".format(val[0], sub_text)
214 1
                    path2 = "./{0}/{1}.txt".format(val[0], co_text)
215 1
                    self.APP.fmc.now_path = path2
216
                    # テキストの名前を変更する
217 1
                    os.rename(path1, path2)
218 1
                    self.APP.tree.delete(curItem)
219 1
                    Item = self.APP.tree.insert(
220
                        parentItem,
221
                        'end',
222
                        text=co_text
223
                    )
224 1
                    self.APP.tree.selection_set(Item)
225 1
                    self.path_read_text(val[0], co_text)
226 1
                    return
227
228 1
    def path_read_image(self, image_path, image_name, scale):
229
        """イメージを読み込んで表示.
230
231
        ・パスが存在すればイメージファイルを読み込んで表示する。
232
233
        Args:
234
            image_path (str): イメージファイルの相対パス
235
            image_name (str): イメージファイルの名前
236
            scale (int): 拡大率(%)
237
238
        """
239 1
        if not self.APP.fmc.now_path == "":
240 1
            title = "{0}/{1}.gif".format(
241
                image_path,
242
                image_name
243
            )
244 1
            giffile = Image.open(title)
245 1
            if scale > 0:
246 1
                giffile = giffile.resize(
247
                    (
248
                        int(giffile.width / 100*scale),
249
                        int(giffile.height / 100*scale)
250
                    ),
251
                    resample=Image.LANCZOS
252
                )
253
254 1
            self.APP.image_space.photo = ImageTk.PhotoImage(giffile)
255 1
            self.APP.image_space.itemconfig(
256
                self.APP.image_on_space,
257
                image=self.APP.image_space.photo
258
            )
259
            # イメージサイズにキャンバスサイズを合わす
260 1
            self.APP.image_space.config(
261
                scrollregion=(
262
                    0,
263
                    0,
264
                    giffile.size[0],
265
                    giffile.size[1]
266
                )
267
            )
268 1
            giffile.close()
269
270 1
        self.APP.winfo_toplevel().title(
271
                u"小説エディタ\\{0}\\{1}".format(self.tree_folder[4][1], image_name)
272
            )
273
274 1
    def path_read_text(self, text_path, text_name):
275
        """テキストを読み込んで表示.
276
277
        ・パスが存在すればテキストを読み込んで表示する。
278
279
        Args:
280
            text_path (str): テキストファイルの相対パス
281
            text_name (str): テキストファイルの名前
282
283
        """
284 1
        if not self.APP.fmc.now_path == "":
285 1
            if not self.APP.fmc.now_path.find(self.tree_folder[0][0]) == -1:
286 1
                self.APP.txt_yobi_name.delete('0', tk.END)
287 1
                self.APP.txt_name.delete('0', tk.END)
288 1
                self.APP.txt_birthday.delete('0', tk.END)
289 1
                self.APP.text_body.delete('1.0', tk.END)
290 1
                tree = ET.parse(self.APP.fmc.now_path)
291 1
                elem = tree.getroot()
292 1
                self.APP.txt_yobi_name.insert(tk.END, elem.findtext("call"))
293 1
                self.APP.txt_name.insert(tk.END, elem.findtext("name"))
294 1
                self.APP.var.set(elem.findtext("sex"))
295 1
                self.APP.txt_birthday.insert(tk.END, elem.findtext("birthday"))
296 1
                self.APP.text_body.insert(tk.END, elem.findtext("body"))
297 1
                title = "{0}/{1}.gif".format(
298
                    self.tree_folder[0][0],
299
                    elem.findtext("call")
300
                )
301 1
                if os.path.isfile(title):
302 1
                    self.APP.sfc.print_gif(title)
303
            else:
304 1
                self.APP.text.delete('1.0', tk.END)
305 1
                f = open(self.APP.fmc.now_path, 'r', encoding='utf-8')
306 1
                self.text_text = f.read()
307 1
                self.APP.text.insert(tk.END, self.text_text)
308 1
                f.close()
309
310 1
            self.APP.winfo_toplevel().title(
311
                u"小説エディタ\\{0}\\{1}".format(text_path, text_name)
312
            )
313
            # シンタックスハイライトをする
314 1
            self.APP.hpc.all_highlight()
315
316 1
    def on_double_click(self, event=None):
317
        """ツリービューをダブルクリック.
318
319
        ・ファイルを保存して閉じて、選択されたアイテムを表示する。
320
321
        Args:
322
            event (instance): tkinter.Event のインスタンス
323
324
        """
325
        # 選択アイテムの認識番号取得
326 1
        curItem = self.APP.tree.focus()
327
        # 親アイテムの認識番号取得
328 1
        parentItem = self.APP.tree.parent(curItem)
329 1
        text = self.APP.tree.item(parentItem)["text"]
330
        # 開いているファイルを保存
331 1
        self.APP.fmc.open_file_save(self.APP.fmc.now_path)
332
        # テキストを読み取り専用を解除する
333 1
        self.APP.cwc.frame()
334 1
        self.APP.text.configure(state='disabled')
335
        # 条件によって分離
336 1
        self.select_list_item = self.APP.tree.item(curItem)["text"]
337 1
        path = ""
338 1
        for val in self.tree_folder:
339 1
            if text == val[1]:
340 1
                if val[0] == self.tree_folder[4][0]:
341
                    path = "./{0}/{1}.txt".format(
342
                        val[0],
343
                        self.select_list_item
344
                    )
345
                    f = open(path, 'r', encoding='utf-8')
346
                    zoom = f.read()
347
                    self.APP.sfc.zoom = int(zoom)
348
                    self.APP.fmc.now_path = path
349
                    self.APP.cwc.frame_image()
350
                    self.path_read_image(
351
                        self.tree_folder[4][0],
352
                        self.select_list_item,
353
                        self.APP.sfc.zoom
354
                    )
355
                else:
356 1
                    path = "./{0}/{1}.txt".format(
357
                        val[0],
358
                        self.select_list_item
359
                    )
360 1
                    self.APP.fmc.now_path = path
361 1
                    if val[0] == self.tree_folder[0][0]:
362 1
                        self.APP.cwc.frame_character()
363
                    else:
364
                        # テキストを読み取り専用を解除する
365 1
                        self.APP.text.configure(state='normal')
366 1
                        self.APP.text.focus()
367
368 1
                    self.path_read_text(text, self.select_list_item)
369
370 1
                return
371
372
        self.APP.fmc.now_path = ""
373
        self.APP.winfo_toplevel().title(u"小説エディタ")
374