2022-06-23 21:36:00 -04:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMenuBar>
|
2022-07-10 23:51:49 -04:00
|
|
|
#include <QMessageBox>
|
2022-06-23 21:36:00 -04:00
|
|
|
#include <QSplitter>
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
#include "interleaf.h"
|
|
|
|
#include "siview/siview.h"
|
|
|
|
|
2022-06-27 12:44:50 -04:00
|
|
|
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);
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
tree_ = new QTreeView();
|
|
|
|
//tree_->setModel(&chunk_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
|
|
|
|
|
|
|
config_stack_ = new QStackedWidget();
|
|
|
|
splitter->addWidget(config_stack_);
|
|
|
|
|
|
|
|
panel_blank_ = new Panel();
|
|
|
|
config_stack_->addWidget(panel_blank_);
|
|
|
|
|
2022-06-23 21:36:00 -04:00
|
|
|
InitializeMenuBar();
|
2022-06-24 13:46:25 -04:00
|
|
|
|
|
|
|
splitter->setSizes({99999, 99999});
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 12:44:50 -04:00
|
|
|
void MainWindow::OpenFilename(const QString &s)
|
|
|
|
{
|
2022-07-10 23:51:49 -04:00
|
|
|
Chunk si;
|
|
|
|
if (si.Read(s.toStdString())) {
|
|
|
|
SIViewDialog d(SIViewDialog::Import, &si, this);
|
|
|
|
if (d.exec() == QDialog::Accepted) {
|
|
|
|
Interleaf interleaf;
|
|
|
|
interleaf.Parse(&si);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QMessageBox::critical(this, QString(), tr("Failed to load Interleaf file."));
|
|
|
|
}
|
2022-06-27 12:44:50 -04:00
|
|
|
}
|
|
|
|
|
2022-06-23 21:36:00 -04:00
|
|
|
void MainWindow::InitializeMenuBar()
|
|
|
|
{
|
|
|
|
auto menubar = new QMenuBar();
|
|
|
|
|
|
|
|
auto file_menu = menubar->addMenu(tr("&File"));
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
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();
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
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-06-27 12:44:50 -04:00
|
|
|
void MainWindow::SetPanel(Panel *panel, si::Chunk *chunk)
|
2022-06-24 13:46:25 -04:00
|
|
|
{
|
|
|
|
auto current = static_cast<Panel*>(config_stack_->currentWidget());
|
|
|
|
current->SetData(nullptr);
|
|
|
|
|
|
|
|
config_stack_->setCurrentWidget(panel);
|
2022-06-27 12:44:50 -04:00
|
|
|
panel->SetData(chunk);
|
|
|
|
last_set_data_ = chunk;
|
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()) {
|
2022-06-27 12:44:50 -04:00
|
|
|
OpenFilename(s);
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 13:46:25 -04:00
|
|
|
|
|
|
|
void MainWindow::SelectionChanged(const QModelIndex &index)
|
|
|
|
{
|
2022-06-27 12:44:50 -04:00
|
|
|
Panel *p = panel_blank_;
|
|
|
|
Chunk *c = static_cast<Chunk*>(index.internalPointer());
|
2022-06-24 13:46:25 -04:00
|
|
|
|
2022-06-27 12:44:50 -04:00
|
|
|
if (c) {
|
2022-07-10 23:51:49 -04:00
|
|
|
// HECK
|
2022-06-24 13:46:25 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 12:44:50 -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()
|
|
|
|
{
|
2022-07-10 23:51:49 -04:00
|
|
|
auto selected = tree_->selectionModel()->selectedRows();
|
2022-07-04 02:34:49 -04:00
|
|
|
if (selected.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const QModelIndex &i : selected) {
|
|
|
|
if (Chunk *chunk = static_cast<Chunk*>(i.internalPointer())) {
|
|
|
|
QString filename(chunk->data("FileName"));
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
filename = QString(chunk->data("Name"));
|
|
|
|
filename.append(QStringLiteral(".bin"));
|
|
|
|
}
|
|
|
|
if (filename.isEmpty()) {
|
2022-07-11 00:09:39 -04:00
|
|
|
filename = QStringLiteral("%1_%2.bin").arg(QString::fromLatin1((const char *) &chunk->id(), sizeof(uint32_t)),
|
2022-07-04 02:34:49 -04:00
|
|
|
QString::number(chunk->offset(), 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString s = QFileDialog::getSaveFileName(this, tr("Export Object"), filename);
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
//chunk->Export()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|