File: plot.cpp

package info (click to toggle)
odin 2.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,196 kB
  • sloc: cpp: 62,638; sh: 4,541; makefile: 779
file content (708 lines) | stat: -rw-r--r-- 20,403 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include <qevent.h> // for mouse events

#include "plot.h"


#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_layout.h>
#include <qwt_wheel.h>
#if QWT_VERSION < 0x060000
#include <qwt_double_rect.h>
#else
#include <qwt_picker_machine.h>
#include <qwt_plot_renderer.h>
#endif

#if QWT_VERSION > 0x04FFFF
#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_grid.h>
#include <qwt_symbol.h>
#include <qwt_scale_div.h>
#include <qwt_scale_draw.h>
#else
#include <qwt_scldraw.h>
#endif



#if QWT_VERSION < 0x060000

class GuiPlotPrintFilter : public QwtPlotPrintFilter {

 public:
  GuiPlotPrintFilter(long baseline_id) : baseline_id_cache(baseline_id) {}

 private:
  long baseline_id_cache;

  // overloading virtual functions from QwtPlotPrintFilter
#if QWT_VERSION > 0x04FFFF
  QColor color(const QColor &c, Item item) const {
    QColor result=QwtPlotPrintFilter::color(c,item); // default
#else
  QColor color(const QColor &c, Item item, int id = -1) const {
    QColor result=QwtPlotPrintFilter::color(c,item,id); // default
#endif
    if(item==QwtPlotPrintFilter::Curve)            result=QColor("Black");
    if(item==QwtPlotPrintFilter::CurveSymbol)      result=QColor("Black");
    if(item==QwtPlotPrintFilter::Marker)           result=QColor("Black");
    if(item==QwtPlotPrintFilter::MajorGrid)        result=QColor("Gray").light(130);
    if(item==QwtPlotPrintFilter::MinorGrid)        result=QColor("Gray").light(150);
#if QWT_VERSION > 041
    if(item==QwtPlotPrintFilter::CanvasBackground)       result=QColor("White");
#else
    if(item==QwtPlotPrintFilter::Background)             result=QColor("White");
#endif
#if QWT_VERSION < 0x04FFFF
    if(item==QwtPlotPrintFilter::Curve && id==baseline_id_cache) result=QColor("Gray").dark(150);
#endif
    return result;
  }


#if QWT_VERSION > 0x04FFFF
  QFont font(const QFont &, Item item) const {
#else
  QFont font(const QFont &, Item item, int id = -1) const {
#endif
    return QFont(_FONT_TYPE_, 7);
  }

};

#endif // QWT_VERSION < 0x060000



////////////////////////////////////////////////////////////////////////////

struct GuiScaleDraw : QwtScaleDraw {

  // implementing virtual functions of QwtScaleDraw
#if QWT_VERSION < 0x04FFFF
  QString label (double val) const {return get_str(val);}
#else
  QwtText label (double val) const {return QString(get_str(val));}
#endif

 private:
  const char* get_str(double val) const {
    bool vertical=false;

#if QWT_VERSION < 0x04FFFF
    QwtScaleDraw::Orientation ort=orientation();
    if(ort==QwtScaleDraw::Left || ort==QwtScaleDraw::Right) vertical=true;
#else
    QwtScaleDraw::Alignment ort=alignment();
    if(ort==QwtScaleDraw::LeftScale || ort==QwtScaleDraw::RightScale) vertical=true;
#endif

    if(vertical) result=ftos(val,2,alwaysExp);
    else         result=ftos(val,2,neverExp);

    return result.c_str();
  }

  mutable STD_string result;
};

////////////////////////////////////////////////////////////////////////////

class GuiPlotPicker : public QwtPlotPicker {

 public:
  GuiPlotPicker(QwtPlotCanvas* canvas, GuiPlot* plot4feedback) : QwtPlotPicker(canvas), plot(plot4feedback) {}


 protected:
  void widgetMousePressEvent(QMouseEvent* qme) {
    Log<OdinQt> odinlog("GuiPlotPicker","widgetMousePressEvent");
    QwtPlotPicker::widgetMousePressEvent(qme);
    plot->emit_plotMousePressed(*qme);
    ODINLOG(odinlog,normalDebug) << "widgetMousePressEvent" << STD_endl;
  }

  void widgetMouseReleaseEvent(QMouseEvent* qme) {
    Log<OdinQt> odinlog("GuiPlotPicker","widgetMouseReleaseEvent");
    QwtPlotPicker::widgetMouseReleaseEvent(qme);
    plot->emit_plotMouseReleased(*qme);
    ODINLOG(odinlog,normalDebug) << "widgetMouseReleaseEvent" << STD_endl;
  }

  void widgetMouseMoveEvent(QMouseEvent* qme) {
    QwtPlotPicker::widgetMouseMoveEvent(qme);
    plot->emit_plotMouseMoved(*qme);
  }

 private:
  GuiPlot* plot;
};


////////////////////////////////////////////////////////////////////////////


GuiPlot::GuiPlot(QWidget *parent, bool fixed_size, int width, int height) {
  Log<OdinQt> odinlog("GuiPlot","GuiPlot(...)");

  baseline_id_cache=0;

  qwtplotter = new QwtPlot(parent);
  if(fixed_size) qwtplotter->setFixedSize  (width,height);
  else           qwtplotter->setMinimumSize(width,height);
  qwtplotter->resize(width,height);

  qwtplotter->plotLayout()->setAlignCanvasToScales(true);

  // to get mouse-move events
  qwtplotter->canvas()->setMouseTracking(true);

  // set default axis titles
  set_x_axis_label(0);
  set_y_axis_label(0,0);

/*
// still necessary?
#if QWT_VERSION < 0x04FFFF
  qwtplotter->setAxisTitleFont(QwtPlot::xBottom,QFont(_FONT_TYPE_, _FONT_SIZE_));
  qwtplotter->setAxisTitleFont(QwtPlot::yLeft,  QFont(_FONT_TYPE_, _FONT_SIZE_));
  qwtplotter->setAxisTitleFont(QwtPlot::yRight, QFont(_FONT_TYPE_, _FONT_SIZE_));
#endif
*/

  qwtplotter->enableAxis(QwtPlot::xBottom,true);

/*
  qwtplotter->setAxisLabelFormat(QwtPlot::yLeft,'e',1,10);
  qwtplotter->setAxisLabelFormat(QwtPlot::xBottom,'f',2);
*/
  qwtplotter->setAxisScaleDraw(QwtPlot::yLeft, new GuiScaleDraw());
  qwtplotter->setAxisScaleDraw(QwtPlot::xBottom, new GuiScaleDraw());


#if QWT_VERSION < 0x060000
  qwtplotter->setCanvasBackground(_ARRAY_BACKGROUND_COLOR_);
#else
  qwtplotter->setCanvasBackground(QBrush(QColor(_ARRAY_BACKGROUND_COLOR_)));
#endif


#if QWT_VERSION > 0x04FFFF
  plotgrid=new QwtPlotGrid();
  ODINLOG(odinlog,normalDebug) << "plotgrid=" << plotgrid << STD_endl;
#endif
  QPen gridpen(QColor(_ARRAY_GRID_COLOR_).dark(_ARRAY_GRID_DARK_FACTOR_));
#if QWT_VERSION > 0x04FFFF
  ODINLOG(odinlog,normalDebug) << "plotgrid=" << plotgrid << STD_endl;
  plotgrid->setPen(gridpen);

#if QWT_VERSION < 0x060100
  plotgrid->setMajPen(gridpen);
  plotgrid->setMinPen(gridpen);
#else
  plotgrid->setMajorPen(gridpen);
  plotgrid->setMinorPen(gridpen);
#endif

#else
  qwtplotter->setGridPen(gridpen);
  qwtplotter->setGridMajPen(gridpen);
  qwtplotter->setGridMinPen(gridpen);
#endif
  enable_grid(true);
#if QWT_VERSION > 0x04FFFF
  plotgrid->attach(qwtplotter);
#endif


#if QWT_VERSION < 0x04FFFF
  qwtplotter->enableLegend(false);
  qwtplotter->enableOutline(false);
#endif

  QwtPlotCanvas* canv=(QwtPlotCanvas*)qwtplotter->canvas(); // cast required for qwt 6.1 (and higher?)

  canvas_framewidth=canv->lineWidth();

  picker=new GuiPlotPicker(canv,this);
  picker->setRubberBandPen(QColor(_ARRAY_SELECTION_COLOR_));
  set_rect_outline_style(); // do this after picker was initialized

}


void GuiPlot::set_axis_label(int axisId, const char* label, bool omit, int alignment) {
  Log<OdinQt> odinlog("GuiPlot","set_axis_label");
  ODINLOG(odinlog,normalDebug) << "axisId/label/omit/alignment=" << axisId << "/" << label << "/" << omit << "/" << alignment << STD_endl;
  if(!label || STD_string(label)=="") omit=true;
  if(omit) {
#if QWT_VERSION > 0x04FFFF
    QwtText txt("");
    txt.setFont(QFont(_FONT_TYPE_, 1));
    qwtplotter->setAxisTitle(axisId,txt);
#else
    qwtplotter->setAxisTitle(axisId,"");
    qwtplotter->setAxisTitleFont(axisId,QFont(_FONT_TYPE_, 1)); // make font zero size to save space in widget
#endif
  } else {
#if QWT_VERSION > 0x04FFFF
    QwtText txt(label);
    txt.setFont(QFont(_FONT_TYPE_, _FONT_SIZE_));
    txt.setRenderFlags(alignment);
    qwtplotter->setAxisTitle(axisId,txt);
#else
    qwtplotter->setAxisTitle(axisId,label);
    qwtplotter->setAxisTitleFont(axisId,QFont(_FONT_TYPE_, _FONT_SIZE_));
    qwtplotter->setAxisTitleAlignment(axisId,alignment);
#endif
  }
}


void GuiPlot::set_x_axis_label(const char *xAxisLabel, bool omit) {
  set_axis_label(QwtPlot::xBottom,xAxisLabel,omit,Qt::AlignRight);
}


void GuiPlot::set_y_axis_label(const char *yAxisLabelLeft, const char *yAxisLabelRight) {
  if(yAxisLabelLeft)  set_axis_label(QwtPlot::yLeft, yAxisLabelLeft, false,Qt::AlignCenter);
  if(yAxisLabelRight) set_axis_label(QwtPlot::yRight,yAxisLabelRight,false,Qt::AlignCenter);

  if(yAxisLabelLeft)  qwtplotter->enableAxis(QwtPlot::yLeft,true);
  if(yAxisLabelRight) qwtplotter->enableAxis(QwtPlot::yRight,true);
}


long GuiPlot::insert_curve(bool use_right_y_axis, bool draw_spikes, bool baseline) {
  Log<OdinQt> odinlog("GuiPlot","insert_curve");
  long result=0;

  int yaxis=QwtPlot::yLeft;
  if(use_right_y_axis) yaxis=QwtPlot::yRight;

  QPen curvepen(QColor(_ARRAY_FOREGROUND_COLOR1_));
  if(use_right_y_axis || baseline) {
    curvepen=QColor(_ARRAY_FOREGROUND_COLOR2_);
  }

#if QWT_VERSION > 0x04FFFF
  QwtPlotCurve* curve=new QwtPlotCurve();
#if QWT_VERSION < 0x060000
  curve->setAxis(QwtPlot::xBottom, yaxis);
#else
  curve->setAxes(QwtPlot::xBottom, yaxis);
#endif
  curve->setPen(curvepen);
  curve->attach(qwtplotter);
  result=curve_map.size()+1;
  curve_map[result]=curve;
#else
  result=qwtplotter->insertCurve("",QwtPlot::xBottom,yaxis);
  qwtplotter->setCurvePen(result,curvepen);
#endif


  if(draw_spikes) {
#if QWT_VERSION > 0x04FFFF
    curve->setBaseline(0.0);
    curve->setStyle(QwtPlotCurve::Sticks);
#else
    qwtplotter->setCurveBaseline(result,0.0);
    qwtplotter->setCurveStyle(result,QwtCurve::Sticks);
#endif
  }


  if(baseline) baseline_id_cache=result;

  ODINLOG(odinlog,normalDebug) << "result=" << result << STD_endl;
  return result;
}


long GuiPlot::insert_marker(const char* label, double x, bool outline, bool horizontal, bool animate) {
  Log<OdinQt> odinlog("GuiPlot","insert_marker");
  long result=0;

  ODINLOG(odinlog,normalDebug) << "label/x/outline/horizontal/animate=" << label << "/" << x << "/" << outline << "/" << horizontal << "/" << animate << STD_endl;

  QColor markcolor(QColor(_ARRAY_MARKER_COLOR_).light(_ARRAY_MARKER_BRIGHT_FACTOR_));
  if(animate) markcolor=QColor("red");
  if(outline) markcolor=QColor(_ARRAY_SELECTION_COLOR_);

#if QWT_VERSION > 0x04FFFF
  QwtPlotMarker* marker=new QwtPlotMarker();
  if(horizontal) {
    marker->setLineStyle(QwtPlotMarker::HLine);
    marker->setYValue(x);
  } else {
    marker->setLineStyle(QwtPlotMarker::VLine);
    marker->setXValue(x);
  }
  marker->setLinePen(QPen(markcolor));
#if QWT_VERSION > 0x04FFFF
  QwtText txt(label);
  txt.setColor(markcolor);
  txt.setRenderFlags(Qt::AlignTop);
  marker->setLabel(txt);
#else
  marker->setLabelPen(QPen(markcolor));
  marker->setLabelAlignment(Qt::AlignTop);
  marker->setLabel(QString(label));
#endif
  marker->attach(qwtplotter);
  result=marker_map.size()+1;
  marker_map[result]=marker;
#else
  int axis=QwtPlot::xBottom;
  if(horizontal) axis=QwtPlot::yLeft;
  result=qwtplotter->insertLineMarker(label, axis);
  qwtplotter->setMarkerPen(result,QPen(markcolor));
  if(horizontal) qwtplotter->setMarkerYPos(result,x);
  else           qwtplotter->setMarkerXPos(result,x);
#endif

  ODINLOG(odinlog,normalDebug) << "result=" << result << STD_endl;
  return result;
}



void GuiPlot::remove_marker(long id) {
  Log<OdinQt> odinlog("GuiPlot","remove_marker");
  ODINLOG(odinlog,normalDebug) << "id=" << id << STD_endl;
#if QWT_VERSION > 0x04FFFF
  QwtPlotMarker* qpm=get_marker(id);
  if(qpm) qpm->detach();
#else
  qwtplotter->removeMarker(id);
#endif
}

void GuiPlot::set_marker_pos(long id, double x) {
  Log<OdinQt> odinlog("GuiPlot","remove_marker");
  ODINLOG(odinlog,normalDebug) << "id/x=" << id << "/" << x << STD_endl;
#if QWT_VERSION > 0x04FFFF
  QwtPlotMarker* qpm=get_marker(id);
  if(qpm) qpm->setXValue(x);
#else
  qwtplotter->setMarkerXPos(id,x);
#endif
}


void GuiPlot::set_curve_data(long curveid, const double* x, const double* y, int n, bool symbol) {
  Log<OdinQt> odinlog("GuiPlot","set_curve_data");
  ODINLOG(odinlog,normalDebug) << "curveid/x/y/symbol=" << curveid << "/" << x << "/" << y << "/" << symbol << STD_endl;

#if QWT_VERSION > 0x04FFFF
  QwtSymbol::Style symbstyle=QwtSymbol::NoSymbol;
#else
  QwtSymbol::Style symbstyle=QwtSymbol::None;
#endif
  if(symbol) {
    symbstyle=QwtSymbol::Ellipse;
  }

  QwtSymbol* symb=new QwtSymbol(symbstyle,QBrush(),QColor(_ARRAY_FOREGROUND_COLOR1_),QSize(PLOT_SYMBOLS_SIZE,PLOT_SYMBOLS_SIZE));

  if(n) {
    ODINLOG(odinlog,normalDebug) << "y[" << n/2 << "]=" << y[n/2] << STD_endl;
  }
#if QWT_VERSION > 0x04FFFF
  QwtPlotCurve* qpc=get_curve(curveid);
  if(qpc) {
#if QWT_VERSION < 0x060000
    qpc->setSymbol(*symb);
    qpc->setRawData(x, y, n);
#else
    qpc->setSymbol(symb);
    qpc->setRawSamples(x, y, n);
#endif
  }
#else
  qwtplotter->setCurveSymbol(curveid,*symb);
  qwtplotter->setCurveRawData(curveid, x, y, n);
#endif
#if QWT_VERSION < 0x060000
  delete symb;  // qwt6 deletes symbol itself
#endif
}


void GuiPlot::replot() {
  Log<OdinQt> odinlog("GuiPlot","replot");
  ODINLOG(odinlog,normalDebug) << "replotting " << curve_map.size() << " curves and " << marker_map.size() << " markers" << STD_endl;
  qwtplotter->replot();
//  qwtzoomer->setZoomBase();
}


void GuiPlot::autoscale() {
  qwtplotter->setAxisAutoScale(QwtPlot::xBottom);
  qwtplotter->setAxisAutoScale(QwtPlot::yLeft);
  qwtplotter->setAxisAutoScale(QwtPlot::yRight);
  replot();
}


void GuiPlot::autoscale_y(double& maxBound) {
  qwtplotter->setAxisAutoScale(QwtPlot::yLeft);
  qwtplotter->replot(); // seems to be neccessary for correct autoscaling, even if the curve is already added

#if QWT_VERSION > 0x04FFFF
#if QWT_VERSION > 0x0501FF

#if QWT_VERSION < 0x060100
   double new_lbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft)->lowerBound();
   double new_hbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft)->upperBound();
#else
   double new_lbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft).lowerBound();
   double new_hbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft).upperBound();
