Creating Classes, Libraries and Functions for the Common SLS Detector GUI

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorGui@1 af1100a4-978c-4157-bff7-07162d2ba061
This commit is contained in:
maliakal_d
2012-05-21 16:00:26 +00:00
commit 9f851d6156
49 changed files with 6522 additions and 0 deletions

View File

@ -0,0 +1,145 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT1DPLOT_H
#define SLSQT1DPLOT_H
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include "SlsQt1DZoomer.h"
class QPen;
class SlsQt1DPlot;
class SlsQtH1D:public QwtPlotCurve{
public:
SlsQtH1D(QString title, int n, double xmin, double xmax, double* data=0);
SlsQtH1D(QString title, int n, double* data_x, double* data_y);
~SlsQtH1D();
void Attach(SlsQt1DPlot* p);
void Detach(SlsQt1DPlot* p);
int SetLineColor(int c=-1);
int SetLineWidth(int w=1);
void SetLineStyle(int s=0);
void SetData(int n, double xmin, double xmax, double* d=0);
void SetData(int n, double* dx, double* dy);
double* GetX() {return x;}
double* GetY() {return y;}
int GetNBinsX() {return ndata;}
double FillBin(int bx, double v=1);
double Fill(double x, double v=1);
double SetBinContent(int bx,double v);
double SetContent(double x,double v);
int FindBinIndex(double px);
double GetXMin() {return x[0];}
double GetFirstXgtZero() {return firstXgt0;}
double GetXMax() {return x[ndata-1];}
double GetYMin() {return ymin;}
double GetFirstYgtZero() {return firstYgt0;}
double GetYMax() {return ymax;}
SlsQtH1D* Add(double v);
private:
int ndata;
int n_array;
double dx;
double *x,*y;
double ymin,ymax;
double firstXgt0,firstYgt0;
void Initailize();
int SetUpArrays(int n);
int CheckIndex(int bx);
QPen* pen_ptr;
};
class SlsQtH1DList{
public:
SlsQtH1DList(SlsQtH1D* hist=0);
~SlsQtH1DList();
SlsQtH1D* Add(SlsQtH1D* h);
void Remove(SlsQtH1D* h);
void Print();
SlsQtH1D* Hist() {return the_hist;} //if no hist returns 0
SlsQtH1DList* Next() {return the_next;}
private:
SlsQtH1DList* the_next;
SlsQtH1D* the_hist;
};
class SlsQt1DPlot:public QwtPlot{
Q_OBJECT
public:
SlsQt1DPlot(QWidget* = NULL);
~SlsQt1DPlot();
void SetTitle(const char *t);
void SetXTitle(const char* title);
void SetYTitle(const char* title);
void InsertHLine(double y);
void RemoveHLine();
void InsertVLine(double v);
void RemoveVLine();
void SetZoom(double xmin,double ymin,double x_width,double y_width);
void SetZoomBase(double xmin,double ymin,double x_width, double y_width){ zoomer->SetZoomBase(xmin,ymin,x_width,y_width);}
private:
SlsQtH1DList* hist_list;
SlsQt1DZoomer* zoomer;
QwtPlotPanner* panner;
QwtPlotMarker *hline;
QwtPlotMarker *vline;
void SetupZoom();
void UnknownStuff();
void alignScales();
void CalculateNResetZoomBase();
void NewHistogramAttached(SlsQtH1D* h);
void HistogramDetached(SlsQtH1D* h);
void SetLog(int axisId, bool yes);
friend void SlsQtH1D::Attach(SlsQt1DPlot* p);
friend void SlsQtH1D::Detach(SlsQt1DPlot* p);
public slots:
void UnZoom();
void Update();
void SetLogX(bool yes=1);
void SetLogY(bool yes=1);
protected:
};
#endif

View File

