#include "plot.h"
#include <QtTest>
#include <math.h>
#include <qwt_color_map.h>
#include <qwt_double_interval.h>
th::th() :
blocksize(0), blocksize_check(0)
{
}
void th::run()
{
socket->connectToHost("127.0.0.1", 9999);
connect(socket, SIGNAL(readyRead()), this, SLOT(read_socket()));
exec();
}
void th::read_socket()
{
qDebug()<<"read_socket";
if (blocksize == 0)
{
if (socket->bytesAvailable() < (int)sizeof(quint32))
return;
in >> blocksize;
qDebug()<<"inside if";
wc.wakeAll();
arr=new float[blocksize];
qDebug()<<" block size "<<blocksize;
}
//fill the array which is accessed by the PLOT class later
while (blocksize_check<blocksize)
{
in>>*(arr+blocksize_check);
blocksize_check++;
}
}
PLOT::PLOT() :
zoomfactor(1)
{
resize(800, 600);
t.start();
offset.setX(0);
offset.setY(0);
int degree=360;
int coloum;
wc.wait(&m); // replacing with QTest::qWait(5000); makes work
coloum=(int)(t.blocksize/360);
qDebug()<<" PLOT "<<t.blocksize<<"coloum "<<coloum;
while (t.blocksize == (qint32)0)
{
qDebug()<<" PLOT inside while "<<t.blocksize;
coloum=(int)(t.blocksize/360);
}
//calculate the polygon points (a circle is split in to (degree*coloum) polygons)
for (int i = 0; i<degree; i++)
{
for (int j=0; j<coloum; j++)
{
points[0].setX(j * cos(i*(3.14/180)));
points[0].setY(j * sin(i*(3.14/180)));
points[1].setX((j +1)* cos(i*(3.14/180)));
points[1].setY((j +1)* sin(i*(3.14/180)));
points[2].setX((j +1)* cos((i+1)*(3.14/180)));
points[2].setY((j +1)* sin((i+1)*(3.14/180)));
points[3].setX((j)* cos((i+1)*(3.14/180)));
points[3].setY((j)* sin((i+1)*(3.14/180)));
QVector<QPointF> vecp;
for (int k=0; k<4; k++)
vecp.append(points[k]);
pol.append(polygon);
}
}
//find min and max value to set the color value
float min=0, max=0;
for (int i=0; i<(int)t.blocksize; i++)
{
if (*(t.arr+i)>max)
max=*(t.arr+i);
if (*(t.arr+i)<min)
min=*(t.arr+i);
qDebug()<<*(t.arr+i)<<" "<<i;
}
pixmap
= QPixmap(coloum
*2, coloum
*2);
pixmap.fill(Qt::transparent);
painter.
setRenderHint(QPainter::Antialiasing,
true);
painter.translate(coloum, coloum);
for (int i = 0; i<degree; i++)
{
for (int j=0; j<coloum; j++)
{
painter.save();
painter.
setBrush(QBrush(colorMap.
color(di,
*(t.
arr +(coloum
*i
+j
)))));
painter.setPen(colorMap.color(di, *(t.arr+(coloum*i+j))));
painter.drawPolygon(pol.at(coloum*i+j));
painter.restore();
}
}
pixmap_scaled=pixmap.scaled(1*pixmap.size());
}
{
painter.save();
painter.drawPixmap(offset, pixmap_scaled);
painter.restore();
}
{
if (event->delta()>0)//if rotated forward delta() gives positive value
zoomfactor=zoomfactor+0.055;
else
zoomfactor=zoomfactor-0.055;
QSize oldsize
= pixmap_scaled.
size();
pixmap_scaled=pixmap.scaled(zoomfactor*pixmap.size());
QSize newsize
= pixmap_scaled.
size();
QSize tempsize
= (oldsize
-newsize
)/2;
offset.setX(offset.x()+tempsize.width());
offset.setY(offset.y()+tempsize.height());
update();
}
{
offset=event->pos()-mouse_press+offset;
mouse_press=event->pos();
update();
}
{
mouse_press=event->pos();
}