fix: 统一输入框编辑能力、修复拖拽合并网格并支持光标吸附数据点
统一文本输入(新增 Utilities/Inc/TextEdit.hpp + TextEdit.cpp) - 一个共享的编辑模型,提供光标/选区/过滤/限长,接入全部输入框: 样式区数值与信号名/分组名、波形图例改名、视图标签页改名、 拾色器 R/G/B 与色号、光标时间与网格上下限。 - 新增能力:Ctrl+A/C/X/V(含剪贴板清洗)、Shift+方向键与 Home/End 选区、 退格与删除清除选区、代理对安全的光标步进。 - 渲染改为选区高亮 + 插入符,文本超出框宽时水平滚动并让插入符始终可见; 初始态为整段选中,首字符键入即整体替换(保持"登录框"式交互)。 - 新增 Utf8Util::PasteTextFromClipboard(换行/制表符折叠为空格)。 修复网格合并(Utilities/Src/DataModel.cpp) - 拖拽信号到其它网格时先固定目标网格的锚信号,再移除源信号并按引用找回目标, 避免空网格回收导致下标错位,从"与原网格交换位置"改为真正合并。 - 自定义划分下未分配网格的可见信号改为各自独占一个新网格, 修复"拖拽合并后再勾选新信号被并入该合并网格"的问题。 绘图区(Components/PlotView) - 网格右键菜单新增"时间轴自适应(匹配本网格)",把时间轴对齐到该网格信号范围。 - 时间轴放大到采样点在屏幕上可分辨(>=6 逻辑像素)时,拖动光标吸附到最近数据点; 缩小到无法分辨时保持自由移动,避免光标"跳格"。 验证:真实输入脚本逐项核对——拖拽合并后单网格含两条曲线、拖拽合并再勾选新信号得 G2 独立网格、 Shift 选区 + Delete 清除、Ctrl+A/C/V 复制粘贴、46 字符文本水平滚动、拾色器色号 1E90FF、 标签页改名 A1、光标时间框复制粘贴、全视图拖动得 0.522213(不吸附)而放大后得 0.28125(= 225 × 0.00125,已吸附)。
This commit is contained in:
1 parent
4e295a2a7d
commit
53768b8b25
16 files changed
+1135
-288
No files matched your search
@@ -25,6 +25,7 @@ namespace jdi
|
||||
const float kPlotSidePadding = 8.0f; /**< 绘图区左右留白(逻辑像素)。 */
|
||||
const float kLaneDividerWidth = 2.0f; /**< 网格之间分界线的宽度。 */
|
||||
const float kSignalHitTolerance = 6.0f; /**< 信号曲线命中容差(逻辑像素)。 */
|
||||
const float kCursorSnapMinPixels = 6.0f; /**< 光标吸附采样点的最小采样间隔(逻辑像素)。 */
|
||||
const float kLaneLabelPad = 6.0f; /**< 网格内侧标尺带的内边距。 */
|
||||
const float kLegendHeight = 19.0f; /**< 图例条目高度(逻辑像素)。 */
|
||||
const float kLegendPad = 5.0f; /**< 图例相对网格左上角的内缩。 */
|
||||
@@ -483,6 +484,137 @@ namespace jdi
|
||||
}
|
||||
}
|
||||
|
||||
bool PlotView::FitTimeToLane(int laneIndex)
|
||||
{
|
||||
if (m_dataSet == nullptr || m_tab == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<Lane>& lanes = m_tab->Lanes();
|
||||
if (laneIndex < 0 || laneIndex >= static_cast<int>(lanes.size()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double low = std::numeric_limits<double>::infinity();
|
||||
double high = -std::numeric_limits<double>::infinity();
|
||||
for (const SignalRef& ref : lanes[static_cast<std::size_t>(laneIndex)].signals)
|
||||
{
|
||||
const DataFile* file = m_dataSet->FileAt(ref.fileIndex);
|
||||
if (file == nullptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
low = std::min(low, file->minTime);
|
||||
high = std::max(high, file->maxTime);
|
||||
}
|
||||
|
||||
if (!std::isfinite(low) || !std::isfinite(high) || !(high > low))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Tab().ViewMin() = low;
|
||||
Tab().ViewMax() = high;
|
||||
Tab().ViewInitialized() = true;
|
||||
// 与“适应视图”保持一致:退出选区缩放模式,避免模式按钮与实际状态不符。
|
||||
Tab().ZoomMode() = PlotZoomMode::NONE;
|
||||
m_rubberRect = gd::RectF();
|
||||
BuildLaneVisuals();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlotView::SampleSpacingPixels(const DataFile& file, double time, double& pixels) const
|
||||
{
|
||||
pixels = 0.0;
|
||||
|
||||
const std::vector<double>& samples = file.time;
|
||||
if (samples.size() < 2 || m_plotRect.Width <= 1.0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const double viewSpan = Tab().ViewMax() - Tab().ViewMin();
|
||||
if (!(viewSpan > 0.0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const int index = FindNearestSample(file, time);
|
||||
if (index < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double spacing = 0.0;
|
||||
if (index > 0)
|
||||
{
|
||||
spacing = samples[static_cast<std::size_t>(index)] - samples[static_cast<std::size_t>(index) - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
spacing = samples[1] - samples[0];
|
||||
}
|
||||
// 时间轴非单调时前后差值可能为负,此时不做吸附。
|
||||
if (!(spacing > 0.0) || !std::isfinite(spacing))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pixels = spacing / viewSpan * static_cast<double>(m_plotRect.Width);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PlotView::SnapTimeToSample(double time, double& snapped) const
|
||||
{
|
||||
if (m_dataSet == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const double minPixels = static_cast<double>(kCursorSnapMinPixels) * static_cast<double>(m_scale);
|
||||
|
||||
bool found = false;
|
||||
double bestDistance = std::numeric_limits<double>::infinity();
|
||||
double bestTime = 0.0;
|
||||
|
||||
for (const DataFile& file : m_dataSet->Files())
|
||||
{
|
||||
// 只有采样点在屏幕上足够稀疏时才吸附,否则缩小视图后光标会“跳格”。
|
||||
double pixels = 0.0;
|
||||
if (!SampleSpacingPixels(file, time, pixels) || pixels < minPixels)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const int index = FindNearestSample(file, time);
|
||||
if (index < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const double candidate = file.time[static_cast<std::size_t>(index)];
|
||||
if (!std::isfinite(candidate))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const double distance = std::fabs(candidate - time);
|
||||
if (distance < bestDistance)
|
||||
{
|
||||
bestDistance = distance;
|
||||
bestTime = candidate;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
snapped = bestTime;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
bool PlotView::ToggleCursor()
|
||||
{
|
||||
if (Tab().CursorEnabled())
|
||||
@@ -830,7 +962,7 @@ namespace jdi
|
||||
|
||||
const float height = 22.0f * m_scale;
|
||||
float width = std::max(90.0f * m_scale,
|
||||
UiMeasureTextWidth(m_fonts->monoSmall, m_editText) + 24.0f * m_scale);
|
||||
UiMeasureTextWidth(m_fonts->monoSmall, m_edit.Text()) + 24.0f * m_scale);
|
||||
|
||||
if (m_editTarget == PlotEditTarget::CURSOR_TIME)
|
||||
{
|
||||
@@ -878,8 +1010,11 @@ namespace jdi
|
||||
}
|
||||
m_editTarget = target;
|
||||
m_editLane = laneIndex;
|
||||
m_editText = initialText;
|
||||
m_editFresh = true;
|
||||
|
||||
// 数值字段限制为数字与小数点,信号名可输入任意字符(保留括号与空格)。
|
||||
const bool nameField = (target == PlotEditTarget::SIGNAL_NAME);
|
||||
m_edit.Begin(initialText, true, nameField ? EditFilter::ANY : EditFilter::NUMBER,
|
||||
nameField ? 96u : 18u);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -907,14 +1042,13 @@ namespace jdi
|
||||
const PlotEditTarget target = m_editTarget;
|
||||
const int laneIndex = m_editLane;
|
||||
const SignalRef editSignal = m_editSignal;
|
||||
const std::wstring text = Trim(m_editText);
|
||||
const std::wstring text = Trim(m_edit.Text());
|
||||
|
||||
m_editTarget = PlotEditTarget::NONE;
|
||||
m_editLane = -1;
|
||||
m_editFresh = false;
|
||||
m_editSignal = SignalRef();
|
||||
m_editFieldRect = gd::RectF();
|
||||
m_editText.clear();
|
||||
m_edit.End();
|
||||
|
||||
// 信号改名:交给宿主窗口落到数据层,绘图区只持有只读数据。
|
||||
if (target == PlotEditTarget::SIGNAL_NAME)
|
||||
@@ -1002,72 +1136,40 @@ namespace jdi
|
||||
return false;
|
||||
}
|
||||
|
||||
// 剪贴板与全选快捷键经 WM_CHAR 送达(Ctrl+C/X/V/A 分别对应 3/24/22/1)。
|
||||
switch (character)
|
||||
{
|
||||
case 0x03: // Ctrl+C
|
||||
m_edit.CopyToClipboard(m_host);
|
||||
return false;
|
||||
case 0x18: // Ctrl+X
|
||||
return m_edit.CutToClipboard(m_host);
|
||||
case 0x16: // Ctrl+V
|
||||
return m_edit.PasteFromClipboard(m_host);
|
||||
case 0x01: // Ctrl+A
|
||||
m_edit.SelectAll();
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (character == L'\r' || character == L'\n')
|
||||
{
|
||||
return EndEdit(true);
|
||||
}
|
||||
if (character == L'\b')
|
||||
{
|
||||
m_editFresh = false;
|
||||
if (!m_editText.empty())
|
||||
{
|
||||
m_editText.pop_back();
|
||||
}
|
||||
return true;
|
||||
return m_edit.Backspace();
|
||||
}
|
||||
|
||||
// 信号改名接受任意可见字符(保留中文、空格与括号等原始信息)。
|
||||
if (m_editTarget == PlotEditTarget::SIGNAL_NAME)
|
||||
if (character == 0x7F)
|
||||
{
|
||||
if (character < L' ')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (m_editFresh)
|
||||
{
|
||||
m_editText.clear();
|
||||
m_editFresh = false;
|
||||
}
|
||||
if (m_editText.size() < 96)
|
||||
{
|
||||
m_editText.push_back(character);
|
||||
}
|
||||
return true;
|
||||
return m_edit.DeleteForward();
|
||||
}
|
||||
|
||||
const bool digit = (character >= L'0' && character <= L'9');
|
||||
const bool special = (character == L'.' || character == L'-' || character == L'+' || character == L'e' ||
|
||||
character == L'E');
|
||||
if (!digit && !special)
|
||||
if (character < 0x20)
|
||||
{
|
||||
// 数值输入只接受数字与科学计数法符号,其余字符吞掉。
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 首次键入直接替换原有数值,无需先手动删除。
|
||||
if (m_editFresh)
|
||||
{
|
||||
m_editText.clear();
|
||||
m_editFresh = false;
|
||||
}
|
||||
|
||||
if (m_editText.size() < 16)
|
||||
{
|
||||
if (special && character == L'.' &&
|
||||
(m_editText.find(L'.') != std::wstring::npos ||
|
||||
m_editText.find(L'e') != std::wstring::npos ||
|
||||
m_editText.find(L'E') != std::wstring::npos))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ((character == L'-' || character == L'+') && !m_editText.empty() &&
|
||||
m_editText.back() != L'e' && m_editText.back() != L'E')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
m_editText.push_back(character);
|
||||
}
|
||||
return true;
|
||||
return m_edit.Insert(character);
|
||||
}
|
||||
|
||||
bool PlotView::OnEditKey(WPARAM key)
|
||||
@@ -1077,6 +1179,9 @@ namespace jdi
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool shift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
|
||||
const bool control = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
|
||||
|
||||
switch (key)
|
||||
{
|
||||
case VK_RETURN:
|
||||
@@ -1084,10 +1189,21 @@ namespace jdi
|
||||
case VK_ESCAPE:
|
||||
return EndEdit(false);
|
||||
case VK_BACK:
|
||||
m_editFresh = false;
|
||||
if (!m_editText.empty())
|
||||
return m_edit.Backspace();
|
||||
case VK_DELETE:
|
||||
return m_edit.DeleteForward();
|
||||
case VK_LEFT:
|
||||
return m_edit.MoveCaret(-1, shift);
|
||||
case VK_RIGHT:
|
||||
return m_edit.MoveCaret(1, shift);
|
||||
case VK_HOME:
|
||||
return m_edit.MoveHome(shift);
|
||||
case VK_END:
|
||||
return m_edit.MoveEnd(shift);
|
||||
case 'A':
|
||||
if (control)
|
||||
{
|
||||
m_editText.pop_back();
|
||||
m_edit.SelectAll();
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@@ -1246,7 +1362,14 @@ namespace jdi
|
||||
}
|
||||
case PlotDragMode::CURSOR_MOVE:
|
||||
{
|
||||
Tab().CursorTime() = XToTime(fx);
|
||||
// 放大到采样点可分辨时吸附到最近的数据点,便于精确对比取值。
|
||||
double target = XToTime(fx);
|
||||
double snapped = target;
|
||||
if (SnapTimeToSample(target, snapped))
|
||||
{
|
||||
target = snapped;
|
||||
}
|
||||
Tab().CursorTime() = target;
|
||||
m_dragMoved = true;
|
||||
m_cursorHover = true;
|
||||
return true;
|
||||
@@ -2190,7 +2313,7 @@ namespace jdi
|
||||
if (m_editTarget == PlotEditTarget::SIGNAL_NAME && m_editSignal == ref)
|
||||
{
|
||||
m_editFieldRect = hit.rect;
|
||||
UiDrawEditField(g, hit.rect, m_editText, m_editFresh, m_fonts->uiSmall, m_scale);
|
||||
UiDrawEditField(g, hit.rect, m_edit, m_fonts->uiSmall, m_scale);
|
||||
cursorX += itemWidth;
|
||||
++drawnCount;
|
||||
continue;
|
||||
@@ -2467,24 +2590,8 @@ namespace jdi
|
||||
return;
|
||||
}
|
||||
|
||||
// 编辑态:强调色外框 + 整段高亮文本,首次键入即整体替换。
|
||||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->cardBg);
|
||||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->accent, 1.6f);
|
||||
|
||||
const std::wstring text = m_editText.empty() ? std::wstring(L" ") : m_editText;
|
||||
if (m_editFresh)
|
||||
{
|
||||
const float textWidth = UiTextWidth(g, text, m_fonts->monoSmall) + 8.0f * m_scale;
|
||||
const gd::RectF highlight(fieldRect.X + (fieldRect.Width - textWidth) * 0.5f,
|
||||
fieldRect.Y + 2.0f * m_scale, textWidth,
|
||||
fieldRect.Height - 4.0f * m_scale);
|
||||
UiFillRoundRect(g, highlight, 2.0f * m_scale, m_theme->accent);
|
||||
UiText(g, text, m_fonts->monoSmall, highlight, m_theme->textOnAccent, TextAlign::CENTER);
|
||||
}
|
||||
else
|
||||
{
|
||||
UiText(g, text + L"|", m_fonts->monoSmall, fieldRect, m_theme->textStrong, TextAlign::CENTER);
|
||||
}
|
||||
// 编辑态:强调色外框 + 选区高亮 + 插入符,首次键入即整体替换(与常规输入框一致)。
|
||||
UiDrawEditField(g, fieldRect, m_edit, m_fonts->monoSmall, m_scale);
|
||||
}
|
||||
|
||||
void PlotView::PaintSignalInteraction(gd::Graphics& g)
|
||||
|
||||
Reference in new issue
Block a user