// Function copied from qslider.cpp and modified to make it compile void Slider::initStyleOption_Qt430( QStyleOptionSlider *option ) const { if (!option) return; option->initFrom( this ); option->subControls = QStyle::SC_None; option->activeSubControls = QStyle::SC_None; option->orientation = orientation(); option->maximum = maximum(); option->minimum = minimum(); option->tickPosition = (QSlider::TickPosition) tickPosition(); option->tickInterval = tickInterval(); option->upsideDown = (orientation() == Qt::Horizontal) ? (invertedAppearance() != (option->direction == Qt::RightToLeft)) : (!invertedAppearance()); option->direction = Qt::LeftToRight; // we use the upsideDown option instead option->sliderPosition = sliderPosition(); option->sliderValue = value(); option->singleStep = singleStep(); option->pageStep = pageStep(); if (orientation() == Qt::Horizontal) option->state |= QStyle::State_Horizontal; }
QSize sizeHint() const { if (tickPosition() == QSlider::NoTicks) return QSlider::sizeHint(); // preserve space for labels QSize extra(0,0); QFontMetrics fm(font()); int h = fm.height() + 3; int w = fm.width(QString::number(maximum())) + 3; if (orientation() == Qt::Horizontal) { extra.setHeight(tickPosition() == QSlider::TicksBothSides ? h * 2 : h); } else { extra.setWidth(tickPosition() == QSlider::TicksBothSides ? w * 2 : w); } return QSlider::sizeHint() + extra; }
void KexiSlider::updateLayout() { d->layout->setDirection(orientation() == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); if (tickPosition() == QSlider::TicksBothSides || tickPosition() == QSlider::NoTicks) { d->layout->setAlignment(d->slider, orientation() == Qt::Horizontal ? Qt::AlignVCenter :Qt::AlignHCenter); d->layout->setAlignment(d->spinBox, orientation() == Qt::Horizontal ? Qt::AlignVCenter :Qt::AlignHCenter); } else { if (orientation() == Qt::Horizontal) { d->layout->setAlignment(d->slider, tickPosition() == QSlider::TicksAbove ? Qt::AlignBottom : Qt::AlignTop); d->layout->setAlignment(d->spinBox, tickPosition() == QSlider::TicksAbove ? Qt::AlignBottom : Qt::AlignTop); } else { d->layout->setAlignment(d->slider, tickPosition() == QSlider::TicksLeft ? Qt::AlignRight : Qt::AlignLeft); d->layout->setAlignment(d->spinBox, tickPosition() == QSlider::TicksLeft ? Qt::AlignRight : Qt::AlignLeft); } } }
QSize TickSlider::sizeHint() const { QSize size = QSlider::sizeHint(); bool using_labels = tickLabelPosition() != NoTicks; QSize label = using_labels ? biggestLabel() : QSize(); bool using_ticks = tickPosition() != NoTicks; int n_potential_labels = (maximum() - minimum()) / tickInterval(); if (orientation() == Qt::Horizontal) { // Horizontal if (using_labels) { size.rheight() += 2 * label.height(); size = size.expandedTo(QSize( n_potential_labels * label.width() + (n_potential_labels - 1) * m_min_interlabel_gap, 0)); } if (using_ticks) { size.rheight() += 2 * m_tick_length; } if (using_labels && using_ticks) { size.rheight() += 2 * m_tick_label_gap; } if (using_labels || using_ticks) { size.rheight() += 2 * m_gap_to_slider; } } else { // Vertical if (using_labels) { size.rwidth() += 2 * label.width(); size = size.expandedTo(QSize( 0, n_potential_labels * label.height() + (n_potential_labels - 1) * m_min_interlabel_gap)); } if (using_ticks) { size.rwidth() += 2 * m_tick_length; } if (using_labels && using_ticks) { size.rwidth() += 2 * m_tick_label_gap; } if (using_labels || using_ticks) { size.rwidth() += 2 * m_gap_to_slider; } } return size; }
void RangeSlider::paintEvent(QPaintEvent *ev) { QPainter p(this); QStyleOptionSlider opt; initStyleOption(&opt); opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; if (tickPosition() != NoTicks) opt.subControls |= QStyle::SC_SliderTickmarks; // if (ev->) { // opt.activeSubControls = d->pressedControl; // opt.state |= QStyle::State_Sunken; // } else { // opt.activeSubControls = d->hoverControl; // } style()->drawComplexControl((QStyle::ComplexControl)slider_style, &opt, &p, this); }
int QSlider::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = QAbstractSlider::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { if (_id < 2) qt_static_metacall(this, _c, _id, _a); _id -= 2; } #ifndef QT_NO_PROPERTIES else if (_c == QMetaObject::ReadProperty) { void *_v = _a[0]; switch (_id) { case 0: *reinterpret_cast< TickPosition*>(_v) = tickPosition(); break; case 1: *reinterpret_cast< int*>(_v) = tickInterval(); break; } _id -= 2; } else if (_c == QMetaObject::WriteProperty) { void *_v = _a[0]; switch (_id) { case 0: setTickPosition(*reinterpret_cast< TickPosition*>(_v)); break; case 1: setTickInterval(*reinterpret_cast< int*>(_v)); break; } _id -= 2; } else if (_c == QMetaObject::ResetProperty) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyDesignable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyScriptable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyStored) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyEditable) { _id -= 2; } else if (_c == QMetaObject::QueryPropertyUser) { _id -= 2; } #endif // QT_NO_PROPERTIES return _id; }
virtual void paintEvent(QPaintEvent *ev) { if (tickPosition() == QSlider::NoTicks) return QSlider::paintEvent(ev); QPainter p(this); QStyleOptionSlider option; initStyleOption(&option); const int& ticks( option.tickPosition ); const int available(style()->proxy()->pixelMetric(QStyle::PM_SliderSpaceAvailable, &option, this)); int interval = option.tickInterval; if( interval < 1 ) interval = option.pageStep; if( interval < 1 ) return; const QRect& r(option.rect); const QPalette palette(option.palette); const int fudge(style()->proxy()->pixelMetric(QStyle::PM_SliderLength, &option, this) / 2); int current(option.minimum); int nextLabel = current; QFontMetrics fm(font()); int h = fm.height() + 3; int w = fm.width(QString::number(option.maximum)) + 3; if(available<w) nextLabel = -1; float i = available/(orientation() == Qt::Horizontal ? w : h); float t = option.maximum/interval; int valStep = t/ i + 1; // Since there is no subrect for tickmarks do a translation here. p.save(); p.translate(r.x(), r.y()); p.setPen(palette.color(QPalette::WindowText)); int extra = (option.tickPosition == QSlider::TicksBothSides ? 2 : 1); int tickSize(option.orientation == Qt::Horizontal ? (r.height() - h*extra)/3:(r.width() - w*extra)/3); while(current <= option.maximum) { const int position(QStyle::sliderPositionFromValue(option.minimum, option.maximum, current, available, option.upsideDown) + fudge); // calculate positions if(option.orientation == Qt::Horizontal) { if (ticks & QSlider::TicksAbove) { p.drawLine(position, h, position, tickSize + h); if(current == nextLabel) p.drawText(QRect(position - w/2, 0, w, h), Qt::AlignHCenter, QString::number(current)); } if (ticks & QSlider::TicksBelow) { p.drawLine( position, r.height() - h - tickSize, position, r.height() - h ); if(current == nextLabel) p.drawText(QRect(position - w/2, r.height() - h + 3, w, h), Qt::AlignHCenter, QString::number(current)); } } else { if (ticks & QSlider::TicksAbove) { p.drawLine(w, position, tickSize + w, position); if(current == nextLabel) p.drawText(QRect(0, position - h/2, w - 3, h), Qt::AlignRight | Qt::AlignVCenter, QString::number(current)); } if (ticks & QSlider::TicksBelow) { p.drawLine(r.width() - w - tickSize, position, r.width() - w, position ); if(current == nextLabel) p.drawText(QRect(r.width() - w + 3, position - h/2, w, h), Qt::AlignVCenter, QString::number(current)); } } // go to next position if (current == nextLabel) nextLabel += interval*valStep; current += interval; } p.restore(); option.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; style()->proxy()->drawComplexControl(QStyle::CC_Slider, &option, &p, this); }
void TickSlider::paintEvent(QPaintEvent *ev) { Q_UNUSED(ev) QStylePainter p(this); QStyleOptionSlider opt; initStyleOption(&opt); QRect handle = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this); // Draw the slider first opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; opt.activeSubControls = getHoverControl(); // addition here p.drawComplexControl(QStyle::CC_Slider, opt); // draw tick marks // do this manually because they are very badly behaved with style sheets int interval = tickInterval(); if (interval == 0) { interval = pageStep(); } // see http://doc.qt.io/qt-5.7/coordsys.html // ... positive is right and down bool using_ticks = tickPosition() != NoTicks; bool using_labels = tickLabelPosition() != NoTicks; if (!using_ticks && !using_labels) { return; } QPen pen; pen.setColor(m_tick_colour); pen.setWidth(m_tick_thickness); p.setPen(pen); bool horizontal = orientation() == Qt::Horizontal; QSize biggest_label = biggestLabel(); int max_label_height = using_labels ? biggest_label.height() : 0; int max_label_width = using_labels ? biggest_label.width() : 0; if (horizontal) { // -------------------------------------------------------------------- // HORIZONTAL // -------------------------------------------------------------------- int move_tick_vertically_by = (max_label_height > 0) ? (max_label_height + m_tick_label_gap) : 0; // Top int bounding_box_top = this->rect().top(); int top_label_top = bounding_box_top; int top_tick_top = bounding_box_top + move_tick_vertically_by; int top_tick_bottom = top_tick_top + m_tick_length; // Bottom, working up int bounding_box_bottom = this->rect().bottom(); int bottom_label_bottom = bounding_box_bottom; int bottom_tick_bottom = bounding_box_bottom - move_tick_vertically_by; int bottom_tick_top = bottom_tick_bottom - m_tick_length; // OK: for (int i = minimum(); i <= maximum(); i += interval) { Qt::Alignment halign = Qt::AlignHCenter; if (m_edge_in_extreme_labels) { bool leftmost = i == minimum(); bool rightmost = i == maximum(); if (leftmost) { halign = Qt::AlignLeft; } else if (rightmost) { halign = Qt::AlignRight; } } int q = m_reverse_horizontal_labels ? (maximum() - i) : i; int x = round( (double)( (double)( (double)(q - this->minimum()) / (double)(this->maximum() - this->minimum()) ) * (double)(this->width() - handle.width()) + (double)(handle.width() / 2.0) ) ) - 1; bool has_label = m_tick_labels.contains(i); QString label_text; if (has_label) { label_text = m_tick_labels[i]; } if (tickPosition() == TicksBothSides || tickPosition() == TicksAbove) { p.drawLine(x, top_tick_top, x, top_tick_bottom); } if (has_label && (tickLabelPosition() == TicksBothSides || tickLabelPosition() == TicksAbove)) { UiFunc::drawText(p, x, top_label_top, halign | Qt::AlignTop, label_text); } if (tickPosition() == TicksBothSides || tickPosition() == TicksBelow) { p.drawLine(x, bottom_tick_top, x, bottom_tick_bottom); } if (has_label && (tickLabelPosition() == TicksBothSides || tickLabelPosition() == TicksBelow)) { UiFunc::drawText(p, x, bottom_label_bottom, halign | Qt::AlignBottom, label_text); } } } else { // -------------------------------------------------------------------- // VERTICAL // -------------------------------------------------------------------- int move_tick_horizontally_by = (max_label_width > 0) ? (max_label_width + m_tick_label_gap) : 0; // Left int bounding_box_left = this->rect().left(); int left_label_right = bounding_box_left + max_label_width; int left_tick_left = bounding_box_left + move_tick_horizontally_by; int left_tick_right = left_tick_left + m_tick_length; // Right, working leftwards int bounding_box_right = this->rect().right(); int right_label_left = bounding_box_right - max_label_width; int right_tick_right = bounding_box_right - move_tick_horizontally_by; int right_tick_left = right_tick_right - m_tick_length; // OK: for (int i = minimum(); i <= maximum(); i += interval) { Qt::Alignment valign = Qt::AlignVCenter; int q = m_reverse_vertical_labels ? (maximum() - i) : i; int y = round( (double)( (double)( (double)(q - this->minimum()) / (double)(this->maximum() - this->minimum()) ) * (double)(this->height() - handle.height()) + (double)(handle.height() / 2.0) ) ) - 1; bool has_label = m_tick_labels.contains(i); QString label_text; if (has_label) { label_text = m_tick_labels[i]; } if (tickPosition() == TicksBothSides || tickPosition() == TicksLeft) { p.drawLine(left_tick_left, y, left_tick_right, y); } if (has_label && (tickLabelPosition() == TicksBothSides || tickLabelPosition() == TicksLeft)) { UiFunc::drawText(p, left_label_right, y, Qt::AlignRight | valign, label_text); } if (tickPosition() == TicksBothSides || tickPosition() == TicksRight) { p.drawLine(right_tick_left, y, right_tick_right, y); } if (has_label && (tickLabelPosition() == TicksBothSides || tickLabelPosition() == TicksRight)) { UiFunc::drawText(p, right_label_left, y, Qt::AlignLeft | valign, label_text); } } } }