~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kchart/kdchart/src/KDChartCartesianGrid.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 ** Copyright (C) 2007 Klarälvdalens Datakonsult AB.  All rights reserved.
3
 
 **
4
 
 ** This file is part of the KD Chart library.
5
 
 **
6
 
 ** This file may be used under the terms of the GNU General Public
7
 
 ** License versions 2.0 or 3.0 as published by the Free Software
8
 
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
9
 
 ** included in the packaging of this file.  Alternatively you may (at
10
 
 ** your option) use any later version of the GNU General Public
11
 
 ** License if such license has been publicly approved by
12
 
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
13
 
 ** 
14
 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
15
 
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
16
 
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
17
 
 ** not expressly granted herein.
18
 
 ** 
19
 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
 
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21
 
 **
22
 
 **********************************************************************/
23
 
 
24
 
#include "KDChartCartesianGrid.h"
25
 
#include "KDChartAbstractCartesianDiagram.h"
26
 
#include "KDChartPaintContext.h"
27
 
#include "KDChartPainterSaver_p.h"
28
 
#include "KDChartPrintingParameters.h"
29
 
 
30
 
#include <QPainter>
31
 
 
32
 
#include <KDABLibFakes>
33
 
 
34
 
#include <limits>
35
 
 
36
 
using namespace KDChart;
37
 
 
38
 
 
39
 
void CartesianGrid::drawGrid( PaintContext* context )
40
 