#endif

#else
   double new_lbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft)->lBound();
   double new_hbound=qwtplotter->axisScaleDiv(QwtPlot::yLeft)->hBound();
#endif
#else
  double new_lbound=qwtplotter->axisScale(QwtPlot::yLeft)->lBound();
  double new_hbound=qwtplotter->axisScale(QwtPlot::yLeft)->hBound();
#endif

  // symmetrical adjustment
  maxBound=STD_max(fabs(new_lbound),fabs(new_hbound));
  new_lbound=-maxBound;
  new_hbound= maxBound;

  qwtplotter->setAxisScale(QwtPlot::yLeft, new_lbound, new_hbound); // switch back to manual scaling for scrolling
  replot();
}

void GuiPlot::rescale_y(double maxBound) {
  qwtplotter->setAxisScale(QwtPlot::yLeft, -maxBound, maxBound);
  replot();
}


void GuiPlot::clear() {
  Log<OdinQt> odinlog("GuiPlot","clear()");
#if QWT_VERSION > 0x04FFFF
  for(STD_map<long,QwtPlotCurve*>::const_iterator curvit=curve_map.begin(); curvit!=curve_map.end(); ++curvit) {
    ODINLOG(odinlog,normalDebug) << "Detaching curve #" << curvit->first << STD_endl;
    curvit->second->detach();
    ODINLOG(odinlog,normalDebug) << "Deleting curve #" << curvit->first << STD_endl;
    delete curvit->second;
  }
  ODINLOG(odinlog,normalDebug) << "Clearing curve cache" << STD_endl;
  curve_map.clear();
  ODINLOG(odinlog,normalDebug) << "Removing markers" << STD_endl;
  remove_markers();
#endif
#if QWT_VERSION < 0x060000
  qwtplotter->clear();  // do this after detaching curves
#else
//  qwtplotter->detachItems(); // does not work with QwtPlotGrid
#endif
}

