-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
346 lines (286 loc) · 10.7 KB
/
mainwindow.cpp
File metadata and controls
346 lines (286 loc) · 10.7 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "mainwindow.h"
#include <QGridLayout>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMap>
#include <QString>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QCoreApplication>
#include <QSettings>
//#include <QText>
QVector<PPC> commands0, commands2;
struct AppConfig {
QString path1;
QString path2;
QString path3;
QString text;
int n1 = 0;
int n2 = 0;
};
inline AppConfig loadConfig(const QString& iniFile) {
QSettings s(iniFile, QSettings::IniFormat);
AppConfig cfg;
s.beginGroup("Paths");
cfg.path1 = s.value("path1").toString();
cfg.path2 = s.value("path2").toString();
cfg.path3 = s.value("path3").toString();
s.endGroup();
s.beginGroup("Params");
cfg.text = s.value("filter").toString();
cfg.n1 = s.value("p1").toInt();
cfg.n2 = s.value("p2").toInt();
s.endGroup();
return cfg;
}
static QWidget* createScrollableButtonList(const QString& labelPrefix, QVector<IdButton*> &buttons2,
QTextEdit *textEdit, QVector<PPC> &commands1, bool second) {
QWidget *listWidget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(listWidget);
for (int i = 0; i < commands1.length(); ++i) {
auto* pb2= new IdButton( commands1[i].content,commands1[i].name, *textEdit);//, m_content2, textEdit);
buttons2.append(pb2);
// if(i==2){
QPalette palette = pb2->palette();
if(commands1[i].unique){
if(second) palette.setColor(QPalette::Button, QColor(Qt::green)); else
palette.setColor(QPalette::Button, QColor(Qt::cyan));
}
else if(commands1[i].same){
palette.setColor(QPalette::Button, QColor(Qt::gray));
}
else {
palette.setColor(QPalette::Button, QColor(Qt::yellow));
}
if(commands1[i].filter)
palette.setColor(QPalette::Button, QColor(Qt::red));
pb2->setPalette(palette);
pb2->setAutoFillBackground(true);
pb2->repaint();
layout->addWidget(pb2);
}
layout->addStretch();
return listWidget;
}
void MainWindow::createMenus()
{
// Create File menu
fileMenu = menuBar()->addMenu(tr("&File"));
QAction *openLeftAct = fileMenu->addAction(tr("Open Left..."), this, &MainWindow::openLeftFile);
openLeftAct->setShortcut(QKeySequence::Open);
QAction *openRightAct = fileMenu->addAction(tr("Open Right..."), this, &MainWindow::openRightFile);
openRightAct->setShortcut(tr("Ctrl+Shift+O"));
QAction *mergeAct = fileMenu->addAction(tr("&Let's Merge!!!"), this, &MainWindow::mergeFile);
mergeAct->setShortcut(tr("Ctrl+M"));
/*
fileMenu->addSeparator();
QAction *saveLeftAct = fileMenu->addAction(tr("Save Left..."), this, &MainWindow::saveLeftFile);
saveLeftAct->setShortcut(QKeySequence::Save);
QAction *saveRightAct = fileMenu->addAction(tr("Save Right..."), this, &MainWindow::saveRightFile);
saveRightAct->setShortcut(tr("Ctrl+Shift+S"));
fileMenu->addSeparator();
*/
QAction *quitAct = fileMenu->addAction(tr("&Quit"), this, &MainWindow::quit);
quitAct->setShortcut(QKeySequence::Quit);
helpMenu = menuBar()->addMenu(tr("&Filter"));
// helpMenu = menuBar()->addMenu(tr("&Let's Merge!!!"));
// Create Help menu
helpMenu = menuBar()->addMenu(tr("&Help"));
QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about);
}
void MainWindow::mergeFile()
{
QString wfileName = "D:/MT2/qt_project/recompiler.cpp"; // Or provide a full path
QFile wfile(wfileName);
if (!wfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
// Handle error: file could not be opened
qDebug() << "Could not open file for writing.";
return;
}
QTextStream out(&wfile);
// for (int i=0; i<commands2.length(); i++)
// {
// out << commands2[i].content;
// out <<Qt::endl;
// out <<Qt::endl;
// }
// for (int i=0; i<commands0.length(); i++)
// if(commands0[i].unique){
// out << commands0[i].content;
// out <<Qt::endl;
// out <<Qt::endl;
// }
for (int i=0; i<commands0.length(); i++)
{
out << commands0[i].content;
out <<Qt::endl;
out <<Qt::endl;
}
for (int i=0; i<commands2.length(); i++)
if(commands2[i].unique&&!commands2[i].filter){
out << commands2[i].content;
out <<Qt::endl;
out <<Qt::endl;
}
wfile.close();
}
void MainWindow::loadFile(const QString &filePath, bool isRight)
{
}
void MainWindow::loadCommands(const QString &fileName, QVector<PPC> &commands1, bool second){
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
// Handle error: file could not be opened
return;
}
QTextStream in(&file);
QString searchString = "case PPC_INST_";
QString searchString2 = " switch (id)";
QString filterString = "tsimd";
int lineNumber = 0;
PPC *testppc = new PPC;
bool content=false;
bool foundedsw=false;
while (!in.atEnd()) {
QString line = in.readLine();
lineNumber++;
if(foundedsw){
if (line.contains(searchString, Qt::CaseInsensitive)) { // Case-insensitive search
qDebug() << "String found on line:" << lineNumber << " - " << line;
// Additional actions can be performed here, e.g., storing line numbers
int underscoreIndex = line.indexOf('_');
// line.remove(0, 6);
QString resultString = line.mid(underscoreIndex + 1);
resultString.remove(0, 5);
resultString.chop(1);
testppc->name=resultString;
testppc->sline=lineNumber;
content=true;
testppc->content.append(line);
testppc->content.append("\n");
}
else if (line.contains("break;", Qt::CaseInsensitive)) { // Case-insensitive search
qDebug() << "break found on line:" << lineNumber << " - " << line;
// Additional actions can be performed here, e.g., storing line numbers
testppc->eline=lineNumber;
if(second){
// foreach(auto ppc2, commands1){
for(int j=0;j<commands0.length();j++){
auto &ppc2 =commands0[j];
if(ppc2.name==testppc->name){
ppc2.unique=false;
testppc->unique=false;
if( ppc2.content!=testppc->content){
ppc2.same=false;
testppc->same=false;
}
}
}
}
testppc->content.append(line); //- break;
// testppc->content.append("\n");
commands1.append(*testppc);
//set default
content=false;
testppc->same=true;
testppc->unique=true;
testppc->filter=false;
testppc->content="";
}
else if (line.contains("return false;", Qt::CaseInsensitive)) {
break;
}
else{
if(content){
testppc->content.append(line);
testppc->content.append("\n");
if(testppc->content.contains(filterString))//(QStringLiteral("\t"),Qt::CaseInsensitive))
testppc->filter=true;
}
}
}
else{
if (line.contains(searchString2, Qt::CaseInsensitive)) foundedsw=true;
}
}
file.close();
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("PPC Merger");
resize(1200, 800);
createMenus();
const auto cfg = loadConfig(QCoreApplication::applicationDirPath() + "/default.ini");
//load of recomps
QString fileName = "D:/MT2/qt_project/c/recompiler.cpp";
QString fileName2 = "D:/MT2/qt_project/o/recompiler.cpp";
loadCommands(fileName,commands0,0);
loadCommands(fileName2,commands2,1);
qDebug() << "PPC from 1:" << commands0.length();
qDebug() << "PPC from 2:" << commands2.length();
QWidget *central = new QWidget(this);
QGridLayout *grid = new QGridLayout(central);
// Set proportional resizing
grid->setColumnStretch(0, 4);
grid->setColumnStretch(1, 5);
grid->setRowStretch(0, 70);
grid->setRowStretch(1, 30);
leftTextEdit1 = new QTextEdit(this);
rightTextEdit1 = new QTextEdit(this);
// Top-left
QScrollArea *scrollAreaTL = new QScrollArea;
scrollAreaTL->setWidgetResizable(true);
scrollAreaTL->setWidget(createScrollableButtonList("Top Left Button", buttons, leftTextEdit1, commands0,0));
grid->addWidget(scrollAreaTL, 0, 0);
// Top-right
QScrollArea *scrollAreaTR = new QScrollArea;
scrollAreaTR->setWidgetResizable(true);
scrollAreaTR->setWidget(createScrollableButtonList("Top Right Button", buttons, rightTextEdit1, commands2,1));
grid->addWidget(scrollAreaTR, 0, 1);
leftTextEdit1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
grid->addWidget(leftTextEdit1, 1, 0);
rightTextEdit1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
grid->addWidget(rightTextEdit1, 1, 1);
setCentralWidget(central);
}
void MainWindow::openLeftFile()
{
QString filePath = QFileDialog::getOpenFileName(this, "Open Left File");
if (!filePath.isEmpty()) {
leftFilePath = filePath;
loadFile(filePath, true);
}
}
void MainWindow::openRightFile()
{
QString filePath = QFileDialog::getOpenFileName(this, "Open Right File");
if (!filePath.isEmpty()) {
rightFilePath = filePath;
loadFile(filePath, false);
}
}
void MainWindow::about()
{
QMessageBox::about(this, "About PPC Merger",
"PPC Merger v1.0\n\n"
"A simple tool to compare and search through two files side by side.\n\n"
"Features:\n"
"- Open and compare two files\n"
"- Search functionality\n"
"- Save files\n"
"- Results listing");
}
/*
void MainWindow::searchText()
{
updateSearchResults();
}
*/
void MainWindow::quit()
{
QApplication::quit();
}