{
41
 
    //qDebug() << "KDChart::CartesianGrid::drawGrid( PaintContext* context ) called";
42
 
 
43
 
    CartesianCoordinatePlane* plane = dynamic_cast<CartesianCoordinatePlane*>(context->coordinatePlane());
44
 
   
45
 
    // This plane is used for tranlating the coordinates - not for the data boundaries
46
 
    PainterSaver p( context->painter() );
47
 
    plane = dynamic_cast< CartesianCoordinatePlane* >( plane->sharedAxisMasterPlane( context->painter() ) );
48
 
 
49
 
    Q_ASSERT_X ( plane, "CartesianGrid::drawGrid",
50
 
                 "Bad function call: PaintContext::coodinatePlane() NOT a cartesian plane." );
51
 
 
52
 
 
53
 
    const GridAttributes gridAttrsX( plane->gridAttributes( Qt::Horizontal ) );
54
 
    const GridAttributes gridAttrsY( plane->gridAttributes( Qt::Vertical ) );
55
 
 
56
 
    //qDebug() << "OK:";
57
 
    if ( !gridAttrsX.isGridVisible() && !gridAttrsY.isGridVisible() ) return;
58
 
    //qDebug() << "A";
59
 
 
60
 
    // important: Need to update the calculated mData,
61
 
    //            before we may use it!
62
 
    updateData( context->coordinatePlane() );
63
 
 
64
 
    if( plane->axesCalcModeX() == KDChart::AbstractCoordinatePlane::Logarithmic && mData.first().stepWidth == 0.0 )
65
 
            mData.first().stepWidth = 1.0;
66
 
    if( plane->axesCalcModeY() == KDChart::AbstractCoordinatePlane::Logarithmic && mData.last().stepWidth == 0.0 )
67
 
            mData.last().stepWidth = 1.0;
68
 
 
69
 
    // test for programming errors: critical
70
 
    Q_ASSERT_X ( mData.count() == 2, "CartesianGrid::drawGrid",
71
 
                 "Error: updateData did not return exactly two dimensions." );
72
 
 
73
 
    // test for invalid boundaries: non-critical
74
 
    if( !isBoundariesValid( mData ) ) return;
75
 
    //qDebug() << "B";
76
 
 
77
 
    DataDimension dimX = mData.first();
78
 
    const DataDimension& dimY = mData.last();
79
 
    // test for other programming errors: critical
80
 
    Q_ASSERT_X ( dimX.stepWidth, "CartesianGrid::drawGrid",
81
 
                 "Error: updateData returned a Zero step width for the X grid." );
82
 
    Q_ASSERT_X ( dimY.stepWidth, "CartesianGrid::drawGrid",
83
 
                 "Error: updateData returned a Zero step width for the Y grid." );
84
 
 
85
 
 
86
 
    qreal numberOfUnitLinesX =
87
 
        qAbs( dimX.distance() / dimX.stepWidth )
88
 
        + (dimX.isCalculated ? 1.0 : 0.0);
89
 
    qreal numberOfUnitLinesY =
90
 
        qAbs( dimY.distance() / dimY.stepWidth )
91
 
        + (dimY.isCalculated ? 1.0 : 0.0);
92
 
    //qDebug("numberOfUnitLinesX: %f    numberOfUnitLinesY: %f",numberOfUnitLinesX,numberOfUnitLinesY);
93
 
 
94
 
    // do not draw a Zero size grid (and do not divide by Zero)
95
 
    if( numberOfUnitLinesX <= 0.0 || numberOfUnitLinesY <= 0.0 ) return;
96
 
    //qDebug() << "C";
97
 
 
98
 
    const QPointF p1 = plane->translate( QPointF(dimX.start, dimY.start) );
99
 
    const QPointF p2 = plane->translate( QPointF(dimX.end, dimY.end) );
100
 
//qDebug() << "dimX.isCalculated:" << dimX.isCalculated << "dimY.isCalculated:" << dimY.isCalculated;
101
 
//qDebug() << "dimX.start: " << dimX.start << "dimX.end: " << dimX.end;
102
 
//qDebug() << "dimY.start: " << dimY.start << "dimY.end: " << dimY.end;
103
 
//qDebug() << "p1:" << p1 << "  p2:" << p2;
104
 
 
105
 
    const qreal screenRangeX = qAbs ( p1.x() - p2.x() );
106
 
    const qreal screenRangeY = qAbs ( p1.y() - p2.y() );
107
 
 
108
 
    /*
109
 
     * let us paint the grid at a smaller resolution
110
 
     * the user can disable at any time
111
 
     * by setting the grid attribute to false
112
 
     * Same Value as for Cartesian Axis
113
 
     */
114
 
    static const qreal GridLineDistanceTreshold = 4.0; // <Treshold> pixels between each grid line
115
 
    const qreal MinimumPixelsBetweenLines =
116
 
            GridLineDistanceTreshold;
117
 
    //qDebug() << "x step " << dimX.stepWidth << "  y step " << dimY.stepWidth;
118
 
 
119
 
    //qreal unitFactorX = 1.0;
120
 
//    qreal unitFactorY = 1.0;
121
 
 
122
 
    //FIXME(khz): Remove this code, and do the calculation in the grid calc function
123
 
    if( ! dimX.isCalculated ){
124
 
 
125
 
        while( screenRangeX / numberOfUnitLinesX <= MinimumPixelsBetweenLines ){
126
 
            dimX.stepWidth *= 10.0;
127
 
            dimX.subStepWidth *= 10.0;
128
 
            numberOfUnitLinesX = qAbs( dimX.distance() / dimX.stepWidth );
129
 
        }
130
 
    }
131
 
    if( dimX.subStepWidth && (screenRangeX / (dimX.distance() / dimX.subStepWidth) <= MinimumPixelsBetweenLines) ){
132
 
        dimX.subStepWidth = 0.0;
133
 
        //qDebug() << "de-activating grid sub steps: not enough space";
134
 
    }
135
 
 
136
 
    const bool drawUnitLinesX = gridAttrsX.isGridVisible() &&
137
 
            (screenRangeX / numberOfUnitLinesX > MinimumPixelsBetweenLines);
138
 
    const bool drawUnitLinesY = gridAttrsY.isGridVisible() &&
139
 
            (screenRangeY / numberOfUnitLinesY > MinimumPixelsBetweenLines);
140
 
 
141
 
    const bool isLogarithmicX = dimX.isCalculated && (dimX.calcMode == AbstractCoordinatePlane::Logarithmic );
142
 
    const bool isLogarithmicY = (dimY.calcMode == AbstractCoordinatePlane::Logarithmic );
143
 
/*
144
 
    while ( !drawUnitLinesX ) {
145
 
        unitFactorX *= 10.0;
146
 
        drawUnitLinesX = screenRangeX / (numberOfUnitLinesX / unitFactorX) > MinimumPixelsBetweenLines;
147
 
    }
148
 
    while ( !drawUnitLinesY ) {
149
 
        unitFactorY *= 10.0;
150
 
        drawUnitLinesY = screenRangeY / (numberOfUnitLinesY / unitFactorY) > MinimumPixelsBetweenLines;
151
 
    }
152
 
*/
153
 
 
154
 
    const bool drawSubGridLinesX = isLogarithmicX ||
155
 
        ((dimX.subStepWidth != 0.0) &&
156
 
        (screenRangeX / (numberOfUnitLinesX / dimX.stepWidth * dimX.subStepWidth) > MinimumPixelsBetweenLines) &&
157
 
        gridAttrsX.isSubGridVisible());
158
 
 
159
 
    const bool drawSubGridLinesY = isLogarithmicY ||
160
 
        ((dimY.subStepWidth != 0.0) &&
161
 
        (screenRangeY / (numberOfUnitLinesY / dimY.stepWidth * dimY.subStepWidth) > MinimumPixelsBetweenLines) &&
162
 
        gridAttrsY.isSubGridVisible());
163
 
 
164
 
    qreal minValueX = qMin( dimX.start, dimX.end );
165
 
    qreal maxValueX = qMax( dimX.start, dimX.end );
166
 
    qreal minValueY = qMin( dimY.start, dimY.end );
167
 
    qreal maxValueY = qMax( dimY.start, dimY.end );
168
 
    AbstractGrid::adjustLowerUpperRange( minValueX, maxValueX, dimX.stepWidth, true, true );
169
 
    AbstractGrid::adjustLowerUpperRange( minValueY, maxValueY, dimY.stepWidth, true, true );
170
 
 
171
 
    if ( drawSubGridLinesX ) {
172
 
        context->painter()->setPen( PrintingParameters::scalePen( gridAttrsX.subGridPen() ) );
173
 
        qreal f = minValueX;
174
 
        qreal fLogSubstep = minValueX;
175
 
 
176
 
        int logSubstep = 0;
177
 
        while ( f <= maxValueX ) {
178
 
            QPointF topPoint( f, maxValueY );
179
 
            QPointF bottomPoint( f, minValueY );
180
 
            topPoint = plane->translate( topPoint );
181
 
            bottomPoint = plane->translate( bottomPoint );
182
 
            context->painter()->drawLine( topPoint, bottomPoint );
183
 
            if ( isLogarithmicX ){
184
 
                if( logSubstep == 9 ){
185
 
                    fLogSubstep *= ( fLogSubstep > 0.0 ) ? 10.0 : 0.1;
186
 
                    if( fLogSubstep == 0.0 )
187
 
                        fLogSubstep = pow( 10.0, floor( log10( dimX.start ) ) );
188
 
 
189
 
                    logSubstep = 0;
190
 
                    f = fLogSubstep;
191
 
                }
192
 
                else
193
 
                {
194
 
                    f += fLogSubstep;
195
 
                }
196
 
                ++logSubstep;
197
 
            }else{
198
 
                f += dimX.subStepWidth;
199
 
            }
200
 
        }
201
 
    }
202
 
 
203
 
    if ( drawSubGridLinesY ) {
204
 
        context->painter()->setPen( PrintingParameters::scalePen( gridAttrsY.subGridPen() ) );
205
 
        qreal f = minValueY;
206
 
        qreal fLogSubstep = minValueY;
207
 
 
208
 
        int logSubstep = 0;
209
 
        while ( f <= maxValueY ) {
210
 
            //qDebug() << "sub grid line Y at" << f;
211
 
            QPointF leftPoint( minValueX, f );
212
 
            QPointF rightPoint( maxValueX, f );
213
 
            leftPoint = plane->translate( leftPoint );
214
 
            rightPoint = plane->translate( rightPoint );
215
 
            context->painter()->drawLine( leftPoint, rightPoint );
216
 
            if ( isLogarithmicY ){
217
 
                if( logSubstep == 9 ){
218
 
                    fLogSubstep *= ( fLogSubstep > 0.0 ) ? 10.0 : 0.1;
219
 
                    if( fLogSubstep == 0.0 )
220
 
                        fLogSubstep = pow( 10.0, floor( log10( dimY.start ) ) );
221
 
 
222
 
                    logSubstep = 0;
223
 
                    f = fLogSubstep;
224
 
                }
225
 
                else
226
 
                {
227
 
                    f += fLogSubstep;
228
 
                }
229
 
                ++logSubstep;
230
 
            }else{
231
 
                f += dimY.subStepWidth;
232
 
            }
233
 
        }
234
 
    }
235
 
 
236
 
    const bool drawXZeroLineX
237
 
        = dimX.isCalculated &&
238
 
        gridAttrsX.zeroLinePen().style() != Qt::NoPen;
239
 
 
240
 
    const bool drawZeroLineY
241
 
        = gridAttrsY.zeroLinePen().style() != Qt::NoPen;
242
 
 
243
 
    if ( drawUnitLinesX || drawXZeroLineX ) {
244
 
        //qDebug() << "E";
245
 
        if ( drawUnitLinesX )
246
 
            context->painter()->setPen( PrintingParameters::scalePen( gridAttrsX.gridPen() ) );
247
 
//        const qreal minX = dimX.start;
248
 
 
249
 
        qreal f = minValueX;
250
 
 
251
 
        while ( f <= maxValueX ) {
252
 
            // PENDING(khz) FIXME: make draving/not drawing of Zero line more sophisticated?:
253
 
            const bool zeroLineHere = drawXZeroLineX && (f == 0.0);
254
 
            if ( drawUnitLinesX || zeroLineHere ){
255
 
                //qDebug("main grid line X at: %f --------------------------",f);
256
 
                QPointF topPoint( f, maxValueY );
257
 
                QPointF bottomPoint( f, minValueY );
258
 
                topPoint = plane->translate( topPoint );
259
 
                bottomPoint = plane->translate( bottomPoint );
260
 
                if ( zeroLineHere )
261
 
                    context->painter()->setPen( PrintingParameters::scalePen( gridAttrsX.zeroLinePen() ) );
262
 
                context->painter()->drawLine( topPoint, bottomPoint );
263
 
                if ( zeroLineHere )
264
 
                    context->painter()->setPen( PrintingParameters::scalePen( gridAttrsX.gridPen() ) );
265
 
            }
266
 
            if ( isLogarithmicX ) {
267
 
                f *= ( f > 0.0 ) ? 10.0 : 0.1;
268
 
                if( f == 0.0 )
269
 
                    f = pow( 10.0, floor( log10( dimX.start ) ) );
270
 
            }
271
 
            else
272
 
                f += dimX.stepWidth;
273
 
        }
274
 
        // draw the last line if not logarithmic calculation
275
 
        // we need the in order to get the right grid line painted
276
 
        // when f + dimX.stepWidth jump over maxValueX
277
 
        if (  ! isLogarithmicX )
278
 
        context->painter()->drawLine( plane->translate( QPointF(  maxValueX, maxValueY ) ),
279
 
                                      plane->translate( QPointF( maxValueX, minValueY ) ) );
280
 
 
281
 
    }
282
 
    if ( drawUnitLinesY || drawZeroLineY ) {
283
 
        //qDebug() << "F";
284
 
        if ( drawUnitLinesY )
285
 
            context->painter()->setPen( PrintingParameters::scalePen( gridAttrsY.gridPen() ) );
286
 
        //const qreal minY = dimY.start;
287
 
        //qDebug("minY: %f   maxValueY: %f   dimY.stepWidth: %f",minY,maxValueY,dimY.stepWidth);
288
 
        qreal f = minValueY;
289
 
 
290
 
        while ( f <= maxValueY ) {
291
 
            // PENDING(khz) FIXME: make draving/not drawing of Zero line more sophisticated?:
292
 
            //qDebug("main grid line Y at: %f",f);
293
 
            const bool zeroLineHere = (f == 0.0);
294
 
            if ( drawUnitLinesY || zeroLineHere ){
295
 
                QPointF leftPoint(  minValueX, f );
296
 
                QPointF rightPoint( maxValueX, f );
297
 
                leftPoint  = plane->translate( leftPoint );
298
 
                rightPoint = plane->translate( rightPoint );
299
 
                if ( zeroLineHere )
300
 
                    context->painter()->setPen( PrintingParameters::scalePen( gridAttrsY.zeroLinePen() ) );
301
 
                context->painter()->drawLine( leftPoint, rightPoint );
302
 
                if ( zeroLineHere )
303
 
                    context->painter()->setPen( PrintingParameters::scalePen( gridAttrsY.gridPen() ) );
304
 
            }
305
 
            if ( isLogarithmicY ) {
306
 
                f *= ( f > 0.0 ) ? 10.0 : 0.1;
307
 
                if( f == 0.0 )
308
 
                    f = pow( 10.0, floor( log10( dimY.start ) ) );
309
 
            }
310
 
            else
311
 
                f += dimY.stepWidth;
312
 
        }
313
 
    }
314
 
    //qDebug() << "Z";
315
 
}
316
 
 
317
 
 
318
 
