-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqwt_scale_draw.cpp
427 lines (358 loc) · 10.1 KB
/
qwt_scale_draw.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
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
/* -*- 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_scale_draw.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include <qpen.h>
#include <qpainter.h>
#include <qmath.h>
/*!
\brief Constructor
The range of the scale is initialized to [0, 100],
The position is at (0, 0) with a length of 100.
The orientation is QwtAbstractScaleDraw::Bottom.
*/
QwtScaleDraw::QwtScaleDraw():
len( 0 ),
alignment( QwtScaleDraw::BottomScale )
{
Multiplier = 1.;
setLength( 100 );
}
/*!
Set the alignment of the scale
The default alignment is QwtScaleDraw::BottomScale
\sa alignment()
*/
void QwtScaleDraw::setAlignment( Alignment align )
{
alignment = align;
}
/*!
Return the orientation
TopScale, BottomScale are horizontal (Qt::Horizontal) scales,
LeftScale, RightScale are vertical (Qt::Vertical) scales.
\sa alignment()
*/
Qt::Orientation QwtScaleDraw::orientation() const
{
switch ( alignment )
{
case TopScale:
case BottomScale:
return Qt::Horizontal;
case LeftScale:
case RightScale:
default:
return Qt::Vertical;
}
}
/*!
Calculate the width/height that is needed for a
vertical/horizontal scale.
The extent is calculated from the pen width of the backbone,
the major tick length, the spacing and the maximum width/height
of the labels.
\param font Font used for painting the labels
\sa minLength()
*/
double QwtScaleDraw::extent( const QFont &font ) const
{
double d = 0;
if ( orientation() == Qt::Vertical )
d = maxLabelWidth( font );
else
d = maxLabelHeight( font );
d += spacing();
d += maxTickLength();
const double pw = qMax( 1, penWidth() ); // penwidth can be zero
d += pw;
return d;
}
/*!
Find the position, where to paint a label
The position has a distance of majTickLength() + spacing() + 1
from the backbone. The direction depends on the alignment()
\param value Value
*/
QPointF QwtScaleDraw::labelPosition( double value ) const
{
const double tval = scaleMap().transform( value );
double dist = spacing() + qMax( 1, penWidth() );
dist += tickLength( QwtScaleDiv::MajorTick );
double px = 0;
double py = 0;
switch ( alignment )
{
case RightScale:
{
px = pos.x() + dist;
py = tval;
break;
}
case LeftScale:
{
px = pos.x() - dist;
py = tval;
break;
}
case BottomScale:
{
px = tval;
py = pos.y() + dist;
break;
}
case TopScale:
{
px = tval;
py = pos.y() - dist;
break;
}
}
return QPointF( px, py );
}
/*!
Draw a tick
\param painter Painter
\param value Value of the tick
\param len Lenght of the tick
\sa drawBackbone(), drawLabel()
*/
void QwtScaleDraw::drawTick( QPainter *painter, double value, double len ) const
{
double tval = scaleMap().transform( value );
const int pw = penWidth();
switch ( alignment )
{
case LeftScale:
{
double x1 = pos.x();
double x2 = x1 - pw - len;
painter->drawLine( x1, tval, x2, tval );
break;
}
case RightScale:
{
double x1 = pos.x();
double x2 = x1 + pw + len;
painter->drawLine( x1, tval, x2, tval );
break;
}
case BottomScale:
{
double y1 = pos.y();
double y2 = y1 + pw + len;
painter->drawLine( tval, y1, tval, y2 );
break;
}
case TopScale:
{
double y1 = pos.y();
double y2 = y1 - pw - len;
painter->drawLine( tval, y1, tval, y2 );
break;
}
}
}
/*!
Draws the baseline of the scale
\param painter Painter
\sa drawTick(), drawLabel()
*/
void QwtScaleDraw::drawBackbone( QPainter *painter ) const
{
// pos indicates a border not the center of the backbone line
// so we need to shift its position depending on the pen width
// and the alignment of the scale
double off = 0.5 * penWidth();
switch ( alignment )
{
case LeftScale:
{
double x = pos.x() - off;
painter->drawLine( x, pos.y(), x, pos.y() + len );
break;
}
case RightScale:
{
double x = pos.x() + off;
painter->drawLine( x, pos.y(), x, pos.y() + len );
break;
}
case TopScale:
{
double y = pos.y() - off;
painter->drawLine( pos.x(), y, pos.x() + len, y );
break;
}
case BottomScale:
{
double y = pos.y() + off;
painter->drawLine( pos.x(), y, pos.x() + len, y );
break;
}
}
}
/*!
\brief Move the position of the scale
The meaning of the parameter pos depends on the alignment:
<dl>
<dt>QwtScaleDraw::LeftScale
<dd>The origin is the topmost point of the
backbone. The backbone is a vertical line.
Scale marks and labels are drawn
at the left of the backbone.
<dt>QwtScaleDraw::RightScale
<dd>The origin is the topmost point of the
backbone. The backbone is a vertical line.
Scale marks and labels are drawn
at the right of the backbone.
<dt>QwtScaleDraw::TopScale
<dd>The origin is the leftmost point of the
backbone. The backbone is a horizontal line.
Scale marks and labels are drawn
above the backbone.
<dt>QwtScaleDraw::BottomScale
<dd>The origin is the leftmost point of the
backbone. The backbone is a horizontal line
Scale marks and labels are drawn
below the backbone.
</dl>
\param pos Origin of the scale
\sa pos(), setLength()
*/
void QwtScaleDraw::move( const QPointF &_pos )
{
pos = _pos;
updateMap();
}
/*!
Set the length of the backbone.
The length doesn't include the space needed for
overlapping labels.
\sa move()
*/
void QwtScaleDraw::setLength( double length )
{
len = length;
updateMap();
}
/*!
Draws the label for a major scale tick
\param painter Painter
\param value Value
\sa drawTick(), drawBackbone()
*/
void QwtScaleDraw::drawLabel( QPainter *painter, double value ) const
{
QwtText lbl = tickLabel( value / Multiplier );
if ( lbl.isEmpty() )
return;
QPointF poss = labelPosition( value );
QSizeF labelSize = lbl.textSize( painter->font() );
poss += labelOffset( labelSize );
//confine label inside rect()
if (orientation() == Qt::Horizontal) {
if (poss.x() <= pos.x())
poss.rx() = pos.x() + 1;
if (poss.x() + labelSize.width() >= pos.x() + len)
poss.rx() = pos.x() + len - 1 - labelSize.width();
} else {
if (poss.y() <= pos.y())
poss.ry() = pos.y() + 1;
if (poss.y() + labelSize.height() >= pos.y() + len)
poss.ry() = pos.y() + len - 1 - labelSize.height();
}
lbl.draw ( painter, QRect( poss.toPoint(), labelSize.toSize() ) );
}
/*!
Calculate the transformation that is needed to paint a label
depending on its alignment.
\param size Size of the label
*/
QPointF QwtScaleDraw::labelOffset( const QSizeF &size ) const
{
double x, y;
switch ( alignment )
{
case RightScale:
x = 0;
y = -( 0.5 * size.height() );
break;
case LeftScale:
x = -size.width();
y = -( 0.5 * size.height() );
break;
case BottomScale:
x = -( 0.5 * size.width() );
y = 0;
break;
case TopScale:
x = -( 0.5 * size.width() );
y = -size.height();
break;
}
return QPointF(x, y);
}
/*!
Calculate the size that is needed to draw a label
\param font Label font
\param value Value
*/
QSizeF QwtScaleDraw::labelSize( const QFont &font, double value ) const
{
QwtText lbl = tickLabel( value / Multiplier );
if ( lbl.isEmpty() )
return QSizeF( 0, 0 );
const QSizeF labelSize = lbl.textSize( font );
return labelSize;
}
/*!
\param font Font
\return the maximum width of a label
*/
int QwtScaleDraw::maxLabelWidth( const QFont &font ) const
{
int maxWidth = 0;
const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
for ( uint i = 0; i < ( uint )ticks.count(); i++ )
{
const int w = labelSize( font, ticks[i] ).width();
if ( w > maxWidth )
maxWidth = w;
if (i == 1) //optimize: only first and last 2 ticks
i = qMax(1, ticks.count()-1 -2);
}
return maxWidth;
}
/*!
\param font Font
\return the maximum height of a label
*/
int QwtScaleDraw::maxLabelHeight( const QFont &font ) const
{
int maxHeight = 0;
const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
for ( uint i = 0; i < ( uint )ticks.count(); i++ )
{
const int h = labelSize( font, ticks[i] ).height();
if ( h > maxHeight )
maxHeight = h;
break; //optimize: all one-liners
}
return maxHeight;
}
void QwtScaleDraw::updateMap()
{
QwtScaleMap &sm = scaleMap();
if ( orientation() == Qt::Vertical )
sm.setPaintInterval( pos.y() + len, pos.y() );
else
sm.setPaintInterval( pos.x(), pos.x() + len );
}