-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
172 lines (134 loc) · 5.67 KB
/
mainwindow.cpp
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
#include <QFileDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "FPTFile.hpp"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
fptFile = nullptr;
ui->setupUi(this);
this->folderModel = new QStringListModel();
this->messageModel = new QStringListModel();
this->messageLineModel = new QStringListModel();
connect(ui->openFPTFolder, SIGNAL(clicked()), this, SLOT(openFPTFolderTapped()));
connect(ui->FPTList, SIGNAL(doubleClicked(QModelIndex)), this , SLOT(FPTListSelected(QModelIndex)));
connect(ui->FPTMessageList, SIGNAL(clicked(QModelIndex)), this , SLOT(FPTMessageListSelected(QModelIndex)));
connect(ui->ValidTranslate, SIGNAL(clicked()), this, SLOT(ValidTranslate()));
connect(ui->SaveTranslate, SIGNAL(clicked()), this, SLOT(SaveTranslate()));
connect(ui->SaveFPT, SIGNAL(clicked()), this, SLOT(SaveFPT()));
ui->FPTList->setModel(this->folderModel);
ui->FPTList->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->FPTMessageList->setModel(this->messageModel);
ui->FPTMessageList->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->FPTMessageLineList->setModel(this->messageLineModel);
}
MainWindow::~MainWindow()
{
delete ui;
delete folderModel;
delete messageModel;
delete messageLineModel;
}
void MainWindow::openFPTFolderTapped()
{
QString selectedPath = QFileDialog::getExistingDirectory();
QDir FPTScan(selectedPath);
QStringList files = FPTScan.entryList();
folderModel->removeRows(0,folderModel->rowCount());
foreach(QString file , files){
if(file.endsWith(".fpt")){
int row = folderModel->rowCount();
folderModel->insertRows(row, 1);
QModelIndex index = folderModel->index(row);
folderModel->setData(index, file);
}
}
fptFolderPath = selectedPath;
}
void MainWindow::FPTListSelected(QModelIndex index){
QString fpt = index.data().toString();
QString fptPath = fptFolderPath + "/" + fpt;
if(fptFile) delete fptFile;
fptFile = new FPTFile;
fptFile->ReadFile((const char *)fptPath.toUtf8().data());
messageModel->removeRows(0,messageModel->rowCount());
for(std::shared_ptr<TXTEntity> mess : fptFile->entities){
int row = messageModel->rowCount();
messageModel->insertRows(row, 1);
QModelIndex index = messageModel->index(row);
QString messageName(mess.get()->name);
messageModel->setData(index, messageName);
}
}
void MainWindow::FPTMessageListSelected(QModelIndex index){
std::shared_ptr<TXTEntity> mess = fptFile->entities[index.row()];
std::shared_ptr<unsigned char> str = fptFile->ReadString(mess.get());
QString qStr((const char *)str.get());
ui->messageContent->setPlainText(qStr);
QStringList lines = qStr.split(QString("\r\n"));
lines.removeLast();
ui->messageInfo->setText(QString().sprintf("%s 行数:%d 长度:%d",((QString)lines.at(0)).toUtf8().data(),lines.count(),mess.get()->length));
messageLineModel->removeRows(0,messageLineModel->rowCount());
messageLineModel->insertRows(0 ,lines.count());
for(int lineIdx = 0; lineIdx < lines.count(); lineIdx ++){
QString lineContent = lines.at(lineIdx);
QModelIndex index = messageModel->index(lineIdx);
messageLineModel->setData(index,lineContent);
}
}
void MainWindow::ValidTranslate(){
if(_validTranslate()){
ui->ErrorMsg->setText("OK");
}
else{
ui->ErrorMsg->setText("翻译的文本太长了.");
}
}
void MainWindow::SaveTranslate(){
if(!_validTranslate()){
ui->ErrorMsg->setText("翻译的文本太长了.");
}
QModelIndexList selectedIdxs = ui->FPTMessageList->selectionModel()->selectedIndexes();
if(selectedIdxs.count() == 0) return;
QStringList list;
for(int idx = 0; idx < messageLineModel->rowCount(); idx ++){
QModelIndex modelIndex = messageLineModel->index(idx);
list.append(modelIndex.data().toString());
}
QString trans = list.join("\r\n") + "\r\n";
QByteArray transByte = trans.toUtf8();
QModelIndex selected = selectedIdxs.at(0);
std::shared_ptr<TXTEntity> mess = fptFile->entities[selected.row()];
unsigned int realStart = fptFile->dataStart + 0x40 + mess.get()->location;
memset(fptFile->fileBuf.get() + realStart, 0 , mess.get()->length);
memcpy(fptFile->fileBuf.get() + realStart, transByte.data(), transByte.count() );
ui->ErrorMsg->setText("保存完成了.");
}
bool MainWindow::_validTranslate(){
QStringList list;
for(int idx = 0; idx < messageLineModel->rowCount(); idx ++){
QModelIndex modelIndex = messageLineModel->index(idx);
list.append(modelIndex.data().toString());
}
QString trans = list.join("\r\n") + "\r\n";
int row = ListViewSelectedRow(ui->FPTMessageList);
if(row == -1) return false;
std::shared_ptr<TXTEntity> mess = fptFile->entities[row];
return trans.toUtf8().count() <= (int)mess.get()->length;
}
int MainWindow::ListViewSelectedRow(QListView *view){
QModelIndexList selectedIdxs = ui->FPTMessageList->selectionModel()->selectedIndexes();
if(selectedIdxs.count() == 0) return -1;
QModelIndex selected = selectedIdxs.at(0);
return selected.row();
}
void MainWindow::SaveFPT(){
int ftpFileRow = ListViewSelectedRow(ui->FPTList);
QString ftpFileName = this->folderModel->index(ftpFileRow).data().toString();
QString fptPath = fptFolderPath + "/trans" + ftpFileName;
FILE *fp = fopen(fptPath.toUtf8().data(),"w+");
fwrite(this->fptFile->fileBuf.get(), 1, this->fptFile->fileLen, fp);
fclose(fp);
ui->ErrorMsg->setText("保存." + fptPath + " 完了.");
}