Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "extract all" function #22

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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