SIEdit/app/mainwindow.cpp

198 lines
4.9 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>
#include "siview/siview.h"
using namespace 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);
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_);
2022-07-11 04:48:50 -04:00
panel_wav_ = new WavPanel();
config_stack_->addWidget(panel_wav_);
panel_bmp_ = new BitmapPanel();
config_stack_->addWidget(panel_bmp_);
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)
{
Chunk si;
if (si.Read(s.toStdString())) {
SIViewDialog d(SIViewDialog::Import, &si, this);
if (d.exec() == QDialog::Accepted) {
2022-07-11 04:48:50 -04:00
model_.SetCore(nullptr);
interleaf_.Parse(&si);
model_.SetCore(&interleaf_);
}
} else {
QMessageBox::critical(this, QString(), tr("Failed to load Interleaf file."));
}
}
2022-06-23 21:36:00 -04:00
void MainWindow::InitializeMenuBar()
{
auto menubar = new QMenuBar();
auto file_menu = menubar->addMenu(tr("&File"));
auto new_action = file_menu->addAction(tr("&New"));
2022-06-23 21:36:00 -04:00
auto open_action = file_menu->addAction(tr("&Open"), this, &MainWindow::OpenFile, tr("Ctrl+O"));
auto save_action = file_menu->addAction(tr("&Save"));
auto save_as_action = file_menu->addAction(tr("Save &As"));
file_menu->addSeparator();
auto export_action = file_menu->addAction(tr("&Export"));
file_menu->addSeparator();
2022-06-23 21:36:00 -04:00
auto exit_action = file_menu->addAction(tr("E&xit"));
connect(exit_action, &QAction::triggered, this, &MainWindow::close);
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);
}
void MainWindow::ExtractObject(si::Object *obj)
{
QString filename = QString::fromStdString(obj->filename());
if (filename.isEmpty()) {
filename = QString::fromStdString(obj->name());
filename.append(QStringLiteral(".bin"));
}
QString s = QFileDialog::getSaveFileName(this, tr("Export Object"), filename);
if (!s.isEmpty()) {
QFile f(s);
if (f.open(QFile::WriteOnly)) {
2022-07-11 11:04:47 -04:00
bytearray b = obj->GetNormalizedData();
f.write(b.data(), b.size());
2022-07-11 04:48:50 -04:00
f.close();
} else {
QMessageBox::critical(this, QString(), tr("Failed to write to file \"%1\".").arg(s));
}
}
2022-06-24 13:46:25 -04:00
}
2022-06-23 21:36:00 -04:00
void MainWindow::OpenFile()
{
QString s = QFileDialog::getOpenFileName(this, QString(), QString(), tr("Interleaf Files (*.si)"));
if (!s.isEmpty()) {
OpenFilename(s);
2022-06-23 21:36:00 -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) {
2022-07-11 04:48:50 -04:00
switch (c->filetype()) {
case MxOb::WAV:
p = panel_wav_;
break;
case MxOb::STL:
p = panel_bmp_;
break;
}
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_);
}