DataDimensionsList CartesianGrid::calculateGrid(
319
 
    const DataDimensionsList& rawDataDimensions ) const
320
 
{
321
 
    Q_ASSERT_X ( rawDataDimensions.count() == 2, "CartesianGrid::calculateGrid",
322
 
                 "Error: calculateGrid() expects a list with exactly two entries." );
323
 
 
324
 
    CartesianCoordinatePlane* plane = dynamic_cast<CartesianCoordinatePlane*>( mPlane );
325
 
    Q_ASSERT_X ( plane, "CartesianGrid::calculateGrid",
326
 
                 "Error: PaintContext::calculatePlane() called, but no cartesian plane set." );
327
 
 
328
 
    DataDimensionsList l( rawDataDimensions );
329
 
    // rule:  Returned list is either empty, or it is providing two
330
 
    //        valid dimensions, complete with two non-Zero step widths.
331
 
    if( isBoundariesValid( l ) ) {
332
 
        const QPointF translatedBottomLeft( plane->translateBack( plane->geometry().bottomLeft() ) );
333
 
        const QPointF translatedTopRight(   plane->translateBack( plane->geometry().topRight() ) );
334
 
        //qDebug() << "CartesianGrid::calculateGrid()         first:" << l.first().start << l.first().end <<                   "   last:" << l.last().start << l.last().end;
335
 
        //qDebug() << "CartesianGrid::calculateGrid()  translated x:" << translatedBottomLeft.x() << translatedTopRight.x() << "      y:" << translatedBottomLeft.y() << translatedTopRight.y();
336
 
        //qDebug() << "CartesianGrid::calculateGrid()  raw data y-range  :" << l.last().end - l.last().start;
337
 
        //qDebug() << "CartesianGrid::calculateGrid()  translated y-range:" << translatedTopRight.y() - translatedBottomLeft.y();
338
 
 
339
 
        /* Code is obsolete. The dataset dimension of the diagram should *never* be > 1.
340
 
        if( l.first().isCalculated
341
 
            && plane->autoAdjustGridToZoom()
342
 
            && plane->axesCalcModeX() == CartesianCoordinatePlane::Linear
343
 
            && plane->zoomFactorX() > 1.0 )
344
 
        {
345
 
            l.first().start = translatedBottomLeft.x();
346
 
            l.first().end   = translatedTopRight.x();
347
 
        }
348
 
        */
349
 
 
350
 
        const GridAttributes gridAttrsX( plane->gridAttributes( Qt::Horizontal ) );
351
 
        const GridAttributes gridAttrsY( plane->gridAttributes( Qt::Vertical ) );
352
 
 
353
 
        const DataDimension dimX
354
 
                = calculateGridXY( l.first(), Qt::Horizontal,
355
 
                                   gridAttrsX.adjustLowerBoundToGrid(),
356
 
                                   gridAttrsX.adjustUpperBoundToGrid() );
357
 
        if( dimX.stepWidth ){
358
 
            //qDebug("CartesianGrid::calculateGrid()   l.last().start:  %f   l.last().end:  %f", l.last().start, l.last().end);
359
 
            //qDebug("                                 l.first().start: %f   l.first().end: %f", l.first().start, l.first().end);
360
 
 
361
 
            // one time for the min/max value
362
 
            const DataDimension minMaxY
363
 
                    = calculateGridXY( l.last(), Qt::Vertical,
364
 
                                       gridAttrsY.adjustLowerBoundToGrid(),
365
 
                                       gridAttrsY.adjustUpperBoundToGrid() );
366
 
 
367
 
            if( plane->autoAdjustGridToZoom()
368
 
                && plane->axesCalcModeY() == CartesianCoordinatePlane::Linear
369
 
                && plane->zoomFactorY() > 1.0 )
370
 
            {
371
 
                l.last().start = translatedBottomLeft.y();
372
 
                l.last().end   = translatedTopRight.y();
373
 
            }
374
 
            // and one other time for the step width
375
 
            const DataDimension dimY
376
 
                    = calculateGridXY( l.last(), Qt::Vertical,
377
 
                                       gridAttrsY.adjustLowerBoundToGrid(),
378
 
                                       gridAttrsY.adjustUpperBoundToGrid() );
379
 
            if( dimY.stepWidth ){
380
 
                l.first().start        = dimX.start;
381
 
                l.first().end          = dimX.end;
382
 
                l.first().stepWidth    = dimX.stepWidth;
383
 
                l.first().subStepWidth = dimX.subStepWidth;
384
 
                l.last().start        = minMaxY.start;
385
 
                l.last().end          = minMaxY.end;
386
 
                l.last().stepWidth    = dimY.stepWidth;
387
 
                l.last().subStepWidth    = dimY.subStepWidth;
388
 
                //qDebug() << "CartesianGrid::calculateGrid()  final grid y-range:" << l.last().end - l.last().start << "   step width:" << l.last().stepWidth << endl;
389
 
                // calculate some reasonable subSteps if the
390
 
                // user did not set the sub grid but did set
391
 
                // the stepWidth.
392
 
                
393
 
                // FIXME (Johannes)
394
 
                // the last (y) dimension is not always the dimension for the ordinate!
395
 
                // since there's no way to check for the orientation of this dimension here,
396
 
                // we cannot automatically assume substep values
397
 
                //if ( dimY.subStepWidth == 0 )
398
 
                //    l.last().subStepWidth = dimY.stepWidth/2;
399
 
                //else
400
 
                //    l.last().subStepWidth = dimY.subStepWidth;
401
 
            }
402
 
        }
403
 
    }
404
 
    //qDebug() << "CartesianGrid::calculateGrid()  final grid Y-range:" << l.last().end - l.last().start << "   substep width:" << l.last().subStepWidth;
405
 
    //qDebug() << "CartesianGrid::calculateGrid()  final grid X-range:" << l.first().end - l.first().start << "   substep width:" << l.first().subStepWidth;
406
 
 
407
 
    return l;
408
 
}
409
 
 
410
 
 
411
 
