1
/***************************************************************************
2
* Copyright (C) 2020 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
24
#include "plotdisplay.h"
25
#include "mainwindow.h"
26
#include "circuitview.h"
27
#include "simulator.h"
29
PlotDisplay::PlotDisplay( QWidget* parent )
32
for( int i=0; i<2; i++ )
39
m_foregroundA[i] = QPixmap( 100, 100 );
40
m_foregroundB[i] = QPixmap( 100, 100 );
41
m_foreground[i] = &m_foregroundA[i];
44
m_background = QPixmap( 100, 100 );
46
double fontScale = MainWindow::self()->fontScale();
47
m_fontB.setPixelSize(int(8*fontScale));
48
m_fontB.setBold(true);
49
m_fontS.setPixelSize(int(7*fontScale));
51
m_scaleColor[0] = QColor( 90, 90, 90 );
52
m_scaleColor[1] = QColor( 130, 130, 130 );
53
m_scaleColor[2] = QColor( 230, 230, 230 );
54
m_color[0] = QColor( 240, 240, 100 ); //QColor( 190, 190, 0 );
55
m_color[1] = QColor( 220, 220, 255 ); //QColor( 255, 110, 50 );
56
m_color[2] = QColor( 255, 255, 255 );
58
setMouseTracking(true);
61
QSize PlotDisplay::minimumSizeHint() const { return QSize( m_width, m_height ); }
63
QSize PlotDisplay::sizeHint() const { return QSize( m_width, m_height ); }
65
void PlotDisplay::setSize( int width, int height )
67
m_width = width+6*m_margin;
68
m_height = height+2*m_margin;
74
setFixedSize( m_width, m_height );
75
m_background = m_background.scaled( m_width, m_height );
77
for( int i=0; i<2; i++ )
79
m_foregroundA[i] = m_foregroundA[i].scaled( m_sizeX+2, m_sizeY+2 );
80
m_foregroundB[i] = m_foregroundB[i].scaled( m_sizeX+2, m_sizeY+2 );
87
QPainter p( &m_background );
92
void PlotDisplay::setMaxMin( int ch, double max, double min )
97
double ampli = max-min;
99
m_scaleY[ch] = ampli/(m_vTick[ch]*10);
103
void PlotDisplay::setData( int ch, QList<QPointF>* data )
109
void PlotDisplay::setXFrame( uint64_t tf )
111
m_scaleX = (double)m_sizeX/(double)tf;
115
void PlotDisplay::setVTick( int ch, double tick )
118
m_scaleY[ch] = m_ampli[ch]/(tick*10);
122
void PlotDisplay::setVPos( int ch, double vPos )
128
void PlotDisplay::setHPos( int ch, int64_t hPos )
134
void PlotDisplay::updateValues()
136
m_hCenter = (double)m_width/2;
137
m_vCenter = (double)m_height/2;
138
m_ceroX = m_margin*3;
139
m_endX = m_width-m_ceroX;
141
m_endS = m_height-m_margin;
143
m_linWi = (4+6*m_sizeY/200)/10;
145
for( int i=0; i<2; i++ )
147
m_endY[i] = m_vCenter+(m_height-m_margin*2)*m_scaleY[i]/2;
148
m_midV[i] = (m_vMax[i]+m_vMin[i])/2;
149
m_sclY[i] = m_sizeY/(m_vTick[i]*10);
150
m_posY[i] = m_vMin[i]+m_vPos[i];
151
m_vMaxPos[i] = m_endY[i]-(m_vMax[i]-m_posY[i])*m_sclY[i];
152
m_vMinPos[i] = m_endY[i]-(m_vMin[i]-m_posY[i])*m_sclY[i];
157
void PlotDisplay::drawBackground( QPainter* p )
159
//p->setRenderHint( QPainter::Antialiasing, true );
161
p->setBrush( QColor( 0, 0, 0 ) );
162
p->drawRoundRect(0, 0, m_width, m_height, 7, 7 );
164
QPen pen( m_scaleColor[0], 1.5*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
166
for( double i=m_ceroY; i<=m_endS; i+=m_sizeY/10. )
167
p->drawLine( QPointF(m_ceroX, i), QPointF(m_endX, i) ); //Horizontal lines
169
for( double i=m_lineX; i<=m_endX; i+=m_sizeX/10. )
170
p->drawLine( QPointF(i, m_ceroY), QPointF(i, m_endS) ); //Vertical lines
172
QPen pen2( m_scaleColor[1], 1*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
175
double m = 2*m_linWi;
176
for( double i=m_ceroY; i<=m_endS; i+=m_sizeY/50. )
177
p->drawLine( QPointF(m_hCenter-m, i), QPointF(m_hCenter+m, i) ); //Horizontal Marks
179
for( double i=m_lineX; i<=m_endX; i+=m_sizeX/50. )
180
p->drawLine( QPointF(i, m_vCenter-m), QPointF(i, m_vCenter+m) ); //Vertical Marks
182
QPen pen3( m_scaleColor[1], 1.5*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
184
p->drawLine( m_ceroX, m_vCenter, m_endX, m_vCenter ); //Horizontal Center line
185
p->drawLine( m_hCenter, m_ceroY, m_hCenter, m_endS ); //Vertical Center line
188
/*void PlotDisplay::clear( int ch )
190
//for( int i=0; i<2; i++ )
191
m_foregroundA[ch].fill( Qt::transparent );
192
m_foregroundB[ch].fill( Qt::transparent );
195
void PlotDisplay::paintEvent( QPaintEvent* /* event */ )
199
drawBackground( &p );
200
p.setRenderHint( QPainter::Antialiasing, true );
208
for( int i=0; i<2; i++ )
212
QPen pen1( m_color[i], 0.5*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
215
p.drawLine( m_ceroX, m_vMaxPos[i], m_endX, m_vMaxPos[i] ); //Horizontal Max V line
216
p.drawLine( m_ceroX, m_vMinPos[i], m_endX, m_vMinPos[i] ); //Horizontal Min V line
219
QPen pen2( m_color[i], 2*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
223
double filter = m_filter*m_sclY[i];//m_ampli[i]*m_sclY[i]/4; //
225
for( QPointF P2 : *m_data[i] )
227
x2 = P2.x()*m_scaleX+m_margin*3+m_hPos[i]*m_scaleX;
228
y2 = m_endY[i]-(P2.y()-m_posY[i])*m_sclY[i];
232
if( k == 0 ) // First Point
234
if( x2 > 0 ) P1 = QPointF( 0, y2 ); // We need another point before
242
if(( x1<0 && x2<0 ) ||( x1>m_width && x2>m_width )) // Out of display
246
if( fabs( y2-y1 ) <= filter ) p.drawLine( P1, P2 );
249
QPointF PM = QPointF( x2, y1 );
250
p.drawLine( P1, PM );
251
p.drawLine( PM, P2 );
256
// Draw Rects to crop data plots
257
p.fillRect( 0, 0, m_margin*3, m_height, QColor( 10, 15, 50 ) );
258
p.fillRect( m_sizeX+m_margin*3, 0, m_margin*3, m_height, QColor( 10, 15, 50 ) );
261
for( int i=0; i<2; i++ ) // Draw scales, text, Max-Min
265
int xPos = 2+i*(m_sizeX+3*m_margin);
267
QPen pen3( m_color[i], 1.5*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
269
p.setFont( m_fontS );
271
// Draw Background scale values
273
for( double j=m_ceroY; j<=m_endS; j+=m_sizeY/10 )
275
//p.drawLine( m_ceroX, i, m_endX, i ); //Horizontal lines
278
double volTick = (m_vTick[i]*5-m_vTick[i]*s++);
280
//volTick = ((double)vt+0.5)/100;
281
if ( volTick < 10) decs = 2;
282
else if( volTick < 100) decs = 1;
284
p.drawText( xPos, j-5, m_width, 10, Qt::AlignVCenter, QString::number( volTick,'f', decs) );
287
/*p.drawText( xPos, 3, m_width, 10, Qt::AlignVCenter, QString::number( m_midV[i]+m_vTick[i]*5,'f', 2) );
288
p.drawText( xPos, m_vCenter-5, m_width, m_margin, Qt::AlignVCenter, QString::number( m_midV[i] ,'f', 2) );
289
p.drawText( xPos, m_endS-5, m_width, 10, Qt::AlignVCenter, QString::number( m_midV[i]-m_vTick[i]*5,'f', 2) );
291
// Draw Rects behind Max & Min Numbers
292
p.fillRect( xPos, m_vMaxPos[i]-4, 21, 8, QColor( 10, 15, 50 ) );
293
p.fillRect( xPos, m_vMinPos[i]-4, 21, 8, QColor( 10, 15, 50 ) );
295
// Draw Max & Min Values
296
p.setFont( m_fontB );
297
//p.setPen( m_color[i] );
298
p.drawText( xPos, m_vMaxPos[i]-5, m_width, 10, Qt::AlignVCenter, QString::number(m_vMax[i],'f', 2) );
299
p.drawText( xPos, m_vMinPos[i]-5, m_width, 10, Qt::AlignVCenter, QString::number(m_vMin[i],'f', 2) );
302
QPoint cPos = QCursor::pos()-mapToGlobal( QPoint(0, 0) );
303
qreal scale = CircuitView::self()->getScale();
304
int cursorX = cPos.x()/scale;
305
int cursorY = cPos.y()/scale;
307
if( (cursorX > m_ceroX) && (cursorX < m_endX ) // Draw Cursor
308
&& (cursorY > 0) && (cursorY < m_height) )
310
QPen pen1( m_scaleColor[2], 1*m_linWi, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin );
313
p.drawLine( cursorX, 0, cursorX, m_height ); //Horizontal Cursor V line
314
p.drawLine( m_ceroX, cursorY, m_endX, cursorY ); //Horizontal Cursor H line m_scaleX[i]
316
pen1.setColor( Qt::white );
318
double volt = (double)(cursorX-m_hCenter)/m_scaleX/1e6;
319
p.drawText( cursorX-25, cursorY-10, 50, 10, Qt::AlignLeft, QString::number( volt,'f', 1)+" us" );
325
#include "moc_plotdisplay.cpp"