From 7130e674c4d105d3ddefa064ffbf0eb2ef630d20 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 5 Jan 2022 22:29:34 +0300 Subject: [PATCH] some warnings fixed, new way of drawing avatars in message line --- core/account.cpp | 5 +- core/adapterFuctions.cpp | 2 +- core/conference.cpp | 2 +- ui/models/accounts.cpp | 5 +- ui/widgets/messageline/feedview.cpp | 56 ++++++++++++++++++++-- ui/widgets/messageline/feedview.h | 1 + ui/widgets/messageline/messagedelegate.cpp | 10 ++-- 7 files changed, 65 insertions(+), 16 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 035299b..a923690 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -601,6 +601,9 @@ void Core::Account::onClientError(QXmppClient::Error err) errorText = "Policy violation"; break; #endif + default: + errorText = "Unknown Error"; + break; } errorType = "Client stream error"; @@ -837,7 +840,6 @@ void Core::Account::uploadVCard(const Shared::VCard& card) QXmppVCardIq iq; initializeQXmppVCard(iq, card); - bool avatarChanged = false; if (card.getAvatarType() != Shared::Avatar::empty) { QString newPath = card.getAvatarPath(); QString oldPath = getAvatarPath(); @@ -859,7 +861,6 @@ void Core::Account::uploadVCard(const Shared::VCard& card) } } else { data = avatar.readAll(); - avatarChanged = true; } } else { if (avatarType.size() > 0) { diff --git a/core/adapterFuctions.cpp b/core/adapterFuctions.cpp index e2559d8..3d84dfb 100644 --- a/core/adapterFuctions.cpp +++ b/core/adapterFuctions.cpp @@ -264,7 +264,7 @@ void Core::initializeQXmppVCard(QXmppVCardIq& iq, const Shared::VCard& card) { phone.setType(phone.type() | QXmppVCardPhone::Preferred); } } - for (const std::pair& phone : phones) { + for (const std::pair& phone : phones) { phs.push_back(phone.second); } diff --git a/core/conference.cpp b/core/conference.cpp index cda19fd..55280e2 100644 --- a/core/conference.cpp +++ b/core/conference.cpp @@ -356,7 +356,7 @@ Shared::VCard Core::Conference::handleResponseVCard(const QXmppVCardIq& card, co QMap Core::Conference::getAllAvatars() const { QMap result; - for (const std::pair& pair : exParticipants) { + for (const std::pair& pair : exParticipants) { result.insert(pair.first, avatarPath(pair.first) + "." + pair.second.type); } return result; diff --git a/ui/models/accounts.cpp b/ui/models/accounts.cpp index f5ffce8..4343481 100644 --- a/ui/models/accounts.cpp +++ b/ui/models/accounts.cpp @@ -97,7 +97,10 @@ void Models::Accounts::addAccount(Account* account) void Models::Accounts::onAccountChanged(Item* item, int row, int col) { - if (row < accs.size()) { + if (row < 0) { + return; + } + if (static_cast::size_type>(row) < accs.size()) { Account* acc = getAccount(row); if (item != acc) { return; //it means the signal is emitted by one of accounts' children, not exactly him, this model has no interest in that diff --git a/ui/widgets/messageline/feedview.cpp b/ui/widgets/messageline/feedview.cpp index 7bdfb9e..618ecfb 100644 --- a/ui/widgets/messageline/feedview.cpp +++ b/ui/widgets/messageline/feedview.cpp @@ -31,6 +31,10 @@ constexpr int approximateSingleMessageHeight = 20; constexpr int progressSize = 70; constexpr int dateDeviderMargin = 10; +constexpr int avatarHeight = 50; +constexpr int margin = 6; +constexpr int halfMargin = 3; + const std::set FeedView::geometryChangingRoles = { Models::MessageFeed::Attach, Models::MessageFeed::Text, @@ -334,6 +338,20 @@ void FeedView::paintEvent(QPaintEvent* event) drawDateDevider(option.rect.bottom(), lastDate, painter); } lastDate = currentDate; + + + if ((option.rect.y() < 1) || (index.row() == m->rowCount() - 1)) { + drawAvatar(index, option, painter); + } else { + QString mySender = index.data(Models::MessageFeed::Sender).toString(); + QModelIndex prevIndex = m->index(index.row() + 1, 0, rootIndex()); + if ( + (prevIndex.data(Models::MessageFeed::Sender).toString() != mySender) || + (prevIndex.data(Models::MessageFeed::Date).toDateTime().daysTo(currentDate) != 0) + ) { + drawAvatar(index, option, painter); + } + } } if (!lastDate.isNull() && inZone) { //if after drawing all messages there is still space drawDateDevider(option.rect.bottom(), lastDate, painter); @@ -344,10 +362,6 @@ void FeedView::paintEvent(QPaintEvent* event) del->endClearWidgets(); clearWidgetsMode = false; } - - if (event->rect().height() == vp->height()) { - // draw the blurred drop shadow... - } } void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter) @@ -360,6 +374,40 @@ void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter painter.restore(); } +void FeedView::drawAvatar(const QModelIndex& index, const QStyleOptionViewItem& option, QPainter& painter) +{ + int currentRow = index.row(); + int y = option.rect.y(); + bool firstAttempt = true; + QString mySender = index.data(Models::MessageFeed::Sender).toString(); + QDateTime currentDate = index.data(Models::MessageFeed::Date).toDateTime(); + QIcon icon(index.data(Models::MessageFeed::Avatar).toString()); + while (y < 0 && currentRow > 0) { + QRect rect; + if (firstAttempt) { + firstAttempt = false; + rect = option.rect; + } else { + QModelIndex ci = model()->index(currentRow, 0, rootIndex()); + if ( + (ci.data(Models::MessageFeed::Sender).toString() != mySender) || + (ci.data(Models::MessageFeed::Date).toDateTime().daysTo(currentDate) != 0) + ) { + break; + } + rect = visualRect(ci); + } + y = std::min(0, rect.bottom() - margin - avatarHeight); + --currentRow; + } + if (index.data(Models::MessageFeed::SentByMe).toBool()) { + painter.drawPixmap(option.rect.width() - avatarHeight - margin, y + halfMargin, icon.pixmap(avatarHeight, avatarHeight)); + } else { + painter.drawPixmap(margin, y + halfMargin, icon.pixmap(avatarHeight, avatarHeight)); + } +} + + void FeedView::verticalScrollbarValueChanged(int value) { vo = verticalScrollBar()->maximum() - value; diff --git a/ui/widgets/messageline/feedview.h b/ui/widgets/messageline/feedview.h index 5e08946..e3e57b7 100644 --- a/ui/widgets/messageline/feedview.h +++ b/ui/widgets/messageline/feedview.h @@ -74,6 +74,7 @@ private: bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight); void positionProgress(); void drawDateDevider(int top, const QDateTime& date, QPainter& painter); + void drawAvatar(const QModelIndex& index, const QStyleOptionViewItem& option, QPainter& painter); private: struct Hint { diff --git a/ui/widgets/messageline/messagedelegate.cpp b/ui/widgets/messageline/messagedelegate.cpp index 649230e..0fe1ed0 100644 --- a/ui/widgets/messageline/messagedelegate.cpp +++ b/ui/widgets/messageline/messagedelegate.cpp @@ -104,13 +104,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio painter->fillRect(option.rect, option.palette.brush(QPalette::Inactive, QPalette::Highlight)); } - QIcon icon(data.avatar); - - if (data.sentByMe) { - painter->drawPixmap(option.rect.width() - avatarHeight - margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight)); - } else { - painter->drawPixmap(margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight)); - } + QStyleOptionViewItem opt = option; QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2); @@ -163,6 +157,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio break; //but it's a possible performance problem case Models::uploading: paintPreview(data, painter, opt); + [[fallthrough]]; case Models::downloading: paintBar(getBar(data), painter, data.sentByMe, opt); break; @@ -268,6 +263,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel break; case Models::uploading: messageSize.rheight() += Preview::calculateAttachSize(attach.localPath, messageRect).height() + textMargin; + [[fallthrough]]; case Models::downloading: messageSize.rheight() += barHeight + textMargin; break;