Skip to content

Commit

Permalink
Merge pull request mixxxdj#12744 from ronso0/lib-played-text-color
Browse files Browse the repository at this point in the history
Tracks: custom text color for played tracks (qss)
  • Loading branch information
daschuer authored Feb 29, 2024
2 parents a5e4626 + 3d8054c commit 2d2d5f3
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 23 deletions.
9 changes: 6 additions & 3 deletions res/skins/Deere/style.qss
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,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 @@ -2121,7 +2121,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 @@ -2587,11 +2587,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 @@ -526,7 +526,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 @@ -198,7 +198,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 @@ -2615,7 +2615,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 @@ -125,7 +125,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 @@ -396,6 +397,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 @@ -450,6 +460,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 @@ -92,6 +92,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
2 changes: 1 addition & 1 deletion src/library/colordelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ void ColorDelegate::paintItem(

// Draw a border if the color cell has focus
if (option.state & QStyle::State_HasFocus) {
drawBorder(painter, m_pFocusBorderColor, option.rect);
drawBorder(painter, m_focusBorderColor, option.rect);
}
}
2 changes: 1 addition & 1 deletion src/library/coverartdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ void CoverArtDelegate::paintItem(

// Draw a border if the cover art cell has focus
if (option.state & QStyle::State_HasFocus) {
drawBorder(painter, m_pFocusBorderColor, option.rect);
drawBorder(painter, m_focusBorderColor, option.rect);
}
}
2 changes: 1 addition & 1 deletion src/library/stardelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ QWidget* StarDelegate::createEditor(QWidget* parent,
initStyleOption(&newOption, index);

StarEditor* editor =
new StarEditor(parent, m_pTableView, index, newOption, m_pFocusBorderColor);
new StarEditor(parent, m_pTableView, index, newOption, m_focusBorderColor);
connect(editor,
&StarEditor::editingFinished,
this,
Expand Down
4 changes: 2 additions & 2 deletions src/library/stareditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ StarEditor::StarEditor(QWidget* parent,
m_pTableView(pTableView),
m_index(index),
m_styleOption(option),
m_pFocusBorderColor(focusBorderColor),
m_focusBorderColor(focusBorderColor),
m_starCount(StarRating::kMinStarCount) {
DEBUG_ASSERT(m_pTableView);
setMouseTracking(true);
Expand Down Expand Up @@ -93,7 +93,7 @@ void StarEditor::paintEvent(QPaintEvent*) {

// Draw a border if the color cell is selected
if (m_styleOption.state & QStyle::State_HasFocus) {
TableItemDelegate::drawBorder(&painter, m_pFocusBorderColor, m_styleOption.rect);
TableItemDelegate::drawBorder(&painter, m_focusBorderColor, m_styleOption.rect);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/library/stareditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class StarEditor : public QWidget {
QTableView* m_pTableView;
QModelIndex m_index;
QStyleOptionViewItem m_styleOption;
QColor m_pFocusBorderColor;
QColor m_focusBorderColor;
StarRating m_starRating;
int m_starCount;
};
26 changes: 24 additions & 2 deletions src/library/tableitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ TableItemDelegate::TableItemDelegate(QTableView* pTableView)
// WTrackTableView {
// qproperty-focusBorderColor: red;
// }
m_pFocusBorderColor = pTrackTableView->getFocusBorderColor();
m_focusBorderColor = pTrackTableView->getFocusBorderColor();
// For some reason the color is not initialized from the stylesheet for
// some WTrackTableViews (in DlgAutoDJ, DlgAnalysis, ...)
// Listen to the property changed signal.
connect(pTrackTableView,
&WTrackTableView::focusBorderColorChanged,
this,
[this](QColor col) {
m_focusBorderColor = col;
});
}
}

Expand All @@ -39,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
2 changes: 1 addition & 1 deletion src/library/tableitemdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ class TableItemDelegate : public QStyledItemDelegate {
// Having this here avoids including QTableView there.
int columnWidth(const QModelIndex &index) const;

QColor m_pFocusBorderColor;
QColor m_focusBorderColor;
QTableView* m_pTableView;
};
7 changes: 3 additions & 4 deletions src/widget/wtracktableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ const ConfigKey kVScrollBarPosConfigKey{
QStringLiteral("[Library]"),
QStringLiteral("VScrollBarPos")};

// Default color for the focus border of TableItemDelegates
const QColor kDefaultFocusBorderColor = Qt::white;

} // anonymous namespace

WTrackTableView::WTrackTableView(QWidget* parent,
Expand All @@ -52,7 +49,9 @@ WTrackTableView::WTrackTableView(QWidget* parent,
m_pConfig(pConfig),
m_pLibrary(pLibrary),
m_backgroundColorOpacity(backgroundColorOpacity),
m_pFocusBorderColor(kDefaultFocusBorderColor),
// 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
23 changes: 20 additions & 3 deletions src/widget/wtracktableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,29 @@ class WTrackTableView : public WLibraryTableView {
return m_backgroundColorOpacity;
}

Q_PROPERTY(QColor focusBorderColor MEMBER m_pFocusBorderColor DESIGNABLE true);
Q_PROPERTY(QColor focusBorderColor
MEMBER m_focusBorderColor
NOTIFY focusBorderColorChanged
DESIGNABLE true);
QColor getFocusBorderColor() const {
return m_pFocusBorderColor;
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 @@ -124,7 +140,8 @@ class WTrackTableView : public WLibraryTableView {
parented_ptr<WTrackMenu> m_pTrackMenu;

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

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

0 comments on commit 2d2d5f3

Please sign in to comment.