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