-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqwt_plot.cpp
More file actions
656 lines (561 loc) · 16.8 KB
/
qwt_plot.cpp
File metadata and controls
656 lines (561 loc) · 16.8 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot.h"
#include "qwt_plot_dict.h"
#include "qwt_plot_layout.h"
#include "qwt_scale_widget.h"
#include "qwt_scale_engine.h"
#include "qwt_text_label.h"
#include "qwt_legend.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_plot_canvas.h"
#include <qpainter.h>
#include <qpointer.h>
#include <qpaintengine.h>
#include <qapplication.h>
#include <qevent.h>
class QwtPlot::PrivateData
{
public:
QPointer<QwtTextLabel> lblTitle;
QPointer<QwtPlotCanvas> canvas;
QPointer<QwtLegend> legend;
QwtPlotLayout *layout;
};
/*!
\brief Constructor
\param parent Parent widget
*/
QwtPlot::QwtPlot( QWidget *parent ):
QFrame( parent )
{
initPlot( QwtText() );
}
/*!
\brief Constructor
\param title Title text
\param parent Parent widget
*/
QwtPlot::QwtPlot( const QwtText &title, QWidget *parent ):
QFrame( parent )
{
initPlot( title );
}
//! Destructor
QwtPlot::~QwtPlot()
{
detachItems( autoDelete() );
delete d_data->layout;
deleteAxesData();
delete d_data;
}
/*!
\brief Initializes a QwtPlot instance
\param title Title text
*/
void QwtPlot::initPlot( const QwtText &title )
{
d_data = new PrivateData;
d_data->layout = new QwtPlotLayout;
d_data->lblTitle = new QwtTextLabel( title, this );
d_data->lblTitle->setObjectName( "QwtPlotTitle" );
d_data->lblTitle->setFont( QFont( fontInfo().family(), 14, QFont::Bold ) );
QwtText text( title );
text.setRenderFlags( Qt::AlignCenter | Qt::TextWordWrap );
d_data->lblTitle->setText( text );
d_data->legend = NULL;
initAxesData();
d_data->canvas = new QwtPlotCanvas( this );
d_data->canvas->setObjectName( "QwtPlotCanvas" );
d_data->canvas->setFrameStyle( QFrame::Panel | QFrame::Sunken );
d_data->canvas->setLineWidth( 2 );
updateTabOrder();
setSizePolicy( QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding );
resize( 200, 200 );
}
/*!
\brief Adds handling of layout requests
\param event Event
*/
bool QwtPlot::event( QEvent *event )
{
bool ok = QFrame::event( event );
switch ( event->type() )
{
case QEvent::LayoutRequest:
updateLayout();
break;
case QEvent::PolishRequest:
replot();
break;
default:;
}
return ok;
}
/*!
Change the plot's title
\param title New title
*/
void QwtPlot::setTitle( const QString &title )
{
if ( title != d_data->lblTitle->text().text() )
{
d_data->lblTitle->setText( title );
updateLayout();
}
}
/*!
Change the plot's title
\param title New title
*/
void QwtPlot::setTitle( const QwtText &title )
{
if ( title != d_data->lblTitle->text() )
{
d_data->lblTitle->setText( title );
updateLayout();
}
}
//! \return the plot's title
QwtText QwtPlot::title() const
{
return d_data->lblTitle->text();
}
//! \return the plot's title
QwtPlotLayout *QwtPlot::plotLayout()
{
return d_data->layout;
}
//! \return the plot's titel label.
const QwtPlotLayout *QwtPlot::plotLayout() const
{
return d_data->layout;
}
//! \return the plot's titel label.
QwtTextLabel *QwtPlot::titleLabel()
{
return d_data->lblTitle;
}
/*!
\return the plot's titel label.
*/
const QwtTextLabel *QwtPlot::titleLabel() const
{
return d_data->lblTitle;
}
/*!
\return the plot's legend
\sa insertLegend()
*/
QwtLegend *QwtPlot::legend()
{
return d_data->legend;
}
/*!
\return the plot's legend
\sa insertLegend()
*/
const QwtLegend *QwtPlot::legend() const
{
return d_data->legend;
}
/*!
\return the plot's canvas
*/
QwtPlotCanvas *QwtPlot::canvas()
{
return d_data->canvas;
}
/*!
\return the plot's canvas
*/
const QwtPlotCanvas *QwtPlot::canvas() const
{
return d_data->canvas;
}
/*!
Return sizeHint
\sa minimumSizeHint()
*/
QSize QwtPlot::sizeHint() const
{
int dw = 0;
int dh = 0;
for ( int axisId = 0; axisId < axisCnt; axisId++ )
{
if ( axisEnabled( axisId ) )
{
const int niceDist = 40;
const QwtScaleWidget *scaleWidget = axisWidget( axisId );
const QwtScaleDiv &scaleDiv = scaleWidget->scaleDraw()->scaleDiv();
const int majCnt = scaleDiv.ticks( QwtScaleDiv::MajorTick ).count();
if ( axisId == yLeft || axisId == yRight )
{
int hDiff = ( majCnt - 1 ) * niceDist
- scaleWidget->minimumSizeHint().height();
if ( hDiff > dh )
dh = hDiff;
}
else
{
int wDiff = ( majCnt - 1 ) * niceDist
- scaleWidget->minimumSizeHint().width();
if ( wDiff > dw )
dw = wDiff;
}
}
}
return minimumSizeHint() + QSize( dw, dh );
}
/*!
\brief Return a minimum size hint
*/
QSize QwtPlot::minimumSizeHint() const
{
QSize hint = d_data->layout->minimumSizeHint( this );
hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
return hint;
}
/*!
Resize and update internal layout
\param e Resize event
*/
void QwtPlot::resizeEvent( QResizeEvent *e )
{
QFrame::resizeEvent( e );
updateLayout();
}
/*!
\brief Redraw the plot
*/
void QwtPlot::replot()
{
updateAxes();
/*
Maybe the layout needs to be updated, because of changed
axes labels. We need to process them here before painting
to avoid that scales and canvas get out of sync.
*/
QApplication::sendPostedEvents( this, QEvent::LayoutRequest );
d_data->canvas->update();
}
/*!
\brief Adjust plot content to its current size.
\sa resizeEvent()
*/
void QwtPlot::updateLayout()
{
d_data->layout->activate( this, contentsRect() );
QRect titleRect = d_data->layout->titleRect().toRect();
QRect scaleRect[QwtPlot::axisCnt];
for ( int axisId = 0; axisId < axisCnt; axisId++ )
scaleRect[axisId] = d_data->layout->scaleRect( axisId ).toRect();
QRect legendRect = d_data->layout->legendRect().toRect();
QRect canvasRect = d_data->layout->canvasRect().toRect();
//
// resize and show the visible widgets
//
if ( !d_data->lblTitle->text().isEmpty() )
{
d_data->lblTitle->setGeometry( titleRect );
if ( !d_data->lblTitle->isVisibleTo( this ) )
d_data->lblTitle->show();
}
else
d_data->lblTitle->hide();
for ( int axisId = 0; axisId < axisCnt; axisId++ )
{
if ( axisEnabled( axisId ) )
{
if ( axisId == xBottom || axisId == xTop )
scaleRect[axisId].adjust(0, 0, 1, 0); // +1px padding for last tick
else
scaleRect[axisId].adjust(0, 0, 0, 1); // +1px padding for last tick
axisWidget( axisId )->setGeometry( scaleRect[axisId] );
if ( !axisWidget( axisId )->isVisibleTo( this ) )
axisWidget( axisId )->show();
}
else
axisWidget( axisId )->hide();
}
if ( d_data->legend &&
d_data->layout->legendPosition() != ExternalLegend )
{
if ( d_data->legend->itemCount() > 0 )
{
d_data->legend->setGeometry( legendRect );
d_data->legend->show();
}
else
d_data->legend->hide();
}
d_data->canvas->setGeometry( canvasRect );
}
/*!
Update the focus tab order
The order is changed so that the canvas will be in front of the
first legend item, or behind the last legend item - depending
on the position of the legend.
*/
void QwtPlot::updateTabOrder()
{
if ( d_data->canvas->focusPolicy() == Qt::NoFocus )
return;
if ( d_data->legend.isNull()
|| d_data->layout->legendPosition() == ExternalLegend
|| d_data->legend->legendItems().count() == 0 )
{
return;
}
// Depending on the position of the legend the
// tab order will be changed that the canvas is
// next to the last legend item, or before
// the first one.
const bool canvasFirst =
d_data->layout->legendPosition() == QwtPlot::BottomLegend ||
d_data->layout->legendPosition() == QwtPlot::RightLegend;
QWidget *previous = NULL;
QWidget *w = d_data->canvas;
while ( ( w = w->nextInFocusChain() ) != d_data->canvas )
{
bool isLegendItem = false;
if ( w->focusPolicy() != Qt::NoFocus
&& w->parent() && w->parent() == d_data->legend->contentsWidget() )
{
isLegendItem = true;
}
if ( canvasFirst )
{
if ( isLegendItem )
break;
previous = w;
}
else
{
if ( isLegendItem )
previous = w;
else
{
if ( previous )
break;
}
}
}
if ( previous && previous != d_data->canvas )
setTabOrder( previous, d_data->canvas );
}
/*!
Redraw the canvas.
\param painter Painter used for drawing
\warning drawCanvas calls drawItems what is also used
for printing. Applications that like to add individual
plot items better overload drawItems()
\sa drawItems()
*/
void QwtPlot::drawCanvas( QPainter *painter )
{
QwtScaleMap maps[axisCnt];
for ( int axisId = 0; axisId < axisCnt; axisId++ )
maps[axisId] = canvasMap( axisId );
drawItems( painter, d_data->canvas->contentsRect(), maps );
}
/*!
Redraw the canvas items.
\param painter Painter used for drawing
\param canvasRect Bounding rectangle where to paint
\param map QwtPlot::axisCnt maps, mapping between plot and paint device coordinates
*/
void QwtPlot::drawItems( QPainter *painter, const QRectF &canvasRect,
const QwtScaleMap map[axisCnt] ) const
{
const QwtPlotItemList& itmList = itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
QwtPlotItem *item = *it;
if ( item && item->isVisible() )
{
painter->save();
painter->setRenderHint( QPainter::Antialiasing,
item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
item->draw( painter,
map[QwtPlot::xBottom], map[QwtPlot::yLeft],
canvasRect );
painter->restore();
}
}
}
/*!
\param axisId Axis
\return Map for the axis on the canvas. With this map pixel coordinates can
translated to plot coordinates and vice versa.
\sa QwtScaleMap, transform(), invTransform()
*/
QwtScaleMap QwtPlot::canvasMap( int axisId ) const
{
QwtScaleMap map;
if ( !d_data->canvas )
return map;
map.setTransformation( axisScaleEngine( axisId )->transformation() );
const QwtScaleDiv *sd = axisScaleDiv( axisId );
map.setScaleInterval( sd->lowerBound(), sd->upperBound() );
if ( axisEnabled( axisId ) )
{
const QwtScaleWidget *s = axisWidget( axisId );
if ( axisId == yLeft || axisId == yRight )
{
double y = s->y() - d_data->canvas->y();
double h = s->height();
map.setPaintInterval( y + h, y );
}
else
{
double x = s->x() - d_data->canvas->x();
double w = s->width();
map.setPaintInterval( x, x + w );
}
}
else
{
const QRect &canvasRect = d_data->canvas->contentsRect();
if ( axisId == yLeft || axisId == yRight )
{
map.setPaintInterval( canvasRect.bottom(),
canvasRect.top() );
}
else
{
map.setPaintInterval( canvasRect.left(),
canvasRect.right() );
}
}
return map;
}
/*!
\brief Change the background of the plotting area
Sets brush to QPalette::Window of all colorgroups of
the palette of the canvas. Using canvas()->setPalette()
is a more powerful way to set these colors.
\param brush New background brush
\sa canvasBackground()
*/
void QwtPlot::setCanvasBackground( const QBrush &brush )
{
QPalette pal = d_data->canvas->palette();
for ( int i = 0; i < QPalette::NColorGroups; i++ )
pal.setBrush( ( QPalette::ColorGroup )i, QPalette::Window, brush );
canvas()->setPalette( pal );
}
/*!
Nothing else than: canvas()->palette().brush(
QPalette::Normal, QPalette::Window);
\return Background brush of the plotting area.
\sa setCanvasBackground()
*/
QBrush QwtPlot::canvasBackground() const
{
return canvas()->palette().brush(
QPalette::Normal, QPalette::Window );
}
/*!
\brief Change the border width of the plotting area
Nothing else than canvas()->setLineWidth(w),
left for compatibility only.
\param width New border width
*/
void QwtPlot::setCanvasLineWidth( int width )
{
canvas()->setLineWidth( width );
updateLayout();
}
/*!
Nothing else than: canvas()->lineWidth(),
left for compatibility only.
\return the border width of the plotting area
*/
int QwtPlot::canvasLineWidth() const
{
return canvas()->lineWidth();
}
/*!
\return \c true if the specified axis exists, otherwise \c false
\param axisId axis index
*/
bool QwtPlot::axisValid( int axisId )
{
return ( ( axisId >= QwtPlot::yLeft ) && ( axisId < QwtPlot::axisCnt ) );
}
/*!
\brief Insert a legend
If the position legend is \c QwtPlot::LeftLegend or \c QwtPlot::RightLegend
the legend will be organized in one column from top to down.
Otherwise the legend items will be placed in a table
with a best fit number of columns from left to right.
If pos != QwtPlot::ExternalLegend the plot widget will become
parent of the legend. It will be deleted when the plot is deleted,
or another legend is set with insertLegend().
\param legend Legend
\param pos The legend's position. For top/left position the number
of colums will be limited to 1, otherwise it will be set to
unlimited.
\param ratio Ratio between legend and the bounding rect
of title, canvas and axes. The legend will be shrinked
if it would need more space than the given ratio.
The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0
it will be reset to the default ratio.
The default vertical/horizontal ratio is 0.33/0.5.
\sa legend(), QwtPlotLayout::legendPosition(),
QwtPlotLayout::setLegendPosition()
*/
void QwtPlot::insertLegend( QwtLegend *legend,
QwtPlot::LegendPosition pos, double ratio )
{
d_data->layout->setLegendPosition( pos, ratio );
if ( legend != d_data->legend )
{
if ( d_data->legend && d_data->legend->parent() == this )
delete d_data->legend;
d_data->legend = legend;
if ( d_data->legend )
{
if ( pos != ExternalLegend )
{
if ( d_data->legend->parent() != this )
d_data->legend->setParent( this );
}
const QwtPlotItemList& itmList = itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
( *it )->updateLegend( d_data->legend );
}
QLayout *l = d_data->legend->contentsWidget()->layout();
if ( l && l->inherits( "QwtDynGridLayout" ) )
{
QwtDynGridLayout *tl = ( QwtDynGridLayout * )l;
switch ( d_data->layout->legendPosition() )
{
case LeftLegend:
case RightLegend:
tl->setMaxCols( 1 ); // 1 column: align vertical
break;
case TopLegend:
case BottomLegend:
tl->setMaxCols( 0 ); // unlimited
break;
case ExternalLegend:
break;
}
}
}
updateTabOrder();
}
updateLayout();
}