⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qwt_dial.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/* -*- 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 <qpaintengine.h>
#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"

#if QT_VERSION >= 0x040000
static void setAntialiasing(QPainter *painter, bool on)
{
    QPaintEngine *engine = painter->paintEngine();
    if ( engine && engine->hasFeature(QPaintEngine::Antialiasing) )
        painter->setRenderHint(QPainter::Antialiasing, on);
}
#else
static void setAntialiasing(QPainter *, bool)
{
}
#endif

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)
    {
    }

    ~PrivateData()
    {
        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()
*/
    
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;
}

/*! 
  Call QwtDial::scaleLabel of the parent dial widget.

  \param value Value to display
    
  \sa QwtDial::scaleLabel
*/  
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. 
  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.
*/  

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. 
  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.
*/  
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);
#endif 

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

    QPalette p = palette();
    for ( int i = 0; i < QPalette::NColorGroups; i++ )
    {
        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));
#else
        p.setColor(cg, QPalette::Foreground, 
            p.color(cg, QPalette::Base));
#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() 
{
    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
           border might disappear too.
*/
void QwtDial::showBackground(bool show)
{
    if ( d_data->visibleBackground != show )
    {
        d_data->visibleBackground = show;
        updateMask();
    }
}

/*!
  true when the area outside of the frame is visible

  \sa showBackground(), setMask()
*/
bool QwtDial::hasVisibleBackground() const 
{ 
    return d_data->visibleBackground; 
}

/*!
  Sets the frame shadow value from the frame style.
  \param shadow Frame shadow
  \sa setLineWidth(), QFrame::setFrameShadow()
*/
void QwtDial::setFrameShadow(Shadow shadow)
{
    if ( shadow != d_data->frameShadow )
    {
        d_data->frameShadow = shadow;
        if ( lineWidth() > 0 )
            update();
    }
}

/*!
  \return Frame shadow
  /sa setFrameShadow(), lineWidth(), QFrame::frameShadow
*/
QwtDial::Shadow QwtDial::frameShadow() const 
{ 
    return d_data->frameShadow; 
}

/*!
  Sets the line width

  \param lineWidth Line width
  \sa setFrameShadow()
*/
void QwtDial::setLineWidth(int lineWidth)
{
    if ( lineWidth < 0 )
        lineWidth = 0;

    if ( d_data->lineWidth != lineWidth )
    {
        d_data->lineWidth = lineWidth;
        update();
    }
}

/*!
  \return Line width of the frame
  \sa setLineWidth(), frameShadow(), lineWidth()
*/
int QwtDial::lineWidth() const 
{ 
    return d_data->lineWidth; 
}

/*!
  \return bounding rect of the circle inside the frame
  \sa setLineWidth(), scaleContentsRect(), boundingRect()
*/
QRect QwtDial::contentsRect() const
{
    const int lw = lineWidth();

    QRect r = boundingRect();
    if ( lw > 0 )
    {
        r.setRect(r.x() + lw, r.y() + lw, 
            r.width() - 2 * lw, r.height() - 2 * lw);
    }
    return r;
}

/*!
  \return bounding rect of the dial including the frame
  \sa setLineWidth(), scaleContentsRect(), contentsRect()
*/
QRect QwtDial::boundingRect() const
{
    const int radius = qwtMin(width(), height()) / 2;

    QRect r(0, 0, 2 * radius, 2 * radius);
    r.moveCenter(rect().center());
    return r;
}

/*!
  \return rect inside the scale
  \sa setLineWidth(), boundingRect(), contentsRect()
*/
QRect QwtDial::scaleContentsRect() const
{
#if QT_VERSION < 0x040000
    const QPen scalePen(colorGroup().text(), 0, Qt::NoPen);
#else
    const QPen scalePen(palette().text(), 0, Qt::NoPen);
#endif

    int scaleDist = 0;
    if ( d_data->scaleDraw )
    {
        scaleDist = d_data->scaleDraw->extent(scalePen, font());
        scaleDist++; // margin
    }

    const QRect rect = contentsRect();
    return QRect(rect.x() + scaleDist, rect.y() + scaleDist,
        rect.width() - 2 * scaleDist, rect.height() - 2 * scaleDist);
}

/*!
  \brief Change the mode of the meter.
  \param mode New mode 
    
  The value of the meter is indicated by the difference
  between north of the scale and the direction of the needle.
  In case of QwtDial::RotateNeedle north is pointing
  to the origin() and the needle is rotating, in case of
  QwtDial::RotateScale, the needle points to origin()
  and the scale is rotating.
    
  The default mode is QwtDial::RotateNeedle.

  \sa mode(), setValue(), setOrigin()
*/  
void QwtDial::setMode(Mode mode)
{   
    if ( mode != d_data->mode )
    {
        d_data->mode = mode;
        update(); 
    }
}       

/*! 
  \return mode of the dial.
    
  The value of the dial is indicated by the difference
  between the origin and the direction of the needle.
  In case of QwtDial::RotateNeedle the scale arc is fixed
  to the origin() and the needle is rotating, in case of
  QwtDial::RotateScale, the needle points to origin()
  and the scale is rotating.
 
  The default mode is QwtDial::RotateNeedle.

  \sa setMode(), origin(), setScaleArc(), value()
*/
QwtDial::Mode QwtDial::mode() const
{
    return d_data->mode;
}

/*! 
    Sets whether it is possible to step the value from the highest value to 
    the lowest value and vice versa to on.

    \param wrapping en/disables wrapping

    \sa wrapping(), QwtDoubleRange::periodic()
    \note The meaning of wrapping is like the wrapping property of QSpinBox,
          but not like it is used in QDial. 
*/
void QwtDial::setWrapping(bool wrapping)
{
    setPeriodic(wrapping);
} 

/*! 
    wrapping() holds whether it is possible to step the value from the 
    highest value to the lowest value and vice versa. 

    \sa setWrapping(), QwtDoubleRange::setPeriodic()
    \note The meaning of wrapping is like the wrapping property of QSpinBox,
          but not like it is used in QDial. 
*/ 
bool QwtDial::wrapping() const
{
    return periodic();
}

/*! 
   Resize the dial widget
   \param e Resize event
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -