| 1 | /* Copyright 2026 Maxim Iorsh <[email protected]> |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "histogram.hpp" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <utility> |
| 32 | |
| 33 | #include "../application.hpp" |
| 34 | |
| 35 | namespace ff::widgets { |
| 36 | |
| 37 | std::vector<double> moving_average(const std::vector<int>& series, |
| 38 | size_t window_size) { |
| 39 | const size_t safe_window = std::max((size_t)1, window_size); |
| 40 | const size_t half_window = safe_window / 2; |
| 41 | |
| 42 | std::vector<double> smoothed(series.size(), 0); |
| 43 | for (size_t i = 0; i < series.size(); ++i) { |
| 44 | const size_t from = i < half_window ? 0 : i - half_window; |
| 45 | const size_t to = std::min(series.size() - 1, i + half_window); |
| 46 | |
| 47 | double sum = 0; |
| 48 | for (size_t j = from; j <= to; ++j) { |
| 49 | sum += series[j]; |
| 50 | } |
| 51 | smoothed[i] = sum / (to - from + 1); |
| 52 | } |
| 53 | |
| 54 | return smoothed; |
| 55 | } |
| 56 | |
| 57 | static constexpr int kHistogramMinWidth = 240; |
| 58 | static constexpr int kHistogramHeight = 180; |
| 59 | static constexpr int kBarGapPx = 1; |
| 60 | static constexpr int kOuterMarginPx = 4; |
| 61 | static constexpr int kAxisTickPx = 4; |
| 62 | static constexpr int kAxisLabelGapPx = 2; |
| 63 | static constexpr int kAxisLabelBottomPx = 2; |
| 64 | |
| 65 | Histogram::Histogram() { |
| 66 | set_hexpand(true); |
| 67 | set_vexpand(true); |
| 68 | set_has_tooltip(true); |
| 69 | signal_query_tooltip().connect( |
| 70 | sigc::mem_fun(*this, &Histogram::on_query_tooltip_event)); |
| 71 | add_events(Gdk::BUTTON_PRESS_MASK); |
| 72 | signal_button_press_event().connect( |
| 73 | sigc::mem_fun(*this, &Histogram::on_button_press_event)); |
| 74 | set_size_request(kHistogramMinWidth, kHistogramHeight); |
| 75 | } |
| 76 | |
| 77 | void Histogram::set_values(const std::vector<int>& values) { |
| 78 | values_ = values; |
| 79 | avg_values_ = moving_average(values_, moving_average_window_); |
| 80 | update_size_request(); |
| 81 | } |
| 82 | |
| 83 | void Histogram::set_bar_width(int width_px) { |
| 84 | bar_width_px_ = std::max(1, width_px); |
| 85 | update_size_request(); |
| 86 | } |
| 87 | |
| 88 | void Histogram::set_moving_average_window(int window_size) { |
| 89 | moving_average_window_ = std::max(1, window_size); |
| 90 | avg_values_ = moving_average(values_, moving_average_window_); |
| 91 | queue_draw(); |
| 92 | } |
| 93 | |
| 94 | void Histogram::set_lower_bound(int lower_bound) { |
| 95 | lower_bound_ = lower_bound; |
| 96 | queue_draw(); |
| 97 | } |
| 98 | |
| 99 | void Histogram::set_tooltip_text_callback( |
| 100 | std::function<std::string(int, double)> tooltip_text_callback) { |
| 101 | tooltip_text_cb_ = std::move(tooltip_text_callback); |
| 102 | } |
| 103 | |
| 104 | void Histogram::set_bar_click_callback( |
| 105 | std::function<void(int, bool)> bar_click_callback) { |
| 106 | bar_click_cb_ = std::move(bar_click_callback); |
| 107 | } |
| 108 | |
| 109 | void Histogram::update_size_request() { |
| 110 | int content_width = kHistogramMinWidth; |
| 111 | if (!values_.empty()) { |
| 112 | content_width = |
| 113 | std::max(kHistogramMinWidth, 2 * kOuterMarginPx + |
| 114 | static_cast<int>(values_.size()) * |
| 115 | (bar_width_px_ + kBarGapPx) - |
| 116 | kBarGapPx); |
| 117 | } |
| 118 | set_size_request(content_width, kHistogramHeight); |
| 119 | queue_draw(); |
| 120 | } |
| 121 | |
| 122 | void Histogram::draw_axis_tick(const Cairo::RefPtr<Cairo::Context>& cr, |
| 123 | double axis_y, int index) { |
| 124 | const double tick_x = kOuterMarginPx + |
| 125 | (index - lower_bound_) * (bar_width_px_ + kBarGapPx) + |
| 126 | bar_width_px_ / 2.0; |
| 127 | const double tick_y1 = axis_y + kAxisTickPx; |
| 128 | cr->move_to(tick_x, axis_y); |
| 129 | cr->line_to(tick_x, tick_y1); |
| 130 | cr->stroke(); |
| 131 | |
| 132 | const std::string label = std::to_string(index); |
| 133 | auto label_layout = create_pango_layout(label); |
| 134 | int label_width = 0; |
| 135 | int label_height = 0; |
| 136 | label_layout->get_pixel_size(label_width, label_height); |
| 137 | |
| 138 | const double label_x = tick_x - label_width / 2.0; |
| 139 | const double label_y = tick_y1 + kAxisLabelGapPx; |
| 140 | cr->move_to(label_x, label_y); |
| 141 | label_layout->show_in_cairo_context(cr); |
| 142 | } |
| 143 | |
| 144 | double Histogram::draw_axis(const Cairo::RefPtr<Cairo::Context>& cr, int width, |
| 145 | int height) { |
| 146 | int axis_label_height = 0; |
| 147 | int tick_step = 1000; |
| 148 | if (!values_.empty()) { |
| 149 | // Use the last (widest) label value to compute tick spacing. |
| 150 | auto sample_layout = create_pango_layout( |
| 151 | std::to_string(values_.size() - 1 + lower_bound_)); |
| 152 | int sample_width = 0; |
| 153 | sample_layout->get_pixel_size(sample_width, axis_label_height); |
| 154 | |
| 155 | // Select sufficiently wide tick step |
| 156 | for (int step : {1, 2, 5, 10, 20, 50, 100, 500, 1000}) { |
| 157 | if (step * (bar_width_px_ + kBarGapPx) >= sample_width * 3 / 2) { |
| 158 | tick_step = step; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | const double axis_x0 = kOuterMarginPx; |
| 165 | const double axis_x1 = |
| 166 | std::max(axis_x0, static_cast<double>(width - kOuterMarginPx)); |
| 167 | const double axis_height = kAxisTickPx + kAxisLabelGapPx + |
| 168 | axis_label_height + kAxisLabelBottomPx + 0.5; |
| 169 | const double axis_y = height - axis_height; |
| 170 | |
| 171 | app::ColorManager::instance().set_color_in_context(cr, "ff_histogram_axis"); |
| 172 | cr->set_line_width(1.0); |
| 173 | cr->move_to(axis_x0, axis_y); |
| 174 | cr->line_to(axis_x1, axis_y); |
| 175 | cr->stroke(); |
| 176 | |
| 177 | // Ticks at round values. Fix for rounding towards zero for integer division |
| 178 | // of negative values. |
| 179 | int i = (lower_bound_ > 0) ? ((lower_bound_ / tick_step + 1) * tick_step) |
| 180 | : (lower_bound_ / tick_step * tick_step); |
| 181 | for (; i < (int)values_.size() + lower_bound_; i += tick_step) { |
| 182 | draw_axis_tick(cr, axis_y, i); |
| 183 | } |
| 184 | |
| 185 | return axis_height; |
| 186 | } |
| 187 | |
| 188 | void Histogram::draw_bars(const Cairo::RefPtr<Cairo::Context>& cr, |
| 189 | double bar_base) { |
| 190 | if (values_.empty()) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | const int max_value = *std::max_element(values_.cbegin(), values_.cend()); |
| 195 | if (max_value <= 0) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | const double bar_max_height = std::max(1.0, bar_base - kOuterMarginPx); |
| 200 | |
| 201 | app::ColorManager::instance().set_color_in_context(cr, "ff_histogram_bars"); |
| 202 | for (size_t i = 0; i < values_.size(); ++i) { |
| 203 | const double norm = static_cast<double>(values_[i]) / max_value; |
| 204 | const double bar_height = norm * bar_max_height; |
| 205 | const double x = kOuterMarginPx + i * (bar_width_px_ + kBarGapPx); |
| 206 | const double y = bar_base - bar_height; |
| 207 | |
| 208 | cr->rectangle(x, y, bar_width_px_, bar_height); |
| 209 | cr->fill(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void Histogram::draw_moving_average(const Cairo::RefPtr<Cairo::Context>& cr, |
| 214 | double bar_base) { |
| 215 | // For window size 1 the moving average is the same as the original values. |
| 216 | if (values_.empty() || moving_average_window_ <= 1) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | const int max_value = *std::max_element(values_.cbegin(), values_.cend()); |
| 221 | if (max_value <= 0) { |
| 222 | return; |
| 223 | } |
| 224 | const double bar_max_height = std::max(1.0, bar_base - kOuterMarginPx); |
| 225 | |
| 226 | app::ColorManager::instance().set_color_in_context( |
| 227 | cr, "ff_histogram_moving_average"); |
| 228 | cr->set_line_width(1.5); |
| 229 | |
| 230 | bool started = false; |
| 231 | for (size_t i = 0; i < avg_values_.size(); ++i) { |
| 232 | const double norm = avg_values_[i] / max_value; |
| 233 | const double x = kOuterMarginPx + i * (bar_width_px_ + kBarGapPx) + |
| 234 | bar_width_px_ / 2.0; |
| 235 | const double y = bar_base - norm * bar_max_height; |
| 236 | |
| 237 | if (!started) { |
| 238 | cr->move_to(x, y); |
| 239 | started = true; |
| 240 | } else { |
| 241 | cr->line_to(x, y); |
| 242 | } |
| 243 | } |
| 244 | cr->stroke(); |
| 245 | } |
| 246 | |
| 247 | bool Histogram::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) { |
| 248 | Gtk::Allocation allocation = get_allocation(); |
| 249 | const int width = allocation.get_width(); |
| 250 | const int height = allocation.get_height(); |
| 251 | |
| 252 | app::ColorManager::instance().set_color_in_context(cr, "ff_histogram_bg"); |
| 253 | cr->paint(); |
| 254 | |
| 255 | if (width <= 0 || height <= 0) { |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | const double axis_height = draw_axis(cr, width, height); |
| 260 | const double bar_base = height - axis_height; |
| 261 | draw_bars(cr, bar_base); |
| 262 | draw_moving_average(cr, bar_base); |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | bool Histogram::get_bar_index(int x, int& index) const { |
| 268 | const int pitch = bar_width_px_ + kBarGapPx; |
| 269 | if (pitch <= 0) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | const int relative_x = x - kOuterMarginPx; |
| 274 | if (relative_x < 0) { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | const size_t candidate = static_cast<size_t>(relative_x / pitch); |
| 279 | if (candidate >= values_.size()) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // Convert from index in the vector of values to actual bar X-value. |
| 284 | index = candidate + lower_bound_; |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool Histogram::on_query_tooltip_event( |
| 289 | int x, int y, bool keyboard_tooltip, |
| 290 | const Glib::RefPtr<Gtk::Tooltip>& tooltip) { |
| 291 | if (keyboard_tooltip || values_.empty()) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | int index = 0; |
| 296 | if (!get_bar_index(x, index)) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | const Gtk::Allocation allocation = get_allocation(); |
| 301 | const int height = allocation.get_height(); |
| 302 | if (height <= 0) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | int axis_label_height = 0; |
| 307 | auto sample_layout = |
| 308 | create_pango_layout(std::to_string(values_.size() - 1)); |
| 309 | int sample_width = 0; |
| 310 | sample_layout->get_pixel_size(sample_width, axis_label_height); |
| 311 | |
| 312 | const double axis_height = kAxisTickPx + kAxisLabelGapPx + |
| 313 | axis_label_height + kAxisLabelBottomPx + 0.5; |
| 314 | const double bar_base = height - axis_height; |
| 315 | if (y < 0 || y > bar_base) { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | // Make tooltip follow the mouse (the normal GTK behavior is to anchor the |
| 320 | // tooltip once shown). |
| 321 | const int bar_x = kOuterMarginPx + static_cast<int>(index - lower_bound_) * |
| 322 | (bar_width_px_ + kBarGapPx); |
| 323 | tooltip->set_tip_area( |
| 324 | Gdk::Rectangle(bar_x, 0, bar_width_px_, static_cast<int>(height))); |
| 325 | if (tooltip_text_cb_) { |
| 326 | tooltip->set_text( |
| 327 | tooltip_text_cb_(index, avg_values_[index - lower_bound_])); |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool Histogram::on_button_press_event(GdkEventButton* event) { |
| 333 | if (!bar_click_cb_ || values_.empty()) { |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | const int x = static_cast<int>(event->x); |
| 338 | const int y = static_cast<int>(event->y); |
| 339 | const bool shift_pressed = (event->state & GDK_SHIFT_MASK) != 0; |
| 340 | |
| 341 | int index = 0; |
| 342 | if (!get_bar_index(x, index)) { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | const Gtk::Allocation allocation = get_allocation(); |
| 347 | const int height = allocation.get_height(); |
| 348 | if (height <= 0) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | int axis_label_height = 0; |
| 353 | auto sample_layout = |
| 354 | create_pango_layout(std::to_string(values_.size() - 1 + lower_bound_)); |
| 355 | int sample_width = 0; |
| 356 | sample_layout->get_pixel_size(sample_width, axis_label_height); |
| 357 | |
| 358 | const double axis_height = kAxisTickPx + kAxisLabelGapPx + |
| 359 | axis_label_height + kAxisLabelBottomPx + 0.5; |
| 360 | const double bar_base = height - axis_height; |
| 361 | if (y < 0 || y > bar_base) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | bar_click_cb_(index, shift_pressed); |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | } // namespace ff::widgets |