Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions panels/dock/taskmanager/textcalculator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2025-2026 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -30,9 +30,7 @@ TextCalculator::TextCalculator(QObject *parent)

TextCalculator::~TextCalculator()
{
if (m_dataModel) {
disconnectDataModelSignals();
}
disconnectDataModelSignals();
}

void TextCalculator::setFont(const QFont &font)
Expand Down Expand Up @@ -113,10 +111,7 @@ void TextCalculator::setEnabled(bool enabled)
if (m_enabled) {
scheduleCalculation();
} else {
m_optimalSingleTextWidth = 0.0;
m_totalWidth = 0;
emit optimalSingleTextWidthChanged();
emit totalWidthChanged();
resetCalculatedWidths();
}
emit enabledChanged();
}
Expand All @@ -137,11 +132,12 @@ void TextCalculator::connectDataModelSignals()
this,
[this](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles) {
const auto titleRole = m_dataModel->roleNames().key("title");
if (roles.contains(titleRole) || roles.isEmpty()) {
scheduleCalculation();
}
});
if (roles.contains(titleRole) || roles.isEmpty()) {
scheduleCalculation();
}
});
connect(m_dataModel, &QAbstractItemModel::modelReset, this, &TextCalculator::onDataModelChanged);
connect(m_dataModel, &QObject::destroyed, this, &TextCalculator::onDataModelDestroyed);
}
}

Expand All @@ -157,17 +153,41 @@ void TextCalculator::onDataModelChanged()
scheduleCalculation();
}

void TextCalculator::onDataModelDestroyed()
{
m_dataModel = nullptr;
emit dataModelChanged();
resetCalculatedWidths();
}

void TextCalculator::scheduleCalculation()
{
if (!complete)
return;

if (!m_enabled || !m_dataModel) {
resetCalculatedWidths();
return;
}
calculateOptimalTextWidth();
}

void TextCalculator::resetCalculatedWidths()
{
const bool optimalWidthWasSet = !qFuzzyIsNull(m_optimalSingleTextWidth);
const bool totalWidthWasSet = !qFuzzyIsNull(m_totalWidth);

m_optimalSingleTextWidth = 0.0;
m_totalWidth = 0.0;

if (optimalWidthWasSet) {
emit optimalSingleTextWidthChanged();
}
if (totalWidthWasSet) {
emit totalWidthChanged();
}
}

qreal TextCalculator::calculateBaselineWidth(int charCount) const
{
if (m_baselineWidthCache.contains(charCount)) {
Expand Down Expand Up @@ -372,11 +392,22 @@ QString TextCalculatorAttached::elidedText() const

void TextCalculatorAttached::setCalculator(TextCalculator *calculator)
{
if (calculator) {
m_calculator = calculator;
connect(calculator, &TextCalculator::optimalSingleTextWidthChanged, this, &TextCalculatorAttached::updateElidedText);
updateElidedText();
if (m_calculator == calculator) {
return;
}

if (m_calculator) {
disconnect(m_calculator, nullptr, this, nullptr);
}

m_calculator = calculator;
emit calculatorChanged();

if (m_calculator) {
connect(m_calculator, &TextCalculator::optimalSingleTextWidthChanged, this, &TextCalculatorAttached::updateElidedText);
}

updateElidedText();
}

TextCalculator *TextCalculatorAttached::calculator()
Expand Down Expand Up @@ -422,7 +453,7 @@ void TextCalculatorAttached::updateElidedText()
if (visibleText.isEmpty()) {
visibleText = {};
}

if (visibleText != m_text) {
m_ellipsisWidth = fontMetrics.horizontalAdvance(QString::fromUtf8("…"));
emit ellipsisWidthChanged();
Expand Down
9 changes: 6 additions & 3 deletions panels/dock/taskmanager/textcalculator.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// SPDX-FileCopyrightText: 2025-2026 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QAbstractItemModel>

Check warning on line 7 in panels/dock/taskmanager/textcalculator.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QAbstractItemModel> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFont>

Check warning on line 8 in panels/dock/taskmanager/textcalculator.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFont> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPointer>

Check warning on line 9 in panels/dock/taskmanager/textcalculator.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPointer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtQml/QtQml>

Check warning on line 10 in panels/dock/taskmanager/textcalculator.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtQml/QtQml> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace dock
{
Expand Down Expand Up @@ -55,7 +56,7 @@
QString m_text;
QString m_elidedText;
qreal m_ellipsisWidth = 0.0;
TextCalculator *m_calculator;
QPointer<TextCalculator> m_calculator;
bool m_initialized = false;
};

Expand Down Expand Up @@ -159,12 +160,14 @@

private slots:
void onDataModelChanged();
void onDataModelDestroyed();
void calculateOptimalTextWidth();

private:
void connectDataModelSignals();
void disconnectDataModelSignals();
void scheduleCalculation();
void resetCalculatedWidths();

qreal calculateBaselineWidth(int charCount) const;
qreal calculateElidedTextWidth(const QString &text, qreal maxWidth) const;
Expand All @@ -178,7 +181,7 @@
qreal m_cellSize;
int m_spacing;
int m_itemPadding;
QAbstractItemModel *m_dataModel;
QPointer<QAbstractItemModel> m_dataModel;
qreal m_remainingSpace;
bool m_enabled;

Expand Down
Loading