1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
import os |
3
|
|
|
import zipfile |
4
|
|
|
import shutil |
5
|
|
|
import tkinter as tk |
6
|
|
|
import tkinter.messagebox as messagebox |
7
|
|
|
import tkinter.filedialog as filedialog |
8
|
|
|
|
9
|
|
|
from . import lm |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class FileMenuClass(): |
13
|
|
|
"""ファイルメニューバーのクラス. |
14
|
|
|
|
15
|
|
|
・ファイルメニューバーにあるプログラム群 |
16
|
|
|
|
17
|
|
|
Args: |
18
|
|
|
app (instance): MainProcessingClass のインスタンス |
19
|
|
|
master (instance): toplevel のインスタンス |
20
|
|
|
tree_folder (list): ツリーフォルダの配列 |
21
|
|
|
""" |
22
|
|
|
now_path = "" |
23
|
|
|
"""今の処理ししているファイルのパス.""" |
24
|
|
|
file_path = "" |
25
|
|
|
"""現在開いているファイル.""" |
26
|
|
|
|
27
|
|
|
def __init__(self, app, master, tree_folder): |
28
|
|
|
self.APP = app |
29
|
|
|
self.MASTER = master |
30
|
|
|
self.TREE_FOLDER = tree_folder |
31
|
|
|
|
32
|
|
|
def new_open(self, event=None): |
33
|
|
|
"""新規作成. |
34
|
|
|
|
35
|
|
|
・変更があれば、ファイル保存するか尋ねて、新規作成する。 |
36
|
|
|
|
37
|
|
|
Args: |
38
|
|
|
event (instance): tkinter.Event のインスタンス |
39
|
|
|
""" |
40
|
|
|
if not self.APP.text.get('1.0', 'end - 1c') == lm.ListMenuClass.text_text: |
41
|
|
|
if messagebox.askokcancel( |
42
|
|
|
u"小説エディタ", |
43
|
|
|
u"上書き保存しますか?" |
44
|
|
|
): |
45
|
|
|
self.overwrite_save_file() |
46
|
|
|
self.new_file() |
47
|
|
|
|
48
|
|
|
elif messagebox.askokcancel(u"小説エディタ", u"今の編集を破棄して新規作成しますか?"): |
49
|
|
|
self.new_file() |
50
|
|
|
else: |
51
|
|
|
self.new_file() |
52
|
|
|
|
53
|
|
|
def open_file(self, event=None): |
54
|
|
|
"""ファイルを開く処理. |
55
|
|
|
|
56
|
|
|
・ファイルを開くダイアログを作成しファイルを開く。 |
57
|
|
|
|
58
|
|
|
Args: |
59
|
|
|
event (instance): tkinter.Event のインスタンス |
60
|
|
|
""" |
61
|
|
|
# ファイルを開くダイアログを開く |
62
|
|
|
fTyp = [(u'小説エディタ', '*.ned')] |
63
|
|
|
iDir = os.path.abspath(os.path.dirname(__file__)) |
64
|
|
|
filepath = filedialog.askopenfilename( |
65
|
|
|
filetypes=fTyp, |
66
|
|
|
initialdir=iDir |
67
|
|
|
) |
68
|
|
|
# ファイル名があるとき |
69
|
|
|
if not filepath == "": |
70
|
|
|
# 初期化する |
71
|
|
|
self.APP.initialize() |
72
|
|
|
# ファイルを開いてdataフォルダに入れる |
73
|
|
|
with zipfile.ZipFile(filepath) as existing_zip: |
74
|
|
|
existing_zip.extractall('./data') |
75
|
|
|
# ツリービューを削除する |
76
|
|
|
for val in self.TREE_FOLDER: |
77
|
|
|
self.APP.tree.delete(val[0]) |
78
|
|
|
|
79
|
|
|
# ツリービューを表示する |
80
|
|
|
self.tree_get_loop() |
81
|
|
|
# ファイルパスを拡張子抜きで表示する |
82
|
|
|
filepath, ___ = os.path.splitext(filepath) |
83
|
|
|
FileMenuClass.file_path = filepath |
84
|
|
|
FileMenuClass.now_path = "" |
85
|
|
|
# テキストビューを新にする |
86
|
|
|
self.APP.cwc.frame() |
87
|
|
|
|
88
|
|
|
def overwrite_save_file(self, event=None): |
89
|
|
|
"""上書き保存処理. |
90
|
|
|
|
91
|
|
|
・上書き保存するための処理。ファイルがあれば保存して、 |
92
|
|
|
なければ保存ダイアログを出す。 |
93
|
|
|
|
94
|
|
|
Args: |
95
|
|
|
event (instance): tkinter.Event のインスタンス |
96
|
|
|
""" |
97
|
|
|
# ファイルパスが存在するとき |
98
|
|
|
if not FileMenuClass.file_path == "": |
99
|
|
|
# 編集中のファイルを保存する |
100
|
|
|
self.open_file_save(FileMenuClass.now_path) |
101
|
|
|
# zipファイルにまとめる |
102
|
|
|
shutil.make_archive(FileMenuClass.file_path, "zip", "./data") |
103
|
|
|
# 拡張子の変更を行う |
104
|
|
|
shutil.move( |
105
|
|
|
"{0}.zip".format(FileMenuClass.file_path), |
106
|
|
|
"{0}.ned".format(FileMenuClass.file_path) |
107
|
|
|
) |
108
|
|
|
# ファイルパスが存在しないとき |
109
|
|
|
else: |
110
|
|
|
# 保存ダイアログを開く |
111
|
|
|
self.save_file() |
112
|
|
|
|
113
|
|
|
def save_file(self, event=None): |
114
|
|
|
"""ファイルを保存処理. |
115
|
|
|
|
116
|
|
|
・ファイルを保存する。ファイル保存ダイアログを作成し保存をおこなう。 |
117
|
|
|
|
118
|
|
|
Args: |
119
|
|
|
event (instance): tkinter.Event のインスタンス |
120
|
|
|
""" |
121
|
|
|
# ファイル保存ダイアログを表示する |
122
|
|
|
fTyp = [(u"小説エディタ", ".ned")] |
123
|
|
|
iDir = os.path.abspath(os.path.dirname(__file__)) |
124
|
|
|
filepath = filedialog.asksaveasfilename( |
125
|
|
|
filetypes=fTyp, |
126
|
|
|
initialdir=iDir |
127
|
|
|
) |
128
|
|
|
# ファイルパスが決まったとき |
129
|
|
|
if not filepath == "": |
130
|
|
|
# 拡張子を除いて保存する |
131
|
|
|
FileMenuClass.file_path, ___ = os.path.splitext(filepath) |
132
|
|
|
# 上書き保存処理 |
133
|
|
|
self.overwrite_save_file() |
134
|
|
|
|
135
|
|
|
def on_closing(self): |
136
|
|
|
"""終了時の処理. |
137
|
|
|
|
138
|
|
|
・ソフトを閉じるか確認してから閉じる。 |
139
|
|
|
""" |
140
|
|
|
if messagebox.askokcancel(u"小説エディタ", u"終了してもいいですか?"): |
141
|
|
|
shutil.rmtree("./data") |
142
|
|
|
if os.path.isfile("./userdic.csv"): |
143
|
|
|
os.remove("./userdic.csv") |
144
|
|
|
|
145
|
|
|
self.MASTER.destroy() |
146
|
|
|
|
147
|
|
|
def new_file(self): |
148
|
|
|
"""新規作成をするための準備. |
149
|
|
|
|
150
|
|
|
・ファイルの新規作成をするための準備処理をおこなう。 |
151
|
|
|
""" |
152
|
|
|
self.APP.initialize() |
153
|
|
|
for val in self.TREE_FOLDER: |
154
|
|
|
self.APP.tree.delete(val[0]) |
155
|
|
|
|
156
|
|
|
# ツリービューを表示する |
157
|
|
|
self.tree_get_loop() |
158
|
|
|
self.APP.cwc.frame() |
159
|
|
|
self.APP.winfo_toplevel().title(u"小説エディタ") |
160
|
|
|
# テキストを読み取り専用にする |
161
|
|
|
self.APP.text.configure(state='disabled') |
162
|
|
|
# テキストにフォーカスを当てる |
163
|
|
|
self.APP.text.focus() |
164
|
|
|
|
165
|
|
|
def open_file_save(self, path): |
166
|
|
|
"""開いてるファイルを保存. |
167
|
|
|
|
168
|
|
|
・開いてるファイルをそれぞれの保存形式で保存する。 |
169
|
|
|
|
170
|
|
|
Args: |
171
|
|
|
path (str): 保存ファイルのパス |
172
|
|
|
""" |
173
|
|
|
# 編集ファイルを保存する |
174
|
|
|
if not path == "": |
175
|
|
|
f = open(path, 'w', encoding='utf-8') |
176
|
|
|
if not path.find(self.TREE_FOLDER[0][0]) == -1: |
177
|
|
|
f.write(self.save_charactor_file()) |
178
|
|
|
self.charactor_file = "" |
179
|
|
|
elif not path.find(self.TREE_FOLDER[4][0]) == -1: |
180
|
|
|
f.write(str(self.APP.spc.zoom)) |
181
|
|
|
else: |
182
|
|
|
f.write(self.APP.text.get("1.0", tk.END+'-1c')) |
183
|
|
|
|
184
|
|
|
f.close() |
185
|
|
|
FileMenuClass.now_path = path |
186
|
|
|
|
187
|
|
|
def save_charactor_file(self): |
188
|
|
|
"""キャラクターファイルの保存準備. |
189
|
|
|
|
190
|
|
|
・それぞれの項目をxml形式で保存する。 |
191
|
|
|
""" |
192
|
|
|
return '<?xml version="1.0"?>\n<data>\n\t<call>{0}</call>\ |
193
|
|
|
\n\t<name>{1}</name>\n\t<sex>{2}</sex>\n\t<birthday>{3}</birthday>\ |
194
|
|
|
\n\t<body>{4}</body>\n</data>'.format( |
195
|
|
|
self.APP.txt_yobi_name.get(), |
196
|
|
|
self.APP.txt_name.get(), |
197
|
|
|
self.APP.var.get(), |
198
|
|
|
self.APP.txt_birthday.get(), |
199
|
|
|
self.APP.text_body.get( |
200
|
|
|
'1.0', |
201
|
|
|
'end -1c' |
202
|
|
|
) |
203
|
|
|
) |
204
|
|
|
|
205
|
|
|
def tree_get_loop(self): |
206
|
|
|
"""ツリービューに挿入. |
207
|
|
|
|
208
|
|
|
・保存データからファイルを取得してツリービューに挿入する。 |
209
|
|
|
""" |
210
|
|
|
for val in self.TREE_FOLDER: |
211
|
|
|
self.APP.tree.insert('', 'end', val[0], text=val[1]) |
212
|
|
|
# フォルダのファイルを取得 |
213
|
|
|
path = "./{0}".format(val[0]) |
214
|
|
|
files = os.listdir(path) |
215
|
|
|
for filename in files: |
216
|
|
|
if os.path.splitext(filename)[1] == ".txt": |
217
|
|
|
self.APP.tree.insert( |
218
|
|
|
val[0], |
219
|
|
|
'end', |
220
|
|
|
text=os.path.splitext(filename)[0] |
221
|
|
|
) |
222
|
|
|
|