SIEdit/app/mainwindow.cpp

357 lines
8.6 KiB
C++
Raw Normal View History

2022-06-23 21:36:00 -04:00
#include "mainwindow.h"
#include <iostream>
#include <QFileDialog>
#include <QMenuBar>
#include <QMessageBox>
2022-06-23 21:36:00 -04:00
#include <QSplitter>
2022-07-18 03:27:00 -04:00
#include "siview/siview.h"
using namespace si;
2022-07-18 03:27:00 -04:00
const QString MainWindow::kFileFilter = tr("Interleaf Files (*.si)");
2022-06-23 21:36:00 -04:00
MainWindow::MainWindow(QWidget *parent) :
2022-06-24 13:46:25 -04:00
QMainWindow{parent},
last_set_data_(nullptr)
2022-06-23 21:36:00 -04:00
{
auto splitter = new QSplitter();
2022-06-24 13:46:25 -04:00
splitter->setChildrenCollapsible(false);
2022-06-23 21:36:00 -04:00
this->setCentralWidget(splitter);
tree_ = new QTreeView();
2022-07-11 04:48:50 -04:00
tree_->setModel(&model_);
tree_->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tree_->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &MainWindow::SelectionChanged);
connect(tree_, &QTreeView::customContextMenuRequested, this, &MainWindow::ShowContextMenu);
splitter->addWidget(tree_);
2022-06-24 13:46:25 -04:00
2022-07-11 04:48:50 -04:00
auto config_area = new QWidget();
splitter->addWidget(config_area);
auto config_layout = new QVBoxLayout(config_area);
action_grp_ = new QGroupBox();
config_layout->addWidget(action_grp_);
auto action_layout = new QHBoxLayout(action_grp_);
action_layout->addStretch();
auto extract_btn = new QPushButton(tr("Extract"));
action_layout->addWidget(extract_btn);
connect(extract_btn, &QPushButton::clicked, this, &MainWindow::ExtractClicked);
auto replace_btn = new QPushButton(tr("Replace"));
action_layout->addWidget(replace_btn);
2022-07-18 03:27:00 -04:00
connect(replace_btn, &QPushButton::clicked, this, &MainWindow::ReplaceClicked);
2022-07-11 04:48:50 -04:00
action_layout->addStretch();
2022-06-24 13:46:25 -04:00
config_stack_ = new QStackedWidget();
2022-07-11 04:48:50 -04:00
config_layout->addWidget(config_stack_);
2022-06-24 13:46:25 -04:00
panel_blank_ = new Panel();
config_stack_->addWidget(panel_blank_);
panel_media_ = new MediaPanel();
config_stack_->addWidget(panel_media_);
2022-07-11 04:48:50 -04:00
properties_group_ = new QGroupBox();
config_layout->addWidget(properties_group_);
auto properties_layout = new QGridLayout(properties_group_);
{
int prow = 0;
properties_layout->addWidget(new QLabel(tr("Location")), prow, 0);
m_LocationEdit = new Vector3Edit();
connect(m_LocationEdit, &Vector3Edit::changed, this, &MainWindow::LocationChanged);
properties_layout->addWidget(m_LocationEdit, prow, 1);
prow++;
2023-08-23 17:34:23 -04:00
properties_layout->addWidget(new QLabel(tr("Up")), prow, 0);
m_UpEdit = new Vector3Edit();
connect(m_UpEdit, &Vector3Edit::changed, this, &MainWindow::UpChanged);
properties_layout->addWidget(m_UpEdit, prow, 1);
prow++;
properties_layout->addWidget(new QLabel(tr("Start Time")), prow, 0);
start_time_edit_ = new QSpinBox();
start_time_edit_->setMinimum(0);
start_time_edit_->setMaximum(INT_MAX);
connect(start_time_edit_, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::StartTimeChanged);
properties_layout->addWidget(start_time_edit_, prow, 1);
}
2022-06-23 21:36:00 -04:00
InitializeMenuBar();
2022-06-24 13:46:25 -04:00
splitter->setSizes({99999, 99999});
2022-07-11 11:04:47 -04:00
setWindowTitle(tr("SI Editor"));
2022-06-23 21:36:00 -04:00
}
void MainWindow::OpenFilename(const QString &s)
{
tree_->clearSelection();
SetPanel(panel_blank_, nullptr);
2022-07-17 21:51:16 -04:00
model_.SetCore(nullptr);
2022-07-18 03:27:00 -04:00
if (OpenInterleafFileInternal(this, &interleaf_, s)) {
//tree_->blockSignals(true);
2022-07-17 21:51:16 -04:00
model_.SetCore(&interleaf_);
// tree_->blockSignals(false);
}
}
2022-06-23 21:36:00 -04:00
void MainWindow::InitializeMenuBar()
{
auto menubar = new QMenuBar();
auto file_menu = menubar->addMenu(tr("&File"));
2022-07-18 03:27:00 -04:00
file_menu->addAction(tr("&New"), this, &MainWindow::NewFile, tr("Ctrl+N"));
file_menu->addAction(tr("&Open"), this, &MainWindow::OpenFile, tr("Ctrl+O"));
2022-07-18 03:27:00 -04:00
file_menu->addAction(tr("&Save"), this, &MainWindow::SaveFile, tr("Ctrl+S"));
2022-06-23 21:36:00 -04:00
2022-07-18 03:27:00 -04:00
file_menu->addAction(tr("Save &As"), this, &MainWindow::SaveFileAs, tr("Ctrl+Shift+S"));
2022-06-23 21:36:00 -04:00
file_menu->addSeparator();
file_menu->addAction(tr("&View SI File"), this, &MainWindow::ViewSIFile, tr("Ctrl+I"));
file_menu->addSeparator();
2022-07-18 03:27:00 -04:00
file_menu->addAction(tr("E&xit"), this, &MainWindow::close);
2022-06-23 21:36:00 -04:00
setMenuBar(menubar);
}
2022-07-11 04:48:50 -04:00
void MainWindow::SetPanel(Panel *panel, si::Object *chunk)
2022-06-24 13:46:25 -04:00
{
auto current = static_cast<Panel*>(config_stack_->currentWidget());
current->SetData(nullptr);
config_stack_->setCurrentWidget(panel);
panel->SetData(chunk);
last_set_data_ = chunk;
2022-07-11 04:48:50 -04:00
action_grp_->setEnabled(chunk);
properties_group_->setEnabled(chunk);
if (chunk) {
m_LocationEdit->SetValue(chunk->location_);
2023-08-23 17:34:23 -04:00
m_UpEdit->SetValue(chunk->up_);
start_time_edit_->setValue(chunk->time_offset_);
}
2022-07-11 04:48:50 -04:00
}
void MainWindow::ExtractObject(si::Object *obj)
{
QString filename = QString::fromStdString(obj->filename());
if (filename.isEmpty()) {
filename = QString::fromStdString(obj->name());
filename.append(QStringLiteral(".bin"));
2022-07-11 17:20:05 -04:00
} else {
// Strip off directory
int index = filename.lastIndexOf('\\');
if (index != -1) {
filename = filename.mid(index+1);
}
2022-07-11 04:48:50 -04:00
}
QString s = QFileDialog::getSaveFileName(this, tr("Export Object"), filename);
if (!s.isEmpty()) {
2022-07-18 03:27:00 -04:00
if (!obj->ExtractToFile(
#ifdef Q_OS_WINDOWS
s.toStdWString().c_str()
#else
s.toUtf8()
#endif
)) {
2022-07-11 04:48:50 -04:00
QMessageBox::critical(this, QString(), tr("Failed to write to file \"%1\".").arg(s));
}
}
2022-06-24 13:46:25 -04:00
}
2022-07-18 03:27:00 -04:00
void MainWindow::ReplaceObject(si::Object *obj)
{
QString s = QFileDialog::getOpenFileName(this, tr("Replace Object"));
if (!s.isEmpty()) {
if (obj->ReplaceWithFile(
2022-07-18 03:27:00 -04:00
#ifdef Q_OS_WINDOWS
s.toStdWString().c_str()
#else
s.toUtf8()
#endif
)) {
static_cast<Panel*>(config_stack_->currentWidget())->ResetData();
} else {
2022-07-18 03:27:00 -04:00
QMessageBox::critical(this, QString(), tr("Failed to open to file \"%1\".").arg(s));
}
}
}
bool MainWindow::OpenInterleafFileInternal(QWidget *parent, si::Interleaf *interleaf, const QString &s)
{
Interleaf::Error r = interleaf->Read(
#ifdef Q_OS_WINDOWS
s.toStdWString().c_str()
#else
s.toUtf8()
#endif
);
if (r == Interleaf::ERROR_SUCCESS) {
return true;
} else {
QMessageBox::critical(parent, QString(), tr("Failed to load Interleaf file: %1").arg(r));
return false;
}
}
QString MainWindow::GetOpenFileName()
{
return QFileDialog::getOpenFileName(this, QString(), QString(), kFileFilter);
}
void MainWindow::NewFile()
{
model_.SetCore(nullptr);
interleaf_.Clear();
model_.SetCore(&interleaf_);
}
2022-06-23 21:36:00 -04:00
void MainWindow::OpenFile()
{
2022-07-18 03:27:00 -04:00
QString s = GetOpenFileName();
2022-06-23 21:36:00 -04:00
if (!s.isEmpty()) {
OpenFilename(s);
2022-06-23 21:36:00 -04:00
}
}
2022-06-24 13:46:25 -04:00
2022-07-18 03:27:00 -04:00
bool MainWindow::SaveFile()
2022-07-11 17:20:05 -04:00
{
2022-07-18 03:27:00 -04:00
if (current_filename_.isEmpty()) {
return SaveFileAs();
} else {
Interleaf::Error r = interleaf_.Write(
#ifdef Q_OS_WINDOWS
current_filename_.toStdWString().c_str()
#else
current_filename_.toUtf8()
#endif
);
if (r == Interleaf::ERROR_SUCCESS) {
return true;
} else {
QMessageBox::critical(this, QString(), tr("Failed to write SI file: %1").arg(r));
return false;
}
}
}
bool MainWindow::SaveFileAs()
{
current_filename_ = QFileDialog::getSaveFileName(this, QString(), QString(), kFileFilter);
if (!current_filename_.isEmpty()) {
return SaveFile();
}
return false;
2022-07-11 17:20:05 -04:00
}
2022-06-24 13:46:25 -04:00
void MainWindow::SelectionChanged(const QModelIndex &index)
{
Panel *p = panel_blank_;
2022-07-11 04:48:50 -04:00
Object *c = dynamic_cast<Object*>(static_cast<Core*>(index.internalPointer()));
2022-06-24 13:46:25 -04:00
if (c) {
p = panel_media_;
2022-06-24 13:46:25 -04:00
}
if (p != config_stack_->currentWidget() || c != last_set_data_) {
SetPanel(p, c);
2022-06-24 13:46:25 -04:00
}
}
2022-07-04 02:34:49 -04:00
void MainWindow::ShowContextMenu(const QPoint &p)
{
QMenu menu(this);
QAction *extract_action = menu.addAction(tr("E&xtract"));
connect(extract_action, &QAction::triggered, this, &MainWindow::ExtractSelectedItems);
menu.exec(static_cast<QWidget*>(sender())->mapToGlobal(p));
}
void MainWindow::ExtractSelectedItems()
{
auto selected = tree_->selectionModel()->selectedRows();
2022-07-04 02:34:49 -04:00
if (selected.empty()) {
return;
}
for (const QModelIndex &i : selected) {
2022-07-11 04:48:50 -04:00
if (Object *obj = dynamic_cast<Object*>(static_cast<Core*>(i.internalPointer()))) {
ExtractObject(obj);
2022-07-04 02:34:49 -04:00
}
}
}
2022-07-11 04:48:50 -04:00
void MainWindow::ExtractClicked()
{
ExtractObject(last_set_data_);
}
2022-07-18 03:27:00 -04:00
void MainWindow::ReplaceClicked()
{
ReplaceObject(last_set_data_);
}
void MainWindow::ViewSIFile()
{
QString s = GetOpenFileName();
if (!s.isEmpty()) {
std::unique_ptr<Interleaf> temp = std::make_unique<Interleaf>();
if (OpenInterleafFileInternal(this, temp.get(), s)) {
SIViewDialog *v = new SIViewDialog(temp->GetInformation(), this);
2022-07-30 12:03:21 -04:00
v->SetSubtitle(QFileInfo(s).fileName());
2022-07-18 03:27:00 -04:00
v->temp = std::move(temp);
v->setAttribute(Qt::WA_DeleteOnClose);
v->show();
}
}
}
void MainWindow::LocationChanged(const Vector3 &v)
{
if (last_set_data_) {
last_set_data_->location_ = v;
}
}
2023-08-23 17:34:23 -04:00
void MainWindow::UpChanged(const si::Vector3 &v)
{
if (last_set_data_) {
last_set_data_->up_ = v;
}
}
void MainWindow::StartTimeChanged(int t)
{
if (last_set_data_) {
last_set_data_->time_offset_ = t;
}
}