Files
Just-DataInspector/Components/Toolbar/Src/Toolbar.cpp
T
DeepSeek V4.1 Flash 388d3b6441 feat(ui): 修复光标放大后丢失、选区放大合并与右键放弃、双光标与数据点取值标注
1) 打开「选区 Y 轴自适应」时立即自适应一次
- 之前打开开关(单个网格的菜单项或全局项/工具栏按钮)不会真的改变量程,
  只有当用户再次做时间轴选区放大时才生效,观感上就是"点了没反应"
- ToggleLaneAutoFitToSelection / SetAllLanesAutoFitToSelection(enable, applyNow=true)
  在打开时按当前视图立刻执行一次 AutoRangeLaneToView
- 新增 ApplyAutoFitToSelectionNow / ApplyAllAutoFitToSelectionNow

2) 「按网格时间轴展开」同时对该网格做一次 Y 轴自适应
- FitTimeToLane 在把时间轴展开到本网格信号范围后调用 AutoRangeLaneToView,
  避免出现"时间对了但幅值区间不合适"的波形

3) 修复时间光标在大幅放大后丢失、无法拖动的问题
- 根因:缩放/平移改变 ViewMin/ViewMax 后,光标时间可能落到新视野之外,
  PaintCursor 直接 return → 光标消失,用户报告为"放大到 10ms 一格后拖动光标会直接丢失"
- 新增 ClampCursorToView(),在 ApplyTimeSelection / FitAll / FitTimeToLane /
  平移(PAN) 后把两条光标夹回可见区间;CURSOR_MOVE 也把目标时间夹在 [ViewMin, ViewMax]
- IsOnCursorLine 改为输出命中的光标序号,为双光标拖动做准备

4) 工具栏「时间轴选区」+「Y 轴选区」合并为「选区放大」,并支持右键放弃选区
- 单一 toggle(ToolbarAction::ZOOM_SELECT / PlotZoomMode::SELECT);
  按下后按首次明显位移方向决定轴向:横向=时间轴选区,纵向=该网格 Y 轴选区,
  轴向固定不变,轴向未定时不画选区(避免闪烁)
- 框选过程中按右键 = 放弃当前选区并显示提示浮层;与 Esc 不同,**不退出**选区放大模式
- 为此 WM_RBUTTONUP 在左键仍按下时不 ReleaseCapture / 不清 m_activeTarget,
  否则松左键收不到 OnLButtonUp,m_dragMode 会永远停在 RUBBER_SELECT
- 新增 PlotView 内的提示浮层(m_hintText + PaintHint):切换模式/放弃选区时显示一行说明,
  在网格内再次移动鼠标自动清除,无需定时器
- Esc 语义保持原样:放弃拖拽 + 退出选区放大模式

5) 光标功能改为三态:常态 → 单光标 → 双光标 → 常态
- TabConfig 用 PlotCursorMode{NONE,SINGLE,DOUBLE} + CursorTime/CursorTimeAlt 两个时间;
  CursorEnabled() 变成只读派生;ToggleCursor 删除,改为 CycleCursorMode()
- 单 → 双时第二条光标落在第一条附近(+12% 视图跨度,越界反向)并保证在可见范围内
- 两条光标颜色区分且同族:光标 1 = Solarized 橙 #CB4B16(theme.cursor),
  光标 2 = 青 #2AA198(新增 theme.cursorAlt),光标 2 的顶部把手画成朝上的三角
- 时间读数从绘图区顶部移到**底部**,单/双光标都始终显示(不必悬停才可见)

6) 取消原有的光标数值读数框,改为直接在数据点位置标注取值
- 删除 PaintLaneReadout/PaintLaneReadouts(网格右上角的读数框)
- PaintCursor 在光标与每条可见信号曲线的交点处绘制信号色圆角胶囊(底色=信号色,
  文字按 IsDarkColor 自动取深/浅色),一条信号一个标注,颜色与信号一一对应;
  右侧空间不足时自动标到左侧
- 双光标时以时间较小者为左;**右侧光标**显示"当前值 , ±增量"(增量 = 右 − 左),
  增量为负时用负号前缀,避免出现 "±-" 双符号

验证([1].CSV 11 信号 / VERY_BIG_DATA 1.77GB,窗口 1725x996,125% DPI):
- 三态循环:点 1 次出现单光标、2 次双光标、3 次回到无光标
- 双光标读数:左"426.316 / 427.749",右"538.583 , ±112.267 / 513.726 , ±85.9769"
- 时间读数在底部显示(橙 0.63 / 青 0.7812);第二条光标出现在第一条右侧同一视野内
- 超大文件:放大两次后开启光标仍然可见,拖动从 68.2343 变为 68.1911(原先直接消失)
- 「Y 轴自适应」在放大后的视图中点击立即把量程收到可见区间(300~520)
- 「选区 Y 轴自适应」菜单项点击后立即生效并被勾选,全局项同步勾选
- 「按网格时间轴展开」把时间轴展开回全范围并重新自适应 Y 轴
- 选区放大:横向拖拽只放大时间轴、纵向拖拽只放大 Y 轴;右键放弃后视图不变、提示出现、
  模式仍保持选中;Esc 则同时放弃并退出模式
