数据层与视图配置解耦:新增 DataSet(多文件只读数据层)与 TabConfig(每标签页独立配置),
以 SignalRef{fileIndex,signalIndex} 标识全局信号,替换原有单文件 SignalSet。
- 标签页:新增 TabBar 组件,支持新建/切换/关闭标签页;每页独立保存信号勾选、
颜色/线宽/显示方式、网格划分、视图范围与时间光标,互不影响。
- 网格收敛:可见性变更统一经 TabConfig 的 Set*Visible 接口并同步重算网格,
取消勾选后对应的空网格立即回收(修复此前留下空轨道的问题)。
- 多文件分组:左侧面板改为「文件分组 + 信号」树形列表,分组可折叠、可整组勾选,
分组复选框支持全选/部分/未选三态;新加载文件的信号默认未勾选,由用户自行选择。
- 来源标识:信号行、样式编辑区标题、波形图例与光标读数在跨文件时标注来源文件名。
- 交互补充:面板滚动条支持点击跳转与拖拽,切换标签页自动滚动到已选信号。
- 工具栏精简为「打开 CSV/适应视图/放大/缩小/合并网格/每信号一格/全选信号/清空勾选」,
按钮宽度改为按实际文本像素测量;打开对话框支持多选文件,拖放支持批量载入。
- 新增 TestData/[2].CSV 作为跨文件对比测试数据。
308 lines
8.5 KiB
C++
308 lines
8.5 KiB
C++
#include "TabBar.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace jdi
|
|
{
|
|
|
|
namespace
|
|
{
|
|
const float kTabHeight = 32.0f; /**< 标签高度(逻辑像素)。 */
|
|
const float kTopMargin = 4.0f; /**< 标签顶部留白。 */
|
|
const float kLeftPadding = 8.0f; /**< 左侧留白。 */
|
|
const float kTabPadding = 14.0f; /**< 标签文本左右内边距。 */
|
|
const float kCloseSize = 16.0f; /**< 关闭按钮尺寸。 */
|
|
const float kCloseGap = 6.0f; /**< 文本与关闭按钮间距。 */
|
|
const float kAddSize = 24.0f; /**< 新建按钮尺寸。 */
|
|
const float kAddGap = 6.0f; /**< 新建按钮与末个标签的间距。 */
|
|
const float kCorner = 5.0f; /**< 标签圆角半径。 */
|
|
const float kMaxTabWidth = 200.0f; /**< 单个标签最大宽度(逻辑像素)。 */
|
|
}
|
|
|
|
TabBar::TabBar() = default;
|
|
|
|
void TabBar::Setup(const UiFonts* fonts, const Theme* theme)
|
|
{
|
|
m_fonts = fonts;
|
|
m_theme = theme;
|
|
}
|
|
|
|
void TabBar::SetScale(float scale)
|
|
{
|
|
m_scale = (scale > 0.1f) ? scale : 1.0f;
|
|
}
|
|
|
|
void TabBar::SetTabs(const std::vector<std::wstring>& names)
|
|
{
|
|
m_tabs.clear();
|
|
m_tabs.resize(names.size());
|
|
for (std::size_t index = 0; index < names.size(); ++index)
|
|
{
|
|
m_tabs[index].name = names[index];
|
|
}
|
|
if (m_activeIndex >= static_cast<int>(m_tabs.size()))
|
|
{
|
|
m_activeIndex = m_tabs.empty() ? -1 : static_cast<int>(m_tabs.size()) - 1;
|
|
}
|
|
m_hoverIndex = -1;
|
|
m_hoverClose = false;
|
|
}
|
|
|
|
void TabBar::SetActiveIndex(int index)
|
|
{
|
|
m_activeIndex = index;
|
|
}
|
|
|
|
float TabBar::DesiredHeight() const
|
|
{
|
|
return (kTabHeight + kTopMargin) * m_scale;
|
|
}
|
|
|
|
void TabBar::Layout(const gd::RectF& area)
|
|
{
|
|
m_area = area;
|
|
if (m_fonts == nullptr || m_fonts->ui == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
const float tabHeight = kTabHeight * m_scale;
|
|
const float top = area.Y + kTopMargin * m_scale;
|
|
const float closeSize = kCloseSize * m_scale;
|
|
const float maxWidth = kMaxTabWidth * m_scale;
|
|
const float minWidth = 70.0f * m_scale;
|
|
|
|
float cursorX = area.X + kLeftPadding * m_scale;
|
|
|
|
for (TabItem& tab : m_tabs)
|
|
{
|
|
const float textWidth = UiMeasureTextWidth(m_fonts->ui, tab.name);
|
|
float width = kTabPadding * 2.0f * m_scale + textWidth + kCloseGap * m_scale + closeSize;
|
|
width = std::max(minWidth, std::min(width, maxWidth));
|
|
|
|
tab.rect = gd::RectF(cursorX, top, width, tabHeight);
|
|
tab.closeRect = gd::RectF(cursorX + width - kTabPadding * m_scale - closeSize,
|
|
top + (tabHeight - closeSize) * 0.5f, closeSize, closeSize);
|
|
|
|
cursorX += width + kAddGap * m_scale;
|
|
}
|
|
|
|
const float addSize = kAddSize * m_scale;
|
|
m_addRect = gd::RectF(cursorX, top + (tabHeight - addSize) * 0.5f, addSize, addSize);
|
|
}
|
|
|
|
void TabBar::Paint(gd::Graphics& g)
|
|
{
|
|
if (m_theme == nullptr || m_fonts == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
UiFillRect(g, m_area, m_theme->panelBg);
|
|
|
|
const float closeIconSize = 8.0f * m_scale;
|
|
|
|
for (std::size_t index = 0; index < m_tabs.size(); ++index)
|
|
{
|
|
const TabItem& tab = m_tabs[index];
|
|
const bool active = (static_cast<int>(index) == m_activeIndex);
|
|
const bool hovered = (static_cast<int>(index) == m_hoverIndex);
|
|
|
|
std::uint32_t background = m_theme->panelBg;
|
|
if (active)
|
|
{
|
|
background = m_theme->cardBg;
|
|
}
|
|
else if (hovered)
|
|
{
|
|
background = m_theme->hover;
|
|
}
|
|
UiFillRoundRect(g, tab.rect, kCorner * m_scale, background);
|
|
|
|
if (!active)
|
|
{
|
|
UiDrawRoundRect(g, tab.rect, kCorner * m_scale, m_theme->border, 1.0f);
|
|
}
|
|
else
|
|
{
|
|
// 激活页使用顶部强调条指示。
|
|
UiFillRoundRect(g, gd::RectF(tab.rect.X + kCorner * m_scale, tab.rect.Y,
|
|
tab.rect.Width - kCorner * 2.0f * m_scale, 2.5f * m_scale),
|
|
1.0f * m_scale, m_theme->accent);
|
|
}
|
|
|
|
const float textLeft = tab.rect.X + kTabPadding * m_scale;
|
|
const float textWidth = tab.closeRect.X - kCloseGap * m_scale - textLeft;
|
|
const std::wstring label = UiEllipsize(g, tab.name, m_fonts->ui, std::max(8.0f, textWidth));
|
|
UiText(g, label, active ? m_fonts->uiBold : m_fonts->ui,
|
|
gd::RectF(textLeft, tab.rect.Y, std::max(8.0f, textWidth), tab.rect.Height),
|
|
active ? m_theme->textStrong : m_theme->text, TextAlign::LEFT);
|
|
|
|
// 关闭按钮:悬停时显示底色,图标始终可见。
|
|
const bool closeHot = hovered && m_hoverClose;
|
|
if (closeHot)
|
|
{
|
|
UiFillRoundRect(g, tab.closeRect, 3.0f * m_scale, m_theme->pressed);
|
|
}
|
|
const float cx = tab.closeRect.X + tab.closeRect.Width * 0.5f;
|
|
const float cy = tab.closeRect.Y + tab.closeRect.Height * 0.5f;
|
|
const std::uint32_t iconColor =
|
|
closeHot ? m_theme->danger : (active ? m_theme->text : m_theme->textMuted);
|
|
UiDrawLine(g, cx - closeIconSize * 0.5f, cy - closeIconSize * 0.5f, cx + closeIconSize * 0.5f,
|
|
cy + closeIconSize * 0.5f, iconColor, 1.4f * m_scale);
|
|
UiDrawLine(g, cx + closeIconSize * 0.5f, cy - closeIconSize * 0.5f, cx - closeIconSize * 0.5f,
|
|
cy + closeIconSize * 0.5f, iconColor, 1.4f * m_scale);
|
|
}
|
|
|
|
// 新建按钮。
|
|
const std::uint32_t addBg = m_hoverAdd ? m_theme->hover : m_theme->panelBg;
|
|
UiFillRoundRect(g, m_addRect, kCorner * m_scale, addBg);
|
|
UiDrawRoundRect(g, m_addRect, kCorner * m_scale, m_theme->border, 1.0f);
|
|
|
|
const float ax = m_addRect.X + m_addRect.Width * 0.5f;
|
|
const float ay = m_addRect.Y + m_addRect.Height * 0.5f;
|
|
const float armLength = 4.5f * m_scale;
|
|
const std::uint32_t addColor = m_hoverAdd ? m_theme->accent : m_theme->text;
|
|
UiDrawLine(g, ax - armLength, ay, ax + armLength, ay, addColor, 1.6f * m_scale);
|
|
UiDrawLine(g, ax, ay - armLength, ax, ay + armLength, addColor, 1.6f * m_scale);
|
|
|
|
// 底部分隔线。
|
|
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 TabBar::HitTest(int x, int y) const
|
|
{
|
|
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
|
}
|
|
|
|
int TabBar::TabAtPoint(int x, int y) const
|
|
{
|
|
const float fx = static_cast<float>(x);
|
|
const float fy = static_cast<float>(y);
|
|
for (std::size_t index = 0; index < m_tabs.size(); ++index)
|
|
{
|
|
if (UiPointInRect(m_tabs[index].rect, fx, fy))
|
|
{
|
|
return static_cast<int>(index);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool TabBar::OnMouseMove(int x, int y)
|
|
{
|
|
const int index = TabAtPoint(x, y);
|
|
const bool overClose =
|
|
(index >= 0 && UiPointInRect(m_tabs[static_cast<std::size_t>(index)].closeRect,
|
|
static_cast<float>(x), static_cast<float>(y)));
|
|
const bool overAdd = UiPointInRect(m_addRect, static_cast<float>(x), static_cast<float>(y));
|
|
|
|
if (index == m_hoverIndex && overClose == m_hoverClose && overAdd == m_hoverAdd)
|
|
{
|
|
return false;
|
|
}
|
|
m_hoverIndex = index;
|
|
m_hoverClose = overClose;
|
|
m_hoverAdd = overAdd;
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnLButtonDown(int x, int y)
|
|
{
|
|
m_pressedIndex = -1;
|
|
m_pressedClose = false;
|
|
m_pressedAdd = false;
|
|
|
|
if (UiPointInRect(m_addRect, static_cast<float>(x), static_cast<float>(y)))
|
|
{
|
|
m_pressedAdd = true;
|
|
return true;
|
|
}
|
|
|
|
const int index = TabAtPoint(x, y);
|
|
if (index < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_pressedIndex = index;
|
|
m_pressedClose = UiPointInRect(m_tabs[static_cast<std::size_t>(index)].closeRect,
|
|
static_cast<float>(x), static_cast<float>(y));
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnLButtonUp(int x, int y, TabBarResult& result)
|
|
{
|
|
result = TabBarResult();
|
|
|
|
if (m_pressedAdd)
|
|
{
|
|
m_pressedAdd = false;
|
|
if (UiPointInRect(m_addRect, static_cast<float>(x), static_cast<float>(y)))
|
|
{
|
|
result.type = TabBarResult::Type::ADD;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (m_pressedIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const int index = m_pressedIndex;
|
|
const bool wasClose = m_pressedClose;
|
|
m_pressedIndex = -1;
|
|
m_pressedClose = false;
|
|
|
|
if (TabAtPoint(x, y) != index)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (wasClose)
|
|
{
|
|
result.type = TabBarResult::Type::CLOSE;
|
|
result.index = index;
|
|
}
|
|
else
|
|
{
|
|
result.type = TabBarResult::Type::SELECT;
|
|
result.index = index;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnRButtonDown(int x, int y, int screenX, int screenY, TabBarResult& result)
|
|
{
|
|
result = TabBarResult();
|
|
|
|
const int index = TabAtPoint(x, y);
|
|
if (index < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
result.type = TabBarResult::Type::CONTEXT;
|
|
result.index = index;
|
|
result.screenX = screenX;
|
|
result.screenY = screenY;
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnMouseLeave()
|
|
{
|
|
if (m_hoverIndex < 0 && !m_hoverClose && !m_hoverAdd)
|
|
{
|
|
return false;
|
|
}
|
|
m_hoverIndex = -1;
|
|
m_hoverClose = false;
|
|
m_hoverAdd = false;
|
|
return true;
|
|
}
|
|
|
|
} // namespace jdi
|