#pragma execution_character_set("utf-8") #include "wkslider.h" #include "navlabel.h" #include "qpainter.h" #include "qevent.h" #include "qdebug.h" WKSlider::WKSlider(QWidget *parent) : QSlider(parent) { borderRadius = 5; arrowSize = 5; arrowPosition = ArrowPosition_Bottom; background = QColor(100, 184, 255); foreground = QColor(255, 255, 255); labTipWidth = 50; labTipHeight = 30; labTipFont = this->font(); clickEnable = true; unit = ""; labTip = new NavLabel; labTip->setBorderRadius(borderRadius); labTip->setArrowSize(arrowSize); labTip->setArrowPosition((NavLabel::ArrowPosition)arrowPosition); labTip->setBackground(background); labTip->setForeground(foreground); labTip->resize(labTipWidth, labTipHeight); labTip->setFont(labTipFont); labTip->setFocusPolicy(Qt::NoFocus); labTip->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint); labTip->setAttribute(Qt::WA_TranslucentBackground, true); this->setOrientation(Qt::Horizontal); } WKSlider::~WKSlider() { labTip->deleteLater(); } void WKSlider::mousePressEvent(QMouseEvent *e) { //限定必须是鼠标左键按下 if (e->button() != Qt::LeftButton) { return; } if (clickEnable) { //获取鼠标的位置 double pos, value; if (orientation() == Qt::Horizontal) { pos = e->pos().x() / (double)width(); value = pos * (maximum() - minimum()) + minimum(); } else { pos = e->pos().y() / (double)height(); value = maximum() - pos * (maximum() - minimum()) + minimum(); } setValue(value + 0.5); //发送自定义的鼠标单击信号 emit clicked(); } mouseMoveEvent(e); labTip->setVisible(true); QSlider::mousePressEvent(e); } void WKSlider::mouseReleaseEvent(QMouseEvent *e) { labTip->setVisible(false); QSlider::mouseReleaseEvent(e); } void WKSlider::mouseMoveEvent(QMouseEvent *e) { //限定必须是鼠标左键按下 if (e->button() & Qt::RightButton) { return; } //过滤掉控件外坐标,横向的限制X轴,纵向的限制Y轴 QPoint pos = e->pos(); int x = pos.x(); int y = pos.y(); if (orientation() == Qt::Horizontal) { if (x < 0 || x > width()) { return; } x = QCursor::pos().x(); y = mapToGlobal(this->pos()).y(); x = x - labTipWidth / 2 + this->x(); y = y - (arrowPosition == ArrowPosition_Top ? -height() : labTipHeight); } else { if (y < 0 || y > height()) { return; } x = mapToGlobal(this->pos()).x(); y = QCursor::pos().y(); x = x - (arrowPosition == ArrowPosition_Left ? -width() : labTipWidth); y = y - labTipHeight / 2 + this->y(); } labTip->setText(QString("%1%2").arg(value()).arg(unit)); labTip->move(this->mapFromParent(QPoint(x, y))); QSlider::mouseMoveEvent(e); } int WKSlider::getBorderRadius() const { return this->borderRadius; } int WKSlider::getArrowSize() const { return this->arrowSize; } WKSlider::ArrowPosition WKSlider::getArrowPosition() const { return this->arrowPosition; } QColor WKSlider::getBackground() const { return this->background; } QColor WKSlider::getForeground() const { return this->foreground; } int WKSlider::getLabTipWidth() const { return this->labTipWidth; } int WKSlider::getLabTipHeight() const { return this->labTipHeight; } QFont WKSlider::getLabTipFont() const { return this->labTipFont; } bool WKSlider::getClickEnable() const { return this->clickEnable; } QString WKSlider::getUnit() const { return this->unit; } void WKSlider::setBorderRadius(int borderRadius) { if (this->borderRadius != borderRadius) { this->borderRadius = borderRadius; labTip->setBorderRadius(borderRadius); } } void WKSlider::setArrowSize(int arrowSize) { if (this->arrowSize != arrowSize) { this->arrowSize = arrowSize; labTip->setArrowSize(arrowSize); } } void WKSlider::setArrowPosition(const WKSlider::ArrowPosition &arrowPosition) { if (this->arrowPosition != arrowPosition) { this->arrowPosition = arrowPosition; labTip->setArrowPosition((NavLabel::ArrowPosition)arrowPosition); } } void WKSlider::setBackground(const QColor &background) { if (this->background != background) { this->background = background; labTip->setBackground(background); } } void WKSlider::setForeground(const QColor &foreground) { if (this->foreground != foreground) { this->foreground = foreground; labTip->setForeground(foreground); } } void WKSlider::setLabTipWidth(int labTipWidth) { if (this->labTipWidth != labTipWidth) { this->labTipWidth = labTipWidth; labTip->resize(labTipWidth, labTipHeight); } } void WKSlider::setLabTipHeight(int labTipHeight) { if (this->labTipHeight != labTipHeight) { this->labTipHeight = labTipHeight; labTip->resize(labTipWidth, labTipHeight); } } void WKSlider::setLabTipFont(const QFont &labTipFont) { if (this->labTipFont != labTipFont) { this->labTipFont = labTipFont; labTip->setFont(labTipFont); } } void WKSlider::setClickEnable(bool clickEnable) { if (this->clickEnable != clickEnable) { this->clickEnable = clickEnable; } } void WKSlider::setUnit(const QString &unit) { if (this->unit != unit) { this->unit = unit; } }