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 92 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 121 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/* Graph
* Versin 0.1
* Date 29 janv 2015
* Auteur Sangfeust
*
* Classe de gestion pour l'affichage d'un graphique avec des axes multiples!!
* Il semble qu'il faille faire très attention à la gestion des CANVAS et à la superposition des
* contrôles sur le PLOT car cela peut tout faire foirer !!
*
* Info:
* Cette version est adapté pour l'affichage d'un log AKEH !
* Il faut partir de cette base pour d'autre graphique!!
*
* How to:
* TBD
*
**/
#include <qwt_scale_engine.h>
#include <qwt_symbol.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_legend.h>
#include <qwt_text.h>
#include <qwt_plot_canvas.h>
#include <qwt_legend_label.h>
#include <qwt_picker_machine.h>
#include <qwt_scale_widget.h>
#include "graph.h"
///
/// \brief The Zoomer class
/// Il faut que je travaille sur cette classe pour la gestion du zoom.
///
class Zoomer: public QwtPlotZoomer
{
public:
Zoomer( int xAxis, int yAxis, QWidget *canvas ):
QwtPlotZoomer( xAxis, yAxis, canvas )
{
setTrackerMode( QwtPicker::AlwaysOff );
setRubberBand( QwtPicker::NoRubberBand );
// RightButton: zoom out by 1
// Ctrl+RightButton: zoom out to full size
setMousePattern( QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlModifier );
setMousePattern( QwtEventPattern::MouseSelect3,
Qt::RightButton );
}
protected:
virtual QwtText trackerTextF( const QPointF &pos ) const
{
//const QDateTime dt = QwtDate::toDateTime( pos.x() );
QString s;
// s += QwtDate::toString( QwtDate::toDateTime( pos.x() ),
// "MMM dd hh:mm ", QwtDate::FirstThursday );
s += QString("y= %1 - %2ms").arg(pos.y()).arg(pos.x());
QwtText text( s );
text.setColor( Qt::black );
QColor c = rubberBandPen().color();
text.setBorderPen( QPen( c ) );
text.setBorderRadius( 6 );
c.setAlpha( 170 );
text.setBackgroundBrush( c );
return text;
}
};
Graph::Graph( QWidget *parent ):
QwtPlot( parent )
{
setAutoReplot( false );
setTitle("Title"); //Think to set the title
// Canvas
// The canvas picker handles all mouse and key
// events on the plot canvas
// TODO rajouter ici la définition du canvas / define the canvas here
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
canvas->setLineWidth( 1 );
canvas->setPalette( Qt::white );
setCanvas( canvas );
// Legend
QwtLegend *legend = new QwtLegend;
legend->setDefaultItemMode( QwtLegendData::Checkable );
insertLegend( legend, QwtPlot::TopLegend );
// Grid
QwtPlotGrid *grid = new QwtPlotGrid;
grid->enableX( true );
grid->enableXMin( true );
grid->enableY( true );
grid->enableYMin( false );
grid->setMajorPen( Qt::black, 0, Qt::DotLine );
grid->setMinorPen( Qt::gray, 0, Qt::DotLine );
grid->attach( this );
// Axes
// TODO définir la forme des axes ici / set the axis here
enableAxis( QwtPlot::yRight );
setAxesCount( QwtAxis::yLeft, 2 );
setAxesCount( QwtAxis::yRight, 2 );
// Axe X bottom
setAxisTitle( QwtPlot::xBottom, "Time [ms]" );
// setAxisScale( QwtPlot::xBottom, 0.0, 50000.0 ); //On laisse le std
// setAxisMaxMajor( QwtAxis::xBottom, 0 ); // Inutile
// setAxisMaxMinor( QwtAxis::xBottom, 35000 ); // Inutile
setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
// Axe Left 0 : Courant
setAxisTitle(QwtAxis::yLeft, "Courant (A)");
setAxisLabelRotation(QwtAxis::yLeft, -50);
setAxisFont(QwtAxis::yLeft, QFont("Calibri", 8, false));
QwtScaleWidget *scalewidget = axisWidget(QwtAxisId(QwtAxis::yLeft, 0));
QPalette pal = scalewidget->palette();
pal.setColor(QPalette::Text, QColor( Qt::blue ));
scalewidget->setPalette(pal);
// Axe Left 1 : Frequence
setAxisTitle(QwtAxisId(QwtAxis::yLeft, 1), "Frequence (Hz)");
setAxisLabelRotation(QwtAxisId(QwtAxis::yLeft, 1), -50);
setAxisFont(QwtAxisId(QwtAxis::yLeft, 1), QFont("Calibri", 8, false));
scalewidget = axisWidget(QwtAxisId(QwtAxis::yLeft, 1));
pal = scalewidget->palette();
pal.setColor(QPalette::Text, QColor( Qt::red ));
scalewidget->setPalette(pal);
// Axe Right 0 : Position
setAxisTitle(QwtAxis::yRight, "Position (°)");
setAxisLabelRotation(QwtAxis::yRight, -50);
setAxisLabelAlignment(QwtAxis::yRight, Qt::AlignVCenter | Qt::AlignHCenter);
setAxisFont(QwtAxis::yRight, QFont("Calibri", 8, false));
scalewidget = axisWidget(QwtAxisId(QwtAxis::yRight, 0));
pal = scalewidget->palette();
pal.setColor(QPalette::Text, QColor( Qt::black ));
scalewidget->setPalette(pal);
// Axe Right 1 : Temperature
setAxisTitle(QwtAxisId(QwtAxis::yRight, 1), "Temperature (°C)");
setAxisLabelAlignment(QwtAxisId(QwtAxis::yRight, 1), Qt::AlignVCenter | Qt::AlignHCenter);
setAxisLabelRotation(QwtAxisId(QwtAxis::yRight, 1), -50);
setAxisFont(QwtAxisId(QwtAxis::yRight, 1), QFont("Calibri", 8, false));
setAxisScale( QwtAxisId(QwtAxis::yRight, 1), 0.0, 140.0 ); //On laisse le std
scalewidget = axisWidget(QwtAxisId(QwtAxis::yRight, 1));
pal = scalewidget->palette();
pal.setColor(QPalette::Text, QColor( Qt::green ));
scalewidget->setPalette(pal);
// NOTE need to find how to use this...
//setAxisScaleEngine( QwtPlot::xBottom, new QwtScaleEngine() );
// Zoom
_zoomer[0] = new Zoomer( QwtAxis::xBottom,
QwtAxisId(QwtAxis::yLeft, 0).id,
canvas );
_zoomer[0]->setRubberBand( QwtPicker::RectRubberBand );
_zoomer[0]->setRubberBandPen( QColor( Qt::green ) );
_zoomer[0]->setTrackerMode( QwtPicker::ActiveOnly );
_zoomer[0]->setTrackerPen( QColor( Qt::white ) );
_zoomer[1] = new Zoomer( QwtAxis::xBottom,
QwtAxisId(QwtAxis::yLeft, 1).id,
canvas );
_zoomer[2] = new Zoomer( QwtAxis::xTop,
QwtAxisId(QwtAxis::yRight, 0).id,
canvas );
_zoomer[3] = new Zoomer( QwtAxis::xTop,
QwtAxisId(QwtAxis::yRight, 1).id,
canvas );
// Panner
// TODO je ne sais pas trop à quoi cela sert ...
_panner = new QwtPlotPanner( canvas );
_panner->setMouseButton( Qt::MidButton );
_picker = new QwtPlotPicker( QwtAxis::xBottom, QwtAxis::yLeft,
QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
canvas );
_picker->setStateMachine( new QwtPickerDragPointMachine() );
_picker->setRubberBandPen( QColor( Qt::green ) );
_picker->setRubberBand( QwtPicker::CrossRubberBand );
_picker->setTrackerPen( QColor( Qt::black ) );
// Curves
// TODO cette section peut être implémenter en cas de graphique statique
_pos = new QwtPlotCurve( "Position" );
_pos->setPen(Qt::black, 0.0, Qt::SolidLine);
_pos->setYAxis(QwtAxisId(QwtAxis::yRight, 0));
_pos->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_pos->attach( this );
_ia = new QwtPlotCurve( "Courant Ia" );
_ia->setPen(Qt::darkCyan, 0.0, Qt::DotLine);
_ia->setYAxis(QwtAxisId(QwtAxis::yLeft, 0));
_ia->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_ia->attach( this );
_ib = new QwtPlotCurve( "Courant Ib" );
_ib->setPen(Qt::darkCyan, 0.0, Qt::DotLine);
_ib->setYAxis(QwtAxisId(QwtAxis::yLeft, 0));
_ib->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_ib->attach( this );
_ief = new QwtPlotCurve( "Courant Efficace" );
_ief->setPen(Qt::blue, 0.0, Qt::SolidLine);
_ief->setYAxis(QwtAxisId(QwtAxis::yLeft, 0));
_ief->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_ief->attach( this );
_freq = new QwtPlotCurve( "Frequence" );
_freq->setPen(Qt::red, 0.0, Qt::SolidLine);
_freq->setYAxis(QwtAxisId(QwtAxis::yLeft, 1));
_freq->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_freq->attach( this );
_deg = new QwtPlotCurve( "Temperature" );
_deg->setPen(Qt::green, 0.0, Qt::SolidLine);
_deg->setYAxis(QwtAxisId(QwtAxis::yRight, 1));
_deg->setLegendAttribute( QwtPlotCurve::LegendShowLine );
_deg->attach( this );
//marker
d_marker1 = new QwtPlotMarker();
d_marker1->setValue( 0.0, 0.0 );
d_marker1->setLineStyle( QwtPlotMarker::VLine );
d_marker1->setLabelAlignment( Qt::AlignRight | Qt::AlignBottom );
d_marker1->setLinePen( Qt::green, 0, Qt::DashDotLine );
d_marker1->attach( this );
d_marker2 = new QwtPlotMarker();
d_marker2->setLineStyle( QwtPlotMarker::HLine );
d_marker2->setLabelAlignment( Qt::AlignRight | Qt::AlignBottom );
d_marker2->setLinePen( QColor( 200, 150, 0 ), 0, Qt::DashDotLine );
d_marker2->setSymbol( new QwtSymbol( QwtSymbol::Diamond,
QColor( Qt::yellow ), QColor( Qt::green ), QSize( 8, 8 ) ) );
d_marker2->attach( this );
enableZoomMode(false);
setAutoReplot( true );
connect( legend, SIGNAL( checked( const QVariant &, bool, int ) ),
SLOT( legendChecked( const QVariant &, bool ) ) );
connect( _picker, SIGNAL( moved( const QPoint & ) ),
SLOT( moved( const QPoint & ) ) );
connect( _picker, SIGNAL( selected( const QPolygon & ) ),
SLOT( selected( const QPolygon & ) ) );
}
Graph::~Graph()
{
}
///
/// \brief AkGraphView::showData
/// \param frequency
/// \param amplitude
/// \param phase
/// \param count
/// TODO configurer cette fonction suivant le graphique voulu
/// choix du nombre de courbe
/// choix du nom des paramètres ...
///
void Graph::showData(const double *X, const double *position, const double *ia,
const double *ib, const double *ief, const double *frequency, const double *temperature, int count )
{
_pos->setSamples( X, position, count);
_ia->setSamples( X, ia, count);
_ib->setSamples( X, ib, count);
_ief->setSamples( X, ief, count);
_freq->setSamples( X, frequency, count);
_deg->setSamples( X, temperature, count);
}
///
/// \brief Graph::showData
/// \param X
/// \param position
/// \param ia
/// \param ib
/// \param ief
/// \param frequency
/// \param temperature
/// \param count
///
/// NOTE personal method to show the data to be change to what you need
///
void Graph::showData( QVector<double> *X, QVector<double> *position, QVector<double> *ia,
QVector<double> *ib, QVector<double> *ief, QVector<double> *frequency, QVector<double> *temperature, int count )
{
_pos->setSamples( X->data(), position->data() , position->size() );
_ia->setSamples( X->data(), ia->data() , ia->size());
_ib->setSamples( X->data(), ib->data() , ib->size());
_ief->setSamples( X->data(), ief->data() , ief->size());
_freq->setSamples( X->data(), frequency->data() , frequency->size());
_deg->setSamples( X->data(), temperature->data() , temperature->size());
}
///
/// \brief AkGraphView::showPeak
/// \param pos
/// \param value
/// NOTE exemple pour modifier les markers
/// NOTE example to change marker
/// TODO to be check
///
void Graph::showPeak(double pos, double value)
{
QString label;
label.sprintf( "Peak: %.3g dB", value );
QwtText text( label );
text.setFont( QFont( "Helvetica", 10, QFont::Bold ) );
text.setColor( QColor( 200, 150, 0 ) );
d_marker2->setValue( pos, value );
d_marker2->setLabel( text );
}
///
/// \brief AkGraphView::show3dB
/// \param freq
/// NOTE exemple pour modifier les markers
/// NOTE example to change marker
/// TODO to be check
///
void Graph::show3dB(double freq)
{
QString label;
label.sprintf( "-3 dB at f = %.3g", freq );
QwtText text( label );
text.setFont( QFont( "Helvetica", 10, QFont::Bold ) );
text.setColor( Qt::green );
d_marker1->setValue( freq, 0.0 );
d_marker1->setLabel( text );
}
///
/// \brief AkGraphView::legendChecked
/// \param itemInfo
/// \param on
///
/// Slot de gestion du clic sur la légende.
/// Control slot for the legend
///
void Graph::legendChecked( const QVariant &itemInfo, bool on )
{
QwtPlotItem *plotItem = infoToItem( itemInfo );
if ( plotItem )
showCurve( plotItem, on );
}
///
/// \brief AkGraphView::showCurve
/// \param item
/// \param on
///
/// Gestion de l'affichage des courbe ou non si la légende est check.
/// Show/Hide curbe if the legend is checked
///
void Graph::showCurve( QwtPlotItem *item, bool on )
{
item->setVisible( on );
QwtLegend *lgd = qobject_cast<QwtLegend *>( legend() );
QList<QWidget *> legendWidgets =
lgd->legendWidgets( itemToInfo( item ) );
if ( legendWidgets.size() == 1 )
{
QwtLegendLabel *legendLabel =
qobject_cast<QwtLegendLabel *>( legendWidgets[0] );
if ( legendLabel )
legendLabel->setChecked( on );
}
replot();
}
///
/// \brief AkGraphView::enableZoomMode
/// \param on
/// Permet ou non de zoomer sur le graphique
void Graph::enableZoomMode(bool on)
{
_panner->setEnabled( on );
_zoomer[0]->setEnabled( on );
_zoomer[0]->zoom( 0 );
_zoomer[1]->setEnabled( on );
_zoomer[1]->zoom( 0 );
// _zoomer[2]->setEnabled( on );
// _zoomer[2]->zoom( 0 );
// _zoomer[3]->setEnabled( on );
// _zoomer[3]->zoom( 0 );
_picker->setEnabled( !on );
showInfo();
}
///
/// \brief AkGraphView::showInfo
/// \param text
/// Affiche la valeur sous le curseur
/// TODO optimiser cette fonction peut être très utile !!
void Graph::showInfo( QString text )
{
if ( text == QString::null )
{
if ( _picker->rubberBand() )
text = "Cursor Pos: Press left mouse button in plot region";
else
text = "Zoom: Press mouse button and drag";
}
#ifndef QT_NO_STATUSBAR
emit showCursor( text );
#endif
}
///
/// \brief AkGraphView::selected
///
void Graph::selected( const QPolygon & )
{
showInfo();
}
///
/// \brief AkGraphView::moved
/// \param pos
///
void Graph::moved( const QPoint &pos )
{
QString info;
info.sprintf( "Pos=%g, Ia=%g, Ib=%g, Ief=%g, F=%g, T=%g",
// invTransform( QwtAxis::xBottom, pos.x() ),
invTransform( QwtAxisId(QwtAxis::yRight, 0).id , pos.y() ),
invTransform( QwtAxisId(QwtAxis::yLeft , 0).id , pos.y() ),
invTransform( QwtAxisId(QwtAxis::yLeft , 0).id , pos.y() ),
invTransform( QwtAxisId(QwtAxis::yLeft , 0).id , pos.y() ),
invTransform( QwtAxisId(QwtAxis::yLeft , 1).id , pos.y() ),
invTransform( QwtAxisId(QwtAxis::yRight, 1).id , pos.y() )
);
showInfo( info );
} |
Partager