Skip to content

Commit

Permalink
Add "extract all" function (#22)
Browse files Browse the repository at this point in the history
* add extract all functionality

* use more correct include header
  • Loading branch information
itsmattkc authored Dec 12, 2024
1 parent b418637 commit 0b484ea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
52 changes: 51 additions & 1 deletion app/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "mainwindow.h"

#include <iostream>
#include <QFileDialog>
#include <QLineEdit>
#include <QMenuBar>
Expand Down Expand Up @@ -139,6 +138,8 @@ void MainWindow::InitializeMenuBar()

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

file_menu->addAction(tr("E&xtract All"), this, &MainWindow::ExtractAll);

file_menu->addSeparator();

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

bool MainWindow::ExtractAllRecursiveInternal(const QDir &dir, const si::Core *obj)
{
if (!dir.mkpath(QStringLiteral("."))) {
QMessageBox::critical(this, tr("Extract All Failed"), tr("Failed to create directory \"%1\". Try extracting somewhere else.").arg(dir.absolutePath()));
return false;
}

for (const Core *child : obj->GetChildren()) {
if (const Object *obj = dynamic_cast<const Object*>(child)) {
if (!obj->data().empty()) {
QString realFilename = QString::fromStdString(obj->filename());
realFilename = realFilename.mid(realFilename.lastIndexOf('\\')+1);

QString output = dir.filePath(realFilename);

if (!obj->ExtractToFile(output.toUtf8())) {
QMessageBox::critical(this, tr("Extract All Failed"), tr("Failed to create file \"%1\". Try extracting somewhere else.").arg(output));
return false;
}
}

if (obj->HasChildren()) {
// Extract its children too
if (!ExtractAllRecursiveInternal(QDir(dir.filePath(QString::fromStdString(obj->name()))), obj)) {
return false;
}
}
}
}

return true;
}

void MainWindow::NewFile()
{
model_.SetCore(nullptr);
Expand Down Expand Up @@ -354,6 +388,22 @@ void MainWindow::ViewSIFile()
}
}

void MainWindow::ExtractAll()
{
QString s = QFileDialog::getExistingDirectory(this, tr("Extract All To..."));
if (s.isEmpty()) {
return;
}

QDir dir(s);
if (!dir.exists()) {
QMessageBox::critical(this, tr("Extract All Failed"), tr("Directory \"%1\" is not valid. Try extracting somewhere else.").arg(s));
return;
}

ExtractAllRecursiveInternal(dir, &interleaf_);
}

void MainWindow::ExtraChanged()
{
if (last_set_data_) {
Expand Down
4 changes: 4 additions & 0 deletions app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <interleaf.h>
#include <object.h>
#include <QDir>
#include <QGroupBox>
#include <QMainWindow>
#include <QPlainTextEdit>
Expand Down Expand Up @@ -36,6 +37,8 @@ class MainWindow : public QMainWindow

QString GetOpenFileName();

bool ExtractAllRecursiveInternal(const QDir &dir, const si::Core *obj);

static const QString kFileFilter;

QStackedWidget *config_stack_;
Expand Down Expand Up @@ -78,6 +81,7 @@ private slots:
void ReplaceClicked();

void ViewSIFile();
void ExtractAll();

void ExtraChanged();
void LocationChanged(const si::Vector3 &v);
Expand Down

0 comments on commit 0b484ea

Please sign in to comment.