Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use trackcolor for whole row #2539

Merged
merged 11 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/library/basesqltablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,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();
}

Expand All @@ -723,6 +725,18 @@ 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(32); // Make this 12.5% opaque
Holzhaus marked this conversation as resolved.
Show resolved Hide resolved
value = QBrush(color);
}
uklotzde marked this conversation as resolved.
Show resolved Hide resolved
break;
}
case Qt::ToolTipRole:
if (column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_COLOR)) {
value = mixxx::RgbColor::toQString(mixxx::RgbColor::fromQVariant(value));
Expand Down Expand Up @@ -1050,7 +1064,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();
Expand Down
2 changes: 2 additions & 0 deletions src/library/coverartdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void CoverArtDelegate::slotCoverFound(const QObject* pRequestor,
void CoverArtDelegate::paintItem(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
paintItemBackground(painter, option, index);

CoverArtCache* pCache = CoverArtCache::instance();
if (pCache == NULL || m_iIdColumn == -1 || m_iCoverSourceColumn == -1 ||
m_iCoverTypeColumn == -1 || m_iCoverLocationColumn == -1 ||
Expand Down
2 changes: 2 additions & 0 deletions src/library/locationdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/library/previewbuttondelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 8 additions & 12 deletions src/library/stardelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* *
***************************************************************************/

#include "library/stardelegate.h"

#include <QPainter>
#include <QtDebug>

#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),
Expand All @@ -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>();
starRating.paint(painter, option.rect);
Expand Down
3 changes: 0 additions & 3 deletions src/library/stardelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions src/library/tableitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ void TableItemDelegate::paint(
int TableItemDelegate::columnWidth(const QModelIndex &index) const {
return m_pTableView->columnWidth(index.column());
}

void 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<QBrush>());
painter->fillRect(option.rect, qvariant_cast<QBrush>(bgValue));
}
}
}
5 changes: 5 additions & 0 deletions src/library/tableitemdelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ class TableItemDelegate : public QStyledItemDelegate {
private:
QTableView* m_pTableView;
};

void paintItemBackground(
Holzhaus marked this conversation as resolved.
Show resolved Hide resolved
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index);