MCPcopy Create free account
hub / github.com/Liniyous/ElaWidgetTools / paintEvent

Method paintEvent

ElaWidgetTools/ElaToggleSwitch.cpp:143–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141}
142
143void ElaToggleSwitch::mouseMoveEvent(QMouseEvent* event)
144{
145 Q_D(ElaToggleSwitch);
146 if (!d->_isLeftButtonPress)
147 {
148 QWidget::mouseMoveEvent(event);
149 return;
150 }
151 // 滑块立即跟手,距离阈值仅用于松开时区分点击与拖动
152 if ((event->pos() - d->_mousePressPos).manhattanLength() >= qMax(1, QApplication::startDragDistance()))
153 {
154 d->_isDragging = true;
155 }
156 d->_circleCenterX += event->pos().x() - d->_lastMouseX;
157 d->_lastMouseX = event->pos().x();
158 d->_adjustCircleCenterX();
159 setProperty("circleCenterX", d->_circleCenterX);
160 update();
161 event->accept();
162}
163
164void ElaToggleSwitch::paintEvent(QPaintEvent* event)
165{
166 Q_D(ElaToggleSwitch);
167 const int trackHeight = d->_getTrackHeight();
168 QPainter painter(this);
169 painter.save();
170 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
171 // 外框按标准高度参与布局 轨道和滑块在外框内垂直居中
172 painter.translate(0, (height() - trackHeight) / 2.0);
173 // 背景绘制
174 painter.setPen(d->_isToggled ? QPen(ElaThemeColor(d->_themeMode, BasicBorder), 1.5) : QPen(ElaThemeColor(d->_themeMode, BasicBorderDeep), 1.5));
175 QStyleOption option;
176 option.initFrom(this);
177 option.state.setFlag(QStyle::State_Sunken, d->_isLeftButtonPress);
178 painter.setBrush(ElaColorAnimation::getColor(this, ElaControlStyle::getIndicatorBackgroundColor(d->_themeMode, option.state, d->_isToggled)));
179 QPainterPath path;
180 const int arcDiameter = trackHeight - d->_margin * 2;
181 path.moveTo(width() - trackHeight - d->_margin, trackHeight - d->_margin);
182 path.arcTo(QRectF(width() - trackHeight - d->_margin, d->_margin, arcDiameter, arcDiameter), -90, 180);
183 path.lineTo(trackHeight / 2 + d->_margin, d->_margin);
184 path.arcTo(QRectF(d->_margin, d->_margin, arcDiameter, arcDiameter), 90, 180);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected