📄 qwt_dial.cpp
字号:
void QwtDial::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
if ( !hasVisibleBackground() )
updateMask();
}
/*!
Paint the dial
\param e Paint event
*/
void QwtDial::paintEvent(QPaintEvent *e)
{
const QRect &ur = e->rect();
if ( ur.isValid() )
{
#if QT_VERSION < 0x040000
QwtPaintBuffer paintBuffer(this, ur);
QPainter &painter = *paintBuffer.painter();
#else
QPainter painter(this);
#endif
setAntialiasing(&painter, true);
painter.save();
drawContents(&painter);
painter.restore();
painter.save();
drawFrame(&painter);
painter.restore();
if ( hasFocus() )
drawFocusIndicator(&painter);
}
}
/*!
Draw a dotted round circle, if !isReadOnly()
\param painter Painter
*/
void QwtDial::drawFocusIndicator(QPainter *painter) const
{
if ( !isReadOnly() )
{
QRect focusRect = contentsRect();
const int margin = 2;
focusRect.setRect(
focusRect.x() + margin,
focusRect.y() + margin,
focusRect.width() - 2 * margin,
focusRect.height() - 2 * margin);
#if QT_VERSION < 0x040000
QColor color = colorGroup().color(QColorGroup::Base);
#else
QColor color = palette().color(QPalette::Base);
#endif
if (color.isValid())
{
const QColor gray(Qt::gray);
int h, s, v;
#if QT_VERSION < 0x040000
color.hsv(&h, &s, &v);
#else
color.getHsv(&h, &s, &v);
#endif
color = (v > 128) ? gray.dark(120) : gray.light(120);
}
else
color = Qt::darkGray;
painter->save();
painter->setBrush(Qt::NoBrush);
painter->setPen(QPen(color, 0, Qt::DotLine));
painter->drawEllipse(focusRect);
painter->restore();
}
}
/*!
Draw the frame around the dial
\param painter Painter
\sa lineWidth(), frameShadow()
*/
void QwtDial::drawFrame(QPainter *painter)
{
const int lw = lineWidth();
const int off = (lw + 1) % 2;
QRect r = boundingRect();
r.setRect(r.x() + lw / 2 - off, r.y() + lw / 2 - off,
r.width() - lw + off + 1, r.height() - lw + off + 1);
#if QT_VERSION >= 0x040000
#ifdef __GNUC__
#endif
r.setX(r.x() + 1);
r.setY(r.y() + 1);
r.setWidth(r.width() - 2);
r.setHeight(r.height() - 2);
#endif
if ( lw > 0 )
{
switch(d_data->frameShadow)
{
case QwtDial::Raised:
#if QT_VERSION < 0x040000
QwtPainter::drawRoundFrame(painter, r,
lw, colorGroup(), false);
#else
QwtPainter::drawRoundFrame(painter, r,
lw, palette(), false);
#endif
break;
case QwtDial::Sunken:
#if QT_VERSION < 0x040000
QwtPainter::drawRoundFrame(painter, r,
lw, colorGroup(), true);
#else
QwtPainter::drawRoundFrame(painter, r,
lw, palette(), true);
#endif
break;
default: // Plain
{
painter->save();
painter->setPen(QPen(Qt::black, lw));
painter->setBrush(Qt::NoBrush);
painter->drawEllipse(r);
painter->restore();
}
}
}
}
/*!
\brief Draw the contents inside the frame
QColorGroup::Background is the background color outside of the frame.
QColorGroup::Base is the background color inside the frame.
QColorGroup::Foreground is the background color inside the scale.
\param painter Painter
\sa boundingRect(), contentsRect(),
scaleContentsRect(), QWidget::setPalette
*/
void QwtDial::drawContents(QPainter *painter) const
{
#if QT_VERSION < 0x040000
if ( backgroundMode() == Qt::NoBackground ||
colorGroup().brush(QColorGroup::Base) !=
colorGroup().brush(QColorGroup::Background) )
#else
if ( testAttribute(Qt::WA_NoSystemBackground) ||
palette().brush(QPalette::Base) !=
palette().brush(QPalette::Background) )
#endif
{
const QRect br = boundingRect();
painter->save();
painter->setPen(Qt::NoPen);
#if QT_VERSION < 0x040000
painter->setBrush(colorGroup().brush(QColorGroup::Base));
#else
painter->setBrush(palette().brush(QPalette::Base));
#endif
painter->drawEllipse(br);
painter->restore();
}
const QRect insideScaleRect = scaleContentsRect();
#if QT_VERSION < 0x040000
if ( colorGroup().brush(QColorGroup::Foreground) !=
colorGroup().brush(QColorGroup::Base) )
#else
if ( palette().brush(QPalette::Foreground) !=
palette().brush(QPalette::Base) )
#endif
{
painter->save();
painter->setPen(Qt::NoPen);
#if QT_VERSION < 0x040000
painter->setBrush(colorGroup().brush(QColorGroup::Foreground));
#else
painter->setBrush(palette().brush(QPalette::Foreground));
#endif
painter->drawEllipse(insideScaleRect.x() - 1, insideScaleRect.y() - 1,
insideScaleRect.width(), insideScaleRect.height() );
painter->restore();
}
const QPoint center = insideScaleRect.center();
const int radius = insideScaleRect.width() / 2;
painter->save();
drawScaleContents(painter, center, radius);
painter->restore();
double direction = d_data->origin;
if (isValid())
{
direction = d_data->origin + d_data->minScaleArc;
if ( maxValue() > minValue() && d_data->maxScaleArc > d_data->minScaleArc )
{
const double ratio =
(value() - minValue()) / (maxValue() - minValue());
direction += ratio * (d_data->maxScaleArc - d_data->minScaleArc);
}
if ( direction >= 360.0 )
direction -= 360.0;
}
double origin = d_data->origin;
if ( mode() == RotateScale )
{
origin -= direction - d_data->origin;
direction = d_data->origin;
}
painter->save();
drawScale(painter, center, radius, origin, d_data->minScaleArc, d_data->maxScaleArc);
painter->restore();
if ( isValid() )
{
QPalette::ColorGroup cg;
if ( isEnabled() )
cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
else
cg = QPalette::Disabled;
painter->save();
drawNeedle(painter, center, radius, direction, cg);
painter->restore();
}
}
/*!
Draw the needle
\param painter Painter
\param center Center of the dial
\param radius Length for the needle
\param direction Direction of the needle in degrees, counter clockwise
\param cg ColorGroup
*/
void QwtDial::drawNeedle(QPainter *painter, const QPoint ¢er,
int radius, double direction, QPalette::ColorGroup cg) const
{
if ( d_data->needle )
{
direction = 360.0 - direction; // counter clockwise
d_data->needle->draw(painter, center, radius, direction, cg);
}
}
/*!
Draw the scale
\param painter Painter
\param center Center of the dial
\param radius Radius of the scale
\param origin Origin of the scale
\param minArc Minimum of the arc
\param maxArc Minimum of the arc
\sa QwtAbstractScaleDraw::setAngleRange
*/
void QwtDial::drawScale(QPainter *painter, const QPoint ¢er,
int radius, double origin, double minArc, double maxArc) const
{
if ( d_data->scaleDraw == NULL )
return;
origin -= 270.0; // hardcoded origin of QwtScaleDraw
double angle = maxArc - minArc;
if ( angle > 360.0 )
angle = fmod(angle, 360.0);
minArc += origin;
if ( minArc < -360.0 )
minArc = fmod(minArc, 360.0);
maxArc = minArc + angle;
if ( maxArc > 360.0 )
{
// QwtAbstractScaleDraw::setAngleRange accepts only values
// in the range [-360.0..360.0]
minArc -= 360.0;
maxArc -= 360.0;
}
painter->setFont(font());
d_data->scaleDraw->setAngleRange(minArc, maxArc);
d_data->scaleDraw->setRadius(radius);
d_data->scaleDraw->moveCenter(center);
#if QT_VERSION < 0x040000
QColorGroup cg = colorGroup();
const QColor textColor = cg.color(QColorGroup::Text);
cg.setColor(QColorGroup::Foreground, textColor);
painter->setPen(QPen(textColor, d_data->scaleDraw->penWidth()));
d_data->scaleDraw->draw(painter, cg);
#else
QPalette pal = palette();
const QColor textColor = pal.color(QPalette::Text);
pal.setColor(QPalette::Foreground, textColor); //ticks, backbone
painter->setPen(QPen(textColor, d_data->scaleDraw->penWidth()));
d_data->scaleDraw->draw(painter, pal);
#endif
}
void QwtDial::drawScaleContents(QPainter *,
const QPoint &, int) const
{
// empty default implementation
}
/*!
Set a needle for the dial
Qwt is missing a set of good looking needles.
Contributions are very welcome.
\param needle Needle
\warning The needle will be deleted, when a different needle is
set or in ~QwtDial()
*/
void QwtDial::setNeedle(QwtDialNeedle *needle)
{
if ( needle != d_data->needle )
{
if ( d_data->needle )
delete d_data->needle;
d_data->needle = needle;
update();
}
}
/*!
\return needle
\sa setNeedle()
*/
const QwtDialNeedle *QwtDial::needle() const
{
return d_data->needle;
}
/*!
\return needle
\sa setNeedle()
*/
QwtDialNeedle *QwtDial::needle()
{
return d_data->needle;
}
//! QwtDoubleRange update hook
void QwtDial::rangeChange()
{
updateScale();
}
/*!
Update the scale with the current attributes
\sa setScale()
*/
void QwtDial::updateScale()
{
if ( d_data->scaleDraw )
{
QwtLinearScaleEngine scaleEngine;
const QwtScaleDiv scaleDiv = scaleEngine.divideScale(
minValue(), maxValue(),
d_data->maxMajIntv, d_data->maxMinIntv, d_data->scaleStep);
d_data->scaleDraw->setTransformation(scaleEngine.transformation());
d_data->scaleDraw->setScaleDiv(scaleDiv);
}
}
//! Return the scale draw
QwtDialScaleDraw *QwtDial::scaleDraw()
{
return d_data->scaleDraw;
}
//! Return the scale draw
const QwtDialScaleDraw *QwtDial::scaleDraw() const
{
return d_data->scaleDraw;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -