qwt_scale_draw.cpp 22.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
#include "qwt_scale_draw.h"
pixhawk's avatar
pixhawk committed
11 12
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
Bryant's avatar
Bryant committed
13 14 15 16 17
#include "qwt_math.h"
#include "qwt_painter.h"
#include <qpen.h>
#include <qpainter.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed
18

Bryant's avatar
Bryant committed
19 20 21
#if QT_VERSION < 0x040601
#define qFastSin(x) qSin(x)
#define qFastCos(x) qCos(x)
pixhawk's avatar
pixhawk committed
22 23 24 25 26 27
#endif

class QwtScaleDraw::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
28 29 30 31 32
        len( 0 ),
        alignment( QwtScaleDraw::BottomScale ),
        labelAlignment( 0 ),
        labelRotation( 0.0 )
    {
pixhawk's avatar
pixhawk committed
33 34
    }

Bryant's avatar
Bryant committed
35 36
    QPointF pos;
    double len;
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    Alignment alignment;

    Qt::Alignment labelAlignment;
    double labelRotation;
};

/*!
  \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()
{
    d_data = new QwtScaleDraw::PrivateData;
Bryant's avatar
Bryant committed
54
    setLength( 100 );
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60 61 62
}

//! Destructor
QwtScaleDraw::~QwtScaleDraw()
{
    delete d_data;
}

63
/*!
pixhawk's avatar
pixhawk committed
64 65
   Return alignment of the scale
   \sa setAlignment()
Bryant's avatar
Bryant committed
66
   \return Alignment of the scale
pixhawk's avatar
pixhawk committed
67
*/
68
QwtScaleDraw::Alignment QwtScaleDraw::alignment() const
pixhawk's avatar
pixhawk committed
69
{
70
    return d_data->alignment;
pixhawk's avatar
pixhawk committed
71 72 73 74 75
}

/*!
   Set the alignment of the scale

Bryant's avatar
Bryant committed
76 77
   \param align Alignment of the scale

pixhawk's avatar
pixhawk committed
78 79 80
   The default alignment is QwtScaleDraw::BottomScale
   \sa alignment()
*/
Bryant's avatar
Bryant committed
81
void QwtScaleDraw::setAlignment( Alignment align )
pixhawk's avatar
pixhawk committed
82 83 84 85 86 87 88 89 90 91
{
    d_data->alignment = align;
}

/*!
  Return the orientation

  TopScale, BottomScale are horizontal (Qt::Horizontal) scales,
  LeftScale, RightScale are vertical (Qt::Vertical) scales.

Bryant's avatar
Bryant committed
92 93
  \return Orientation of the scale

pixhawk's avatar
pixhawk committed
94 95 96 97
  \sa alignment()
*/
Qt::Orientation QwtScaleDraw::orientation() const
{
Bryant's avatar
Bryant committed
98 99 100 101 102 103 104 105 106
    switch ( d_data->alignment )
    {
        case TopScale:
        case BottomScale:
            return Qt::Horizontal;
        case LeftScale:
        case RightScale:
        default:
            return Qt::Vertical;
pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113 114 115 116 117 118 119
    }
}

/*!
  \brief Determine the minimum border distance

  This member function returns the minimum space
  needed to draw the mark labels at the scale's endpoints.

  \param font Font
  \param start Start border distance
  \param end End border distance
*/
Bryant's avatar
Bryant committed
120 121
void QwtScaleDraw::getBorderDistHint( 
    const QFont &font, int &start, int &end ) const
pixhawk's avatar
pixhawk committed
122 123 124
{
    start = 0;
    end = 0;
125

Bryant's avatar
Bryant committed
126
    if ( !hasComponent( QwtAbstractScaleDraw::Labels ) )
pixhawk's avatar
pixhawk committed
127 128
        return;

Bryant's avatar
Bryant committed
129
    const QList<double> &ticks = scaleDiv().ticks( QwtScaleDiv::MajorTick );
130
    if ( ticks.count() == 0 )
pixhawk's avatar