diff --git a/src/library/basesqltablemodel.cpp b/src/library/basesqltablemodel.cpp index bea29d4dd32..a45fa51965b 100644 --- a/src/library/basesqltablemodel.cpp +++ b/src/library/basesqltablemodel.cpp @@ -40,6 +40,9 @@ const int kMaxSortColumns = 3; // Constant for getModelSetting(name) const QString COLUMNS_SORTING = QStringLiteral("ColumnsSorting"); +// Alpha value for row color background (range 0 - 255) +constexpr int kTrackColorRowBackgroundOpacity = 0x20; // 12.5% opacity + } // anonymous namespace BaseSqlTableModel::BaseSqlTableModel(QObject* pParent, @@ -706,10 +709,12 @@ int BaseSqlTableModel::fieldIndex(const QString& fieldName) const { QVariant BaseSqlTableModel::data(const QModelIndex& index, int role) const { //qDebug() << this << "data()"; - if (!index.isValid() || (role != Qt::DisplayRole && - role != Qt::EditRole && - role != Qt::CheckStateRole && - role != Qt::ToolTipRole)) { + if (!index.isValid() || ( + role != Qt::BackgroundRole && + role != Qt::DisplayRole && + role != Qt::EditRole && + role != Qt::CheckStateRole && + role != Qt::ToolTipRole)) { return QVariant(); } @@ -723,6 +728,20 @@ QVariant BaseSqlTableModel::data(const QModelIndex& index, int role) const { // Format the value based on whether we are in a tooltip, display, or edit // role switch (role) { + case Qt::BackgroundRole: { + QModelIndex colorIndex = index.sibling( + index.row(), + fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_COLOR)); + QColor color = mixxx::RgbColor::toQColor( + mixxx::RgbColor::fromQVariant(getBaseValue(colorIndex, role))); + if (color.isValid()) { + color.setAlpha(kTrackColorRowBackgroundOpacity); + value = QBrush(color); + } else { + value = QVariant(); + } + break; + } case Qt::ToolTipRole: if (column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_COLOR)) { value = mixxx::RgbColor::toQString(mixxx::RgbColor::fromQVariant(value)); @@ -1054,7 +1073,8 @@ void BaseSqlTableModel::setTrackValueForColumn(TrackPointer pTrack, int column, QVariant BaseSqlTableModel::getBaseValue( const QModelIndex& index, int role) const { - if (role != Qt::DisplayRole && + if (role != Qt::BackgroundRole && + role != Qt::DisplayRole && role != Qt::ToolTipRole && role != Qt::EditRole) { return QVariant(); diff --git a/src/library/coverartdelegate.cpp b/src/library/coverartdelegate.cpp index 6dd45b44062..732624df6d4 100644 --- a/src/library/coverartdelegate.cpp +++ b/src/library/coverartdelegate.cpp @@ -108,6 +108,8 @@ void CoverArtDelegate::slotCoverFound( void CoverArtDelegate::paintItem(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { + paintItemBackground(painter, option, index); + if (m_iIdColumn < 0 || m_iCoverSourceColumn == -1 || m_iCoverTypeColumn == -1 || diff --git a/src/library/locationdelegate.cpp b/src/library/locationdelegate.cpp index e54c2698d70..bb197bdadcf 100644 --- a/src/library/locationdelegate.cpp +++ b/src/library/locationdelegate.cpp @@ -11,6 +11,8 @@ void LocationDelegate::paintItem( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + paintItemBackground(painter, option, index); + QString elidedText = option.fontMetrics.elidedText( index.data().toString(), Qt::ElideLeft, diff --git a/src/library/previewbuttondelegate.cpp b/src/library/previewbuttondelegate.cpp index 21fb8352e2b..4584dd30090 100644 --- a/src/library/previewbuttondelegate.cpp +++ b/src/library/previewbuttondelegate.cpp @@ -85,6 +85,8 @@ void PreviewButtonDelegate::setModelData(QWidget* editor, void PreviewButtonDelegate::paintItem(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + paintItemBackground(painter, option, index); + // Let the editor paint in this case if (index == m_currentEditedCellIndex) { return; diff --git a/src/library/stardelegate.cpp b/src/library/stardelegate.cpp index cbfa4f081b6..125a058076b 100644 --- a/src/library/stardelegate.cpp +++ b/src/library/stardelegate.cpp @@ -15,13 +15,14 @@ * * ***************************************************************************/ +#include "library/stardelegate.h" +#include #include -#include "library/tableitemdelegate.h" -#include "library/stardelegate.h" #include "library/stareditor.h" #include "library/starrating.h" +#include "library/tableitemdelegate.h" StarDelegate::StarDelegate(QTableView* pTableView) : TableItemDelegate(pTableView), @@ -30,21 +31,16 @@ StarDelegate::StarDelegate(QTableView* pTableView) connect(pTableView, &QTableView::entered, this, &StarDelegate::cellEntered); } -void StarDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, - const QModelIndex& index) const { +void StarDelegate::paintItem( + QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) const { // let the editor do the painting if this cell is currently being edited if (index == m_currentEditedCellIndex) { return; } - TableItemDelegate::paint(painter, option, index); -} -void StarDelegate::paintItem(QPainter* painter, const QStyleOptionViewItem& option, - const QModelIndex& index) const { - // let the editor do the painting if this cell is currently being edited - if (index == m_currentEditedCellIndex) { - return; - } + paintItemBackground(painter, option, index); StarRating starRating = index.data().value(); starRating.paint(painter, option.rect); diff --git a/src/library/stardelegate.h b/src/library/stardelegate.h index 2b35fc61bbd..999c65e2ffc 100644 --- a/src/library/stardelegate.h +++ b/src/library/stardelegate.h @@ -28,9 +28,6 @@ class StarDelegate : public TableItemDelegate { // reimplemented from QItemDelegate and is called whenever the view needs to // repaint an item - void paint(QPainter* painter, const QStyleOptionViewItem& option, - const QModelIndex& index) const; - void paintItem(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; diff --git a/src/library/tableitemdelegate.cpp b/src/library/tableitemdelegate.cpp index 2bae8d1ced7..617b805d018 100644 --- a/src/library/tableitemdelegate.cpp +++ b/src/library/tableitemdelegate.cpp @@ -54,3 +54,18 @@ void TableItemDelegate::paint( int TableItemDelegate::columnWidth(const QModelIndex &index) const { return m_pTableView->columnWidth(index.column()); } + +void TableItemDelegate::paintItemBackground( + QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) { + // If the row is not selected, paint the desired background color before + // painting the delegate item + if (!option.showDecorationSelected || !(option.state & QStyle::State_Selected)) { + QVariant bgValue = index.data(Qt::BackgroundRole); + if (bgValue.isValid()) { + DEBUG_ASSERT(bgValue.canConvert()); + painter->fillRect(option.rect, qvariant_cast(bgValue)); + } + } +} diff --git a/src/library/tableitemdelegate.h b/src/library/tableitemdelegate.h index 11779659961..01f6fd23356 100644 --- a/src/library/tableitemdelegate.h +++ b/src/library/tableitemdelegate.h @@ -21,6 +21,11 @@ class TableItemDelegate : public QStyledItemDelegate { const QModelIndex &index) const = 0; protected: + static void paintItemBackground( + QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index); + int columnWidth(const QModelIndex &index) const; private: