void ScaleSliderQWidget::paintEvent(QPaintEvent* /*e*/) { if (_imp->mustInitializeSliderPosition) { centerOn(_imp->minimum, _imp->maximum); _imp->mustInitializeSliderPosition = false; seekScalePosition(_imp->value); _imp->initialized = true; } ///fill the background with the appropriate style color QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); double txtR,txtG,txtB; appPTR->getCurrentSettings()->getTextColor(&txtR, &txtG, &txtB); QColor textColor; textColor.setRgbF(Natron::clamp(txtR), Natron::clamp(txtG), Natron::clamp(txtB)); QColor scaleColor; scaleColor.setRgbF(textColor.redF() / 2., textColor.greenF() / 2., textColor.blueF() / 2.); QFontMetrics fontM(*_imp->font); p.setPen(scaleColor); QPointF btmLeft = _imp->zoomCtx.toZoomCoordinates(0,height() - 1); QPointF topRight = _imp->zoomCtx.toZoomCoordinates(width() - 1, 0); if ( btmLeft.x() == topRight.x() ) { return; } /*drawing X axis*/ double lineYpos = height() - 1 - fontM.height() - TICK_HEIGHT / 2; p.drawLine(0, lineYpos, width() - 1, lineYpos); double tickBottom = _imp->zoomCtx.toZoomCoordinates( 0,height() - 1 - fontM.height() ).y(); double tickTop = _imp->zoomCtx.toZoomCoordinates(0,height() - 1 - fontM.height() - TICK_HEIGHT).y(); const double smallestTickSizePixel = 5.; // tick size (in pixels) for alpha = 0. const double largestTickSizePixel = 1000.; // tick size (in pixels) for alpha = 1. std::vector<double> acceptedDistances; acceptedDistances.push_back(1.); acceptedDistances.push_back(5.); acceptedDistances.push_back(10.); acceptedDistances.push_back(50.); const double rangePixel = width(); const double range_min = btmLeft.x(); const double range_max = topRight.x(); const double range = range_max - range_min; double smallTickSize; bool half_tick; ticks_size(range_min, range_max, rangePixel, smallestTickSizePixel, &smallTickSize, &half_tick); int m1, m2; const int ticks_max = 1000; double offset; ticks_bounds(range_min, range_max, smallTickSize, half_tick, ticks_max, &offset, &m1, &m2); std::vector<int> ticks; ticks_fill(half_tick, ticks_max, m1, m2, &ticks); const double smallestTickSize = range * smallestTickSizePixel / rangePixel; const double largestTickSize = range * largestTickSizePixel / rangePixel; const double minTickSizeTextPixel = fontM.width( QString("00") ); // AXIS-SPECIFIC const double minTickSizeText = range * minTickSizeTextPixel / rangePixel; for (int i = m1; i <= m2; ++i) { double value = i * smallTickSize + offset; const double tickSize = ticks[i - m1] * smallTickSize; const double alpha = ticks_alpha(smallestTickSize, largestTickSize, tickSize); QColor color(textColor); color.setAlphaF(alpha); QPen pen(color); pen.setWidthF(1.9); p.setPen(pen); QPointF tickBottomPos = _imp->zoomCtx.toWidgetCoordinates(value, tickBottom); QPointF tickTopPos = _imp->zoomCtx.toWidgetCoordinates(value, tickTop); p.drawLine(tickBottomPos,tickTopPos); bool renderText = _imp->dataType == eDataTypeDouble || std::abs(std::floor(0.5 + value) - value) == 0.; if (renderText && tickSize > minTickSizeText) { const int tickSizePixel = rangePixel * tickSize / range; const QString s = QString::number(value); const int sSizePixel = fontM.width(s); if (tickSizePixel > sSizePixel) { const int sSizeFullPixel = sSizePixel + minTickSizeTextPixel; double alphaText = 1.0; //alpha; if (tickSizePixel < sSizeFullPixel) { // when the text size is between sSizePixel and sSizeFullPixel, // draw it with a lower alpha alphaText *= (tickSizePixel - sSizePixel) / (double)minTickSizeTextPixel; } QColor c = _imp->readOnly || !isEnabled() ? Qt::black : textColor; c.setAlphaF(alphaText); p.setFont(*_imp->font); p.setPen(c); QPointF textPos = _imp->zoomCtx.toWidgetCoordinates( value, btmLeft.y() ); p.drawText(textPos, s); } } } double positionValue = _imp->zoomCtx.toWidgetCoordinates(_imp->value,0).x(); QPointF sliderBottomLeft(positionValue - SLIDER_WIDTH / 2,height() - 1 - fontM.height() / 2); QPointF sliderTopRight(positionValue + SLIDER_WIDTH / 2,height() - 1 - fontM.height() / 2 - SLIDER_HEIGHT); /*draw the slider*/ p.setPen(_imp->sliderColor); p.fillRect(sliderBottomLeft.x(), sliderBottomLeft.y(), sliderTopRight.x() - sliderBottomLeft.x(), sliderTopRight.y() - sliderBottomLeft.y(),_imp->sliderColor); /*draw a black rect around the slider for contrast*/ p.setPen(Qt::black); p.drawLine( sliderBottomLeft.x(),sliderBottomLeft.y(),sliderBottomLeft.x(),sliderTopRight.y() ); p.drawLine( sliderBottomLeft.x(),sliderTopRight.y(),sliderTopRight.x(),sliderTopRight.y() ); p.drawLine( sliderTopRight.x(),sliderTopRight.y(),sliderTopRight.x(),sliderBottomLeft.y() ); p.drawLine( sliderTopRight.x(),sliderBottomLeft.y(),sliderBottomLeft.x(),sliderBottomLeft.y() ); } // paintEvent
void ScaleSliderQWidget::paintEvent(QPaintEvent* /*e*/) { if (_imp->mustInitializeSliderPosition) { if (_imp->minimum < _imp->maximum) { centerOn(_imp->minimum, _imp->maximum); } _imp->mustInitializeSliderPosition = false; seekScalePosition(_imp->value); _imp->initialized = true; } ///fill the background with the appropriate style color QStyleOption opt; opt.init(this); QPainter p(this); p.setOpacity(1); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); double txtR, txtG, txtB; if (_imp->altered) { appPTR->getCurrentSettings()->getAltTextColor(&txtR, &txtG, &txtB); } else { appPTR->getCurrentSettings()->getTextColor(&txtR, &txtG, &txtB); } QColor textColor; textColor.setRgbF( Image::clamp<qreal>(txtR, 0., 1.), Image::clamp<qreal>(txtG, 0., 1.), Image::clamp<qreal>(txtB, 0., 1.) ); QColor scaleColor; scaleColor.setRgbF(textColor.redF() / 2., textColor.greenF() / 2., textColor.blueF() / 2.); QFontMetrics fontM(_imp->font, 0); if (!_imp->useLineColor) { p.setPen(scaleColor); } else { p.setPen(_imp->lineColor); } QPointF btmLeft = _imp->zoomCtx.toZoomCoordinates(0, height() - 1); QPointF topRight = _imp->zoomCtx.toZoomCoordinates(width() - 1, 0); if ( btmLeft.x() == topRight.x() ) { return; } /*drawing X axis*/ double lineYpos = height() - 1 - fontM.height() - TO_DPIY(TICK_HEIGHT) / 2; p.drawLine(0, lineYpos, width() - 1, lineYpos); double tickBottom = _imp->zoomCtx.toZoomCoordinates( 0, height() - 1 - fontM.height() ).y(); double tickTop = _imp->zoomCtx.toZoomCoordinates( 0, height() - 1 - fontM.height() - TO_DPIY(TICK_HEIGHT) ).y(); const double smallestTickSizePixel = 10.; // tick size (in pixels) for alpha = 0. const double largestTickSizePixel = 1000.; // tick size (in pixels) for alpha = 1. const double rangePixel = width(); const double range_min = btmLeft.x(); const double range_max = topRight.x(); const double range = range_max - range_min; double smallTickSize; bool half_tick; ticks_size(range_min, range_max, rangePixel, smallestTickSizePixel, &smallTickSize, &half_tick); if ( (_imp->dataType == eDataTypeInt) && (smallTickSize < 1.) ) { smallTickSize = 1.; half_tick = false; } int m1, m2; const int ticks_max = 1000; double offset; ticks_bounds(range_min, range_max, smallTickSize, half_tick, ticks_max, &offset, &m1, &m2); std::vector<int> ticks; ticks_fill(half_tick, ticks_max, m1, m2, &ticks); const double smallestTickSize = range * smallestTickSizePixel / rangePixel; const double largestTickSize = range * largestTickSizePixel / rangePixel; const double minTickSizeTextPixel = fontM.width( QLatin1String("00") ); // AXIS-SPECIFIC const double minTickSizeText = range * minTickSizeTextPixel / rangePixel; for (int i = m1; i <= m2; ++i) { double value = i * smallTickSize + offset; const double tickSize = ticks[i - m1] * smallTickSize; const double alpha = ticks_alpha(smallestTickSize, largestTickSize, tickSize); QColor color(textColor); color.setAlphaF(alpha); QPen pen(color); pen.setWidthF(1.9); p.setPen(pen); // for Int slider, because smallTickSize is at least 1, isFloating can never be true bool isFloating = std::abs(std::floor(0.5 + value) - value) != 0.; assert( !(_imp->dataType == eDataTypeInt && isFloating) ); bool renderFloating = _imp->dataType == eDataTypeDouble || !isFloating; if (renderFloating) { QPointF tickBottomPos = _imp->zoomCtx.toWidgetCoordinates(value, tickBottom); QPointF tickTopPos = _imp->zoomCtx.toWidgetCoordinates(value, tickTop); p.drawLine(tickBottomPos, tickTopPos); } if ( renderFloating && (tickSize > minTickSizeText) ) { const int tickSizePixel = rangePixel * tickSize / range; const QString s = QString::number(value); const int sSizePixel = fontM.width(s); if (tickSizePixel > sSizePixel) { const int sSizeFullPixel = sSizePixel + minTickSizeTextPixel; double alphaText = 1.0; //alpha; if (tickSizePixel < sSizeFullPixel) { // when the text size is between sSizePixel and sSizeFullPixel, // draw it with a lower alpha alphaText *= (tickSizePixel - sSizePixel) / (double)minTickSizeTextPixel; } QColor c = _imp->readOnly || !isEnabled() ? Qt::black : textColor; c.setAlphaF(alphaText); p.setFont(_imp->font); p.setPen(c); QPointF textPos = _imp->zoomCtx.toWidgetCoordinates( value, btmLeft.y() ); p.drawText(textPos, s); } } } double positionValue = _imp->zoomCtx.toWidgetCoordinates(_imp->value, 0).x(); QPointF sliderBottomLeft(positionValue - TO_DPIX(SLIDER_WIDTH) / 2, height() - 1 - fontM.height() / 2); QPointF sliderTopRight( positionValue + TO_DPIX(SLIDER_WIDTH) / 2, height() - 1 - fontM.height() / 2 - TO_DPIY(SLIDER_HEIGHT) ); /*draw the slider*/ p.setPen(_imp->sliderColor); p.fillRect(sliderBottomLeft.x(), sliderBottomLeft.y(), sliderTopRight.x() - sliderBottomLeft.x(), sliderTopRight.y() - sliderBottomLeft.y(), _imp->sliderColor); /*draw a black rect around the slider for contrast or orange when focused*/ if ( !hasFocus() ) { p.setPen(Qt::black); } else { QPen pen = p.pen(); pen.setColor( QColor(243, 137, 0) ); QVector<qreal> dashStyle; qreal space = 2; dashStyle << 1 << space; pen.setDashPattern(dashStyle); p.setOpacity(0.8); p.setPen(pen); } p.drawLine( sliderBottomLeft.x(), sliderBottomLeft.y(), sliderBottomLeft.x(), sliderTopRight.y() ); p.drawLine( sliderBottomLeft.x(), sliderTopRight.y(), sliderTopRight.x(), sliderTopRight.y() ); p.drawLine( sliderTopRight.x(), sliderTopRight.y(), sliderTopRight.x(), sliderBottomLeft.y() ); p.drawLine( sliderTopRight.x(), sliderBottomLeft.y(), sliderBottomLeft.x(), sliderBottomLeft.y() ); } // paintEvent