1
0
forked from blue/squawk

self nick in the chat fix, hovering message feature

This commit is contained in:
Blue 2021-01-08 00:50:12 +03:00
parent 270a32db9e
commit 15342f3c53
7 changed files with 130 additions and 30 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.4)
project(squawk) project(squawk)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)

View File

@ -29,7 +29,9 @@ const QHash<int, QByteArray> Models::MessageFeed::roles = {
{DeliveryState, "deliveryState"}, {DeliveryState, "deliveryState"},
{Correction, "correction"}, {Correction, "correction"},
{SentByMe,"sentByMe"}, {SentByMe,"sentByMe"},
{Avatar, "avatar"} {Avatar, "avatar"},
{Attach, "attach"},
{Bulk, "bulk"}
}; };
Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent): Models::MessageFeed::MessageFeed(const Element* ri, QObject* parent):
@ -94,15 +96,11 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
answer = msg->getBody(); answer = msg->getBody();
break; break;
case Sender: case Sender:
if (rosterItem->isRoom()) { if (sentByMe(*msg)) {
if (sentByMe(*msg)) { answer = rosterItem->getAccountName();
answer = rosterItem->getDisplayedName();
} else {
answer = msg->getFromResource();
}
} else { } else {
if (sentByMe(*msg)) { if (rosterItem->isRoom()) {
answer = rosterItem->getAccountName(); answer = msg->getFromResource();
} else { } else {
answer = rosterItem->getDisplayedName(); answer = rosterItem->getDisplayedName();
} }
@ -139,7 +137,38 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
answer = path; answer = path;
} }
} }
case Attach:
break; break;
case Bulk: {
FeedItem item;
item.sentByMe = sentByMe(*msg);
item.date = msg->getTime();
item.state = msg->getState();
item.correction = msg->getEdited();
item.text = msg->getBody();
item.avatar.clear();
if (item.sentByMe) {
item.sender = rosterItem->getAccountName();
item.avatar = rosterItem->getAccountAvatarPath();
} else {
if (rosterItem->isRoom()) {
item.sender = msg->getFromResource();
const Room* room = static_cast<const Room*>(rosterItem);
item.avatar = room->getParticipantIconPath(msg->getFromResource());
} else {
item.sender = rosterItem->getDisplayedName();
if (rosterItem->getAvatarState() != Shared::Avatar::empty) {
item.avatar = rosterItem->getAvatarPath();
}
}
}
if (item.avatar.size() == 0) {
item.avatar = Shared::iconPath("user", true);
}
answer.setValue(item);
}
default: default:
break; break;
} }

View File

@ -72,7 +72,17 @@ public:
DeliveryState, DeliveryState,
Correction, Correction,
SentByMe, SentByMe,
Avatar Avatar,
Attach,
Bulk
};
enum Attachment {
none,
remote,
downloading,
uploading,
ready
}; };
private: private:
enum SyncState { enum SyncState {
@ -80,6 +90,12 @@ private:
syncing, syncing,
complete complete
}; };
struct Attach {
Attachment state;
qreal progress;
QString localPath;
};
//tags //tags
struct id {}; struct id {};
struct time {}; struct time {};
@ -118,6 +134,19 @@ private:
static const QHash<int, QByteArray> roles; static const QHash<int, QByteArray> roles;
}; };
struct FeedItem {
QString text;
QString sender;
QString avatar;
bool sentByMe;
bool correction;
QDateTime date;
Shared::Message::State state;
MessageFeed::Attachment attach;
};
}; };
Q_DECLARE_METATYPE(Models::FeedItem);
#endif // MESSAGEFEED_H #endif // MESSAGEFEED_H

View File

@ -33,6 +33,9 @@ FeedView::FeedView(QWidget* parent):
{ {
horizontalScrollBar()->setRange(0, 0); horizontalScrollBar()->setRange(0, 0);
verticalScrollBar()->setSingleStep(approximateSingleMessageHeight); verticalScrollBar()->setSingleStep(approximateSingleMessageHeight);
setMouseTracking(true);
setSelectionBehavior(SelectItems);
// viewport()->setAttribute(Qt::WA_Hover, true);
} }
FeedView::~FeedView() FeedView::~FeedView()
@ -41,14 +44,12 @@ FeedView::~FeedView()
QModelIndex FeedView::indexAt(const QPoint& point) const QModelIndex FeedView::indexAt(const QPoint& point) const
{ {
int32_t totalHeight = viewport()->height() + vo; int32_t vh = viewport()->height();
if (point.y() <= totalHeight) { //if it's bigger - someone wants to know the index below the feed beginning, it's invalid uint32_t y = vh - point.y() + vo;
uint32_t y = totalHeight - point.y();
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) { if (hints[i].offset >= y) {
if (y > hints[i].offset) { return model()->index(i - 1, 0, rootIndex());
return model()->index(i - 1, 0, rootIndex());
}
} }
} }
@ -219,9 +220,11 @@ void FeedView::paintEvent(QPaintEvent* event)
QPainter painter(vp); QPainter painter(vp);
QStyleOptionViewItem option = viewOptions(); QStyleOptionViewItem option = viewOptions();
option.features = QStyleOptionViewItem::WrapText; option.features = QStyleOptionViewItem::WrapText;
QPoint cursor = vp->mapFromGlobal(QCursor::pos());
for (const QModelIndex& index : toRener) { for (const QModelIndex& index : toRener) {
option.rect = visualRect(index); option.rect = visualRect(index);
option.state.setFlag(QStyle::State_MouseOver, option.rect.contains(cursor));
itemDelegate(index)->paint(&painter, option, index); itemDelegate(index)->paint(&painter, option, index);
} }
} }
@ -233,6 +236,16 @@ void FeedView::verticalScrollbarValueChanged(int value)
QAbstractItemView::verticalScrollbarValueChanged(vo); QAbstractItemView::verticalScrollbarValueChanged(vo);
} }
void FeedView::mouseMoveEvent(QMouseEvent* event)
{
if (!isVisible()) {
return;
}
QAbstractItemView::mouseMoveEvent(event);
}
QFont FeedView::getFont() const QFont FeedView::getFont() const
{ {
return viewOptions().font; return viewOptions().font;

View File

@ -45,6 +45,8 @@ public:
QFont getFont() const; QFont getFont() const;
public slots:
protected slots: protected slots:
void rowsInserted(const QModelIndex & parent, int start, int end) override; void rowsInserted(const QModelIndex & parent, int start, int end) override;
void verticalScrollbarValueChanged(int value) override; void verticalScrollbarValueChanged(int value) override;
@ -54,6 +56,7 @@ protected:
int horizontalOffset() const override; int horizontalOffset() const override;
void paintEvent(QPaintEvent * event) override; void paintEvent(QPaintEvent * event) override;
void updateGeometries() override; void updateGeometries() override;
void mouseMoveEvent(QMouseEvent * event) override;
private: private:
bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight); bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight);

