Passed
Pull Request — master (#53)
by Yoshihiro
02:11
created

LM   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 233
dl 0
loc 366
rs 9.76
c 0
b 0
f 0
wmc 33

6 Methods

Rating   Name   Duplication   Size   Complexity  
B ListMenuClass.on_double_click() 0 56 5
A ListMenuClass.path_read_text() 0 41 4
F ListMenuClass.message_window() 0 146 16
A ListMenuClass.on_name_click() 0 37 4
A ListMenuClass.__init__() 0 13 1
A ListMenuClass.path_read_image() 0 44 3
1
import os
2
import shutil
3
import tkinter as tk
4
import tkinter.filedialog as filedialog
5
import tkinter.messagebox as messagebox
6
import xml.etree.ElementTree as ET
7
8
from PIL import Image, ImageTk
9
10
import MD
11
12
13
class ListMenuClass():
14
    """リストメニューバーのクラス
15
16
    ・リストメニューバーにあるプログラム群
17
18
    Attributes:
19
        text_text (str): 現在入力中の初期テキスト
20
        self.select_list_item (str): 選択中のリストボックスアイテム名
21
22
    """
23
    def __init__(self, app, root, tree_folder):
24
        """
25
        Args:
26
            app (instance): lineframeインスタンス
27
            root (instance): toplevelインスタンス
28
            tree_folder (str): ツリーフォルダの配列
29
30
        """
31
        self.text_text = ""
32
        self.select_list_item = ""
33
        self.APP = app
34
        self.ROOT = root
35
        self.tree_folder = tree_folder
36
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)
0 ignored issues
show
introduced by
The variable tree does not seem to be defined for all execution paths.
Loading history...
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):
0 ignored issues
show
introduced by
The variable image_path does not seem to be defined for all execution paths.
Loading history...
176
                            os.remove(image_path)
177
178
                        # パスが存在したとき
179
                        if not path == "":
0 ignored issues
show
introduced by
The variable path does not seem to be defined for all execution paths.
Loading history...
180
                            os.remove(path)
181
                            self.APP.cwc.frame()
182
                            self.APP.text.focus()
183
184
    def on_name_click(self, event=None):
185
        """名前の変更
186
187
        ・リストボックスの名前を変更する。
188
189
        Args:
190
            event (instance): tkinter.Event のインスタンス
191
192
        """
193
        curItem = self.APP.tree.focus()              # 選択アイテムの認識番号取得
194
        parentItem = self.APP.tree.parent(curItem)   # 親アイテムの認識番号取得
195
        text = self.APP.tree.item(parentItem)["text"]
196
        if not text == "":
197
            sub_text = self.APP.tree.item(curItem)["text"]
198
            title = u'{0}の名前を変更'.format(sub_text)
199
            dialog2 = MD.MyDialog(self.APP, u"変更", True, title, sub_text)
200
            self.ROOT.wait_window(dialog2.sub_name_win)
201
            # テキストを読み取り専用を解除する
202
            self.APP.text.configure(state='normal')
203
            co_text = dialog2.txt
204
            del dialog2
205
            for val in self.tree_folder:
206
                if text == val[1]:
207
                    path1 = "./{0}/{1}.txt".format(val[0], sub_text)
208
                    path2 = "./{0}/{1}.txt".format(val[0], co_text)
209
                    self.APP.fmc.now_path = path2
210
                    # テキストの名前を変更する
211
                    os.rename(path1, path2)
212
                    self.APP.tree.delete(curItem)
213
                    Item = self.APP.tree.insert(
214
                        parentItem,
215
                        'end',
216
                        text=co_text
217
                    )
218
                    self.APP.tree.selection_set(Item)
219
                    self.path_read_text(val[0], co_text)
220
                    return
221
222
    def path_read_image(self, image_path, image_name, scale):
223
        """イメージを読み込んで表示
224
225
        ・パスが存在すればイメージファイルを読み込んで表示する。
226
227
        Args:
228
            image_path (str): イメージファイルの相対パス
229
            image_name (str): イメージファイルの名前
230
            scale (int): 拡大率(%)
231
232
        """
233
        if not self.APP.fmc.now_path == "":
234
            title = "{0}/{1}.gif".format(
235
                image_path,
236
                image_name
237
            )
238
            giffile = Image.open(title)
239
            if scale > 0:
240
                giffile = giffile.resize(
241
                    (
242
                        int(giffile.width / 100*scale),
243
                        int(giffile.height / 100*scale)
244
                    ),
245
                    resample=Image.LANCZOS
246
                )
247
248
            self.APP.image_space.photo = ImageTk.PhotoImage(giffile)
249
            self.APP.image_space.itemconfig(
250
                self.APP.image_on_space,
251
                image=self.APP.image_space.photo
252
            )
