4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Thomas McGuire <thomas.mcguire@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "timerinfo.h"
29 using namespace GammaRay
;
31 static const int maxTimeoutEvents
= 1000;
32 static const int maxTimeSpan
= 10000;
34 TimerInfo::TimerInfo(QTimer
*timer
)
37 m_timerId(timer
->timerId()),
43 TimerInfo::TimerInfo(int timerId
)
44 : m_type(QObjectType
),
50 void TimerInfo::addEvent(const TimeoutEvent
&timeoutEvent
)
52 m_timeoutEvents
.append(timeoutEvent
);
57 int TimerInfo::numEvents() const
59 return m_timeoutEvents
.size();
62 QTimer
*TimerInfo::timer() const
67 int TimerInfo::timerId() const
72 FunctionCallTimer
*TimerInfo::functionCallTimer()
74 return &m_functionCallTimer
;
77 QString
TimerInfo::wakeupsPerSec() const
81 int end
= m_timeoutEvents
.size() - 1;
82 for (int i
= end
; i
>= 0; i
--) {
83 const TimeoutEvent
&event
= m_timeoutEvents
.at(i
);
84 if (event
.timeStamp
.msecsTo(QTime::currentTime()) > maxTimeSpan
) {
91 if (totalWakeups
> 0 && end
> start
) {
92 const QTime startTime
= m_timeoutEvents
[start
].timeStamp
;
93 const QTime endTime
= m_timeoutEvents
[end
].timeStamp
;
94 const int timeSpan
= startTime
.msecsTo(endTime
);
95 const float wakeupsPerSec
= totalWakeups
/ (float)timeSpan
* 1000.0f
;
96 return QString::number(wakeupsPerSec
, 'f', 1);
101 QString
TimerInfo::timePerWakeup() const
103 if (m_type
== QObjectType
) {
107 int totalWakeups
= 0;
109 for (int i
= m_timeoutEvents
.size() - 1; i
>= 0; i
--) {
110 const TimeoutEvent
&event
= m_timeoutEvents
.at(i
);
111 if (event
.timeStamp
.msecsTo(QTime::currentTime()) > maxTimeSpan
) {
115 totalTime
+= event
.executionTime
;
118 if (totalWakeups
> 0) {
119 return QString::number(totalTime
/ (float)totalWakeups
, 'f', 1);
124 QString
TimerInfo::maxWakeupTime() const
126 if (m_type
== QObjectType
) {
131 for (int i
= 0; i
< m_timeoutEvents
.size(); i
++) {
132 const TimeoutEvent
&event
= m_timeoutEvents
.at(i
);
133 if (event
.executionTime
> max
) {
134 max
= event
.executionTime
;
137 return QString::number(max
);
140 int TimerInfo::totalWakeups() const
142 return m_totalWakeups
;
145 QString
TimerInfo::state() const
148 return QObject::tr("None");
151 if (!m_timer
->isActive()) {
152 return QObject::tr("Inactive");
154 if (m_timer
->isSingleShot()) {
155 return QObject::tr("Singleshot (%1 ms)").arg(m_timer
->interval());
157 return QObject::tr("Repeating (%1 ms)").arg(m_timer
->interval());
162 void TimerInfo::removeOldEvents()
164 if (m_timeoutEvents
.size() > maxTimeoutEvents
) {
165 m_timeoutEvents
.removeFirst();
169 void TimerInfo::setLastReceiver(QObject
*receiver
)
171 m_lastReceiver
= receiver
;
174 QString
TimerInfo::displayName() const
177 return Util::displayString(timer());
179 if (m_lastReceiver
) {
180 return Util::displayString(m_lastReceiver
);
182 return QObject::tr("Unknown QObject");