void GuiPlot::remove_markers() {
#if QWT_VERSION > 0x04FFFF
  for(STD_map<long,QwtPlotMarker*>::const_iterator markit=marker_map.begin(); markit!=marker_map.end(); ++markit) {
    markit->second->detach();
    delete markit->second;
  }
  marker_map.clear();
#else
  qwtplotter->removeMarkers();
#endif
}

double GuiPlot::get_x(int x_pixel) const {
  Log<OdinQt> odinlog("GuiPlot","get_x");
  double result=qwtplotter->invTransform(QwtPlot::xBottom,x_pixel+canvas_framewidth);
  ODINLOG(odinlog,normalDebug) << "result/x_pixel=" << result << "/" << x_pixel << STD_endl;
  return result;;
}

double GuiPlot::get_y(int y_pixel, bool right_axes) const {
  if(right_axes) return qwtplotter->invTransform(QwtPlot::yRight, y_pixel+canvas_framewidth);
  else           return qwtplotter->invTransform(QwtPlot::yLeft,  y_pixel+canvas_framewidth);
}

long GuiPlot::closest_curve(int x, int y, int& dist) const {
#if QWT_VERSION > 0x04FFFF
  Log<OdinQt> odinlog("GuiPlot","closest_curve");
  long result=-1;
  double dmin = 1.0e10;
  double curvedist;
  QPoint pos(x,y);
  for(STD_map<long,QwtPlotCurve*>::const_iterator curvit=curve_map.begin(); curvit!=curve_map.end(); ++curvit) {
    curvit->second->closestPoint(pos,&curvedist);
    if(curvedist<dmin) {
      result=curvit->first;
      dist=int(curvedist);
      dmin=curvedist;
    }
  }
  ODINLOG(odinlog,normalDebug) << "result/dist" << result << "/" << dist << STD_endl;
  return result;
#else
  return qwtplotter->closestCurve(x,y,dist);
#endif
}

