Skip to content

Commit 0b484ea

Browse files
authored
Add "extract all" function (#22)
* add extract all functionality * use more correct include header
1 parent b418637 commit 0b484ea

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

app/mainwindow.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "mainwindow.h"
22

3-
#include <iostream>
43
#include <QFileDialog>
54
#include <QLineEdit>
65
#include <QMenuBar>
@@ -139,6 +138,8 @@ void MainWindow::InitializeMenuBar()
139138

140139
file_menu->addAction(tr("&View SI File"), tr("Ctrl+I"), this, &MainWindow::ViewSIFile);
141140

141+
file_menu->addAction(tr("E&xtract All"), this, &MainWindow::ExtractAll);
142+
142143
file_menu->addSeparator();
143144

144145
file_menu->addAction(tr("E&xit"), this, &MainWindow::close);
@@ -244,6 +245,39 @@ QString MainWindow::GetOpenFileName()
244245
return QFileDialog::getOpenFileName(this, QString(), QString(), kFileFilter);
245246
}
246247

248+
bool MainWindow::ExtractAllRecursiveInternal(const QDir &dir, const si::Core *obj)
249+
{
250+
if (!dir.mkpath(QStringLiteral("."))) {
251+
QMessageBox::critical(this, tr("Extract All Failed"), tr("Failed to create directory \"%1\". Try extracting somewhere else.").arg(dir.absolutePath()));
252+
return false;
253+
}
254+
255+
for (const Core *child : obj->GetChildren()) {
256+
if (const Object *obj = dynamic_cast<const Object*>(child)) {
257+
if (!obj->data().empty()) {
258+
QString realFilename = QString::fromStdString(obj->filename());
259+
realFilename = realFilename.mid(realFilename.lastIndexOf('\\')+1);
260+
261+
QString output = dir.filePath(realFilename);
262+
263+
if (!obj->ExtractToFile(output.toUtf8())) {
264+
QMessageBox::critical(this, tr("Extract All Failed"), tr("Failed to create file \"%1\". Try extracting somewhere else.").arg(output));
265+
return false;
266+
}
267+
}
268+
269+
if (obj->HasChildren()) {
270+
// Extract its children too
271+
if (!ExtractAllRecursiveInternal(QDir(dir.filePath(QString::fromStdString(obj->name()))), obj)) {
272+
return false;
273+
}
274+
}
275+
}
276+
}
277+
278+
return true;
279+
}
280+
247281
void MainWindow::NewFile()
248282
{
249283
model_.SetCore(nullptr);
@@ -354,6 +388,22 @@ void MainWindow::ViewSIFile()
354388
}
355389
}
356390

391+
void MainWindow::ExtractAll()
392+
{
393+
QString s = QFileDialog::getExistingDirectory(this, tr("Extract All To..."));
394+
if (s.isEmpty()) {
395+
return;
396+
}
397+
398+
QDir dir(s);
399+
if (!dir.exists()) {
400+
QMessageBox::critical(this, tr("Extract All Failed"), tr("Directory \"%1\" is not valid. Try extracting somewhere else.").arg(s));
401+
return;
402+
}
403+
404+
ExtractAllRecursiveInternal(dir, &interleaf_);
405+
}
406+
357407
void MainWindow::ExtraChanged()
358408
{
359409
if (last_set_data_) {

app/mainwindow.h

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <interleaf.h>
55
#include <object.h>
6+
#include <QDir>
67
#include <QGroupBox>
78
#include <QMainWindow>
89
#include <QPlainTextEdit>
@@ -36,6 +37,8 @@ class MainWindow : public QMainWindow
3637

3738
QString GetOpenFileName();
3839

40+
bool ExtractAllRecursiveInternal(const QDir &dir, const si::Core *obj);
41+
3942
static const QString kFileFilter;
4043

4144
QStackedWidget *config_stack_;
@@ -78,6 +81,7 @@ private slots:
7881
void ReplaceClicked();
7982

8083
void ViewSIFile();
84+
void ExtractAll();
8185

8286
void ExtraChanged();
8387
void LocationChanged(const si::Vector3 &v);

0 commit comments

Comments
 (0)