/*!
  Sets internal pointers to frequently used QML items, thus reducing the overhead
  of repeated searching for them at runtime.
  */
void CcfQmlBaseScenario::init()
{
    if (mScenarioFile != "") {
        QStringList unitSideList;
        QObject *unitsLoader = child("unitsLoader");
        QObject *unitItem = unitsLoader->property("unitsItem").value<QObject *>();
        QObject *map = child("map");

        if (unitItem->objectName() != "Campaign") {
            // This is a single scenario
            map->set("source", unitItem->getString("mapFile"));
            QObjectList tempList = unitItem->children();

            for (int i = 0; i < tempList.count(); ++i) {
                CcfQmlBaseUnit *unit = ccfUnit(tempList.at(i));
                if ((unit != 0) && (unit->property("unitIndex").isValid())) {
                    unit->setUnitIndex(i);
                    connect(this, &CcfQmlBaseScenario::togglePause,
                            unit, &CcfQmlBaseUnit::togglePause);
                    connect(unit, &CcfQmlBaseUnit::actionFinished,
                            this, &CcfQmlBaseScenario::actionFinished);
                    connect(unit, &CcfQmlBaseUnit::movementStateChange,
                            this, &CcfQmlBaseScenario::handleUnitMovement);
                    unitSideList.append(unit->getUnitSide());
                    mUnits.append(unit);
                }
            }

            mMapItem = map->property("mapItem").value<CcfQmlBaseMap *>();

            if(mMapItem != 0)
                mMapItem->setUnits(mUnits);
            else
                mlogger->error("MapItem object was not properly initialised",
                               "Robin Hood is a jerk");
        } else {
            // This is a campaign
            // TODO: add some clever code here ;)
        }

        mScenarioState->setAvailableSides(unitSideList);

        mAimLine = item("aimLine");
        mGameArea = item("gameArea");
        mZoomArea = item("zoomArea");
        mContextMenu = item("contextMenu");
        mEffectsTimer = child("effectsTimer");
        mRotationTimer = child("rotationTimer");
        mFollowingTimer = child("followingTimer");
        mMouseAreaMain = child("mouseAreaMain");

        mRoster = findChild<CcfQmlBaseRosterMenu *>("roster");
        mRoster->populateUnits(mUnits);

        connect(mContextMenu, SIGNAL(menuEntryClicked(QString)), this, SLOT(scheduleContextAction(QString)));
        connect(mRotationTimer, SIGNAL(triggered()), this, SLOT(updateAimLine()));
        connect(mEffectsTimer, SIGNAL(triggered()), this, SLOT(updateEffects()));
        connect(mFollowingTimer, SIGNAL(triggered()), this, SLOT(updateFollowingUnit()));
        connect(child("sceneUpdateTimer"), SIGNAL(triggered()), this, SLOT(updateUnitVisibility()));
        connect(child("rubberBandTimer"), SIGNAL(triggered()), this, SLOT(updateRubberBand()));

        hideNonPlayerUnits();
        setSideMarks();
    }
}
/*!
  Schedules a game save.
  */
void CcfQmlBaseScenario::saveGame()
{
    QObject *unitsLoader = child("unitsLoader");
    QObject *unitItem = unitsLoader->property("unitsItem").value<QObject *>();
    mmain->gameManager()->saveGame(mUnits, unitItem->getString("mapFile"));
}