void GuiPlot::highlight_curve(long id, bool flag) {
  const char* color=_ARRAY_FOREGROUND_COLOR1_;
  if(flag) color=_ARRAY_HIGHLIGHT_COLOR_;
  set_curve_pen(id,color);
}

void GuiPlot::set_x_axis_scale(double lbound, double ubound) {
  Log<OdinQt> odinlog("GuiPlot","set_x_axis_scale");
  ODINLOG(odinlog,normalDebug) << "lbound/ubound=" << lbound << "/" << ubound << STD_endl;
  qwtplotter->setAxisScale(QwtPlot::xBottom,lbound,ubound);
}


void GuiPlot::set_y_axis_scale(double lbound, double ubound, bool right_axes) {
  if(right_axes) qwtplotter->setAxisScale(QwtPlot::yRight,lbound,ubound);
  else           qwtplotter->setAxisScale(QwtPlot::yLeft, lbound,ubound);
}


void GuiPlot::set_curve_pen(long id, const char* color, int width) {
  QPen qp;
  qp.setColor(color);
  qp.setWidth(width);
#if QWT_VERSION > 0x04FFFF
  QwtPlotCurve* qpc=get_curve(id);
  if(qpc) qpc->setPen(qp);
#else
  qwtplotter->setCurvePen(id,qp);
#endif
}

