ProcessView.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. painter.translate(0, -m_scrollbar->value());
  64. int horizontal_padding = 5;
  65. int painted_item_index = 0;
  66. int y_offset = header_height();
  67. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  68. int y = y_offset + painted_item_index * item_height();
  69. Color background_color;
  70. Color text_color;
  71. if (row_index == m_model->selected_index().row()) {
  72. background_color = Color::from_rgb(0x84351a);
  73. text_color = Color::White;
  74. } else {
  75. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  76. text_color = Color::Black;
  77. }
  78. painter.fill_rect(row_rect(painted_item_index), background_color);
  79. int x_offset = 0;
  80. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  81. Rect cell_rect(horizontal_padding + x_offset, y, m_model->column_width(column_index), item_height());
  82. painter.draw_text(cell_rect, m_model->data(row_index, column_index), TextAlignment::CenterLeft, text_color);
  83. x_offset += m_model->column_width(column_index) + horizontal_padding;
  84. }
  85. ++painted_item_index;
  86. };
  87. Rect unpainted_rect(0, painted_item_index * item_height(), width(), height());
  88. unpainted_rect.intersect(rect());
  89. painter.fill_rect(unpainted_rect, Color::White);
  90. // Untranslate the painter and paint the column headers.
  91. painter.translate(0, m_scrollbar->value());
  92. painter.fill_rect({ 0, 0, width(), header_height() }, Color::LightGray);
  93. int x_offset = 0;
  94. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  95. Rect cell_rect(horizontal_padding + x_offset, 0, m_model->column_width(column_index), item_height());
  96. painter.draw_text(cell_rect, m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  97. x_offset += m_model->column_width(column_index) + horizontal_padding;
  98. }
  99. painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
  100. painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
  101. }
  102. void ProcessView::set_status_message(String&& message)
  103. {
  104. if (on_status_message)
  105. on_status_message(move(message));
  106. }
  107. int ProcessView::item_count() const
  108. {
  109. return m_model->row_count();
  110. }
  111. pid_t ProcessView::selected_pid() const
  112. {
  113. return m_model->selected_pid();
  114. }