Commit 522c2f1d authored by Anthony Léonard's avatar Anthony Léonard Committed by Olivier SOLDANO

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: default avatarOlivier Soldano <olivier.soldano@savoirfairelinux.com>
parent 2d99df02
......@@ -42,70 +42,66 @@ SmartListDelegate::paint(QPainter* painter
, const QModelIndex& index
) const
{
painter->setRenderHint(QPainter::Antialiasing);
QStyleOptionViewItem opt(option);
painter->setRenderHint(QPainter::Antialiasing);
opt.decorationSize = QSize(sizeImage_, sizeImage_);
opt.decorationPosition = QStyleOptionViewItem::Left;
opt.decorationAlignment = Qt::AlignCenter;
// 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;
rect.setLeft(0);
QRect &rect = opt.rect;
// Avatar drawing
opt.decorationSize = QSize(sizeImage_, sizeImage_);
opt.decorationPosition = QStyleOptionViewItem::Left;
opt.decorationAlignment = Qt::AlignCenter;
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 @@ SmartListDelegate::paint(QPainter* painter
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 @@ SmartListDelegate::paint(QPainter* painter
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);
......
......@@ -28,7 +28,6 @@ class SmartListDelegate : public QItemDelegate
Q_OBJECT
public:
explicit SmartListDelegate(QObject* parent = 0);
inline void setRowHighlighted(int i){rowHighlighted_ = i;};
protected:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
......@@ -40,10 +39,5 @@ private:
constexpr static int dy_ = 6;
constexpr static int dx_ = 12;
constexpr static int fontSize_ = 10;
constexpr static int pinSize_ = 12;
constexpr static int effectiveComBarSize_ = 48;
int rowHighlighted_ = -1;
signals:
void rowSelected(const QRect& rect) const;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment