00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "gxsm/unit.h"
00030 #include "gxsm/pcs.h"
00031 #include "app_databox.h"
00032
00033 #define DEFAULT_BOXWIDTH 2
00034
00035
00036
00037
00038 DataBox::DataBox(gchar *title, gint ux, gint uy){
00039 cout << "*** DataBox::DataBox Txy *** : Init" << endl;
00040
00041 Init();
00042
00043 Window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
00044 if(ux>0)
00045 gtk_widget_set_usize (Window, ux, uy);
00046 gtk_widget_show (Window);
00047
00048 gtk_signal_connect (GTK_OBJECT (Window), "destroy",
00049 GTK_SIGNAL_FUNC(gtk_widget_destroyed),
00050 &Window);
00051
00052 gtk_window_set_title (GTK_WINDOW (Window), title);
00053 gtk_container_border_width (GTK_CONTAINER (Window), 0);
00054
00055 vbox = gtk_vbox_new (FALSE, 0);
00056 gtk_container_add (GTK_CONTAINER (Window), vbox);
00057 gtk_widget_show (vbox);
00058
00059 hbox_t = gtk_hbox_new (FALSE, 0);
00060 gtk_widget_show (hbox_t);
00061 gtk_box_pack_start (GTK_BOX (vbox), hbox_t, FALSE, FALSE, 0);
00062
00063 Screen = gtk_databox_new();
00064 gtk_signal_connect (GTK_OBJECT (Screen), "destroy",
00065 GTK_SIGNAL_FUNC(gtk_databox_data_destroy_all),
00066 NULL);
00067 gtk_box_pack_start (GTK_BOX (vbox), Screen, TRUE, TRUE, 0);
00068 gtk_widget_show(Screen);
00069 }
00070
00071 DataBox::DataBox(GtkWidget *Vbox){
00072 cout << "*** DataBox::DataBox Vb *** : Init" << endl;
00073
00074 Init();
00075
00076 vbox = Vbox;
00077
00078 hbox_t = gtk_hbox_new (FALSE, 0);
00079 gtk_widget_show (hbox_t);
00080 gtk_box_pack_start (GTK_BOX (vbox), hbox_t, FALSE, FALSE, 0);
00081
00082 Screen = gtk_databox_new();
00083 gtk_signal_connect (GTK_OBJECT (Screen), "destroy",
00084 GTK_SIGNAL_FUNC(gtk_databox_data_destroy_all),
00085 NULL);
00086 gtk_box_pack_start (GTK_BOX (vbox), Screen, TRUE, TRUE, 0);
00087 gtk_widget_show(Screen);
00088 }
00089
00090 void DataBox::Init(){
00091 CpyMode=FALSE;
00092 CbId=0;
00093 delay=200.;
00094 LoopActive=FALSE;
00095 nPoints=0;
00096 CXpoints=Xpoints=NULL;
00097 for(int i=0; i<NUMCHANNELS; ++i) CYpoints[i]=Ypoints[i]=NULL;
00098 BoxType=GTK_DATABOX_LINES;
00099 BoxWidth=DEFAULT_BOXWIDTH;
00100 Window=NULL; Screen=NULL;
00101 label_t = label_x = label_y = NULL;
00102 }
00103
00104 DataBox::~DataBox(){
00105 stop();
00106 removeData ();
00107 if(Window)
00108 gtk_widget_destroy(Window);
00109 }
00110
00111 void DataBox::SetHeading(gchar *title, gchar *tx, gchar *ty){
00112 if(label_t && title)
00113 gtk_label_set_text( GTK_LABEL (label_t), title );
00114 else
00115 if(title){
00116 label_t = gtk_label_new( title );
00117 gtk_widget_show (label_t);
00118 gtk_box_pack_start (GTK_BOX (hbox_t), label_t, TRUE, TRUE, 0);
00119 }
00120
00121 if(label_x && tx)
00122 gtk_label_set_text( GTK_LABEL (label_x), tx );
00123 else
00124 if(tx){
00125 label_x = gtk_label_new( tx );
00126 gtk_widget_show (label_x);
00127 gtk_box_pack_start (GTK_BOX (hbox_t), label_x, TRUE, TRUE, 0);
00128 }
00129
00130 if(label_y && ty)
00131 gtk_label_set_text( GTK_LABEL (label_y), ty );
00132 else
00133 if(ty){
00134 label_y = gtk_label_new( ty );
00135 gtk_widget_show (label_y);
00136 gtk_box_pack_start (GTK_BOX (hbox_t), label_y, TRUE, TRUE, 0);
00137 }
00138 }
00139
00140 void DataBox::setSetType ( gint idx, GtkDataboxDataType typ, gint dotsize){
00141 if(Ypoints[idx])
00142 gtk_databox_data_set_type(GTK_DATABOX(Screen), idx,
00143 typ>=0 ? typ:BoxType,
00144 dotsize>=0 ? dotsize:BoxWidth);
00145 }
00146
00147 void DataBox::setSetColor ( gint idx, GdkColor color ){
00148 if(Ypoints[idx])
00149 gtk_databox_data_set_color(GTK_DATABOX(Screen), idx, color );
00150 }
00151
00152 void DataBox::removeData (){
00153 gtk_databox_data_remove_all(GTK_DATABOX(Screen));
00154 if(!CpyMode){
00155 Xpoints=NULL;
00156 for(int i=0; i<NUMCHANNELS; ++i) Ypoints[i]=NULL;
00157 }else{
00158 if(Xpoints)
00159 delete Xpoints;
00160 Xpoints=NULL;
00161 for(int i=0; i<NUMCHANNELS; ++i)
00162 if(Ypoints[i]){
00163 delete Ypoints[i];
00164 Ypoints[i]=NULL;
00165 }
00166 }
00167 }
00168
00169 void DataBox::setxyData (PLOTTYP *SetX, PLOTTYP *SetY, int n, int cpy){
00170 PLOTTYP *y[NUMCHANNELS];
00171 y[0]=SetY;
00172 for(int i=1; i<NUMCHANNELS; ++i) y[i]=NULL;
00173 setxyData(SetX, y, n, cpy);
00174 }
00175
00176 void DataBox::setxyData (PLOTTYP *SetX, PLOTTYP *SetY[NUMCHANNELS], int n, int cpy){
00177 GdkColor color[NUMCHANNELS];
00178 int j;
00179
00180 removeData ();
00181
00182 nPoints=n;
00183
00184 for(j=0; j<NUMCHANNELS; j++){
00185 color[j].red = (j+1)&1 ? 65535:0;
00186 color[j].green = (j+1)&4 ? 65535:0;
00187 color[j].blue = (j+1)&2 ? 65535:0;
00188 if(SetY[j]){
00189 if(cpy){
00190 Ypoints[j] = new PLOTTYP[nPoints];
00191 memcpy(Ypoints[j], SetY[j], nPoints*sizeof(PLOTTYP));
00192 if(!Xpoints){
00193 Xpoints = new PLOTTYP[nPoints];
00194 memcpy(Xpoints, SetX, nPoints*sizeof(PLOTTYP));
00195 gtk_databox_data_add_x_y(GTK_DATABOX(Screen), nPoints,
00196 Xpoints, Ypoints[j], color[j],
00197 BoxType, BoxWidth);
00198 }else
00199 gtk_databox_data_add_y(GTK_DATABOX(Screen), nPoints,
00200 Ypoints[j], 0, color[j],
00201 BoxType, BoxWidth);
00202 }else{
00203 Ypoints[j] = SetY[j];
00204 if(!Xpoints){
00205 Xpoints = SetX;
00206 gtk_databox_data_add_x_y(GTK_DATABOX(Screen), nPoints,
00207 Xpoints, Ypoints[j], color[j],
00208 BoxType, BoxWidth);
00209 }else
00210 gtk_databox_data_add_y(GTK_DATABOX(Screen), nPoints,
00211 Ypoints[j], 0, color[j],
00212 BoxType, BoxWidth);
00213 }
00214 }
00215 }
00216 CpyMode=cpy;
00217 }
00218
00219 void DataBox::updatexyData (PLOTTYP *SetX, PLOTTYP *SetY){
00220 if(Xpoints && SetX)
00221 memcpy(Xpoints, SetX, nPoints*sizeof(PLOTTYP));
00222 if(SetY && Ypoints[0])
00223 memcpy(Ypoints[0], SetY, nPoints*sizeof(PLOTTYP));
00224 }
00225
00226 void DataBox::updatexyData (PLOTTYP *SetX, PLOTTYP *SetY[NUMCHANNELS]){
00227 if(Xpoints && SetX)
00228 memcpy(Xpoints, SetX, nPoints*sizeof(PLOTTYP));
00229 for(int j=0; j<NUMCHANNELS; j++)
00230 if(SetY[j] && Ypoints[j])
00231 memcpy(Ypoints[j], SetY[j], nPoints*sizeof(PLOTTYP));
00232 }
00233
00234 void DataBox::print(gchar *name){
00235 }
00236
00237 void DataBox::load(gchar *name){
00238 gchar line[256];
00239 int np=0, nch=0;
00240 int ii;
00241 ifstream in;
00242 in.open(name, ios::in);
00243 do{
00244 in.getline(line, 256);
00245
00246 if(! strncmp(line, "# nPoints:", 10))
00247 np = atoi(line+11);
00248 else if(! strncmp(line, "# nChannels:", 12))
00249 nch = atoi(line+13);
00250 else checkheader(line);
00251 }while(strcmp(line, "# DataStart"));
00252
00253
00254 if(np && nch){
00255 if(CXpoints)
00256 delete CXpoints;
00257 CXpoints=NULL;
00258 for(int i=0; i<NUMCHANNELS; ++i)
00259 if(CYpoints[i]){
00260 delete CYpoints[i];
00261 CYpoints[i]=NULL;
00262 }
00263 CnPoints=np;
00264 CXpoints = new PLOTTYP[CnPoints];
00265 for(int i=0; i<nch; ++i)
00266 CYpoints[i] = new PLOTTYP[CnPoints];
00267
00268 for(int i=0; i<CnPoints; i++){
00269 in >> ii >> CXpoints[i];
00270 for(int j=0; j<nch; j++)
00271 in >> CYpoints[j][i];
00272 }
00273 setxyData (CXpoints, CYpoints, np, TRUE);
00274 draw(TRUE);
00275 if(CXpoints)
00276 delete CXpoints;
00277 CXpoints=NULL;
00278 for(int i=0; i<NUMCHANNELS; ++i)
00279 if(CYpoints[i]){
00280 delete CYpoints[i];
00281 CYpoints[i]=NULL;
00282 }
00283 CnPoints=0;
00284 }
00285 in.close();
00286 }
00287
00288 void DataBox::save(gchar *name){
00289 int nch;
00290 char hn[256];
00291 time_t t;
00292 ofstream out;
00293 gethostname(hn, 256);
00294 t=time(NULL);
00295
00296 for(nch=0; nch<NUMCHANNELS; nch++)
00297 if(!Ypoints[nch])
00298 break;
00299
00300 out.open(name, ios::out);
00301
00302 out << "# goszi: DataAq mode" << endl;
00303 out << "# date: " << ctime(&t);
00304 out << "# user: " << getlogin() << "@" << hn << endl;
00305 out << "# nPoints: " << nPoints << endl;
00306 out << "# nChannels: " << nch << endl;
00307 out << "# --- Options Start ---" << endl;
00308 addheader(out);
00309 out << "# --- Options End ---" << endl;
00310 out << "# Format: index x y1 y2 ... y" << nch << endl;
00311 out << "# DataStart" << endl;
00312
00313 for(int i=0; i<nPoints; i++){
00314 out << i << " " << setprecision(Xprec()) << Xpoints[i];
00315 for(int j=0; j<nch; j++)
00316 if(Ypoints[j])
00317 out << " " << setprecision(Yprec(j)) << Ypoints[j][i];
00318 out << endl;
00319 }
00320 out.close();
00321 }
00322
00323 void DataBox::cut(){
00324 int i1, i2;
00325 GtkDataboxValue min, max;
00326 gtk_databox_data_get_visible_extrema(GTK_DATABOX(Screen), &min, &max);
00327 for(i1= 0; i1<nPoints && Xpoints[i1]<min.x; ++i1);
00328 for(i2=i1; i2<nPoints && Xpoints[i2]<max.x; ++i2);
00329
00330 }
00331
00332 void DataBox::copy(){
00333 int i1, i2;
00334 GtkDataboxValue min, max;
00335 if(!Xpoints) return;
00336 if(CXpoints)
00337 delete CXpoints;
00338 CXpoints=NULL;
00339 for(int i=0; i<NUMCHANNELS; ++i)
00340 if(CYpoints[i]){
00341 delete CYpoints[i];
00342 CYpoints[i]=NULL;
00343 }
00344 gtk_databox_data_get_visible_extrema(GTK_DATABOX(Screen), &min, &max);
00345 for(i1= 0; i1<nPoints && Xpoints[i1]<min.x; ++i1);
00346 for(i2=i1; i2<nPoints && Xpoints[i2]<max.x; ++i2);
00347 CnPoints = i2-i1+1;
00348 CXpoints = new PLOTTYP[CnPoints];
00349 memcpy(CXpoints, Xpoints+i1, sizeof(CXpoints));
00350 for(int i=0; i<NUMCHANNELS; ++i)
00351 if(Ypoints[i]){
00352 CYpoints[i] = new PLOTTYP[CnPoints];
00353 memcpy(CYpoints[i], Ypoints[i]+i1, sizeof(CYpoints[i]));
00354 }
00355 }
00356
00357 void DataBox::GetVisible( GtkDataboxValue *vmin, GtkDataboxValue *vmax){
00358 gtk_databox_data_get_visible_extrema(GTK_DATABOX(Screen), vmin, vmax);
00359 }
00360
00361 void DataBox::paste(){
00362 int i1;
00363 GtkDataboxValue min, max;
00364 gtk_databox_data_get_visible_extrema(GTK_DATABOX(Screen), &min, &max);
00365 for(i1= 0; i1<nPoints && Xpoints[i1]<min.x; ++i1);
00366
00367 }
00368
00369 void DataBox::run(){
00370
00371 if(atrun())
00372 if(!CbId)
00373 CbId = gtk_timeout_add((int)delay, (GtkFunction) loop, this);
00374 }
00375 void DataBox::stop(){
00376
00377 if(CbId)
00378 gtk_timeout_remove(CbId);
00379 CbId=0;
00380 atstop();
00381 }
00382
00383 void DataBox::draw(int autoskl, GtkDataboxValue *vmin, GtkDataboxValue *vmax){
00384 if(autoskl)
00385 gtk_databox_rescale(GTK_DATABOX(Screen));
00386 else
00387 if(vmin && vmax)
00388 gtk_databox_rescale_with_values(GTK_DATABOX(Screen), *vmin, *vmax);
00389
00390 gtk_databox_redraw(GTK_DATABOX(Screen));
00391 }
00392
00393 gint DataBox::loop(DataBox *db){
00394 if( db->LoopActive ) return FALSE;
00395 db->LoopActive=TRUE;
00396
00397 if(db->CbId){
00398 gtk_timeout_remove(db->CbId);
00399 db->CbId=0;
00400 }
00401 db->getdata();
00402
00403 db->CbId = gtk_timeout_add((int)db->delay, (GtkFunction) DataBox::loop, db);
00404
00405 db->LoopActive=FALSE;
00406 return TRUE;
00407 }
00408
00409 void DataBox::make_copy(){
00410 PLOTTYP *cx, *cy[NUMCHANNELS];
00411 int n;
00412
00413 if(Xpoints && !CpyMode){
00414 cx = Xpoints;
00415 n = nPoints;
00416 for(int i=0; i<NUMCHANNELS; ++i)
00417 cy[i] = Ypoints[i];
00418 setxyData(cx, cy, n, TRUE);
00419 }
00420 }
00421