@ -0,0 +1,59 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT1DZOOMER_H
#define SLSQT1DZOOMER_H
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
class SlsQtH1D;
class SlsQt1DZoomer:public QwtPlotZoomer{
private:
double x0,x1,y0,y1;
double firstXgt0,firstYgt0;
bool xIsLog,yIsLog;
public:
SlsQt1DZoomer(QwtPlotCanvas *canvas):QwtPlotZoomer(canvas){
setTrackerMode(AlwaysOn);
xIsLog=yIsLog=0;
}
double x() {return x0;}
double x_firstGreaterThan0() {return firstXgt0;}
double w() {return x1-x0;}
double y() {return y0;}
double y_firstGreaterThan0() {return firstYgt0;}
double h() {return y1-y0;}
void SetZoomBase(double xmin,double ymin,double x_width, double y_width);
void SetZoomBase(SlsQtH1D* h);
void ExtendZoomBase(SlsQtH1D* h);
void ResetZoomBase();
bool IsLogX(){ return xIsLog;}
bool IsLogY(){ return yIsLog;}
bool SetLogX(bool yes) { return xIsLog=yes;}
bool SetLogY(bool yes) { return yIsLog=yes;}
virtual QwtText trackerText(const QwtDoublePoint &pos) const{
QColor bg(Qt::white);
#if QT_VERSION >= 0x040300
bg.setAlpha(200);
#endif
QwtText text = QwtPlotZoomer::trackerText(pos);
text.setBackgroundBrush( QBrush( bg ));
return text;
}
};
#endif

View File

@ -0,0 +1,113 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT2DHIST_H
#define SLSQT2DHIST_H
#if QT_VERSION >= 0x040000
#include <qprintdialog.h>
#endif
#include <qwt_color_map.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
class SlsQt2DHist: public QwtRasterData{
private:
double x_min,x_max,y_min,y_max;
double x_width,y_width;
int nx,ny,nb;
double *data;
double z_min,z_mean,z_max;
bool z_mean_has_been_calculated;
int nx_array,ny_array;
bool interp;
static double value_between_points(double p1,double v1,double p2,double v2,double p){ //linear extrap
return (v2-v1)/(p2-p1)*(p-p1)+v1;
}
public:
SlsQt2DHist(int nbinsx=10, double xmin=0, double xmax=10, int nbinsy=10, double ymin=0, double ymax=10, double* d=0,double zmin=0,double zmax=-1);
virtual ~SlsQt2DHist();
double GetXMin() {return x_min;}
double GetXMax() {return x_max;}
double GetXBinWidth() {return x_width;}
double GetYMin() {return y_min;}
double GetYMax() {return y_max;}
double GetYBinWidth() {return y_width;}
double GetMinimum() {return z_min;}
double GetMaximum() {return z_max;}
double GetMean();
int GetNBinsX(){return nx;}
int GetNBinsY(){return ny;}
double GetBinValue(int bx,int by);
int GetBinIndex(int bx,int by);
double* GetDataPtr(){return data;}
void Interpolate(bool on=1) {interp=on;}
void SetBinValue(int bx,int by,double v);
void SetData(int nbinsx, double xmin, double xmax, int nbinsy,double ymin, double ymax,double *d,double zmin=0, double zmax=-1);
double SetMinimumToFirstGreaterThanZero();
void SetMinimum(double zmin) {z_min=zmin;}
void SetMaximum(double zmax) {z_max=zmax;}
void SetMinMax(double zmin=0,double zmax=-1);
int FindBinIndex(double x, double y);
virtual QwtRasterData *copy() const{
//this function does not create a new SlsQt2DHistData instance,
//just passes a pointer so that data is common to both the copy and the original instance
return (QwtRasterData*) this;
}
virtual QwtDoubleInterval range() const{ return QwtDoubleInterval(z_min,z_max);}
virtual double value(double x, double y) const{
//if(!interp){ //default is box like plot
int index = int((x-x_min)/x_width) + int((y-y_min)/y_width)*nx;
if(index<0||index>nb) index = nb;
if(!interp) return data[index];
//}
int x_int = int((x-x_min)/x_width-0.5);
if(x_int<0) x_int = 0; else if(x_int>nx-2) x_int = nx-2;
int y_int = int((y-y_min)/y_width-0.5);
if(y_int<0) y_int = 0; else if(y_int>ny-2) y_int = ny-2;
int b00 = x_int*ny + y_int;
int b01 = x_int*ny + y_int+1;
int b10 = (x_int+1)*ny + y_int;
int b11 = (x_int+1)*ny + y_int+1;
//vertical extrap
double y0 = y_min+(y_int+0.5)*y_width;
double y1 = y_min+(y_int+1.5)*y_width;
double left_v = value_between_points(y0,data[b00],y1,data[b01],y);
double right_v = value_between_points(y0,data[b10],y1,data[b11],y);
//horazontal extrap
return value_between_points(x_min+(x_int+0.5)*x_width,left_v,
x_min+(x_int+1.5)*x_width,right_v,x);
}
};
#endif

