dialogplots.cpp 13.3 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
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
#include "dialogplots.h"
#include "ui_dialogplots.h"
#include "tb_interface.h"
#include <algorithm>
#include <QRandomGenerator64>

DialogPlots::DialogPlots(const TASKBUS::cmdlineParser * cmd,QWidget *parent) :
	QDialog(parent),
	m_cmd(cmd),
	ui(new Ui::DialogPlots),
	m_rthread(new reciv_thread(this))
{
	ui->setupUi(this);
	connect(m_rthread,&reciv_thread::sig_quit,this,&DialogPlots::close);
	connect(m_rthread,&reciv_thread::new_package,this,&DialogPlots::deal_package);
	connect(m_rthread,&reciv_thread::new_textcmd,ui->lineEdit_messages,&QLineEdit::setText);

	Qt::WindowFlags flg = windowFlags();
	flg |= Qt::WindowMinMaxButtonsHint;
	setWindowFlags(flg);
	m_rthread->start();

	//命令行参数解释
	QString schannels = QString::fromStdString(m_cmd->toString("channels","1,1,1,1,1"));
	QStringList lst = schannels.split(",");
	foreach(QString c, lst)
	{
		int vc = c.toInt();
		if (vc!=1 && vc !=2 && vc !=0)
			vc = 1;
		m_plot_chans.push_back(vc);
	}
	QString stypes = QString::fromStdString(m_cmd->toString("datatypes","9,9,9,9,9"));
	QStringList lsttps = stypes.split(",");
	foreach(QString c, lsttps)
	{
		int vc = c.toInt();
		if (vc<0 || vc>10)
			vc = 9;
		m_plot_types.push_back(vc);
	}
	QMap<int,int> plot_idxes;
	//Key is subject id, value is the id of plots
	for (int i=0;i<16;++i)
	{
		QString key = QString("plot%1").arg(i);
		int ins = m_cmd->toInt(key.toStdString(),0);
		if (ins!=0)
			m_plot_idxes[ins] = i;
	}

	refid = startTimer(30);
}

DialogPlots::~DialogPlots()
{
	m_rthread->terminate();
	m_rthread->wait();
	delete ui;
}

void DialogPlots::deal_package(QByteArray package)
{
	using namespace TASKBUS;
	if (!m_cmd)
		return;

	const subject_package_header * pheader = (const subject_package_header *)
			package.constData();

	if (m_plot_idxes.contains(pheader->subject_id)==false)
		return;
	const int nIDofPlot = m_plot_idxes[pheader->subject_id];
	const int channels = nIDofPlot<m_plot_chans.size()?m_plot_chans[nIDofPlot]:1;

	const quint64 hash_subidx = ((quint64(pheader->subject_id))<<32)+quint64(pheader->path_id);
	m_plot_refresh[hash_subidx] = true;

	if (m_subidxs.contains(hash_subidx)==false)
	{
		QString strName = tr("Sub%1 Path%2").arg(pheader->subject_id).arg(pheader->path_id);
		if (tid==-1)
			tid=startTimer(1000);
		QChartView * pv = new QChartView(this);
		m_chat_views.push_back(pv);
		m_subidxs[hash_subidx] = m_chat_views.size()-1;

		QXYSeries * serials = nullptr;
		if (channels<2)
		{
			serials = new QLineSeries(this);
M
manjaro-xfce 已提交
92
			serials->setPen(QPen(QColor(255,255,0)));
丁劲犇's avatar
丁劲犇 已提交
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
		}
		else
		{
			QScatterSeries * nserials = new QScatterSeries(this);
			nserials->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
			nserials->setMarkerSize(1);
			serials = nserials;
		}
		m_chat_serials.push_back(serials);

		QValueAxis * ax = new QValueAxis(this);
		ax->setGridLineVisible(true);
		m_char_axis_x.push_back(ax);
		QValueAxis * ay = new QValueAxis(this);
		m_char_axis_y.push_back(ay);
		ay->setGridLineVisible(true);

		QChart *chart = new QChart();
		chart->legend()->hide();
		chart->addAxis(ax,Qt::AlignBottom);
		chart->addAxis(ay,Qt::AlignLeft);
		m_chars.push_back(chart);

		chart->setTheme(QChart::ChartThemeDark);

		pv->setChart(chart);
		chart->setTitle(tr("SUB%1 PATH%2").arg(pheader->subject_id).arg(pheader->path_id));

D
dev 已提交
121
		pv->setRubberBand(channels<2?QChartView::VerticalRubberBand:QChartView::RectangleRubberBand);
丁劲犇's avatar
丁劲犇 已提交
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

		chart->addSeries(serials);
		serials->attachAxis(ax);
		serials->attachAxis(ay);
		pv->show();
		if (channels==0)
		{
			QSplitter * spliter = new QSplitter(this);
			spliter->setOrientation(Qt::Vertical);
			spliter->addWidget(pv);
			SpectroWidget * ws = new SpectroWidget(this);
			spliter->addWidget(ws);
			ui->tabWidget_outputs->addTab(spliter,strName);
			m_chat_spec.push_back(ws);
			ws->show();
		}
		else
		{
			ui->tabWidget_outputs->addTab(pv,strName);
			m_chat_spec.push_back(0);
		}


	}

	m_plot_buffer[hash_subidx] = flush_data(package);
}

