Mercurial > hg > easaier-soundaccess
comparison widgets/Plotter.cpp @ 211:fbd4905ada5e
add plotter
author | benoitrigolleau |
---|---|
date | Fri, 01 Feb 2008 15:46:16 +0000 |
parents | |
children | 7b2a7880a501 |
comparison
equal
deleted
inserted
replaced
210:bf1d6386eb4b | 211:fbd4905ada5e |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* Sound Access | |
4 EASAIER client application. | |
5 Silogic 2007. Benoit Rigolleau. | |
6 | |
7 This program is free software; you can redistribute it and/or | |
8 modify it under the terms of the GNU General Public License as | |
9 published by the Free Software Foundation; either version 2 of the | |
10 License, or (at your option) any later version. See the file | |
11 COPYING included with this distribution for more information. | |
12 */ | |
13 | |
14 #include "Plotter.h" | |
15 #include <QtGui> | |
16 | |
17 Plotter::Plotter(QWidget *parent): | |
18 m_signalWidth(400), | |
19 m_signalHeight(180), | |
20 m_margin(5), | |
21 m_curveMaskActive(false) | |
22 { | |
23 m_curveMask = new int[m_signalWidth]; | |
24 for(int i = 0 ; i < m_signalWidth ; i++){ | |
25 m_curveMask[i] = 0; | |
26 } | |
27 | |
28 | |
29 m_curve.push_back(QPoint(399,179)); | |
30 m_curve.push_back(QPoint(300,20)); | |
31 m_curve.push_back(QPoint(60,150)); | |
32 m_curve.push_back(QPoint(10,30)); | |
33 m_curve.push_back(QPoint(0,0)); | |
34 | |
35 | |
36 refreshPixmap(); | |
37 | |
38 } | |
39 | |
40 void Plotter::setCurveData(const QVector<QPoint> &data){ | |
41 m_curve = data; | |
42 refreshPixmap(); | |
43 } | |
44 | |
45 void Plotter::clearCurve(){ | |
46 m_curve.clear(); | |
47 refreshPixmap(); | |
48 } | |
49 | |
50 void Plotter::paintEvent(QPaintEvent *event){ | |
51 QStylePainter painter(this); | |
52 painter.drawPixmap(0, 0, m_pixmap); | |
53 } | |
54 | |
55 void Plotter::resizeEvent(QResizeEvent *event){ | |
56 refreshPixmap(); | |
57 } | |
58 | |
59 void Plotter::mousePressEvent(QMouseEvent *event){ | |
60 QRect rect(m_margin, m_margin, | |
61 m_signalWidth + m_margin, m_signalHeight + m_margin); | |
62 | |
63 if (event->button() == Qt::LeftButton) { | |
64 if (rect.contains(event->pos())) { | |
65 m_curveMaskActive = true; | |
66 int x = event->pos().x()- m_margin; | |
67 int y = event->pos().y()- m_margin; | |
68 m_curveMask[x] = y; | |
69 m_lastPoint.setX(x); | |
70 m_lastPoint.setY(y); | |
71 setCursor(Qt::CrossCursor); | |
72 refreshPixmap(); | |
73 } | |
74 } | |
75 } | |
76 | |
77 void Plotter::mouseMoveEvent(QMouseEvent *event){ | |
78 QRect rect(m_margin, m_margin, | |
79 m_signalWidth + m_margin , m_signalHeight + m_margin); | |
80 | |
81 if(m_curveMaskActive && rect.contains(event->pos())){ | |
82 int x = event->pos().x() - m_margin; | |
83 int y = event->pos().y() - m_margin; | |
84 if(y>m_signalHeight-1) | |
85 y= m_signalHeight-1; | |
86 int xlast = m_lastPoint.x(); | |
87 int ylast = m_lastPoint.y(); | |
88 | |
89 //regul mask curve | |
90 int minx, maxx, miny, maxy; | |
91 if(x>xlast){ | |
92 minx = xlast; | |
93 maxx = x; | |
94 }else{ | |
95 minx = x; | |
96 maxx = xlast; | |
97 } | |
98 if(y>ylast){ | |
99 miny = ylast; | |
100 maxy = y; | |
101 }else{ | |
102 miny = y; | |
103 maxy = ylast; | |
104 } | |
105 | |
106 if((maxx - minx)>1){ | |
107 double incr = (double)(maxy - miny)/(double)(maxx - minx); | |
108 if(x>xlast){ | |
109 for (int i = xlast + 1 ; i < x ; i++){ | |
110 if(ylast > y){ | |
111 m_curveMask[i] = ylast - (i-xlast)*incr; | |
112 }else{ | |
113 m_curveMask[i] = ylast + (i-xlast)*incr; | |
114 } | |
115 } | |
116 }else{ | |
117 for (int i = xlast - 1 ; i > x ; i--){ | |
118 if(ylast > y){ | |
119 m_curveMask[i] = ylast + (i-xlast)*incr; | |
120 }else{ | |
121 m_curveMask[i] = ylast - (i-xlast)*incr; | |
122 } | |
123 } | |
124 } | |
125 } | |
126 m_curveMask[x] = y; | |
127 m_lastPoint.setX(x); | |
128 m_lastPoint.setY(y); | |
129 refreshPixmap(); | |
130 } | |
131 } | |
132 | |
133 void Plotter::mouseReleaseEvent(QMouseEvent *event){ | |
134 if ((event->button() == Qt::LeftButton) && m_curveMaskActive) { | |
135 m_curveMaskActive = false; | |
136 unsetCursor(); | |
137 } | |
138 } | |
139 | |
140 void Plotter::drawGrid(QPainter *painter){ | |
141 QPixmap pm(":icons/grid.png"); | |
142 painter->drawPixmap(0,0,pm); | |
143 } | |
144 | |
145 void Plotter::drawCurve(QPainter *painter){ | |
146 QPolygonF polyline(m_signalWidth); | |
147 for(int i = 0 ; i < m_curve.count() ; i++){ | |
148 polyline[i] = QPoint(m_curve.at(i).x()+m_margin, m_curve.at(i).y()+m_margin); | |
149 } | |
150 painter->setPen(Qt::green); | |
151 painter->drawPolyline(polyline); | |
152 } | |
153 | |
154 void Plotter::drawMaskCurve(QPainter *painter){ | |
155 | |
156 QPolygonF polyline(m_signalWidth); | |
157 | |
158 for(int i = 0 ; i < m_signalWidth ; i++){ | |
159 polyline[i] = QPoint(i+m_margin, m_curveMask[i]+m_margin); | |
160 } | |
161 painter->setPen(Qt::red); | |
162 painter->drawPolyline(polyline); | |
163 } | |
164 | |
165 void Plotter::refreshPixmap(){ | |
166 m_pixmap = QPixmap(size()); | |
167 m_pixmap.fill(this, 0, 0); | |
168 | |
169 QPainter painter(&m_pixmap); | |
170 painter.initFrom(this); | |
171 drawGrid(&painter); | |
172 drawCurve(&painter); | |
173 drawMaskCurve(&painter); | |
174 update(); | |
175 } | |
176 | |
177 void Plotter::setMargin(int margin){ | |
178 m_margin = margin; | |
179 } |