Skip to content

Commit

Permalink
Debugger: Simplify TooltipManager further
Browse files Browse the repository at this point in the history
A tooltip is always associated to one text editor, no need to store
this relation "externally".

Also rename things a bit for more consistency.

Change-Id: Ie6b18a89e9e36639da1d425c1679efa2eafbfe3d
  • Loading branch information
hjk committed Jan 31, 2025
1 parent 826de8a commit bed86fb
Showing 1 changed file with 73 additions and 63 deletions.
136 changes: 73 additions & 63 deletions src/plugins/debugger/debuggertooltipmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ enum DebuggerTooltipState
Released // Widget shown, engine released
};

class DebuggerToolTipWidget;
class DebuggerToolTip;

class DebuggerToolTipManagerPrivate final : public QObject
{
Expand Down Expand Up @@ -94,8 +94,8 @@ class DebuggerToolTipManagerPrivate final : public QObject

void removeToolTipsForWidget(TextEditorWidget *widget)
{
m_tooltips = Utils::filtered(m_tooltips, [widget](const auto &pair) {
return pair.first == widget;
m_tooltips = Utils::filtered(m_tooltips, [widget](const auto &tooltip) {
return tooltip->editorWidget == widget;
});
}

Expand Down Expand Up @@ -123,12 +123,12 @@ class DebuggerToolTipManagerPrivate final : public QObject

bool debugModeActive() const { return ModeManager::currentModeId() == Constants::MODE_DEBUG; }

DebuggerToolTipWidget *findToolTip(TextEditorWidget *editorWidget,
DebuggerToolTip *findToolTip(TextEditorWidget *editorWidget,
const DebuggerToolTipContext &context);

public:
DebuggerEngine *m_engine;
QList<QPair<QPointer<TextEditorWidget>, QPointer<DebuggerToolTipWidget>>> m_tooltips;
QList<QPointer<DebuggerToolTip>> m_tooltips;
};

// A label that can be dragged to drag something else.
Expand Down Expand Up @@ -352,15 +352,18 @@ class DebuggerToolTipTreeView : public QTreeView
//
/////////////////////////////////////////////////////////////////////////

class DebuggerToolTipWidget : public QWidget
class DebuggerToolTip : public QWidget
{
public:
DebuggerToolTipWidget(DebuggerEngine *engine, const DebuggerToolTipContext &context);
~DebuggerToolTipWidget() override { DEBUG("DESTROY DEBUGGERTOOLTIP WIDGET"); }
DebuggerToolTip(DebuggerEngine *engine,
const DebuggerToolTipContext &context,
TextEditorWidget *editorWidget);

~DebuggerToolTip() override { DEBUG("DESTROY DEBUGGERTOOLTIP WIDGET"); }

void releaseEngine();

void positionShow(const TextEditorWidget *editorWidget);
void positionShow();

void updateTooltip();

Expand Down Expand Up @@ -445,11 +448,13 @@ class DebuggerToolTipWidget : public QWidget
DraggableLabel *titleLabel;
DebuggerToolTipTreeView *treeView;
ToolTipModel model;
QPointer<TextEditorWidget> editorWidget;
};

DebuggerToolTipWidget::DebuggerToolTipWidget(DebuggerEngine *engine,
const DebuggerToolTipContext &context)
: engine(engine), context(context)
DebuggerToolTip::DebuggerToolTip(DebuggerEngine *engine,
const DebuggerToolTipContext &context,
TextEditorWidget *editorWidget)
: engine(engine), context(context), editorWidget(editorWidget)
{
setObjectName("DebuggerTreeViewToolTipWidget: " + context.iname);
setAttribute(Qt::WA_DeleteOnClose);
Expand Down Expand Up @@ -508,14 +513,14 @@ DebuggerToolTipWidget::DebuggerToolTipWidget(DebuggerEngine *engine,
connect(treeView, &QTreeView::expanded, &model, &ToolTipModel::expandNode);
connect(treeView, &QTreeView::collapsed, &model, &ToolTipModel::collapseNode);

connect(treeView, &QTreeView::collapsed, this, &DebuggerToolTipWidget::computeSize,
connect(treeView, &QTreeView::collapsed, this, &DebuggerToolTip::computeSize,
Qt::QueuedConnection);
connect(treeView, &QTreeView::expanded, this, &DebuggerToolTipWidget::computeSize,
connect(treeView, &QTreeView::expanded, this, &DebuggerToolTip::computeSize,
Qt::QueuedConnection);
DEBUG("CREATE DEBUGGERTOOLTIP WIDGET");
}

void DebuggerToolTipWidget::computeSize()
void DebuggerToolTip::computeSize()
{
int columns = 30; // Decoration
int rows = 0;
Expand Down Expand Up @@ -569,6 +574,8 @@ void DebuggerToolTipWidget::computeSize()
treeView->setRootIsDecorated(rootDecorated);
}



/*!
\class Debugger::Internal::DebuggerToolTipContext
Expand Down Expand Up @@ -649,7 +656,7 @@ QDebug operator<<(QDebug d, const DebuggerToolTipContext &c)
// If we are in "Acquired" or "Released", this is an update
// after normal WatchModel update.

void DebuggerToolTipWidget::updateTooltip()
void DebuggerToolTip::updateTooltip()
{
if (!engine) {
setState(Released);
Expand Down Expand Up @@ -677,7 +684,7 @@ void DebuggerToolTipWidget::updateTooltip()
titleLabel->setToolTip(context.toolTip());
}

void DebuggerToolTipWidget::setState(DebuggerTooltipState newState)
void DebuggerToolTip::setState(DebuggerTooltipState newState)
{
bool ok = (state == New && newState == PendingUnshown)
|| (state == New && newState == Acquired)
Expand All @@ -691,7 +698,7 @@ void DebuggerToolTipWidget::setState(DebuggerTooltipState newState)
state = newState;
}

void DebuggerToolTipWidget::releaseEngine()
void DebuggerToolTip::releaseEngine()
{
DEBUG("RELEASE ENGINE: STATE " << state);
if (state == Released)
Expand All @@ -715,7 +722,7 @@ void DebuggerToolTipWidget::releaseEngine()
titleLabel->setText(Tr::tr("%1 (Previous)").arg(context.expression));
}

void DebuggerToolTipWidget::positionShow(const TextEditorWidget *editorWidget)
void DebuggerToolTip::positionShow()
{
// Figure out new position of tooltip using the text edit.
// If the line changed too much, close this tip.
Expand All @@ -730,7 +737,8 @@ void DebuggerToolTipWidget::positionShow(const TextEditorWidget *editorWidget)

const QPoint screenPos = editorWidget->toolTipPosition(cursor) + titleLabel->m_offset;
const QRect toolTipArea = QRect(screenPos, QSize(sizeHint()));
const QRect plainTextArea = QRect(editorWidget->mapToGlobal(QPoint(0, 0)), editorWidget->size());
const QRect plainTextArea = QRect(editorWidget->mapToGlobal(QPoint(0, 0)),
editorWidget->size());
const bool visible = plainTextArea.intersects(toolTipArea);
// DEBUG("DebuggerToolTipWidget::positionShow() " << this << m_context
// << " line: " << line << " plainTextPos " << toolTipArea
Expand Down Expand Up @@ -774,7 +782,7 @@ DebuggerToolTipManager::~DebuggerToolTipManager()
void DebuggerToolTipManagerPrivate::hideAllToolTips()
{
purgeClosedToolTips();
for (const auto &[editor, tooltip] : m_tooltips) {
for (const auto &tooltip : m_tooltips) {
if (tooltip)
tooltip->hide();
}
Expand Down Expand Up @@ -803,10 +811,12 @@ void DebuggerToolTipManagerPrivate::updateVisibleToolTips()
}

TextEditorWidget *editorWidget = toolTipEditor->editorWidget();
QTC_ASSERT(editorWidget, return);

// Reposition and show all tooltips of that file.
for (const auto &[editor, tooltip] : m_tooltips) {
if (editor && editor == editorWidget && tooltip)
tooltip->positionShow(editorWidget);
for (const auto &tooltip : m_tooltips) {
if (tooltip && tooltip->editorWidget == editorWidget)
tooltip->positionShow();
}
}

Expand All @@ -818,7 +828,7 @@ void DebuggerToolTipManager::updateToolTips()

// Stack frame changed: All tooltips of that file acquire the engine,
// all others release (arguable, this could be more precise?)
for (const auto &[editor, tooltip] : d->m_tooltips) {
for (const auto &tooltip : d->m_tooltips) {
if (tooltip)
tooltip->updateTooltip();
}
Expand All @@ -831,13 +841,13 @@ void DebuggerToolTipManager::deregisterEngine()

d->purgeClosedToolTips();

for (const auto &[editor, tooltip] : d->m_tooltips) {
for (const auto &tooltip : d->m_tooltips) {
if (tooltip && tooltip->context.engineType == d->m_engine->objectName())
tooltip->releaseEngine();
}

// FIXME: For now remove all.
for (const auto &[editor, tooltip] : d->m_tooltips) {
for (const auto &tooltip : d->m_tooltips) {
if (tooltip)
tooltip->destroy();
}
Expand All @@ -861,7 +871,7 @@ void DebuggerToolTipManager::closeAllToolTips()

void DebuggerToolTipManagerPrivate::closeAllToolTips()
{
for (const auto &[editor, tooltip] : m_tooltips) {
for (const auto &tooltip : m_tooltips) {
if (tooltip)
tooltip->destroy();
}
Expand All @@ -871,7 +881,7 @@ void DebuggerToolTipManagerPrivate::closeAllToolTips()
void DebuggerToolTipManager::resetLocation()
{
d->purgeClosedToolTips();
for (const auto &[editor, tooltip] : d->m_tooltips) {
for (const auto &tooltip : d->m_tooltips) {
if (tooltip)
tooltip->pin();
}
Expand Down Expand Up @@ -918,44 +928,44 @@ void DebuggerToolTipManagerPrivate::slotTooltipOverrideRequested
context.expression = localVariable->name;
context.iname = localVariable->iname;

DebuggerToolTipWidget *tooltipWidget = findToolTip(editorWidget, context);
DebuggerToolTip *tooltip = findToolTip(editorWidget, context);

if (tooltipWidget) {
if (tooltip) {
DEBUG("REUSING LOCALS TOOLTIP");
tooltipWidget->context.mousePosition = point;
tooltip->context.mousePosition = point;
ToolTip::move(point);
} else {
DEBUG("CREATING LOCALS, WAITING...");
tooltipWidget = new DebuggerToolTipWidget(m_engine, context);
tooltipWidget->setState(Acquired);
m_tooltips.append({editorWidget, tooltipWidget});
ToolTip::show(point, tooltipWidget, editorWidget);
tooltip = new DebuggerToolTip(m_engine, context, editorWidget);
tooltip->setState(Acquired);
m_tooltips.append(tooltip);
ToolTip::show(point, tooltip, editorWidget);
}
DEBUG("SYNC IN STATE" << tooltipWidget->state);
tooltipWidget->updateTooltip();
DEBUG("SYNC IN STATE" << tooltip->state);
tooltip->updateTooltip();

} else {

context.iname = "tooltip." + toHex(context.expression);

DebuggerToolTipWidget *tooltipWidget = findToolTip(editorWidget, context);
DebuggerToolTip *tooltip = findToolTip(editorWidget, context);

if (tooltipWidget) {
if (tooltip) {
//tooltip->destroy();
tooltipWidget->context.mousePosition = point;
tooltip->context.mousePosition = point;
ToolTip::move(point);
DEBUG("UPDATING DELAYED.");
} else {
DEBUG("CREATING DELAYED.");
tooltipWidget = new DebuggerToolTipWidget(m_engine, context);
tooltipWidget->context.mousePosition = point;
m_tooltips.append({editorWidget, tooltipWidget});
tooltipWidget->setState(PendingUnshown);
tooltip = new DebuggerToolTip(m_engine, context, editorWidget);
tooltip->context.mousePosition = point;
m_tooltips.append(tooltip);
tooltip->setState(PendingUnshown);
if (m_engine->canHandleToolTip(context)) {
m_engine->updateItem(context.iname);
} else {
ToolTip::show(point, Tr::tr("Expression too complex"), editorWidget);
tooltipWidget->destroy();
tooltip->destroy();
}
}
}
Expand Down Expand Up @@ -989,7 +999,7 @@ DebuggerToolTipContexts DebuggerToolTipManager::pendingTooltips() const
StackFrame frame = d->m_engine->stackHandler()->currentFrame();
DebuggerToolTipContexts rc;
d->purgeClosedToolTips();
for (const auto &[editor, tooltip] : d->m_tooltips) {
for (const auto &tooltip : d->m_tooltips) {
if (tooltip && tooltip->context.iname.startsWith("tooltip") && tooltip->context.matchesFrame(frame))
rc.push_back(tooltip->context);
}
Expand All @@ -1005,12 +1015,12 @@ bool DebuggerToolTipManagerPrivate::eventFilter(QObject *o, QEvent *e)
const auto me = static_cast<const QMoveEvent *>(e);
const QPoint dist = me->pos() - me->oldPos();
purgeClosedToolTips();
QList<QPointer<DebuggerToolTipWidget>> affectedTooltips;
for (auto &[editor, tooltips] : m_tooltips) {
if (editor && editor->window() == o)
affectedTooltips.append(tooltips);
QList<QPointer<DebuggerToolTip>> affectedTooltips;
for (auto &tooltip : m_tooltips) {
if (tooltip && tooltip->editorWidget && tooltip->editorWidget->window() == o)
affectedTooltips.append(tooltip);
}
for (const QPointer<DebuggerToolTipWidget> &tooltip : std::as_const(affectedTooltips)) {
for (const QPointer<DebuggerToolTip> &tooltip : std::as_const(affectedTooltips)) {
if (tooltip && tooltip->isVisible())
tooltip->move(tooltip->pos() + dist);
}
Expand All @@ -1022,12 +1032,12 @@ bool DebuggerToolTipManagerPrivate::eventFilter(QObject *o, QEvent *e)
const bool isMinimized = static_cast<const QWidget *>(o)->windowState() & Qt::WindowMinimized;
if (wasMinimized != isMinimized) {
purgeClosedToolTips();
QList<QPointer<DebuggerToolTipWidget>> affectedTooltips;
for (auto &[editor, tooltips] : m_tooltips) {
if (editor->window() == o)
affectedTooltips.append(tooltips);
QList<QPointer<DebuggerToolTip>> affectedTooltips;
for (auto &tooltip : m_tooltips) {
if (tooltip && tooltip->editorWidget && tooltip->editorWidget->window() == o)
affectedTooltips.append(tooltip);
}
for (DebuggerToolTipWidget *tooltip : std::as_const(affectedTooltips))
for (DebuggerToolTip *tooltip : std::as_const(affectedTooltips))
tooltip->setVisible(!isMinimized);
}
break;
Expand All @@ -1038,20 +1048,20 @@ bool DebuggerToolTipManagerPrivate::eventFilter(QObject *o, QEvent *e)
return false;
}

DebuggerToolTipWidget *DebuggerToolTipManagerPrivate::findToolTip(TextEditorWidget *editorWidget,
const DebuggerToolTipContext &context)
DebuggerToolTip *DebuggerToolTipManagerPrivate::findToolTip(TextEditorWidget *editorWidget,
const DebuggerToolTipContext &context)
{
for (const auto &[editor, tooltip] : m_tooltips) {
if (editor && editor == editorWidget && tooltip && tooltip->context.isSame(context))
for (const auto &tooltip : m_tooltips) {
if (tooltip && tooltip->editorWidget == editorWidget && tooltip->context.isSame(context))
return tooltip;
}
return nullptr;
}

void DebuggerToolTipManagerPrivate::purgeClosedToolTips()
{
m_tooltips = Utils::filtered(m_tooltips, [](const auto &pair) {
return pair.second;
m_tooltips = Utils::filtered(m_tooltips, [](const QPointer<DebuggerToolTip> &tooltip) {
return bool(tooltip);
});
}

Expand Down

0 comments on commit bed86fb

Please sign in to comment.