object_properties.vala 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class ObjectProperties : Gtk.Box
  8. {
  9. public const string NOTHING_TO_SHOW = "nothing-to-show";
  10. public const string UNKNOWN_OBJECT_TYPE = "unknown-object-type";
  11. public const string PROPERTIES = "properties";
  12. public DatabaseEditor _database_editor;
  13. public Database _database;
  14. public Gee.ArrayList<bool> _expanders_states;
  15. public Gee.ArrayList<PropertyGrid> _grids;
  16. public Gtk.Viewport _viewport;
  17. public Gtk.ScrolledWindow _scrolled_window;
  18. public PropertyGridSet _object_view;
  19. public Gtk.Stack _stack;
  20. public ObjectProperties(DatabaseEditor database_editor)
  21. {
  22. // Data
  23. _database_editor = database_editor;
  24. _database_editor.selection_changed.connect(on_database_selection_changed);
  25. _database = database_editor._database;
  26. _expanders_states = new Gee.ArrayList<bool>();
  27. _grids = new Gee.ArrayList<PropertyGrid>();
  28. // Widgets
  29. _object_view = new PropertyGridSet();
  30. _object_view.margin_bottom
  31. = _object_view.margin_end
  32. = _object_view.margin_start
  33. = _object_view.margin_top
  34. = 6
  35. ;
  36. _viewport = new Gtk.Viewport(null, null);
  37. _viewport.add(_object_view);
  38. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  39. _scrolled_window.add(_viewport);
  40. _stack = new Gtk.Stack();
  41. _stack.add_named(new Gtk.Label("Select an object to start editing"), NOTHING_TO_SHOW);
  42. _stack.add_named(new Gtk.Label("Unknown object type"), UNKNOWN_OBJECT_TYPE);
  43. _stack.add_named(_scrolled_window, PROPERTIES);
  44. this.pack_start(_stack);
  45. this.get_style_context().add_class("properties-view");
  46. }
  47. public void set_object(Guid id)
  48. {
  49. if (id == GUID_ZERO) {
  50. _stack.set_visible_child_name(NOTHING_TO_SHOW);
  51. return;
  52. }
  53. if (!_database.has_object(id) || !_database.is_alive(id)) {
  54. loge("Object does not exist");
  55. return;
  56. }
  57. _stack.set_visible_child_name(PROPERTIES);
  58. int num_found = 0;
  59. for (int i = 0; i < _grids.size; ++i) {
  60. PropertyGrid grid = _grids[i];
  61. if (Guid.equal_func(grid._id, id)) {
  62. grid._visible = true;
  63. grid.read_properties();
  64. ++num_found;
  65. } else {
  66. grid._visible = false;
  67. }
  68. }
  69. if (num_found == 0) {
  70. PropertyGrid grid = new PropertyGrid.from_object(id, _database);
  71. grid._visible = true;
  72. grid.read_properties();
  73. _object_view.add_property_grid(grid, "General");
  74. _grids.add(grid);
  75. }
  76. _object_view._list_box.invalidate_filter();
  77. _object_view._list_box.invalidate_sort();
  78. _object_view.show_all();
  79. }
  80. public void on_database_selection_changed()
  81. {
  82. Gee.ArrayList<Guid?> selection = _database_editor._selection;
  83. if (selection.size == 0)
  84. set_object(GUID_ZERO);
  85. else
  86. set_object(selection.last());
  87. }
  88. }
  89. } /* namespace Crown */