-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix data flow #43
Fix data flow #43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,8 @@ | |
|
||
WStarRating::WStarRating(const QString& group, QWidget* pParent) | ||
: WWidget(pParent), | ||
m_starRating(0, 5), | ||
m_focused(false), | ||
m_currRating(0) { | ||
m_starCount(0), | ||
m_visualStarRating(m_starCount, 5) { | ||
// Controls to change the star rating with controllers. | ||
// Note that 'group' maybe NULLPTR, e.g. when called from DlgTrackInfo, | ||
// so only create rate change COs if there's a group passed when creating deck widgets. | ||
|
@@ -38,22 +37,26 @@ QSize WStarRating::sizeHint() const { | |
QStyleOption option; | ||
option.initFrom(this); | ||
QSize widgetSize = style()->sizeFromContents(QStyle::CT_PushButton, &option, | ||
m_starRating.sizeHint(), this); | ||
m_visualStarRating.sizeHint(), this); | ||
|
||
m_contentRect.setRect( | ||
(widgetSize.width() - m_starRating.sizeHint().width()) / 2, | ||
(widgetSize.height() - m_starRating.sizeHint().height()) / 2, | ||
m_starRating.sizeHint().width(), | ||
m_starRating.sizeHint().height() | ||
(widgetSize.width() - m_visualStarRating.sizeHint().width()) / 2, | ||
(widgetSize.height() - m_visualStarRating.sizeHint().height()) / 2, | ||
m_visualStarRating.sizeHint().width(), | ||
m_visualStarRating.sizeHint().height() | ||
); | ||
|
||
return widgetSize; | ||
} | ||
|
||
void WStarRating::slotSetRating(int rating) { | ||
m_starRating.setStarCount(rating); | ||
m_currRating = rating; | ||
update(); | ||
void WStarRating::slotSetRating(int starCount) { | ||
if (starCount == m_starCount) { | ||
// Unchanged | ||
return; | ||
} | ||
m_starCount = starCount; | ||
resetVisualRating(); | ||
emit ratingChanged(starCount); | ||
} | ||
|
||
void WStarRating::paintEvent(QPaintEvent * /*unused*/) { | ||
|
@@ -64,70 +67,70 @@ void WStarRating::paintEvent(QPaintEvent * /*unused*/) { | |
painter.setBrush(option.palette.text()); | ||
painter.drawPrimitive(QStyle::PE_Widget, option); | ||
|
||
m_starRating.paint(&painter, m_contentRect); | ||
m_visualStarRating.paint(&painter, m_contentRect); | ||
} | ||
|
||
void WStarRating::mouseMoveEvent(QMouseEvent *event) { | ||
m_focused = true; | ||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) | ||
int star = starAtPosition(event->position().toPoint().x()); | ||
#else | ||
int star = starAtPosition(event->x()); | ||
#endif | ||
|
||
if (star != m_starRating.starCount() && star != -1) { | ||
m_starRating.setStarCount(star); | ||
update(); | ||
if (star != -1) { | ||
updateVisualRating(star); | ||
} | ||
} | ||
|
||
void WStarRating::slotStarsUp(double v) { | ||
if (v > 0 && m_currRating < m_starRating.maxStarCount()) { | ||
int star = m_currRating + 1; | ||
emit ratingChanged(star); | ||
if (v > 0 && m_starCount < m_visualStarRating.maxStarCount()) { | ||
slotSetRating(m_starCount + 1); | ||
} | ||
} | ||
|
||
void WStarRating::slotStarsDown(double v) { | ||
if (v > 0 && m_currRating > 0) { | ||
int star = m_currRating - 1; | ||
emit ratingChanged(star); | ||
if (v > 0 && m_starCount > StarRating::kMinStarCount) { | ||
slotSetRating(m_starCount - 1); | ||
} | ||
} | ||
|
||
void WStarRating::leaveEvent(QEvent* /*unused*/) { | ||
m_focused = false; | ||
// reset to applied track rating | ||
m_starRating.setStarCount(m_currRating); | ||
resetVisualRating(); | ||
} | ||
|
||
void WStarRating::updateVisualRating(int starCount) { | ||
if (starCount == m_visualStarRating.starCount()) { | ||
return; | ||
} | ||
m_visualStarRating.setStarCount(starCount); | ||
update(); | ||
} | ||
|
||
// The method uses basic linear algebra to find out which star is under the cursor. | ||
int WStarRating::starAtPosition(int x) { | ||
int WStarRating::starAtPosition(int x) const { | ||
// If the mouse is very close to the left edge, set 0 stars. | ||
if (x < m_starRating.sizeHint().width() * 0.05) { | ||
if (x < m_visualStarRating.sizeHint().width() * 0.05) { | ||
return 0; | ||
} | ||
int star = (x / (m_starRating.sizeHint().width() / m_starRating.maxStarCount())) + 1; | ||
int star = (x / (m_visualStarRating.sizeHint().width() / m_visualStarRating.maxStarCount())) + 1; | ||
|
||
if (star <= 0 || star > m_starRating.maxStarCount()) { | ||
if (star <= 0 || star > m_visualStarRating.maxStarCount()) { | ||
return 0; | ||
} | ||
|
||
return star; | ||
} | ||
|
||
void WStarRating::mouseReleaseEvent(QMouseEvent* /*unused*/) { | ||
emit ratingChanged(m_starRating.starCount()); | ||
slotSetRating(m_visualStarRating.starCount()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels wrong: we now store the stars without confirmation, as if it was an update from the owner/mediator. IMO it was correct before: emit the change (don't store it) and wait for the owner to confirm/reject/reset. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Result of this implementation is that you can now set stars in the rating widget of an empty deck. |
||
} | ||
|
||
void WStarRating::fillDebugTooltip(QStringList* debug) { | ||
WWidget::fillDebugTooltip(debug); | ||
|
||
QString currentRating; | ||
currentRating.setNum(m_currRating); | ||
QString maximumRating = QString::number(m_starRating.maxStarCount()); | ||
|
||
currentRating.setNum(m_starCount); | ||
QString maximumRating = QString::number(m_visualStarRating.maxStarCount()); | ||
|
||
*debug << QString("Rating: %1/%2").arg(currentRating, maximumRating); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be
updateVisualrating()
?Also, emitting the signal again with the value we may just received doesn't make sense to me. What for?
It seems emit is required here only because you use
slotSetRating
instead of emit inmouseReleaseEvent
.