qreal fastPow10( int x )
412
 
{
413
 
    qreal res = 1.0;
414
 
    if( 0 <= x ){
415
 
        for( int i = 1; i <= x; ++i )
416
 
            res *= 10.0;
417
 
    }else{
418
 
        for( int i = -1; i >= x; --i )
419
 
            res /= 10.0;
420
 
    }
421
 
    return res;
422
 
}
423
 
 
424
 
#if defined ( Q_WS_WIN)
425
 
#define trunc(x) ((int)(x))
426
 
#endif
427
 
 
428
 
DataDimension CartesianGrid::calculateGridXY(
429
 
    const DataDimension& rawDataDimension,
430
 
    Qt::Orientation orientation,
431
 
    bool adjustLower, bool adjustUpper ) const
432
 
{
433
 
    CartesianCoordinatePlane* const plane = dynamic_cast<CartesianCoordinatePlane*>( mPlane );
434
 
    if(    ((orientation == Qt::Vertical)   && (plane->autoAdjustVerticalRangeToData()   >= 100))
435
 
        || ((orientation == Qt::Horizontal) && (plane->autoAdjustHorizontalRangeToData() >= 100)) )
436
 
    {
437
 
        adjustLower = false;
438
 
        adjustUpper = false;
439
 
    }
440
 
 
441
 
    DataDimension dim( rawDataDimension );
442
 
    if( dim.isCalculated && dim.start != dim.end ){
443
 
        if( dim.calcMode == AbstractCoordinatePlane::Linear ){
444
 
            // linear ( == not-logarithmic) calculation
445
 
            if( dim.stepWidth == 0.0 ){
446
 
                QList<qreal> granularities;
447
 
                switch( dim.sequence ){
448
 
                    case KDChartEnums::GranularitySequence_10_20:
449
 
                        granularities << 1.0 << 2.0;
450
 
                        break;
451
 
                    case KDChartEnums::GranularitySequence_10_50:
452
 
                        granularities << 1.0 << 5.0;
453
 
                        break;
454
 
                    case KDChartEnums::GranularitySequence_25_50:
455
 
                        granularities << 2.5 << 5.0;
456
 
                        break;
457
 
                    case KDChartEnums::GranularitySequence_125_25:
458
 
                        granularities << 1.25 << 2.5;
459
 
                        break;
460
 
                    case KDChartEnums::GranularitySequenceIrregular:
461
 
                        granularities << 1.0 << 1.25 << 2.0 << 2.5 << 5.0;
462
 
                        break;
463
 
                    default:
464
 
                        break;
465
 
                }
466
 
                //qDebug("CartesianGrid::calculateGridXY()   dim.start: %f   dim.end: %f", dim.start, dim.end);
467
 
                calculateStepWidth(
468
 
                    dim.start, dim.end, granularities, orientation,
469
 
                    dim.stepWidth, dim.subStepWidth,
470
 
                    adjustLower, adjustUpper );
471
 
            }
472
 
            // if needed, adjust start/end to match the step width:
473
 
            //qDebug() << "CartesianGrid::calculateGridXY() has 1st linear range: min " << dim.start << " and max" << dim.end;
474
 
 
475
 
            AbstractGrid::adjustLowerUpperRange( dim.start, dim.end, dim.stepWidth,
476
 
                    adjustLower, adjustUpper );
477
 
            //qDebug() << "CartesianGrid::calculateGridXY() returns linear range: min " << dim.start << " and max" << dim.end;
478
 
        }else{
479
 
            // logarithmic calculation with negative values
480
 
            if( dim.end <= 0 )
481
 
            {
482
 
                qreal min;
483
 
                const qreal minRaw = qMin( dim.start, dim.end );
484
 
                const int minLog = -static_cast<int>(trunc( log10( -minRaw ) ) );
485
 
                if( minLog >= 0 )
486
 
                    min = qMin( minRaw, -std::numeric_limits< qreal >::epsilon() );
487
 
                else
488
 
                    min = -fastPow10( -(minLog-1) );
489
 
            
490
 
                qreal max;
491
 
                const qreal maxRaw = qMin( -std::numeric_limits< qreal >::epsilon(), qMax( dim.start, dim.end ) );
492
 
                const int maxLog = -static_cast<int>(ceil( log10( -maxRaw ) ) );
493
 
                if( maxLog >= 0 )
494
 
                    max = -1;
495
 
                else if( fastPow10( -maxLog ) < maxRaw )
496
 
                    max = -fastPow10( -(maxLog+1) );
497
 
                else
498
 
                    max = -fastPow10( -maxLog );
499
 
                if( adjustLower )
500
 
                    dim.start = min;
501
 
                if( adjustUpper )
502
 
                    dim.end   = max;
503
 
                dim.stepWidth = -pow( 10.0, ceil( log10( qAbs( max - min ) / 10.0 ) ) );
504
 
            }
505
 
            // logarithmic calculation (ignoring all negative values)
506
 
            else
507
 
            {
508
 
                qreal min;
509
 
                const qreal minRaw = qMax( qMin( dim.start, dim.end ), qreal( 0.0 ) );
510
 
                const int minLog = static_cast<int>(trunc( log10( minRaw ) ) );
511
 
                if( minLog <= 0 && dim.end < 1.0 )
512
 
                    min = qMax( minRaw, std::numeric_limits< qreal >::epsilon() );
513
 
                else if( minLog <= 0 )
514
 
                    min = qMax( qreal(0.00001), dim.start );
515
 
                else
516
 
                    min = fastPow10( minLog-1 );
517
 
 
518
 
                // Uh oh. Logarithmic scaling doesn't work with a lower or upper
519
 
                // bound being 0.
520
 
                const bool zeroBound = dim.start == 0.0 || dim.end == 0.0;
521
 
 
522
 
                qreal max;
523
 
                const qreal maxRaw = qMax( qMax( dim.start, dim.end ), qreal( 0.0 ) );
524
 
                const int maxLog = static_cast<int>(ceil( log10( maxRaw ) ) );
525
 
                if( maxLog <= 0 )
526
 
                    max = 1;
527
 
                else if( fastPow10( maxLog ) < maxRaw )
528
 
                    max = fastPow10( maxLog+1 );
529
 
                else
530
 
                    max = fastPow10( maxLog );
531
 
                if( adjustLower || zeroBound )
532
 
                    dim.start = min;
533
 
                if( adjustUpper || zeroBound )
534
 
                    dim.end   = max;
535
 
                dim.stepWidth = pow( 10.0, ceil( log10( qAbs( max - min ) / 10.0 ) ) );
536
 
            }
537
 
        }
538
 
    }else{
539
 
        //qDebug() << "CartesianGrid::calculateGridXY() returns stepWidth 1.0  !!";
540
 
        // Do not ignore the user configuration
541
 
        dim.stepWidth = dim.stepWidth ? dim.stepWidth : 1.0;
542
 
    }
543
 
    return dim;
544
 
}
545
 
 
546
 
 
547
 
