/* mainwindow.cc This file is part of the Project::OSiRiON world editor and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "mainwindow.h" #include "editorwindow.h" namespace editor { /** * @brief MainWindow is the application's main window. * The main window contains a QMdiArea that manages ZoneEditor children * */ MainWindow::MainWindow() { // set window title setWindowTitle(tr("Project::OSiRiON world editor")); // initialize MDI (multiple document interface) area mainwindow_mdiarea = new QMdiArea(); mainwindow_mdiarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); mainwindow_mdiarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setCentralWidget(mainwindow_mdiarea); // initialize actions init_actions(); // initialize menu bar init_menu(); } void MainWindow::init_actions() { // File -> New action_new = new QAction( tr("&New..."), this); action_new->setShortcuts(QKeySequence::New); action_new->setStatusTip(tr("Create a new zone")); connect(action_new, SIGNAL(triggered()), this, SLOT(slot_new())); // File -> Open action_open = new QAction( tr("&Open..."), this); action_open->setShortcuts(QKeySequence::Open); action_open->setStatusTip(tr("Open an existing zone ini file")); connect(action_open, SIGNAL(triggered()), this, SLOT(slot_open())); action_save = new QAction( tr("&Save"), this); action_save->setShortcuts(QKeySequence::Save); action_save->setStatusTip(tr("Save zone")); connect(action_save, SIGNAL(triggered()), this, SLOT(slot_save())); action_save_as = new QAction( tr("Save &As..."), this); action_save_as->setShortcuts(QKeySequence::SaveAs); action_save_as->setStatusTip(tr("Save zone as ini file")); connect(action_save_as, SIGNAL(triggered()), this, SLOT(slot_save_as())); // File -> Quit action_quit = new QAction(tr("&Quit"), this); action_quit->setShortcuts(QKeySequence::Quit); action_quit->setStatusTip(tr("Exit the application")); connect(action_quit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); } void MainWindow::init_menu() { mainwindow_filemenu = menuBar()->addMenu(tr("&File")); mainwindow_filemenu->addAction(action_new); mainwindow_filemenu->addAction(action_open); mainwindow_filemenu->addAction(action_save); mainwindow_filemenu->addAction(action_save_as); mainwindow_filemenu->addSeparator(); mainwindow_filemenu->addAction(action_quit); mainwindow_editmenu = menuBar()->addMenu(tr("&Edit")); } EditorWindow *MainWindow::active_child() { if (QMdiSubWindow *active_subwindow = mainwindow_mdiarea->activeSubWindow()) return qobject_cast(active_subwindow->widget()); return 0; } QMdiSubWindow *MainWindow::find_child(const QString &filename) { QString canonical = QFileInfo(filename).canonicalFilePath(); foreach (QMdiSubWindow *subwindow, mainwindow_mdiarea->subWindowList()) { EditorWindow *editorwindow = qobject_cast(subwindow->widget()); if (canonical == QFileInfo(editorwindow->filename()).canonicalFilePath()) return subwindow; } return 0; } EditorWindow *MainWindow::add_child() { // create a child widget EditorWindow *child_widget = new EditorWindow(); // add the widget to the MDI area, // this will wrap an QMdiSubWindow around it QMdiSubWindow *subwindow = mainwindow_mdiarea->addSubWindow(child_widget); child_widget->show(); // FIXME check if maximized subwindow->resize(768, 512); mainwindow_mdiarea->setActiveSubWindow(subwindow); return child_widget; } void MainWindow::slot_new() { add_child(); } void MainWindow::slot_open() { QString filename = QFileDialog::getOpenFileName(this, tr("Open...")); if (!filename.isEmpty()) { QMdiSubWindow *subwindow = find_child(filename); if (subwindow) { mainwindow_mdiarea->setActiveSubWindow(subwindow); } else { EditorWindow *editorwindow = add_child(); editorwindow->loadFile(filename); subwindow = find_child(filename); if (subwindow) { subwindow->setWindowTitle(editorwindow->filename()); } } } } void MainWindow::slot_save_as() { EditorWindow *editorwindow = active_child(); if (editorwindow) { QString filename = QFileDialog::getSaveFileName(this, tr("Save as...")); if (!filename.isEmpty()) { editorwindow->saveFile(filename); QMdiSubWindow *subwindow = find_child(filename); if (subwindow) { subwindow->setWindowTitle(editorwindow->filename()); } } } } void MainWindow::slot_save() { /* if (active_child()) { if (!active_child()->filename().size()) { slot_save_as(); return; } else { active_child()->saveFile(active_child()->filename()); } } */ } }