Пример #1
0
void MainWindow::OpenFile(QString FilePath, bool addToRecentList)
{
    if(m_isFileReloading) return;
    m_isFileReloading = true;
    BoolReseter rst(&m_isFileReloading); Q_UNUSED(rst);

    if(!m_isAppInited) return;
    qApp->setActiveWindow(this);

    //Check if data configs are valid
    if( configs.check() )
    {
        LogCritical(QString("Error! *.INI configs not loaded"));
        QMessageBox::warning(this,
                             tr("Configuration is loaded with errors"),
                             tr("Cannot open file:\n"
                                "Configuration package loaded with errors.")
                                        .arg(ConfStatus::configPath));
        //Show configuration status window
        on_actionCurConfig_triggered();
        return;
    }

    QMdiSubWindow *newSubWin = NULL;
    QMdiSubWindow *existing = findOpenedFileWin(FilePath);
            if (existing) {
                ui->centralWidget->setActiveSubWindow(existing);
                return;
            }

    QFile file(FilePath);
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(this, tr("File open error"),
        tr("Can't open the file: %1").arg(file.errorString()), QMessageBox::Ok);
        return;
    }

    QFileInfo in_1(FilePath);

    GlobalSettings::openPath = in_1.absoluteDir().absolutePath();

    if((in_1.suffix().toLower() == "lvl")||(in_1.suffix().toLower() == "lvlx"))
    {
        LevelData FileData;

        LogDebug("> parsing level file format");
        if( !FileFormats::OpenLevelFile(FilePath, FileData) )
        {
            formatErrorMsgBox(FilePath, FileData.meta.ERROR_info, FileData.meta.ERROR_linenum, FileData.meta.ERROR_linedata);
            return;
        }
        LogDebug("File was read!");
        FileData.meta.filename   = util::getBaseFilename(in_1.fileName());
        FileData.meta.path       = in_1.absoluteDir().absolutePath();
        FileData.playmusic  = GlobalSettings::autoPlayMusic;
        file.close();

        LogDebug("Creating of sub-window");
        LevelEdit *child = createLvlChild(&newSubWin);
        if ( child->loadFile(FilePath, FileData, configs, GlobalSettings::LvlOpts) )
        {
            child->show();
            updateMenus(newSubWin, true);
            child->updateGeometry();
            child->ResetPosition();
            dock_LvlItemBox->setLvlItemBoxes(false, false);
            statusBar()->showMessage(tr("Level file loaded"), 2000);
            SetCurrentLevelSection(0);
            dock_LvlWarpProps->init();
            dock_LvlLayers->setLayersBox();

            if(GlobalSettings::autoPlayMusic) ui->actionPlayMusic->setChecked(true);
            on_actionPlayMusic_triggered(ui->actionPlayMusic->isChecked());

        } else {
            LogDebug(">>File loading aborted");
            //child->show();
            child->LvlData.meta.modified = false;
            newSubWin->close();
            LogDebug(">>Windows closed");
        }
    }
    else
    if((in_1.suffix().toLower() == "wld")||(in_1.suffix().toLower() == "wldx"))
    {
        WorldData FileData;
        if( !FileFormats::OpenWorldFile( FilePath, FileData ) )
        {
            formatErrorMsgBox(FilePath, FileData.meta.ERROR_info, FileData.meta.ERROR_linenum, FileData.meta.ERROR_linedata);
            return;
        }
        file.close();

        WorldEdit *child = createWldChild(&newSubWin);
        if ( child->loadFile(FilePath, FileData, configs, GlobalSettings::LvlOpts) ) {
            child->show();
            updateMenus(newSubWin, true);
            child->updateGeometry();
            child->ResetPosition();
            dock_WldItemBox->setWldItemBoxes(false, false);
            dock_WldSettingsBox->setCurrentWorldSettings();
            if(FileData.HubStyledWorld)
            {
                dock_WldSettingsBox->setVisible(true);
                dock_WldSettingsBox->raise();
            }
            statusBar()->showMessage(tr("World map file loaded"), 2000);
        } else {
            LogDebug(">>File loading aborted");
            child->close();
            newSubWin->close();
            LogDebug(">>Windows closed");
        }
    }
    else
    if(in_1.suffix().toLower() == "txt")
    {
        NPCConfigFile FileData;
        if( !FileFormats::ReadNpcTXTFileF(FilePath, FileData) )
        {
            QMessageBox::critical(this, QObject::tr("File open error"), tr("Can't read the file"), QMessageBox::Ok);
            return;
        }

        if( !FileData.unknownLines.isEmpty() )
        {
            QMessageBox::warning(this,
                                 QObject::tr("Unknown values are presented"),
                                 QObject::tr("Your file have an unknown values which will be removed\n"
                                             " when you will save file") +
                                 QString("\n====================================\n"
                                         "%1").arg(FileData.unknownLines),
                                 QMessageBox::Ok);
        }

        NpcEdit *child = createNPCChild(&newSubWin);
        if (child->loadFile(FilePath, FileData)) {
            statusBar()->showMessage(tr("NPC Config loaded"), 2000);
            child->show();
            updateMenus(newSubWin, true);
        } else {
            child->close();
            newSubWin->close();
        }
    }
    else
    if(in_1.suffix().toLower() == "sav")
    {
        GamesaveData FileData;

        QString statistics;
        if(!FileFormats::ReadSMBX64SavFileF( FilePath, FileData))
        {
            formatErrorMsgBox( FilePath, FileData.meta.ERROR_info, FileData.meta.ERROR_linenum, FileData.meta.ERROR_linedata );
            return;
        }

        statistics+= QString("SMBX Game Save file version %1\n\n").arg(FileData.meta.RecentFormatVersion);
        if(FileData.gameCompleted)
            statistics+= "      ====This game was completed====\n\n";
        statistics+= QString("Lives: %1,   Coins:%2,   ").arg(FileData.lives).arg(FileData.coins);
        statistics+= QString("Total stars: %1,   Gotten stars: %2\n")
                .arg(FileData.totalStars)
                .arg(FileData.gottenStars.size());
        statistics+= QString("Position X=%1, Y=%2\n")
                .arg(FileData.worldPosX)
                .arg(FileData.worldPosY);

        if(FileData.musicID>0)
        {
            long item=configs.getMusWldI(FileData.musicID);
            if(item>=0)
            {
                statistics+= QString("Current music: %1\n\n").arg(configs.main_music_wld[item].name);
            }
        }

        statistics += "\n===========Players:============\n";
        for(int i=0; i<FileData.characterStates.size();i++)
        {
            if(i<configs.main_characters.size())
                statistics += QString("%1:\n").arg(configs.main_characters[i].name);
            else
                statistics += QString("<unknown character>:\n");
            statistics += QString("Health: %1,    ").arg(FileData.characterStates[i].health);
            statistics += QString("Power-UP: %1,    ").arg(FileData.characterStates[i].state);
            switch(FileData.characterStates[i].mountType)
            {
            case 0:
                break;
            case 1:
                statistics += QString("Mounted vehicle: Shoe - ");
                switch(FileData.characterStates[i].mountID)
                {
                case 1:
                    statistics += QString("Kuribo's (green)");break;
                case 2:
                    statistics += QString("Podoboo's (red)");break;
                case 3:
                    statistics += QString("Lakitu's (blue)");break;
                default:
                    statistics += QString("of God Power :D");break;
                }
                break;
            case 3:
                statistics += QString("Mounted vehicle: Yoshi");break;
            default:
                statistics += QString("Mounted vehicle: <unknown>");break;
            }

            if( (FileData.characterStates[i].itemID>0) && configs.main_npc.contains(FileData.characterStates[i].itemID) )
            {
                statistics += QString("%2Has item: %1")
                                .arg(configs.main_npc[FileData.characterStates[i].itemID].setup.name)
                                .arg(FileData.characterStates[i].mountType > 0 ? ",    ":"");
            }

            if(i<FileData.characterStates.size()-1)
                statistics += "\n----------------------------\n";
        }
        statistics += "\n=========================\n";

        if( !FileData.meta.ReadFileValid )
            return;

        QMessageBox::information(this,
                                 tr("Game save statistics"),
                                 statistics,
                                 QMessageBox::Ok);
    }
    else
    {
        QMessageBox::warning(this,
                             tr("Bad file"),
                             tr("This file have unknown extension"),
                             QMessageBox::Ok);
        return;
    }

    // Add to recent fileList
    if(addToRecentList){
        AddToRecentFiles(FilePath);
        SyncRecentFiles();
    }
}
Пример #2
0
//Reload opened file data
void MainWindow::on_actionReload_triggered()
{
    if(m_isFileReloading) return;

    m_isFileReloading = true;
    BoolReseter rst(&m_isFileReloading);
    Q_UNUSED(rst);
    QString filePath;
    QRect wnGeom;
    int activeWindow = activeChildWindow(LastActiveSubWindow);

    if(activeWindow == WND_Level)
    {
        LevelEdit *lvlEdit = activeLvlEditWin(LastActiveSubWindow);
        LevelData FileData;
        filePath = lvlEdit->curFile;

        if(lvlEdit->isUntitled)
        {
            QMessageBox::warning(this, tr("File not saved"),
                                 tr("File doesn't saved on disk."), QMessageBox::Ok);
            return;
        }

        if(!QFileInfo(filePath).exists())
        {
            QMessageBox::critical(this, tr("File open error"),
                                  tr("Can't open the file!\nFile not exist."), QMessageBox::Ok);
            return;
        }

        if(lvlEdit->LvlData.meta.modified)
        {
            QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Reload file and custom stuff"),
                                              tr("Do you want to save before reload stuff?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

            if((ret == QMessageBox::Cancel) || (ret == 0))
                return;
            else if(ret == QMessageBox::Yes)
                save();
        }

        //Open level file
        if(!FileFormats::OpenLevelFile(filePath, FileData))
        {
            formatErrorMsgBox(filePath, FileData.meta.ERROR_info, FileData.meta.ERROR_linenum, FileData.meta.ERROR_linedata);
            statusBar()->showMessage(tr("Reloading error"), 2000);
            return;
        }

        FileData.playmusic = GlobalSettings::autoPlayMusic;
        lvlEdit->LvlData.meta.modified = false;
        //Remember last section ID and positions!
        int lastSection = 0;
        QHash<int, QPair<long, long> > sectionPoss;
        lastSection = lvlEdit->LvlData.CurSection;
        SetCurrentLevelSection(lastSection); //Need to remember position of current section

        for(int i = 0; i < lvlEdit->LvlData.sections.size(); i++)
        {
            LevelSection sct = lvlEdit->LvlData.sections[i];
            sectionPoss[sct.id] = QPair<long, long >(sct.PositionX, sct.PositionY);
        }

        long posX = lvlEdit->scene->m_viewPort->horizontalScrollBar()->value();
        long posY = lvlEdit->scene->m_viewPort->verticalScrollBar()->value();
        lvlEdit->close();//Close old widget without closing of sub-window
        //Get geometry of current subwindow
        wnGeom = LastActiveSubWindow->geometry();
        //delete window->widget();
        LevelEdit *chLvlWin = new LevelEdit(this, LastActiveSubWindow);
        connect(chLvlWin, SIGNAL(forceReload()), this, SLOT(on_actionReload_triggered()));
        LastActiveSubWindow->setWidget(chLvlWin);
        GraphicsWorkspace *gr = static_cast<GraphicsWorkspace *>(chLvlWin->getGraphicsView());
        connect(gr, SIGNAL(zoomValueChanged(QString)), zoom, SLOT(setText(QString)));
        //LastActiveSubWindow->close();
        LevelEdit *child = chLvlWin;//createLvlChild();
        LvlMusPlay::setNoMusic();
        setMusic();

        if(static_cast<bool>(child->loadFile(filePath, FileData, configs, GlobalSettings::LvlOpts)))
        {
            child->show();
            LastActiveSubWindow->setGeometry(wnGeom);
            child->updateGeometry();
            child->ResetPosition();
            statusBar()->showMessage(tr("Level file reloaded"), 2000);
            LvlMusPlay::musicForceReset = true; //reset musics
            updateMenus(LastActiveSubWindow, true);
            child->setFocus();

            //Restore saved section positions
            for(int i = 0; i < child->LvlData.sections.size(); i++)
            {
                if(sectionPoss.contains(child->LvlData.sections[i].id))
                {
                    QPair<long, long> &sct = sectionPoss[child->LvlData.sections[i].id];
                    child->LvlData.sections[i].PositionX = sct.first;
                    child->LvlData.sections[i].PositionY = sct.second;
                }
            }

            SetCurrentLevelSection(lastSection);
            child->scene->m_viewPort->horizontalScrollBar()->setValue(static_cast<int>(posX));
            child->scene->m_viewPort->verticalScrollBar()->setValue(static_cast<int>(posY));

            if(GlobalSettings::autoPlayMusic)
                ui->actionPlayMusic->setChecked(true);

            child->showCustomStuffWarnings();
        }
        else
        {
            LogDebug(">>File loading aborted");
            LogDebug(">>Window showed");
            child->LvlData.meta.modified = false;
            LogDebug(">>Option set");
            LastActiveSubWindow->close();
            LogDebug(">>Windows closed");
        }
    }
    else if(activeWindow == WND_NpcTxt)
    {
        NpcEdit *npcEdit = activeNpcEditWin(LastActiveSubWindow);
        filePath = npcEdit->curFile;

        if(npcEdit->isUntitled)
        {
            QMessageBox::warning(this, tr("File not saved"),
                                 tr("File doesn't saved on disk."), QMessageBox::Ok);
            return;
        }

        if(npcEdit->isModyfied)
        {
            QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Reload file and custom stuff"),
                                              tr("Do you want to save before reload stuff?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

            if((ret == QMessageBox::Cancel) || (ret == 0))
                return;
            else if(ret == QMessageBox::Yes)
                save();
        }

        //        if (!fileIn.open(QIODevice::ReadOnly)) {
        //        QMessageBox::critical(this, tr("File open error"),
        //        tr("Can't open the file!"), QMessageBox::Ok);
        //            return;
        //        }
        NPCConfigFile FileData;

        if(!FileFormats::ReadNpcTXTFileF(filePath, FileData))
        {
            QMessageBox::critical(this, QObject::tr("File open error"), tr("Can't read the file"), QMessageBox::Ok);
            return;
        }

        wnGeom = LastActiveSubWindow->geometry();
        npcEdit->isModyfied = false;
        npcEdit->close();
        //NpcEdit *child = createNPCChild();
        //QMdiSubWindow *npcWindow = LastActiveSubWindow;
        NpcEdit *child = new NpcEdit(&configs, LastActiveSubWindow);
        LastActiveSubWindow->setWidget(child);

        if(child->loadFile(filePath, FileData))
        {
            statusBar()->showMessage(tr("NPC Config reloaded"), 2000);
            child->show();
            LastActiveSubWindow->setGeometry(wnGeom);
            updateMenus(LastActiveSubWindow, true);
        }
        else
        {
            child->close();
            LastActiveSubWindow->close();
        }
    }
    else if(activeWindow == WND_World)
    {
        WorldEdit *wldEdit = activeWldEditWin(LastActiveSubWindow);
        WorldData FileData;
        filePath = wldEdit->curFile;

        if(wldEdit->isUntitled)
        {
            QMessageBox::warning(this, tr("File not saved"),
                                 tr("File doesn't saved on disk."), QMessageBox::Ok);
            return;
        }

        if(!QFileInfo(filePath).exists())
        {
            QMessageBox::critical(this, tr("File open error"),
                                  tr("Can't open the file!\nFile not exist."), QMessageBox::Ok);
            return;
        }

        if(wldEdit->WldData.meta.modified)
        {
            QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Reload file and custom stuff"),
                                              tr("Do you want to save before reload stuff?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

            if((ret == QMessageBox::Cancel) || (ret == 0))
                return;
            else if(ret == QMessageBox::Yes)
                save();
        }

        if(!FileFormats::OpenWorldFile(filePath, FileData))
        {
            formatErrorMsgBox(filePath, FileData.meta.ERROR_info, FileData.meta.ERROR_linenum, FileData.meta.ERROR_linedata);
            statusBar()->showMessage(tr("Reloading error"), 2000);
            return;
        }

        QFileInfo finfo(filePath);
        FileData.meta.filename = util::getBaseFilename(finfo.fileName());
        FileData.meta.path = finfo.absoluteDir().absolutePath();
        FileData.playmusic = GlobalSettings::autoPlayMusic;
        wldEdit->WldData.meta.modified = false;
        wnGeom = LastActiveSubWindow->geometry();
        QMdiSubWindow *worldWindow = LastActiveSubWindow;
        long posX = wldEdit->scene->m_viewPort->horizontalScrollBar()->value();
        long posY = wldEdit->scene->m_viewPort->verticalScrollBar()->value();
        wldEdit->close();
        WorldEdit *child = new WorldEdit(this, worldWindow);
        connect(child, &WorldEdit::forceReload, this, &MainWindow::on_actionReload_triggered);
        worldWindow->setWidget(child);
        GraphicsWorkspace *gr = static_cast<GraphicsWorkspace *>(child->getGraphicsView());
        connect(gr, SIGNAL(zoomValueChanged(QString)), zoom, SLOT(setText(QString)));

        if(bool(child->loadFile(filePath, FileData, configs, GlobalSettings::LvlOpts)))
        {
            child->show();
            LastActiveSubWindow->setGeometry(wnGeom);
            child->updateGeometry();
            child->ResetPosition();
            updateMenus(LastActiveSubWindow, true);
            dock_WldSettingsBox->setCurrentWorldSettings();

            if(FileData.HubStyledWorld)
            {
                dock_WldSettingsBox->setVisible(true);
                dock_WldSettingsBox->raise();
            }

            child->scene->m_viewPort->horizontalScrollBar()->setValue(static_cast<int>(posX));
            child->scene->m_viewPort->verticalScrollBar()->setValue(static_cast<int>(posY));
            statusBar()->showMessage(tr("World map file loaded"), 2000);
            child->showCustomStuffWarnings();
        }
        else
        {
            LogDebug(">>File loading aborted");
            //child->show();
            LogDebug(">>Window showed");
            child->WldData.meta.modified = false;
            LogDebug(">>Option set");
            LastActiveSubWindow->close();
            LogDebug(">>Windows closed");
        }
    }

    dock_LvlItemBox->clearFilter();
}