void caCircularGauge::paintEvent(QPaintEvent *) { QPainter painter(this); int size = qMin(height(), width()); /* see http://doc.trolltech.com/4.4/coordsys.html#window-viewport-conversion for a analogous example */ painter.setRenderHint(QPainter::Antialiasing); painter.setViewport((int)((width()-size)*.5),(int)((height()-size)*.5),size,size); painter.setWindow(-50,-50,100,100); drawColorBar(&painter); if (isEnabled()) drawNeedle(&painter); if (m_valueDisplayed) drawValue(&painter); if (!isEnabled()) { QColor c = palette().color(QPalette::Background); c.setAlpha(200); painter.fillRect(painter.window(), c); } if (m_scaleEnabled) { drawScale(&painter); drawLabels(&painter); } }
/*! Draw the needle \param painter Painter \param center Center of the dial, start position for the needle \param length Length of the needle \param direction Direction of the needle, in degrees counter clockwise \param colorGroup Color group, used for painting */ void QwtDialNeedle::draw( QPainter *painter, const QPointF ¢er, double length, double direction, QPalette::ColorGroup colorGroup ) const { painter->save(); painter->translate( center ); painter->rotate( -direction ); drawNeedle( painter, length, colorGroup ); painter->restore(); }
/*! \brief Draw the contents inside the frame QPalette::Window is the background color outside of the frame. QPalette::Base is the background color inside the frame. QPalette::WindowText is the background color inside the scale. \param painter Painter \sa boundingRect(), innerRect(), scaleInnerRect(), QWidget::setPalette() */ void QwtDial::drawContents( QPainter *painter ) const { if ( testAttribute( Qt::WA_NoSystemBackground ) || palette().brush( QPalette::Base ) != palette().brush( QPalette::Window ) ) { const QRectF br = boundingRect(); painter->save(); painter->setPen( Qt::NoPen ); painter->setBrush( palette().brush( QPalette::Base ) ); painter->drawEllipse( br ); painter->restore(); } const QRectF insideScaleRect = scaleInnerRect(); if ( palette().brush( QPalette::WindowText ) != palette().brush( QPalette::Base ) ) { painter->save(); painter->setPen( Qt::NoPen ); painter->setBrush( palette().brush( QPalette::WindowText ) ); painter->drawEllipse( insideScaleRect ); painter->restore(); } const QPointF center = insideScaleRect.center(); const double radius = 0.5 * insideScaleRect.width(); double direction = d_data->origin; if ( isValid() ) { direction = 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 ( d_data->direction == QwtDial::CounterClockwise ) direction = d_data->maxScaleArc - ( direction - d_data->minScaleArc ); direction += d_data->origin; if ( direction >= 360.0 ) direction -= 360.0; else if ( direction < 0.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(); painter->save(); drawScaleContents( painter, center, radius ); 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(); } }
static void initNeedle(unsigned w, unsigned h, unsigned max) { CGColorSpaceRef colorspace; CGContextRef gc; unsigned char *data; float cx, cy; float angle, radius, needle; data = (unsigned char *)malloc(w * h * 4); colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); gc = CGBitmapContextCreate(data, w, h, 8, w * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); cx = CENTERX * w; cy = CENTERY * h; radius = 0.5 * (w > h ? w : h); needle = radius * 0.85; CGContextTranslateCTM(gc, 0.0, h); CGContextScaleCTM(gc, 1.0, -1.0); CGContextClearRect(gc, CGRectMake(0, 0, w, h)); angle = 0;//angleForValue(0, max); { // draw glow reflecting on inner bevel float dx, dy; dx = -cos(angle) + 1; dy = -sin(angle) + 1; CGGradientRef gradient; size_t num_locations = 2; CGFloat locations[2] = { 0.0, 1.0 }; CGFloat components[8] = { 0.7, 0.7, 1.0, 0.7, // Start color 0.0, 0.0, 0.0, 0.0 }; // End color gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations); CGContextSaveGState(gc); CGContextAddArc(gc, cx, cy, needle*1.05, 0, 2*M_PI, false); CGContextAddArc(gc, cx, cy, needle*0.96, 0, 2*M_PI, false); CGContextEOClip(gc); CGContextDrawRadialGradient (gc, gradient, CGPointMake(cx*dx, cy*dy), radius*0.1, CGPointMake(cx*1.0, cy*1.0), radius*1.0, 0); CGContextRestoreGState(gc); } CGContextSetRGBFillColor(gc, 0.9, 0.9, 1.0, 1.0); // draw several glow passes, with the content offscreen CGContextTranslateCTM(gc, 0, OFFSCREEN - 10); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawNeedle(gc, w, h, angle); CGContextTranslateCTM(gc, 0, 20); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawNeedle(gc, w, h, angle); CGContextTranslateCTM(gc, -10, -10); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawNeedle(gc, w, h, angle); CGContextTranslateCTM(gc, 20, 0); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawNeedle(gc, w, h, angle); CGContextTranslateCTM(gc, -10, -OFFSCREEN); // draw real content CGContextSetShadowWithColor(gc, CGSizeMake(0, 1), 6.0, CGColorCreateGenericRGB(0.0, 0.0, 0.5, 0.7)); drawNeedle(gc, w, h, angle); glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data); CGContextRelease(gc); CGColorSpaceRelease(colorspace); free(data); }
/*! \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 QwtDial::boundingRect, QwtDial::contentsRect, QwtDial::scaleContentsRect, QWidget::setPalette */ void QwtDial::drawContents(QPainter *painter) const { if ( backgroundMode() == NoBackground || colorGroup().brush(QColorGroup::Base) != colorGroup().brush(QColorGroup::Background) ) { // Don´t use QPainter::drawEllipse. There are some pixels // different compared to the region in the mask, leaving // them in background color. painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(colorGroup().brush(QColorGroup::Base)); // Even if we want to fill the contentsRect only, we fill the // complete boundingRect. The frame will be painted later // above, but we want to have the base color below it // because round objects doesn´t cover all pixels. QRect br = boundingRect(); #if QT_VERSION < 300 #ifdef _WS_WIN32_ // Qt-230-NC draws ellipses not as nicely as Qt-2.3.x on X Windows br.setTop(br.top()-1); br.setLeft(br.left()-1); br.setBottom(br.bottom()+1); br.setRight(br.right()+1); #endif #endif painter->setClipRegion(QRegion(painter->xForm(br), QRegion::Ellipse)); painter->drawRect(br); painter->restore(); } const QRect insideScaleRect = scaleContentsRect(); if ( colorGroup().brush(QColorGroup::Foreground) != colorGroup().brush(QColorGroup::Base) ) { painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(colorGroup().brush(QColorGroup::Foreground)); painter->setClipRegion( QRegion(painter->xForm(insideScaleRect), QRegion::Ellipse)); painter->drawRect(insideScaleRect); painter->restore(); } const QPoint center = insideScaleRect.center(); const int radius = insideScaleRect.width() / 2; painter->save(); drawScaleContents(painter, center, radius); painter->restore(); double direction = d_origin; if (isValid()) { direction = d_origin + d_minScaleArc; if ( maxValue() > minValue() && d_maxScaleArc > d_minScaleArc ) { const double ratio = (value() - minValue()) / (maxValue() - minValue()); direction += ratio * (d_maxScaleArc - d_minScaleArc); } if ( direction >= 360.0 ) direction -= 360.0; } double origin = d_origin; if ( mode() == RotateScale ) { origin -= direction - d_origin; direction = d_origin; } painter->save(); drawScale(painter, center, radius, origin, d_minScaleArc, d_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(); } }
void QNeedleIndicator::paintEvent (QPaintEvent *event) { drawBackground(); drawNeedle(); QWidget::paintEvent(event); }