View File

@ -0,0 +1,79 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT2DPLOT_H
#define SLSQT2DPLOT_H
#include <qwt_plot.h>
#include <qwt_plot_spectrogram.h>
#include "SlsQt2DZoomer.h"
#include "SlsQt2DHist.h"
class QwtPlotPanner;
class QwtScaleWidget;
class QwtLinearColorMap;
class SlsQt2DPlot: public QwtPlot{
Q_OBJECT
private:
QwtPlotSpectrogram *d_spectrogram;
SlsQt2DHist* hist;
SlsQt2DZoomer* zoomer;
QwtPlotPanner* panner;
QwtScaleWidget *rightAxis;
QwtLinearColorMap* colorMapLinearScale;
QwtLinearColorMap* colorMapLogScale;
QwtValueList* contourLevelsLinear;
QwtValueList* contourLevelsLog;
void SetupZoom();
void SetupColorMap();
public:
SlsQt2DPlot(QWidget * = NULL);
// SlsQt2DHist *GetHistogram(){ return hist; }
void UnZoom();
void SetZoom(double xmin,double ymin,double x_width,double y_width);
double GetZMinimum(){ return hist->GetMinimum();}
double GetZMaximum(){ return hist->GetMaximum();}
void SetZMinMax(double zmin=0,double zmax=-1);
void SetZMinimumToFirstGreaterThanZero(){hist->SetMinimumToFirstGreaterThanZero();}
double GetZMean() { return hist->GetMean();}
void SetData(int nbinsx, double xmin, double xmax, int nbinsy,double ymin, double ymax,double *d,double zmin=0, double zmax=-1){
hist->SetData(nbinsx,xmin,xmax,nbinsy,ymin,ymax,d,zmin,zmax);
}
double* GetDataPtr() {return hist->GetDataPtr();}
int GetBinIndex(int bx,int by) {return hist->GetBinIndex(bx,by);}
int FindBinIndex(double x,double y) {return hist->FindBinIndex(x,y);}
void SetBinValue(int bx,int by,double v) {hist->SetBinValue(bx,by,v);}
double GetBinValue(int bx,int by) {return hist->GetBinValue(bx,by);}
void FillTestPlot(int i=0);
void Update();
public slots:
void LogZ(bool on=1);
void InterpolatedPlot(bool on);
void showContour(bool on);
void showSpectrogram(bool on);
// void printPlot();
};
#endif

View File

@ -0,0 +1,112 @@
/**
* @author Ian Johnson
* @version 1.0
*/
/*
#ifndef SLSQT2DPLOTLAYOUT_H
#define SLSQT2DPLOTLAYOUT_H
#include <qwidget.h>
#include <qgroupbox.h>
#include <QString>
#include "SlsQtNumberEntry.h"
#include "SlsQt2DPlot.h"
//class QGridLayout;
//class QToolbar;
class SlsQt2DPlotLayout: public QGroupBox{
Q_OBJECT
public:
SlsQt2DPlotLayout(QWidget * = NULL);
~SlsQt2DPlotLayout();
SlsQt2DPlot* GetPlot() {return the_plot;}
void SetXTitle(QString st);
void SetYTitle(QString st);
void SetZTitle(QString st);
void UpdateNKeepSetRangeIfSet();
private:
//QGridLayout* the_layout;
SlsQt2DPlot* the_plot;
//SlsQtNumberEntry* z_range_ne;
void ConnectSignalsAndSlots();
public slots:
void SetZScaleToLog(bool yes);
void ResetRange();
};
#endif
*/
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT2DPLOTLAYOUT_H
#define SLSQT2DPLOTLAYOUT_H
#include <qwidget.h>
#include <qgroupbox.h>
#include "SlsQtNumberEntry.h"
#include "SlsQt2DPlot.h"
class QGridLayout;
class SlsQt2DPlotLayout: public QGroupBox{
Q_OBJECT
public:
SlsQt2DPlotLayout(QWidget * = NULL);
~SlsQt2DPlotLayout();
SlsQt2DPlot* GetPlot() {return the_plot;}
void SetXTitle(QString st);
void SetYTitle(QString st);
void SetZTitle(QString st);
void UpdateNKeepSetRangeIfSet();
private:
QGridLayout* the_layout;
SlsQt2DPlot* the_plot;
SlsQtNumberEntry* z_range_ne;
bool logsChecked;
void ConnectSignalsAndSlots();
void Layout();
public slots:
void SetZScaleToLog(bool yes);
void ResetRange();
signals:
void InterpolateSignal(bool);
void ContourSignal(bool);
};
#endif

