/*
* gdisplay.cxx
*
* Definition of graph display class for CPhone
*
* A cross platform voip gui.
*
* Copyright (c) Indranet Technologies Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Anna, and then it was copied here for CPhone
*
* The Initial Developer of the Original Code is Indranet Technologies Ltd.
*
* The Initial Author of the Original Code is Derek J Smithies. (derek@indranet.co.nz)
*
*
* Contributor(s): ______________________________________.
*
*
*
* $Log: gdisplay.cxx,v $
* Revision 1.2 2003/08/26 04:48:02 dereksmithies
* Fixes to get statistics working right, add icons, add ok,cancel,apply buttons
*
* Revision 1.1 2003/08/26 03:36:14 dereksmithies
* Initial release of statistics display
*
*
*
*
*
*/
#include <qdatetime.h>
#include <sys/types.h>
#include <unistd.h>
#include <qpainter.h>
#include <qevent.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qstring.h>
#include "gdisplay.h"
#include "cpendpoint.h"
#include "message.h"
GraphDisplay::GraphDisplay( QWidget *parent, const char *name, WFlags f)
: QFrame( parent, name, f )
{
PurgeOldData();
};
GraphDisplay::~GraphDisplay()
{
}
double GraphDisplay::GetCurrentMax()
{
accessMutex.lock();
double res = currentMax;
accessMutex.unlock();
return res;
}
QString GraphDisplay::GetRateString()
{
accessMutex.lock();
QString res;
res.sprintf("%5.1f kb/sec", currentMax);
accessMutex.unlock();
return res;
}
QString GraphDisplay::GetRateString(bool isSend)
{
accessMutex.lock();
QString res;
res.sprintf("%5.1f kb/sec", (isSend ? currentSentMax : currentRcvdMax));
accessMutex.unlock();
return res;
}
QString GraphDisplay::GetDualRateString()
{
accessMutex.lock();
PINDEX i = (writeIndex + DATA_ELEMS - 1) % DATA_ELEMS;
QString res;
res.sprintf("%5.1f / %5.1f", rate_rcvd[i], rate_sent[i]);
accessMutex.unlock();
return res;
}
void GraphDisplay::SetDisplayData(StatisticsPacketList *data)
{
accessMutex.lock();
PINDEX i;
currentMax = 10;
for (i = 0; i < data->GetSize(); i++) {
StatisticsPacket *sp = data->GetEntry(i);
if (sp != NULL) {
rate_rcvd[writeIndex] = sp->GetRate(false);
rate_sent[writeIndex] = sp->GetRate(true);
writeIndex = (writeIndex + 1) % DATA_ELEMS;
}
}
currentRcvdMax = 10;
currentSentMax = 10;
for (i = 0; i < DATA_ELEMS; i++) {
currentRcvdMax = PMAX(rate_rcvd[i], currentRcvdMax);
currentSentMax = PMAX(rate_sent[i], currentSentMax);
}
currentMax = PMAX(currentSentMax, currentRcvdMax);
accessMutex.unlock();
InitiateRepaint(true);
}
void GraphDisplay::PurgeOldData()
{
accessMutex.lock();
currentMax = 10;
currentRcvdMax = 10;
currentSentMax = 10;
refreshCount = 0;
writeIndex = 0;
for (PINDEX i = 0; i < DATA_ELEMS; i++) {
rate_rcvd[i] = 0;
rate_sent[i] = 0;
}
accessMutex.unlock();
}
void GraphDisplay::RemoveGraph()
{
InitiateRepaint();
}
void GraphDisplay::InitiateRepaint(bool doErase)
{
refreshMutex.lock();
if (refreshCount > 0) {
refreshMutex.unlock();
return;
}
refreshCount++;
refreshMutex.unlock();
QRect r = contentsRect();
QThread::postEvent(this, new QPaintEvent(r, doErase));
}
void GraphDisplay::drawContents (QPainter *p)
{
refreshMutex.lock();
refreshCount = 0;
refreshMutex.unlock();
accessMutex.lock();
QRect r = contentsRect();
QBrush b(QColor(255, 255, 255)); // fill with white.
p->fillRect(r, b);
PINDEX i, readIndex = (writeIndex + DATA_ELEMS - 20) % DATA_ELEMS;
double hscale = r.width() / 20;
double vscale = r.height() / currentMax;
int ox, nx;
double osy, ory, nsy, nry;
ox = 0;
osy = 0;
ory = 0; //Stop compiler warnings about usage.
for (i = 0; i < 20; i++) {
nx = (int)(i * hscale);
nsy = r.height() - (vscale * rate_sent[(readIndex + i) % DATA_ELEMS]);
nry = r.height() - (vscale * rate_rcvd[(readIndex + i) % DATA_ELEMS]);
if (i != 0) {
p->setPen(Qt::blue);
p->drawLine(ox, (int)osy, nx, (int)nsy);
p->setPen(Qt::red);
p->drawLine(ox, (int)ory, nx, (int)nry);
}
ox = nx;
osy = nsy;
ory = nry;
}
accessMutex.unlock();
}
syntax highlighted by Code2HTML, v. 0.9.1