- 3.0.2 core module.
plotQwt.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <qwt/qwt_plot_curve.h>
5 #include <qwt/qwt_plot.h>
6 #include <qwt/qwt_plot_magnifier.h>
7 #include <qwt/qwt_plot_panner.h>
8 #include <qwt/qwt_plot_picker.h>
9 #include <qwt/qwt_picker_machine.h>
10 #include <qwt/qwt_legend.h>
11 #include <qapplication.h>
12 
13 namespace ct {
14 namespace core {
15 namespace plotQwt {
16 
17 class Figure
18 {
19 public:
20  Figure() : holdOn_(false), magnifier_(plot_.canvas()), panner_(plot_.canvas()), picker_(plot_.canvas())
21  {
22  plot_.setFixedWidth(800);
23  plot_.setFixedHeight(600);
24  plot_.setCanvasBackground(QBrush(Qt::white));
25 
26  picker_.setStateMachine(new QwtPickerDragPointMachine);
27  picker_.setTrackerMode(QwtPicker::AlwaysOn);
28  picker_.setEnabled(true);
29  picker_.setRubberBandPen(QColor(Qt::black));
30  picker_.setRubberBand(QwtPicker::CrossRubberBand);
31  //picker_.setTrackerPen(QPen(QColor(255,0,0)));
32  }
33 
34  void plot(const std::vector<double>& y)
35  {
36  std::cout << "plotting" << std::endl;
37 
38  if (!holdOn_ || curves_.size() == 0)
39  createCurve();
40 
41  Eigen::VectorXd x;
42  x.setLinSpaced(y.size(), 0, y.size());
43 
44  curves_.back()->setSamples(x.data(), y.data(), x.size());
45 
46  std::cout << "set data" << std::endl;
47  }
48 
49  void draw()
50  {
51  std::cout << "showing result" << std::endl;
52  plot_.replot();
53  plot_.show();
54  std::cout << "done" << std::endl;
55  }
56 
57  void hold(bool hold = true) { holdOn_ = hold; }
58  void title(const std::string& title) { plot_.setWindowTitle(title.c_str()); }
59  void setDimensions(size_t dimX_pixels, size_t dimY_pixels)
60  {
61  plot_.setFixedWidth(dimX_pixels);
62  plot_.setFixedHeight(dimY_pixels);
63  }
64 
65 private:
66  typedef std::shared_ptr<QwtPlotCurve> QwtPlotCurvePtr;
67 
68  void setDefaultCurveParams(QwtPlotCurvePtr& curve) { curve->setRenderHint(QwtPlotItem::RenderAntialiased); }
69  void createCurve()
70  {
71  std::cout << "creating curve" << std::endl;
72 
73  // create new curve
74  curves_.resize(curves_.size() + 1);
75  curves_.back() = QwtPlotCurvePtr(new QwtPlotCurve);
76  setDefaultCurveParams(curves_.back());
77 
78  curves_.back()->attach(&plot_);
79  }
80 
81  bool holdOn_;
82 
83  QwtPlot plot_;
84  QwtPlotMagnifier magnifier_;
85  QwtPlotPanner panner_;
86  QwtPlotPicker picker_;
87  std::vector<QwtPlotCurvePtr> curves_;
88 };
89 
90 typedef std::shared_ptr<Figure> FigurePtr;
91 
92 namespace detail {
93 
94 
96 {
97 public:
98  FigurePtr createFigure()
99  {
100  std::cout << "creating figure" << std::endl;
101 
102  figures_.resize(figures_.size() + 1);
103  figures_.back() = FigurePtr(new Figure());
104  figures_.back()->title("Figure " + std::to_string(figures_.size() - 1));
105 
106  return figures_.back();
107  }
108 
110  {
111  std::cout << "initializing application " << std::endl;
112 
113  fake_argv_[0] = "foo";
114  fake_argv_[1] = NULL;
115 
116  application_ = new QApplication(fake_argc_, fake_argv_);
117  }
118 
119 
120  ~_application() { delete application_; }
121  void exec() { application_->exec(); }
122 private:
123  std::string appName_ = "Plot";
124  static const int fake_argc_size_ = 2;
125  int fake_argc_ = fake_argc_size_;
126  char* fake_argv_[fake_argc_size_];
127 
128  QApplication* application_;
129 
130  std::vector<FigurePtr> figures_;
131 };
132 
133 static void _startApplication(_application*& app)
134 {
135  std::cout << "starting application" << std::endl;
136  app = new _application();
137  std::cout << "created application, will now exec" << std::endl;
138  app->exec();
139 }
140 
142 {
143  static _application* get()
144  {
145  static _interpreter ctx;
146  if (ctx.app_ == nullptr)
147  {
148  std::cout << "ctx.app_ is null ptr" << std::endl;
149  exit(-1);
150  }
151  return ctx.app_;
152  }
153 
154 private:
155  _interpreter() : app_(nullptr)
156  {
157  // std::cout << "initializing interpreter" << std::endl;
158  // applicationThread_ = new std::thread( [this] {_startApplication(this->app_); });
159  //
160  // // hacky: wait for initialization
161  // while(app_ == nullptr)
162  // {
163  // std::this_thread::sleep_for(std::chrono::milliseconds(10));
164  // }
165  //
166  // std::cout << "interpreter init done " <<std::endl;
167  app_ = new _application();
168  }
169 
170  _interpreter(const _interpreter& other) = delete;
171 
172  ~_interpreter()
173  {
174  std::cout << "waiting for app to finish" << std::endl;
175  // applicationThread_->join();
176  // delete applicationThread_;
177  delete app_;
178  }
179 
180  //std::thread* applicationThread_;
181  _application* app_;
182 };
183 
184 
185 } // namespace detail
186 
187 FigurePtr createFigure()
188 {
190 }
191 
192 void render()
193 {
194  return detail::_interpreter::get()->exec();
195 }
196 }
197 }
198 }
std::shared_ptr< Figure > FigurePtr
Definition: plotQwt.h:90
_application()
Definition: plotQwt.h:109
void plot(const std::vector< double > &y)
Definition: plotQwt.h:34
Figure()
Definition: plotQwt.h:20
void setDimensions(size_t dimX_pixels, size_t dimY_pixels)
Definition: plotQwt.h:59
FigurePtr createFigure()
Definition: plotQwt.h:98
FigurePtr createFigure()
Definition: plotQwt.h:187
static _application * get()
Definition: plotQwt.h:143
void exec()
Definition: plotQwt.h:121
~_application()
Definition: plotQwt.h:120
void draw()
Definition: plotQwt.h:49
void hold(bool hold=true)
Definition: plotQwt.h:57
ct::core::StateVector< state_dim > x
void render()
Definition: plotQwt.h:192
Definition: plotQwt.h:17
void title(const std::string &title)
Definition: plotQwt.h:58