1
0
forked from blue/squawk

seem to have found a text block, to activate with the click later

This commit is contained in:
Blue 2022-04-28 00:08:59 +03:00
parent d86e2c28a0
commit eac87e713f
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 101 additions and 6 deletions

View File

@ -50,7 +50,8 @@ FeedView::FeedView(QWidget* parent):
modelState(Models::MessageFeed::complete), modelState(Models::MessageFeed::complete),
progress(), progress(),
dividerFont(), dividerFont(),
dividerMetrics(dividerFont) dividerMetrics(dividerFont),
mousePressed(false)
{ {
horizontalScrollBar()->setRange(0, 0); horizontalScrollBar()->setRange(0, 0);
verticalScrollBar()->setSingleStep(approximateSingleMessageHeight); verticalScrollBar()->setSingleStep(approximateSingleMessageHeight);
@ -412,10 +413,36 @@ void FeedView::mouseMoveEvent(QMouseEvent* event)
if (!isVisible()) { if (!isVisible()) {
return; return;
} }
mousePressed = false;
//qDebug() << event;
QAbstractItemView::mouseMoveEvent(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<MessageDelegate*>(itemDelegate());
if (rect.contains(point)) {
del->leftClick(point, index, rect);
}
}
}
}
void FeedView::resizeEvent(QResizeEvent* event) void FeedView::resizeEvent(QResizeEvent* event)
{ {
QAbstractItemView::resizeEvent(event); QAbstractItemView::resizeEvent(event);

View File

@ -68,6 +68,8 @@ protected:
void paintEvent(QPaintEvent * event) override; void paintEvent(QPaintEvent * event) override;
void updateGeometries() override; void updateGeometries() override;
void mouseMoveEvent(QMouseEvent * event) override; void mouseMoveEvent(QMouseEvent * event) override;
void mousePressEvent(QMouseEvent * event) override;
void mouseReleaseEvent(QMouseEvent * event) override;
void resizeEvent(QResizeEvent * event) override; void resizeEvent(QResizeEvent * event) override;
private: private:
@ -93,6 +95,7 @@ private:
Progress progress; Progress progress;
QFont dividerFont; QFont dividerFont;
QFontMetrics dividerMetrics; QFontMetrics dividerMetrics;
bool mousePressed;
static const std::set<int> geometryChangingRoles; static const std::set<int> geometryChangingRoles;

View File

@ -22,7 +22,9 @@
#include <QApplication> #include <QApplication>
#include <QMouseEvent> #include <QMouseEvent>
#include <QAbstractItemView> #include <QAbstractItemView>
#include <QtMath> #include <QAbstractTextDocumentLayout>
#include <QTextBlock>
#include <cmath>
#include "messagedelegate.h" #include "messagedelegate.h"
#include "messagefeed.h" #include "messagefeed.h"
@ -303,7 +305,7 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
QSizeF size = bodyRenderer->size(); QSizeF size = bodyRenderer->size();
size.setWidth(bodyRenderer->idealWidth()); 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; messageSize.rheight() += textMargin;
} }
@ -364,6 +366,68 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
return messageSize; 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<Models::FeedItem>(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) void MessageDelegate::initializeFonts(const QFont& font)
{ {
bodyFont = font; bodyFont = font;
@ -625,9 +689,9 @@ int MessageDelegate::paintBody(const Models::FeedItem& data, QPainter* painter,
painter->translate(option.rect.topLeft()); painter->translate(option.rect.topLeft());
bodyRenderer->drawContents(painter); bodyRenderer->drawContents(painter);
painter->restore(); 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; QTextBrowser* editor = nullptr;
if (option.state.testFlag(QStyle::State_MouseOver)) { if (option.state.testFlag(QStyle::State_MouseOver)) {
std::set<QString> ids({data.id}); std::set<QString> ids({data.id});
@ -645,7 +709,7 @@ int MessageDelegate::paintBody(const Models::FeedItem& data, QPainter* painter,
editor->setMaximumSize(bodySize); editor->setMaximumSize(bodySize);
editor->move(option.rect.left(), option.rect.y()); editor->move(option.rect.left(), option.rect.y());
editor->show(); editor->show();
} }*/
option.rect.adjust(0, bodySize.height() + textMargin, 0, 0); option.rect.adjust(0, bodySize.height() + textMargin, 0, 0);
return bodySize.width(); return bodySize.width();

View File

@ -58,6 +58,7 @@ public:
bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override; bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) override;
void endClearWidgets(); void endClearWidgets();
void beginClearWidgets(); void beginClearWidgets();
void leftClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const;
static int avatarHeight; static int avatarHeight;
static int margin; static int margin;