self nick in the chat fix, hovering message feature
This commit is contained in:
parent
270a32db9e
commit
15342f3c53
7 changed files with 130 additions and 30 deletions
|
@ -33,6 +33,9 @@ FeedView::FeedView(QWidget* parent):
|
|||
{
|
||||
horizontalScrollBar()->setRange(0, 0);
|
||||
verticalScrollBar()->setSingleStep(approximateSingleMessageHeight);
|
||||
setMouseTracking(true);
|
||||
setSelectionBehavior(SelectItems);
|
||||
// viewport()->setAttribute(Qt::WA_Hover, true);
|
||||
}
|
||||
|
||||
FeedView::~FeedView()
|
||||
|
@ -41,14 +44,12 @@ FeedView::~FeedView()
|
|||
|
||||
QModelIndex FeedView::indexAt(const QPoint& point) const
|
||||
{
|
||||
int32_t totalHeight = viewport()->height() + vo;
|
||||
if (point.y() <= totalHeight) { //if it's bigger - someone wants to know the index below the feed beginning, it's invalid
|
||||
uint32_t y = totalHeight - point.y();
|
||||
|
||||
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
|
||||
if (y > hints[i].offset) {
|
||||
return model()->index(i - 1, 0, rootIndex());
|
||||
}
|
||||
int32_t vh = viewport()->height();
|
||||
uint32_t y = vh - point.y() + vo;
|
||||
|
||||
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
|
||||
if (hints[i].offset >= y) {
|
||||
return model()->index(i - 1, 0, rootIndex());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -219,9 +220,11 @@ void FeedView::paintEvent(QPaintEvent* event)
|
|||
QPainter painter(vp);
|
||||
QStyleOptionViewItem option = viewOptions();
|
||||
option.features = QStyleOptionViewItem::WrapText;
|
||||
QPoint cursor = vp->mapFromGlobal(QCursor::pos());
|
||||
|
||||
for (const QModelIndex& index : toRener) {
|
||||
option.rect = visualRect(index);
|
||||
option.state.setFlag(QStyle::State_MouseOver, option.rect.contains(cursor));
|
||||
itemDelegate(index)->paint(&painter, option, index);
|
||||
}
|
||||
}
|
||||
|
@ -233,6 +236,16 @@ void FeedView::verticalScrollbarValueChanged(int value)
|
|||
QAbstractItemView::verticalScrollbarValueChanged(vo);
|
||||
}
|
||||
|
||||
void FeedView::mouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
if (!isVisible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QAbstractItemView::mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
|
||||
QFont FeedView::getFont() const
|
||||
{
|
||||
return viewOptions().font;
|
||||
|
|
|
@ -45,6 +45,8 @@ public:
|
|||
|
||||
QFont getFont() const;
|
||||
|
||||
public slots:
|
||||
|
||||
protected slots:
|
||||
void rowsInserted(const QModelIndex & parent, int start, int end) override;
|
||||
void verticalScrollbarValueChanged(int value) override;
|
||||
|
@ -54,6 +56,7 @@ protected:
|
|||
int horizontalOffset() const override;
|
||||
void paintEvent(QPaintEvent * event) override;
|
||||
void updateGeometries() override;
|
||||
void mouseMoveEvent(QMouseEvent * event) override;
|
||||
|
||||
private:
|
||||
bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight);
|
||||
|
|
|
@ -16,8 +16,10 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QApplication>
|
||||
|
||||
#include "messagedelegate.h"
|
||||
#include "ui/models/messagefeed.h"
|
||||
|
||||
|
@ -41,16 +43,21 @@ MessageDelegate::~MessageDelegate()
|
|||
|
||||
void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
bool sentByMe = false;
|
||||
QVariant sbm = index.data(Models::MessageFeed::SentByMe);
|
||||
if (sbm.isValid()) {
|
||||
sentByMe = sbm.toBool();
|
||||
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
||||
if (!vi.isValid()) {
|
||||
return;
|
||||
}
|
||||
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
||||
painter->save();
|
||||
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));
|
||||
} else {
|
||||
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;
|
||||
QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 2);
|
||||
if (!sentByMe) {
|
||||
if (!data.sentByMe) {
|
||||
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
|
||||
messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0);
|
||||
} else {
|
||||
|
@ -66,27 +73,39 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
|
|||
}
|
||||
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;
|
||||
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);
|
||||
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);
|
||||
painter->setFont(dateFont);
|
||||
QColor q = painter->pen().color();
|
||||
q.setAlpha(180);
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
opt.rect = messageRect;
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
// {
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
//void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
|
||||
|
||||
void initializeFonts(const QFont& font);
|
||||
bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override;
|
||||
|
||||
private:
|
||||
QFont bodyFont;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue