void LyricWidget::setTime(const QTime &time) { if (lyric.size() == 0) return; int befind = curInd; if (time < lyric[curInd].time) { for (int i = curInd - 1; i >= 0; -- i) { if (lyric[i].time <= time && lyric[i + 1].time > time) { curInd = i; break; } } } else { if (curInd == lyric.size() - 2 && time >= lyric[lyric.size() - 1].time) curInd ++; else if (curInd < lyric.size() - 2) { for (int i = curInd; i < lyric.size(); ++ i) { if (lyric[i].time > time) { curInd = i > 0 ? i - 1 : 0; break; } } } } if (curInd == 0 && !firstShowing) firstShowing = true; else if (curInd == befind) return; QPropertyAnimation *anim = new QPropertyAnimation(animWidget, "geometry"); anim->setDuration(400); anim->setStartValue(animWidget->geometry()); int accuHeight = 0; for (int i = 0; i < curInd; ++ i) accuHeight += heights[i]; accuHeight += heights[curInd] / 2; accuHeight = this->height() / 2 - accuHeight; QRect endval(0, accuHeight, animWidget->width(), animWidget->height()); anim->setEndValue(endval); anim->setEasingCurve(QEasingCurve::OutCubic); labels[befind]->setText(labels[befind]->text().replace("black", "grey")); connect(anim, &QPropertyAnimation::finished, [this] () { labels[curInd]->setText(labels[curInd]->text().replace("grey", "black")); }); anim->start(QPropertyAnimation::DeleteWhenStopped); }
void HorizontalSlider::scrollToIndex(int index) { if (index == curIndex) return; if (index > numberOfChildren() - 1) return; this->curIndex = index; QPropertyAnimation *anim = new QPropertyAnimation(container, "geometry"); anim->setDuration(300); anim->setStartValue(container->geometry()); int accuwidth = 0; for (int i = 0; i < index; ++ i) accuwidth += widths[i]; accuwidth += widths[index] / 2 - this->width() / 2; QRect endval(-accuwidth, container->geometry().y(), container->geometry().width(), container->geometry().height() ); anim->setEndValue(endval); anim->setEasingCurve(QEasingCurve::OutCubic); connect(anim, &QPropertyAnimation::finished, [=] () { emit scrollFinished(); }); anim->start(QAbstractAnimation::DeleteWhenStopped); }