From 5b81d51fd69a6c90634d3f5f277845e81b274d3f Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 10 Jul 2017 14:14:28 -0400 Subject: [PATCH] Made graph work better with dark themes and prevent lines from going outside graph. --- src/graph.cpp | 39 ++++++++++++++++++++++++++++----------- src/graph.h | 8 +++++--- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/graph.cpp b/src/graph.cpp index 433cc3f..ce47a41 100644 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -25,15 +25,22 @@ void Graph::update_vals(vector &sensor_readings) { // https://developer.gnome.org/gtkmm-tutorial/stable/chapter-drawingarea.html.en bool Graph::on_draw(const Cairo::RefPtr &cr) { + // Canvas and graph size Gtk::Allocation allocation = get_allocation(); width = allocation.get_width(); height = allocation.get_height(); - draw_title(cr); graph_width = width - graph_x_start - right_padding; graph_height = height - graph_y_start - scale_offset; + + // Grab text color from theme + auto sc = this->get_style_context(); + text_color = sc->get_color(); + + // Draw it + draw_title(cr); check_resize(); draw_graph_grid(cr); - make_plot(cr); + draw_plot(cr); return true; } @@ -47,9 +54,17 @@ const unsigned int Graph::scale_val(unsigned int raw_val) const { const bool Graph::update() { // update values for (unsigned int i = 0; i < sensor_readings.size(); i++) { - const unsigned int raw_val = - (sensor_readings[i].cur_val >= 0) ? sensor_readings[i].cur_val : 0; + int raw_val; + // Make sure raw_val fits on graph; + if(sensor_readings[i].cur_val < 0) { + raw_val = 0; + } else if(sensor_readings[i].cur_val > Device::sensor_max_vals[type]) { + raw_val = Device::sensor_max_vals[type]; + } else { + raw_val = sensor_readings[i].cur_val; + } const unsigned int scaled_val = scale_val(raw_val); + raw_vals[i].push_front(raw_val); scaled_vals[i].push_front(scaled_val); if (scaled_vals[i].size() > ticks + 1) { @@ -61,8 +76,7 @@ const bool Graph::update() { // Refresh window auto win = get_window(); if (win) { - Gdk::Rectangle r(graph_x_start, graph_y_start, graph_x_start + width, - graph_y_start + height); + Gdk::Rectangle r(graph_x_start, graph_y_start, graph_width, graph_height); win->invalidate_rect(r, false); } return true; @@ -85,9 +99,8 @@ void Graph::check_resize() { void Graph::draw_title(const Cairo::RefPtr &cr) { const int left_offset = 10; const int top_offset = 10; - const double title_color = 0.4; cr->move_to(left_offset, top_offset); - cr->set_source_rgb(title_color, title_color, title_color); + Gdk::Cairo::set_source_rgba(cr, text_color); Pango::FontDescription font; font.set_weight(Pango::WEIGHT_BOLD); @@ -98,6 +111,11 @@ void Graph::draw_title(const Cairo::RefPtr &cr) { } void Graph::draw_graph_grid(const Cairo::RefPtr &cr) { + // Paint the background white + cr->set_source_rgb(1, 1, 1); + cr->rectangle(graph_x_start, graph_y_start, graph_width, graph_height); + cr->fill(); + // See https://www.cairographics.org/FAQ/#sharp_lines for the 0.5's here cr->set_line_width(line_width); const unsigned int overshoot = 5; @@ -153,8 +171,7 @@ void Graph::draw_graph_grid(const Cairo::RefPtr &cr) { cr->stroke(); // Make that scale: - const double scale_color = 0.4; - cr->set_source_rgb(scale_color, scale_color, scale_color); + Gdk::Cairo::set_source_rgba(cr, text_color); Pango::FontDescription font; font.set_absolute_size(10000); // Not sure why this is so big // Vertical scale: @@ -184,7 +201,7 @@ void Graph::draw_graph_grid(const Cairo::RefPtr &cr) { } } -void Graph::make_plot(const Cairo::RefPtr &cr) { +void Graph::draw_plot(const Cairo::RefPtr &cr) { for (unsigned int i = 0; i < sensor_readings.size(); i++) { const unsigned int starting_x_val = graph_width + graph_x_start; const double delta_x = (double)graph_width / ticks; diff --git a/src/graph.h b/src/graph.h index f272eee..930c6e4 100644 --- a/src/graph.h +++ b/src/graph.h @@ -2,7 +2,7 @@ #define GRAPH_H #include "device.h" -#include +#include #include #include @@ -36,17 +36,19 @@ class Graph : public Gtk::DrawingArea { unsigned int prev_height = 0; + Gdk::RGBA text_color; + vector> raw_vals; vector> scaled_vals; vector sensor_readings; vector &m_colors; bool on_draw(const Cairo::RefPtr &cr) override; - const unsigned int scale_val (unsigned int raw_val) const; + const unsigned int scale_val(unsigned int raw_val) const; const bool update(); void check_resize(); void draw_title(const Cairo::RefPtr &cr); void draw_graph_grid(const Cairo::RefPtr &cr); - void make_plot(const Cairo::RefPtr &cr); + void draw_plot(const Cairo::RefPtr &cr); }; #endif