View File

@ -16,8 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QDebug>
#include <QPainter> #include <QPainter>
#include <QApplication> #include <QApplication>
#include "messagedelegate.h" #include "messagedelegate.h"
#include "ui/models/messagefeed.h" #include "ui/models/messagefeed.h"
@ -41,16 +43,21 @@ MessageDelegate::~MessageDelegate()
void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{ {
bool sentByMe = false; QVariant vi = index.data(Models::MessageFeed::Bulk);
QVariant sbm = index.data(Models::MessageFeed::SentByMe); if (!vi.isValid()) {
if (sbm.isValid()) { return;
sentByMe = sbm.toBool();
} }
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
painter->save(); painter->save();
painter->setRenderHint(QPainter::Antialiasing, true); painter->setRenderHint(QPainter::Antialiasing, true);
QIcon icon(index.data(Models::MessageFeed::Avatar).toString());
if (sentByMe) { if (option.state & QStyle::State_MouseOver) {
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)); painter->drawPixmap(option.rect.width() - avatarHeight - margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
} else { } else {
painter->drawPixmap(margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight)); painter->drawPixmap(margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
@ -58,7 +65,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2); QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2);
if (!sentByMe) { if (!data.sentByMe) {
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop; opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0); messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0);
} else { } else {
@ -66,27 +73,39 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
} }
opt.rect = messageRect; opt.rect = messageRect;
QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, data.text).size();
messageSize.rheight() += nickMetrics.lineSpacing();
messageSize.rheight() += dateMetrics.height();
if (messageSize.width() < opt.rect.width()) {
QSize senderSize = nickMetrics.boundingRect(messageRect, 0, data.sender).size();
if (senderSize.width() > messageSize.width()) {
messageSize.setWidth(senderSize.width());
}
} else {
messageSize.setWidth(opt.rect.width());
}
QRect rect; QRect rect;
painter->setFont(nickFont); painter->setFont(nickFont);
painter->drawText(opt.rect, opt.displayAlignment, index.data(Models::MessageFeed::Sender).toString(), &rect); painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect);
opt.rect.adjust(0, rect.height(), 0, 0); opt.rect.adjust(0, rect.height(), 0, 0);
painter->setFont(bodyFont); painter->setFont(bodyFont);
painter->drawText(opt.rect, opt.displayAlignment | Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString(), &rect); painter->drawText(opt.rect, opt.displayAlignment | Qt::TextWordWrap, data.text, &rect);
opt.rect.adjust(0, rect.height(), 0, 0); opt.rect.adjust(0, rect.height(), 0, 0);
painter->setFont(dateFont); painter->setFont(dateFont);
QColor q = painter->pen().color(); QColor q = painter->pen().color();
q.setAlpha(180); q.setAlpha(180);
painter->setPen(q); painter->setPen(q);
painter->drawText(opt.rect, opt.displayAlignment, index.data(Models::MessageFeed::Date).toDateTime().toLocalTime().toString(), &rect); painter->drawText(opt.rect, opt.displayAlignment, data.date.toLocalTime().toString(), &rect);
painter->restore(); painter->restore();
} }
QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{ {
QRect messageRect = option.rect.adjusted(0, margin / 2, -(avatarHeight + 3 * margin), -margin); QRect messageRect = option.rect.adjusted(0, margin / 2, -(avatarHeight + 3 * margin), -margin / 2);
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
opt.rect = messageRect; opt.rect = messageRect;
QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString()).size(); QSize messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, index.data(Models::MessageFeed::Text).toString()).size();
@ -123,6 +142,12 @@ void MessageDelegate::initializeFonts(const QFont& font)
dateMetrics = QFontMetrics(dateFont); dateMetrics = QFontMetrics(dateFont);
} }
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{
//qDebug() << event->type();
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const // void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
// { // {

View File

@ -37,6 +37,7 @@ public:
//void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override; //void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void initializeFonts(const QFont& font); void initializeFonts(const QFont& font);
bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override;
private: private:
QFont bodyFont; QFont bodyFont;