static void calculateSteps(
548
 
    qreal start_, qreal end_, const QList<qreal>& list,
549
 
    int minSteps, int maxSteps,
550
 
    int power,
551
 
    qreal& steps, qreal& stepWidth,
552
 
    bool adjustLower, bool adjustUpper )
553
 
{
554
 
    //qDebug("-----------------------------------\nstart: %f   end: %f   power-of-ten: %i", start_, end_, power);
555
 
 
556
 
    qreal distance;
557
 
    steps = 0.0;
558
 
 
559
 
    const int lastIdx = list.count()-1;
560
 
    for( int i = 0;  i <= lastIdx;  ++i ){
561
 
        const qreal testStepWidth = list.at(lastIdx - i) * fastPow10( power );
562
 
        //qDebug( "testing step width: %f", testStepWidth);
563
 
        qreal start = qMin( start_, end_ );
564
 
        qreal end   = qMax( start_, end_ );
565
 
        //qDebug("pre adjusting    start: %f   end: %f", start, end);
566
 
        AbstractGrid::adjustLowerUpperRange( start, end, testStepWidth, adjustLower, adjustUpper );
567
 
        //qDebug("post adjusting   start: %f   end: %f", start, end);
568
 
 
569
 
        const qreal testDistance = qAbs(end - start);
570
 
        const qreal testSteps    = testDistance / testStepWidth;
571
 
 
572
 
        //qDebug() << "testDistance:" << testDistance << "  distance:" << distance;
573
 
        if( (minSteps <= testSteps) && (testSteps <= maxSteps)
574
 
              && ( (steps == 0.0) || (testDistance <= distance) ) ){
575
 
            steps     = testSteps;
576
 
            stepWidth = testStepWidth;
577
 
            distance  = testDistance;
578
 
            //qDebug( "start: %f   end: %f   step width: %f   steps: %f   distance: %f", start, end, stepWidth, steps, distance);
579
 
        }
580
 
    }
581
 
}
582
 
 
583
 
 
584
 
