void TimeBar::mousePressEvent( QMouseEvent * event )
{
	if (event->y()<=25)
	{
		//如果小于25,那么就是移动当前位置
		mMoveCurrTime=true;
		float x=event->x();
		x=x/mZoom*1000;
		setCurrTime(x);
		mToopTip=QString("CurrTime:%1").arg(x);
		mToolTipX=event->x()+10;
		mToolTipY=event->y()+5;
		repaint();
	}
	else
	{
		if (mSelectedTimeLine!=NULL)
		{
			return;
		}

		//检查是否选中某个TimeLine
		QMapIterator<int,QVector<TimeLine*>*> mapIt(mTimeLine);
		while (mapIt.hasNext()) 
		{
			mapIt.next();

			for (QVector<TimeLine*>::const_iterator it=mapIt.value()->begin();it!=mapIt.value()->end();it++)
			{
					//计算TimeLine起始位置
					float startX=mZoom*((*it)->startTime/1000.0);
					QRectF rect(startX,28+mapIt.key()*15+mapIt.key()*3,mZoom*((*it)->len()/1000.0),15);
					if (rect.contains(event->posF()))
					{
						mSelectedTimeLine=(*it);
						break;
					}
			}

			if (mSelectedTimeLine!=NULL)
			{
				setCursor(QCursor(Qt::ClosedHandCursor));
				mLastX=event->x();
				break;
			}

		}
	}
}
void FrameHandler::frame(Time frameTime)
{
    if(_mfUninitializedFrameTasks.size() != 0)
    {
        this->init();
    }

    setCurrTime(frameTime);

    if(osgAbs(_sfStartTime.getValue()) < 0.00001)
    {
        setStartTime(_sfCurrTime.getValue());

        setLastTime(0.f);
    }

    _sfCurrTime.getValue() -= _sfStartTime.getValue();

    if(_sfPaused.getValue() == false)
    {
        SFTime *pSFTimeStamp = editSFTimeStamp();

        if(_sfConstantTime.getValue() == true)
        {
            pSFTimeStamp->getValue() += _sfConstantTimeStep.getValue();

            if(pSFTimeStamp->getValue() < 0.)
                pSFTimeStamp->setValue(0.0);
        }
        else
        {
            pSFTimeStamp->getValue() +=
                (_sfCurrTime.getValue() - _sfLastTime.getValue()) *
                _sfTimeScale.getValue();

            if(pSFTimeStamp->getValue() < 0.)
                pSFTimeStamp->setValue(0.0);
        }
    }

    setLastTime(_sfCurrTime.getValue());

    ++(editSFFrameCount()->getValue());

    callTasks();
}
int checkClock(clockType cType,int coolDown)	{
    GameClock gClock = getClock(NULL);
    ClockNode currNode;
    currNode = gClock->first;
    clock_t currTime = (double) clock() / (CLOCKS_PER_SEC/100);
    while(currNode != NULL)	{
        if (currNode->type == cType)	{
            if((currTime - currNode->time) >= coolDown)	{
                setCurrTime(currNode);
                return 1;
            } else {
                return 0;
            }
        }
        currNode = currNode->next;
    }

    fprintf(stderr,"clock does not exist\n");
    return 0;
}
void TimeBar::mouseMoveEvent( QMouseEvent * event )
{
	if (mSelectedTimeLine!=NULL)
	{
		int offect=(event->x()-mLastX)/(float)mZoom*1000;
		mLastX=event->x();
		int startTime=mSelectedTimeLine->startTime;
		if (startTime+offect>=0)
		{
			mSelectedTimeLine->startTime+=offect;
			mSelectedTimeLine->endTime+=offect;
			mToopTip=QString(tr("Start:%1,End:%2")).arg(mSelectedTimeLine->startTime).arg(mSelectedTimeLine->endTime);
			mToolTipX=event->x()+5;
			mToolTipY=event->y()-5;
			repaint();
		}
	}
	else if (mMoveCurrTime)
	{
		float x=event->x();
		x=x/mZoom*1000;
		if (x<0)
		{
			x=0;
		}
		setCurrTime(x);
		mToopTip=QString(tr("CurrTime:%1")).arg(x);
		mToolTipX=event->x()+10;
		if (mToolTipX<0)
		{
			mToolTipX=0;
		}
		mToolTipY=event->y()+5;
		repaint();
	}
}
void ComplexSceneManager::frame(void)
{
#if 0
    setCurrTime(getSystemTime());
    
    if(osgAbs(_sfStartTime.getValue()) < 0.00001)
    {
        setStartTime(_sfCurrTime.getValue());
        
        setLastTime(0.f);
    }
    
    _sfCurrTime.getValue() -= _sfStartTime.getValue();

    if(_sfPaused.getValue() == false)
    {
        SFTime *pSFTimeStamp = editSFTimeStamp();

        if(_sfConstantTime.getValue() == true)
        {
            pSFTimeStamp->getValue() += _sfConstantTimeStep.getValue();

            if(pSFTimeStamp->getValue() < 0.)
                pSFTimeStamp->setValue(0.0);
        }
        else
        {
            pSFTimeStamp->getValue() += 
                (_sfCurrTime.getValue() - _sfLastTime.getValue()) * 
                _sfTimeScale.getValue();
            
            if(pSFTimeStamp->getValue() < 0.)
                pSFTimeStamp->setValue(0.0);
        }
    }
    
    setLastTime(_sfCurrTime.getValue());

    SystemTime = _sfTimeStamp.getValue();

    ++(editSFFrameCount()->getValue());

    if(_sfSensorTask.getValue() != NULL)
    {
        _sfSensorTask.getValue()->frame(_sfTimeStamp.getValue (), 
                                        _sfFrameCount.getValue());
    }
#endif

    if(_sfDumpFrameStart.getValue() == true)
    {
        fprintf(stderr, "=================================================\n");
        fprintf(stderr, "Render Frame\n");
        fprintf(stderr, "=================================================\n");
    }

    FrameHandler::the()->frame();

    SystemTime = FrameHandler::the()->getTimeStamp();

    commitChanges();

    if(_sfDrawManager.getValue() != NULL)
    {
        _sfDrawManager.getValue()->frame(FrameHandler::the()->getTimeStamp(), 
                                         FrameHandler::the()->getFrameCount());
    }
}
TimeBar::TimeBar(QWidget *parent)
	: QWidget(parent),mZoom(250),mSelectedTimeLine(NULL),mToolTipX(0),mToolTipY(0)
{
	setCurrTime(1000);
}