ProcessStateWidget.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProcessStateWidget.h"
  7. #include "ProcessModel.h"
  8. #include <LibCore/Timer.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/HeaderView.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/TableView.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibGfx/Palette.h>
  15. class ProcessStateModel final
  16. : public GUI::Model
  17. , public GUI::ModelClient {
  18. public:
  19. explicit ProcessStateModel(ProcessModel& target, pid_t pid)
  20. : m_target(target)
  21. , m_pid(pid)
  22. {
  23. m_target.register_client(*this);
  24. refresh();
  25. }
  26. virtual ~ProcessStateModel() override
  27. {
  28. m_target.unregister_client(*this);
  29. }
  30. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_target.column_count({}); }
  31. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 2; }
  32. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role = GUI::ModelRole::Display) const override
  33. {
  34. if (role == GUI::ModelRole::Display) {
  35. if (index.column() == 0) {
  36. if (index.row() == ProcessModel::Column::Icon) {
  37. // NOTE: The icon column is nameless in ProcessModel, but we want it to have a name here.
  38. return "Icon";
  39. }
  40. return m_target.column_name(index.row());
  41. }
  42. return m_target_index.sibling_at_column(index.row()).data();
  43. }
  44. if (role == GUI::ModelRole::Font) {
  45. if (index.column() == 0) {
  46. return Gfx::FontDatabase::default_font().bold_variant();
  47. }
  48. }
  49. return {};
  50. }
  51. virtual void model_did_update([[maybe_unused]] unsigned flags) override
  52. {
  53. refresh();
  54. }
  55. void refresh()
  56. {
  57. m_target_index = {};
  58. for (int row = 0; row < m_target.row_count({}); ++row) {
  59. auto index = m_target.index(row, ProcessModel::Column::PID);
  60. if (index.data().to_i32() == m_pid) {
  61. m_target_index = index;
  62. break;
  63. }
  64. }
  65. invalidate();
  66. }
  67. private:
  68. ProcessModel& m_target;
  69. GUI::ModelIndex m_target_index;
  70. pid_t m_pid { -1 };
  71. };
  72. ProcessStateWidget::ProcessStateWidget(pid_t pid)
  73. {
  74. set_layout<GUI::VerticalBoxLayout>();
  75. layout()->set_margins(4);
  76. m_table_view = add<GUI::TableView>();
  77. m_table_view->set_model(adopt_ref(*new ProcessStateModel(ProcessModel::the(), pid)));
  78. m_table_view->column_header().set_visible(false);
  79. m_table_view->column_header().set_section_size(0, 90);
  80. }
  81. ProcessStateWidget::~ProcessStateWidget()
  82. {
  83. }