24 #include "evodataviewer.h"
28 #include <QPaintEvent>
29 #include <QApplication>
30 #include <QScrollArea>
32 #include <QStringList>
34 #include <QtAlgorithms>
35 #include <configurationparameters.h>
48 this->setMaximumWidth(chunksize+label_width);
49 this->setMinimumWidth(label_width*2);
50 this->setMinimumHeight(10*nchunks);
51 this->nviewChange=
false;
52 vchunks=
new int[nchunks];
54 this->setAttribute(Qt::WA_OpaquePaintEvent,
true);
56 qpixmap =
new QPixmap(width(), height());
59 this->nchunks = nchunks;
60 this->chunksize = chunksize;
61 this->nvchunks=nchunks;
64 for(
int i=0;i<nchunks;i++)
66 dataChunks[i] =
new DataChunk(QString(
"chunk"),QColor(255,0,0),chunksize,
true);
69 stepChunk=
new DataChunk(QString(
"step"),QColor(255,0,0),chunksize,
false);
75 qtimer=
new QTimer(
this);
76 qtimer->setInterval(4000);
77 QObject::connect(qtimer,SIGNAL(timeout()),
this,SLOT(pickUnvisible()));
84 pickValueVisible =
false;
87 EvoDataViewer::~EvoDataViewer()
89 for(
int i=0;i<nchunks;i++) {
101 void EvoDataViewer::evoDataPaint()
104 QPainter painter(qpixmap);
105 QPen pen(Qt::black, 1);
106 QPen pen2(Qt::lightGray);
107 QPen pen3(Qt::darkGray);
111 painter.fillRect(0,0,width(),height(),Qt::white);
113 for(
int i=0;i<nvchunks+1;i++)
119 painter.drawText(4,(
int)(vertical_step*(i+1)-vertical_step/2.0),dataChunks[vchunks[i]]->getLabel());
120 painter.drawText(label_width-40,(
int)(vertical_step*(i+1))-4,QString::number(dataChunks[vchunks[i]]->getRangeMin()));
121 painter.drawText(label_width-40,(
int)(vertical_step*(i+1))-4-(
int)(vertical_step-20),QString::number(dataChunks[vchunks[i]]->getRangeMax()));
125 painter.drawLine(0,(
int)(vertical_step*i),width(),(
int)(vertical_step*i));
127 painter.setPen(pen3);
128 painter.drawLine(0,(
int)(vertical_step*i+1),width(),(
int)(vertical_step*i+1));
130 painter.setPen(pen2);
131 painter.drawLine(0,(
int)(vertical_step*i+2),width(),(
int)(vertical_step*i+2));
137 hh=(int)(vertical_step*(nvchunks));
140 painter.drawLine(label_width-1,0,label_width-1,hh);
141 painter.setPen(pen2);
143 painter.drawLine(label_width,0,label_width,hh);
144 painter.drawLine(label_width-2,0,label_width-2,hh);
148 void EvoDataViewer::updateGraphic(
int ch)
155 if (!dataChunks[ch]->isVisible()) {
160 QPainter painter(qpixmap);
161 const QPen pen(Qt::red);
162 const QPen penw(Qt::white);
163 const QPen leadpen(Qt::black);
167 for (
int ii = 0; ii < ch; ii++) {
168 if (dataChunks[ii]->isVisible()) {
173 int actualindex = -1;
176 if(dataChunks[i]->getIndex() > -1) {
177 actualindex=(int)(dataChunks[vchunks[i]]->getIndex()*dataChunks[vchunks[i]]->getDPRatio());
178 predindex=(int)((dataChunks[vchunks[i]]->getIndex()-1)*dataChunks[vchunks[i]]->getDPRatio());
181 QPen personalpen(dataChunks[vchunks[i]]->getColor());
182 painter.setPen(personalpen);
185 int zeropoint=(int)((vertical_step-panning_width-1)*dataChunks[vchunks[i]]->getZeroValue());
188 painter.drawLine(actualindex+label_width,(
int)(vertical_step*(i+1)-1)-zeropoint,actualindex+label_width,(
int)(vertical_step*(i+1)-1)-zeropoint-(
int)((dataChunks[vchunks[i]]->getValueToDraw()-dataChunks[vchunks[i]]->getZeroValue())*(vertical_step-panning_width)));
189 }
else if (style == 1) {
190 painter.drawLine(actualindex+label_width,(
int)(vertical_step*(i+1)-1)-zeropoint-(
int)((dataChunks[vchunks[i]]->getValueToDraw()-dataChunks[vchunks[i]]->getZeroValue())*(vertical_step-panning_width)),predindex+label_width,(
int)(vertical_step*(i+1)-1)-zeropoint-(
int)((dataChunks[vchunks[i]]->getValue(dataChunks[vchunks[i]]->getIndex()-1)-dataChunks[vchunks[i]]->getZeroValue())*(vertical_step-panning_width)));
196 painter.setPen(penw);
197 painter.drawLine(actualindex+label_width+1,(
int)(vertical_step*(i+1)-1),actualindex+label_width+1,(
int)(vertical_step*(i+1)-1)-(
int)(vertical_step-panning_width));
200 painter.setPen(leadpen);
201 painter.drawLine(actualindex+label_width+2,(
int)(vertical_step*(i+1)-1),actualindex+label_width+2,(
int)(vertical_step*(i+1)-1)-(
int)(vertical_step-panning_width));
204 void EvoDataViewer::resizeEvent(QResizeEvent *evt)
208 if (qpixmap!=NULL)
delete qpixmap;
209 qpixmap=
new QPixmap(width(), height());
213 void EvoDataViewer::paintEvent(QPaintEvent *evt)
219 if (pickY<20) offy=20-pickY;
220 if ((width()-pickX)<50) offx=-60;
222 QPainter painter(
this);
223 painter.drawPixmap(0, 0, width(), height(), *qpixmap);
226 QPen bpen(Qt::black);
227 painter.drawText(pickX+offx,pickY+offy,QString(
"Val : ")+QString::number(pickValue));
228 painter.drawText(pickX+offx,pickY+10+offy,QString(
"Step: ")+QString::number(pickStep));
235 dataChunks[ch]->setLabel(label);
241 if ((ch >= 0) && (ch < this->nchunks)) {
242 dataChunks[ch]->setData(value);
251 for(
int i=0;i<nchunks;i++)
253 if(name == dataChunks[i]->getLabel())
266 dataChunks[ch]->setRange(mn, mx);
269 void EvoDataViewer::mousePressEvent(QMouseEvent* evt)
272 if(evt->button()==Qt::LeftButton)
276 int nochunk = ((float)yc/vertical_step);
277 if (nochunk >= nvchunks) {
280 int chunkIndex = xc-label_width;
281 float val=dataChunks[vchunks[nochunk]]->getValue((
int)((
float)chunkIndex/dataChunks[vchunks[nochunk]]->getDPRatio()));
282 val = dataChunks[vchunks[nochunk]]->linearMap(val,0.0, 1.0,dataChunks[vchunks[nochunk]]->getRangeMin(), dataChunks[vchunks[nochunk]]->getRangeMax());
288 pickStep = (int)stepChunk->getValue((
int)((
float)chunkIndex/dataChunks[vchunks[nochunk]]->getDPRatio()));
289 pickValueVisible=
true;
295 if(evt->button()==Qt::RightButton)
298 elw =
new EvoListViewer (this->dataChunks,this->nchunks, &this->nviewChange);
302 elw->restoreSelected();
303 elw->setVisible(
true);
310 void EvoDataViewer::pickUnvisible()
312 pickValueVisible=
false;
322 stepChunk->setDataRaw(step);
343 int EvoDataViewer::visibleChunks()
346 for(
int i=0;i<nchunks;i++)
347 if(dataChunks[i]->isVisible())
357 for(
int i=0;i<this->nchunks;i++)
359 if (dataChunks[i]->isVisible())
367 vertical_step=height()/(double)nvchunks;
370 void EvoDataViewer::setChunkColor(
int ch, QColor color)
372 dataChunks[ch]->setColor(color);
377 dataChunks[ch]->setRange(rangeMin, rangeMax);
378 dataChunks[ch]->setLabel(label);
379 dataChunks[ch]->setColor(color);
380 dataChunks[ch]->setVisible(visible);
387 vertical_step=height()/(double)nvchunks;
389 for(
int i=0;i<nchunks;i++)
392 dataChunks[i]->setDPRatio((
double)(width()-label_width)/(
double)chunksize);
399 DataChunk::DataChunk(
const QString& label,
const QColor& color,
int size,
bool visible)
404 this->visible=visible;
406 data=
new double[size];
408 for(
int i=0;i<size;i++)
418 DataChunk::~DataChunk()
423 void DataChunk::setColor(QColor color)
429 void DataChunk::setData(
double value)
433 data[index]=linearMap(value, min, max);
434 checkMaxValue(data[index]);
436 if(index>size-2) index=-1;
439 void DataChunk::setDataRaw(
double value)
443 checkMaxValue(value);
446 if(index>size-2) index=-1;
448 void DataChunk::setDataRaw(
int ind,
double value)
450 if(ind>-1 && ind<size)
452 checkMaxValue(value);
455 void DataChunk::setLabel(
const QString& label)
460 void DataChunk::setRange(
double min,
double max)
464 zeroValue=linearMap(0.0,min,max);
467 void DataChunk::setStyle(
int style)
472 const QString& DataChunk::getLabel()
479 double DataChunk::getValueToDraw()
484 int DataChunk::getIndex()
489 double DataChunk::linearMap(
double x,
double rmin,
double rmax,
double outMin,
double outMax)
492 double m = ( outMax-outMin )/( rmax-rmin );
493 double q = outMin - m*rmin;
495 if (ret < outMin)
return outMin;
496 if (ret > outMax)
return outMax;
502 double DataChunk::getRangeMin()
508 double DataChunk::getRangeMax()
513 double DataChunk::getZeroValue()
518 void DataChunk::setDPRatio(
double val)
524 double DataChunk::getDPRatio()
529 double DataChunk::getValue(
int ind)
534 bool DataChunk::isVisible()
539 void DataChunk::setVisible(
bool vis)
543 QColor& DataChunk::getColor()
547 double DataChunk::getMaxValue()
552 namespace __DataChunk_loadRawData_helpers {
555 bool orderByNumberAfterColon(
const QString& s1,
const QString& s2)
559 QStringList list = s1.split(
':', QString::SkipEmptyParts);
560 if (list.size() < 2) {
563 const double ns1 = list[1].toDouble();
564 list = s2.split(
':', QString::SkipEmptyParts);
565 if (list.size() < 2) {
568 const double ns2 = list[1].toDouble();
573 void DataChunk::checkMaxValue(
double val)
575 if(val>maxValue) maxValue=val;
577 bool DataChunk::loadRawData(
const QString &filename,
int column)
580 if (filename.endsWith(
".fit", Qt::CaseInsensitive)) {
582 QFile file(filename);
584 if(file.open(QIODevice::ReadOnly)) {
585 QTextStream in(&file);
586 line = in.readLine();
588 while(!line.isNull()) {
591 list = line.split(
" ");
592 if (column<0 && column>=list.size()) {
593 Logger::error(QString(
"column number %1 does not exist in the loaded file.").arg(column));
597 QString value=(QString)list.at(column);
598 setDataRaw(index, value.toDouble());
611 if (!params.loadParameters(filename)) {
619 paramName =
"bestFitness";
622 paramName =
"averageFitness";
625 paramName =
"worstFitness";
628 paramName =
"Unknown";
633 QStringList genotypes = params.getGroupsWithPrefixList(
"/",
"GENOTYPES:");
635 qSort(genotypes.begin(), genotypes.end(), __DataChunk_loadRawData_helpers::orderByNumberAfterColon);
637 for (
int i = 0; i < genotypes.size(); i++) {
653 EvoListViewer::EvoListViewer(DataChunk **dataChunks,
int n,
bool *nviewChange , QWidget *parent) :
656 this->dataChunks=dataChunks;
658 this->nvchange=nviewChange;
659 QListWidgetItem *listItem;
662 layout=
new QGridLayout();
663 listwidget =
new QListWidget();
664 deselectAll =
new QPushButton(
"deselect all");
665 QPushButton *bcancel =
new QPushButton(
"cancel");
666 QPushButton *bok =
new QPushButton(
"ok");
670 listwidget->insertItem(i,
new QListWidgetItem());
672 listItem=listwidget->item(i);
673 listItem->setText(dataChunks[i]->getLabel());
674 Qt::ItemFlags mflags;
675 mflags= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
676 listItem->setFlags(mflags);
677 listItem->setCheckState(Qt::Checked);
683 layout->addWidget(deselectAll, 0,0);
684 layout->addWidget(listwidget,1,0);
685 layout->addWidget(bcancel,2,0);
686 layout->addWidget(bok,3,0);
690 this->setLayout(layout);
691 this->setWindowTitle(
"Chunks List");
693 this->setGeometry(50,50,250,300);
694 this->setVisible(
true);
697 QObject::connect(bok,SIGNAL(clicked()),
this,SLOT(okSelected()));
698 QObject::connect(bcancel,SIGNAL(clicked()),
this,SLOT(restoreSelected()));
699 QObject::connect(deselectAll, SIGNAL(clicked()),
this,SLOT(allOnOrAllOff()));
709 void EvoListViewer::okSelected()
711 for(
int i=0;i<nchunks;i++)
713 if(this->listwidget->item(i)->checkState()== Qt::Checked)
714 dataChunks[i]->setVisible(
true);
716 dataChunks[i]->setVisible(
false);
718 this->setVisible(
false);
722 void EvoListViewer::restoreSelected()
725 this->setVisible(
false);
726 for(
int i=0;i<nchunks;i++)
728 if(dataChunks[i]->isVisible())
729 this->listwidget->item(i)->setCheckState(Qt::Checked);
731 this->listwidget->item(i)->setCheckState(Qt::Unchecked);
737 void EvoListViewer::allOnOrAllOff()
739 bool someoneIsChecked =
false;
740 for(
int i=0;i<nchunks;i++)
742 if(listwidget->item(i)->checkState() == Qt::Checked)
743 someoneIsChecked =
true;
750 deselectAll->setText(
"select all");
755 deselectAll->setText(
"deselect all");
758 for(
int i=0;i<nchunks;i++)
761 this->listwidget->item(i)->setCheckState(Qt::Checked);
763 this->listwidget->item(i)->setCheckState(Qt::Unchecked);
774 EvoListViewer::~EvoListViewer()
784 void FitViewer::setValues(
int gen,
double min,
double average,
double max)
788 fitVal[gen][1]=average;
795 checkGraphRange(min);
796 checkGraphRange(average);
797 checkGraphRange(max);
802 int FitViewer::checkGraphRange(
double val)
818 void FitViewer::checkChunkRange(
int chunk)
821 for(
int c=0;c<=currentGen;c++)
822 checkGraphRange(dataChunks[chunk]->getValue(c));
825 FitViewer::FitViewer( QWidget* parent, Qt::WindowFlags flags ) : QWidget( parent, flags )
835 FitViewer::FitViewer(
int nchunks,
int chunksize, QWidget* parent, Qt::WindowFlags flags) : QWidget( parent, flags )
840 this->nchunks=nchunks;
841 this->chunksize=chunksize;
843 sortedIndex=
new int[nchunks];
844 dataChunks =
new DataChunk*[nchunks];
845 for(
int i=0;i<nchunks;i++) {
846 dataChunks[i] =
new DataChunk(QString(
"chunk"),QColor(255,0,0),chunksize,
true);
851 FitViewer::~FitViewer()
853 delete[] sortedIndex;
854 if (dataChunks != NULL) {
855 for (
int i = 0; i < nchunks; i++) {
856 delete dataChunks[i];
862 void FitViewer::paintEvent(QPaintEvent* )
864 QPainter painter(
this);
865 QPen blackPen(Qt::black);
866 QPen bluePen(Qt::blue);
867 QPen greenPen(Qt::green);
868 QPen redPen(Qt::red);
871 painter.fillRect(0,0,width(),height(),Qt::white);
872 painter.setPen(blackPen);
873 painter.setRenderHint(QPainter::Antialiasing,
false);
874 xstep=(double)(width()-2*padding)/(
double)(currentGen);
875 wyaxes=height()-2*padding;
877 painter.drawRect(padding,padding,width()-2*padding,height()-2*padding);
878 if (vmin>0) zeroy=height()-padding;
881 zeroy=((-1*vmin)/(vmax-vmin))*wyaxes;
884 painter.drawLine(padding,height()-padding-zeroy,width()-padding,height()-padding-zeroy);
885 painter.drawText(padding/2.0,padding,QString::number(vmax));
886 painter.drawText(padding/2.0,height()-padding,QString::number(vmin));
887 painter.drawText(padding/2.0,height()-padding-zeroy,QString::number(0));
890 painter.drawText(width()-padding,height()-padding-zeroy,QString::number(currentGen));
893 painter.drawText(width()/2.0-padding,padding/2.0,gtitle);
896 painter.drawText(width()/2.0-padding,height()-padding/2.0,xlabel);
902 painter.translate(xt,yt);
903 painter.rotate(-90.0);
904 painter.drawText(0,0,ylabel);
917 painter.setRenderHint(QPainter::Antialiasing,
true);
918 for (
int dc=0;dc<nchunks;dc++)
920 int c=sortedIndex[dc];
921 painter.setPen(dataChunks[c]->getColor());
922 painter.drawText(width()-padding+2,padding+dc*20,dataChunks[c]->getLabel());
924 for (
int i=1;i<currentGen+1;i++)
935 checkGraphRange(dataChunks[c]->getValue(i-1));
936 painter.drawLine(padding+xstep*(i-1), height()-padding-getYnormValue(dataChunks[c]->getValue(i-1)),padding+xstep*i,height()-padding-getYnormValue(dataChunks[c]->getValue(i)));
944 double FitViewer::getYnormValue(
double val)
948 return (val/vmax)*wyaxes;
953 return ((val-vmin)/(vmax-vmin))*wyaxes;
958 void FitViewer::setChunkLabel(
int ch,
const QString &label)
961 dataChunks[ch]->setLabel(label);
963 void FitViewer::setChunkProperties(
int ch,
const QString &label, QColor color,
bool visible)
965 dataChunks[ch]->setLabel(label);
966 dataChunks[ch]->setColor(color);
967 dataChunks[ch]->setVisible(visible);
970 void FitViewer::setChunkValue(
int ch,
int ind,
double value)
972 if (ch>= 0 && ch < this->nchunks)
974 dataChunks[ch]->setDataRaw(ind,value);
975 checkGraphRange(value);
979 bool FitViewer::setChunkValue(
const QString &name,
int ind,
double value)
981 for(
int i=0;i<nchunks;i++)
983 if(name == dataChunks[i]->getLabel())
985 setChunkValue(i,ind, value);
986 checkGraphRange(value);
993 void FitViewer::diplayUntilStep(
int st)
998 void FitViewer::setLabels(
const QString &title,
const QString &xlabel,
const QString &ylabel)
1001 this->xlabel=xlabel;
1002 this->ylabel=ylabel;
1005 void FitViewer::reset()
1010 setMinimumSize(padding*4,padding*4);
1014 bool FitViewer::loadRawData(
int nchunk,
const QString &filename,
int column)
1017 res=dataChunks[nchunk]->loadRawData(filename,column);
1021 if((dataChunks[nchunk]->getIndex()-1)>currentGen) currentGen=dataChunks[nchunk]->getIndex()-1;
1023 checkChunkRange(nchunk);
1027 int FitViewer::getCurrentGeneration()
1032 void FitViewer::sortchunks()
1035 for(
int i=0;i<nchunks;i++)
1044 for(
int i=0;i<nchunks-1;i++)
1046 if (dataChunks[sortedIndex[i]]->getMaxValue()<dataChunks[sortedIndex[i+1]]->getMaxValue())
1049 sortedIndex[i]=sortedIndex[i+1];