Skip to content
qwt_thermo.cpp 22.5 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- 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
 *****************************************************************************/

Bryant's avatar
Bryant committed
#include "qwt_thermo.h"
pixhawk's avatar
pixhawk committed
#include "qwt_scale_engine.h"
#include "qwt_scale_draw.h"
#include "qwt_scale_map.h"
Bryant's avatar
Bryant committed
#include "qwt_color_map.h"
#include <qpainter.h>
#include <qevent.h>
#include <qdrawutil.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qmath.h>

static inline void qwtDrawLine( QPainter *painter, int pos, 
    const QColor &color, const QRect &pipeRect, const QRect &liquidRect,
    Qt::Orientation orientation )
{
    painter->setPen( color );
    if ( orientation == Qt::Horizontal )
    {
        if ( pos >= liquidRect.left() && pos < liquidRect.right() )
            painter->drawLine( pos, pipeRect.top(), pos, pipeRect.bottom() );
    }
    else
    {
        if ( pos >= liquidRect.top() && pos < liquidRect.bottom() )
            painter->drawLine( pipeRect.left(), pos, pipeRect.right(), pos );
    }
}

QVector<double> qwtTickList( const QwtScaleDiv &scaleDiv )
{
    QVector<double> values;

    double lowerLimit = scaleDiv.interval().minValue();
    double upperLimit = scaleDiv.interval().maxValue();

    if ( upperLimit < lowerLimit )
        qSwap( lowerLimit, upperLimit );

    values += lowerLimit;

    for ( int tickType = QwtScaleDiv::MinorTick;
        tickType < QwtScaleDiv::NTickTypes; tickType++ )
    {
        const QList<double> ticks = scaleDiv.ticks( tickType );

        for ( int i = 0; i < ticks.count(); i++ )
        {
            const double v = ticks[i];
            if ( v > lowerLimit && v < upperLimit )
                values += v;
        }       
    }   

    values += upperLimit;
    
    return values;
}
pixhawk's avatar
pixhawk committed

class QwtThermo::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
        orientation( Qt::Vertical ),
        scalePosition( QwtThermo::TrailingScale ),
        spacing( 3 ),
        borderWidth( 2 ),
        pipeWidth( 10 ),
        alarmLevel( 0.0 ),
        alarmEnabled( false ),
        autoFillPipe( true ),
        originMode( QwtThermo::OriginMinimum ),
        origin( 0.0 ),
        colorMap( NULL ),
        value( 0.0 )
    {
        rangeFlags = QwtInterval::IncludeBorders;
    }

    ~PrivateData()
    {
        delete colorMap;
    }
pixhawk's avatar
pixhawk committed

    Qt::Orientation orientation;
Bryant's avatar
Bryant committed
    QwtThermo::ScalePosition scalePosition;

    int spacing;
pixhawk's avatar
pixhawk committed
    int borderWidth;
Bryant's avatar
Bryant committed
    int pipeWidth;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    QwtInterval::BorderFlags rangeFlags;
pixhawk's avatar
pixhawk committed
    double alarmLevel;
    bool alarmEnabled;
Bryant's avatar
Bryant committed
    bool autoFillPipe;
    QwtThermo::OriginMode originMode;
    double origin;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    QwtColorMap *colorMap;

    double value;
};
pixhawk's avatar
pixhawk committed

pixhawk's avatar
pixhawk committed
  Constructor
  \param parent Parent widget
*/
Bryant's avatar
Bryant committed
QwtThermo::QwtThermo( QWidget *parent ):
    QwtAbstractScale( parent )
pixhawk's avatar
pixhawk committed
{
    d_data = new PrivateData;

Bryant's avatar
Bryant committed
    QSizePolicy policy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
    if ( d_data->orientation == Qt::Vertical )
pixhawk's avatar
pixhawk committed
        policy.transpose();

Bryant's avatar
Bryant committed
    setSizePolicy( policy );
Bryant's avatar
Bryant committed
    setAttribute( Qt::WA_WState_OwnSizePolicy, false );
    layoutThermo( true );
pixhawk's avatar
pixhawk committed
}

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

Bryant's avatar
Bryant committed
/*!
  \brief Exclude/Include min/max values
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  According to the flags minValue() and maxValue()
  are included/excluded from the pipe. In case of an
  excluded value the corresponding tick is painted
  1 pixel off of the pipeRect().
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  F.e. when a minimum
  of 0.0 has to be displayed as an empty pipe the minValue()
  needs to be excluded.

  \param flags Range flags
  \sa rangeFlags()
*/
void QwtThermo::setRangeFlags( QwtInterval::BorderFlags flags )
Bryant's avatar
Bryant committed
    if ( d_data->rangeFlags != flags )
    {
        d_data->rangeFlags = flags;
        update();
    }
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
/*!
  \return Range flags
  \sa setRangeFlags()
*/
QwtInterval::BorderFlags QwtThermo::rangeFlags() const
Bryant's avatar
Bryant committed
    return d_data->rangeFlags;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
/*!
  Set the current value.

  \param value New Value
  \sa value()
*/
void QwtThermo::setValue( double value )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( d_data->value != value )
    {
        d_data->value = value;
pixhawk's avatar
pixhawk committed
        update();
    }
}

//! Return the value.
double QwtThermo::value() const
{
    return d_data->value;
pixhawk's avatar
pixhawk committed
}

/*!
  \brief Set a scale draw

  For changing the labels of the scales, it
  is necessary to derive from QwtScaleDraw and
  overload QwtScaleDraw::label().

  \param scaleDraw ScaleDraw object, that has to be created with
Bryant's avatar
Bryant committed
                   new and will be deleted in ~QwtThermo() or the next
Loading
Loading full blame...