View File

@ -0,0 +1,51 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQT2DZOOMER_H
#define SLSQT2DZOOMER_H
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include "SlsQt2DHist.h"
class SlsQt2DZoomer:public QwtPlotZoomer{
private:
SlsQt2DHist* hist;
public:
SlsQt2DZoomer(QwtPlotCanvas *canvas):QwtPlotZoomer(canvas){
setTrackerMode(AlwaysOn);
}
void SetHist(SlsQt2DHist* h){
hist=h;
}
virtual QwtText trackerText(const QwtDoublePoint &pos) const{
QColor bg(Qt::white);
#if QT_VERSION >= 0x040300
bg.setAlpha(200);
#endif
//QwtText text = QwtPlotZoomer::trackerText(pos);
static QwtText text;
if(hist){
static char t[200];
sprintf(t,"%3.2f, %3.2f, %3.2f",pos.x(),pos.y(),hist->value(pos.x(),pos.y()));
text.setText(t);
}else text = QwtPlotZoomer::trackerText(pos);
text.setBackgroundBrush( QBrush( bg ));
return text;
}
};
#endif

View File

@ -0,0 +1,140 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQTNUMBERENTRY_H
#define SLSQTNUMBERENTRY_H
#include <qwidget.h>
#include <qgroupbox.h>
class QGridLayout;
class QLabel;
class QLineEdit;
class QIntValidator;
class QDoubleValidator;
class QCheckBox;
class QComboBox;
class QSpinBox;
#include "SlsQtValidators.h"
class SlsQtNumberEntry:public QWidget{
Q_OBJECT
public:
//type=0->units only, type=1->int,type=2->double, type=3->spinbox
//adding middle text will automatically add a second number field
SlsQtNumberEntry(QWidget *parent,int with_checkbox=0, char *start_string=0, int num_type=0, char *middle_string=0, int num2_type=0, int n_units=0, char** units=0, double* unit_factors=0, char* end_string=0);
//without unit box
SlsQtNumberEntry(QWidget *parent,int with_checkbox, char *start_strin, int num_type, char *middle_string, int num2_type, char* end_string);
~SlsQtNumberEntry();
void Enable(bool en_flag=1);
void Disable();
void EnableNumberField(int which_number_field,bool en_flag=1); //which_number_field is 0 or 1
void DisableNumberField(int which_number_field);
void AddCheckBox();
void SetFrontText(char* s);
void SetMiddleText(char* s);
void SetBackText(char* s);
void SetupNumberField(int type,int which_number_field=0);
void SetUnits(int n_units,char** units,double* unit_factors);
void SetMinimumNumberWidth(int nchar_width,int which_number_field=0);
void SetNDecimalsOfDoubleValidators(int ndecimals);
void SetNDecimalsOfDoubleValidator(int ndecimals,int which_number_field=0);
void SetMinimumUnitWidth(int nchar_width);
bool Enabled();
bool CheckBoxState();
bool IsValueOk(int which_number_field=0);
const char* GetFrontText();
const char* GetMiddleText();
const char* GetBackText();
int GetNumberInt(int which_number_field=0,bool* ok=0);
double GetNumber(int which_number_field=0,bool *ok=0);
int GetValueInt(int which_number_field=0,bool* ok=0);
double GetValue(int which_number_field=0,bool *ok=0);
int GetComboBoxIndex();
double GetComboBoxValue();
private:
void SetupNumberEntry(int with_checkbox=0, char *start_string=0, int num_type=0, char *middle_string=0, int num2_type=0, int n_units=0, char** units=0, double* unit_factors=0, char* end_string=0);
QGridLayout* layout; //default layout
QCheckBox* check_box;
QLabel* front_text;
QLabel* middle_text;
QLineEdit* num_field[2];
QSpinBox* spin_box[2];
bool num_field_enabled[2];
SlsQtIntValidator* validator_int[2];
SlsQtDoubleValidator* validator_double[2];
QComboBox* unit_cbb;
double* factors;
QLabel* back_text;
void SetText(char* s, QLabel** pp);
void SetLayout();
public slots:
void FirstValueEntered();
void SecondValueEntered();
void UnitSelected();
void SetRange(int min, int max, int which_number_field=0);
void SetRange(double min, double max, int which_number_field=0);
void SetFirstRange(int min, int max) {SetRange(min,max,0);}
void SetSecondRange(int min, int max) {SetRange(min,max,1);}
double SetValue(double v, int which_number_field=0);
void SetFirstValue(int v) {SetValue(v,0);}
void SetSecondValue(int v) {SetValue(v,1);}
void SetFirstValue(double v) {SetValue(v,0);}
void SetSecondValue(double v) {SetValue(v,1);}
// double SetNumber(int v, int which_number_field=0);
double SetNumber(double v, int which_number_field=0);
void SetFirstNumber(int v) {SetNumber(v,0);}
void SetSecondNumber(int v) {SetNumber(v,1);}
void SetFirstNumber(double v) {SetNumber(v,0);}
void SetSecondNumber(double v) {SetNumber(v,1);}
int SetComboBoxIndex(int index);
void CheckBoxClicked();
void PrintTheValue();
void RefreshFirstNumberEntry();
void RefreshSecondNumberEntry();
void RefreshNumberEntery(int number_field=0);
signals:
void CheckBoxChanged(bool state);
void CheckBoxChanged(SlsQtNumberEntry* ptr);
void AValueChanged(SlsQtNumberEntry* ptr);
void FirstValueChanged(int value);
void FirstValueChanged(double value);
void FirstValueChanged(SlsQtNumberEntry* ptr);
void SecondValueChanged(int value);
void SecondValueChanged(double value);
void SecondValueChanged(SlsQtNumberEntry* ptr);
void UnitChanged(double);
void UnitChanged(SlsQtNumberEntry* ptr);
};
#endif

