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