#include "mainwindow.hpp"
#include "groupview.hpp"
#include "mapdump.hpp"
#include "sliceview.hpp"
#include "diffview.hpp"
#include "library.hpp"
#include <QMdiArea>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QApplication>
#include <QMessageBox>
#include <filesystem>
#include <vector>
#define MAPMAN_VERSION "pre-alpha"
mapman_main_window::mapman_main_window() : QMainWindow()
{
l = nullptr;
cw = new QMdiArea();
setCentralWidget(cw);
gv = new group_view();
sv = new slice_view();
dv = new diff_view();
cw->addSubWindow(gv);
cw->addSubWindow(sv);
dv->hide();
this->setWindowTitle("Mapman");
cw->cascadeSubWindows();
auto fm = this->menuBar()->addMenu("&File");
auto cra = fm->addAction("Create / L&oad MapDB...");
auto cla = fm->addAction("&Close MapDB");
fm->addSeparator();
auto xca = fm->addAction("&Export Current Art...");
auto xaa = fm->addAction("Export &All Arts...");
fm->addSeparator();
auto lda = fm->addAction("&Load Map Dump...");
fm->addSeparator();
auto cta = fm->addAction("&Compare Map Tally...");
fm->addSeparator();
auto qa = fm->addAction("&Quit");
cla->setEnabled(false);
lda->setEnabled(false);
cta->setEnabled(false);
xca->setEnabled(false);
xaa->setEnabled(false);
connect(cra, &QAction::triggered, [this, cla, lda, cta, xca, xaa] {
QString fn = QFileDialog::getSaveFileName(this, "Create / Load MapDB", QString(), "*.mapdb", nullptr, QFileDialog::Option::DontConfirmOverwrite);
if (fn.length())
{
if (l) delete l;
l = new map_library();
std::filesystem::path p(fn.toStdWString());
if (!l->open_db(p))
{
delete l;
l = nullptr;
return;
}
sv->set_library(l);
gv->set_library(l);
sv->refresh();
gv->refresh_list();
cla->setEnabled(true);
lda->setEnabled(true);
cta->setEnabled(true);
xca->setEnabled(true);
xaa->setEnabled(true);
}
});
connect(cla, &QAction::triggered, [this, cla, lda, cta, xca, xaa] {
if (l) delete l;
l = nullptr;
cla->setEnabled(false);
lda->setEnabled(false);
cta->setEnabled(false);
xca->setEnabled(false);
xaa->setEnabled(false);
});
connect(xca, &QAction::triggered, gv, &group_view::export_current_group);
connect(xaa, &QAction::triggered, gv, &group_view::export_all_groups);
connect(lda, &QAction::triggered, [this] {
if (!l) return;
QString fn = QFileDialog::getOpenFileName(this, "Load Map Dump", QString(), "*.gz");
if (fn.length())
{
std::vector<map_t> m;
if (load_dumps(fn.toStdString().c_str(), m))
{
for (auto &map : m)
l->set_map(map);
sv->refresh();
}
}
});
connect(cta, &QAction::triggered, [this] {
if (!l) return;
QString fn = QFileDialog::getOpenFileName(this, "Select Map Tally", QString(), "*.gz");
if (fn.length())
{
auto tally = load_tally(fn.toStdString().c_str());
std::vector<int> a_b, b_a;
l->tally_diff(tally, a_b, b_a);
dv->set_results(a_b, b_a);
dv->show();
}
});
connect(qa, &QAction::triggered, [] {
QApplication::exit();
});
auto wm = this->menuBar()->addMenu("&Windows");
auto swa = wm->addAction("Map &listings");
auto gwa = wm->addAction("Map &art listings");
connect(swa, &QAction::triggered, [this]{ sv->widget()->show(); sv->show(); });
connect(gwa, &QAction::triggered, [this]{ gv->widget()->show(); gv->show(); });
auto hm = this->menuBar()->addMenu("&Help");
auto aba = hm->addAction("&About");
auto aqa = hm->addAction("About &Qt");
connect(aba, &QAction::triggered, [this]{
QMessageBox::about(this, "About Mapman", QString(R"(
Mapman
A minecraft map art manager.
%1
Chris Xiong 2023
License: MIT (expat)
)")
.arg(MAPMAN_VERSION).trimmed());
});
connect(aqa, &QAction::triggered, []{ QApplication::aboutQt(); });
}
mapman_main_window::~mapman_main_window()
{
if (l)
delete l;
delete dv;
}