📄 qwt_wheel.cpp
字号:
/* -*- 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 <qevent.h>
#include <qdrawutil.h>
#include <qpainter.h>
#include <qstyle.h>
#include "qwt_math.h"
#include "qwt_painter.h"
#include "qwt_paint_buffer.h"
#include "qwt_wheel.h"
#define NUM_COLORS 30
class QwtWheel::PrivateData
{
public:
PrivateData()
{
viewAngle = 175.0;
totalAngle = 360.0;
tickCnt = 10;
intBorder = 2;
borderWidth = 2;
wheelWidth = 20;
#if QT_VERSION < 0x040000
allocContext = 0;
#endif
};
QRect sliderRect;
double viewAngle;
double totalAngle;
int tickCnt;
int intBorder;
int borderWidth;
int wheelWidth;
#if QT_VERSION < 0x040000
int allocContext;
#endif
QColor colors[NUM_COLORS];
};
//! Constructor
QwtWheel::QwtWheel(QWidget *parent):
QwtAbstractSlider(Qt::Horizontal, parent)
{
initWheel();
}
#if QT_VERSION < 0x040000
QwtWheel::QwtWheel(QWidget *parent, const char *name):
QwtAbstractSlider(Qt::Horizontal, parent)
{
setName(name);
initWheel();
}
#endif
void QwtWheel::initWheel()
{
d_data = new PrivateData;
#if QT_VERSION < 0x040000
setWFlags(Qt::WNoAutoErase);
#endif
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
#if QT_VERSION >= 0x040000
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
#else
clearWState( WState_OwnSizePolicy );
#endif
setUpdateTime(50);
}
//! Destructor
QwtWheel::~QwtWheel()
{
#if QT_VERSION < 0x040000
if ( d_data->allocContext )
QColor::destroyAllocContext( d_data->allocContext );
#endif
delete d_data;
}
//! Set up the color array for the background pixmap.
void QwtWheel::setColorArray()
{
if ( !d_data->colors )
return;
#if QT_VERSION < 0x040000
const QColor light = colorGroup().light();
const QColor dark = colorGroup().dark();
#else
const QColor light = palette().color(QPalette::Light);
const QColor dark = palette().color(QPalette::Dark);
#endif
if ( !d_data->colors[0].isValid() ||
d_data->colors[0] != light ||
d_data->colors[NUM_COLORS - 1] != dark )
{
#if QT_VERSION < 0x040000
if ( d_data->allocContext )
QColor::destroyAllocContext( d_data->allocContext );
d_data->allocContext = QColor::enterAllocContext();
#endif
d_data->colors[0] = light;
d_data->colors[NUM_COLORS - 1] = dark;
int dh, ds, dv, lh, ls, lv;
#if QT_VERSION < 0x040000
d_data->colors[0].rgb(&lh, &ls, &lv);
d_data->colors[NUM_COLORS - 1].rgb(&dh, &ds, &dv);
#else
d_data->colors[0].getRgb(&lh, &ls, &lv);
d_data->colors[NUM_COLORS - 1].getRgb(&dh, &ds, &dv);
#endif
for ( int i = 1; i < NUM_COLORS - 1; ++i )
{
const double factor = double(i) / double(NUM_COLORS);
d_data->colors[i].setRgb( lh + int( double(dh - lh) * factor ),
ls + int( double(ds - ls) * factor ),
lv + int( double(dv - lv) * factor ));
}
#if QT_VERSION < 0x040000
QColor::leaveAllocContext();
#endif
}
}
/*!
\brief Adjust the number of grooves in the wheel's surface.
The number of grooves is limited to 6 <= cnt <= 50.
Values outside this range will be clipped.
The default value is 10.
\param cnt Number of grooves per 360 degrees
*/
void QwtWheel::setTickCnt(int cnt)
{
d_data->tickCnt = qwtLim( cnt, 6, 50 );
update();
}
int QwtWheel::tickCnt() const
{
return d_data->tickCnt;
}
/*!
\return mass
*/
double QwtWheel::mass() const
{
return QwtAbstractSlider::mass();
}
/*!
\brief Set the internal border width of the wheel.
The internal border must not be smaller than 1
and is limited in dependence on the wheel's size.
Values outside the allowed range will be clipped.
The internal border defaults to 2.
\param w border width
*/
void QwtWheel::setInternalBorder( int w )
{
const int d = qwtMin( width(), height() ) / 3;
w = qwtMin( w, d );
d_data->intBorder = qwtMax( w, 1 );
layoutWheel();
}
int QwtWheel::internalBorder() const
{
return d_data->intBorder;
}
//! Draw the Wheel's background gradient
void QwtWheel::drawWheelBackground( QPainter *p, const QRect &r )
{
p->save();
//
// initialize pens
//
#if QT_VERSION < 0x040000
const QColor light = colorGroup().light();
const QColor dark = colorGroup().dark();
#else
const QColor light = palette().color(QPalette::Light);
const QColor dark = palette().color(QPalette::Dark);
#endif
QPen lightPen;
lightPen.setColor(light);
lightPen.setWidth(d_data->intBorder);
QPen darkPen;
darkPen.setColor(dark);
darkPen.setWidth(d_data->intBorder);
setColorArray();
//
// initialize auxiliary variables
//
const int nFields = NUM_COLORS * 13 / 10;
const int hiPos = nFields - NUM_COLORS + 1;
if ( orientation() == Qt::Horizontal )
{
const int rx = r.x();
int ry = r.y() + d_data->intBorder;
const int rh = r.height() - 2* d_data->intBorder;
const int rw = r.width();
//
// draw shaded background
//
int x1 = rx;
for (int i = 1; i < nFields; i++ )
{
const int x2 = rx + (rw * i) / nFields;
p->fillRect(x1, ry, x2-x1 + 1 ,rh, d_data->colors[qwtAbs(i-hiPos)]);
x1 = x2 + 1;
}
p->fillRect(x1, ry, rw - (x1 - rx), rh, d_data->colors[NUM_COLORS - 1]);
//
// draw internal border
//
p->setPen(lightPen);
ry = r.y() + d_data->intBorder / 2;
p->drawLine(r.x(), ry, r.x() + r.width() , ry);
p->setPen(darkPen);
ry = r.y() + r.height() - (d_data->intBorder - d_data->intBorder / 2);
p->drawLine(r.x(), ry , r.x() + r.width(), ry);
}
else // Qt::Vertical
{
int rx = r.x() + d_data->intBorder;
const int ry = r.y();
const int rh = r.height();
const int rw = r.width() - 2 * d_data->intBorder;
//
// draw shaded background
//
int y1 = ry;
for ( int i = 1; i < nFields; i++ )
{
const int y2 = ry + (rh * i) / nFields;
p->fillRect(rx, y1, rw, y2-y1 + 1, d_data->colors[qwtAbs(i-hiPos)]);
y1 = y2 + 1;
}
p->fillRect(rx, y1, rw, rh - (y1 - ry), d_data->colors[NUM_COLORS - 1]);
//
// draw internal borders
//
p->setPen(lightPen);
rx = r.x() + d_data->intBorder / 2;
p->drawLine(rx, r.y(), rx, r.y() + r.height());
p->setPen(darkPen);
rx = r.x() + r.width() - (d_data->intBorder - d_data->intBorder / 2);
p->drawLine(rx, r.y(), rx , r.y() + r.height());
}
p->restore();
}
/*!
\brief Set the total angle which the wheel can be turned.
One full turn of the wheel corresponds to an angle of
360 degrees. A total angle of n*360 degrees means
that the wheel has to be turned n times around its axis
to get from the minimum value to the maximum value.
The default setting of the total angle is 360 degrees.
\param angle total angle in degrees
*/
void QwtWheel::setTotalAngle(double angle)
{
if ( angle < 0.0 )
angle = 0.0;
d_data->totalAngle = angle;
update();
}
double QwtWheel::totalAngle() const
{
return d_data->totalAngle;
}
/*!
\brief Set the wheel's orientation.
\param o Orientation. Allowed values are
Qt::Horizontal and Qt::Vertical.
Defaults to Qt::Horizontal.
\sa QwtAbstractSlider::orientation()
*/
void QwtWheel::setOrientation(Qt::Orientation o)
{
if ( orientation() == o )
return;
#if QT_VERSION >= 0x040000
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -