input_object.vala 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 InputObject : InputEnum
  8. {
  9. public StringId64 _type;
  10. public Database _database;
  11. public override GLib.Value union_value()
  12. {
  13. return this.value;
  14. }
  15. public override void set_union_value(GLib.Value v)
  16. {
  17. this.value = (Guid)v;
  18. }
  19. public new Guid value
  20. {
  21. get
  22. {
  23. Guid id;
  24. string active_id = this._combo.get_active_id();
  25. if (active_id == null)
  26. return GUID_ZERO;
  27. return Guid.try_parse(active_id, out id) ? id : GUID_ZERO;
  28. }
  29. set
  30. {
  31. _filter.refilter();
  32. bool success = this._combo.set_active_id(value.to_string());
  33. set_inconsistent(!success);
  34. }
  35. }
  36. public new bool filter_visible_func(Gtk.TreeModel model, Gtk.TreeIter iter)
  37. {
  38. Value id_val;
  39. model.get_value(iter, 0, out id_val);
  40. if (!_inconsistent && (string)id_val == INCONSISTENT_ID)
  41. return false;
  42. // TODO: filter based on type.
  43. return true;
  44. }
  45. public InputObject(StringId64 type, Database database)
  46. {
  47. _type = type;
  48. _database = database;
  49. _database.objects_created.connect(on_objects_created);
  50. _database.objects_destroyed.connect(on_objects_destroyed);
  51. _database.objects_changed.connect(on_objects_changed);
  52. append_objects();
  53. }
  54. public void append_objects()
  55. {
  56. Guid?[] all_of_type = _database.all_objects_of_type(_type);
  57. Guid previous_value = this.value;
  58. clear();
  59. foreach (Guid? id in all_of_type) {
  60. StringId64 object_type = StringId64(_database.object_type(id));
  61. Aspect? name_aspect = _database.get_aspect(object_type, StringId64("name"));
  62. if (name_aspect == null)
  63. name_aspect = default_name_aspect;
  64. string object_name;
  65. name_aspect(out object_name, _database, id);
  66. append(id.to_string(), object_name);
  67. }
  68. this.value = previous_value;
  69. }
  70. public void on_objects_created(Guid?[] object_ids, uint32 flags)
  71. {
  72. append_objects();
  73. }
  74. public void on_objects_destroyed(Guid?[] object_ids, uint32 flags)
  75. {
  76. append_objects();
  77. }
  78. public void on_objects_changed(Guid?[] object_ids, uint32 flags)
  79. {
  80. append_objects();
  81. }
  82. }
  83. } /* namespace Crown */