-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathedittext.py
207 lines (199 loc) · 8.3 KB
/
edittext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
import config
from language import translations
import pokeversion
from nds import narc, txt
files = pokeversion.textfiles
wintitle = "%s - Text Editor - PPRE"
class EditText(QMainWindow):
def __init__(self, parent=None):
super(EditText, self).__init__(parent)
self.setupUi()
self.dirty = False
self.openTextNarc("Main")
def setupUi(self):
self.setObjectName("EditText")
self.setWindowTitle(wintitle%"No file opened")
self.resize(600, 400)
icon = QIcon()
icon.addPixmap(QPixmap("PPRE.ico"), QIcon.Normal, QIcon.Off)
self.setWindowIcon(icon)
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QRect(0, 0, 600, 20))
self.menus = {}
self.menus["file"] = QMenu(self.menubar)
self.menus["file"].setTitle(translations["menu_file"])
self.menubar.addAction(self.menus["file"].menuAction())
self.menutasks = {}
self.menutasks["newtext"] = QAction(self.menus["file"])
self.menutasks["newtext"].setText(translations["menu_newtext"])
self.menus["file"].addAction(self.menutasks["newtext"])
QObject.connect(self.menutasks["newtext"],
QtCore.SIGNAL("triggered()"), self.newText)
for f in files[config.project["versioninfo"][0]]:
self.menutasks["opentext_"+f] = QAction(self.menus["file"])
self.menutasks["opentext_"+f].setText(
translations["menu_opentext"]+": "+f)
self.menus["file"].addAction(self.menutasks["opentext_"+f])
func = lambda x: (lambda: self.openTextNarc(x))
QObject.connect(self.menutasks["opentext_"+f],
QtCore.SIGNAL("triggered()"), func(f))
self.menutasks["savetext"] = QAction(self.menus["file"])
self.menutasks["savetext"].setText(translations["menu_savetext"])
self.menus["file"].addAction(self.menutasks["savetext"])
QObject.connect(self.menutasks["savetext"],
QtCore.SIGNAL("triggered()"), self.saveText)
self.menutasks["close"] = QAction(self.menus["file"])
self.menutasks["close"].setText(translations["menu_close"])
self.menus["file"].addAction(self.menutasks["close"])
QObject.connect(self.menutasks["close"],
QtCore.SIGNAL("triggered()"), self.quit)
self.menubar.addAction(self.menus["file"].menuAction())
self.menus["tools"] = QMenu(self.menubar)
self.menus["tools"].setTitle(translations["menu_tools"])
self.menutasks["search"] = QAction(self.menus["tools"])
self.menutasks["search"].setText(translations["menu_search"])
self.menus["tools"].addAction(self.menutasks["search"])
QObject.connect(self.menutasks["search"],
QtCore.SIGNAL("triggered()"), self.search)
self.menubar.addAction(self.menus["tools"].menuAction())
self.setMenuBar(self.menubar)
self.statusbar = QStatusBar(self)
self.setStatusBar(self.statusbar)
self.widgetcontainer = QWidget(self)
self.widgetcontainer.setObjectName("widgetcontainer")
self.textfilelist = QComboBox(self.widgetcontainer)
self.textfilelist.setGeometry(QRect(50, 25, 200, 20))
QObject.connect(self.textfilelist,
QtCore.SIGNAL("currentIndexChanged(int)"), self.openText)
self.textedit = QTextEdit(self.widgetcontainer)
self.textedit.setGeometry(QRect(50, 50, 500, 300))
self.textedit.setEnabled(False)
QObject.connect(self.textedit,
QtCore.SIGNAL("textChanged()"), self.changed)
self.setCentralWidget(self.widgetcontainer)
QMetaObject.connectSlotsByName(self)
def newText(self):
print("newText() Unimplemented")
def openTextNarc(self, f):
if not self.checkClean():
return
self.fname = config.project["directory"]+"fs"+files[
config.project["versioninfo"][0]][f]
self.narc = narc.NARC(open(self.fname, "rb").read())
self.textfilelist.clear()
for i in range(self.narc.btaf.getEntryNum()):
self.textfilelist.addItem(str(i))
self.openText(0)
def openText(self, i):
if not self.checkClean(False):
return
self.currentfile = i
self.textedit.setEnabled(True)
version = config.project["versioninfo"]
if pokeversion.gens[version[0]] == 4:
text = txt.gen4get(self.narc.gmif.files[self.currentfile])
elif pokeversion.gens[version[0]] == 5:
text = txt.gen5get(self.narc.gmif.files[self.currentfile])
buff = ""
for entry in text:
buff += entry[0]+": "+entry[1]+"\n\n"
self.textedit.setText(buff.strip("\n"))
self.dirty = False
self.updateCurrentFileLabel()
def saveText(self):
if not self.dirty:
return
version = config.project["versioninfo"]
texts = []
for lines in unicode(self.textedit.toPlainText()).split("\n\n"):
t = lines.split(": ", 1)
if len(t) < 2:
t.extend([""])
texts.append(t)
if pokeversion.gens[version[0]] == 4:
self.narc.gmif.files[self.currentfile] = txt.gen4put(texts)
elif pokeversion.gens[version[0]] == 5:
self.narc.gmif.files[self.currentfile] = txt.gen5put(texts)
self.narc.toFile(open(self.fname, "wb"))
self.dirty = False
self.updateCurrentFileLabel()
self.openText(self.currentfile)
def changed(self):
if not self.dirty:
self.dirty = True
self.updateCurrentFileLabel()
def updateCurrentFileLabel(self):
if self.dirty:
dirt = " [modified]"
else:
dirt = ""
self.setWindowTitle(wintitle%(str(self.currentfile)+dirt))
def quit(self):
if not self.checkClean():
return
self.close()
def checkClean(self, allowCancel=True):
if self.dirty:
if allowCancel:
prompt = QMessageBox.question(self, "Close?",
"This text file has been modified.\n"+
"Do you want to save this text file?",
QMessageBox.Yes, QMessageBox.No, QMessageBox.Cancel)
if prompt == QMessageBox.Cancel:
return False
else:
prompt = QMessageBox.question(self, "Close?",
"This text file has been modified.\n"+
"Do you want to save this text file?",
QMessageBox.Yes, QMessageBox.No)
if prompt == QMessageBox.Yes:
if not self.saveText():
return False
return True
def search(self):
s, ok = QInputDialog.getText(None, "Search all files", "Search (Case Insensitive)")
if not ok or not s:
return
s = str(s).lower()
hits = []
version = config.project["versioninfo"]
for i, f in enumerate(self.narc.gmif.files):
if pokeversion.gens[version[0]] == 4:
texts = txt.gen4get(f)
else:
texts = txt.gen5get(f)
for entry in texts:
if s in entry[1].lower():
hits.append((i, entry[0], entry[1]))
if not hits:
QMessageBox.information(None, "Search results", "None found")
return
dlg = QMainWindow(config.mw)
dlg.setWindowTitle("Search Results")
dlg.resize(600, 400)
wdgt = QWidget(dlg)
scroller = QScrollArea(wdgt)
scroller.setGeometry(QRect(0, 0, 600, 400))
container = QWidget(scroller)
y = 10
for result in hits:
label = QLabel(container)
label.setText("[%s] %s: %s"%result)
label.setGeometry(QRect(0, y, 580, 20))
y += 20
container.setGeometry(QRect(0, 0, 600, y))
scroller.setWidget(container)
dlg.setCentralWidget(wdgt)
dlg.show()
def create():
if not config.project:
QMessageBox.critical(None, translations["error_noromloaded_title"],
translations["error_noromloaded"])
return
EditText(config.mw).show()