📄 qwt_slider.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 <math.h>#include <qstyle.h>#include <qdrawutil.h>#include <qpainter.h>#include "qwt_paint_buffer.h"#include "qwt_slider.h"/*! \brief Constructor \param parent parent widget \param name The Widget's name. Default = 0. \param orient Orientation of the slider. Can be Qt::Horizontal or Qt::Vertical. Defaults to Horizontal. \param scalePos Position of the scale. Can be QwtSlider::None, QwtSlider::Left, QwtSlider::Right, QwtSlider::Top, or QwtSlider::Bottom. Defaults to QwtSlider::None. \param bgStyle Background style. QwtSlider::BgTrough draws the slider button in a trough, QwtSlider::BgSlot draws a slot underneath the button. An or-combination of both may also be used. The default is QwtSlider::BgTrough.*/QwtSlider::QwtSlider(QWidget *parent, const char *name, Qt::Orientation orient, ScalePos scalePos, int bgStyle): QwtSliderBase(orient, parent, name, WRepaintNoErase|WResizeNoErase){ d_borderWidth = 2; d_scaleDist = 4; d_scalePos = scalePos; d_xMargin = 0; d_yMargin = 0; d_bgStyle = bgStyle; if (bgStyle == BgSlot) { d_thumbLength = 16; d_thumbWidth = 30; } else { d_thumbLength = 31; d_thumbWidth = 16; } d_sliderRect.setRect(0,0,8,8); QwtScaleDraw::Orientation so; if ( orientation() == Qt::Vertical ) { if (d_scalePos == Right) so = QwtScaleDraw::Right; else so = QwtScaleDraw::Left; } else { if (d_scalePos == Bottom) so = QwtScaleDraw::Bottom; else so = QwtScaleDraw::Top; } scaleDraw()->setGeometry(0,0,100,so);}/*! \brief Set the orientation. \param o Orientation. Allowed values are Qt::Horizontal and Qt::Vertical. Defaults to Qt::Horizontal. \sa QwtSliderBase::orientation()*/void QwtSlider::setOrientation(Qt::Orientation o) { QwtSliderBase::setOrientation(o); layoutSlider();}/*! \brief Change the slider's border width \param bd border width*/void QwtSlider::setBorderWidth(int bd){ if ( bd < 0 ) bd = 0; if ( bd != d_borderWidth ) { d_borderWidth = bd; layoutSlider(); }}/*! \brief Set the slider's thumb length \param thumbLength new length*/void QwtSlider::setThumbLength(int thumbLength){ if ( thumbLength < 8 ) thumbLength = 8; if ( thumbLength != d_thumbLength ) { d_thumbLength = thumbLength; layoutSlider(); }}/*! \brief Change the width of the thumb \param w new width*/void QwtSlider::setThumbWidth(int w){ if ( w < 4 ) w = 4; if ( d_thumbWidth != w ) { d_thumbWidth = w; layoutSlider(); }}//! Notify changed scalevoid QwtSlider::scaleChange(){ if (!hasUserScale()) { scaleDraw()->setScale(minValue(), maxValue(), scaleMaxMajor(), scaleMaxMinor(), scaleDraw()->scaleDiv().logScale()); } layoutSlider();}//! Notify change in fontvoid QwtSlider::fontChange(const QFont &f){ QwtSliderBase::fontChange( f ); layoutSlider();}//! Draw the slider into the specified rectangle.void QwtSlider::drawSlider(QPainter *p, const QRect &r){ QRect cr(r); if (d_bgStyle & BgTrough) { qDrawShadePanel(p, r.x(), r.y(), r.width(), r.height(), colorGroup(), TRUE, d_borderWidth,0); cr.setRect(r.x() + d_borderWidth, r.y() + d_borderWidth, r.width() - 2 * d_borderWidth, r.height() - 2 * d_borderWidth); p->fillRect(cr.x(), cr.y(), cr.width(), cr.height(), colorGroup().brush(QColorGroup::Mid)); } if ( d_bgStyle & BgSlot) { int ws = 4; int ds = d_thumbLength / 2 - 4; if ( ds < 1 ) ds = 1; QRect rSlot; if (orientation() == Qt::Horizontal) { if ( cr.height() & 1 ) ws++; rSlot = QRect(cr.x() + ds, cr.y() + (cr.height() - ws) / 2, cr.width() - 2 * ds, ws); } else { if ( cr.width() & 1 ) ws++; rSlot = QRect(cr.x() + (cr.width() - ws) / 2, cr.y() + ds, ws, cr.height() - 2 * ds); } p->fillRect(rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(), colorGroup().brush(QColorGroup::Dark)); qDrawShadePanel(p, rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(), colorGroup(), TRUE, 1 ,0); } if ( isValid() ) drawThumb(p, cr, xyPosition(value()));}//! Draw the thumb at a positionvoid QwtSlider::drawThumb(QPainter *p, const QRect &sliderRect, int pos){ pos++; // shade line points one pixel below if (orientation() == Qt::Horizontal) { qDrawShadePanel(p, pos - d_thumbLength / 2, sliderRect.y(), d_thumbLength, sliderRect.height(), colorGroup(), FALSE, d_borderWidth, &colorGroup().brush(QColorGroup::Button)); qDrawShadeLine(p, pos, sliderRect.y(), pos, sliderRect.y() + sliderRect.height() - 2, colorGroup(), TRUE, 1); } else // Vertical { qDrawShadePanel(p,sliderRect.x(), pos - d_thumbLength / 2, sliderRect.width(), d_thumbLength, colorGroup(),FALSE, d_borderWidth, &colorGroup().brush(QColorGroup::Button)); qDrawShadeLine(p, sliderRect.x(), pos, sliderRect.x() + sliderRect.width() - 2, pos, colorGroup(), TRUE, 1); }}//! Find the x/y position for a given value vint QwtSlider::xyPosition(double v) const{ int pos; if ( minValue() == scaleDraw()->d1() && maxValue() == scaleDraw()->d2() ) { // We prefer to use the transformation of scaleDraw // So ticks and marker are always in sync and we don't have // to take care of rounding problems. pos = scaleDraw()->transform(v); } else { // range and scaleDraw differ ? Sounds strange but // might happen with logarithmic scales const double f = (v - minValue()) / (maxValue() - minValue()); double dPos; if ( orientation() == Qt::Horizontal ) dPos = scaleDraw()->i1() + f * (scaleDraw()->i2() - scaleDraw()->i1()); else dPos = scaleDraw()->i1() - f * (scaleDraw()->i1() - scaleDraw()->i2()); pos = qRound(dPos); } return pos;}//! Determine the value corresponding to a specified mouse location.double QwtSlider::getValue(const QPoint &p){ double rv; if ( minValue() == scaleDraw()->d1() && maxValue() == scaleDraw()->d2() ) { rv = scaleDraw()->invTransform( orientation() == Qt::Horizontal ? p.x() : p.y()); } else { double pos; double range; if ( orientation() == Qt::Horizontal ) { pos = p.x() - scaleDraw()->i1(); range = scaleDraw()->i2() - scaleDraw()->i1(); } else { pos = scaleDraw()->i1() - p.y(); range = scaleDraw()->i1() - scaleDraw()->i2(); } rv = minValue() + pos / range * (maxValue() - minValue()); } return(rv);}/*! \brief Determine scrolling mode and direction \param p point \param scrollMode Scrolling mode \param direction Direction*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -