some thoughts about fonts, lastInteraction and label into keyDelegate

This commit is contained in:
Blue 2023-01-11 23:45:38 +03:00
parent 2aed8a1209
commit 15fb4bbd62
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
15 changed files with 166 additions and 171 deletions

View file

@ -24,6 +24,7 @@
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <QFontDatabase>
#include "messagedelegate.h"
#include "messagefeed.h"
@ -40,6 +41,8 @@ const std::set<int> FeedView::geometryChangingRoles = {
Models::MessageFeed::Error,
Models::MessageFeed::Date
};
QFont FeedView::dividerFont = QFontDatabase::systemFont(QFontDatabase::TitleFont);
QFontMetrics FeedView::dividerMetrics = QFontMetrics(dividerFont);
FeedView::FeedView(QWidget* parent):
QAbstractItemView(parent),
@ -51,8 +54,6 @@ FeedView::FeedView(QWidget* parent):
clearWidgetsMode(false),
modelState(Models::MessageFeed::complete),
progress(),
dividerFont(),
dividerMetrics(dividerFont),
mousePressed(false),
dragging(false),
hovered(Shared::Hover::nothing),
@ -68,23 +69,11 @@ FeedView::FeedView(QWidget* parent):
progress.setParent(viewport());
progress.resize(progressSize, progressSize);
dividerFont = getFont();
dividerFont.setBold(true);
float ndps = dividerFont.pointSizeF();
if (ndps != -1) {
dividerFont.setPointSizeF(ndps * 1.2);
} else {
dividerFont.setPointSize(dividerFont.pointSize() + 2);
}
}
FeedView::~FeedView()
{
}
FeedView::~FeedView() {}
QModelIndex FeedView::indexAt(const QPoint& point) const
{
QModelIndex FeedView::indexAt(const QPoint& point) const {
int32_t vh = viewport()->height();
uint32_t y = vh - point.y() + vo;
@ -102,12 +91,8 @@ QModelIndex FeedView::indexAt(const QPoint& point) const
return QModelIndex();
}
void FeedView::scrollTo(const QModelIndex& index, QAbstractItemView::ScrollHint hint)
{
}
QRect FeedView::visualRect(const QModelIndex& index) const
{
QRect FeedView::visualRect(const QModelIndex& index) const {
unsigned int row = index.row();
if (!index.isValid() || row >= hints.size()) {
qDebug() << "visualRect for" << row;
@ -119,44 +104,24 @@ QRect FeedView::visualRect(const QModelIndex& index) const
}
}
int FeedView::horizontalOffset() const
{
return 0;
}
QString FeedView::getSelectedText() const{return selectedText;}
bool FeedView::isIndexHidden(const QModelIndex& index) const
{
return false;
}
//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();}
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();
}
void FeedView::rowsInserted(const QModelIndex& parent, int start, int end)
{
void FeedView::rowsInserted(const QModelIndex& parent, int start, int end){
QAbstractItemView::rowsInserted(parent, start, end);
scheduleDelayedItemsLayout();
}
void FeedView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
{
void FeedView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles) {
if (specialDelegate) {
for (int role : roles) {
if (geometryChangingRoles.count(role) != 0) {
@ -168,8 +133,7 @@ void FeedView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottom
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
}
void FeedView::updateGeometries()
{
void FeedView::updateGeometries() {
//qDebug() << "updateGeometries";
QScrollBar* bar = verticalScrollBar();
@ -260,8 +224,7 @@ void FeedView::updateGeometries()
QAbstractItemView::updateGeometries();
}
bool FeedView::tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* m, uint32_t totalHeight)
{
bool FeedView::tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* m, uint32_t totalHeight) {
uint32_t previousOffset = elementMargin;
QDateTime lastDate;
for (int i = 0, size = m->rowCount(); i < size; ++i) {
@ -307,9 +270,7 @@ bool FeedView::tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewIt
return true;
}
void FeedView::paintEvent(QPaintEvent* event)
{
void FeedView::paintEvent(QPaintEvent* event) {
//qDebug() << "paint" << event->rect();
const QAbstractItemModel* m = model();
QWidget* vp = viewport();
@ -388,8 +349,7 @@ void FeedView::paintEvent(QPaintEvent* event)
}
}
void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter)
{
void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter) {
int divisionHeight = dateDeviderMargin * 2 + dividerMetrics.height();
QRect r(QPoint(0, top), QSize(viewport()->width(), divisionHeight));
painter.save();
@ -398,8 +358,7 @@ void FeedView::drawDateDevider(int top, const QDateTime& date, QPainter& painter
painter.restore();
}
void FeedView::verticalScrollbarValueChanged(int value)
{
void FeedView::verticalScrollbarValueChanged(int value) {
vo = verticalScrollBar()->maximum() - value;
positionProgress();
@ -415,8 +374,7 @@ void FeedView::verticalScrollbarValueChanged(int value)
QAbstractItemView::verticalScrollbarValueChanged(vo);
}
void FeedView::setAnchorHovered(Shared::Hover type)
{
void FeedView::setAnchorHovered(Shared::Hover type) {
if (hovered != type) {
hovered = type;
switch (hovered) {
@ -433,8 +391,7 @@ void FeedView::setAnchorHovered(Shared::Hover type)
}
}
void FeedView::mouseMoveEvent(QMouseEvent* event)
{
void FeedView::mouseMoveEvent(QMouseEvent* event) {
if (!isVisible()) {
return;
}
@ -479,8 +436,7 @@ void FeedView::mouseMoveEvent(QMouseEvent* event)
}
}
void FeedView::mousePressEvent(QMouseEvent* event)
{
void FeedView::mousePressEvent(QMouseEvent* event) {
QAbstractItemView::mousePressEvent(event);
mousePressed = event->button() == Qt::LeftButton;
@ -500,8 +456,7 @@ void FeedView::mousePressEvent(QMouseEvent* event)
}
}
void FeedView::mouseDoubleClickEvent(QMouseEvent* event)
{
void FeedView::mouseDoubleClickEvent(QMouseEvent* event) {
QAbstractItemView::mouseDoubleClickEvent(event);
mousePressed = event->button() == Qt::LeftButton;
if (mousePressed) {
@ -530,8 +485,7 @@ void FeedView::mouseDoubleClickEvent(QMouseEvent* event)
}
}
void FeedView::mouseReleaseEvent(QMouseEvent* event)
{
void FeedView::mouseReleaseEvent(QMouseEvent* event) {
QAbstractItemView::mouseReleaseEvent(event);
if (mousePressed) {
@ -551,8 +505,7 @@ void FeedView::mouseReleaseEvent(QMouseEvent* event)
}
}
void FeedView::keyPressEvent(QKeyEvent* event)
{
void FeedView::keyPressEvent(QKeyEvent* event) {
QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
if (key_event->matches(QKeySequence::Copy)) {
if (selectedText.size() > 0) {
@ -562,16 +515,14 @@ void FeedView::keyPressEvent(QKeyEvent* event)
}
}
void FeedView::resizeEvent(QResizeEvent* event)
{
void FeedView::resizeEvent(QResizeEvent* event) {
QAbstractItemView::resizeEvent(event);
positionProgress();
emit resized();
}
void FeedView::positionProgress()
{
void FeedView::positionProgress() {
QSize layoutBounds = maximumViewportSize();
int progressPosition = layoutBounds.height() - progressSize;
std::deque<Hint>::size_type size = hints.size();
@ -585,13 +536,7 @@ void FeedView::positionProgress()
progress.move((width() - progressSize) / 2, progressPosition);
}
QFont FeedView::getFont() const
{
return viewOptions().font;
}
void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
{
void FeedView::setItemDelegate(QAbstractItemDelegate* delegate) {
if (specialDelegate) {
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
@ -613,8 +558,7 @@ void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
}
}
void FeedView::setModel(QAbstractItemModel* p_model)
{
void FeedView::setModel(QAbstractItemModel* p_model) {
if (specialModel) {
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
disconnect(feed, &Models::MessageFeed::syncStateChange, this, &FeedView::onModelSyncStateChange);
@ -633,24 +577,21 @@ void FeedView::setModel(QAbstractItemModel* p_model)
}
}
void FeedView::onMessageButtonPushed(const QString& messageId)
{
void FeedView::onMessageButtonPushed(const QString& messageId) {
if (specialModel) {
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
feed->downloadAttachment(messageId);
}
}
void FeedView::onMessageInvalidPath(const QString& messageId)
{
void FeedView::onMessageInvalidPath(const QString& messageId) {
if (specialModel) {
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
feed->reportLocalPathInvalid(messageId);
}
}
void FeedView::onModelSyncStateChange(Models::MessageFeed::SyncState state)
{
void FeedView::onModelSyncStateChange(Models::MessageFeed::SyncState state) {
bool needToUpdateGeometry = false;
if (modelState != state) {
if (state == Models::MessageFeed::complete || modelState == Models::MessageFeed::complete) {
@ -671,8 +612,3 @@ void FeedView::onModelSyncStateChange(Models::MessageFeed::SyncState state)
scheduleDelayedItemsLayout();
}
}
QString FeedView::getSelectedText() const
{
return selectedText;
}

View file

@ -50,7 +50,6 @@ public:
void setItemDelegate(QAbstractItemDelegate* delegate);
void setModel(QAbstractItemModel * model) override;
QFont getFont() const;
QString getSelectedText() const;
signals:
@ -100,8 +99,8 @@ private:
bool clearWidgetsMode;
Models::MessageFeed::SyncState modelState;
Progress progress;
QFont dividerFont;
QFontMetrics dividerMetrics;
static QFont dividerFont;
static QFontMetrics dividerMetrics;
bool mousePressed;
bool dragging;
Shared::Hover hovered;

View file

@ -24,29 +24,40 @@
#include <QAbstractItemView>
#include <QAbstractTextDocumentLayout>
#include <QTextBlock>
#include <QFontDatabase>
#include <cmath>
#include "messagedelegate.h"
#include "messagefeed.h"
int MessageDelegate::avatarHeight(50);
int MessageDelegate::margin(6);
QFont getFont (QFontDatabase::SystemFont type, bool bold, bool italic) {
QFont font = QFontDatabase::systemFont(type);
if (bold)
font.setBold(true);
if (italic)
font.setItalic(true);
return font;
}
constexpr int textMargin = 2;
constexpr int statusIconSize = 16;
constexpr float nickFontMultiplier = 1.1;
constexpr float dateFontMultiplier = 0.8;
constexpr int bubbleMargin = 6;
constexpr int bubbleBorderRadius = 3;
int MessageDelegate::avatarHeight(50);
int MessageDelegate::margin(6);
bool MessageDelegate::fontsInitialized(false);
QFont MessageDelegate::bodyFont;
QFont MessageDelegate::nickFont;
QFont MessageDelegate::dateFont;
QFontMetrics MessageDelegate::nickMetrics(nickFont);
QFontMetrics MessageDelegate::dateMetrics(dateFont);
MessageDelegate::MessageDelegate(QObject* parent):
QStyledItemDelegate(parent),
bodyFont(),
nickFont(),
dateFont(),
bodyRenderer(new QTextDocument()),
nickMetrics(nickFont),
dateMetrics(dateFont),
buttonHeight(0),
buttonWidth(0),
barHeight(0),
@ -60,7 +71,13 @@ MessageDelegate::MessageDelegate(QObject* parent):
currentId(""),
selection(0, 0)
{
if (!fontsInitialized) {
fontsInitialized = true;
initializeFonts();
}
bodyRenderer->setDocumentMargin(0);
bodyRenderer->setDefaultFont(bodyFont);
QPushButton btn(QCoreApplication::translate("MessageLine", "Download"));
buttonHeight = btn.sizeHint().height();
@ -285,7 +302,6 @@ bool MessageDelegate::needToDrawSender(const QModelIndex& index, const Models::F
}
}
QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QRect messageRect = option.rect.adjusted(bubbleMargin, margin / 2 + bubbleMargin, -(avatarHeight + 3 * margin + bubbleMargin), -(margin + bubbleMargin) / 2);
@ -538,38 +554,6 @@ QString MessageDelegate::clearSelection()
return lastSelectedId;
}
void MessageDelegate::initializeFonts(const QFont& font)
{
bodyFont = font;
nickFont = font;
dateFont = font;
nickFont.setBold(true);
float ndps = nickFont.pointSizeF();
if (ndps != -1) {
nickFont.setPointSizeF(ndps * nickFontMultiplier);
} else {
nickFont.setPointSize(nickFont.pointSize() * nickFontMultiplier);
}
dateFont.setItalic(true);
float dps = dateFont.pointSizeF();
if (dps != -1) {
dateFont.setPointSizeF(dps * dateFontMultiplier);
} else {
dateFont.setPointSize(dateFont.pointSize() * dateFontMultiplier);
}
bodyFont.setKerning(false);
nickMetrics = QFontMetrics(nickFont);
dateMetrics = QFontMetrics(dateFont);
bodyRenderer->setDefaultFont(bodyFont);
Preview::initializeFont(bodyFont);
}
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
{
//qDebug() << event->type();
@ -828,3 +812,11 @@ void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const
}
}
}
void MessageDelegate::initializeFonts () {
bodyFont = getFont(QFontDatabase::GeneralFont, false, false);
nickFont = getFont(QFontDatabase::TitleFont, true, false);
dateFont = getFont(QFontDatabase::SmallestReadableFont, false, true);
nickMetrics = QFontMetrics(nickFont);
dateMetrics = QFontMetrics(dateFont);
}

View file

@ -53,7 +53,6 @@ public:
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
//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;
void endClearWidgets();
void beginClearWidgets();
@ -94,6 +93,9 @@ protected:
protected slots:
void onButtonPushed() const;
private:
static void initializeFonts();
private:
class FeedButton : public QPushButton {
@ -101,12 +103,13 @@ private:
QString messageId;
};
QFont bodyFont;
QFont nickFont;
QFont dateFont;
static bool fontsInitialized;
static QFont bodyFont;
static QFont nickFont;
static QFont dateFont;
QTextDocument* bodyRenderer;
QFontMetrics nickMetrics;
QFontMetrics dateMetrics;
static QFontMetrics nickMetrics;
static QFontMetrics dateMetrics;
int buttonHeight;
int buttonWidth;

View file

@ -18,11 +18,18 @@
#include "preview.h"
#include <QFontDatabase>
constexpr int margin = 6;
constexpr int maxAttachmentHeight = 500;
QFont Preview::font;
QFont getFont () {
QFont font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
font.setBold(true);
return font;
}
QFont Preview::font = getFont();
QFontMetrics Preview::metrics(Preview::font);
Preview::Preview(const QString& pPath, const QSize& pMaxSize, const QPoint& pos, QWidget* pParent):
@ -38,20 +45,12 @@ Preview::Preview(const QString& pPath, const QSize& pMaxSize, const QPoint& pos,
fileReachable(true),
actualPreview(false)
{
initializeElements();
if (fileReachable) {
positionElements();
}
}
void Preview::initializeFont(const QFont& newFont)
{
font = newFont;
font.setBold(true);
metrics = QFontMetrics(font);
}
Preview::~Preview()
{
clean();

View file

@ -48,7 +48,6 @@ public:
bool isFileReachable() const;
QSize size() const;
static void initializeFont(const QFont& newFont);
static QSize constrainAttachSize(QSize src, QSize bounds);
static QSize calculateAttachSize(const QString& path, const QRect& bounds);
static bool canVisualize(const Shared::Global::FileInfo& info);