refactoring of SmartListDelegate
Comments are added to improve readability of the item drawing code as
it is not always self-explanatory.
Furthermore, some lines are moved or deleted for various reasons:
* Some lines were repeated or not doing any operations.
* Objects/structures were initialised at one place but used much
later in code which lead to poor legibility.
* Dead code, variables and constants were present.
Change-Id: I833769cc594033d88a70ce192ae002ac091a37a5
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
diff --git a/smartlistdelegate.cpp b/smartlistdelegate.cpp
index fa4418e..e9411ed 100644
--- a/smartlistdelegate.cpp
+++ b/smartlistdelegate.cpp
@@ -42,70 +42,66 @@
, const QModelIndex& index
) const
{
- painter->setRenderHint(QPainter::Antialiasing);
-
QStyleOptionViewItem opt(option);
painter->setRenderHint(QPainter::Antialiasing);
+ // Not having focus removes dotted lines around the item
+ if (opt.state & QStyle::State_HasFocus)
+ opt.state ^= QStyle::State_HasFocus;
+
+ // First, we draw the control itself
+ QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
+ style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
+
+ QRect &rect = opt.rect;
+
+ // Avatar drawing
opt.decorationSize = QSize(sizeImage_, sizeImage_);
opt.decorationPosition = QStyleOptionViewItem::Left;
opt.decorationAlignment = Qt::AlignCenter;
- if (opt.state & QStyle::State_HasFocus)
- opt.state ^= QStyle::State_HasFocus;
-
- QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
- style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
-
- QRect rect = opt.rect;
- rect.setLeft(0);
-
- QRect rectTexts(16 + rect.left() + dx_ + sizeImage_,
- rect.top(),
- rect.width(),
- rect.height() / 2);
QRect rectAvatar(16 + rect.left(), rect.top() + dy_, sizeImage_, sizeImage_);
drawDecoration(painter, opt, rectAvatar,
QPixmap::fromImage(index.data(Qt::DecorationRole).value<QImage>()));
QFont font(painter->font());
+ // If there's unread messages, a message count is displayed
if (auto messageCount = index.data(static_cast<int>(Ring::Role::UnreadTextMessageCount)).toInt()) {
-
font.setPointSize(8);
QFontMetrics textFontMetrics(font);
- QString messageCountText = QString::number(messageCount);
- QRect pastilleRect;
- QRect(rectAvatar.right() - 7, rectAvatar.bottom() - 7, pinSize_, pinSize_);
- pastilleRect = textFontMetrics.boundingRect(QRect(rectAvatar.left() + sizeImage_/3,
- rectAvatar.bottom() - 6, sizeImage_, 0),
- Qt::AlignCenter, messageCountText);
+ // If there is more than 10 unread messages, "10+" is displayed
+ QString messageCountText = (messageCount > 10)? "10+" : QString::number(messageCount);
- painter->setOpacity(0.9);
- QRect bubbleRect(pastilleRect.left(), pastilleRect.top(),
- pastilleRect.width() + 3, pastilleRect.height());
+ // This QRect is the bounding one containing the message count to be displayed
+ QRect pastilleRect = textFontMetrics.boundingRect(QRect(rectAvatar.left() + sizeImage_/3,
+ rectAvatar.bottom() - textFontMetrics.height(), sizeImage_, textFontMetrics.height()),
+ Qt::AlignCenter, messageCountText);
+ // This one is larger than pastilleRect and is used to prepare the red background
+ QRect bubbleRect(pastilleRect.left() - 3, pastilleRect.top(),
+ pastilleRect.width() + 6, pastilleRect.height() + 1);
+
+ // The background is displayed
QPainterPath path;
path.addRoundedRect(bubbleRect, 3, 3);
QPen pen(RingTheme::red_, 5);
+ painter->setOpacity(0.9);
painter->setPen(pen);
painter->fillPath(path, RingTheme::red_);
- painter->drawPath(path);
+ // Then the message count
painter->setPen(Qt::white);
painter->setOpacity(1);
painter->setFont(font);
- painter->drawText(bubbleRect, Qt::AlignCenter, messageCountText);
+ painter->drawText(pastilleRect, Qt::AlignCenter, messageCountText);
}
- font.setPointSize(fontSize_);
+ font.setPointSize(fontSize_);
QPen pen(painter->pen());
- const int currentRow = index.row();
- if (currentRow == rowHighlighted_)
- emit rowSelected(opt.rect);
-
+ // A lightGrey separator is painted at the bottom if the current item is not selected
if (not (opt.state & QStyle::State_Selected)) {
pen.setColor(RingTheme::lightGrey_);
painter->setPen(pen);
@@ -113,10 +109,17 @@
rect.right() - 20,
rect.bottom());
}
+
if (index.column() == 0)
{
painter->setPen(pen);
+ QRect rectTexts(16 + rect.left() + dx_ + sizeImage_,
+ rect.top(),
+ rect.width(),
+ rect.height() / 2);
+
+ // The name is displayed at the avatar's left
QVariant name = index.data(static_cast<int>(Ring::Role::Name));
if (name.isValid())
{
@@ -130,6 +133,7 @@
painter->drawText(rectTexts, Qt::AlignBottom | Qt::AlignLeft, nameStr);
}
+ // Finally, either last interaction date or call state is displayed
QVariant state = index.data(static_cast<int>(Ring::Role::FormattedState));
pen.setColor(RingTheme::grey_);
painter->setPen(pen);