253
            # イメージサイズにキャンバスサイズを合わす
254
            self.APP.image_space.config(
255
                scrollregion=(
256
                    0,
257
                    0,
258
                    giffile.size[0],
259
                    giffile.size[1]
260
                )
261
            )
262
            giffile.close()
263
264
        self.APP.winfo_toplevel().title(
265
                u"小説エディタ\\{0}\\{1}".format(self.tree_folder[4][1], image_name)
266
            )
267
268
    def path_read_text(self, text_path, text_name):
269
        """テキストを読み込んで表示
270
271
        ・パスが存在すればテキストを読み込んで表示する。
272
273
        Args:
274
            text_path (str): テキストファイルの相対パス
275
            text_name (str): テキストファイルの名前
276
277
        """
278
        if not self.APP.fmc.now_path == "":
279
            if not self.APP.fmc.now_path.find(self.tree_folder[0][0]) == -1:
280
                self.APP.txt_yobi_name.delete('0', tk.END)
281
                self.APP.txt_name.delete('0', tk.END)
282
                self.APP.txt_birthday.delete('0', tk.END)
283
                self.APP.text_body.delete('1.0', tk.END)
284
                tree = ET.parse(self.APP.fmc.now_path)
285
                elem = tree.getroot()
286
                self.APP.txt_yobi_name.insert(tk.END, elem.findtext("call"))
287
                self.APP.txt_name.insert(tk.END, elem.findtext("name"))
288
                self.APP.var.set(elem.findtext("sex"))
289
                self.APP.txt_birthday.insert(tk.END, elem.findtext("birthday"))
290
                self.APP.text_body.insert(tk.END, elem.findtext("body"))
291
                title = "{0}/{1}.gif".format(
292
                    self.tree_folder[0][0],
293
                    elem.findtext("call")
294
                )
295
                if os.path.isfile(title):
296
                    self.APP.sfc.print_gif(title)
297
            else:
298
                self.APP.text.delete('1.0', tk.END)
299
                f = open(self.APP.fmc.now_path, 'r', encoding='utf-8')
300
                self.text_text = f.read()
301
                self.APP.text.insert(tk.END, self.text_text)
302
                f.close()
303
304
            self.APP.winfo_toplevel().title(
305
                u"小説エディタ\\{0}\\{1}".format(text_path, text_name)
306
            )
307
            # シンタックスハイライトをする
308
            self.APP.hpc.all_highlight()
309
310
    def on_double_click(self, event=None):
311
        """ツリービューをダブルクリック
312
313
        ・ファイルを保存して閉じて、選択されたアイテムを表示する。
314
315
        Args:
316
            event (instance): tkinter.Event のインスタンス
317
318
        """
319
        curItem = self.APP.tree.focus()              # 選択アイテムの認識番号取得
320
        parentItem = self.APP.tree.parent(curItem)   # 親アイテムの認識番号取得
321
        text = self.APP.tree.item(parentItem)["text"]
322
        # 開いているファイルを保存
323
        self.APP.fmc.open_file_save(self.APP.fmc.now_path)
324
        # テキストを読み取り専用を解除する
325
        self.APP.cwc.frame()
326
        self.APP.text.configure(state='disabled')
327
        # 条件によって分離
328
        self.select_list_item = self.APP.tree.item(curItem)["text"]
329
        path = ""
330
        for val in self.tree_folder:
331
            if text == val[1]:
332
                if val[0] == self.tree_folder[4][0]:
333
                    path = "./{0}/{1}.txt".format(
334
                        val[0],
335
                        self.select_list_item
336
                    )
337
                    f = open(path, 'r', encoding='utf-8')
338
                    zoom = f.read()
339
                    self.APP.sfc.zoom = int(zoom)
340
                    self.APP.fmc.now_path = path
341
                    self.APP.cwc.frame_image()
342
                    self.path_read_image(
343
                        self.tree_folder[4][0],
344
                        self.select_list_item,
345
                        self.APP.sfc.zoom
346
                    )
347
                else:
348
                    path = "./{0}/{1}.txt".format(
349
                        val[0],
350
                        self.select_list_item
351
                    )
352
                    self.APP.fmc.now_path = path
353
                    if val[0] == self.tree_folder[0][0]:
354
                        self.APP.cwc.frame_character()
355
                    else:
356
                        # テキストを読み取り専用を解除する
357
                        self.APP.text.configure(state='normal')
358
                        self.APP.text.focus()
359
360
                    self.path_read_text(text, self.select_list_item)
361
362
                return
363
364
        self.APP.fmc.now_path = ""
365
        self.APP.winfo_toplevel().title(u"小説エディタ")
366