ProcessStateWidget.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ProcessStateWidget.h"
  27. #include "ProcessModel.h"
  28. #include <LibCore/Timer.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/HeaderView.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/SortingProxyModel.h>
  33. #include <LibGUI/TableView.h>
  34. #include <LibGfx/FontDatabase.h>
  35. #include <LibGfx/Palette.h>
  36. class ProcessStateModel final
  37. : public GUI::Model
  38. , public GUI::ModelClient {
  39. public:
  40. explicit ProcessStateModel(ProcessModel& target, pid_t pid)
  41. : m_target(target)
  42. , m_pid(pid)
  43. {
  44. refresh();
  45. }
  46. virtual ~ProcessStateModel() override { }
  47. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_target.column_count({}); }
  48. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 2; }
  49. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role = GUI::ModelRole::Display) const override
  50. {
  51. if (role == GUI::ModelRole::Display) {
  52. if (index.column() == 0) {
  53. if (index.row() == ProcessModel::Column::Icon) {
  54. // NOTE: The icon column is nameless in ProcessModel, but we want it to have a name here.
  55. return "Icon";
  56. }
  57. return m_target.column_name(index.row());
  58. }
  59. return m_target_index.sibling_at_column(index.row()).data();
  60. }
  61. if (role == GUI::ModelRole::Font) {
  62. if (index.column() == 0) {
  63. return Gfx::FontDatabase::default_bold_font();
  64. }
  65. }
  66. return {};
  67. }
  68. virtual void update() override
  69. {
  70. did_update(GUI::Model::DontInvalidateIndexes);
  71. }
  72. virtual void model_did_update([[maybe_unused]] unsigned flags) override
  73. {
  74. refresh();
  75. }
  76. void refresh()
  77. {
  78. m_target_index = {};
  79. for (int row = 0; row < m_target.row_count({}); ++row) {
  80. auto index = m_target.index(row, ProcessModel::Column::PID);
  81. if (index.data().to_i32() == m_pid) {
  82. m_target_index = index;
  83. break;
  84. }
  85. }
  86. update();
  87. }
  88. private:
  89. ProcessModel& m_target;
  90. GUI::ModelIndex m_target_index;
  91. pid_t m_pid { -1 };
  92. };
  93. ProcessStateWidget::ProcessStateWidget(pid_t pid)
  94. {
  95. set_layout<GUI::VerticalBoxLayout>();
  96. layout()->set_margins({ 4, 4, 4, 4 });
  97. m_table_view = add<GUI::TableView>();
  98. m_table_view->column_header().set_visible(false);
  99. m_table_view->column_header().set_section_size(0, 90);
  100. m_table_view->set_model(adopt(*new ProcessStateModel(ProcessModel::the(), pid)));
  101. }
  102. ProcessStateWidget::~ProcessStateWidget()
  103. {
  104. }