ClassViewWidget.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <AK/Vector.h>
  9. #include <LibGUI/AutocompleteProvider.h>
  10. #include <LibGUI/TreeView.h>
  11. #include <LibGUI/Widget.h>
  12. namespace HackStudio {
  13. class ClassViewWidget final : public GUI::Widget {
  14. C_OBJECT(ClassViewWidget)
  15. public:
  16. virtual ~ClassViewWidget() override { }
  17. ClassViewWidget();
  18. void refresh();
  19. private:
  20. RefPtr<GUI::TreeView> m_class_tree;
  21. };
  22. // Note: A ClassViewNode stores a raw pointer to the Declaration from ProjectDeclarations and a StringView into its name.
  23. // We should take care to update the ClassViewModel whenever the declarations change, because otherwise we may be holding pointers to freed memory.
  24. // This is currently achieved with the on_update callback of ProjectDeclarations.
  25. struct ClassViewNode {
  26. StringView name;
  27. const GUI::AutocompleteProvider::Declaration* declaration { nullptr };
  28. NonnullOwnPtrVector<ClassViewNode> children;
  29. ClassViewNode* parent { nullptr };
  30. explicit ClassViewNode(const StringView& name)
  31. : name(name) {};
  32. };
  33. class ClassViewModel final : public GUI::Model {
  34. public:
  35. static RefPtr<ClassViewModel> create();
  36. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  37. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 1; }
  38. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole role) const override;
  39. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  40. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent_index = GUI::ModelIndex()) const override;
  41. private:
  42. explicit ClassViewModel();
  43. void add_declaration(const GUI::AutocompleteProvider::Declaration&);
  44. NonnullOwnPtrVector<ClassViewNode> m_root_scope;
  45. };
  46. }