Skip to content

Commit

Permalink
Library: paint played tracks with custom color (qss)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Feb 9, 2024
1 parent 8325fa3 commit 3d8054c
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 8 deletions.
9 changes: 6 additions & 3 deletions res/skins/Deere/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,14 @@ WTrackTableView,
WTrackTableView QCheckBox:focus {
outline: 1px solid #D6D6D6;
}
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #D6D6D6;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

WTrackTableView:focus,
Expand Down
6 changes: 6 additions & 0 deletions res/skins/LateNight/style_classic.qss
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,13 @@ WTrackTableView QCheckBox:focus {
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #fff;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

WLibrarySidebar {
Expand Down
9 changes: 6 additions & 3 deletions res/skins/LateNight/style_palemoon.qss
Original file line number Diff line number Diff line change
Expand Up @@ -2586,11 +2586,14 @@ WTrackTableView,
WTrackTableView QCheckBox:focus {
outline: 1px solid #fff;
}
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #fff;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

/* Table cell in edit mode */
Expand Down
6 changes: 6 additions & 0 deletions res/skins/Shade/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,13 @@ WTrackTableView QCheckBox:focus {
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #c9c9c9;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

/* Table cell in edit mode */
Expand Down
6 changes: 6 additions & 0 deletions res/skins/Shade/style_dark.qss
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ WTrackTableView QCheckBox:focus {
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #ccc;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

/* Table cell in edit mode */
Expand Down
6 changes: 6 additions & 0 deletions res/skins/Tango/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,13 @@ WTrackTableView QCheckBox:focus {
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
WTrackTableView {
/* This uses a custom qproperty to set the focus border
for Color and Cover Art cells, 1px solid, sharp corners.
See src/library/tableitemdelegate.cpp */
qproperty-focusBorderColor: #fff;
/* This is the color used to paint the text of played tracks.
BaseTrackTableModel::data() uses this to override Qt::ForegroundRole (QBrush). */
qproperty-playedInactiveColor: #555;
}

/* Table cell in edit mode */
Expand Down
22 changes: 21 additions & 1 deletion src/library/basetracktablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ BaseTrackTableModel::BaseTrackTableModel(
TrackModel(cloneDatabase(pTrackCollectionManager), settingsNamespace),
m_pTrackCollectionManager(pTrackCollectionManager),
m_previewDeckGroup(PlayerManager::groupForPreviewDeck(0)),
m_backgroundColorOpacity(WLibrary::kDefaultTrackTableBackgroundColorOpacity) {
m_backgroundColorOpacity(WLibrary::kDefaultTrackTableBackgroundColorOpacity),
m_playedInactiveColor(QColor::fromRgb(WTrackTableView::kDefaultPlayedInactiveColorHex)) {
connect(&pTrackCollectionManager->internalCollection()->getTrackDAO(),
&TrackDAO::forceModelUpdate,
this,
Expand Down Expand Up @@ -395,6 +396,15 @@ QAbstractItemDelegate* BaseTrackTableModel::delegateForColumn(
return nullptr;
}
m_backgroundColorOpacity = pTableView->getBackgroundColorOpacity();
// This is the color used for the text of played tracks.
// data() uses this to compose the ForegroundRole QBrush if 'played' is checked.
m_playedInactiveColor = pTableView->getPlayedInactiveColor();
connect(pTableView,
&WTrackTableView::playedInactiveColorChanged,
this,
[this](QColor col) {
m_playedInactiveColor = col;
});
if (index == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_RATING)) {
return new StarDelegate(pTableView);
} else if (index == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_BPM)) {
Expand Down Expand Up @@ -447,6 +457,16 @@ QVariant BaseTrackTableModel::data(
DEBUG_ASSERT(m_backgroundColorOpacity <= 1.0);
bgColor.setAlphaF(static_cast<float>(m_backgroundColorOpacity));
return QBrush(bgColor);
} else if (role == Qt::ForegroundRole) {
// Custom text color for played tracks
auto playedRaw = rawSiblingValue(
index,
ColumnCache::COLUMN_LIBRARYTABLE_PLAYED);
if (!playedRaw.isNull() &&
playedRaw.canConvert<bool>() &&
playedRaw.toBool()) {
return QVariant::fromValue(m_playedInactiveColor);
}
}

// Only retrieve a value for supported roles
Expand Down
1 change: 1 addition & 0 deletions src/library/basetracktablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class BaseTrackTableModel : public QAbstractTableModel, public TrackModel {
const QString m_previewDeckGroup;

double m_backgroundColorOpacity;
QColor m_playedInactiveColor;

ColumnCache m_columnCache;

Expand Down
22 changes: 22 additions & 0 deletions src/library/bpmdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ void BPMDelegate::paintItem(QPainter* painter,const QStyleOptionViewItem &option
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);

// The checkbox uses the QTableView's qss style, therefore it's not picking
// up the 'played' text color via ForegroundRole from BaseTrackTableModel::data().
// Enforce it with an explicit stylesheet. Note: the stylesheet persists so
// we need to reset it to normal/highlighted.
QColor textColor;
auto dat = index.data(Qt::ForegroundRole);
if (dat.canConvert<QColor>()) {
textColor = dat.value<QColor>();
} else {
if (option.state & QStyle::State_Selected) {
textColor = option.palette.color(QPalette::Normal, QPalette::HighlightedText);
} else {
textColor = option.palette.color(QPalette::Normal, QPalette::Text);
// qWarning() << " !! BPM col:" << textColor.name(QColor::HexRgb);
}
}
if (textColor.isValid()) {
m_pCheckBox->setStyleSheet(QStringLiteral(
"#LibraryBPMButton::item { color: %1; }")
.arg(textColor.name(QColor::HexRgb)));
}

QStyle* style = m_pTableView->style();
if (style != nullptr) {
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, m_pCheckBox);
Expand Down
15 changes: 14 additions & 1 deletion src/library/tableitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@ void TableItemDelegate::paint(
if (option.state & QStyle::State_Selected) {
painter->setBrush(option.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setBrush(option.palette.color(cg, QPalette::Text));
// This gets the custom 'played' text color from BaseTrackTableModel
// depending on check state of the 'played' column.
// Note that we need to do this again in BPMDelegate which uses the
// style of the TableView.
auto playedColorData = index.data(Qt::ForegroundRole);
if (playedColorData.canConvert<QColor>()) {
QColor playedColor = playedColorData.value<QColor>();
// for the star rating polygons
painter->setBrush(playedColor);
// for the 'location' text
painter->setPen(playedColor);
} else {
painter->setBrush(option.palette.color(cg, QPalette::Text));
}
}

QStyle* style = m_pTableView->style();
Expand Down
1 change: 1 addition & 0 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ WTrackTableView::WTrackTableView(QWidget* parent,
m_backgroundColorOpacity(backgroundColorOpacity),
// Default color for the focus border of TableItemDelegates
m_focusBorderColor(Qt::white),
m_playedInactiveColor(QColor::fromRgb(kDefaultPlayedInactiveColorHex)),
m_sorting(sorting),
m_selectionChangedSinceLastGuiTick(true),
m_loadCachedOnly(false) {
Expand Down
13 changes: 13 additions & 0 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ class WTrackTableView : public WLibraryTableView {
return m_focusBorderColor;
}

// Default color for played tracks' text color. #555555, bit darker than Qt::darkgray.
// BaseTrackTableModel uses this for the ForegroundRole of played tracks.
static constexpr uint kDefaultPlayedInactiveColorHex = 555555;
Q_PROPERTY(QColor playedInactiveColor
MEMBER m_playedInactiveColor
NOTIFY playedInactiveColorChanged
DESIGNABLE true);
QColor getPlayedInactiveColor() const {
return m_playedInactiveColor;
}

signals:
void trackMenuVisible(bool visible);
void focusBorderColorChanged(QColor col);
void playedInactiveColorChanged(QColor col);

public slots:
void loadTrackModel(QAbstractItemModel* model, bool restoreState = false);
Expand Down Expand Up @@ -129,6 +141,7 @@ class WTrackTableView : public WLibraryTableView {

const double m_backgroundColorOpacity;
QColor m_focusBorderColor;
QColor m_playedInactiveColor;
bool m_sorting;

// Control the delay to load a cover art.
Expand Down

0 comments on commit 3d8054c

Please sign in to comment.