gflow-simple-source.vala 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /********************************************************************
  2. # Copyright 2014-2019 Daniel 'grindhold' Brendle, 2015 Daniel Espinosa <esodan@gmail.com>
  3. #
  4. # This file is part of libgtkflow.
  5. #
  6. # libgtkflow is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public License
  8. # as published by the Free Software Foundation, either
  9. # version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # libgtkflow is distributed in the hope that it will be
  13. # useful, but WITHOUT ANY WARRANTY; without even the implied
  14. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. # PURPOSE. See the GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with libgtkflow.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. namespace GFlow {
  22. /**
  23. * A simple implementation of {@link GFlow.Source}.
  24. */
  25. public class SimpleSource : Object, Dock, Source {
  26. // Dock interface
  27. protected GLib.Value? _val;
  28. protected GLib.Value? _initial;
  29. protected bool _valid = false;
  30. private string? _name = null;
  31. /**
  32. * This SimpleSource's displayname
  33. */
  34. public string? name {
  35. get { return this._name; }
  36. set { this._name = value; }
  37. }
  38. /**
  39. * This SimpleSource's typestring
  40. */
  41. public string? _typename = null;
  42. public string? typename {
  43. get { return this._typename; }
  44. set { this._typename = value; }
  45. }
  46. /**
  47. * Indicates whether this Source should be rendered highlighted
  48. */
  49. public bool highlight { get; set; }
  50. /**
  51. * Indicates whether this Source should be rendered active
  52. */
  53. public bool active {get; set; default=false;}
  54. /**
  55. * A reference to the {@link Node} that this SimpleSource resides in
  56. */
  57. public weak Node? node { get; set; }
  58. /**
  59. * The value that this SimpleSource holds
  60. */
  61. public GLib.Value? val {
  62. get { return _val; }
  63. set {
  64. if (value != null && !_initial.holds (value.type ())) return;
  65. _val = value;
  66. changed ();
  67. }
  68. }
  69. /**
  70. * Creates a new SimpleSource. Supply an arbitrary {@link GLib.Value}. This
  71. * initial value's type will determine this SimpleSource's type.
  72. */
  73. public SimpleSource (GLib.Value initial) {
  74. _initial = initial;
  75. _val = _initial;
  76. }
  77. /**
  78. * The value that this SimpleSource was initialized with
  79. */
  80. public GLib.Value? initial { get { return _initial; } }
  81. // Source interface
  82. private List<Sink> _sinks = new List<Sink> ();
  83. /**
  84. * The {@link Sink}s that this SimpleSource is connected to
  85. */
  86. public List<Sink> sinks { get { return _sinks; } }
  87. /**
  88. * Connects this SimpleSource to the given {@link Sink}. This will
  89. * only succeed if both {@link Dock}s are of the same type. If this
  90. * is not the case, an exception will be thrown
  91. */
  92. protected void add_sink (Sink s) throws Error
  93. {
  94. if (this.initial.type() != s.initial.type()) {
  95. throw new NodeError.INCOMPATIBLE_SINKTYPE(
  96. "Can't connect. Sink has type %s while Source has type %s".printf(
  97. s.initial.type().name(), this.val.type().name()
  98. )
  99. );
  100. }
  101. this._sinks.append (s);
  102. this.changed();
  103. }
  104. /**
  105. * Destroys the connection between this SimpleSource and the given {@link Sink}
  106. */
  107. protected void remove_sink (Sink s) throws GLib.Error
  108. {
  109. if (this._sinks.index(s) != -1)
  110. this._sinks.remove(s);
  111. if (s.is_linked_to(this)) {
  112. s.unlink (this);
  113. this.unlinked(s, this._sinks.length () == 0);
  114. }
  115. }
  116. /**
  117. * Returns true if this Source is connected to the given Sink
  118. */
  119. public bool is_linked_to (Dock dock) {
  120. if (!(dock is Sink)) return false;
  121. return this._sinks.index((Sink) dock) != -1;
  122. }
  123. /**
  124. * Returns true if this Source is connected to one or more Sinks
  125. */
  126. public bool is_linked () {
  127. return this.sinks.length () > 0;
  128. }
  129. /**
  130. * Disconnect from the given {@link Dock}
  131. */
  132. public new void unlink (Dock dock) throws GLib.Error
  133. {
  134. if (!this.is_linked_to (dock)) return;
  135. if (dock is Sink) {
  136. remove_sink ((Sink) dock);
  137. }
  138. }
  139. /**
  140. * Connect to the given {@link Dock}
  141. */
  142. public new void link (Dock dock) throws GLib.Error
  143. {
  144. if (!this.before_linking(this, dock)) return;
  145. if (dock is Sink) {
  146. if (this.is_linked_to (dock)) return;
  147. add_sink ((Sink) dock);
  148. dock.link (this);
  149. linked (dock);
  150. }
  151. }
  152. /**
  153. * Disconnect from any {@link Dock} that this SimplesSource is connected to
  154. */
  155. public new void unlink_all () throws GLib.Error {
  156. foreach (Sink s in this._sinks.copy())
  157. if (s != null)
  158. this.unlink(s);
  159. }
  160. /**
  161. * Set the value of this SimpleSource
  162. */
  163. public void set_value (GLib.Value? v) throws GLib.Error
  164. {
  165. if (v != null && this.initial.type() != v.type())
  166. throw new NodeError.INCOMPATIBLE_VALUE(
  167. "Cannot set a %s value to this %s Source".printf(
  168. v.type().name(),this.val.type().name())
  169. );
  170. this.val = v;
  171. }
  172. }
  173. }