From eac87e713f72f51f1f24ac11d4fc52a57c861f8f Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 28 Apr 2022 00:08:59 +0300 Subject: [PATCH] seem to have found a text block, to activate with the click later --- ui/widgets/messageline/feedview.cpp | 29 ++++++++- ui/widgets/messageline/feedview.h | 3 + ui/widgets/messageline/messagedelegate.cpp | 74 ++++++++++++++++++++-- ui/widgets/messageline/messagedelegate.h | 1 + 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/ui/widgets/messageline/feedview.cpp b/ui/widgets/messageline/feedview.cpp index e0c1477..bf1da61 100644 --- a/ui/widgets/messageline/feedview.cpp +++ b/ui/widgets/messageline/feedview.cpp @@ -50,7 +50,8 @@ FeedView::FeedView(QWidget* parent): modelState(Models::MessageFeed::complete), progress(), dividerFont(), - dividerMetrics(dividerFont) + dividerMetrics(dividerFont), + mousePressed(false) { horizontalScrollBar()->setRange(0, 0); verticalScrollBar()->setSingleStep(approximateSingleMessageHeight); @@ -412,10 +413,36 @@ void FeedView::mouseMoveEvent(QMouseEvent* event) if (!isVisible()) { return; } + + mousePressed = false; + //qDebug() << event; QAbstractItemView::mouseMoveEvent(event); } +void FeedView::mousePressEvent(QMouseEvent* event) +{ + QAbstractItemView::mousePressEvent(event); + mousePressed = event->button() == Qt::LeftButton; +} + +void FeedView::mouseReleaseEvent(QMouseEvent* event) +{ + QAbstractItemView::mouseReleaseEvent(event); + + if (mousePressed && specialDelegate) { + QPoint point = event->localPos().toPoint(); + QModelIndex index = indexAt(point); + if (index.isValid()) { + QRect rect = visualRect(index); + MessageDelegate* del = static_cast(itemDelegate()); + if (rect.contains(point)) { + del->leftClick(point, index, rect); + } + } + } +} + void FeedView::resizeEvent(QResizeEvent* event) { QAbstractItemView::resizeEvent(event); diff --git a/ui/widgets/messageline/feedview.h b/ui/widgets/messageline/feedview.h index 8bcd913..c757986 100644 --- a/ui/widgets/messageline/feedview.h +++ b/ui/widgets/messageline/feedview.h @@ -68,6 +68,8 @@ protected: void paintEvent(QPaintEvent * event) override; void updateGeometries() override; void mouseMoveEvent(QMouseEvent * event) override; + void mousePressEvent(QMouseEvent * event) override; + void mouseReleaseEvent(QMouseEvent * event) override; void resizeEvent(QResizeEvent * event) override; private: @@ -93,6 +95,7 @@ private: Progress progress; QFont dividerFont; QFontMetrics dividerMetrics; + bool mousePressed; static const std::set geometryChangingRoles; diff --git a/ui/widgets/messageline/messagedelegate.cpp b/ui/widgets/messageline/messagedelegate.cpp index 1d094fa..c787cfa 100644 --- a/ui/widgets/messageline/messagedelegate.cpp +++ b/ui/widgets/messageline/messagedelegate.cpp @@ -22,7 +22,9 @@ #include #include #include -#include +#include +#include +#include #include "messagedelegate.h" #include "messagefeed.h" @@ -303,7 +305,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel QSizeF size = bodyRenderer->size(); size.setWidth(bodyRenderer->idealWidth()); - messageSize = QSize(qCeil(size.width()), qCeil(size.height())); + messageSize = QSize(std::ceil(size.width()), std::ceil(size.height())); messageSize.rheight() += textMargin; } @@ -364,6 +366,68 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel return messageSize; } +void MessageDelegate::leftClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const +{ + QVariant vi = index.data(Models::MessageFeed::Bulk); + Models::FeedItem data = qvariant_cast(vi); + if (data.text.size() > 0) { + QRect localHint = sizeHint.adjusted(bubbleMargin, bubbleMargin + margin, -bubbleMargin, -bubbleMargin / 2); + if (needToDrawSender(index, data)) { + localHint.adjust(0, nickMetrics.lineSpacing() + textMargin, 0, 0); + } + + int attachHeight = 0; + switch (data.attach.state) { + case Models::none: + break; + case Models::uploading: + attachHeight += Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint).height() + textMargin; + [[fallthrough]]; + case Models::downloading: + attachHeight += barHeight + textMargin; + break; + case Models::remote: + attachHeight += buttonHeight + textMargin; + break; + case Models::ready: + case Models::local: { + QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint); + attachHeight += aSize.height() + textMargin; + } + break; + case Models::errorDownload: { + QSize commentSize = dateMetrics.boundingRect(localHint, Qt::TextWordWrap, data.attach.error).size(); + attachHeight += commentSize.height() + buttonHeight + textMargin * 2; + } + break; + case Models::errorUpload: { + QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint); + QSize commentSize = dateMetrics.boundingRect(localHint, Qt::TextWordWrap, data.attach.error).size(); + attachHeight += aSize.height() + commentSize.height() + textMargin * 2; + } + break; + } + + int bottomSize = std::max(dateMetrics.lineSpacing(), statusIconSize); + localHint.adjust(0, attachHeight, 0, -(bottomSize + textMargin)); + + if (localHint.contains(point)) { + qDebug() << "MESSAGE CLICKED"; + QPoint translated = point - localHint.topLeft(); + + bodyRenderer->setPlainText(data.text); + bodyRenderer->setTextWidth(localHint.size().width()); + + int pos = bodyRenderer->documentLayout()->hitTest(translated, Qt::FuzzyHit); + QTextBlock block = bodyRenderer->findBlock(pos); + QString text = block.text(); + if (text.size() > 0) { + qDebug() << text; + } + } + } +} + void MessageDelegate::initializeFonts(const QFont& font) { bodyFont = font; @@ -625,9 +689,9 @@ int MessageDelegate::paintBody(const Models::FeedItem& data, QPainter* painter, painter->translate(option.rect.topLeft()); bodyRenderer->drawContents(painter); painter->restore(); - QSize bodySize(qCeil(bodyRenderer->idealWidth()), qCeil(bodyRenderer->size().height())); - + QSize bodySize(std::ceil(bodyRenderer->idealWidth()), std::ceil(bodyRenderer->size().height())); +/* QTextBrowser* editor = nullptr; if (option.state.testFlag(QStyle::State_MouseOver)) { std::set ids({data.id}); @@ -645,7 +709,7 @@ int MessageDelegate::paintBody(const Models::FeedItem& data, QPainter* painter, editor->setMaximumSize(bodySize); editor->move(option.rect.left(), option.rect.y()); editor->show(); - } + }*/ option.rect.adjust(0, bodySize.height() + textMargin, 0, 0); return bodySize.width(); diff --git a/ui/widgets/messageline/messagedelegate.h b/ui/widgets/messageline/messagedelegate.h index b49410f..9333d45 100644 --- a/ui/widgets/messageline/messagedelegate.h +++ b/ui/widgets/messageline/messagedelegate.h @@ -58,6 +58,7 @@ public: bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override; void endClearWidgets(); void beginClearWidgets(); + void leftClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const; static int avatarHeight; static int margin;