Skip to content

Commit

Permalink
Fix legends, axes, and titles placement from recent regression
Browse files Browse the repository at this point in the history
Need to downscale from the current zoom scaling also, not just the DPI scaling.
  • Loading branch information
Blake-Madden committed Jul 14, 2024
1 parent fa416d5 commit eade9aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
25 changes: 12 additions & 13 deletions src/base/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Wisteria::GraphItems
{
//-----------------------------------------
void Axis::AdjustLabelSizeIfUsingBackgroundColor(Label& axisLabel,
wxDC& dc,
const bool useMaxWidth) const
{
if (GetFontBackgroundColor().IsOk() &&
Expand All @@ -26,7 +25,7 @@ namespace Wisteria::GraphItems
if (useMaxWidth)
{
axisLabel.SetMinimumUserSizeDIPs(
dc.ToDIP(m_maxLabelWidth), std::nullopt);
DownscaleFromScreenAndCanvas(m_maxLabelWidth), std::nullopt);
}
axisLabel.SetPadding(std::max(4, GetTopPadding()),
std::max(4, GetRightPadding()),
Expand All @@ -36,8 +35,8 @@ namespace Wisteria::GraphItems
{
if (useMaxWidth)
{
axisLabel.SetMinimumUserSizeDIPs(
std::nullopt, dc.ToDIP(m_maxLabelWidth));
axisLabel.SetMinimumUserSizeDIPs(std::nullopt,
DownscaleFromScreenAndCanvas(m_maxLabelWidth));
}
axisLabel.SetPadding(std::max(4, GetTopPadding()),
std::max(4, GetRightPadding()),
Expand Down Expand Up @@ -1956,9 +1955,9 @@ namespace Wisteria::GraphItems
GetParallelLabelAlignment() == RelativeAlignment::FlushRight) &&
!(axisPtIter == GetAxisPoints().cend()-1 &&
GetParallelLabelAlignment() == RelativeAlignment::FlushLeft))
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, true); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, true); }
else
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, false); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, false); }

if (GetAxisLabelOrientation() == AxisLabelOrientation::Perpendicular)
{
Expand Down Expand Up @@ -2224,9 +2223,9 @@ namespace Wisteria::GraphItems
GetParallelLabelAlignment() == RelativeAlignment::FlushRight) &&
!(axisPtIter == GetAxisPoints().cend()-1 &&
GetParallelLabelAlignment() == RelativeAlignment::FlushLeft))
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, true); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, true); }
else
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, false); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, false); }

if (GetAxisLabelOrientation() == AxisLabelOrientation::Parallel)
{
Expand Down Expand Up @@ -2694,7 +2693,7 @@ namespace Wisteria::GraphItems
Font(GetFont()).DPIScaling(GetDPIScaleFactor()).
Padding(GetTopPadding(), GetRightPadding(),
GetBottomPadding(), GetLeftPadding()) );
AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, true);
AdjustLabelSizeIfUsingBackgroundColor(axisLabel, true);

for (auto pos = GetAxisPoints().cbegin();
pos != GetAxisPoints().cend();
Expand Down Expand Up @@ -3158,9 +3157,9 @@ namespace Wisteria::GraphItems
// with the standard padding
if (!(axisPos == GetAxisPoints().cbegin() &&
GetParallelLabelAlignment() == RelativeAlignment::FlushRight))
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, true); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, true); }
else
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, false); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, false); }
return std::make_pair(axisLabel, axisPos->GetValue());
}
}
Expand All @@ -3187,9 +3186,9 @@ namespace Wisteria::GraphItems
// with the standard padding
if (!(axisPos == GetAxisPoints().crbegin() &&
GetParallelLabelAlignment() == RelativeAlignment::FlushLeft))
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, true); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, true); }
else
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, dc, false); }
{ AdjustLabelSizeIfUsingBackgroundColor(axisLabel, false); }
return std::make_pair(axisLabel, axisPos->GetValue());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ namespace Wisteria::GraphItems

