Skip to content

Commit

Permalink
Track menu: add star rating action
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Apr 18, 2024
1 parent ea9e160 commit 9fbffb4
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL
src/widget/wpixmapstore.cpp
src/widget/wpushbutton.cpp
src/widget/wraterange.cpp
src/widget/wstarratingaction.cpp
src/widget/wrecordingduration.cpp
src/widget/wscrollable.cpp
src/widget/wsearchlineedit.cpp
Expand Down
1 change: 1 addition & 0 deletions res/skins/Deere/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
2 changes: 2 additions & 0 deletions res/skins/LateNight/style_classic.qss
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ WLibraryTextBrowser QMenu,
WTrackMenu,
WTrackMenu QMenu,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QPlainTextEdit QMenu,
WEffectChainPresetButton QMenu,
Expand Down Expand Up @@ -2499,6 +2500,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
2 changes: 2 additions & 0 deletions res/skins/LateNight/style_palemoon.qss
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ WLibraryTextBrowser QMenu,
WTrackMenu,
WTrackMenu QMenu,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
WEffectChainPresetButton QMenu,
WEffectChainPresetButton QMenu QCheckBox,
QLineEdit QMenu,
Expand Down Expand Up @@ -2957,6 +2958,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
1 change: 1 addition & 0 deletions res/skins/Shade/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
1 change: 1 addition & 0 deletions res/skins/Shade/style_dark.qss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
1 change: 1 addition & 0 deletions res/skins/Shade/style_summer_sunset.qss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
1 change: 1 addition & 0 deletions res/skins/Tango/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,7 @@ WTrackMenu::item,
WTrackMenu QMenu,
WTrackMenu QMenu::item,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QLineEdit QMenu::item,
QPlainTextEdit QMenu,
Expand Down
35 changes: 35 additions & 0 deletions src/widget/wstarratingaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "widget/wstarratingaction.h"

#include <QHBoxLayout>

#include "moc_wstarratingaction.cpp"
#include "widget/wstarrating.h"

WStarRatingAction::WStarRatingAction(QWidget* parent)
: QWidgetAction(parent),
m_pStarRating(make_parented<WStarRating>(parent)) {
m_pStarRating->setMouseTracking(true);
// forward the signal
connect(m_pStarRating,
&WStarRating::ratingChangeRequest,
this,
&WStarRatingAction::ratingSet);

QHBoxLayout* pLayout = new QHBoxLayout();
pLayout->addWidget(m_pStarRating);
pLayout->setSizeConstraint(QLayout::SetFixedSize);
pLayout->setContentsMargins(0, 0, 0, 0);

QWidget* pWidget = new QWidget();
pWidget->setLayout(pLayout);
pWidget->setSizePolicy(QSizePolicy());
setDefaultWidget(pWidget);
}

void WStarRatingAction::setRating(const int rating) {
m_pStarRating->slotSetRating(rating);
}

QSize WStarRatingAction::sizeHint() {
return m_pStarRating->sizeHint();
}
23 changes: 23 additions & 0 deletions src/widget/wstarratingaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <QWidgetAction>

#include "util/parented_ptr.h"

class WStarRating;

class WStarRatingAction : public QWidgetAction {
Q_OBJECT
public:
explicit WStarRatingAction(QWidget* parent = nullptr);

void setRating(const int rating);

QSize sizeHint();

signals:
void ratingSet(const int rating);

private:
parented_ptr<WStarRating> m_pStarRating;
};
86 changes: 86 additions & 0 deletions src/widget/wtrackmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
#include "widget/wcoverartmenu.h"
#include "widget/wfindonwebmenu.h"
#include "widget/wsearchrelatedtracksmenu.h"
// WStarRating is required for DlgTrackInfo
#include "widget/wstarrating.h"
#include "widget/wstarratingaction.h"

constexpr WTrackMenu::Features WTrackMenu::kDeckTrackMenuFeatures;

Expand Down Expand Up @@ -334,6 +336,13 @@ void WTrackMenu::createActions() {
}

