This is the header file for my display widget. The backBuf and paintEvent are the ones to note here. I use a pointer for the backBuf so I can decide later what the size should be.
#ifndef SIMDISPW_H
#define SIMDISPW_H
#include <QtGui/QWidget>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QColor>
#include <QtGui/QPixMap>
#include <QtCore/QMutex>
#include "ui_simdispw.h"
#include "common.h"
#include "globalvars.h"
//widget class to display realtime data generated by the simulator
{
Q_OBJECT //ignore
public:
~SimDispW();
//returns the pixmap buffer that should be drawn on
private:
Ui::SimDispWClass ui; //autogenerated by ui_simdispw.h, ignore
QPixmap *backBuf;
//the pixmap buffer that the widget updates from
protected:
void paintEvent
(QPaintEvent *);
//custom paint event that paints the window };
#endif // SIMDISPW_H
#ifndef SIMDISPW_H
#define SIMDISPW_H
#include <QtGui/QWidget>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
#include <QtGui/QColor>
#include <QtGui/QPixMap>
#include <QtCore/QMutex>
#include "ui_simdispw.h"
#include "common.h"
#include "globalvars.h"
//widget class to display realtime data generated by the simulator
class SimDispW : public QWidget
{
Q_OBJECT //ignore
public:
SimDispW(QWidget *parent = 0);
~SimDispW();
//returns the pixmap buffer that should be drawn on
QPixmap *getBackBuf();
private:
Ui::SimDispWClass ui; //autogenerated by ui_simdispw.h, ignore
QPixmap *backBuf; //the pixmap buffer that the widget updates from
protected:
void paintEvent(QPaintEvent *); //custom paint event that paints the window
};
#endif // SIMDISPW_H
To copy to clipboard, switch view to plain text mode
the source file for the display widget. I have some threading stuff in there because my data generation function is in a different thread, so you can ignore it if you want.
#include "../includes/simdispw.h"
#include "../includes/moc_simdispw.h"
SimDispW
::SimDispW(QWidget *parent
){
ui.setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose); //when the window is closed, delete the object
this->setFixedSize(2000, NUMMF);
backBuf
=new QPixmap(this
->width
(), this
->height
());
backBuf
->fill
(QColor(0,
0,
0));
//sets the whole thing to black
}
SimDispW::~SimDispW()
{
delete backBuf;
}
{
return backBuf;
}
{
p.setViewTransformEnabled(true);
p.setWindow(0, 0, backBuf->width(), backBuf->height());
p.setViewport(0, 0, this->width(), this->height());
//p.setWorldMatrixEnabled(false);
bufLock.lock(); //for thread management, locks buffer mutex
p.drawPixmap(event->rect(), *backBuf, event->rect());
//cout<<event->rect().x()<<" "<<event->rect().y()<<" "<<event->rect().width()<<" "<<event->rect().height()<<endl;
//p.drawPixmap(0,0, *backBuf);
bufLock.unlock(); //unlocks when done
}
#include "../includes/simdispw.h"
#include "../includes/moc_simdispw.h"
SimDispW::SimDispW(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose); //when the window is closed, delete the object
this->setFixedSize(2000, NUMMF);
backBuf=new QPixmap(this->width(), this->height());
backBuf->fill(QColor(0,0,0)); //sets the whole thing to black
}
SimDispW::~SimDispW()
{
delete backBuf;
}
QPixmap *SimDispW::getBackBuf()
{
return backBuf;
}
void SimDispW::paintEvent(QPaintEvent * event)
{
QPainter p(this);
p.setViewTransformEnabled(true);
p.setWindow(0, 0, backBuf->width(), backBuf->height());
p.setViewport(0, 0, this->width(), this->height());
//p.setWorldMatrixEnabled(false);
bufLock.lock(); //for thread management, locks buffer mutex
p.drawPixmap(event->rect(), *backBuf, event->rect());
//cout<<event->rect().x()<<" "<<event->rect().y()<<" "<<event->rect().width()<<" "<<event->rect().height()<<endl;
//p.drawPixmap(0,0, *backBuf);
bufLock.unlock(); //unlocks when done
}
To copy to clipboard, switch view to plain text mode
And here is the data generation function.
/*
* simthread.cpp
*
* Created on: Feb 16, 2009
* Author: wen
*/
#include "../includes/simthread.h"
#ifdef INTELCC
#include <omp.h>
#endif
SimThread
::SimThread(QObject *parent, SimDispW
*panel
){
dispW=panel;
}
void SimThread::run()
{
if(dispW==NULL || !initialized)
{
return;
}
simLoop();
}
void SimThread::simLoop()
{
CRandomSFMT0 randGen(time(NULL));
int trialTime;
bool *mfsAP=new bool[NUMMF];
bool *granulesAP=new bool[NUMMF];
bool *golgisAP=new bool[NUMMF];
if(dispW==NULL)
{
return;
}
buf=dispW->getBackBuf();
if(buf==NULL)
{
return;
}
while(true)
{
simStopLock.lock();
if(simStop)
{
simStopLock.unlock();
break;
}
simStopLock.unlock();
trialTime=time(NULL);
for(int i=0; i<NUMMF; i++)
{
if(mossyFibers[i].getMFType()==MossyFiber::activeCS+1 || mossyFibers[i].getMFType()==MossyFiber::activeCS+5)
{
mossyFibers[i].setCSOn(1);
}
else
{
mossyFibers[i].setCSOn(0);
}
}
bufLock.lock();
buf->fill(Qt::black);
bufLock.unlock();
dispW->update();
for(int i=0; i<TRIALTIME; i++)
{
bool tempAP;
bool *drawAP;
simStopLock.lock();
if(simStop)
{
simStopLock.unlock();
break;
}
simStopLock.unlock();
simPauseLock.lock();
for(int j=0; j<NUMMF; j++)
{
//mossyFibers[j].setThresh(mossyFibers[j].getThresh()+(1-mossyFibers[j].getThresh())*MossyFiber::threshDecay);
// mfsSpike[j]=randGen.fRandom()<((int)(mossyFibers[j].getCSOn() && i>=mossyFibers[j].getCSStart() && i<mossyFibers[j].getCSEnd())*mossyFibers[j].getIncFreq()+mossyFibers[j].getbgFreqCont(MossyFiber::activeContext))*mossyFibers[j].getThresh();
//
// if(mfsSpike[j])
// {
// mossyFibers[j].setThresh(0);
//
// {
// for(int k=0; k<mossyFibers[j].getNumSynGR(); k++)
// {
// granuleCells[mossyFibers[j].getConGRInd(k)].setExI(mossyFibers[j].getConGRDen(k),mfsSpike[j]);
// }
// }
// }
mfsAP[j]=mossyFibers[j].calcActivity(i, randGen);
}
#ifdef INTELCC
#pragma omp parallel shared(granuleCells, golgiCells)
#endif
{
#ifdef INTELCC
#pragma omp for schedule(static, NUMGR/16) nowait
#endif
for(int j=0; j<NUMGR; j++)
{
tempAP=granuleCells[j].calcActivity();
if(j<NUMMF)
{
granulesAP[j]=tempAP;
}
}
#ifdef INTELCC
#pragma omp for schedule(static, NUMGO/16) nowait
#endif
for(int j=0; j<NUMGO; j++)
{
golgisAP[j]=golgiCells[j].calcActivity();
}
}
simDispTypeLock.lock();
if(simDispType==0)
{
drawAP=mfsAP;
}
else if(simDispType==1)
{
drawAP=granulesAP;
}
else
{
drawAP=golgisAP;
}
simDispTypeLock.unlock();
bufLock.lock();
p.begin(buf);
p.setWindow(0,0, TRIALTIME, NUMMF);
p.setViewport(0, 0, buf->width(), buf->height());
p.setPen(Qt::white);
for(int j=0; j<NUMMF; j++)
{
if(drawAP[j])
{
p.drawPoint(i, j);
}
}
if(i>=MossyFiber::csOnset[MossyFiber::activeCS] && i<MossyFiber::csOnset[MossyFiber::activeCS]+MossyFiber::csDuration[MossyFiber::activeCS])
{
p.setPen(Qt::blue);
}
else
{
p.setPen(Qt::black);
}
for(int j=0; j<NUMMF; j++)
{
if(!drawAP[j])
{
p.drawPoint(i, j);
}
}
p.scale((float)buf->width()/5000, (float)buf->height()/NUMMF);
QRect updateArea
(p.
worldTransform().
mapRect(QRect(i,
0,
1, NUMMF
)));
if(updateArea.width()<1)
{
updateArea.setWidth(1);
}
p.end();
bufLock.unlock();
dispW->update(updateArea);
simPauseLock.unlock();
}
cout<<"trial run time: "<<time(NULL)-trialTime<<endl;
}
// for(int i=0; i<NUMMF; i++)
// {
// delete &mfsAP[i];
// delete &granulesAP[i];
// delete &golgisAP[i];
// }
delete mfsAP;
delete granulesAP;
delete golgisAP;
}
/*
* simthread.cpp
*
* Created on: Feb 16, 2009
* Author: wen
*/
#include "../includes/simthread.h"
#ifdef INTELCC
#include <omp.h>
#endif
SimThread::SimThread(QObject *parent, SimDispW *panel)
:QThread(parent)
{
dispW=panel;
}
void SimThread::run()
{
if(dispW==NULL || !initialized)
{
return;
}
simLoop();
}
void SimThread::simLoop()
{
QPixmap *buf;
QPainter p;
CRandomSFMT0 randGen(time(NULL));
int trialTime;
bool *mfsAP=new bool[NUMMF];
bool *granulesAP=new bool[NUMMF];
bool *golgisAP=new bool[NUMMF];
if(dispW==NULL)
{
return;
}
buf=dispW->getBackBuf();
if(buf==NULL)
{
return;
}
while(true)
{
simStopLock.lock();
if(simStop)
{
simStopLock.unlock();
break;
}
simStopLock.unlock();
trialTime=time(NULL);
for(int i=0; i<NUMMF; i++)
{
if(mossyFibers[i].getMFType()==MossyFiber::activeCS+1 || mossyFibers[i].getMFType()==MossyFiber::activeCS+5)
{
mossyFibers[i].setCSOn(1);
}
else
{
mossyFibers[i].setCSOn(0);
}
}
bufLock.lock();
buf->fill(Qt::black);
bufLock.unlock();
dispW->update();
for(int i=0; i<TRIALTIME; i++)
{
bool tempAP;
bool *drawAP;
simStopLock.lock();
if(simStop)
{
simStopLock.unlock();
break;
}
simStopLock.unlock();
simPauseLock.lock();
for(int j=0; j<NUMMF; j++)
{
//mossyFibers[j].setThresh(mossyFibers[j].getThresh()+(1-mossyFibers[j].getThresh())*MossyFiber::threshDecay);
// mfsSpike[j]=randGen.fRandom()<((int)(mossyFibers[j].getCSOn() && i>=mossyFibers[j].getCSStart() && i<mossyFibers[j].getCSEnd())*mossyFibers[j].getIncFreq()+mossyFibers[j].getbgFreqCont(MossyFiber::activeContext))*mossyFibers[j].getThresh();
//
// if(mfsSpike[j])
// {
// mossyFibers[j].setThresh(0);
//
// {
// for(int k=0; k<mossyFibers[j].getNumSynGR(); k++)
// {
// granuleCells[mossyFibers[j].getConGRInd(k)].setExI(mossyFibers[j].getConGRDen(k),mfsSpike[j]);
// }
// }
// }
mfsAP[j]=mossyFibers[j].calcActivity(i, randGen);
}
#ifdef INTELCC
#pragma omp parallel shared(granuleCells, golgiCells)
#endif
{
#ifdef INTELCC
#pragma omp for schedule(static, NUMGR/16) nowait
#endif
for(int j=0; j<NUMGR; j++)
{
tempAP=granuleCells[j].calcActivity();
if(j<NUMMF)
{
granulesAP[j]=tempAP;
}
}
#ifdef INTELCC
#pragma omp for schedule(static, NUMGO/16) nowait
#endif
for(int j=0; j<NUMGO; j++)
{
golgisAP[j]=golgiCells[j].calcActivity();
}
}
simDispTypeLock.lock();
if(simDispType==0)
{
drawAP=mfsAP;
}
else if(simDispType==1)
{
drawAP=granulesAP;
}
else
{
drawAP=golgisAP;
}
simDispTypeLock.unlock();
bufLock.lock();
p.begin(buf);
p.setWindow(0,0, TRIALTIME, NUMMF);
p.setViewport(0, 0, buf->width(), buf->height());
p.setPen(Qt::white);
for(int j=0; j<NUMMF; j++)
{
if(drawAP[j])
{
p.drawPoint(i, j);
}
}
if(i>=MossyFiber::csOnset[MossyFiber::activeCS] && i<MossyFiber::csOnset[MossyFiber::activeCS]+MossyFiber::csDuration[MossyFiber::activeCS])
{
p.setPen(Qt::blue);
}
else
{
p.setPen(Qt::black);
}
for(int j=0; j<NUMMF; j++)
{
if(!drawAP[j])
{
p.drawPoint(i, j);
}
}
p.scale((float)buf->width()/5000, (float)buf->height()/NUMMF);
QRect updateArea(p.worldTransform().mapRect(QRect(i, 0, 1, NUMMF)));
if(updateArea.width()<1)
{
updateArea.setWidth(1);
}
p.end();
bufLock.unlock();
dispW->update(updateArea);
simPauseLock.unlock();
}
cout<<"trial run time: "<<time(NULL)-trialTime<<endl;
}
// for(int i=0; i<NUMMF; i++)
// {
// delete &mfsAP[i];
// delete &granulesAP[i];
// delete &golgisAP[i];
// }
delete mfsAP;
delete granulesAP;
delete golgisAP;
}
To copy to clipboard, switch view to plain text mode
the really relevant part of that function is the following:
bufLock.lock();
p.begin(buf);
p.setWindow(0,0, TRIALTIME, NUMMF);
p.setViewport(0, 0, buf->width(), buf->height());
p.setPen(Qt::white);
for(int j=0; j<NUMMF; j++)
{
if(drawAP[j])
{
p.drawPoint(i, j);
}
}
if(i>=MossyFiber::csOnset[MossyFiber::activeCS] && i<MossyFiber::csOnset[MossyFiber::activeCS]+MossyFiber::csDuration[MossyFiber::activeCS])
{
p.setPen(Qt::blue);
}
else
{
p.setPen(Qt::black);
}
for(int j=0; j<NUMMF; j++)
{
if(!drawAP[j])
{
p.drawPoint(i, j);
}
}
p.scale((float)buf->width()/5000, (float)buf->height()/NUMMF);
QRect updateArea
(p.
worldTransform().
mapRect(QRect(i,
0,
1, NUMMF
)));
if(updateArea.width()<1)
{
updateArea.setWidth(1);
}
p.end();
bufLock.unlock();
dispW->update(updateArea);
simPauseLock.unlock();
bufLock.lock();
p.begin(buf);
p.setWindow(0,0, TRIALTIME, NUMMF);
p.setViewport(0, 0, buf->width(), buf->height());
p.setPen(Qt::white);
for(int j=0; j<NUMMF; j++)
{
if(drawAP[j])
{
p.drawPoint(i, j);
}
}
if(i>=MossyFiber::csOnset[MossyFiber::activeCS] && i<MossyFiber::csOnset[MossyFiber::activeCS]+MossyFiber::csDuration[MossyFiber::activeCS])
{
p.setPen(Qt::blue);
}
else
{
p.setPen(Qt::black);
}
for(int j=0; j<NUMMF; j++)
{
if(!drawAP[j])
{
p.drawPoint(i, j);
}
}
p.scale((float)buf->width()/5000, (float)buf->height()/NUMMF);
QRect updateArea(p.worldTransform().mapRect(QRect(i, 0, 1, NUMMF)));
if(updateArea.width()<1)
{
updateArea.setWidth(1);
}
p.end();
bufLock.unlock();
dispW->update(updateArea);
simPauseLock.unlock();
To copy to clipboard, switch view to plain text mode
I do some scaling coordinate transform stuff with the display, but you can ignore that if you don't need it. The code is a bit long and has a lot of irrelevant stuff in there for you, so feel free to ask if something doesn't make sense.
Bookmarks