void GuiPlot::set_rect_outline_style() {
#if QWT_VERSION < 0x060000
  picker->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::CornerToCorner | QwtPicker::DragSelection);
#else
  picker->setStateMachine(new QwtPickerDragRectMachine);
#endif
  picker->setRubberBand(QwtPicker::RectRubberBand);
}

void GuiPlot::set_line_outline_style(bool horizontal) {
#if QWT_VERSION < 0x060000
  picker->setSelectionFlags(QwtPicker::PointSelection | QwtPicker::DragSelection);
#else
  picker->setStateMachine(new QwtPickerDragPointMachine);
#endif
  if(horizontal) picker->setRubberBand(QwtPicker::HLineRubberBand);
  else           picker->setRubberBand(QwtPicker::VLineRubberBand);
}

void GuiPlot::enable_axes(bool flag) {
  qwtplotter->enableAxis(QwtPlot::xBottom,flag);
  qwtplotter->enableAxis(QwtPlot::yLeft,flag);
}


void GuiPlot::enable_grid(bool flag) {
  Log<OdinQt> odinlog("GuiPlot","enable_grid");
#if QWT_VERSION > 0x04FFFF
  plotgrid->enableX(flag);
  plotgrid->enableY(flag);
#else
  qwtplotter->enableGridX(flag);
  qwtplotter->enableGridY(flag);
#endif
}


