Skip to content

Commit

Permalink
chore: move elideText logic to itemdata
Browse files Browse the repository at this point in the history
Log:
  • Loading branch information
Decodetalkers committed Aug 24, 2023
1 parent f1d2099 commit 2bedd95
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 249 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(BIN_NAME dde-clipboard)
project(${BIN_NAME})

#set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
Expand Down
98 changes: 98 additions & 0 deletions dde-clipboard/itemdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "itemdata.h"
#include "constants.h"

#include <QDebug>
#include <QApplication>
#include <QWidget>
#include <QDataStream>

#include <private/qtextengine_p.h>

#include <QLabel>

static inline QString textUriListLiteral() { return QStringLiteral("text/uri-list"); }
Expand Down Expand Up @@ -95,6 +98,7 @@ ItemData::ItemData(const QByteArray &buf)
m_enable = true;
m_iconDataList = info.m_iconDataList;
m_formatMap = info.m_formatMap;
m_text_list = elideText(m_text, QSizeF(ItemWidth - ContentMargin * 2, ItemHeight - ItemTitleHeight), QTextOption::WrapAnywhere, QApplication::font(), Qt::ElideMiddle, 0);
}

QString ItemData::title()
Expand Down Expand Up @@ -179,6 +183,24 @@ const QList<FileIconData> &ItemData::IconDataList()
return m_iconDataList;
}

QSize ItemData::sizeHint(int fontHeight)
{
if (m_type == Text) {
return QSize(ItemWidth, itemHeight(fontHeight) + ItemMargin);
}

return QSize(ItemWidth, ItemHeight + ItemMargin);
}

int ItemData::itemHeight(int fontHeight)
{
if (m_type == Text) {
auto length = m_text_list.length() > 4 ? 4 : m_text_list.length();
return length * fontHeight + ItemTitleHeight + ItemStatusBarHeight + TextContentTopMargin;
}
return ItemHeight;
}

/*!
* \~chinese \name remove
* \~chinese \brief 将当前剪切块数据移除,调用此函数会发出ItemData::destroy的信号
Expand All @@ -201,3 +223,79 @@ const QSize &ItemData::pixSize() const
{
return m_pixSize;
}

QStringList ItemData::elideText(const QString &text, const QSizeF &size,
QTextOption::WrapMode wordWrap, const QFont &font,
Qt::TextElideMode mode, qreal lineHeight, int flags)
{
QTextLayout textLayout(text);

textLayout.setFont(font);

QStringList lines;

elideText(&textLayout, size, wordWrap, mode, lineHeight, flags, &lines);

return lines;
}

void ItemData::elideText(QTextLayout *layout, const QSizeF &size, QTextOption::WrapMode wordWrap,
Qt::TextElideMode mode, qreal lineHeight, int flags, QStringList *lines)
{
qreal height = 0;
QPointF offset(0, 0);

QString text = layout->engine()->hasFormats() ? layout->engine()->block.text() : layout->text();
QTextOption &text_option = *const_cast<QTextOption *>(&layout->textOption());

text_option.setWrapMode(wordWrap);

if (flags & Qt::AlignRight)
text_option.setAlignment(Qt::AlignRight);
else if (flags & Qt::AlignHCenter)
text_option.setAlignment(Qt::AlignHCenter);

// dont paint
layout->engine()->ignoreBidi = true;
layout->beginLayout();

QTextLine line = layout->createLine();

while (line.isValid()) {
height += lineHeight;

if (height + lineHeight > size.height()) {
const QString &end_str = layout->engine()->elidedText(mode, qRound(size.width()), flags, line.textStart());

layout->endLayout();
layout->setText(end_str);

if (layout->engine()->block.docHandle()) {
const_cast<QTextDocument *>(layout->engine()->block.document())->setPlainText(end_str);
}

text_option.setWrapMode(QTextOption::NoWrap);
layout->beginLayout();
line = layout->createLine();
line.setLineWidth(size.width() - 1);
text = end_str;
} else {
line.setLineWidth(size.width());
}

line.setPosition(offset);
offset.setY(offset.y() + lineHeight);

if (lines) {
lines->append(text.mid(line.textStart(), line.textLength()));
}

if (height + lineHeight > size.height())
break;

line = layout->createLine();
}

layout->endLayout();
}

20 changes: 19 additions & 1 deletion dde-clipboard/itemdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <QPixmap>
#include <QUrl>
#include <QDBusArgument>

#include <QTextLayout>
#include "constants.h"
#include "dbus/iteminfo.h"

Expand All @@ -34,7 +34,12 @@ class ItemData : public QObject
const QList<QUrl> &urls(); // 文件链接
const QDateTime &time(); // 复制时间
const QString &text(); // 内容预览
QStringList get_text() const {
return m_text_list;
};
QSize sizeHint(int height);

int itemHeight(int fontHeight);
inline bool dataEnabled() { return m_enable; }
void setDataEnabled(bool enable) { m_enable = enable; }

Expand Down Expand Up @@ -67,13 +72,26 @@ class ItemData : public QObject
*/
void reborn(ItemData *data);

