-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
136 lines (112 loc) · 3.63 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
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
fsModel = NULL;
ui->setupUi(this);
// Add the "Select Partition" widgets to the toolbar
partitionPickerLabel.setText(QString("Partition:"));
ui->toolBar->addWidget(&partitionPickerLabel);
ui->toolBar->addWidget(&partitionPicker);
// Add a label to the status bar
statusLabel.setText(QString(""));
ui->statusBar->addWidget(&statusLabel);
connect(&filePicker, SIGNAL(fileSelected(QString)),
this, SLOT(loadImageFile(QString)));
connect(&partitionPicker, SIGNAL(currentIndexChanged(int)),
this, SLOT(selectNewPartition(int)));
connect(ui->actionSaveFile, SIGNAL(triggered()),
this, SLOT(saveFile()));
openLoadImagePanel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openLoadImagePanel()
{
filePicker.setNameFilter(tr("Disk Images (*.img; *.bin)"));
if (!filePicker.exec())
exit(0);
return;
}
void MainWindow::loadImageFile(const QString &imagePath)
{
imageFile.setFileName(imagePath);
if (!imageFile.open(QIODevice::ReadOnly)) {
openLoadImagePanel();
return;
}
disk.setFile(imageFile);
part.setDisk(&disk);
part.setPartition(part.count()-1);
fsys.setPartition(&part);
if (fsModel)
delete fsModel;
fsModel = new XtafFileSystemModel;
fsModel->setXtafFilesystem(&fsys);
ui->fsTree->setModel(fsModel);
connect(ui->fsTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(selectFile(QItemSelection,QItemSelection)));
partitionPicker.clear();
for (unsigned int i=0; i<part.count(); i++)
partitionPicker.addItem(part.name(i), QVariant(i));
partitionPicker.setCurrentIndex(part.count()-1);
ui->fsTree->header()->setStretchLastSection(false);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
ui->fsTree->header()->setResizeMode(0, QHeaderView::Stretch);
#else
ui->fsTree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
#endif
return;
}
void MainWindow::selectNewPartition(int newPart)
{
if (part.format(newPart) != XtafPart::XTAF) {
qWarning() << "Format for partition" << newPart << "not recognized";
return;
}
partitionPicker.setCurrentIndex(newPart);
fsModel->setXtafFilesystem(&fsys);
fsModel->setPartitionNumber(newPart);
}
void MainWindow::selectFile(const QItemSelection &selected, const QItemSelection &deselected)
{
Q_UNUSED(deselected);
QModelIndex index = selected.at(0).indexes().at(0);
if (!index.isValid()) {
statusLabel.setText(QString(""));
return;
}
XtafFile *file = static_cast<XtafFile *>(index.internalPointer());
QString labelText;
QTextStream(&labelText) << "Start clutser: " << file->startCluster();
statusLabel.setText(labelText);
selectedFile = file;
}
void MainWindow::saveFile()
{
if (!selectedFile)
return;
QFileDialog selectFile(ui->centralWidget);
selectFile.setFileMode(QFileDialog::AnyFile);
selectFile.setAcceptMode(QFileDialog::AcceptSave);
selectFile.selectFile(selectedFile->name());
if (!selectFile.exec()) {
qDebug() << "No file selected";
return;
}
QString fileName;
fileName = selectFile.selectedFiles()[0];
QFile saveFile;
saveFile.setFileName(fileName);
if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "Couldn't open save file:" << saveFile.errorString();
return;
}
selectedFile->write(saveFile);
saveFile.close();
}