QVector<double> DialogPlots::flush_data(QByteArray package)
{
	QVector<double> vec_res;
	using namespace TASKBUS;
	if (!m_cmd)
		return vec_res;

	const subject_package_header * pheader = (const subject_package_header *)
			package.constData();

	if (m_plot_idxes.contains(pheader->subject_id)==false)
		return vec_res;
	const int nSub = m_plot_idxes[pheader->subject_id];
	const int rawchannels = nSub<m_plot_chans.size()?m_plot_chans[nSub]:1;
	const int channels = rawchannels<2?1:2;

	const int types = nSub<m_plot_types.size()?m_plot_types[nSub]:9;

	const char * rawdata =  (const char *)(package.constData()+sizeof(subject_package_header));
	int pts = 0;;

	//"range":{"value":"0:uint8 1:int8 2:uint16 3:int16 4:uint32 5:int32 6:uint64 7:int64 8:float 9:double"}
	switch(types)
	{
	case 0:
		pts = pheader->data_length/sizeof(quint8)/channels;
	{
		const quint8 * ptrRaw = (const quint8 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 1:
		pts = pheader->data_length/sizeof(qint8)/channels;
	{
		const qint8 * ptrRaw = (const qint8 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 2:
		pts = pheader->data_length/sizeof(quint16)/channels;
	{
		const quint16 * ptrRaw = (const quint16 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 3:
		pts = pheader->data_length/sizeof(qint16)/channels;
	{
		const qint16 * ptrRaw = (const qint16 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 4:
		pts = pheader->data_length/sizeof(quint32)/channels;
	{
		const quint32 * ptrRaw = (const quint32 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 5:
		pts = pheader->data_length/sizeof(qint32)/channels;
	{
		const qint32 * ptrRaw = (const qint32 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 6:
		pts = pheader->data_length/sizeof(quint64)/channels;
	{
		const quint64 * ptrRaw = (const quint64 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 7:
		pts = pheader->data_length/sizeof(qint64)/channels;
	{
		const qint64 * ptrRaw = (const qint64 *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 8:
		pts = pheader->data_length/sizeof(float)/channels;
	{
		const float * ptrRaw = (const float *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	case 9:
		pts = pheader->data_length/sizeof(double)/channels;
	{
		const double * ptrRaw = (const double *) rawdata;
		for (int i=0;i<pts;++i)
			vec_res<<ptrRaw[i];
	}
		break;
	default:
		break;
	}

	return vec_res;
}

void DialogPlots::on_pushButton_reset_clicked()
{
	const int nGs = m_chars.size();
	for (int subid=0;subid<nGs;++subid)
	{
		QXYSeries * serials = m_chat_serials[subid];
		//QChart *chart = m_chars[subid];
		QValueAxis * ax = m_char_axis_x[subid];
		QValueAxis * ay = m_char_axis_y[subid];
		//QChartView * pv = m_chat_views[subid];
		int oldsize = serials->points().size();
		if (oldsize)
		{
			double max_x = serials->at(0).x(), max_y = serials->at(0).y();
			double min_x = serials->at(0).x(), min_y = serials->at(0).y();
			for (int i=1;i<oldsize;++i)
			{
				double x = serials->at(i).x(), y = serials->at(i).y();
				if (x>max_x)					max_x = x;
				if (x<min_x)					min_x = x;
				if (y>max_y)					max_y = y;
				if (y<min_y)					min_y = y;
			}

			const double seed_x = max_x - min_x;
			const double seed_y = max_y - min_y;
			double new_max_x = ax->max(), new_max_y = ay->max();
			double new_min_x = ax->min(), new_min_y = ay->min();

			bool needup_x = false,needup_y = false;

M
manjaro-xfce 已提交
292 293
			if (max_x != new_max_x)	new_max_x = max_x ,needup_x=true;
			//if (max_x *1.2 < new_max_x)	new_max_x = max_x + seed_x * 0.2,needup_x=true;
丁劲犇's avatar
丁劲犇 已提交
294 295 296 297

			if (max_y > new_max_y)	new_max_y = max_y + seed_y * 0.2,needup_y=true;
			if (max_y *1.2 < new_max_y)	new_max_y = max_y + seed_y * 0.2,needup_y=true;

M
manjaro-xfce 已提交
298 299
			if (min_x != new_min_x)	new_min_x = min_x,needup_x=true;
			//if (min_x > new_min_x * 1.2)	new_min_x = min_x,needup_x=true;
丁劲犇's avatar
丁劲犇 已提交
300 301 302 303

			if (min_y < new_min_y)	new_min_y = min_y,needup_y=true;
			if (min_y > new_min_y * 1.2)	new_min_y = min_y,needup_y=true;

丁劲犇's avatar
丁劲犇 已提交
304 305 306 307
			if (max_x >=1e9 || max_x <-1e9 || max_y >1e9 ||max_y < -1e9 || max_x - min_x <1e-8)
				needup_x = false,needup_y = false;
			if (min_y >=1e9 || min_y <-1e9 || min_x >1e9 ||min_x < -1e9|| max_y - min_y <1e-8)
				needup_x = false,needup_y = false;
M
manjaro-xfce 已提交
308

丁劲犇's avatar
丁劲犇 已提交
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
			if (needup_x==true)
				ax->setRange(new_min_x,new_max_x);
			if (needup_y==true)
				ay->setRange(new_min_y,new_max_y);



		}

	}
}

void DialogPlots::timerEvent(QTimerEvent *event)
{
	if (event->timerId()==tid)
	{
		killTimer(tid);
		tid = -1;
		on_pushButton_reset_clicked();
	}
	else if (event->timerId()==refid)
	{
		killTimer(refid);
		QRandomGenerator64 rg;
		QList<quint64> hash_subidxes = m_subidxs.keys();
		foreach (quint64 hash_subidx, hash_subidxes)
		{
			const int subject_id = hash_subidx>>32;
			if (!m_plot_idxes.contains(subject_id))
				continue;
			if (!m_plot_refresh[hash_subidx])
				continue;
			m_plot_refresh[hash_subidx] = false;
			const int nIDofPlot = m_plot_idxes[subject_id];
			const int rawchannels = nIDofPlot<m_plot_chans.size()?m_plot_chans[nIDofPlot]:1;
			const int subid = m_subidxs[hash_subidx];
			const int channels = rawchannels<2?1:2;
			QChartView * pv = m_chat_views[subid];
			QXYSeries * serials = m_chat_serials[subid];

			const int pts = m_plot_buffer[hash_subidx].size();
M
manjaro-xfce 已提交
350 351

			QList<QVector<double> >  & avgList = m_plot_lastUpdates[hash_subidx];
D
dev 已提交
352
			bool clear_lst = channels>1?true: false;
M
manjaro-xfce 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
			for (QList<QVector<double> >::iterator pit = avgList.begin();pit!=avgList.end();++pit)
			{
				if (pit->size()!=pts)
				{
					clear_lst = true;
					break;
				}
			}
			if (clear_lst)
				avgList.clear();
			avgList.push_back(m_plot_buffer[hash_subidx]);
			while (avgList.size()>6)
				avgList.pop_front();

			QVector<double> vec_tmp(pts,0);
			double * fdata = vec_tmp.data();
			const int avgsz = avgList.size();
			for (QList<QVector<double> >::iterator pit = avgList.begin();pit!=avgList.end();++pit)
			{
				for (int i=0;i<pts;++i)
丁劲犇's avatar
丁劲犇 已提交
373
					fdata[i] += pit->at(i)/(avgsz+1e-14);
M
manjaro-xfce 已提交
374 375
			}

丁劲犇's avatar
丁劲犇 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388

			double max_x = 0, max_y =  fdata[0];
			double min_x = 0, min_y =  fdata[0];

			int step = channels>1?(pts / 4096):(pts/65536);
			if (step<1)
				step = 1;


			if (serials->points().size()>4096 || pts>1)
			{
				SpectroWidget * ws = m_chat_spec[subid];
				QVector<QPointF> vec_points;
丁劲犇's avatar
丁劲犇 已提交
389
				for (int i=0;i<pts/channels;i+=step)
丁劲犇's avatar
丁劲犇 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403
				{
					double x,y;
					if (channels==1)
						x = i,y=fdata[i];
					else
						x = fdata[i*2], y = fdata[i*2+1];
					vec_points.push_back(QPointF(x,y));

					if (x>max_x)					max_x = x;
					if (x<min_x)					min_x = x;
					if (y>max_y)					max_y = y;
					if (y<min_y)					min_y = y;

				}
丁劲犇's avatar
丁劲犇 已提交
404 405
				if (!(max_x >=1e9 || max_x <-1e9 || max_y >1e9 ||max_y < -1e9 || max_x - min_x <1e-8)||
					(min_y >=1e9 || min_y <-1e9 || min_x >1e9 ||min_x < -1e9|| max_y - min_y <1e-8))
丁劲犇's avatar
丁劲犇 已提交
406
				{
丁劲犇's avatar
丁劲犇 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
					serials->replace(vec_points);
					if (ws)
					{
						QChart * ca = m_chars[subid];
						QValueAxis * axx = m_char_axis_x[subid];
						QValueAxis * axy = m_char_axis_y[subid];
						double dmin = axx->min();
						double dmax = axx->max();
						QRectF r = ca->plotArea();

						ws->setMinMaxRange(axy->min(),axy->max());

						ws->append_data(vec_tmp,
										r.left(),
										r.right(),
										dmin,
										dmax
										);


					}
丁劲犇's avatar
丁劲犇 已提交
428 429
				}

丁劲犇's avatar
丁劲犇 已提交
430

丁劲犇's avatar
丁劲犇 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
			}
			else
			{

				QList<QPointF> lstpts = serials->points();
				const int rawsz = serials->points().size();
				for (int i=0;i<pts;i+=step)
					lstpts.push_back(QPointF(i+rawsz,fdata[i]));

				foreach(QPointF p, lstpts)
				{
					double x = p.x(), y = p.y();
					if (x>max_x)					max_x = x;
					if (x<min_x)					min_x = x;
					if (y>max_y)					max_y = y;
					if (y<min_y)					min_y = y;

				}
丁劲犇's avatar
丁劲犇 已提交
449 450 451
				if (!(max_x >=1e9 || max_x <-1e9 || max_y >1e9 ||max_y < -1e9 || max_x - min_x <1e-8)||
					(min_y >=1e9 || min_y <-1e9 || min_x >1e9 ||min_x < -1e9|| max_y - min_y <1e-8))
					serials->replace(lstpts);
丁劲犇's avatar
丁劲犇 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464
			}

			//QChart *chart = m_chars[subid];
			QValueAxis * ax = m_char_axis_x[subid];
			QValueAxis * ay = m_char_axis_y[subid];

			const double seed_x = max_x - min_x;
			const double seed_y = max_y - min_y;
			double new_max_x = ax->max(), new_max_y = ay->max();
			double new_min_x = ax->min(), new_min_y = ay->min();

			bool needup_x = false,needup_y = false;

M
manjaro-xfce 已提交
465 466
			if (max_x != new_max_x)	new_max_x = max_x ,needup_x=true;
			//if (max_x *1.2 < new_max_x)	new_max_x = max_x + seed_x * 0.2,needup_x=true;
丁劲犇's avatar
丁劲犇 已提交
467 468 469 470

			if (max_y > new_max_y)	new_max_y = max_y + seed_y * 0.2,needup_y=true;
			if (max_y *1.2 < new_max_y)	new_max_y = max_y + seed_y * 0.2,needup_y=true;

M
manjaro-xfce 已提交
471 472
			if (min_x != new_min_x)	new_min_x = min_x,needup_x=true;
			//if (min_x > new_min_x * 1.2)	new_min_x = min_x,needup_x=true;
丁劲犇's avatar
丁劲犇 已提交
473 474 475 476

			if (min_y < new_min_y)	new_min_y = min_y,needup_y=true;
			if (min_y > new_min_y * 1.2)	new_min_y = min_y,needup_y=true;

丁劲犇's avatar
丁劲犇 已提交
477 478 479 480
			if (max_x >=1e9 || max_x <-1e9 || max_y >1e9 ||max_y < -1e9 || max_x - min_x <1e-8)
				needup_x = false,needup_y = false;
			if (min_y >=1e9 || min_y <-1e9 || min_x >1e9 ||min_x < -1e9|| max_y - min_y <1e-8)
				needup_x = false,needup_y = false;
丁劲犇's avatar
丁劲犇 已提交
481

M
manjaro-xfce 已提交
482

D
dev 已提交
483
			if (ui->checkBox_auto_reser->isChecked() || needup_x || needup_y)
丁劲犇's avatar
丁劲犇 已提交
484 485
			{
				if (needup_x==true)
D
dev 已提交
486 487 488 489 490
				{
					double cumin = ax->min();
					double cumax = ax->max();
					ax->setRange(new_min_x<cumin?new_min_x:cumin,new_max_x > cumax?new_max_x:cumax);
				}
丁劲犇's avatar
丁劲犇 已提交
491
				if (needup_y==true)
D
dev 已提交
492 493 494 495 496
				{
					double cumin = ay->min();
					double cumax = ay->max();
					ay->setRange(new_min_y<cumin?new_min_y:cumin,new_max_y > cumax?new_max_y:cumax);
				}
丁劲犇's avatar
丁劲犇 已提交
497 498
			}
		}
M
manjaro-xfce 已提交
499
		refid = startTimer(25);
丁劲犇's avatar
丁劲犇 已提交
500 501 502
	}
}