ProcessView.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <AK/FileSystemPath.h>
  5. #include <AK/HashMap.h>
  6. #include <SharedGraphics/GraphicsBitmap.h>
  7. #include <SharedGraphics/Painter.h>
  8. #include <LibGUI/GScrollBar.h>
  9. #include "ProcessTableModel.h"
  10. #include "ProcessView.h"
  11. static HashMap<unsigned, String>* s_usernames;
  12. ProcessView::ProcessView(GWidget* parent)
  13. : GWidget(parent)
  14. {
  15. m_process_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/gear16.rgb", { 16, 16 });
  16. m_scrollbar = new GScrollBar(Orientation::Vertical, this);
  17. m_scrollbar->set_step(4);
  18. m_scrollbar->set_big_step(30);
  19. m_scrollbar->on_change = [this] (int) {
  20. update();
  21. };
  22. m_model = make<ProcessTableModel>();
  23. start_timer(1000);
  24. reload();
  25. }
  26. ProcessView::~ProcessView()
  27. {
  28. }
  29. void ProcessView::timer_event(GTimerEvent&)
  30. {
  31. reload();
  32. }
  33. void ProcessView::resize_event(GResizeEvent& event)
  34. {
  35. m_scrollbar->set_relative_rect(event.size().width() - m_scrollbar->preferred_size().width(), 0, m_scrollbar->preferred_size().width(), event.size().height());
  36. }
  37. void ProcessView::reload()
  38. {
  39. m_model->update();
  40. int excess_height = max(0, (item_count() * item_height()) - height());
  41. m_scrollbar->set_range(0, excess_height);
  42. set_status_message(String::format("%d processes", item_count()));
  43. update();
  44. }
  45. Rect ProcessView::row_rect(int item_index) const
  46. {
  47. return { 0, header_height() + (item_index * item_height()), width(), item_height() };
  48. }
  49. void ProcessView::mousedown_event(GMouseEvent& event)
  50. {
  51. if (event.button() == GMouseButton::Left) {
  52. for (int i = 0; i < item_count(); ++i) {
  53. if (!row_rect(i).contains(event.position()))
  54. continue;
  55. m_model->set_selected_index({ i, 0 });
  56. update();
  57. }
  58. }
  59. }
  60. void ProcessView::paint_event(GPaintEvent&)
  61. {
  62. Painter painter(*this);
  63. int horizontal_padding = 5;
  64. int painted_item_index = 0;
  65. int x_offset = 0;
  66. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  67. Rect cell_rect(horizontal_padding + x_offset, 0, m_model->column_width(column_index), item_height());
  68. painter.draw_text(cell_rect, m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  69. x_offset += m_model->column_width(column_index) + horizontal_padding;
  70. }
  71. painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
  72. painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
  73. int y_offset = header_height();
  74. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  75. int y = y_offset + painted_item_index * item_height();
  76. Color background_color;
  77. Color text_color;
  78. if (row_index == m_model->selected_index().row()) {
  79. background_color = Color::from_rgb(0x84351a);
  80. text_color = Color::White;
  81. } else {
  82. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  83. text_color = Color::Black;
  84. }
  85. painter.fill_rect(row_rect(painted_item_index), background_color);
  86. int x_offset = 0;
  87. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  88. Rect cell_rect(horizontal_padding + x_offset, y, m_model->column_width(column_index), item_height());
  89. painter.draw_text(cell_rect, m_model->data(row_index, column_index), TextAlignment::CenterLeft, text_color);
  90. x_offset += m_model->column_width(column_index) + horizontal_padding;
  91. }
  92. ++painted_item_index;
  93. };
  94. Rect unpainted_rect(0, painted_item_index * item_height(), width(), height());
  95. unpainted_rect.intersect(rect());
  96. painter.fill_rect(unpainted_rect, Color::White);
  97. }
  98. void ProcessView::set_status_message(String&& message)
  99. {
  100. if (on_status_message)
  101. on_status_message(move(message));
  102. }
  103. int ProcessView::item_count() const
  104. {
  105. return m_model->row_count();
  106. }
  107. pid_t ProcessView::selected_pid() const
  108. {
  109. return m_model->selected_pid();
  110. }