Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized tooltip generation in WBaseWidget #13952

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 50 additions & 41 deletions src/widget/wbasewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,63 +139,72 @@ void WBaseWidget::setControlParameterRightUp(double v) {
}

void WBaseWidget::updateTooltip() {
// If we are in developer mode, update the tooltip.
// Remove leading and trailing whitespace/line breaks from the tooltip.
QString base = baseTooltip().trimmed();

// If we are in developer mode, expand the tooltip.
if (CmdlineArgs::Instance().getDeveloper()) {
QStringList debug;
fillDebugTooltip(&debug);

QString base = baseTooltip();
if (!base.isEmpty()) {
debug.append(QString("Tooltip: \"%1\"").arg(base));
debug.append(QStringLiteral("Tooltip: \"%1\"").arg(base));
}
m_pWidget->setToolTip(debug.join("\n"));
m_pWidget->setToolTip(debug.join(QStringLiteral("\n")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m_pWidget->setToolTip(debug.join(QStringLiteral("\n")));
m_pWidget->setToolTip(debug.join(QChar('\n')));

} else {
m_pWidget->setToolTip(base);
}
}

template <>
template<>
QString toDebugString(const QSizePolicy::Policy& policy) {
switch (policy) {
case QSizePolicy::Fixed:
return "Fixed";
case QSizePolicy::Minimum:
return "Minimum";
case QSizePolicy::Maximum:
return "Maximum";
case QSizePolicy::Preferred:
return "Preferred";
case QSizePolicy::Expanding:
return "Expanding";
case QSizePolicy::MinimumExpanding:
return "MinimumExpanding";
case QSizePolicy::Ignored:
return "Ignored";
default:
break;
}
return QString::number(static_cast<int>(policy));
case QSizePolicy::Fixed:
return QStringLiteral("Fixed");
case QSizePolicy::Minimum:
return QStringLiteral("Minimum");
case QSizePolicy::Maximum:
return QStringLiteral("Maximum");
case QSizePolicy::Preferred:
return QStringLiteral("Preferred");
case QSizePolicy::Expanding:
return QStringLiteral("Expanding");
case QSizePolicy::MinimumExpanding:
return QStringLiteral("MinimumExpanding");
case QSizePolicy::Ignored:
return QStringLiteral("Ignored");
default:
return QString::number(static_cast<int>(policy));
}
}

void WBaseWidget::fillDebugTooltip(QStringList* debug) {
QSizePolicy policy = m_pWidget->sizePolicy();
*debug << QString("ClassName: %1").arg(m_pWidget->metaObject()->className())
<< QString("ObjectName: %1").arg(m_pWidget->objectName())
<< QString("Position: %1").arg(toDebugString(m_pWidget->pos()))
<< QString("SizePolicy: %1,%2").arg(toDebugString(policy.horizontalPolicy()),
toDebugString(policy.verticalPolicy()))
<< QString("Size: %1").arg(toDebugString(m_pWidget->size()))
<< QString("SizeHint: %1").arg(toDebugString(m_pWidget->sizeHint()))
<< QString("MinimumSizeHint: %1").arg(toDebugString(m_pWidget->minimumSizeHint()));

for (const auto& pControlConnection : std::as_const(m_leftConnections)) {
*debug << QString("LeftConnection: %1").arg(pControlConnection->toDebugString());
}
for (const auto& pControlConnection : std::as_const(m_rightConnections)) {
*debug << QString("RightConnection: %1").arg(pControlConnection->toDebugString());
}
for (const auto& pControlConnection : std::as_const(m_connections)) {
*debug << QString("Connection: %1").arg(pControlConnection->toDebugString());
*debug << QStringLiteral("ClassName: %1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is supposed to be an optimization, you may want to consider reserving space as appropriate. This adds quite a lot of elements, so it needs to reallocate at least once, avoiding the second allocation by reserving enough upfront may make sense.

Suggested change
*debug << QStringLiteral("ClassName: %1")
debug->reserve(debug.size() + 7 + m_leftConnections.size() + m_rightConnections.size() + m_connections.size() + 1);
*debug << QStringLiteral("ClassName: %1")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The QStringBuilder does it implicit for us, if we replace the %1 placeholder by normal string concat using +.
However is it worth the efforts in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug is a QList<QString>. I don't see how QStringBuilder is relevant here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean
QStringLiteral("ObjectName: %1").arg(m_pWidget->objectName())
->
QStringLiteral("ObjectName: ") + m_pWidget->objectName()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that makes a meaningful difference for a single argument

.arg(m_pWidget->metaObject()->className())
<< QStringLiteral("ObjectName: %1").arg(m_pWidget->objectName())
<< QStringLiteral("Position: %1")
.arg(toDebugString(m_pWidget->pos()))
<< QStringLiteral("SizePolicy: %1,%2")
.arg(toDebugString(policy.horizontalPolicy()),
toDebugString(policy.verticalPolicy()))
<< QStringLiteral("Size: %1").arg(toDebugString(m_pWidget->size()))
<< QStringLiteral("SizeHint: %1")
.arg(toDebugString(m_pWidget->sizeHint()))
<< QStringLiteral("MinimumSizeHint: %1")
.arg(toDebugString(m_pWidget->minimumSizeHint()));

for (const auto& pControlConnection : m_leftConnections) {
*debug << QStringLiteral("LeftConnection: %1").arg(pControlConnection->toDebugString());
}
for (const auto& pControlConnection : m_rightConnections) {
*debug << QStringLiteral("RightConnection: %1").arg(pControlConnection->toDebugString());
}
for (const auto& pControlConnection : m_connections) {
*debug << QStringLiteral("Connection: %1").arg(pControlConnection->toDebugString());
}
if (m_pDisplayConnection) {
*debug << QString("DisplayConnection: %1").arg(m_pDisplayConnection->toDebugString());
*debug << QStringLiteral("DisplayConnection: %1")
.arg(m_pDisplayConnection->toDebugString());
}
}
2 changes: 1 addition & 1 deletion src/widget/wbasewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WBaseWidget {

virtual void Init();

QWidget* toQWidget() {
QWidget* toQWidget() const {
return m_pWidget;
}

Expand Down
Loading