00001 #include "GraphWidget.h"
00002 #include <cairomm/context.h>
00003 #include <iostream>
00004
00005 GraphWidget::GraphWidget(size_t maxVectors, double intBorder, int height) :
00006 m_maxVectors(maxVectors),
00007 m_step(1.0 / static_cast<double>(m_maxVectors)),
00008 m_intervalBorder(intBorder)
00009 {
00010 set_size_request(m_maxVectors, height);
00011
00012 Glib::signal_timeout().connect(sigc::mem_fun(*this, &GraphWidget::on_timeout), 25);
00013 }
00014
00015 GraphWidget::~GraphWidget()
00016 {
00017 }
00018
00019 void GraphWidget::addData(const Vector3d &v)
00020 {
00021 m_vectors.push_back(v);
00022 while (m_vectors.size() > m_maxVectors)
00023 m_vectors.pop_front();
00024 }
00025
00026 void GraphWidget::resize(size_t width, int height)
00027 {
00028 m_maxVectors = width;
00029 m_step = 1.0 / static_cast<double>(m_maxVectors);
00030 set_size_request(m_maxVectors, height);
00031 }
00032
00033 bool GraphWidget::on_expose_event(GdkEventExpose* event)
00034 {
00035 Glib::RefPtr<Gdk::Window> window = get_window();
00036 if (!window)
00037 return false;
00038
00039 Gtk::Allocation allocation = get_allocation();
00040 const int width = allocation.get_width();
00041 const int height = allocation.get_height();
00042 Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
00043
00044 if (event != NULL) {
00045 cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height);
00046 cr->clip();
00047 }
00048
00049 cr->scale(width, height);
00050 cr->translate(0, 0.5);
00051 cr->set_line_width(0.005);
00052 cr->save();
00053 cr->set_source_rgba(0,0,0,.3);
00054 cr->move_to(0,0);
00055 cr->line_to(1,0);
00056 cr->stroke();
00057
00058 cr->set_source_rgba(0,0,0,1);
00059 cr->set_line_width(0.002);
00060
00061 VectorList::iterator it;
00062 double x = 0;
00063 Vector3d tmp;
00064 Vector3d lastPos(0);
00065 for (it = m_vectors.begin(); it != m_vectors.end() && x < 1; ++it) {
00066 tmp = *it / m_intervalBorder / 1.2;
00067
00068 cr->move_to(x-m_step, lastPos.get(0));
00069 cr->line_to(x, tmp.get(0));
00070
00071 cr->move_to(x-m_step, lastPos.get(1));
00072 cr->line_to(x, tmp.get(1));
00073
00074 cr->move_to(x-m_step, lastPos.get(2));
00075 cr->line_to(x, tmp.get(2));
00076
00077 lastPos = tmp;
00078 x += m_step;
00079 }
00080 cr->stroke();
00081
00082 cr->restore();
00083
00084 return true;
00085 }
00086
00087 bool GraphWidget::on_timeout()
00088 {
00089 Glib::RefPtr<Gdk::Window> win = get_window();
00090 if (win) {
00091 Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
00092 win->invalidate_rect(r, false);
00093 }
00094 return true;
00095 }