void GuiPlot::print(QPainter* painter, const QRect& rect) const {
  Log<OdinQt> odinlog("GuiPlot","print");
#if QWT_VERSION < 0x060000
  GuiPlotPrintFilter pfilter(baseline_id_cache);
  qwtplotter->print(painter,rect,pfilter);
#else
  QwtPlotRenderer renderer;
  renderer.render(qwtplotter, painter, QRectF(rect));
#endif
}


QWidget* GuiPlot::get_widget() {return qwtplotter;}


void GuiPlot::emit_plotMousePressed(const QMouseEvent& qme) {
  emit plotMousePressed(qme);
}

void GuiPlot::emit_plotMouseReleased(const QMouseEvent& qme) {
  emit plotMouseReleased(qme);
}

void GuiPlot::emit_plotMouseMoved(const QMouseEvent& qme) {
  emit plotMouseMoved(qme);
}


QwtPlotCurve* GuiPlot::get_curve(long id) {
  STD_map<long,QwtPlotCurve*>::iterator it=curve_map.find(id);
  if(it==curve_map.end()) return 0;
  return it->second;
}


QwtPlotMarker* GuiPlot::get_marker(long id) {
  STD_map<long,QwtPlotMarker*>::iterator it=marker_map.find(id);
  if(it==marker_map.end()) return 0;
  return it->second;
}



GuiPlot::~GuiPlot() {
  Log<OdinQt> odinlog("GuiPlot","~GuiPlot()");
  clear();
#if QWT_VERSION > 0x04FFFF
  delete plotgrid;
#endif
  delete picker;
  delete qwtplotter;
}


////////////////////////////////////////////////////////////////////////////


GuiWheel::GuiWheel(QWidget *parent) {
  wheel=new QwtWheel(parent);
  wheel->setOrientation(Qt::Vertical);

  connect( wheel, SIGNAL(valueChanged(double)),  SLOT(emit_valueChanged(double)) );
}

void GuiWheel::set_range(double min, double max) {
  wheel->setRange(min,max);
}

void GuiWheel::set_value(double newval) {
//  wheel->fitValue(newval); // ?
  wheel->setValue(newval);
}


QWidget* GuiWheel::get_widget() {return wheel;}

void GuiWheel::emit_valueChanged(double newval) {emit valueChanged(newval);}

GuiWheel::~GuiWheel() {
  delete wheel;
}