Allink  v0.1
ElementiGrafici.cpp
00001 /***********************************************************************
00002 ElementiGrafici: Class that interface the VarData, VarElementiGrafici 
00003 and Matematica classes function for comunicating with main window of 
00004 Visualizza. The main function is the plotting function DisegnaPunti() 
00005 that provides the visualisation of two set of data. The bars at the 
00006 bottom permits to change the visualisation and the limit for the analisys
00007  Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00008 
00009 
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation; either version 2 of the License, or
00013 (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 ***********************************************************************/
00024 #include "ElementiGrafici.h"
00025 ;
00026 #include <qevent.h>
00027 #include <qpainter.h>
00028 #include <qlabel.h>
00029 
00030 ElementiGrafici::ElementiGrafici(QWidget *parent,const char *name)
00031 :QWidget(parent,name){
00032   PuntiMax = 1000;
00033   NVisMin=0;
00034   NVisMax=1000;
00035   NBin = 100;
00036   NMobile = 1;
00037   NCorrela = 2;
00038   Coord = 2;
00039   yMax = 1.;
00040   CoordX = 0;
00041   CoordY = 0;
00042   CoordDY = 0;
00043   NVar = 1;
00044   FontSize = 42;
00045   RatioWidthHeight = 1.3;
00046   PointSize = 10;
00047   IfNorm = 0;
00048   GrigliaX = 6;
00049   GrigliaY = 6;
00050   //Punti = NULL;
00051   //PuntiBlu = NULL;
00052   //Ascissa = NULL;
00053   stringa = new char[256];
00054   //  PuntiLinea = new QPointArray[3];
00055   cPos = new char[30];
00056   nomeFile = new char[160];
00057   sprintf(nomeFile,"Density.dat");
00058   nomeConf = new char[160];
00059   sprintf(nomeConf,"Vis.conf");
00060   nomeSalva = new char[160];
00061   sprintf(nomeSalva,"Grafico.ps");
00062   // nomeTit = new QString();
00063   // nomeEtX = new QString();
00064   // nomeEtY = new QString();
00065   NomeEntrata(QString(nomeFile));
00066   setPalette( QPalette( QColor( 250, 250, 200) ) );
00067   //  TempoGrafica = new QTimer(this, "tempo della grafica");
00068   //  connect(TempoGrafica,SIGNAL(timeout() ),this,SLOT());
00069   ErrPrima = new QErrorMessage(this);
00070   Messaggio = new QMessageBox(this,"Messaggio");
00071   Stampante = new QPrinter(QPrinter::PrinterResolution);
00072   Stampante->setFullPage(TRUE);
00073   Stampante->setOutputToFile(TRUE);
00074   Stampante->setOutputFileName(QString(nomeSalva));
00075   Stampante->setOrientation(QPrinter::Landscape);
00076   Stampante->setPageSize(QPrinter::A4);
00077   Font.setFamily("Helvetica");
00078   Font.setWeight(QFont::Bold);
00079   IfDisegna = 0;
00080   IfRiscala = RIS_TUTTI;
00081   PrimaVolta = 1;
00082   Linee = 1;
00083   Quadrati = 0;
00084   Griglia = 1;
00085   NSequenza = 0;
00086   IfStampa = 0;
00087   NDis = 0;
00088   m1.Min = 0.;
00089   DigPrecX = 3;
00090   DigPrecY = 3;
00091   FormatPrecX = 'g';
00092   FormatPrecY = 'g';
00093   NElMinY=0;
00094   NElMaxY=0;
00095   ViewportX = 0.;//0.05;
00096   ViewportY = 0.;//0.05;
00097   GrLinee = new QColor[INIT_NUM];
00098   GrLabel = new QString[INIT_NUM];
00099   Colors =  new QColor[INIT_NUM];
00100   LineaCome = new int[INIT_NUM];
00101   LineaQuale = new char*[INIT_NUM];
00102   for(int c=0;c<10;c++) LineaQuale[c] = new char[INIT_NUM];
00103   VoltaMomenti = 0;
00104   QColor Colors1[INIT_NUM] = {QColor(157,128,43,255),
00105                QColor(180,40,43,255),
00106                QColor(20,180,70,255),
00107                QColor(60,100,180,255),
00108                QColor(180,50,180,255),
00109                QColor(200,200,50,255),
00110                QColor(50,180,180,255),
00111                QColor(120,70,120,255),
00112                QColor(180,10,240,255),
00113                QColor(125,180,113,255),
00114                QColor(160,110,110,255),
00115                QColor(230,60,220,255),
00116                QColor(132,20,220,255),
00117                QColor(180,110,170,255),
00118                QColor(225,0,143,255)};
00119   //QColor(0,140,210,255)};
00120   for(int c=0;c<10;c++){
00121     Colors[c] = Colors1[c];
00122   }
00123   PosLegenda[0] = 2.0;
00124   PosLegenda[1] = 2.0;
00125   PosLegenda[2] = 3.0;
00126   PosLegenda[3] = 3.0;
00127   PosInterp[0] = .5;
00128   PosInterp[1] = .2;
00129 }
00130 ElementiGrafici::~ElementiGrafici(){
00131   delete [] stringa;
00132   delete [] cPos;
00133   delete [] nomeFile;
00134   delete [] nomeSalva;
00135   delete [] nomeTit;
00136   delete [] nomeConf;
00137   delete [] nomeEtX;
00138   delete [] nomeEtY;
00139   delete [] GrLinee;
00140   delete [] GrLabel;
00141   delete [] Colors;
00142   delete [] LineaCome;
00143   delete [] LineaQuale;
00144   delete ErrPrima;
00145   delete Messaggio;
00146   delete Stampante;
00147 }
00148 void ElementiGrafici::ChooseDataFile(char *FileName){
00149   NomeEntrata(QString(FileName));
00150 }
00151 void ElementiGrafici::ChooseConfFile(char *FileName){
00152   NomeConf(QString(FileName));
00153 }
00154 void ElementiGrafici::Apri(){
00155   int NFile = 1;
00156   char **argv = (char **)calloc(NFile,sizeof(char));
00157   int FileList = 0;
00158   for(int f=0;f<NFile;f++){
00159     argv[f] = (char *)calloc(120,sizeof(char));
00160   }
00161   sprintf(argv[0],"%s",nomeFile);
00162   Apri(argv,&FileList,NFile);
00163   for(int f=0;f<NFile;f++){
00164     free(argv[f]);
00165   }
00166   free(argv);
00167 }
00168 void ElementiGrafici::Apri(char **argv,int *FileList,int NFile){
00169   if(!PrimaVolta){
00170     delete v1;
00171     //    delete Punti;
00172   }
00173   NomeFile(QString(argv[FileList[0]]));
00174   v1 = new VarDatFile(argv,FileList,NFile,NBin);
00175   //v1 = new Variabili(argv[FileList[0]],NBin);
00176   //for(int f=1;f<NFile;f++)
00177   //v1->Aggiungi(argv[FileList[f]]);
00178   NMax = v1->pNMax();
00179   PuntiMax = NMax;
00180   LogLog = DIS_NOLOG;
00181   NVar = v1->pNVar();
00182   GrLinee = (QColor *)realloc(GrLinee,NVar*sizeof(QColor));
00183   delete [] GrLabel;
00184   GrLabel = new QString[NVar];
00185   //GrLabel = (QString *)realloc(GrLabel,NVar*sizeof(QString));
00186   LineaCome = (int *)realloc(LineaCome,NVar*sizeof(int));
00187   //LineaQuale = (char **)realloc(LineaQuale,NVar*sizeof(char));
00188   //for(int c=0;c<NVar;c++)LineaQuale[c] = (char *)realloc(LineaQuale[c],60*sizeof(char));
00189   for(int c=0,cc=0,init=0;c<NVar;c++,cc++){
00190     if(!(c%10)) cc = init++;
00191     if(init > 10) init = 0;
00192     GrLinee[c] = Colors[cc];
00193     LineaCome[c] = LINEA_TRATTO;
00194     //sprintf(LineaQuale[c],"");
00195   }
00196   ImpNVarX(REF_SEQ,NVar);
00197   ImpNVarY(0,NVar);
00198   CoordX = 0;
00199   CoordY = 0;
00200   ImpInter(NVisMax=NMax);
00201   ImpInterEl(NElMax=NMax);
00202   ImpNVisMax(NVisMax=NMax);
00203   ImpNVisMin(NVisMin=0);
00204   ImpNElMax(NElMax=NMax);
00205   ImpNElMin(NElMin=0);
00206   if(NVar > 1){
00207     CoordY = 1;
00208   }
00209   else {
00210     CoordY = 0;
00211     CoordX = REF_SEQ;
00212   }
00213   sprintf(stringa,"Caricati %d vettori da %d Punti da %s",NVar,NMax,nomeFile);
00214   sprintf(stringa,"%s",v1->PrintHeader());
00215   ImpCoordX(CoordX);
00216   ImpCoordY(CoordY);
00217   PrimaVolta = 0;
00218   IfDisegna = 0;
00219   DIS_ADD_TYPE(IfDisegna,DIS_TUTTI);
00220   IfRiscala = 0;
00221   DIS_ADD_TYPE(IfRiscala,RIS_TUTTI);
00222   repaint();
00223 }
00224 void ElementiGrafici::ApriExt(double **ExtSt,int ExtNMax,int ExtNVar,int ExtNBin){
00225   if(!PrimaVolta){
00226     delete v1;
00227     //    delete Punti;
00228   }
00229   NBin = ExtNBin;
00230   NMax = ExtNMax;
00231   NVar = ExtNVar;
00232   NomeFile(QString(nomeFile));
00233   v1 = new VarDatFile(ExtSt,NMax,NVar,NBin);
00234   LogLog = DIS_NOLOG;
00235   NVar = v1->pNVar();
00236   GrLinee = (QColor *)realloc(GrLinee,NVar*sizeof(QColor));
00237   delete [] GrLabel;
00238   GrLabel = new QString[NVar];
00239   //  GrLabel = (QString *)realloc(GrLinee,NVar*sizeof(QString));
00240   LineaCome = (int *)realloc(LineaCome,NVar*sizeof(int));
00241   //LineaQuale = (char **)realloc(LineaQuale,NVar*sizeof(char));
00242   //  for(int c=0;c<NVar;c++)LineaQuale[c] = (char *)realloc(LineaQuale[c],60*sizeof(char));
00243   for(int c=0;c<NVar;c++){
00244     if(c<10)
00245       GrLinee[c] = Colors[c];
00246     else 
00247       GrLinee[c] = Colors[0];
00248     LineaCome[c] = LINEA_TRATTO;
00249     //sprintf(LineaQuale[c],"");
00250   }
00251   ImpNVarX(-1,NVar);
00252   ImpNVarY(0,NVar);
00253   ImpInter(NVisMax=NMax);
00254   ImpInterEl(NElMax=NMax);
00255   ImpNVisMax(NVisMax=NMax);
00256   ImpNVisMin(NVisMin=0);
00257   ImpNElMax(NElMax=NMax);
00258   ImpNElMin(NElMin=0);
00259   sprintf(stringa,"Caricati %d vettori da %d Punti da %s",NVar,NMax,nomeFile);
00260   sprintf(stringa,"%s",v1->PrintHeader());
00261   ImpCoordX(CoordX);
00262   ImpCoordY(CoordY);
00263   PrimaVolta = 0;
00264   //for(int i=0;i<NMax;i++) printf("%lf \n",ExtSt[0][i]);
00265   printf("%d %d %d\n",NMax,NVar,NBin);
00266   repaint();
00267 }
00268 void ElementiGrafici::ApriExt(VarDatFile *v1Ext){
00269   if(!PrimaVolta){
00270     delete v1;
00271     //    delete Punti;
00272   }
00273   v1 = new VarDatFile(0,0,0);
00274   v1 = v1Ext;
00275   NBin = v1->NBin;
00276   NMax = v1->pNMax();
00277   NVar = v1->pNVar();
00278   NomeFile(QString(nomeFile));
00279   LogLog = DIS_NOLOG;
00280   GrLinee = (QColor *)realloc(GrLinee,NVar*sizeof(QColor));
00281   delete [] GrLabel;
00282   GrLabel = new QString[NVar];
00283   //  GrLabel = (QString *)realloc(GrLabel,NVar*sizeof(QString));
00284   LineaCome = (int *)realloc(LineaCome,NVar*sizeof(int));
00285   //LineaQuale = (char **)realloc(LineaQuale,NVar*sizeof(char));
00286   //for(int c=0;c<NVar;c++)LineaQuale[c] = (char *)realloc(LineaQuale[c],60*sizeof(char));
00287   for(int c=0;c<NVar;c++){
00288     if(c<10)
00289       GrLinee[c] = Colors[c];
00290     else 
00291       GrLinee[c] = Colors[0];
00292     LineaCome[c] = LINEA_TRATTO;
00293     //    sprintf(LineaQuale[c],"");
00294   }
00295   ImpNVarX(-1,NVar);
00296   ImpNVarY(0,NVar);
00297   CoordX = 0;
00298   CoordY = 0;
00299   ImpInter(NVisMax=NMax);
00300   ImpInterEl(NElMax=NMax);
00301   ImpNVisMax(NVisMax=NMax);
00302   ImpNVisMin(NVisMin=0);
00303   ImpNElMax(NElMax=NMax);
00304   ImpNElMin(NElMin=0);
00305   if(NMax > 10000) Griglia = 0;
00306   sprintf(stringa,"Caricati %d vettori da %d Punti da %s",NVar,NMax,nomeFile);
00307   sprintf(stringa,"%s",v1->PrintHeader());
00308   IfSegnale = 1;
00309   ImpCoordX(CoordX);
00310   ImpCoordY(CoordY);
00311   IfRiscala = RIS_TUTTI;
00312   PrimaVolta = 0;
00313   //for(int i=0;i<NMax;i++) printf("%lf \n",ExtSt[0][i]);
00314   printf("%d %d %d\n",NMax,NVar,NBin);
00315   repaint();
00316 }
00317 void ElementiGrafici::Aggiungi(){
00318   if(PrimaVolta){
00319     ErrPrima->message(QString("Aprire un file .dat prima"));
00320     return;
00321   }
00322   NomeFile(QString(nomeFile));
00323   if( v1->Aggiungi(nomeFile) == 0) return;
00324   NMax = v1->pNMax(); 
00325   PuntiMax = NMax;
00326   // printf("Caricato un vettore da %d Punti da %s",NMax,nomeFile);
00327   sprintf(stringa,"Caricato un vettore da %d Punti da %s",NMax,nomeFile);
00328   sprintf(stringa,"%s",v1->PrintHeader());
00329   NVar = v1->pNVar();
00330   GrLinee = (QColor *)realloc(GrLinee,NVar*sizeof(QColor));
00331   delete [] GrLabel;
00332   GrLabel = new QString[NVar];
00333   //  GrLabel = (QString *)realloc(GrLabel,NVar*sizeof(QString));
00334   LineaCome = (int *)realloc(LineaCome,NVar*sizeof(int));
00335   //LineaQuale = (char **)realloc(LineaQuale,NVar*sizeof(char));
00336   //  for(int c=0;c<NVar;c++)LineaQuale[c] = (char *)realloc(LineaQuale[c],60*sizeof(char));
00337   for(int c=0;c<NVar;c++){
00338     if(c<10)
00339       GrLinee[c] = Colors[c];
00340     else 
00341       GrLinee[c] = Colors[0];
00342     LineaCome[c] = LINEA_TRATTO;
00343   }
00344   ImpNVarX(-1,NVar);
00345   ImpNVarY(0,NVar);
00346   ImpCoordX(CoordX);
00347   ImpCoordY(CoordY);
00348   PuntiSegnale();
00349 }
00350 void ElementiGrafici::mouseMoveEvent(QMouseEvent *Topo){
00351   sprintf(cPos,"%.3g : %.3g",
00352      (double)(Topo->x())/(double)width()*ScalaTopoX+xMin,
00353      (height()-Topo->y())/(double)height()*ScalaTopoY+yMin);
00354   QString Info = QString(cPos);
00355   emit StatoSequenza(QString(cPos));  
00356 }
00357 void ElementiGrafici::DisegnaTesto(QPainter *p){
00358   p->setBrush( Qt::blue );
00359   p->setPen( Qt::blue );
00360   p->drawText(width()/2,height()/2,QString("L'autocorrelazione di un rumore non bianco\n `e lorentziano e decade come \nr^-2"));
00361   p->drawImage(QPoint(width()/2,height()/2),QImage(QString("Prova.png"),0),QRect(0,0,300,200),0);
00362   p->drawRect(QRect(width()/2-100,height()/2-50,200,100));
00363   p->drawText(0,height()/2,QString("L'autocorrelazione di un rumore non bianco\n `e lorentziano e decade come \nr^-2"));
00364 }
00365 QRect ElementiGrafici::PuntiRett(QPoint P) const{
00366   QRect r(0,0,2*PointSize,2*PointSize);
00367   r.moveCenter(P);
00368   return r;  
00369 }
00370 ImpostaNBin::ImpostaNBin(QWidget *parent,const char *name)
00371   :Q3VBox(parent,name){
00372   title = new QLabel(name,this,"title");
00373   title->setAlignment( Qt::AlignCenter );
00374   Qt::Orientation Orientazione = Qt::Horizontal;
00375   //  if(Modo == 1) Orientazione = Qt::Vertical;
00376   slider = new QSlider(Orientazione, this ,"slider");
00377   slider->setRange(0,100);
00378   slider->setValue(0);
00379 
00380     slider->setTickPosition(QSlider::TicksBelow);
00381   //  else slider->setTickPosition(QSlider::TicksRight);
00382     
00383 
00384   label = new QLabel(" ",this,"label");
00385   label->setAlignment( Qt::AlignCenter );
00386   connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)) );
00387   connect(slider,SIGNAL(valueChanged(int)),SIGNAL(ValoreCambiato(int)) );
00388 }
00389 void ImpostaNBin::ImpNumero(int n){
00390   if(n >= 0){
00391     slider->setValue(n);
00392   }
00393 }
00394 void ImpostaNBin::ImpInter(int Max){
00395   if(Max > 0){
00396     slider->setRange(0,Max);
00397   }
00398 }
00399 void ImpostaNBin::ImpInter(int Min,int Max){
00400   if(Min < Max){
00401     slider->setRange(Min,Max);
00402   }
00403 }
00404 int ImpostaNBin::Numero() const{
00405   return slider->value();
00406 }
00407 // Sets::Sets(QWidget *parent,const char *name )
00408 //   :QMenuBar(parent,name){
00409 //   ASet = NULL;
00410 //   ASet = (QAction *)realloc(ASet,NSet*sizeof(QAction)); 
00411 //   char *stringa;
00412 //   stringa = (char *)malloc(100*sizeof(char));
00413 //   for(int v=0;v<NSet;v++){
00414 //     sprintf(stringa,"Set%d",v);
00415 //     //ASet[v] = addAction(QString(stringa));
00416 //   }
00417 //   //connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)) );
00418 //   //connect(slider,SIGNAL(valueChanged(int)),SIGNAL(ValoreCambiato(int)) );
00419 // }
00420 // void Sets::ImpCoord(int Coord){
00421   
00422 // }