Skip to content

Commit 312d0f1

Browse files
committed
Factor axis tick calculation into reusable AxisTickInfo helper
Made-with: Cursor
1 parent a53652a commit 312d0f1

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

include/ui/graph_widget.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,20 @@ class GraphWidget : public QWidget {
5858
void leaveEvent(QEvent* event) override;
5959

6060
private:
61+
struct AxisTickInfo {
62+
double min{};
63+
double max{};
64+
double step{};
65+
double start{};
66+
};
67+
6168
void draw_axes(QPainter& painter);
6269
void draw_grid(QPainter& painter);
6370
void draw_graphs(QPainter& painter);
6471
void draw_axis_labels(QPainter& painter);
6572
void update_coordinates(const QPoint& pos);
6673
double calculate_grid_step(double range, int target_lines) const;
74+
AxisTickInfo make_tick_info(double min, double max, int target_lines) const;
6775

6876
graph::Graph* m_graph;
6977
graph::GraphConfig m_config;

src/ui/graph_widget.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,11 @@ void GraphWidget::draw_grid(QPainter& painter) {
287287
painter.setPen(QPen(grid_color, 1, Qt::SolidLine));
288288

289289
// Calculate appropriate grid spacing based on viewport
290-
double x_range = x_max - x_min;
291-
double y_range = y_max - y_min;
292-
293-
// Smart grid spacing - adapt to zoom level
294-
double x_step = calculate_grid_step(x_range, m_config.grid_spacing);
295-
double y_step = calculate_grid_step(y_range, m_config.grid_spacing);
290+
AxisTickInfo x_ticks = make_tick_info(x_min, x_max, m_config.grid_spacing);
291+
AxisTickInfo y_ticks = make_tick_info(y_min, y_max, m_config.grid_spacing);
296292

297293
// Vertical grid lines
298-
double x_start = std::floor(x_min / x_step) * x_step;
299-
for (double x = x_start; x <= x_max; x += x_step) {
294+
for (double x = x_ticks.start; x <= x_ticks.max; x += x_ticks.step) {
300295
if (std::abs(x) < 1e-10) continue; // Skip origin (drawn as axis)
301296
int screen_x, screen_y;
302297
viewport.world_to_screen(x, y_min, width(), height(), screen_x, screen_y);
@@ -306,8 +301,7 @@ void GraphWidget::draw_grid(QPainter& painter) {
306301
}
307302

308303
// Horizontal grid lines
309-
double y_start = std::floor(y_min / y_step) * y_step;
310-
for (double y = y_start; y <= y_max; y += y_step) {
304+
for (double y = y_ticks.start; y <= y_ticks.max; y += y_ticks.step) {
311305
if (std::abs(y) < 1e-10) continue; // Skip origin (drawn as axis)
312306
int screen_x, screen_y;
313307
viewport.world_to_screen(x_min, y, width(), height(), screen_x, screen_y);
@@ -337,6 +331,22 @@ double GraphWidget::calculate_grid_step(double range, int target_lines) const {
337331
}
338332
}
339333

334+
GraphWidget::AxisTickInfo GraphWidget::make_tick_info(double min, double max, int target_lines) const {
335+
AxisTickInfo info;
336+
info.min = min;
337+
info.max = max;
338+
const double range = max - min;
339+
if (range <= 0.0 || target_lines <= 0) {
340+
info.step = 1.0;
341+
info.start = min;
342+
return info;
343+
}
344+
345+
info.step = calculate_grid_step(range, target_lines);
346+
info.start = std::floor(min / info.step) * info.step;
347+
return info;
348+
}
349+
340350
void GraphWidget::draw_axes(QPainter& painter) {
341351
if (!m_graph || !m_config.show_axes) return;
342352

@@ -380,14 +390,13 @@ void GraphWidget::draw_axis_labels(QPainter& painter) {
380390

381391
// X-axis labels
382392
if (y_min <= 0 && 0 <= y_max) {
383-
double x_step = calculate_grid_step(x_max - x_min, 8);
384-
double x_start = std::floor(x_min / x_step) * x_step;
385-
for (double x = x_start; x <= x_max; x += x_step) {
393+
AxisTickInfo x_ticks = make_tick_info(x_min, x_max, 8);
394+
for (double x = x_ticks.start; x <= x_ticks.max; x += x_ticks.step) {
386395
if (std::abs(x) < 1e-10) continue; // Skip origin
387396
int screen_x, screen_y;
388397
viewport.world_to_screen(x, 0, width(), height(), screen_x, screen_y);
389398
if (screen_x >= 0 && screen_x <= width()) {
390-
QString label = QString::number(x, 'f', x_step < 1.0 ? 1 : 0);
399+
QString label = QString::number(x, 'f', x_ticks.step < 1.0 ? 1 : 0);
391400
QFontMetrics fm(font);
392401
QRect text_rect = fm.boundingRect(label);
393402
text_rect.moveCenter(QPoint(screen_x, screen_y + 15));
@@ -398,14 +407,13 @@ void GraphWidget::draw_axis_labels(QPainter& painter) {
398407

399408
// Y-axis labels
400409
if (x_min <= 0 && 0 <= x_max) {
401-
double y_step = calculate_grid_step(y_max - y_min, 8);
402-
double y_start = std::floor(y_min / y_step) * y_step;
403-
for (double y = y_start; y <= y_max; y += y_step) {
410+
AxisTickInfo y_ticks = make_tick_info(y_min, y_max, 8);
411+
for (double y = y_ticks.start; y <= y_ticks.max; y += y_ticks.step) {
404412
if (std::abs(y) < 1e-10) continue; // Skip origin
405413
int screen_x, screen_y;
406414
viewport.world_to_screen(0, y, width(), height(), screen_x, screen_y);
407415
if (screen_y >= 0 && screen_y <= height()) {
408-
QString label = QString::number(y, 'f', y_step < 1.0 ? 1 : 0);
416+
QString label = QString::number(y, 'f', y_ticks.step < 1.0 ? 1 : 0);
409417
QFontMetrics fm(font);
410418
QRect text_rect = fm.boundingRect(label);
411419
text_rect.moveCenter(QPoint(screen_x - 15, screen_y));

0 commit comments

Comments
 (0)