ProfileTimelineWidget.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "ProfileTimelineWidget.h"
  2. #include "Profile.h"
  3. #include <LibGUI/GPainter.h>
  4. ProfileTimelineWidget::ProfileTimelineWidget(Profile& profile, GWidget* parent)
  5. : GFrame(parent)
  6. , m_profile(profile)
  7. {
  8. set_frame_thickness(2);
  9. set_frame_shadow(FrameShadow::Sunken);
  10. set_frame_shape(FrameShape::Container);
  11. set_background_color(Color::White);
  12. set_fill_with_background_color(true);
  13. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  14. set_preferred_size(0, 80);
  15. }
  16. ProfileTimelineWidget::~ProfileTimelineWidget()
  17. {
  18. }
  19. void ProfileTimelineWidget::paint_event(GPaintEvent& event)
  20. {
  21. GFrame::paint_event(event);
  22. GPainter painter(*this);
  23. painter.add_clip_rect(event.rect());
  24. float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
  25. for (auto& sample : m_profile.sample_data()) {
  26. u64 t = sample.timestamp;
  27. int x = (int)((float)t * column_width);
  28. int cw = max(1, (int)column_width);
  29. bool in_kernel = sample.in_kernel;
  30. Color color = in_kernel ? Color::from_rgb(0xc25e5a) : Color::from_rgb(0x5a65c2);
  31. for (int i = 0; i < cw; ++i)
  32. painter.draw_line({ x + i, frame_thickness() }, { x + i, height() - frame_thickness() * 2 }, color);
  33. }
  34. u64 normalized_start_time = min(m_select_start_time, m_select_end_time);
  35. u64 normalized_end_time = max(m_select_start_time, m_select_end_time);
  36. int select_start_x = (int)((float)(normalized_start_time - m_profile.first_timestamp()) * column_width);
  37. int select_end_x = (int)((float)(normalized_end_time - m_profile.first_timestamp()) * column_width);
  38. painter.fill_rect({ select_start_x, frame_thickness(), select_end_x - select_start_x, height() - frame_thickness() * 2 }, Color(0, 0, 0, 60));
  39. }
  40. u64 ProfileTimelineWidget::timestamp_at_x(int x) const
  41. {
  42. float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
  43. float ms_into_profile = (float)x / column_width;
  44. return m_profile.first_timestamp() + (u64)ms_into_profile;
  45. }
  46. void ProfileTimelineWidget::mousedown_event(GMouseEvent& event)
  47. {
  48. if (event.button() != GMouseButton::Left)
  49. return;
  50. m_selecting = true;
  51. m_select_start_time = timestamp_at_x(event.x());
  52. m_select_end_time = m_select_start_time;
  53. m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
  54. update();
  55. }
  56. void ProfileTimelineWidget::mousemove_event(GMouseEvent& event)
  57. {
  58. if (!m_selecting)
  59. return;
  60. m_select_end_time = timestamp_at_x(event.x());
  61. m_profile.set_timestamp_filter_range(m_select_start_time, m_select_end_time);
  62. update();
  63. }
  64. void ProfileTimelineWidget::mouseup_event(GMouseEvent& event)
  65. {
  66. if (event.button() != GMouseButton::Left)
  67. return;
  68. m_selecting = false;
  69. if (m_select_start_time == m_select_end_time)
  70. m_profile.clear_timestamp_filter_range();
  71. }