- 回归:空闲 CPU 0.00%;make clean; make all 无警告无错误
2026-10-08 16:58:55 +08:00

312 lines
8.0 KiB
C++

#include "Toolbar.hpp"
#include <cmath>
#include "Utf8Util.hpp"
namespace jdi
{
namespace
{
const float kButtonHeight = 30.0f; /**< 按钮高度(逻辑像素)。 */
const float kButtonGap = 6.0f; /**< 按钮间距。 */
const float kButtonPadding = 14.0f; /**< 按钮左右内边距。 */
const float kGroupGap = 18.0f; /**< 分组间距。 */
const float kButtonRadius = 5.0f; /**< 按钮圆角半径。 */
const float kSidePadding = 12.0f; /**< 工具栏左右留白。 */
const float kTopPadding = 8.0f; /**< 工具栏上下留白。 */
}
Toolbar::Toolbar() = default;
void Toolbar::Setup(const UiFonts* fonts, const Theme* theme)
{
m_fonts = fonts;
m_theme = theme;
}
void Toolbar::SetScale(float scale)
{
m_scale = (scale > 0.1f) ? scale : 1.0f;
}
void Toolbar::SetDocumentLoaded(bool hasDocument)
{
m_documentLoaded = hasDocument;
}
float Toolbar::DesiredHeight() const
{
return (kButtonHeight + kTopPadding * 2.0f) * m_scale;
}
bool Toolbar::IsEnabled(const Button& button) const
{
return button.needsDocument ? m_documentLoaded : true;
}
void Toolbar::Layout(const gd::RectF& area)
{
m_area = area;
m_buttons.clear();
if (m_fonts == nullptr || m_fonts->ui == nullptr)
{
return;
}
struct Spec
{
ToolbarAction action;
const wchar_t* label;
bool primary;
bool needsDocument;
bool toggle;
};
const Spec specs[] = {
{ToolbarAction::OPEN_FILE, L"导入文件", true, false, false},
{ToolbarAction::FIT_ALL, L"适应视图", false, true, false},
{ToolbarAction::AUTO_FIT_Y, L"Y 轴自适应", false, true, true},
{ToolbarAction::ZOOM_SELECT, L"选区放大", false, true, true},
{ToolbarAction::CURSOR, L"光标", false, true, true},
{ToolbarAction::MERGE_LANES, L"合并网格", false, true, false},
{ToolbarAction::SPLIT_LANES, L"拆分网格", false, true, false},
{ToolbarAction::CLEAR_ALL, L"清空网格", false, true, false},
};
const std::size_t specCount = sizeof(specs) / sizeof(specs[0]);
// 按钮宽度按实际文本像素宽 + 固定内边距计算,避免中文标签被裁切。
const float top = area.Y + kTopPadding * m_scale;
const float height = kButtonHeight * m_scale;
const float horizontalPadding = kButtonPadding * 2.0f * m_scale;
// 先建立全部按钮并测量宽度,再决定“导入文件”固定在左、其余按钮整体靠右。
m_buttons.clear();
m_buttons.reserve(specCount);
for (const Spec& spec : specs)
{
Button button;
button.action = spec.action;
button.label = spec.label;
button.primary = spec.primary;
button.needsDocument = spec.needsDocument;
button.toggle = spec.toggle;
button.width = horizontalPadding + UiMeasureTextWidth(m_fonts->ui, button.label);
m_buttons.push_back(std::move(button));
}
// 首按钮(导入文件)靠左;其余按钮作为整体靠右,并留出一组额外间距。
const float leftX = area.X + kSidePadding * m_scale;
float groupWidth = 0.0f;
for (std::size_t index = 1; index < m_buttons.size(); ++index)
{
groupWidth += m_buttons[index].width;
if (index + 1 < m_buttons.size())
{
groupWidth += kButtonGap * m_scale;
}
}
float cursorX = leftX;
m_buttons[0].rect = gd::RectF(cursorX, top, m_buttons[0].width, height);
const float rightEdge = area.X + area.Width - kSidePadding * m_scale;
cursorX = std::max(leftX + m_buttons[0].width + kGroupGap * m_scale, rightEdge - groupWidth);
for (std::size_t index = 1; index < m_buttons.size(); ++index)
{
m_buttons[index].rect = gd::RectF(cursorX, top, m_buttons[index].width, height);
cursorX += m_buttons[index].width + kButtonGap * m_scale;
}
}
void Toolbar::SetToggleState(ToolbarAction action, bool checked)
{
for (Button& button : m_buttons)
{
if (button.action == action && button.toggle)
{
button.checked = checked;
return;
}
}
}
void Toolbar::Paint(gd::Graphics& g)
{
if (m_theme == nullptr || m_fonts == nullptr)
{
return;
}
UiFillRect(g, m_area, m_theme->toolbarBg);
for (const Button& button : m_buttons)
{
const bool enabled = IsEnabled(button);
std::uint32_t background = 0;
std::uint32_t border = 0;
std::uint32_t textColor = 0;
if (button.primary)
{
background = enabled ? m_theme->accent : MixColor(m_theme->accent, m_theme->toolbarBg, 0.6);
if (button.pressed)
{
background = MixColor(m_theme->accent, 0xFF000000u, 0.12);
}
else if (enabled && button.hover)
{
background = MixColor(m_theme->accent, 0xFFFFFFFFu, 0.12);
}
border = background;
textColor = m_theme->textOnAccent;
}
else
{
// 可切换按钮在选中时用强调色浅底 + 强调描边,与普通按钮区分。
background = (button.toggle && button.checked) ? m_theme->accentSoft : m_theme->toolbarBg;
if (button.pressed)
{
background = m_theme->pressed;
}
else if (enabled && button.hover)
{
background = (button.toggle && button.checked) ? MixColor(m_theme->accentSoft, m_theme->accent,
0.16)
: m_theme->hover;
}
if (!enabled)
{
background = m_theme->toolbarBg;
}
if (button.toggle && button.checked && enabled)
{
border = m_theme->accent;
}
else
{
border = MixColor(m_theme->border, m_theme->toolbarBg, enabled ? 0.0 : 0.5);
}
textColor = enabled ? ((button.toggle && button.checked) ? m_theme->textStrong : m_theme->text)
: m_theme->textMuted;
}
if (background != m_theme->toolbarBg)
{
UiFillRoundRect(g, button.rect, kButtonRadius * m_scale, background);
}
if (!button.primary)
{
UiDrawRoundRect(g, button.rect, kButtonRadius * m_scale, border, 1.0f);
}
UiText(g, button.label, m_fonts->ui, button.rect, textColor, TextAlign::CENTER);
}
// 底部分隔线。
UiDrawLine(g, m_area.X, m_area.Y + m_area.Height - 1.0f, m_area.X + m_area.Width,
m_area.Y + m_area.Height - 1.0f, m_theme->border, 1.0f);
}
bool Toolbar::HitTest(int x, int y) const
{
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
}
bool Toolbar::OnMouseMove(int x, int y)
{
int hoverIndex = -1;
for (std::size_t index = 0; index < m_buttons.size(); ++index)
{
if (UiPointInRect(m_buttons[index].rect, static_cast<float>(x), static_cast<float>(y)))
{
hoverIndex = static_cast<int>(index);
break;
}
}
if (hoverIndex == m_hoverIndex)
{
return false;
}
m_hoverIndex = hoverIndex;
return true;
}
bool Toolbar::OnLButtonDown(int x, int y, ToolbarAction& action)
{
action = ToolbarAction::NONE;
for (std::size_t index = 0; index < m_buttons.size(); ++index)
{
Button& button = m_buttons[index];
if (!UiPointInRect(button.rect, static_cast<float>(x), static_cast<float>(y)))
{
continue;
}
if (!IsEnabled(button))
{
return false;
}
m_pressedIndex = static_cast<int>(index);
button.pressed = true;
action = button.action;
return true;
}
return false;
}
bool Toolbar::OnLButtonUp(int x, int y, ToolbarAction& action)
{
action = ToolbarAction::NONE;
if (m_pressedIndex < 0)
{
return false;
}
const int index = m_pressedIndex;
m_pressedIndex = -1;
if (index >= static_cast<int>(m_buttons.size()))
{
return false;
}
Button& button = m_buttons[static_cast<std::size_t>(index)];
button.pressed = false;
const bool inside = UiPointInRect(button.rect, static_cast<float>(x), static_cast<float>(y));
m_hoverIndex = inside ? index : -1;
if (inside && IsEnabled(button))
{
action = button.action;
}
return true;
}
bool Toolbar::OnMouseLeave()
{
bool changed = false;
if (m_hoverIndex >= 0)
{
m_hoverIndex = -1;
changed = true;
}
if (m_pressedIndex >= 0)
{
if (m_pressedIndex < static_cast<int>(m_buttons.size()))
{
m_buttons[static_cast<std::size_t>(m_pressedIndex)].pressed = false;
}
m_pressedIndex = -1;
changed = true;
}
return changed;
}
} // namespace jdi