Skip to content

Commit

Permalink
Merge pull request #2539 from Holzhaus/use-trackcolor-for-whole-row
Browse files Browse the repository at this point in the history
Use trackcolor for whole row
  • Loading branch information
uklotzde authored Mar 10, 2020
2 parents 799b878 + 5e966ee commit 09ab7a2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
30 changes: 25 additions & 5 deletions src/library/basesqltablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}

Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/library/coverartdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
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 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<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 @@ -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:
Expand Down

0 comments on commit 09ab7a2

Please sign in to comment.