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 Feb 11, 2024
1 parent 51eec94 commit 9b49ba0
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,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 @@ -2001,6 +2001,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 @@ -1118,6 +1118,7 @@ WLibraryTextBrowser QMenu,
WTrackMenu,
WTrackMenu QMenu,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
QLineEdit QMenu,
QPlainTextEdit QMenu,
WEffectChainPresetButton QMenu,
Expand Down Expand Up @@ -2486,6 +2487,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 @@ -1209,6 +1209,7 @@ WLibraryTextBrowser QMenu,
WTrackMenu,
WTrackMenu QMenu,
WTrackMenu QMenu QCheckBox,
WTrackMenu WStarRating,
WEffectChainPresetButton QMenu,
WEffectChainPresetButton QMenu QCheckBox,
QLineEdit QMenu,
Expand Down Expand Up @@ -2940,6 +2941,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 @@ -107,6 +107,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 @@ -2303,6 +2303,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 @@ -44,7 +44,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"

namespace {
const QString kAppGroup = QStringLiteral("[App]");
Expand Down Expand Up @@ -331,6 +333,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 @@ -603,6 +612,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 @@ -734,6 +747,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 @@ -1036,6 +1082,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 @@ -1623,6 +1674,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
12 changes: 11 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 @@ -99,6 +100,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 @@ -204,6 +208,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 All @@ -230,6 +238,7 @@ class WTrackMenu : public QMenu {
QMenu* m_pClearMetadataMenu{};
QMenu* m_pAnalyzeMenu{};
QMenu* m_pBPMMenu{};
QMenu* m_pRatingMenu{};
QMenu* m_pColorMenu{};
WCoverArtMenu* m_pCoverMenu{};
parented_ptr<WSearchRelatedTracksMenu> m_pSearchRelatedMenu;
Expand Down Expand Up @@ -285,7 +294,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 9b49ba0

Please sign in to comment.