/
mainwindow.cpp
205 lines (164 loc) · 5.17 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QTextEdit>
#include "MemoryMappedFile.h"
#include "IffContainer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_szBaseTitle()
{
ui->setupUi(this);
m_szBaseTitle = windowTitle();
connect(this, SIGNAL(FileSelection(QString)), this, SLOT(onFileSelected(QString)));
QStringList treeHeaders;
treeHeaders << "Chunk"
//<< "Type"
<< "Offset"
<< "Size" ;
ui->treeWidget->setColumnCount(treeHeaders.size());
ui->treeWidget->setHeaderLabels(treeHeaders);
// if file given in command line
QStringList vCmdLine = QApplication::arguments();
if (vCmdLine.size() > 1)
{
emit FileSelection(vCmdLine[1]);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionAbout_triggered()
{
QTextEdit *pTxt = new QTextEdit(this);
pTxt->setWindowFlags(Qt::Window); //or Qt::Tool, Qt::Dialog if you like
pTxt->setReadOnly(true);
pTxt->append("qIffTool by Ilkka Prusi 2011");
pTxt->append("");
pTxt->append("This program is free to use and distribute. No warranties of any kind.");
pTxt->append("Program uses Qt 4.7.2 under LGPL v. 2.1");
pTxt->append("");
pTxt->append("Keyboard shortcuts:");
pTxt->append("");
pTxt->append("F = open file");
pTxt->append("Esc = close");
pTxt->append("? = about (this dialog)");
pTxt->append("");
pTxt->show();
}
void MainWindow::on_actionFile_triggered()
{
QString szFile = QFileDialog::getOpenFileName(this, tr("Open file"));
if (szFile != NULL)
{
emit FileSelection(szFile);
}
}
void MainWindow::onFileSelected(QString szArchiveFile)
{
ClearAll();
std::wstring szFile = szArchiveFile.toStdWString();
CMemoryMappedFile File;
if (File.Create(szFile.c_str()) == false)
{
ui->statusBar->showMessage("Failure opening file");
return;
}
CIffContainer Iff;
CIffHeader *pHead = Iff.ParseIffFile(File);
if (pHead == nullptr)
{
ui->statusBar->showMessage("No supported header");
return;
}
setWindowTitle(m_szBaseTitle + " - " + szArchiveFile);
QString szMsg;
szMsg.append("File size: ").append(QString::number(File.GetSize()))
.append(" File type: ").append(IdToString(pHead->m_iTypeID));
ui->statusBar->showMessage(szMsg);
// file head-chunk
QString szIdString;
szIdString = IdToString(pHead->m_iFileID);
szIdString += "=";
szIdString += IdToString(pHead->m_iTypeID);
QTreeWidgetItem *pTopItem = new QTreeWidgetItem((QTreeWidgetItem*)0);
pTopItem->setText(0, IdToString(pHead->m_iTypeID));
//pTopItem->setText(0, szIdString);
pTopItem->setText(1, QString::number(pHead->m_iOffset));
pTopItem->setText(2, QString::number(pHead->m_iDataSize));
ui->treeWidget->addTopLevelItem(pTopItem);
m_FormToItem.insert(pHead, pTopItem);
CIffHeader *pComposite = pHead->m_pComposite;
while (pComposite != nullptr)
{
HeaderToDisplay(pComposite);
pComposite = pComposite->m_pComposite;
}
ChunksToDisplay(pHead, pTopItem);
ui->treeWidget->expandAll();
ui->treeWidget->resizeColumnToContents(0);
//ui->treeWidget->sortByColumn(1);
}
void MainWindow::HeaderToDisplay(CIffHeader *pHead)
{
// note: should have parent when this method is called..
QTreeWidgetItem *pTopItem = m_FormToItem.value(pHead, nullptr);
if (pTopItem == nullptr)
{
// need to rethink..
// note: parent should be added first..
//QTreeWidgetItem *pParent = m_FormToItem.value(pHead->m_pParent, nullptr);
//pTopItem = new QTreeWidgetItem(pParent);
pTopItem = new QTreeWidgetItem((QTreeWidgetItem*)0);
pTopItem->setText(0, IdToString(pHead->m_iTypeID));
pTopItem->setText(1, QString::number(pHead->m_iOffset));
pTopItem->setText(2, QString::number(pHead->m_iDataSize));
//pParent->addChild(pTopItem);
m_FormToItem.insert(pHead, pTopItem);
}
ChunksToDisplay(pHead, pTopItem);
}
void MainWindow::ChunksToDisplay(CIffHeader *pHead, QTreeWidgetItem *pTopItem)
{
// root-level chunk-nodes
CIffChunk *pChunk = pHead->m_pFirst;
while (pChunk != nullptr)
{
QTreeWidgetItem *pChunkItem = new QTreeWidgetItem(pTopItem);
pChunkItem->setText(0, IdToString(pChunk->m_iChunkID));
pChunkItem->setText(1, QString::number(pChunk->m_iOffset));
pChunkItem->setText(2, QString::number(pChunk->m_iChunkSize));
pTopItem->addChild(pChunkItem);
CIffSubChunk *pSubChunk = pChunk->m_pSubChunk;
while (pSubChunk != nullptr)
{
QTreeWidgetItem *pSubItem = new QTreeWidgetItem(pChunkItem);
pSubItem->setText(0, IdToString(pSubChunk->m_iChunkID));
pSubItem->setText(1, QString::number(pSubChunk->m_iOffset));
pSubItem->setText(2, QString::number(pSubChunk->m_iSize));
pChunkItem->addChild(pSubItem);
pSubChunk = pSubChunk->m_pNextSub;
}
pChunk = pChunk->m_pNext;
}
}
// "unpack" ID-bytes to displayable string
QString MainWindow::IdToString(uint32_t u32Id)
{
uint32_t u32Mask = 0xFF;
QString szTemp;
szTemp += (char)(u32Id & u32Mask);
szTemp += (char)((u32Id & (u32Mask << 8)) >> 8);
szTemp += (char)((u32Id & (u32Mask << 16)) >> 16);
szTemp += (char)((u32Id & (u32Mask << 24)) >> 24);
return szTemp;
}
void MainWindow::ClearAll()
{
setWindowTitle(m_szBaseTitle);
m_FormToItem.clear();
ui->treeWidget->clear();
}