2020-08-15 21:48:28 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "feedview.h"
|
|
|
|
|
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QPainter>
|
2020-08-17 10:27:14 +00:00
|
|
|
#include <QScrollBar>
|
2022-05-02 19:25:50 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
2020-08-15 21:48:28 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
#include "messagedelegate.h"
|
2021-05-15 22:07:49 +00:00
|
|
|
#include "messagefeed.h"
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
constexpr int maxMessageHeight = 10000;
|
|
|
|
constexpr int approximateSingleMessageHeight = 20;
|
2021-04-30 20:07:00 +00:00
|
|
|
constexpr int progressSize = 70;
|
2021-09-21 22:17:43 +00:00
|
|
|
constexpr int dateDeviderMargin = 10;
|
2020-08-17 10:27:14 +00:00
|
|
|
|
2021-04-22 22:41:32 +00:00
|
|
|
const std::set<int> FeedView::geometryChangingRoles = {
|
|
|
|
Models::MessageFeed::Attach,
|
|
|
|
Models::MessageFeed::Text,
|
|
|
|
Models::MessageFeed::Id,
|
2021-05-07 18:26:02 +00:00
|
|
|
Models::MessageFeed::Error,
|
|
|
|
Models::MessageFeed::Date
|
2021-04-22 22:41:32 +00:00
|
|
|
};
|
|
|
|
|
2020-08-15 21:48:28 +00:00
|
|
|
FeedView::FeedView(QWidget* parent):
|
|
|
|
QAbstractItemView(parent),
|
2020-08-17 10:27:14 +00:00
|
|
|
hints(),
|
2021-02-01 22:55:15 +00:00
|
|
|
vo(0),
|
2022-01-09 14:32:23 +00:00
|
|
|
elementMargin(0),
|
2021-02-01 22:55:15 +00:00
|
|
|
specialDelegate(false),
|
2021-02-06 11:02:42 +00:00
|
|
|
specialModel(false),
|
2021-04-30 20:07:00 +00:00
|
|
|
clearWidgetsMode(false),
|
|
|
|
modelState(Models::MessageFeed::complete),
|
2021-09-21 22:17:43 +00:00
|
|
|
progress(),
|
2023-01-12 17:56:01 +00:00
|
|
|
dividerFont(Shared::Global::getInstance()->titleFont),
|
|
|
|
dividerMetrics(Shared::Global::getInstance()->titleFontMetrics),
|
2022-04-28 21:29:44 +00:00
|
|
|
mousePressed(false),
|
2022-05-01 20:19:52 +00:00
|
|
|
dragging(false),
|
2022-05-02 19:25:50 +00:00
|
|
|
hovered(Shared::Hover::nothing),
|
2022-05-01 20:19:52 +00:00
|
|
|
dragStartPoint(),
|
2022-05-02 19:25:50 +00:00
|
|
|
dragEndPoint(),
|
|
|
|
selectedText()
|
2020-08-15 21:48:28 +00:00
|
|
|
{
|
2020-08-17 10:27:14 +00:00
|
|
|
horizontalScrollBar()->setRange(0, 0);
|
|
|
|
verticalScrollBar()->setSingleStep(approximateSingleMessageHeight);
|
2021-01-07 21:50:12 +00:00
|
|
|
setMouseTracking(true);
|
|
|
|
setSelectionBehavior(SelectItems);
|
|
|
|
// viewport()->setAttribute(Qt::WA_Hover, true);
|
2021-04-30 20:07:00 +00:00
|
|
|
|
|
|
|
progress.setParent(viewport());
|
|
|
|
progress.resize(progressSize, progressSize);
|
2020-08-15 21:48:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
FeedView::~FeedView() {}
|
2020-08-15 21:48:28 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
QModelIndex FeedView::indexAt(const QPoint& point) const {
|
2021-01-07 21:50:12 +00:00
|
|
|
int32_t vh = viewport()->height();
|
|
|
|
uint32_t y = vh - point.y() + vo;
|
|
|
|
|
|
|
|
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
|
2021-05-04 14:09:41 +00:00
|
|
|
const Hint& hint = hints[i];
|
|
|
|
if (y <= hint.offset + hint.height) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (y > hint.offset)
|
2021-05-04 14:09:41 +00:00
|
|
|
return model()->index(i, 0, rootIndex());
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2021-05-04 14:09:41 +00:00
|
|
|
break;
|
2020-08-17 10:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 21:48:28 +00:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
QRect FeedView::visualRect(const QModelIndex& index) const {
|
2021-05-01 23:03:08 +00:00
|
|
|
unsigned int row = index.row();
|
|
|
|
if (!index.isValid() || row >= hints.size()) {
|
|
|
|
qDebug() << "visualRect for" << row;
|
2020-08-15 21:48:28 +00:00
|
|
|
return QRect();
|
|
|
|
} else {
|
2021-05-01 23:03:08 +00:00
|
|
|
const Hint& hint = hints.at(row);
|
2020-08-15 21:48:28 +00:00
|
|
|
const QWidget* vp = viewport();
|
2022-01-09 14:32:23 +00:00
|
|
|
return QRect(hint.x, vp->height() - hint.height - hint.offset + vo, hint.width, hint.height);
|
2020-08-15 21:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
QString FeedView::getSelectedText() const{return selectedText;}
|
2020-08-15 21:48:28 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
//TODO!!!
|
|
|
|
void FeedView::scrollTo(const QModelIndex& index, QAbstractItemView::ScrollHint hint) {}
|
|
|
|
int FeedView::horizontalOffset() const {return 0;}
|
|
|
|
bool FeedView::isIndexHidden(const QModelIndex& index) const{return false;}
|
|
|
|
QModelIndex FeedView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers) {return QModelIndex();}
|
|
|
|
void FeedView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags command) {}
|
|
|
|
int FeedView::verticalOffset() const {return vo;}
|
|
|
|
QRegion FeedView::visualRegionForSelection(const QItemSelection& selection) const {return QRegion();}
|
2020-08-15 21:48:28 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::rowsInserted(const QModelIndex& parent, int start, int end){
|
2020-08-15 21:48:28 +00:00
|
|
|
QAbstractItemView::rowsInserted(parent, start, end);
|
2021-05-01 23:03:08 +00:00
|
|
|
|
|
|
|
scheduleDelayedItemsLayout();
|
2020-08-15 21:48:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles) {
|
2021-04-22 22:41:32 +00:00
|
|
|
if (specialDelegate) {
|
|
|
|
for (int role : roles) {
|
|
|
|
if (geometryChangingRoles.count(role) != 0) {
|
2021-05-01 23:03:08 +00:00
|
|
|
scheduleDelayedItemsLayout(); //to recalculate layout only if there are some geometry changing modifications
|
2021-04-22 22:41:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 21:56:47 +00:00
|
|
|
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::updateGeometries() {
|
2022-05-02 19:25:50 +00:00
|
|
|
//qDebug() << "updateGeometries";
|
2023-11-12 22:55:32 +00:00
|
|
|
const QAbstractItemModel* m = model();
|
|
|
|
if (m == nullptr)
|
|
|
|
return QAbstractItemView::updateGeometries();
|
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
QScrollBar* bar = verticalScrollBar();
|
|
|
|
const QStyle* st = style();
|
2023-11-12 22:55:32 +00:00
|
|
|
|
2021-05-01 23:03:08 +00:00
|
|
|
QSize layoutBounds = maximumViewportSize();
|
2024-12-13 23:53:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
QStyleOptionViewItem option;
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
initViewItemOption(&option);
|
|
|
|
#else
|
|
|
|
option = viewOptions();
|
|
|
|
#endif
|
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
option.rect.setHeight(maxMessageHeight);
|
|
|
|
option.rect.setWidth(layoutBounds.width());
|
|
|
|
int frameAroundContents = 0;
|
|
|
|
int verticalScrollBarExtent = st->pixelMetric(QStyle::PM_ScrollBarExtent, 0, bar);
|
|
|
|
|
|
|
|
bool layedOut = false;
|
|
|
|
if (verticalScrollBarExtent != 0 && verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded && m->rowCount() * approximateSingleMessageHeight < layoutBounds.height()) {
|
|
|
|
hints.clear();
|
|
|
|
layedOut = tryToCalculateGeometriesWithNoScrollbars(option, m, layoutBounds.height());
|
|
|
|
}
|
2020-08-15 21:48:28 +00:00
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
if (layedOut) {
|
|
|
|
bar->setRange(0, 0);
|
2021-05-01 23:03:08 +00:00
|
|
|
vo = 0;
|
2020-08-17 10:27:14 +00:00
|
|
|
} else {
|
|
|
|
int verticalMargin = 0;
|
2023-08-15 15:28:25 +00:00
|
|
|
if (st->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents))
|
2020-08-17 10:27:14 +00:00
|
|
|
frameAroundContents = st->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded)
|
2020-08-17 10:27:14 +00:00
|
|
|
verticalMargin = verticalScrollBarExtent + frameAroundContents;
|
|
|
|
|
2021-05-01 23:03:08 +00:00
|
|
|
layoutBounds.rwidth() -= verticalMargin;
|
2020-08-17 10:27:14 +00:00
|
|
|
|
|
|
|
option.features |= QStyleOptionViewItem::WrapText;
|
|
|
|
option.rect.setWidth(layoutBounds.width());
|
|
|
|
|
|
|
|
hints.clear();
|
2022-01-09 14:32:23 +00:00
|
|
|
uint32_t previousOffset = elementMargin;
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime lastDate;
|
2020-08-17 10:27:14 +00:00
|
|
|
for (int i = 0, size = m->rowCount(); i < size; ++i) {
|
|
|
|
QModelIndex index = m->index(i, 0, rootIndex());
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime currentDate = index.data(Models::MessageFeed::Date).toDateTime();
|
|
|
|
if (i > 0) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (currentDate.daysTo(lastDate) > 0)
|
2021-09-21 22:17:43 +00:00
|
|
|
previousOffset += dividerMetrics.height() + dateDeviderMargin * 2;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-09 14:32:23 +00:00
|
|
|
previousOffset += elementMargin;
|
2021-09-21 22:17:43 +00:00
|
|
|
}
|
|
|
|
lastDate = currentDate;
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2024-12-13 23:53:04 +00:00
|
|
|
QSize messageSize = itemDelegateForIndex(index)->sizeHint(option, index);
|
2024-12-14 16:08:50 +00:00
|
|
|
#else
|
|
|
|
QSize messageSize = itemDelegate(index)->sizeHint(option, index);
|
|
|
|
#endif
|
2022-01-09 14:32:23 +00:00
|
|
|
uint32_t offsetX(0);
|
|
|
|
if (specialDelegate) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (index.data(Models::MessageFeed::SentByMe).toBool())
|
2022-01-09 14:32:23 +00:00
|
|
|
offsetX = layoutBounds.width() - messageSize.width() - MessageDelegate::avatarHeight - MessageDelegate::margin * 2;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-09 14:32:23 +00:00
|
|
|
offsetX = MessageDelegate::avatarHeight + MessageDelegate::margin * 2;
|
|
|
|
}
|
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
hints.emplace_back(Hint({
|
|
|
|
false,
|
|
|
|
previousOffset,
|
2022-01-09 14:32:23 +00:00
|
|
|
static_cast<uint32_t>(messageSize.height()),
|
|
|
|
static_cast<uint32_t>(messageSize.width()),
|
|
|
|
offsetX
|
2020-08-17 10:27:14 +00:00
|
|
|
}));
|
2022-01-09 14:32:23 +00:00
|
|
|
previousOffset += messageSize.height();
|
2020-08-17 10:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
int totalHeight = previousOffset - layoutBounds.height() + dividerMetrics.height() + dateDeviderMargin * 2;
|
2023-08-15 15:28:25 +00:00
|
|
|
if (modelState != Models::MessageFeed::complete)
|
2021-05-01 23:03:08 +00:00
|
|
|
totalHeight += progressSize;
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-05-01 23:03:08 +00:00
|
|
|
vo = qMax(qMin(vo, totalHeight), 0);
|
|
|
|
bar->setRange(0, totalHeight);
|
2020-08-17 10:27:14 +00:00
|
|
|
bar->setPageStep(layoutBounds.height());
|
2021-05-01 23:03:08 +00:00
|
|
|
bar->setValue(totalHeight - vo);
|
2020-08-17 10:27:14 +00:00
|
|
|
}
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2021-05-01 23:03:08 +00:00
|
|
|
positionProgress();
|
|
|
|
|
2023-11-12 22:55:32 +00:00
|
|
|
if (specialDelegate)
|
2021-02-01 22:55:15 +00:00
|
|
|
clearWidgetsMode = true;
|
2021-05-01 23:03:08 +00:00
|
|
|
|
|
|
|
QAbstractItemView::updateGeometries();
|
2020-08-17 10:27:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
bool FeedView::tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* m, uint32_t totalHeight) {
|
2022-01-09 14:32:23 +00:00
|
|
|
uint32_t previousOffset = elementMargin;
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime lastDate;
|
2020-08-15 21:48:28 +00:00
|
|
|
for (int i = 0, size = m->rowCount(); i < size; ++i) {
|
2020-08-17 10:27:14 +00:00
|
|
|
QModelIndex index = m->index(i, 0, rootIndex());
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime currentDate = index.data(Models::MessageFeed::Date).toDateTime();
|
|
|
|
if (i > 0) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (currentDate.daysTo(lastDate) > 0)
|
2021-09-21 22:17:43 +00:00
|
|
|
previousOffset += dateDeviderMargin * 2 + dividerMetrics.height();
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-09 14:32:23 +00:00
|
|
|
previousOffset += elementMargin;
|
2021-09-21 22:17:43 +00:00
|
|
|
}
|
|
|
|
lastDate = currentDate;
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
QSize messageSize = itemDelegateForIndex(index)->sizeHint(option, index);
|
|
|
|
#else
|
|
|
|
QSize messageSize = itemDelegate(index)->sizeHint(option, index);
|
|
|
|
#endif
|
2020-08-17 10:27:14 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (previousOffset + messageSize.height() + elementMargin > totalHeight)
|
2022-01-11 20:50:42 +00:00
|
|
|
return false;
|
2022-01-09 14:32:23 +00:00
|
|
|
|
|
|
|
uint32_t offsetX(0);
|
|
|
|
if (specialDelegate) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (index.data(Models::MessageFeed::SentByMe).toBool())
|
2022-01-09 14:32:23 +00:00
|
|
|
offsetX = option.rect.width() - messageSize.width() - MessageDelegate::avatarHeight - MessageDelegate::margin * 2;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-09 14:32:23 +00:00
|
|
|
offsetX = MessageDelegate::avatarHeight + MessageDelegate::margin * 2;
|
|
|
|
}
|
2020-08-15 21:48:28 +00:00
|
|
|
hints.emplace_back(Hint({
|
|
|
|
false,
|
|
|
|
previousOffset,
|
2022-01-09 14:32:23 +00:00
|
|
|
static_cast<uint32_t>(messageSize.height()),
|
|
|
|
static_cast<uint32_t>(messageSize.width()),
|
|
|
|
offsetX
|
2020-08-15 21:48:28 +00:00
|
|
|
}));
|
2022-01-09 14:32:23 +00:00
|
|
|
previousOffset += messageSize.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
previousOffset += dateDeviderMargin * 2 + dividerMetrics.height();
|
2023-08-15 15:28:25 +00:00
|
|
|
if (previousOffset > totalHeight)
|
2022-01-11 20:50:42 +00:00
|
|
|
return false;
|
2020-08-17 10:27:14 +00:00
|
|
|
|
2022-01-11 20:50:42 +00:00
|
|
|
return true;
|
2020-08-15 21:48:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::paintEvent(QPaintEvent* event) {
|
2022-05-02 19:25:50 +00:00
|
|
|
//qDebug() << "paint" << event->rect();
|
2020-08-15 21:48:28 +00:00
|
|
|
const QAbstractItemModel* m = model();
|
2020-08-17 10:27:14 +00:00
|
|
|
QWidget* vp = viewport();
|
|
|
|
QRect zone = event->rect().translated(0, -vo);
|
|
|
|
uint32_t vph = vp->height();
|
|
|
|
int32_t y1 = zone.y();
|
|
|
|
int32_t y2 = y1 + zone.height();
|
|
|
|
|
|
|
|
bool inZone = false;
|
|
|
|
std::deque<QModelIndex> toRener;
|
|
|
|
for (std::deque<Hint>::size_type i = 0; i < hints.size(); ++i) {
|
|
|
|
const Hint& hint = hints[i];
|
|
|
|
int32_t relativeY1 = vph - hint.offset - hint.height;
|
|
|
|
if (!inZone) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (y2 > relativeY1)
|
2020-08-17 10:27:14 +00:00
|
|
|
inZone = true;
|
|
|
|
}
|
2023-08-15 15:28:25 +00:00
|
|
|
if (inZone)
|
2020-08-17 10:27:14 +00:00
|
|
|
toRener.emplace_back(m->index(i, 0, rootIndex()));
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
if (y1 > relativeY1) {
|
2021-09-21 22:17:43 +00:00
|
|
|
inZone = false;
|
2020-08-17 10:27:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-13 23:53:04 +00:00
|
|
|
QStyleOptionViewItem option;
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
initViewItemOption(&option);
|
|
|
|
#else
|
|
|
|
option = viewOptions();
|
|
|
|
#endif
|
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
QPainter painter(vp);
|
|
|
|
option.features = QStyleOptionViewItem::WrapText;
|
2021-01-07 21:50:12 +00:00
|
|
|
QPoint cursor = vp->mapFromGlobal(QCursor::pos());
|
2020-08-15 21:48:28 +00:00
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
if (specialDelegate) {
|
2021-02-06 11:02:42 +00:00
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
2023-08-15 15:28:25 +00:00
|
|
|
if (clearWidgetsMode)
|
2021-04-26 16:37:36 +00:00
|
|
|
del->beginClearWidgets();
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime lastDate;
|
|
|
|
bool first = true;
|
2022-04-26 22:17:53 +00:00
|
|
|
QRect viewportRect = vp->rect();
|
2020-08-17 10:27:14 +00:00
|
|
|
for (const QModelIndex& index : toRener) {
|
2021-09-21 22:17:43 +00:00
|
|
|
QDateTime currentDate = index.data(Models::MessageFeed::Date).toDateTime();
|
2020-08-15 21:48:28 +00:00
|
|
|
option.rect = visualRect(index);
|
2021-09-21 22:17:43 +00:00
|
|
|
if (first) {
|
|
|
|
int ind = index.row() - 1;
|
|
|
|
if (ind > 0) {
|
|
|
|
QDateTime underDate = m->index(ind, 0, rootIndex()).data(Models::MessageFeed::Date).toDateTime();
|
2023-08-15 15:28:25 +00:00
|
|
|
if (currentDate.daysTo(underDate) > 0)
|
2021-09-21 22:17:43 +00:00
|
|
|
drawDateDevider(option.rect.bottom(), underDate, painter);
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
2022-04-26 22:17:53 +00:00
|
|
|
QRect stripe = option.rect;
|
|
|
|
stripe.setLeft(0);
|
|
|
|
stripe.setWidth(viewportRect.width());
|
|
|
|
bool mouseOver = stripe.contains(cursor) && viewportRect.contains(cursor);
|
2021-05-04 14:09:41 +00:00
|
|
|
option.state.setFlag(QStyle::State_MouseOver, mouseOver);
|
2024-12-14 16:08:50 +00:00
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
itemDelegateForIndex(index)->paint(&painter, option, index);
|
|
|
|
#else
|
|
|
|
itemDelegate(index)->paint(&painter, option, index);
|
|
|
|
#endif
|
2021-09-21 22:17:43 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!lastDate.isNull() && currentDate.daysTo(lastDate) > 0)
|
2021-09-21 22:17:43 +00:00
|
|
|
drawDateDevider(option.rect.bottom(), lastDate, painter);
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-09-21 22:17:43 +00:00
|
|
|
lastDate = currentDate;
|
|
|
|
}
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!lastDate.isNull() && inZone) //if after drawing all messages there is still space
|
2022-01-09 14:32:23 +00:00
|
|
|
drawDateDevider(option.rect.top() - dateDeviderMargin * 2 - dividerMetrics.height(), lastDate, painter);
|
2021-02-01 22:55:15 +00:00
|
|
|
|
|
|
|
if (clearWidgetsMode && specialDelegate) {
|
2021-02-06 11:02:42 +00:00
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
2021-02-01 22:55:15 +00:00
|
|
|
del->endClearWidgets();
|
|
|
|
clearWidgetsMode = false;
|
|
|
|
}
|
2020-08-15 21:48:28 +00:00
|
|
|
}
|
2020-08-17 10:27:14 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter) {
|
2021-09-21 22:17:43 +00:00
|
|
|
int divisionHeight = dateDeviderMargin * 2 + dividerMetrics.height();
|
|
|
|
QRect r(QPoint(0, top), QSize(viewport()->width(), divisionHeight));
|
|
|
|
painter.save();
|
|
|
|
painter.setFont(dividerFont);
|
|
|
|
painter.drawText(r, Qt::AlignCenter, date.toString("d MMMM"));
|
|
|
|
painter.restore();
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::verticalScrollbarValueChanged(int value) {
|
2020-08-17 10:27:14 +00:00
|
|
|
vo = verticalScrollBar()->maximum() - value;
|
|
|
|
|
2021-05-01 23:03:08 +00:00
|
|
|
positionProgress();
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (specialDelegate)
|
2021-02-01 22:55:15 +00:00
|
|
|
clearWidgetsMode = true;
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (modelState == Models::MessageFeed::incomplete && value < progressSize)
|
2021-05-03 00:35:43 +00:00
|
|
|
model()->fetchMore(rootIndex());
|
|
|
|
|
2020-08-17 10:27:14 +00:00
|
|
|
QAbstractItemView::verticalScrollbarValueChanged(vo);
|
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::setAnchorHovered(Shared::Hover type) {
|
2022-05-02 19:25:50 +00:00
|
|
|
if (hovered != type) {
|
|
|
|
hovered = type;
|
|
|
|
switch (hovered) {
|
|
|
|
case Shared::Hover::nothing:
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
break;
|
|
|
|
case Shared::Hover::text:
|
|
|
|
setCursor(Qt::IBeamCursor);
|
|
|
|
break;
|
|
|
|
case Shared::Hover::anchor:
|
|
|
|
setCursor(Qt::PointingHandCursor);
|
|
|
|
break;
|
2022-04-28 21:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::mouseMoveEvent(QMouseEvent* event) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!isVisible())
|
2021-01-07 21:50:12 +00:00
|
|
|
return;
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
dragEndPoint = event->position().toPoint();
|
|
|
|
#else
|
|
|
|
dragEndPoint = event->localPos().toPoint();
|
|
|
|
#endif
|
2022-04-27 21:08:59 +00:00
|
|
|
|
2022-05-01 20:19:52 +00:00
|
|
|
if (mousePressed) {
|
|
|
|
QPoint distance = dragStartPoint - dragEndPoint;
|
2023-08-15 15:28:25 +00:00
|
|
|
if (distance.manhattanLength() > 5)
|
2022-05-01 20:19:52 +00:00
|
|
|
dragging = true;
|
|
|
|
}
|
2021-01-07 21:50:12 +00:00
|
|
|
|
|
|
|
QAbstractItemView::mouseMoveEvent(event);
|
2022-04-28 21:29:44 +00:00
|
|
|
|
|
|
|
if (specialDelegate) {
|
2022-05-02 19:25:50 +00:00
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
|
|
|
if (dragging) {
|
|
|
|
QModelIndex index = indexAt(dragStartPoint);
|
|
|
|
if (index.isValid()) {
|
|
|
|
QRect rect = visualRect(index);
|
|
|
|
if (rect.contains(dragStartPoint)) {
|
|
|
|
QString selected = del->mouseDrag(dragStartPoint, dragEndPoint, index, rect);
|
|
|
|
if (selectedText != selected) {
|
|
|
|
selectedText = selected;
|
2022-05-01 20:19:52 +00:00
|
|
|
setDirtyRegion(rect);
|
|
|
|
}
|
2022-05-02 19:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QModelIndex index = indexAt(dragEndPoint);
|
|
|
|
if (index.isValid()) {
|
|
|
|
QRect rect = visualRect(index);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (rect.contains(dragEndPoint))
|
2022-05-02 19:25:50 +00:00
|
|
|
setAnchorHovered(del->hoverType(dragEndPoint, index, rect));
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-05-02 19:25:50 +00:00
|
|
|
setAnchorHovered(Shared::Hover::nothing);
|
2022-04-28 21:29:44 +00:00
|
|
|
} else {
|
2022-05-02 19:25:50 +00:00
|
|
|
setAnchorHovered(Shared::Hover::nothing);
|
2022-04-28 21:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 21:50:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::mousePressEvent(QMouseEvent* event) {
|
2022-04-27 21:08:59 +00:00
|
|
|
QAbstractItemView::mousePressEvent(event);
|
2022-05-03 09:17:08 +00:00
|
|
|
|
2022-04-27 21:08:59 +00:00
|
|
|
mousePressed = event->button() == Qt::LeftButton;
|
2022-05-01 20:19:52 +00:00
|
|
|
if (mousePressed) {
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
dragStartPoint = event->position().toPoint();
|
|
|
|
#else
|
|
|
|
dragStartPoint = event->localPos().toPoint();
|
|
|
|
#endif
|
2022-05-02 19:25:50 +00:00
|
|
|
if (specialDelegate && specialModel) {
|
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
|
|
|
QString lastSelectedId = del->clearSelection();
|
|
|
|
if (lastSelectedId.size()) {
|
|
|
|
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
|
|
|
QModelIndex index = feed->modelIndexById(lastSelectedId);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (index.isValid())
|
2022-05-02 19:25:50 +00:00
|
|
|
setDirtyRegion(visualRect(index));
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 20:19:52 +00:00
|
|
|
}
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::mouseDoubleClickEvent(QMouseEvent* event) {
|
2022-05-03 09:17:08 +00:00
|
|
|
QAbstractItemView::mouseDoubleClickEvent(event);
|
|
|
|
mousePressed = event->button() == Qt::LeftButton;
|
|
|
|
if (mousePressed) {
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
dragStartPoint = event->position().toPoint();
|
|
|
|
#else
|
|
|
|
dragStartPoint = event->localPos().toPoint();
|
|
|
|
#endif
|
2022-05-03 09:17:08 +00:00
|
|
|
if (specialDelegate && specialModel) {
|
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
|
|
|
QString lastSelectedId = del->clearSelection();
|
|
|
|
selectedText = "";
|
|
|
|
if (lastSelectedId.size()) {
|
|
|
|
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
|
|
|
QModelIndex index = feed->modelIndexById(lastSelectedId);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (index.isValid())
|
2022-05-03 09:17:08 +00:00
|
|
|
setDirtyRegion(visualRect(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex index = indexAt(dragStartPoint);
|
|
|
|
QRect rect = visualRect(index);
|
|
|
|
if (rect.contains(dragStartPoint)) {
|
|
|
|
selectedText = del->leftDoubleClick(dragStartPoint, index, rect);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (selectedText.size() > 0)
|
2022-05-03 09:17:08 +00:00
|
|
|
setDirtyRegion(rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::mouseReleaseEvent(QMouseEvent* event) {
|
2022-04-27 21:08:59 +00:00
|
|
|
QAbstractItemView::mouseReleaseEvent(event);
|
|
|
|
|
2022-05-01 20:19:52 +00:00
|
|
|
if (mousePressed) {
|
|
|
|
if (!dragging && specialDelegate) {
|
2024-12-14 16:08:50 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
2024-12-13 23:53:04 +00:00
|
|
|
QPoint point = event->position().toPoint();
|
2024-12-14 16:08:50 +00:00
|
|
|
#else
|
|
|
|
QPoint point = event->localPos().toPoint();
|
|
|
|
#endif
|
2022-05-01 20:19:52 +00:00
|
|
|
QModelIndex index = indexAt(point);
|
|
|
|
if (index.isValid()) {
|
|
|
|
QRect rect = visualRect(index);
|
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
2023-08-15 15:28:25 +00:00
|
|
|
if (rect.contains(point))
|
2022-05-01 20:19:52 +00:00
|
|
|
del->leftClick(point, index, rect);
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-01 20:19:52 +00:00
|
|
|
dragging = false;
|
|
|
|
mousePressed = false;
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::keyPressEvent(QKeyEvent* event) {
|
2022-05-02 19:25:50 +00:00
|
|
|
QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
|
|
|
|
if (key_event->matches(QKeySequence::Copy)) {
|
|
|
|
if (selectedText.size() > 0) {
|
|
|
|
QClipboard* cb = QApplication::clipboard();
|
|
|
|
cb->setText(selectedText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::resizeEvent(QResizeEvent* event) {
|
2021-04-30 20:07:00 +00:00
|
|
|
QAbstractItemView::resizeEvent(event);
|
2021-05-01 23:03:08 +00:00
|
|
|
|
|
|
|
positionProgress();
|
2021-05-03 00:35:43 +00:00
|
|
|
emit resized();
|
2021-04-30 20:07:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::positionProgress() {
|
2021-05-01 23:03:08 +00:00
|
|
|
QSize layoutBounds = maximumViewportSize();
|
|
|
|
int progressPosition = layoutBounds.height() - progressSize;
|
|
|
|
std::deque<Hint>::size_type size = hints.size();
|
|
|
|
if (size > 0) {
|
|
|
|
const Hint& hint = hints[size - 1];
|
|
|
|
progressPosition -= hint.offset + hint.height;
|
|
|
|
}
|
|
|
|
progressPosition += vo;
|
2021-05-03 11:23:41 +00:00
|
|
|
progressPosition = qMin(progressPosition, 0);
|
2021-05-01 23:03:08 +00:00
|
|
|
|
|
|
|
progress.move((width() - progressSize) / 2, progressPosition);
|
|
|
|
}
|
2021-01-07 21:50:12 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::setItemDelegate(QAbstractItemDelegate* delegate) {
|
2021-02-06 11:02:42 +00:00
|
|
|
if (specialDelegate) {
|
|
|
|
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
|
|
|
disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
|
2021-04-28 20:26:19 +00:00
|
|
|
disconnect(del, &MessageDelegate::invalidPath, this, &FeedView::onMessageInvalidPath);
|
2021-02-06 11:02:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
QAbstractItemView::setItemDelegate(delegate);
|
|
|
|
|
|
|
|
MessageDelegate* del = dynamic_cast<MessageDelegate*>(delegate);
|
|
|
|
if (del) {
|
|
|
|
specialDelegate = true;
|
2022-01-09 14:32:23 +00:00
|
|
|
elementMargin = MessageDelegate::margin;
|
2021-02-06 11:02:42 +00:00
|
|
|
connect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
|
2021-04-28 20:26:19 +00:00
|
|
|
connect(del, &MessageDelegate::invalidPath, this, &FeedView::onMessageInvalidPath);
|
2022-04-28 21:29:44 +00:00
|
|
|
connect(del, &MessageDelegate::openLink, &QDesktopServices::openUrl);
|
2021-02-01 22:55:15 +00:00
|
|
|
} else {
|
|
|
|
specialDelegate = false;
|
2022-01-09 14:32:23 +00:00
|
|
|
elementMargin = 0;
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 11:02:42 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::setModel(QAbstractItemModel* p_model) {
|
2021-04-30 20:07:00 +00:00
|
|
|
if (specialModel) {
|
|
|
|
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
|
|
|
disconnect(feed, &Models::MessageFeed::syncStateChange, this, &FeedView::onModelSyncStateChange);
|
|
|
|
}
|
2021-02-06 11:02:42 +00:00
|
|
|
|
2021-04-30 20:07:00 +00:00
|
|
|
QAbstractItemView::setModel(p_model);
|
|
|
|
|
|
|
|
Models::MessageFeed* feed = dynamic_cast<Models::MessageFeed*>(p_model);
|
2021-02-06 11:02:42 +00:00
|
|
|
if (feed) {
|
2021-04-30 20:07:00 +00:00
|
|
|
onModelSyncStateChange(feed->getSyncState());
|
2021-02-06 11:02:42 +00:00
|
|
|
specialModel = true;
|
2021-04-30 20:07:00 +00:00
|
|
|
connect(feed, &Models::MessageFeed::syncStateChange, this, &FeedView::onModelSyncStateChange);
|
2021-02-06 11:02:42 +00:00
|
|
|
} else {
|
2021-04-30 20:07:00 +00:00
|
|
|
onModelSyncStateChange(Models::MessageFeed::complete);
|
2021-02-06 11:02:42 +00:00
|
|
|
specialModel = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::onMessageButtonPushed(const QString& messageId) {
|
2021-02-06 11:02:42 +00:00
|
|
|
if (specialModel) {
|
|
|
|
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
2021-05-14 19:49:38 +00:00
|
|
|
feed->downloadAttachment(messageId);
|
2021-02-06 11:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-28 20:26:19 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::onMessageInvalidPath(const QString& messageId) {
|
2021-04-28 20:26:19 +00:00
|
|
|
if (specialModel) {
|
|
|
|
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
|
|
|
feed->reportLocalPathInvalid(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
void FeedView::onModelSyncStateChange(Models::MessageFeed::SyncState state) {
|
2021-05-01 23:03:08 +00:00
|
|
|
bool needToUpdateGeometry = false;
|
2021-04-30 20:07:00 +00:00
|
|
|
if (modelState != state) {
|
2021-05-01 23:03:08 +00:00
|
|
|
if (state == Models::MessageFeed::complete || modelState == Models::MessageFeed::complete) {
|
|
|
|
needToUpdateGeometry = true;
|
|
|
|
}
|
2021-04-30 20:07:00 +00:00
|
|
|
modelState = state;
|
|
|
|
|
|
|
|
if (state == Models::MessageFeed::syncing) {
|
|
|
|
progress.show();
|
|
|
|
progress.start();
|
|
|
|
} else {
|
|
|
|
progress.stop();
|
|
|
|
progress.hide();
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 23:03:08 +00:00
|
|
|
|
|
|
|
if (needToUpdateGeometry) {
|
|
|
|
scheduleDelayedItemsLayout();
|
|
|
|
}
|
2021-04-30 20:07:00 +00:00
|
|
|
}
|