// The knob will be dragged to the moved position. void QSlideSwitch::mouseMoveEvent(QMouseEvent *event) { if (!m_dragInProgress) return; m_dragDistanceX = event->x() - m_dragStartPosition.x(); qreal dx = m_rect.width() - knobRect().width(); if (isChecked()) { m_position = 100 * (dx + m_dragDistanceX) / dx; } else { m_position = 100 * m_dragDistanceX / dx; } if (m_position > 100) { m_position = 100; setChecked(true); } if (m_position < 0) { m_position = 0; setChecked(false); } update(); }
void BRadioButton::Draw(BRect updateRect) { // its size depends on the text height font_height fontHeight; GetFontHeight(&fontHeight); rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR); uint32 flags = be_control_look->Flags(this); if (fOutlined) flags |= BControlLook::B_CLICKED; BRect knobRect(_KnobFrame(fontHeight)); BRect rect(knobRect); be_control_look->DrawRadioButton(this, rect, updateRect, base, flags); BRect labelRect(Bounds()); labelRect.left = knobRect.right + 1 + be_control_look->DefaultLabelSpacing(); const BBitmap* icon = IconBitmap( B_INACTIVE_ICON_BITMAP | (IsEnabled() ? 0 : B_DISABLED_ICON_BITMAP)); be_control_look->DrawLabel(this, Label(), icon, labelRect, updateRect, base, flags); }
void QSlideSwitch::mousePressEvent(QMouseEvent *event) { if (event->button() != Qt::LeftButton) { return; } if (knobRect().contains(event->pos())) { m_dragInProgress = true; m_dragStartPosition = event->pos(); } else { toggle(); } }
/*! \brief Determine what to do when the user presses a mouse button. \param pos Mouse position \retval True, when pos is inside the circle of the knob. \sa scrolledTo() */ bool QwtKnob::isScrollPosition( const QPoint &pos ) const { const QRect kr = knobRect(); const QRegion region( kr, QRegion::Ellipse ); if ( region.contains( pos ) && ( pos != kr.center() ) ) { const double angle = QLineF( kr.center(), pos ).angle(); const double valueAngle = qwtToDegrees( scaleMap().transform( value() ) ); d_data->mouseOffset = qwtNormalizeDegrees( angle - valueAngle ); return true; } return false; }
static void qwtDrawShadedPointer( QPainter *painter, const QColor &lightColor, const QColor &darkColor, double length, double width ) { const double peak = qMax( length / 10.0, 5.0 ); const double knobWidth = width + 8; QRectF knobRect( 0, 0, knobWidth, knobWidth ); knobRect.moveCenter( QPointF(0, 0) ); QPainterPath path1; path1.lineTo( 0.0, 0.5 * width ); path1.lineTo( length - peak, 0.5 * width ); path1.lineTo( length, 0.0 ); path1.lineTo( 0.0, 0.0 ); QPainterPath arcPath1; arcPath1.arcTo( knobRect, 0.0, -90.0 ); path1 = path1.united( arcPath1 ); QPainterPath path2; path2.lineTo( 0.0, -0.5 * width ); path2.lineTo( length - peak, -0.5 * width ); path2.lineTo( length, 0.0 ); path2.lineTo( 0.0, 0.0 ); QPainterPath arcPath2; arcPath2.arcTo( knobRect, 0.0, 90.0 ); path2 = path2.united( arcPath2 ); painter->setPen( Qt::NoPen ); painter->setBrush( lightColor ); painter->drawPath( path1 ); painter->setBrush( darkColor ); painter->drawPath( path2 ); }
/*! Draw a compass needle \param painter Painter \param brush Brush \param colorOffset Color offset \param center Center, where the needle starts \param length Length of the needle \param width Width of the needle \param direction Direction */ void QwtCompassMagnetNeedle::drawPointer( QPainter *painter, const QBrush &brush, int colorOffset, const QPoint ¢er, int length, int width, double direction ) { painter->save(); const int peak = qMax( qRound( length / 10.0 ), 5 ); const int knobWidth = width + 8; QRect knobRect( 0, 0, knobWidth, knobWidth ); knobRect.moveCenter( center ); QPolygon pa( 5 ); pa.setPoint( 0, qwtDegree2Pos( center, width / 2, direction + 90.0 ) ); pa.setPoint( 1, center ); pa.setPoint( 2, qwtDegree2Pos( pa.point( 1 ), length - peak, direction ) ); pa.setPoint( 3, qwtDegree2Pos( center, length, direction ) ); pa.setPoint( 4, qwtDegree2Pos( pa.point( 0 ), length - peak, direction ) ); painter->setPen( Qt::NoPen ); QBrush darkBrush = brush; darkBrush.setColor( darkBrush.color().dark( 100 + colorOffset ) ); painter->setBrush( darkBrush ); painter->drawPolygon( pa ); painter->drawPie( knobRect, qRound( direction * 16 ), 90 * 16 ); pa.setPoint( 0, qwtDegree2Pos( center, width / 2, direction - 90.0 ) ); pa.setPoint( 4, qwtDegree2Pos( pa.point( 0 ), length - peak, direction ) ); QBrush lightBrush = brush; lightBrush.setColor( lightBrush.color().dark( 100 - colorOffset ) ); painter->setBrush( lightBrush ); painter->drawPolygon( pa ); painter->drawPie( knobRect, qRound( direction * 16 ), -90 * 16 ); painter->restore(); }
void Slider::drawSlider(QPainter *p, const QRect &r) { const QPalette& pal = palette(); QBrush brBack(pal.window()); QBrush brMid; QBrush brDark(pal.dark()); QRect cr; int ipos, dist1; double rpos; int lineDist; if (d_bwTrough > 0) { // qDrawShadePanel(p, r.x(), r.y(), //r.width(), r.height(), //pal, TRUE, d_bwTrough,0); cr.setRect(r.x() + d_bwTrough, r.y() + d_bwTrough, r.width() - 2 * d_bwTrough, r.height() - 2 * d_bwTrough); brMid = pal.mid(); } else { cr = r; brMid = brBack; } rpos = (value() - minValue()) / (maxValue() - minValue()); lineDist = d_borderWidth - 1; if (lineDist < 1) lineDist = 1; if (d_orient == Qt::Horizontal) { dist1 = int(double(cr.width() - d_thumbLength) * rpos); ipos = cr.x() + dist1; markerPos = ipos + d_thumbHalf; // // draw background // if (d_bgStyle & BgSlot) { drawHsBgSlot(p, cr, QRect(ipos, cr.y(), d_thumbLength, cr.height()), brMid); } else { //p->fillRect(cr.x(), cr.y(), dist1, cr.height(), brMid); //p->fillRect(ipos + d_thumbLength, cr.y(), // cr.width() - d_thumbLength - dist1, cr.height(), brMid); } // // Draw thumb // //qDrawShadePanel(p,ipos, cr.y(), d_thumbLength, cr.height(), // pal, FALSE, d_borderWidth, &brBack); QPixmap thumbp; bool loaded = thumbp.load(":images/slider_thumb_new_h.png"); if (loaded) { p->setCompositionMode(QPainter::CompositionMode_SourceAtop); //QPainter::CompositionMode_SourceOver); p->setClipping(false); p->drawPixmap(ipos, cr.y()-2, thumbp); } /*if (lineDist > 1) qDrawShadeLine(p, markerPos, cr.y() + lineDist, markerPos, cr.y() + cr.height() - lineDist, pal, TRUE, 1); else { p->setPen(pal.dark().color()); p->drawLine(markerPos - 1, cr.y() + lineDist, markerPos - 1, cr.y() + cr.height() - lineDist - 1); p->setPen(pal.light().color()); p->drawLine(markerPos, cr.y() + lineDist, markerPos, cr.y() + cr.height() - lineDist - 1); }*/ } else {//Vertical slider dist1 = int(double(cr.height() - d_thumbLength) * (1.0 - rpos)); ipos = cr.y() + dist1; markerPos = ipos + d_thumbHalf; //NOTE: this is adding the middle line in the slider if (d_bgStyle & BgSlot) { drawVsBgSlot(p, cr, QRect(cr.left(), ipos, cr.width(), d_thumbLength), brMid); } else { //p->fillRect(cr.x(),cr.y(),cr.width(),ipos,brMid); //p->fillRect(cr.x(), ipos + d_thumbLength, cr.width(), //cr.height() - d_thumbLength - dist1, brMid); } //This adds the thumb slider //qDrawShadePanel(p,cr.x(),ipos , cr.width(), d_thumbLength, // pal,FALSE,d_borderWidth, &brBack); QPixmap thumbp; bool loaded = thumbp.load(":images/slider_thumb_new.png"); int knobx = cr.x() + 2; int knoby = ipos - 12; QRect knobRect(knobx, knoby, 18, 33); //printf("Slider: Knob position X: %d Y: %d\n", knobx, knoby); if (loaded) { p->setCompositionMode(QPainter::CompositionMode_SourceAtop); //QPainter::CompositionMode_SourceOver); //p->drawPixmap(knobx, knoby, thumbp); p->setClipping(false); p->drawPixmap(knobRect, thumbp); } // if (lineDist > 1) // qDrawShadeLine(p, cr.x() + lineDist , markerPos, // cr.x() + cr.width() - lineDist, markerPos, // pal, TRUE, 1); // else // { // // p->setPen(pal.dark().color()); // p->drawLine(cr.x() + lineDist, markerPos - 1 , // cr.x() + cr.width() - lineDist - 1, markerPos - 1); // p->setPen(pal.light().color()); // p->drawLine(cr.x() + lineDist, markerPos, // cr.x() + cr.width() - lineDist - 1 , markerPos); // } } }
void BRadioButton::Draw(BRect updateRect) { // its size depends on the text height font_height fontHeight; GetFontHeight(&fontHeight); if (be_control_look != NULL) { rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR); uint32 flags = be_control_look->Flags(this); if (fOutlined) flags |= BControlLook::B_CLICKED; BRect knobRect(_KnobFrame(fontHeight)); BRect rect(knobRect); be_control_look->DrawRadioButton(this, rect, updateRect, base, flags); BRect labelRect(Bounds()); labelRect.left = knobRect.right + be_control_look->DefaultLabelSpacing(); be_control_look->DrawLabel(this, Label(), labelRect, updateRect, base, flags); return; } float textHeight = ceilf(fontHeight.ascent + fontHeight.descent); // layout the rect for the dot BRect rect = _KnobFrame(fontHeight); BPoint labelPos(rect.right + floorf(textHeight / 2.0), floorf((rect.top + rect.bottom + textHeight) / 2.0 - fontHeight.descent + 0.5) + 1.0); // if the focus is changing, just redraw the focus indicator if (IsFocusChanging()) { if (IsFocus()) SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR)); else SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR)); BPoint underLine = labelPos; underLine.y += fontHeight.descent; StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0)); return; } // colors rgb_color bg = ui_color(B_PANEL_BACKGROUND_COLOR); rgb_color lightenmax; rgb_color lighten1; rgb_color darken1; rgb_color darken2; rgb_color darken3; rgb_color darkenmax; rgb_color naviColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR); rgb_color knob; rgb_color knobDark; rgb_color knobLight; if (IsEnabled()) { lightenmax = tint_color(bg, B_LIGHTEN_MAX_TINT); lighten1 = tint_color(bg, B_LIGHTEN_1_TINT); darken1 = tint_color(bg, B_DARKEN_1_TINT); darken2 = tint_color(bg, B_DARKEN_2_TINT); darken3 = tint_color(bg, B_DARKEN_3_TINT); darkenmax = tint_color(bg, B_DARKEN_MAX_TINT); knob = naviColor; knobDark = tint_color(naviColor, B_DARKEN_3_TINT); knobLight = tint_color(naviColor, 0.15); } else { lightenmax = tint_color(bg, B_LIGHTEN_2_TINT); lighten1 = bg; darken1 = bg; darken2 = tint_color(bg, B_DARKEN_1_TINT); darken3 = tint_color(bg, B_DARKEN_2_TINT); darkenmax = tint_color(bg, B_DISABLED_LABEL_TINT); knob = tint_color(naviColor, B_LIGHTEN_2_TINT); knobDark = tint_color(naviColor, B_LIGHTEN_1_TINT); knobLight = tint_color(naviColor, (B_LIGHTEN_2_TINT + B_LIGHTEN_MAX_TINT) / 2.0); } // dot if (Value() == B_CONTROL_ON) { // full SetHighColor(knobDark); FillEllipse(rect); SetHighColor(knob); FillEllipse(BRect(rect.left + 2, rect.top + 2, rect.right - 3, rect.bottom - 3)); SetHighColor(knobLight); FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 5, rect.bottom - 5)); } else { // empty SetHighColor(lightenmax); FillEllipse(rect); } rect.InsetBy(-1.0, -1.0); // outer circle if (fOutlined) { // indicating "about to change value" SetHighColor(darken3); StrokeEllipse(rect); } else { SetHighColor(darken1); StrokeArc(rect, 45.0, 180.0); SetHighColor(lightenmax); StrokeArc(rect, 45.0, -180.0); } rect.InsetBy(1, 1); // inner circle SetHighColor(darken3); StrokeArc(rect, 45.0, 180.0); SetHighColor(bg); StrokeArc(rect, 45.0, -180.0); // for faster font rendering, we can restore B_OP_COPY SetDrawingMode(B_OP_COPY); // label SetHighColor(darkenmax); DrawString(Label(), labelPos); // underline label if focused if (IsFocus()) { SetHighColor(naviColor); BPoint underLine = labelPos; underLine.y += fontHeight.descent; StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0)); } }
// Overloaded paint event to draw the actual state. void QSlideSwitch::paintEvent(QPaintEvent *) { #ifdef WITH_TEXT QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::TextAntialiasing); painter.setPen(QPen(Qt::NoPen)); if (isChecked()) { // Render ON background painter.setBrush(m_onBrush); painter.drawRoundedRect(m_rect, 8, 8); // Render ON text m_text_rect = QRect(5, m_textY, m_textOnWidth, m_textHeight); painter.setPen(QPen(Qt::white)); painter.drawText(m_text_rect, Qt::AlignVCenter, m_textOn); } else { // Render OFF background painter.setBrush(m_offBrush); painter.drawRoundedRect(m_rect, 8, 8); // Render OFF text m_text_rect = QRect(m_rect.width() - m_textOffWidth - 5, m_textY, m_textOffWidth, m_textHeight); painter.setPen(QPen(QColor("#7F7F7F"))); painter.drawText(m_text_rect, Qt::AlignVCenter, m_textOff); } // Render knob painter.setBrush(m_knobBrush); if (m_dragInProgress) { painter.setPen(QPen(Qt::green, m_knobBorder)); } else { painter.setPen(QPen(m_knobBorderBrush, m_knobBorder)); } painter.drawRoundedRect(knobRect(), 18, 18); #else /* iOS 7 Style */ QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // sx and dx borders (half circles) qreal knobWidth = m_rect.height(); QRectF sxRect(0, 0, knobWidth, m_rect.height()); QRectF dxRect(m_rect.width() - knobWidth, 0, knobWidth, m_rect.height()); QPainterPath sliderPath; sliderPath.moveTo(knobWidth/2, 0); sliderPath.arcTo(sxRect, 90, 180); sliderPath.lineTo(m_rect.width() - knobWidth/2, m_rect.height()); sliderPath.arcTo(dxRect, -90, 180); sliderPath.lineTo(knobWidth/2, 0); if (isChecked()) { // Render ON m_sliderBrush.setColor(m_iOSGreen); m_sliderPen.setColor(m_iOSGreen); // m_knobBrush.setColor(Qt::white); UGUALE PER TUTTI m_knobPen.setColor(m_iOSGreen); // painter.setPen(QPen(m_iOSGreen, m_knobBorder)); // painter.setBrush(m_iOSGreen); // painter.setBrush(Qt::white); // Brush fro knob // painter.drawEllipse(knobRect()); } else { // Render OFF m_sliderBrush.setColor(Qt::white); m_sliderPen.setColor(m_iOSGrey); // m_knobBrush.setColor(Qt::white); m_knobPen.setColor(m_iOSGrey); // painter.setBrush(Qt::white); // painter.setPen(QPen(m_iOSGrey, m_knobBorder)); // painter.drawPath(sliderPath); } painter.setBrush(m_sliderBrush); painter.setPen(m_sliderPen); painter.drawPath(sliderPath); painter.setBrush(Qt::white); //painter.setBrush(m_knobBrush); painter.setPen(m_knobPen); // Higlight when dragging slider if (m_dragInProgress) { painter.setPen(QPen(QColor("#377FF8"), m_knobBorder)); painter.drawEllipse(knobRect()); } else { // Render knob with last Pen & Brush painter.drawEllipse(knobRect()); } #endif }