Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 21 additions & 17 deletions src/library/previewbuttondelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ PreviewButtonDelegate::PreviewButtonDelegate(QTableView* parent, int column)
connect(this, SIGNAL(loadTrackToPlayer(TrackPointer, QString, bool)),
parent, SIGNAL(loadTrackToPlayer(TrackPointer, QString, bool)));

// The button needs to be parented to receive the parent styles
m_pButton = make_parented<QPushButton>(parent);
m_pButton->setObjectName("LibraryPreviewButton");
// The button needs to be parented to receive the parent styles.
m_pButton = make_parented<LibraryPreviewButton>(m_pTableView);
m_pButton->setCheckable(true);
m_pButton->setChecked(false);
// We need to hide the button that it is not painted by the QObject tree

// We need to hide the button that it is not painted by the QObject tree
m_pButton->hide();
// Set visible to stop resizing in the background
// which may lead to a crash when already referenced by a painter obeject
// during the render() call.
m_pButton->setAttribute(Qt::WA_WState_Visible);

connect(m_pTableView, SIGNAL(entered(QModelIndex)),
this, SLOT(cellEntered(QModelIndex)));
Expand All @@ -48,8 +44,7 @@ QWidget* PreviewButtonDelegate::createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
Q_UNUSED(option);
QPushButton* btn = new QPushButton(parent);
btn->setObjectName("LibraryPreviewButton");
QPushButton* btn = new LibraryPreviewButton(parent);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use here make_parented

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could, but I have to return a raw pointer from this method to Qt since it takes ownership, so the parented_ptr wouldn't be documenting much.

btn->setCheckable(true);
bool playing = m_pPreviewDeckPlay->toBool();
// Check-state is whether the track is loaded (index.data()) and whether
Expand Down Expand Up @@ -77,8 +72,8 @@ void PreviewButtonDelegate::setModelData(QWidget* editor,
}

void PreviewButtonDelegate::paintItem(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
const QStyleOptionViewItem& option,
const QModelIndex& index) const {
// Let the editor paint in this case
if (index == m_currentEditedCellIndex) {
return;
Expand All @@ -88,15 +83,24 @@ void PreviewButtonDelegate::paintItem(QPainter* painter,
return;
}

m_pButton->setGeometry(option.rect);
bool playing = m_pPreviewDeckPlay->toBool();
// We only need m_pButton to have the right width/height, since we are
// calling its render method directly. Every resize/translate of a widget
// causes Qt to flush the backing store, so we need to avoid this whenever
// possible.
if (option.rect.size() != m_pButton->size()) {
m_pButton->setFixedSize(option.rect.size());
}

// Check-state is whether the track is loaded (index.data()) and whether
// it's playing.
m_pButton->setChecked(index.data().toBool() && playing);
m_pButton->setChecked(index.data().toBool() && m_pPreviewDeckPlay->toBool());

// Render button at the desired position
// Render button at the desired position.
painter->translate(option.rect.topLeft());
m_pButton->render(painter);

// Avoid QWidget::render and call the equivalent of QPushButton::paintEvent
// directly.
m_pButton->paint(painter);
}

void PreviewButtonDelegate::updateEditorGeometry(QWidget* editor,
Expand Down
25 changes: 24 additions & 1 deletion src/library/previewbuttondelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@
#define PREVIEWBUTTONDELEGATE_H

#include <QPushButton>
#include <QStyleOptionButton>

#include "library/tableitemdelegate.h"
#include "track/track.h"
#include "util/parented_ptr.h"

class ControlProxy;

// A QPushButton for rendering the library preview button within the
// PreviewButtonDelegate.
class LibraryPreviewButton : public QPushButton {
Q_OBJECT
public:
LibraryPreviewButton(QWidget* parent=nullptr) : QPushButton(parent) {
setObjectName("LibraryPreviewButton");
}

void paint(QPainter* painter) {
// This matches the implementation of QPushButton::paintEvent, except it
// does not create a new QStylePainter, and it is simpler and more
// direct than QWidget::render(QPainter*, ...).
QStyleOptionButton option;
initStyleOption(&option);
auto pStyle = style();
if (pStyle) {
pStyle->drawControl(QStyle::CE_PushButton, &option, painter, this);
}
}
};

class PreviewButtonDelegate : public TableItemDelegate {
Q_OBJECT

Expand Down Expand Up @@ -43,7 +66,7 @@ class PreviewButtonDelegate : public TableItemDelegate {
QTableView* m_pTableView;
ControlProxy* m_pPreviewDeckPlay;
ControlProxy* m_pCueGotoAndPlay;
parented_ptr<QPushButton> m_pButton;
parented_ptr<LibraryPreviewButton> m_pButton;
bool m_isOneCellInEditMode;
QPersistentModelIndex m_currentEditedCellIndex;
int m_column;
Expand Down
2 changes: 1 addition & 1 deletion src/library/tableitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TableItemDelegate::~TableItemDelegate() {
}

void TableItemDelegate::paint(QPainter* painter,const QStyleOptionViewItem& option,
const QModelIndex& index) const {
const QModelIndex& index) const {

painter->save();

Expand Down