161 lines
3.6 KiB
C++
161 lines
3.6 KiB
C++
|
#include "gtl_gui_chart_series.h"
|
||
|
|
||
|
namespace gtl
|
||
|
{
|
||
|
namespace gui
|
||
|
{
|
||
|
chart_series::chart_series(gtl::analog_data* ad, chart::axis_horz* axis_x, chart::axis_vert* axis_y)
|
||
|
: chart::series::xy::line(axis_x, axis_y)
|
||
|
, _ad(ad)
|
||
|
, _updater(this)
|
||
|
, _is_updating(true)
|
||
|
{
|
||
|
connect(&_updater, &QThread::finished, this, &chart_series::update_series);
|
||
|
connect(&_updater, &QThread::finished, this, &chart_series::data_changed);
|
||
|
|
||
|
if(_ad)
|
||
|
{
|
||
|
connect(_ad, >l::analog_data::color_changed, this, &chart_series::set_icolor);
|
||
|
set_icolor();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
chart_series::~chart_series()
|
||
|
{
|
||
|
_updater.terminate();
|
||
|
}
|
||
|
|
||
|
void chart_series::set_updating(bool value)
|
||
|
{
|
||
|
_is_updating = value;
|
||
|
}
|
||
|
|
||
|
analog_data *chart_series::ad()
|
||
|
{
|
||
|
return _ad;
|
||
|
}
|
||
|
|
||
|
QString chart_series::name() const
|
||
|
{
|
||
|
if(_ad)
|
||
|
return _ad->name();
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
qreal chart_series::left() const
|
||
|
{
|
||
|
if(empty())
|
||
|
return 0;
|
||
|
|
||
|
return front_x();
|
||
|
}
|
||
|
|
||
|
qreal chart_series::right() const
|
||
|
{
|
||
|
if(empty())
|
||
|
return 0;
|
||
|
|
||
|
return back_x();
|
||
|
}
|
||
|
|
||
|
qreal chart_series::get_y(qreal x)
|
||
|
{
|
||
|
if(size() < 2)
|
||
|
return 0;
|
||
|
|
||
|
qreal x_[2];
|
||
|
qreal y_[2];
|
||
|
int idx = 0;
|
||
|
|
||
|
if(x < front_x())
|
||
|
{
|
||
|
idx = 0;
|
||
|
}
|
||
|
else if(x > back_x())
|
||
|
{
|
||
|
idx = size() - 2;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
qreal dx = at_x(1) - at_x(0);
|
||
|
idx = (x - at_x(0))/dx;
|
||
|
}
|
||
|
|
||
|
x_[0] = at_x(idx);
|
||
|
x_[1] = at_x(idx + 1);
|
||
|
y_[0] = at_y(idx);
|
||
|
y_[1] = at_y(idx + 1);
|
||
|
|
||
|
qreal a = (y_[0] - y_[1])/(x_[0] - x_[1]);
|
||
|
qreal b = y_[0] - a*x_[0];
|
||
|
|
||
|
return a*x + b;
|
||
|
}
|
||
|
|
||
|
void chart_series::set_tool_tip(QPoint pos)
|
||
|
{
|
||
|
setToolTip("");
|
||
|
}
|
||
|
|
||
|
update_thread::update_thread(chart::series::xy::line *s)
|
||
|
: QThread(NULL)
|
||
|
, _series(s)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
void update_thread::set_data(std::vector<qreal> &data)
|
||
|
{
|
||
|
_mutex.lock();
|
||
|
_data = data;
|
||
|
_is_updated = true;
|
||
|
_mutex.unlock();
|
||
|
|
||
|
if(!isRunning())
|
||
|
start();
|
||
|
}
|
||
|
|
||
|
void update_thread::get_data(std::back_insert_iterator<std::vector<qreal>> data)
|
||
|
{
|
||
|
std::copy(_data.begin(), _data.end(), data);
|
||
|
}
|
||
|
|
||
|
void update_thread::stop()
|
||
|
{
|
||
|
_is_running = false;
|
||
|
wait();
|
||
|
}
|
||
|
|
||
|
void update_thread::run()
|
||
|
{
|
||
|
_is_running = true;
|
||
|
while(_is_updated)
|
||
|
{
|
||
|
_mutex.lock();
|
||
|
std::vector<qreal> buffer = _data;
|
||
|
_is_updated = false;
|
||
|
_mutex.unlock();
|
||
|
|
||
|
for(int i = 0; i < buffer.size(); i++)
|
||
|
{
|
||
|
_series->set_y(i, buffer[i], false);
|
||
|
if(!_is_running)
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void chart_series::update_series()
|
||
|
{
|
||
|
prepareGeometryChange();
|
||
|
}
|
||
|
|
||
|
void chart_series::set_icolor()
|
||
|
{
|
||
|
set_color(QColor(_ad->color()));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|