private:
QStringList elideText(const QString &text, const QSizeF &size,
QTextOption::WrapMode wordWrap,
const QFont &font,
Qt::TextElideMode mode,
qreal lineHeight,
int flags = 0);

void elideText(QTextLayout *layout, const QSizeF &size,
QTextOption::WrapMode wordWrap,
Qt::TextElideMode mode, qreal lineHeight,
int flags = 0, QStringList *lines = 0);
private:
QMap<QString, QByteArray> m_formatMap;
DataType m_type = Unknown;
QList<QUrl> m_urls;
QVariant m_variantImage;
QSize m_pixSize;
QString m_text;
QStringList m_text_list;
bool m_enable;
QDateTime m_createTime;
QList<FileIconData> m_iconDataList;
Expand Down
44 changes: 5 additions & 39 deletions dde-clipboard/itemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "itemdelegate.h"
#include "clipboardmodel.h"
#include "itemdata.h"
#include "iteminfo.h"
#include "itemwidget.h"

#include <QPointer>
Expand All @@ -16,24 +13,6 @@

DWIDGET_USE_NAMESPACE

static int caculateTextHeight(int width, int height)
{
static const int textCenterWidth = ItemWidth - ContentMargin * 2;
int extraline = 0;
int over = width % textCenterWidth;
if (over != 0) {
extraline = 1;
}
auto caculated = width / textCenterWidth + extraline;
int toShow = caculated > 4 ? 4 : caculated;
return toShow * height;
}

static int getItemHeight(int width, int height)
{
return ItemTitleHeight + ItemStatusBarHeight + TextContentTopMargin + caculateTextHeight(width, height);
}

ItemDelegate::ItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
Expand All @@ -59,32 +38,19 @@ QWidget *ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem

QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);

QPointer<ItemData> data = index.data().value<QPointer<ItemData>>();
if (data->type() == DataType::Text) {
QString text = data->text().simplified();
int height = option.fontMetrics.height();
int width = option.fontMetrics.horizontalAdvance(text);
return QSize(ItemWidth, ItemMargin + getItemHeight(width, height));
}
return QSize(ItemWidth, ItemHeight + ItemMargin);
int height = option.fontMetrics.height();

return data->sizeHint(height);
}

void ItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

Check warning on line 47 in dde-clipboard/itemdelegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'updateEditorGeometry' is never used.
{
Q_UNUSED(index);
QRect rect = option.rect;
QPointer<ItemData> data = index.data().value<QPointer<ItemData>>();
if (data->type() == DataType::Text) {
QString text = data->text().simplified();
int height = option.fontMetrics.height();
int width = option.fontMetrics.horizontalAdvance(text);
editor->setGeometry(rect.x() + ItemMargin, rect.y(), ItemWidth, getItemHeight(width, height));
return;
}
editor->setGeometry(rect.x() + ItemMargin, rect.y(), ItemWidth, ItemHeight);
int height = option.fontMetrics.height();
editor->setGeometry(rect.x() + ItemMargin, rect.y(), ItemWidth, data->itemHeight(height));
}

bool ItemDelegate::eventFilter(QObject *obj, QEvent *event)

Check warning on line 56 in dde-clipboard/itemdelegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'eventFilter' is never used.
Expand Down
11 changes: 7 additions & 4 deletions dde-clipboard/itemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ItemWidget::ItemWidget(QPointer<ItemData> data, QWidget *parent)
, m_nameLabel(new DLabel(this))
, m_timeLabel(new DLabel(this))
, m_closeButton(new IconButton(this))
, m_contentLabel(new PixmapLabel(this))
, m_contentLabel(new PixmapLabel(data,this))
, m_statusLabel(new DLabel(this))
, m_refreshTimer(new QTimer(this))
{
Expand All @@ -68,12 +68,15 @@ const QString &ItemWidget::text()
return m_data->text();
}

void ItemWidget::setText(const QString &text, const QString &length)
void ItemWidget::setTextShown(const QString &length)
{
QFont font = m_contentLabel->font();

font.setItalic(true);

m_contentLabel->setFont(font);
m_contentLabel->setText(text);

m_contentLabel->setText(true);

m_statusLabel->setText(length);
}
Expand Down Expand Up @@ -290,7 +293,7 @@ void ItemWidget::initData(QPointer<ItemData> data)
setCreateTime(data->time());
switch (data->type()) {
case Text: {
setText(data->text(), data->subTitle());
setTextShown(data->subTitle());
}
break;
case Image: {
Expand Down
2 changes: 1 addition & 1 deletion dde-clipboard/itemwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ItemWidget : public DWidget
* \~chinese \brief 设置剪切块属性的接口
*/
const QString& text();
void setText(const QString &text, const QString &length);
void setTextShown(const QString &length);

void setThumnail(const QPixmap &pixmap/*未经处理的原图*/);
void setThumnail(const FileIconData &data);
Expand Down
Loading

0 comments on commit 2bedd95

Please sign in to comment.