if (featureIsEnabled(Feature::Properties)) {
m_pStarRatingAction = new WStarRatingAction(this);
m_pStarRatingAction->setObjectName("RatingAction");
connect(m_pStarRatingAction,
&WStarRatingAction::ratingSet,
this,
&WTrackMenu::slotSetRating);

m_pPropertiesAct = new QAction(tr("Properties"), this);
// This is just for having the shortcut displayed next to the action
// when the menu is invoked from the tracks table.
Expand Down Expand Up @@ -606,6 +615,10 @@ void WTrackMenu::setupActions() {
addMenu(m_pBPMMenu);
}

if (featureIsEnabled(Feature::Properties)) {
addAction(m_pStarRatingAction);
}

if (featureIsEnabled(Feature::Color)) {
m_pColorMenu->addAction(m_pColorPickerAction);
addMenu(m_pColorMenu);
Expand Down Expand Up @@ -737,6 +750,39 @@ std::pair<bool, bool> WTrackMenu::getTrackBpmLockStates() const {
return std::pair<bool, bool>(anyBpmLocked, anyBpmNotLocked);
}

int WTrackMenu::getCommonTrackRating() const {
VERIFY_OR_DEBUG_ASSERT(!isEmpty()) {
return 0;
}
int commonRating;
if (m_pTrackModel) {
const int column =
m_pTrackModel->fieldIndex(LIBRARYTABLE_RATING);
commonRating = m_trackIndexList.first()
.sibling(m_trackIndexList.first().row(), column)
.data()
.value<StarRating>()
.starCount();
for (const auto& trackIndex : m_trackIndexList) {
const auto otherRating =
trackIndex.sibling(trackIndex.row(), column)
.data()
.value<StarRating>()
.starCount();
if (commonRating != otherRating) {
// Multiple, different ratings
return 0;
}
}
} else {
if (!m_pTrack) {
return 0;
}
commonRating = m_pTrack->getRating();
}
return commonRating;
}

std::optional<std::optional<mixxx::RgbColor>> WTrackMenu::getCommonTrackColor() const {
VERIFY_OR_DEBUG_ASSERT(!isEmpty()) {
return std::nullopt;
Expand Down Expand Up @@ -1039,6 +1085,11 @@ void WTrackMenu::updateMenus() {
}

if (featureIsEnabled(Feature::Properties)) {
// Might be needed to resize Menu to fit the star rating
// QResizeEvent resizeEvent(QSize(), m_pStarRatingAction->sizeHint());
// qApp->sendEvent(m_pStarRatingAction, &resizeEvent);
m_pStarRatingAction->setRating(getCommonTrackRating());

m_pPropertiesAct->setEnabled(singleTrackSelected);
}

Expand Down Expand Up @@ -1626,6 +1677,41 @@ void WTrackMenu::lockBpm(bool lock) {

namespace {

class SetRatingTrackPointerOperation : public mixxx::TrackPointerOperation {
public:
explicit SetRatingTrackPointerOperation(const int rating)
: m_rating(rating) {
}

private:
void doApply(
const TrackPointer& pTrack) const override {
pTrack->setRating(m_rating);
}

const int m_rating;
};

} // anonymous namespace

void WTrackMenu::slotSetRating(int rating) {
if (!mixxx::TrackRecord::isValidRating(rating)) {
return;
}

const auto progressLabelText =
tr("Setting rating of %n track(s)", "", getTrackCount());
const auto trackOperator =
SetRatingTrackPointerOperation(rating);
applyTrackPointerOperation(
progressLabelText,
&trackOperator);

hide();
}

namespace {

class SetColorTrackPointerOperation : public mixxx::TrackPointerOperation {
public:
explicit SetColorTrackPointerOperation(const mixxx::RgbColor::optional_t& color)
Expand Down
11 changes: 10 additions & 1 deletion src/widget/wtrackmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class WColorPickerAction;
class WCoverArtMenu;
class WFindOnWebMenu;
class WSearchRelatedTracksMenu;
class WStarRatingAction;

/// A context menu for track(s).
/// Can be used with individual track type widgets based on TrackPointer
Expand Down Expand Up @@ -117,6 +118,9 @@ class WTrackMenu : public QMenu {
void slotOpenInFileBrowser();
void slotSelectInLibrary();

// Track rating
void slotSetRating(int rating);

// Row color
void slotColorPicked(const mixxx::RgbColor::optional_t& color);

Expand Down Expand Up @@ -222,6 +226,10 @@ class WTrackMenu : public QMenu {

std::pair<bool, bool> getTrackBpmLockStates() const;

/// Get the common rating of all selected tracks.
/// Return 0 if ratings differ.
int getCommonTrackRating() const;

/// Get the common track color of all tracks this menu is shown for, or
/// return `nullopt` if there is no common color. Tracks may have no color
/// assigned to them. In that case the inner optional is set to `nullopt`.
Expand Down Expand Up @@ -303,7 +311,8 @@ class WTrackMenu : public QMenu {
QAction* m_pBpmThreeHalvesAction{};
QAction* m_pBpmResetAction{};

// Track color
// Track rating and color
WStarRatingAction* m_pStarRatingAction{};
WColorPickerAction* m_pColorPickerAction{};

// Analysis actions
Expand Down

0 comments on commit 9fbffb4

Please sign in to comment.