-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqwt_plot_item.cpp
387 lines (319 loc) · 8.34 KB
/
qwt_plot_item.cpp
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
/* -*- 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_item.h"
#include "qwt_text.h"
#include "qwt_plot.h"
#include "qwt_legend.h"
#include "qwt_legend_item.h"
#include "qwt_scale_div.h"
#include <qpainter.h>
class QwtPlotItem::PrivateData
{
public:
PrivateData():
plot( NULL ),
isVisible( true ),
renderHints( 0 ),
z( 0.0 )
{
}
mutable QwtPlot *plot;
bool isVisible;
QwtPlotItem::RenderHints renderHints;
double z;
QwtText title;
};
/*!
Constructor
\param title Title of the item
*/
QwtPlotItem::QwtPlotItem( const QwtText &title )
{
d_data = new PrivateData;
d_data->title = title;
}
//! Destroy the QwtPlotItem
QwtPlotItem::~QwtPlotItem()
{
attach( NULL );
delete d_data;
}
/*!
\brief Attach the item to a plot.
This method will attach a QwtPlotItem to the QwtPlot argument. It will first
detach the QwtPlotItem from any plot from a previous call to attach (if
necessary). If a NULL argument is passed, it will detach from any QwtPlot it
was attached to.
\param plot Plot widget
\sa detach()
*/
void QwtPlotItem::attach( QwtPlot *plot )
{
if ( plot == d_data->plot )
return;
// remove the item from the previous plot
if ( d_data->plot )
{
if ( d_data->plot->legend() )
d_data->plot->legend()->remove( this );
d_data->plot->attachItem( this, false );
}
d_data->plot = plot;
if ( d_data->plot )
{
// insert the item into the current plot
d_data->plot->attachItem( this, true );
itemChanged();
}
}
/*!
\brief This method detaches a QwtPlotItem from any
QwtPlot it has been associated with.
detach() is equivalent to calling attach( NULL )
\sa attach()
*/
void QwtPlotItem::detach()
{
attach( NULL );
}
//! Return attached plot
QwtPlot *QwtPlotItem::plot() const
{
return d_data->plot;
}
/*!
Plot items are painted in increasing z-order.
\return setZ(), QwtPlotDict::itemList()
*/
double QwtPlotItem::z() const
{
return d_data->z;
}
/*!
\brief Set the z value
Plot items are painted in increasing z-order.
\param z Z-value
\sa z(), QwtPlotDict::itemList()
*/
void QwtPlotItem::setZ( double z )
{
if ( d_data->z != z )
{
if ( d_data->plot ) // update the z order
d_data->plot->attachItem( this, false );
d_data->z = z;
if ( d_data->plot )
d_data->plot->attachItem( this, true );
itemChanged();
}
}
/*!
Set a new title
\param title Title
\sa title()
*/
void QwtPlotItem::setTitle( const QString &title )
{
setTitle( QwtText( title ) );
}
/*!
Set a new title
\param title Title
\sa title()
*/
void QwtPlotItem::setTitle( const QwtText &title )
{
if ( d_data->title != title )
{
d_data->title = title;
itemChanged();
}
}
/*!
\return Title of the item
\sa setTitle()
*/
const QwtText &QwtPlotItem::title() const
{
return d_data->title;
}
/*!
Toggle an render hint
\param hint Render hint
\param on true/false
\sa testRenderHint(), RenderHint
*/
void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
{
if ( ( ( d_data->renderHints & hint ) != 0 ) != on )
{
if ( on )
d_data->renderHints |= hint;
else
d_data->renderHints &= ~hint;
itemChanged();
}
}
/*!
Test a render hint
\param hint Render hint
\return true/false
\sa setRenderHint(), RenderHint
*/
bool QwtPlotItem::testRenderHint( RenderHint hint ) const
{
return ( d_data->renderHints & hint );
}
//! Show the item
void QwtPlotItem::show()
{
setVisible( true );
}
//! Hide the item
void QwtPlotItem::hide()
{
setVisible( false );
}
/*!
Show/Hide the item
\param on Show if true, otherwise hide
\sa isVisible(), show(), hide()
*/
void QwtPlotItem::setVisible( bool on )
{
if ( on != d_data->isVisible )
{
d_data->isVisible = on;
itemChanged();
}
}
/*!
\return true if visible
\sa setVisible(), show(), hide()
*/
bool QwtPlotItem::isVisible() const
{
return d_data->isVisible;
}
/*!
Update the legend and call QwtPlot::autoRefresh for the
parent plot.
\sa updateLegend()
*/
void QwtPlotItem::itemChanged()
{
if ( d_data->plot )
{
if ( d_data->plot->legend() )
updateLegend( d_data->plot->legend() );
}
}
/*!
\return An invalid bounding rect: QRectF(1.0, 1.0, -2.0, -2.0)
*/
QRectF QwtPlotItem::boundingRect() const
{
return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
}
/*!
\brief Allocate the widget that represents the item on the legend
The default implementation returns a QwtLegendItem(), but an item
could be represented by any type of widget,
by overloading legendItem() and updateLegend().
\return QwtLegendItem()
\sa updateLegend() QwtLegend()
*/
QWidget *QwtPlotItem::legendItem() const
{
QwtLegendItem *item = new QwtLegendItem;
return item;
}
/*!
\brief Update the widget that represents the item on the legend
updateLegend() is called from itemChanged() to adopt the widget
representing the item on the legend to its new configuration.
The default implementation updates a QwtLegendItem(),
but an item could be represented by any type of widget,
by overloading legendItem() and updateLegend().
\param legend Legend
\sa legendItem(), itemChanged(), QwtLegend()
*/
void QwtPlotItem::updateLegend( QwtLegend *legend ) const
{
if ( legend == NULL )
return;
QWidget *lgdItem = legend->find( this );
if ( !d_data->title.isNull() )
{
if ( lgdItem == NULL )
{
lgdItem = legendItem();
if ( lgdItem )
legend->insert( this, lgdItem );
}
if ( lgdItem && lgdItem->inherits( "QwtLegendItem" ) )
{
QwtLegendItem* label = ( QwtLegendItem* )lgdItem;
if ( label )
{
// paint the identifier
const QSize sz = label->identifierSize();
QPixmap identifier( sz.width(), sz.height() );
identifier.fill( Qt::transparent );
QPainter painter( &identifier );
painter.setRenderHint( QPainter::Antialiasing,
testRenderHint( QwtPlotItem::RenderAntialiased ) );
drawLegendIdentifier( &painter,
QRect( 0, 0, sz.width(), sz.height() ) );
painter.end();
const bool doUpdate = label->updatesEnabled();
if ( doUpdate )
label->setUpdatesEnabled( false );
label->setText( title() );
label->setIdentifier( identifier );
if ( doUpdate )
label->setUpdatesEnabled( true );
label->update();
}
}
}
else
{
if ( lgdItem )
{
lgdItem->hide();
lgdItem->deleteLater();
}
}
}
/*!
\brief Calculate the bounding scale rect of 2 maps
\param xMap X map
\param yMap X map
\return Bounding scale rect of the scale maps, normalized
*/
QRectF QwtPlotItem::scaleRect( const QwtScaleMap &xMap,
const QwtScaleMap &yMap ) const
{
return QRectF( xMap.s1(), yMap.s1(),
xMap.sDist(), yMap.sDist() );
}
/*!
\brief Calculate the bounding paint rect of 2 maps
\param xMap X map
\param yMap X map
\return Bounding paint rect of the scale maps, normalized
*/
QRectF QwtPlotItem::paintRect( const QwtScaleMap &xMap,
const QwtScaleMap &yMap ) const
{
const QRectF rect( xMap.p1(), yMap.p1(),
xMap.pDist(), yMap.pDist() );
return rect;
}