diff widgets/Plotter.cpp @ 211:fbd4905ada5e

add plotter
author benoitrigolleau
date Fri, 01 Feb 2008 15:46:16 +0000
parents
children 7b2a7880a501
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/Plotter.cpp	Fri Feb 01 15:46:16 2008 +0000
@@ -0,0 +1,179 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*	Sound Access	
+		EASAIER client application.	
+		Silogic 2007. Benoit Rigolleau. 
+	
+	This program is free software; you can redistribute it and/or    
+	modify it under the terms of the GNU General Public License as    
+	published by the Free Software Foundation; either version 2 of the    
+	License, or (at your option) any later version.  See the file    
+	COPYING included with this distribution for more information.
+*/
+
+#include  "Plotter.h"  
+#include <QtGui>
+
+Plotter::Plotter(QWidget *parent):
+m_signalWidth(400),
+m_signalHeight(180),
+m_margin(5),
+m_curveMaskActive(false)
+{
+	m_curveMask = new int[m_signalWidth];
+	for(int i = 0 ; i < m_signalWidth ; i++){
+		m_curveMask[i] = 0;
+	}
+
+
+	m_curve.push_back(QPoint(399,179));
+	m_curve.push_back(QPoint(300,20));
+	m_curve.push_back(QPoint(60,150));
+	m_curve.push_back(QPoint(10,30));
+	m_curve.push_back(QPoint(0,0));
+
+
+	refreshPixmap();
+
+}
+
+void Plotter::setCurveData(const QVector<QPoint> &data){
+	m_curve = data;
+	refreshPixmap();
+}
+
+void Plotter::clearCurve(){
+	m_curve.clear();
+	refreshPixmap();
+}
+
+void Plotter::paintEvent(QPaintEvent *event){
+	QStylePainter painter(this);
+    painter.drawPixmap(0, 0, m_pixmap);
+}
+
+void Plotter::resizeEvent(QResizeEvent *event){
+	refreshPixmap();
+}
+
+void Plotter::mousePressEvent(QMouseEvent *event){
+	QRect rect(m_margin, m_margin,
+               m_signalWidth + m_margin, m_signalHeight + m_margin);
+
+    if (event->button() == Qt::LeftButton) {
+        if (rect.contains(event->pos())) {
+            m_curveMaskActive = true;
+			int x = event->pos().x()- m_margin;
+			int y = event->pos().y()- m_margin;
+			m_curveMask[x] = y;
+			m_lastPoint.setX(x);
+			m_lastPoint.setY(y);
+            setCursor(Qt::CrossCursor);
+			refreshPixmap();
+        }
+    }
+}
+
+void Plotter::mouseMoveEvent(QMouseEvent *event){
+	QRect rect(m_margin, m_margin,
+               m_signalWidth + m_margin , m_signalHeight + m_margin);
+
+	if(m_curveMaskActive && rect.contains(event->pos())){
+		int x = event->pos().x() - m_margin;
+		int y = event->pos().y() - m_margin;
+		if(y>m_signalHeight-1)
+			y= m_signalHeight-1;
+		int xlast = m_lastPoint.x();
+		int ylast = m_lastPoint.y();
+
+		//regul mask curve
+		int minx, maxx, miny, maxy;
+		if(x>xlast){
+			minx = xlast;
+			maxx = x;
+		}else{
+			minx = x;
+			maxx = xlast;
+		}
+		if(y>ylast){
+			miny = ylast;
+			maxy = y;
+		}else{
+			miny = y;
+			maxy = ylast;
+		}
+
+		if((maxx - minx)>1){
+			double incr = (double)(maxy - miny)/(double)(maxx - minx);
+			if(x>xlast){
+				for (int i = xlast + 1 ; i < x ; i++){
+					if(ylast > y){
+						m_curveMask[i] = ylast - (i-xlast)*incr;
+					}else{
+						m_curveMask[i] = ylast + (i-xlast)*incr;
+					}
+				}
+			}else{
+				for (int i = xlast - 1 ; i > x ; i--){
+					if(ylast > y){
+						m_curveMask[i] = ylast + (i-xlast)*incr;
+					}else{
+						m_curveMask[i] = ylast - (i-xlast)*incr;
+					}
+				}
+			}
+		}
+		m_curveMask[x] = y;
+		m_lastPoint.setX(x);
+		m_lastPoint.setY(y);
+		refreshPixmap();
+	}
+}
+
+void Plotter::mouseReleaseEvent(QMouseEvent *event){
+	 if ((event->button() == Qt::LeftButton) && m_curveMaskActive) {
+        m_curveMaskActive = false;
+        unsetCursor();
+	 }
+}
+
+void Plotter::drawGrid(QPainter *painter){
+	QPixmap pm(":icons/grid.png");
+	painter->drawPixmap(0,0,pm);
+}
+
+void Plotter::drawCurve(QPainter *painter){
+	QPolygonF polyline(m_signalWidth);
+	for(int i = 0 ; i < m_curve.count() ; i++){
+		polyline[i] = QPoint(m_curve.at(i).x()+m_margin, m_curve.at(i).y()+m_margin);
+	}
+	painter->setPen(Qt::green);
+    painter->drawPolyline(polyline);
+}
+
+void Plotter::drawMaskCurve(QPainter *painter){
+
+	QPolygonF polyline(m_signalWidth);
+
+	for(int i = 0 ; i < m_signalWidth ; i++){
+		polyline[i] = QPoint(i+m_margin, m_curveMask[i]+m_margin);
+	}
+	painter->setPen(Qt::red);
+    painter->drawPolyline(polyline);
+}
+
+void Plotter::refreshPixmap(){
+    m_pixmap = QPixmap(size());
+    m_pixmap.fill(this, 0, 0);
+
+    QPainter painter(&m_pixmap);
+    painter.initFrom(this);
+    drawGrid(&painter);
+    drawCurve(&painter);
+	drawMaskCurve(&painter);
+    update();
+}
+
+void Plotter::setMargin(int margin){
+	m_margin = margin;
+}
\ No newline at end of file