ProcessStateWidget.cpp 2.8 KB

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