| 177 | } |
| 178 | |
| 179 | void ToggleSwitch::paintEvent(QPaintEvent* /*event*/) { |
| 180 | QPainter painter(this); |
| 181 | painter.setRenderHint(QPainter::Antialiasing, true); |
| 182 | |
| 183 | // Track: pill (corner radius = half the height). Fill interpolates |
| 184 | // from the "off" tone to the "on" tone as the thumb moves so the |
| 185 | // color transition tracks the animation smoothly. |
| 186 | const QColor off_track(120, 120, 120); |
| 187 | const QColor on_track(0x11, 0x77, 0xFF); // blue |
| 188 | const auto lerp = [](int a, int b, qreal t) { return static_cast<int>(a + ((b - a) * t)); }; |
| 189 | const QColor track_color( |
| 190 | lerp(off_track.red(), on_track.red(), thumb_position_), |
| 191 | lerp(off_track.green(), on_track.green(), thumb_position_), |
| 192 | lerp(off_track.blue(), on_track.blue(), thumb_position_)); |
| 193 | |
| 194 | const QRect track = trackRect(); |
| 195 | const qreal radius = track.height() / 2.0; |
| 196 | painter.setPen(Qt::NoPen); |
| 197 | painter.setBrush(track_color); |
| 198 | painter.drawRoundedRect(track, radius, radius); |
| 199 | |
| 200 | // Optional inline label, in the palette text colour (faded when disabled), |
| 201 | // aligned toward the switch and vertically centred. |
| 202 | if (!text_.isEmpty()) { |
| 203 | const QPalette::ColorGroup group = isEnabled() ? QPalette::Active : QPalette::Disabled; |
| 204 | painter.setPen(palette().color(group, QPalette::WindowText)); |
| 205 | const QRect lr = labelRect(); |
| 206 | const int align = (label_side_ == LabelSide::Left ? Qt::AlignRight : Qt::AlignLeft) | Qt::AlignVCenter; |
| 207 | const QString elided = fontMetrics().elidedText(text_, Qt::ElideRight, lr.width()); |
| 208 | painter.drawText(lr, align, elided); |
| 209 | } |
| 210 | |
| 211 | // Both slot backgrounds are always painted; the thumb composites on top. |
| 212 | // Default impls fade each based on thumb_position_ so only the icon |
| 213 | // opposite the thumb is visible at rest. |
| 214 | paintLeftSlot(painter, leftSlotRect(), thumb_position_); |
| 215 | paintRightSlot(painter, rightSlotRect(), thumb_position_); |
| 216 | |
| 217 | // Thumb: white circle with a faint outer outline for definition. |
| 218 | const QRect thumb = thumbRect(); |
| 219 | painter.setPen(QPen(QColor(0, 0, 0, 40), 1)); |
| 220 | painter.setBrush(Qt::white); |
| 221 | painter.drawEllipse(thumb); |
| 222 | } |
| 223 | |
| 224 | void ToggleSwitch::paintLeftSlot(QPainter& painter, const QRect& slot_rect, qreal thumb_position) { |
| 225 | if (left_icon_.isNull()) { |