void CartesianGrid::calculateStepWidth(
585
 
    qreal start_, qreal end_,
586
 
    const QList<qreal>& granularities,
587
 
    Qt::Orientation orientation,
588
 
    qreal& stepWidth, qreal& subStepWidth,
589
 
    bool adjustLower, bool adjustUpper ) const
590
 
{
591
 
    Q_UNUSED( orientation );
592
 
 
593
 
    Q_ASSERT_X ( granularities.count(), "CartesianGrid::calculateStepWidth",
594
 
                 "Error: The list of GranularitySequence values is empty." );
595
 
    QList<qreal> list( granularities );
596
 
    qSort( list );
597
 
 
598
 
    const qreal start = qMin( start_, end_);
599
 
    const qreal end   = qMax( start_, end_);
600
 
    const qreal distance = end - start;
601
 
    //qDebug( "raw data start: %f   end: %f", start, end);
602
 
 
603
 
    //FIXME(khz): make minSteps and maxSteps configurable by the user.
604
 
    const int minSteps = 2;
605
 
    const int maxSteps = 12;
606
 
 
607
 
    qreal steps;
608
 
    int power = 0;
609
 
    while( list.last() * fastPow10( power ) < distance ){
610
 
        ++power;
611
 
    };
612
 
    // We have the sequence *two* times in the calculation test list,
613
 
    // so we will be sure to find the best match:
614
 
    const int count = list.count();
615
 
    QList<qreal> testList;
616
 
 
617
 
    for( int dec = -1; dec == -1 || fastPow10( dec + 1 ) >= distance; --dec )
618
 
        for( int i = 0;  i < count;  ++i )
619
 
            testList << list.at(i) * fastPow10( dec );
620
 
 
621
 
    testList << list;
622
 
 
623
 
    do{
624
 
        //qDebug() << "list:" << testList;
625
 
        //qDebug( "calculating steps: power: %i", power);
626
 
        calculateSteps( start, end, testList, minSteps, maxSteps, power,
627
 
                        steps, stepWidth,
628
 
                        adjustLower, adjustUpper );
629
 
        --power;
630
 
    }while( steps == 0.0 );
631
 
    ++power;
632
 
    //qDebug( "steps calculated:  stepWidth: %f   steps: %f", stepWidth, steps);
633
 
 
634
 
    // find the matching sub-grid line width in case it is
635
 
    // not set by the user
636
 
 
637
 
    if (  subStepWidth == 0.0 ) {
638
 
        if( stepWidth == list.first() * fastPow10( power ) ){
639
 
            subStepWidth = list.last() * fastPow10( power-1 );
640
 
            //qDebug("A");
641
 
        }else if( stepWidth == list.first() * fastPow10( power-1 ) ){
642
 
            subStepWidth = list.last() * fastPow10( power-2 );
643
 
            //qDebug("B");
644
 
        }else{
645
 
            qreal smallerStepWidth = list.first();
646
 
            for( int i = 1;  i < list.count();  ++i ){
647
 
                if( stepWidth == list.at( i ) * fastPow10( power ) ){
648
 
                    subStepWidth = smallerStepWidth * fastPow10( power );
649
 
                    break;
650
 
                }
651
 
                if( stepWidth == list.at( i ) * fastPow10( power-1 ) ){
652
 
                    subStepWidth = smallerStepWidth * fastPow10( power-1 );
653
 
                    break;
654
 
                }
655
 
                smallerStepWidth = list.at( i );
656
 
            }
657
 
 
658
 
            //qDebug("C");
659
 
        }
660
 
    }
661
 
    //qDebug("CartesianGrid::calculateStepWidth() found stepWidth %f (%f steps) and sub-stepWidth %f", stepWidth, steps, subStepWidth);
662
 
}