quake-tools/source/quam/main_window.cc

90 lines
2.2 KiB
C++

#include "quam/main_window.h"
#include "quam/archive.h"
#include "quam/project.h"
#include <QErrorMessage>
#include <QFileDialog>
#include <QMdiSubWindow>
static bool applyActiveWin(QMdiArea *area, std::function<bool(Project *)> fn) {
if(auto subWin = area->activeSubWindow()) {
if(auto win = qobject_cast<Project *>(subWin->widget())) {
return fn(win);
}
}
return false;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent},
Ui::MainWindow{},
m_errors{new QErrorMessage{this}}
{
setupUi(this);
actionClose->setShortcut(QKeySequence{QKeySequence::Close});
actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
actionUp->setShortcut(Qt::ALT + Qt::Key_Up);
actionTop->setShortcut(Qt::ALT + Qt::Key_Home);
actionBack->setShortcut(QKeySequence{QKeySequence::Back});
actionForward->setShortcut(QKeySequence{QKeySequence::Forward});
}
MainWindow::~MainWindow() {
}
void MainWindow::fileOpen() {
auto fileName =
QFileDialog::getOpenFileName(
this,
tr("Open Archive"),
QString{},
tr("Quake archive file (*.pak *.wad);;"
"Quake PACK file (*.pak);;"
"Quake WAD2 file (*.wad);;"
"All files (*)"));
if(!fileName.isEmpty()) {
try {
auto path = std::filesystem::path(fileName.toStdString());
auto st = openReadBin(path);
auto win = new QMdiSubWindow;
auto proj = new Project{st, m_errors, win};
win->setWidget(proj);
win->setAttribute(Qt::WA_DeleteOnClose);
win->setWindowTitle(QString::fromStdString(path.filename()));
mdiArea->addSubWindow(win);
win->showMaximized();
} catch(std::exception const &exc) {
m_errors->showMessage(tr(exc.what()));
}
}
}
void MainWindow::fileClose() {
if(auto win = mdiArea->activeSubWindow()) {
mdiArea->removeSubWindow(win);
}
}
bool MainWindow::goBack() {
return applyActiveWin(mdiArea, [](auto win) {return win->goBack();});
}
bool MainWindow::goForward() {
return applyActiveWin(mdiArea, [](auto win) {return win->goForward();});
}
bool MainWindow::goTop() {
return applyActiveWin(mdiArea, [](auto win) {return win->goTop();});
}
bool MainWindow::goUp() {
return applyActiveWin(mdiArea, [](auto win) {return win->goUp();});
}
// EOF