//virtual
void PropertySettingSignalTransition::onTransition(QEvent * e)
{
	if (m_qp_targetObject)
	{
		QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
		if (!se->arguments().isEmpty())
		{
            m_qp_targetObject->setProperty(m_propertyName.toLatin1().constData(),se->arguments().at(0));
        }
	}
	QSignalTransition::onTransition(e);
}
void ReorderState::onEntry(QEvent* event)
{
	CardWindowManagerState::onEntry(event);

	QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>(event);
	if (se && !se->arguments().isEmpty()) {
		QPoint pt = se->arguments().at(0).toPoint();		
		m_wm->enterReorder(pt);
		//int slice = se->arguments().at(1).toInt();
		//m_grid = new ReorderGrid(m_wm, slice);
	}

	SystemUiController::instance()->enterOrExitCardReorder(true);
}
void ReorderState::onExit(QEvent* event)
{
	CardWindowManagerState::onExit(event);

	QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>(event);
	if (se && !se->arguments().isEmpty()) {
		bool canceled = se->arguments().at(0).toBool();
		if (canceled)
			m_wm->cancelReorder();
	}

	//delete m_grid;
	SystemUiController::instance()->enterOrExitCardReorder(false);
}
bool ReceivedMessageTransition::eventTest(QEvent* event) {
    if (!QSignalTransition::eventTest(event)) {
        return false;
    }

    QStateMachine::SignalEvent* signalEvent = static_cast<QStateMachine::SignalEvent*>(event);
    const QVariant& qVariant(signalEvent->arguments().at(0));
    if (!qVariant.canConvert<AddressedMessage>()) {
        LOG(ERROR) << "ReceivedMessageTransition::eventTest(): "
                   << "cannot convert to AddressedMessage...";
        return false;
    }

    const AddressedMessage& message = qVariant.value<AddressedMessage>();

    if (message.getMessage().getMessageNumber() == expectedMessageNumber) {
        return true;
    } else {
        LOG(ERROR) << "ReceivedMessageTransition::eventTest(): "
                   << "recevied wrong message number: " << message.getMessage().getMessageNumber()
                   << ", expected: " << expectedMessageNumber;
        return false;
    }

}
bool SignalTransition::eventTest(QEvent *event)
{
    Q_ASSERT(event);
    if (!QSignalTransition::eventTest(event))
        return false;

    if (m_guard.isEmpty())
        return true;

    QQmlContext *outerContext = QQmlEngine::contextForObject(this);
    QQmlContext context(outerContext);
    QQmlContextData::get(outerContext)->imports->addref();
    QQmlContextData::get(&context)->imports = QQmlContextData::get(outerContext)->imports;

    QStateMachine::SignalEvent *e = static_cast<QStateMachine::SignalEvent*>(event);

    // Set arguments as context properties
    int count = e->arguments().count();
    QMetaMethod metaMethod = e->sender()->metaObject()->method(e->signalIndex());
    for (int i = 0; i < count; i++)
        context.setContextProperty(metaMethod.parameterNames()[i], QVariant::fromValue(e->arguments().at(i)));

    QQmlExpression expr(m_guard, &context, this);
    QVariant result = expr.evaluate();

    return result.toBool();
}
void SignalTransition::onTransition(QEvent *event)
{
    if (m_signalExpression) {
        QStateMachine::SignalEvent *e = static_cast<QStateMachine::SignalEvent*>(event);
        m_signalExpression->evaluate(e->arguments());
    }
    QSignalTransition::onTransition(event);
}
bool MaximizeToFocusTransition::eventTest(QEvent* event)
{
	if (!QSignalTransition::eventTest(event))
		return false;

	QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>(event);
	CardWindow* win = se->arguments().at(0).value<CardWindow*>();
	CardWindowManager* wm = static_cast<CardWindowManager*>(senderObject());
	if (win == wm->activeWindow()) {
		// window is already focused
		SystemUiController::instance()->setCardWindowMaximized(true);
		return false;
	}

	return true;
}
// --------------------------------------------------------------------------------------------------
void PreparingState::onEntry(QEvent* event)
{
	CardWindowManagerState::onEntry(event);

	// unpack the window which we need to prepare
	CardWindow* win = 0;
	if (event->type() == QEvent::StateMachineSignal) {
		QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>(event);
		win = se->arguments().at(0).value<CardWindow*>();

		if (win) {
			QRectF boundingRect = m_wm->normalOrScreenBounds(0);
			if(WindowType::Type_ModalChildWindowCard != win->type())
				win->setBoundingRect(boundingRect.width(), boundingRect.height());
			else {
				win->setBoundingRect(Settings::LunaSettings()->modalWindowWidth, Settings::LunaSettings()->modalWindowHeight);
			}
		}
	}

	m_wm->prepareAddWindowSibling(win);
}
void FocusState::onEntry(QEvent* event)
{
	CardWindowManagerState::onEntry(event);

	CardWindow* win = 0;
	if (event->type() == QEvent::StateMachineSignal) {
		QStateMachine::SignalEvent* se = static_cast<QStateMachine::SignalEvent*>(event);
		win = se->arguments().at(0).value<CardWindow*>();
	}

	CardGroup* activeGroup = m_wm->activeGroup();
	Q_ASSERT(activeGroup != 0);
	if (activeGroup == win->cardGroup()) {
		// update the active card within the active group and maximize it
		activeGroup->setActiveCard(win);
		m_wm->maximizeActiveWindow();
	}
	else {
		// make this card the active card in its group and focus the group
		win->cardGroup()->setActiveCard(win);
		m_wm->setActiveGroup(win->cardGroup());
		m_wm->slideAllGroups();
	}
}