Skip to content

Commit

Permalink
Move isHover to drawBackground/drawForeground, at same, remove drawHo…
Browse files Browse the repository at this point in the history
…ver interface.

Draw hover code should implement in drawBackground/drawForeground interfaces by application author.

Change-Id: If6fa48fc227ecbe426177e92922d455268c6d748
  • Loading branch information
manateelazycat committed Feb 28, 2018
1 parent a2452c1 commit 4e6a803
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 39 deletions.
9 changes: 2 additions & 7 deletions examples/dwidget-examples/collections/multilistitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool MultiListItem::sameAs(DSimpleListItem *item)
return artist == (static_cast<MultiListItem*>(item))->artist && song == (static_cast<MultiListItem*>(item))->song && length == (static_cast<MultiListItem*>(item))->length;
}

void MultiListItem::drawBackground(QRect rect, QPainter *painter, int index, bool isSelect)
void MultiListItem::drawBackground(QRect rect, QPainter *painter, int index, bool isSelect, bool isHover)
{
QPainterPath path;
path.addRect(QRectF(rect));
Expand All @@ -45,7 +45,7 @@ void MultiListItem::drawBackground(QRect rect, QPainter *painter, int index, boo
}
}

void MultiListItem::drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect)
void MultiListItem::drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect, bool isHover)
{
int padding = 10;
painter->setOpacity(1);
Expand Down Expand Up @@ -94,8 +94,3 @@ bool MultiListItem::sortByLength(const DSimpleListItem *item1, const DSimpleList

return descendingSort ? sortOrder : !sortOrder;
}

void MultiListItem::drawHover(QRect rect, QPainter *painter)
{

}
5 changes: 2 additions & 3 deletions examples/dwidget-examples/collections/multilistitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class MultiListItem : public DSimpleListItem
MultiListItem(QString artistName, QString songName, QString songLength);

bool sameAs(DSimpleListItem *item);
void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect);
void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect);
void drawHover(QRect rect, QPainter *painter);
void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect, bool isHover);
void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect, bool isHover);

static bool sortByArtist(const DSimpleListItem *item1, const DSimpleListItem *item2, bool descendingSort);
static bool sortBySong(const DSimpleListItem *item1, const DSimpleListItem *item2, bool descendingSort);
Expand Down
8 changes: 2 additions & 6 deletions examples/dwidget-examples/collections/singlelistitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool SingleListItem::sameAs(DSimpleListItem *item)
return name == (static_cast<SingleListItem*>(item))->name;
}

void SingleListItem::drawBackground(QRect rect, QPainter *painter, int index, bool isSelect)
void SingleListItem::drawBackground(QRect rect, QPainter *painter, int index, bool isSelect, bool isHover)
{
QPainterPath path;
path.addRect(QRectF(rect));
Expand All @@ -43,7 +43,7 @@ void SingleListItem::drawBackground(QRect rect, QPainter *painter, int index, bo
}
}

void SingleListItem::drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect)
void SingleListItem::drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect, bool isHover)
{
painter->setOpacity(1);
if (isSelect) {
Expand All @@ -56,7 +56,3 @@ void SingleListItem::drawForeground(QRect rect, QPainter *painter, int column, i
painter->drawText(QRect(rect.x() + padding, rect.y(), rect.width() - padding * 2, rect.height()), Qt::AlignLeft | Qt::AlignVCenter, name);
}

void SingleListItem::drawHover(QRect rect, QPainter *painter)
{

}
5 changes: 2 additions & 3 deletions examples/dwidget-examples/collections/singlelistitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class SingleListItem : public DSimpleListItem
SingleListItem(QString itemName);

bool sameAs(DSimpleListItem *item);
void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect);
void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect);
void drawHover(QRect rect, QPainter *painter);
void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect, bool isHover);
void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect, bool isHover);

QString name;
};
Expand Down
15 changes: 4 additions & 11 deletions src/widgets/dsimplelistitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class LIBDTKWIDGETSHARED_EXPORT DSimpleListItem : public QObject
* @painter the painter used to draw anything you want
* @index the index of DSimpleListItem, you can draw different rows effect based on the index, such as the zebra crossing
* @isSelect current item is selected, you can draw selected effect under content when isSelect is true
* @isHover current item is hovered, you can draw hover effect under content when isHover is true
*/
virtual void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect)=0;
virtual void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect, bool isHover)=0;

/*
* The interface function that used to draw foreground of DSimpleListItem.
Expand All @@ -65,17 +66,9 @@ class LIBDTKWIDGETSHARED_EXPORT DSimpleListItem : public QObject
* @column the column of DSimpleListItem, you can draw different column content based on the column index
* @index the index of DSimpleListItem, you can draw different rows effect based on the index, such as the zebra crossing
* @isSelect current item is selected, you can draw selected effect under content when isSelect is true
* @isHover current item is hovered, you can draw hover effect under content when isHover is true
*/
virtual void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect)=0;


/*
* The interface function that used to draw hover effect.
*
* @rect column corresponding to the drawing of the rectangular area
* @painter the painter used to draw anything you want
*/
virtual void drawHover(QRect rect, QPainter *painter)=0;
virtual void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect, bool isHover)=0;
};

DWIDGET_END_NAMESPACE
Expand Down
21 changes: 13 additions & 8 deletions src/widgets/dsimplelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,22 +1139,27 @@ void DSimpleListView::paintEvent(QPaintEvent *)

// Draw item backround.
bool isSelect = d->selectionItems->contains(item);
bool isHover = d->drawHover && d->drawHoverItem != NULL && item->sameAs(d->drawHoverItem);
painter.save();
item->drawBackground(QRect(0, renderY + rowCounter * d->rowHeight - d->renderOffset, rect().width(), d->rowHeight), &painter, rowCounter, isSelect);
item->drawBackground(QRect(0, renderY + rowCounter * d->rowHeight - d->renderOffset, rect().width(), d->rowHeight),
&painter,
rowCounter,
isSelect,
isHover);
painter.restore();

// Draw hover effect.
if (d->drawHover && d->drawHoverItem != NULL && item->sameAs(d->drawHoverItem)) {
item->drawHover(QRect(0, renderY + rowCounter * d->rowHeight - d->renderOffset, rect().width(), d->rowHeight), &painter);
}


// Draw item foreground.
int columnCounter = 0;
int columnRenderX = 0;
for (int renderWidth:renderWidths) {
if (renderWidth > 0) {
painter.save();
item->drawForeground(QRect(columnRenderX, renderY + rowCounter * d->rowHeight - d->renderOffset, renderWidth, d->rowHeight), &painter, columnCounter, rowCounter, isSelect);
item->drawForeground(QRect(columnRenderX, renderY + rowCounter * d->rowHeight - d->renderOffset, renderWidth, d->rowHeight),
&painter,
columnCounter,
rowCounter,
isSelect,
isHover);
painter.restore();

columnRenderX += renderWidth;
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dsimplelistview.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class LIBDTKWIDGETSHARED_EXPORT DSimpleListView : public QWidget, public DTK_COR
/*
* Set whether display hover effect.
*
* @enableHoverEffect draw hover effect by call item's drawHover interface, default is false.
* @enableHoverEffect draw hover effect by call item's drawBackground/drawForeground interface, default is false.
*/
void setHoverEffect(bool enableHoverEffect);

Expand Down

0 comments on commit 4e6a803

Please sign in to comment.