View File

@ -0,0 +1,73 @@
/**
* @author Ian Johnson
* @version 1.0
*/
#ifndef SLSQTVALIDATORS_H
#define SLSQTVALIDATORS_H
#include <iostream>
#include <math.h>
#include <qwidget.h>
#include <qvalidator.h>
using std::cout;
using std::endl;
class SlsQtIntValidator:public QIntValidator{
public:
SlsQtIntValidator(QWidget *parent):QIntValidator(parent){}
virtual void fixup (QString& text) const {
bool ok = 1;
int v = text.toInt(&ok);
if(!ok){
v = text.toDouble(&ok);
if(ok) text = QString::number(v);
else text = QString::number(0);
fixup(text);
}
if(v<bottom()) text = QString::number(bottom());
else if(v>top()) text = QString::number(top());
}
};
class SlsQtDoubleValidator:public QDoubleValidator{
public:
SlsQtDoubleValidator(QWidget *parent):QDoubleValidator(parent){}
virtual void fixup (QString& text) const {
bool ok = 1;
double v = text.toDouble(&ok);
if(!ok){
text = QString::number(0);
fixup(text);
}
int nd = this->decimals(); //ndigest behind zero
if(v<bottom()){
text = QString::number(bottom(),'g',nd);
}else{
if(nd<0) nd=0;
if(v>top()){
v = floor(top()*pow(10,nd))/pow(10,nd);
text = QString::number(v,'g');
}else{
v = round(v*pow(10,nd))/pow(10,nd);
text = QString::number(v,'g');
}
}
}
};
#endif