Skip to content
qwt_dial.cpp 28.3 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
 *****************************************************************************/

#include <math.h>
#include <qpainter.h>
#if QT_VERSION >= 0x040000
#include <qbitmap.h>
#include <qpalette.h>
#endif
#include <qpixmap.h>
#include <qevent.h>
#include "qwt_math.h"
#include "qwt_scale_engine.h"
#include "qwt_scale_map.h"
#include "qwt_paint_buffer.h"
#include "qwt_painter.h"
#include "qwt_dial_needle.h"
#include "qwt_dial.h"

class QwtDial::PrivateData
{
public:
    PrivateData():
        visibleBackground(true),
        frameShadow(Sunken),
        lineWidth(0),
        mode(RotateNeedle),
        origin(90.0),
        minScaleArc(0.0),
        maxScaleArc(0.0),
        scaleDraw(0),
        maxMajIntv(36),
        maxMinIntv(10),
        scaleStep(0.0),
        needle(0) {
pixhawk's avatar
pixhawk committed
    }

    ~PrivateData() {
pixhawk's avatar
pixhawk committed
        delete scaleDraw;
        delete needle;
    }
    bool visibleBackground;
    Shadow frameShadow;
    int lineWidth;

    QwtDial::Mode mode;

    double origin;
    double minScaleArc;
    double maxScaleArc;

    QwtDialScaleDraw *scaleDraw;
    int maxMajIntv;
    int maxMinIntv;
    double scaleStep;

    QwtDialNeedle *needle;

    static double previousDir;
};

double QwtDial::PrivateData::previousDir = -1.0;

/*!
  Constructor

  \param parent Parent dial widget
*/
QwtDialScaleDraw::QwtDialScaleDraw(QwtDial *parent):
    d_parent(parent),
    d_penWidth(1)
{
}

/*!
  Set the pen width used for painting the scale

  \param penWidth Pen width
  \sa penWidth(), QwtDial::drawScale()
*/
pixhawk's avatar
pixhawk committed
void QwtDialScaleDraw::setPenWidth(uint penWidth)
{
    d_penWidth = penWidth;
}

/*!
  \return Pen width used for painting the scale
  \sa setPenWidth, QwtDial::drawScale()
*/
uint QwtDialScaleDraw::penWidth() const
{
    return d_penWidth;
}

pixhawk's avatar
pixhawk committed
  Call QwtDial::scaleLabel of the parent dial widget.

  \param value Value to display
pixhawk's avatar
pixhawk committed
  \sa QwtDial::scaleLabel
pixhawk's avatar
pixhawk committed
QwtText QwtDialScaleDraw::label(double value) const
{
    if ( d_parent == NULL )
        return QwtRoundScaleDraw::label(value);

    return d_parent->scaleLabel(value);
}

/*!
  \brief Constructor
  \param parent Parent widget

  Create a dial widget with no scale and no needle.
pixhawk's avatar
pixhawk committed
  The default origin is 90.0 with no valid value. It accepts
  mouse and keyboard inputs and has no step size. The default mode
  is QwtDial::RotateNeedle.
pixhawk's avatar
pixhawk committed

QwtDial::QwtDial(QWidget* parent):
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    initDial();
}

#if QT_VERSION < 0x040000
/*!
  \brief Constructor
  \param parent Parent widget
  \param name Object name

  Create a dial widget with no scale and no needle.
pixhawk's avatar
pixhawk committed
  The default origin is 90.0 with no valid value. It accepts
  mouse and keyboard inputs and has no step size. The default mode
  is QwtDial::RotateNeedle.
pixhawk's avatar
pixhawk committed
QwtDial::QwtDial(QWidget* parent, const char *name):
    QwtAbstractSlider(Qt::Horizontal, parent)
{
    setName(name);
    initDial();
}
#endif

void QwtDial::initDial()
{
    d_data = new PrivateData;

#if QT_VERSION < 0x040000
    setWFlags(Qt::WNoAutoErase);
pixhawk's avatar
pixhawk committed

#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif
    setFocusPolicy(TabFocus);

    QPalette p = palette();
    for ( int i = 0; i < QPalette::NColorGroups; i++ ) {
pixhawk's avatar
pixhawk committed
        const QPalette::ColorGroup cg = (QPalette::ColorGroup)i;

        // Base: background color of the circle inside the frame.
        // Foreground: background color of the circle inside the scale

#if QT_VERSION < 0x040000
        p.setColor(cg, QColorGroup::Foreground,
                   p.color(cg, QColorGroup::Base));
pixhawk's avatar
pixhawk committed
#else
        p.setColor(cg, QPalette::Foreground,
                   p.color(cg, QPalette::Base));
pixhawk's avatar
pixhawk committed
#endif
    }
    setPalette(p);

    d_data->scaleDraw = new QwtDialScaleDraw(this);
    d_data->scaleDraw->setRadius(0);

    setScaleArc(0.0, 360.0); // scale as a full circle
    setRange(0.0, 360.0, 1.0, 10); // degrees as deafult
}

//!  Destructor
QwtDial::~QwtDial()
pixhawk's avatar
pixhawk committed
{
    delete d_data;
}

/*!
  Show/Hide the area outside of the frame
  \param show Show if true, hide if false

  \sa hasVisibleBackground(), setMask()
  \warning When QwtDial is a toplevel widget the window
Loading
Loading full blame...