/// @brief If labels have a background color, then make their boxes wide enough
/// to fill the entire place available to them.
void AdjustLabelSizeIfUsingBackgroundColor(Label& axisLabel, wxDC& dc,
void AdjustLabelSizeIfUsingBackgroundColor(Label& axisLabel,
const bool useMaxWidth) const;

/** @brief Retrieves the value along the axis from a physical
Expand Down
12 changes: 8 additions & 4 deletions src/base/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ namespace Wisteria
title.SetTextOrientation(Orientation::Vertical);
title.SetAnchorPoint(wxPoint(leftMarginWidth, GetCanvasRect(dc).GetHeight()));
title.SetAnchoring(Anchoring::TopLeftCorner);
title.SetMinimumUserSizeDIPs(std::nullopt, GetCanvasRectDIPs().GetHeight());
title.SetMinimumUserSizeDIPs(
std::nullopt, safe_divide<wxCoord>(GetCanvasRectDIPs().GetHeight(), GetScaling()));
title.SetPageVerticalAlignment(
(title.GetRelativeAlignment() == RelativeAlignment::Centered) ?
PageVerticalAlignment::Centered :
Expand Down Expand Up @@ -578,7 +579,8 @@ namespace Wisteria
title.SetTextOrientation(Orientation::Vertical);
title.SetAnchorPoint(wxPoint(position, GetCanvasRect(dc).GetHeight()));
title.SetAnchoring(Anchoring::BottomLeftCorner);
title.SetMinimumUserSizeDIPs(std::nullopt, GetCanvasRectDIPs().GetHeight());
title.SetMinimumUserSizeDIPs(
std::nullopt, safe_divide<wxCoord>(GetCanvasRectDIPs().GetHeight(), GetScaling()));
title.SetPageVerticalAlignment(
(title.GetRelativeAlignment() == RelativeAlignment::Centered) ?
PageVerticalAlignment::Centered :
Expand Down Expand Up @@ -623,7 +625,8 @@ namespace Wisteria
title.SetScaling(GetScaling());
title.SetAnchorPoint(wxPoint(0, topMarginHeight));
title.SetAnchoring(Anchoring::TopLeftCorner);
title.SetMinimumUserSizeDIPs(GetCanvasRectDIPs().GetWidth(), std::nullopt);
title.SetMinimumUserSizeDIPs(
safe_divide<wxCoord>(GetCanvasRectDIPs().GetWidth(), GetScaling()), std::nullopt);
title.SetPageHorizontalAlignment(
(title.GetRelativeAlignment() == RelativeAlignment::Centered) ?
PageHorizontalAlignment::Centered :
Expand Down Expand Up @@ -668,7 +671,8 @@ namespace Wisteria
title.SetScaling(GetScaling());
title.SetAnchorPoint(wxPoint(0, position));
title.SetAnchoring(Anchoring::BottomLeftCorner);
title.SetMinimumUserSizeDIPs(GetCanvasRectDIPs().GetWidth(), std::nullopt);
title.SetMinimumUserSizeDIPs(
safe_divide<wxCoord>(GetCanvasRectDIPs().GetWidth(), GetScaling()), std::nullopt);
title.SetPageHorizontalAlignment(
(title.GetRelativeAlignment() == RelativeAlignment::Centered) ?
PageHorizontalAlignment::Centered :
Expand Down
21 changes: 11 additions & 10 deletions src/base/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace Wisteria::GraphItems
}

//-------------------------------------------
wxCoord Label::CalcPageVerticalOffset(const wxDC& dc) const
wxCoord Label::CalcPageVerticalOffset() const
{
return !GetMinimumUserHeightDIPs() ?
0 : // if no min height, then no offset needed
Expand All @@ -120,7 +120,7 @@ namespace Wisteria::GraphItems
}

//-------------------------------------------
wxCoord Label::CalcPageHorizontalOffset(const wxDC& dc) const
wxCoord Label::CalcPageHorizontalOffset() const
{
return !GetMinimumUserWidthDIPs() ?
0 : // if no min width, then no offset needed
Expand Down Expand Up @@ -236,16 +236,17 @@ namespace Wisteria::GraphItems
}

// used for page alignment
SetMinimumUserSizeDIPs(dc.ToDIP(rect.GetWidth()), dc.ToDIP(rect.GetHeight()));
SetMinimumUserSizeDIPs(DownscaleFromScreenAndCanvas(rect.GetWidth()),
DownscaleFromScreenAndCanvas(rect.GetHeight()));

GetSize(dc, measuredWidth, measureHeight);
SetCachedContentBoundingBox(
wxRect(wxPoint(rect.GetTopLeft()), wxSize(measuredWidth, measureHeight)));
// if there is a minimum height that is taller than the text, then center
// the text vertically
auto contentRect = GetCachedContentBoundingBox();
contentRect.y += CalcPageVerticalOffset(dc);
contentRect.x += CalcPageHorizontalOffset(dc);
contentRect.y += CalcPageVerticalOffset();
contentRect.x += CalcPageHorizontalOffset();
SetCachedContentBoundingBox(contentRect);

wxRect clippedRect{ rect };
Expand Down Expand Up @@ -356,8 +357,8 @@ namespace Wisteria::GraphItems
// if there is a minimum height that is taller than the text, then center
// the text vertically
auto contentRect = GetCachedContentBoundingBox();
contentRect.y += CalcPageVerticalOffset(dc);
contentRect.x += CalcPageHorizontalOffset(dc);
contentRect.y += CalcPageVerticalOffset();
contentRect.x += CalcPageHorizontalOffset();
SetCachedContentBoundingBox(contentRect);
// wxRect will be 1x1 if created with empty dimensions, so reset that
// if this label is empty
Expand Down Expand Up @@ -1444,7 +1445,7 @@ namespace Wisteria::GraphItems
// if drawing outline, then calculate that also in case the pen width is large
((GetPen().IsOk() && GetGraphItemInfo().IsShowingRightOutline()) ?
ScaleToScreenAndCanvas(GetPen().GetWidth()) : 0);
const wxCoord leftOffset = CalcPageHorizontalOffset(dc) +
const wxCoord leftOffset = CalcPageHorizontalOffset() +
((GetPen().IsOk() && GetGraphItemInfo().IsShowingTopOutline()) ?
ScaleToScreenAndCanvas(GetPen().GetWidth()) : 0);

Expand Down Expand Up @@ -1647,11 +1648,11 @@ namespace Wisteria::GraphItems
const wxCoord spaceBetweenLines = (GetLineCount()-1) *
std::ceil(ScaleToScreenAndCanvas(GetLineSpacing()));

pt.y += CalcPageVerticalOffset(dc) + ScaleToScreenAndCanvas(GetTopPadding()) +
pt.y += CalcPageVerticalOffset() + ScaleToScreenAndCanvas(GetTopPadding()) +
// if drawing outline, then calculate that also in case the pen width is large
((GetPen().IsOk() && GetGraphItemInfo().IsShowingTopOutline()) ?
ScaleToScreenAndCanvas(GetPen().GetWidth()) : 0);
const wxCoord leftOffset = CalcPageHorizontalOffset(dc) +
const wxCoord leftOffset = CalcPageHorizontalOffset() +
((GetPen().IsOk() && GetGraphItemInfo().IsShowingLeftOutline()) ?
ScaleToScreenAndCanvas(GetPen().GetWidth()) : 0);

Expand Down
4 changes: 2 additions & 2 deletions src/base/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ namespace Wisteria::GraphItems
/// @returns The offset from the top if user-defined minimum size is being used it is
/// taller than the measured size.
[[nodiscard]]
wxCoord CalcPageVerticalOffset(const wxDC& dc) const;
wxCoord CalcPageVerticalOffset() const;
/// @returns The offset from the left if user-defined minimum size is being used it is
/// wider than the measured size.
[[nodiscard]]
wxCoord CalcPageHorizontalOffset(const wxDC& dc) const;
wxCoord CalcPageHorizontalOffset() const;

double m_tiltAngle{ 0 };
double m_spacingBetweenLines{ 1 };
Expand Down
4 changes: 2 additions & 2 deletions src/graphs/heatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ namespace Wisteria::Graphs
.Pen(wxNullPen)
.Padding(0, labelRightPadding, 0, 0)
.LabelPageVerticalAlignment(PageVerticalAlignment::Centered));
groupRowLabel->SetMinimumUserSizeDIPs(dc.ToDIP(groupLabelWidth),
dc.ToDIP(boxWidth));
groupRowLabel->SetMinimumUserSizeDIPs(DownscaleFromScreenAndCanvas(groupLabelWidth),
DownscaleFromScreenAndCanvas(boxWidth));
AddObject(std::move(groupRowLabel));
}

Expand Down

0 comments on commit eade9aa

Please sign in to comment.