📄 kwqtimer.cpp
字号:
/*
* Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
* Portions Copyright (c) 2005 Nokia Corporation, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "KWQTimer.h"
#include "KWQAssertions.h"
#include "KWQPtrList.h"
class KWQTimerPrivate;
static QPtrList<KWQTimerPrivate> deferredQTimers;
class KWQTimerPrivate : public CTimer
{
public:
static KWQTimerPrivate* NewL( QTimer* aOwner );
static KWQTimerPrivate* NewL( QObject*, const char* );
void ConstructL( QTimer* aOwner );
void ConstructL( QObject*, const char* );
void Start( TInt aDelay, TBool aSingleShot );
void RunL();
virtual ~KWQTimerPrivate();
private:
friend class QTimer;
void StartTimer( TInt );
KWQTimerPrivate();
QTimer* iOwner;
KWQSlot* iSlot;
TInt iRemainingMilli;
TInt iDelayMilli;
TBool iSingleShot;
};
KWQTimerPrivate::KWQTimerPrivate() :
CTimer( CActive::EPriorityStandard )
{
}
KWQTimerPrivate* KWQTimerPrivate::NewL( QTimer* aOwner )
{
KWQTimerPrivate* self = new KWQTimerPrivate;
CleanupStack::PushL( self );
self->ConstructL( aOwner );
CleanupStack::Pop();
return self;
}
KWQTimerPrivate* KWQTimerPrivate::NewL( QObject* aReceiver, const char* aMember )
{
KWQTimerPrivate* self = new KWQTimerPrivate;
CleanupStack::PushL( self );
self->ConstructL( aReceiver, aMember );
CleanupStack::Pop();
return self;
}
void KWQTimerPrivate::ConstructL( QTimer* aOwner )
{
CTimer::ConstructL();
iOwner = aOwner;
CActiveScheduler::Add( this );
}
void KWQTimerPrivate::ConstructL( QObject* aReceiver, const char* aMember )
{
CTimer::ConstructL();
iSlot = new KWQSlot( aReceiver, aMember );
CActiveScheduler::Add( this );
}
KWQTimerPrivate::~KWQTimerPrivate()
{
Cancel();
iOwner = 0;
delete iSlot;
deferredQTimers.removeRef(this);
}
void KWQTimerPrivate::RunL()
{
if (iRemainingMilli)
{
// repeat until no more remains
StartTimer(iRemainingMilli);
}
else if (QObject::defersTimers())
{
if (!deferredQTimers.containsRef(this))
deferredQTimers.append(this);
}
else if( iOwner )
{
iOwner->fire();
if (!iSingleShot)
StartTimer(iDelayMilli);
}
else if( iSlot )
{
iSlot->call();
delete this;
}
}
void KWQTimerPrivate::Start( TInt aDelay, TBool aSingleShot )
{
Cancel();
iDelayMilli = aDelay;
iSingleShot = aSingleShot;
StartTimer(iDelayMilli);
}
void KWQTimerPrivate::StartTimer( TInt aDelay )
{
// avoid overflow
TInt t;
if (aDelay<(TInt)(KMaxTInt32/1000))
{
t = aDelay * 1000;
iRemainingMilli = 0;
}
else
{
t = KMaxTInt32;
iRemainingMilli = aDelay - (TInt)(KMaxTInt32/1000);
}
if( !IsActive() )
CTimer::After( TTimeIntervalMicroSeconds32(t) );
}
QTimer::QTimer()
: m_timer(0), m_monitorFunction(0), m_timeoutSignal(this, SIGNAL(timeout()))
{
TRAP_IGNORE(m_timer = KWQTimerPrivate::NewL( this ));
}
QTimer::~QTimer()
{
stop();
delete m_timer;
}
bool QTimer::isActive() const
{
return m_timer?m_timer->IsActive():false;
}
void QTimer::start(int msec, bool singleShot)
{
if (!m_timer) return;
stop();
m_timer->Start( msec, singleShot );
if (m_monitorFunction) {
m_monitorFunction(m_monitorFunctionContext);
}
}
void QTimer::stop()
{
if (!m_timer) return;
m_timer->Cancel();
if (m_monitorFunction) {
m_monitorFunction(m_monitorFunctionContext);
}
}
void QTimer::setMonitor(void (*monitorFunction)(void *context), void *context)
{
ASSERT(!m_monitorFunction);
m_monitorFunction = monitorFunction;
m_monitorFunctionContext = context;
}
void QTimer::fire()
{
// Note: This call may destroy the QTimer, so be sure not to touch any fields afterward.
m_timeoutSignal.call();
}
void QTimer::singleShot(int msec, QObject *receiver, const char *member)
{
TRAP_IGNORE(
KWQTimerPrivate* timer = KWQTimerPrivate::NewL( receiver, member );
timer->Start( msec, ETrue );
);
}
void QTimer::stopDeferringTimers()
{
deferredQTimers.first();
while (deferredQTimers.current() != 0) {
// remove before sending the timer event, in case the timer
// callback cancels the timer - we don't want to remove too
// much in that case.
KWQTimerPrivate *timerTarget = deferredQTimers.take();
timerTarget->StartTimer(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -