nodes.vala 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*******************************************************************
  2. # Copyright 2014 Daniel 'grindhold' Brendle
  3. #
  4. # This file is part of Rainbow Lollipop.
  5. #
  6. # Rainbow Lollipop is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU 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. # Rainbow Lollipop 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 General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public
  18. # License along with Rainbow Lollipop.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. using Gee;
  22. using Math;
  23. namespace RainbowLollipop {
  24. /**
  25. * Converts the 8bit representation of a color into a float between 0.0 and 1.0
  26. */
  27. private float col_h2f(uchar col) {
  28. return col/255.0f;
  29. }
  30. /**
  31. * A Constraint that keeps an area aligned to
  32. * the source's right border and the target's left border.
  33. * Used to assign an area to NodeConnectors
  34. */
  35. public class ConnectorConstraint : Clutter.Constraint {
  36. private Clutter.Actor source;
  37. private Clutter.Actor target;
  38. /**
  39. * Create a new node Connector from source to target
  40. */
  41. public ConnectorConstraint(Clutter.Actor source, Clutter.Actor target) {
  42. this.source=source;
  43. this.target=target;
  44. }
  45. /**
  46. * Updates the area depending on source's and target's positions
  47. */
  48. public override void update_allocation(Clutter.Actor a, Clutter.ActorBox alloc) {
  49. var sourcebox = this.source.get_allocation_box();
  50. var targetbox = this.target.get_allocation_box();
  51. alloc.x1 = sourcebox.x2;
  52. alloc.y1 = sourcebox.y1+Config.c.node_height/2;
  53. alloc.x2 = targetbox.x1;
  54. alloc.y2 = targetbox.y1+Config.c.node_height/2+3;
  55. if (alloc.y2-alloc.y1 < (float)Config.c.connector_stroke) {
  56. alloc.y2 = alloc.y1+(float)Config.c.connector_stroke;
  57. }
  58. var bx = new Clutter.ActorBox();
  59. alloc.clamp_to_pixel(ref bx);
  60. (a.content as Clutter.Canvas).set_size((int)roundf(alloc.x2-alloc.x1), (int)roundf(alloc.y2-alloc.y1));
  61. }
  62. }
  63. /**
  64. * An Actor that draws a connecting line between two nodes.
  65. * You can configure the connectors thickness with the config-entry 'connector_stroke'
  66. */
  67. public class Connector : Clutter.Actor {
  68. private Clutter.Canvas c;
  69. private Node previous;
  70. private Node next;
  71. /**
  72. * Create a new Connector from the Node previous to the Node next
  73. */
  74. public Connector(Node previous, Node next) {
  75. this.previous = previous;
  76. this.next = next;
  77. this.c = new Clutter.Canvas();
  78. this.content = c;
  79. this.set_size(10,10);
  80. this.c.set_size(10,10);
  81. this.x = Config.c.node_height/2+Config.c.track_spacing;
  82. this.y = 0;
  83. Config.c.notify.connect(config_update);
  84. this.c.draw.connect(do_draw);
  85. this.add_constraint(
  86. new ConnectorConstraint(previous, next)
  87. );
  88. this.c.invalidate();
  89. previous.track.add_nodeconnector(this);
  90. }
  91. /**
  92. * Handles changes in config
  93. */
  94. private void config_update() {
  95. this.x = Config.c.node_height/2+Config.c.track_spacing;
  96. this.queue_redraw();
  97. }
  98. /**
  99. * Draws the Connector line
  100. */
  101. public bool do_draw(Cairo.Context cr, int w, int h) {
  102. cr.set_source_rgba(0,0,0,0);
  103. cr.set_operator(Cairo.Operator.SOURCE);
  104. cr.paint();
  105. cr.set_source_rgba(col_h2f(this.previous.color.red)*2,
  106. col_h2f(this.previous.color.green)*2,
  107. col_h2f(this.previous.color.blue)*2,
  108. 1);
  109. cr.set_line_width(Config.c.connector_stroke);
  110. cr.move_to(0,1);
  111. if (h < Config.c.node_height) {
  112. cr.rel_line_to(w,0);
  113. } else {
  114. cr.rel_curve_to(w,0,0,h-Config.c.connector_stroke,w,h-Config.c.connector_stroke);
  115. }
  116. cr.stroke();
  117. return true;
  118. }
  119. /**
  120. * Fade in
  121. */
  122. public void emerge() {
  123. this.visible = true;
  124. this.save_easing_state();
  125. this.opacity = 0xFF;
  126. this.restore_easing_state();
  127. }
  128. /**
  129. * Fade out
  130. */
  131. public void disappear() {
  132. this.save_easing_state();
  133. this.opacity = 0x00;
  134. this.restore_easing_state();
  135. }
  136. }
  137. /**
  138. * This class represents a Node. A node is a point in the history-tree
  139. * of a track. A node represents a call to a website and the result of
  140. * those calls.
  141. */
  142. public class Node : Focusable {
  143. /**
  144. * Reference to the node that spawned this node
  145. */
  146. private weak Node? previous;
  147. /**
  148. * Contains all nodes that have been spawned from this node
  149. */
  150. public Gee.ArrayList<Node> childnodes {get {return this._childnodes;}}
  151. private Gee.ArrayList<Node> _childnodes; //special list only for nodes
  152. /**
  153. * The HistoryTrack that this Node belongs to
  154. */
  155. public HistoryTrack track {get; set;}
  156. /**
  157. * The color in which this Node is displayed. The color is being derived
  158. * from the associated Track's color
  159. */
  160. public Clutter.Color color {get;set;}
  161. private Connector? connector;
  162. protected Clutter.ClickAction clickaction;
  163. /**
  164. * Constructs a node
  165. */
  166. public Node(HistoryTrack track, Node? par) {
  167. if (par != null) {
  168. par.childnodes.add(this);
  169. this.previous = par;
  170. if (this.previous.childnodes.size-1 > this.previous.index_of_child(this)) {
  171. this.previous.recalculate_nodes();
  172. }
  173. }
  174. track.add_node(this);
  175. this._childnodes = new Gee.ArrayList<Node>();
  176. this._track = track;
  177. this.x = par != null ? par.x : 0;
  178. this.y = Config.c.track_spacing;
  179. this.save_easing_state();
  180. this.x = par.x+par.width+(float)Config.c.node_spacing;
  181. this.y = Config.c.track_spacing;
  182. this.restore_easing_state();
  183. if (par != null){
  184. this.connector = new Connector(par,this);
  185. }
  186. this.height = this.width = Config.c.node_height;
  187. Config.c.notify.connect(config_update);
  188. this.color = track.get_background_color().lighten();
  189. this.color = this.color.lighten();
  190. this.previous.recalculate_y(null);
  191. this.reactive = true;
  192. this.clickaction = new Clutter.ClickAction();
  193. this.add_action(this.clickaction);
  194. Focus.S().focused_object = this;
  195. }
  196. protected virtual void config_update() {
  197. this.height = this.width = Config.c.node_height;
  198. this.x = this.previous.x+this.previous.width+(float)Config.c.node_spacing;
  199. this.y = Config.c.track_spacing;
  200. this.queue_redraw();
  201. this.previous.recalculate_y(null);
  202. }
  203. /**
  204. * Declares this node a rootnode by removing the reference to any previous Node
  205. */
  206. public void make_root_node() {
  207. this.previous = null;
  208. }
  209. /**
  210. * Takes this node an all its childnodes, creates a new track, removes said
  211. * nodes from their current track and assigns them to the newly created track
  212. */
  213. public void move_to_new_track() {
  214. SiteNode? new_track_current_node = null;
  215. string new_track_search_string = "";
  216. var prv = this.previous;
  217. if (prv != null) {
  218. bool need_new_current_node = this.contains_current_node();
  219. prv.childnodes.remove(this);
  220. prv.recalculate_y(null);
  221. prv.track.calculate_height();
  222. if (need_new_current_node) {
  223. new_track_current_node = this.track.current_node as SiteNode;
  224. new_track_search_string = this.track.web.get_search_string();
  225. this.track.web.stop_search();
  226. if (prv is SiteNode)
  227. prv.track.current_node = prv as SiteNode;
  228. }
  229. } else {
  230. this.track.delete_track();
  231. }
  232. this.get_parent().remove_child(this);
  233. this.connector.destroy();
  234. this.detach_childnodes();
  235. this.track.tracklist.add_track_with_node(this,
  236. new_track_current_node,
  237. new_track_search_string);
  238. }
  239. /**
  240. * Returns the Index in the childnodes-list of the given child-node
  241. */
  242. private int index_of_child(Node n) {
  243. return this.childnodes.index_of(n);
  244. }
  245. /**
  246. * Returns, into how many side-branches this node holds
  247. * (in general the number of children-1) except when the
  248. * node has only one child.
  249. * This is used to calculate how much offset a node must have in order
  250. * to render a proper non-entangled graphical tree representation
  251. */
  252. public int get_splits() {
  253. int r = 0;
  254. foreach (Node n in this.childnodes) {
  255. r += n.get_splits();
  256. }
  257. if (this.childnodes.size > 1) {
  258. r += this.childnodes.size-1;
  259. }
  260. return r;
  261. }
  262. /**
  263. * Returns how many splits there are until the given childnode-index
  264. */
  265. public int get_splits_until(int index) {
  266. int r = 0;
  267. for (int i = 0; i < index; ++i) {
  268. r += (this.childnodes.get(i) as Node).get_splits();
  269. }
  270. return r;
  271. }
  272. /**
  273. * Determines whether the subtree under this node contains the
  274. * current_node of the track.
  275. */
  276. public bool contains_current_node() {
  277. foreach (Node n in this.childnodes) {
  278. if (n == this.track.current_node || n.contains_current_node()) {
  279. return true;
  280. }
  281. }
  282. return false;
  283. }
  284. /**
  285. * Takes care of recursively deleting this node and all child nodes.
  286. * Causes this Node's track to recalculate its height and rerender
  287. * the node-tree.
  288. */
  289. public void delete_node(bool rec_initial=true) {
  290. bool need_new_current_node = false;
  291. if (rec_initial) {
  292. need_new_current_node = this == this.track.current_node || this.contains_current_node();
  293. }
  294. var prv = this.previous;
  295. Gee.ArrayList<Node> nodes = new Gee.ArrayList<Node>();
  296. foreach (Node n in this.childnodes) {
  297. nodes.add(n);
  298. }
  299. foreach (Node n in nodes) {
  300. n.delete_node(false);
  301. }
  302. prv.childnodes.remove(this);
  303. if (this.contains(Focus.S()))
  304. this.remove_child(Focus.S());
  305. this.connector.destroy();
  306. this.destroy();
  307. prv.recalculate_y(null);
  308. if (rec_initial){
  309. if (prv == null) {
  310. //This was the root node. the track is not necessary anymore, delete it.
  311. this.track.delete_track();
  312. //Focus current_node of next track
  313. Focus.S().focused_object = this.get_down_focusable();
  314. } else {
  315. //Recalculate the tracks height in case there is some free space now
  316. this.track.calculate_height();
  317. if (need_new_current_node && prv is SiteNode) {
  318. this.track.current_node = prv as SiteNode;
  319. }
  320. //Focus previous
  321. Focus.S().focused_object = this.get_left_focusable();
  322. }
  323. }
  324. }
  325. /**
  326. * Handles right-clicking on a node
  327. */
  328. public void do_clicked() {
  329. switch (this.clickaction.get_button()) {
  330. case 3: //Right mousebutton
  331. Application.S().context.set_context(this.track,this);
  332. Application.S().context.popup(null,null,null,
  333. 3,Gtk.get_current_event_time());
  334. break;
  335. }
  336. this.track.clickaction.release(); //TODO: ugly fix.. there has to be a better way
  337. // Prevents nodes from hanging in a pressed
  338. // state after they have been clicked.
  339. }
  340. /**
  341. * Lets each childnode of this node recalculate its y-coordinate positions
  342. */
  343. public void recalculate_nodes() {
  344. foreach (Node n in this.childnodes) {
  345. n.recalculate_y(this);
  346. }
  347. }
  348. /**
  349. * Recalculates this Node's y-coordinate position for the graphical tree
  350. * representation.
  351. */
  352. public void recalculate_y(Node? call_origin) {
  353. if (this.previous != null && call_origin != this.previous) {
  354. this.previous.recalculate_y(this);
  355. return;
  356. } else {
  357. int node_index = this.previous.index_of_child((Node) this);
  358. if (node_index == -1)
  359. warning(_("The node is not a child of its parent. This should not happen"));
  360. int splits_until = this.previous.get_splits_until(node_index);
  361. var prvy = this.previous.y != 0 ? this.previous.y : Config.c.track_spacing;
  362. this.y = prvy + (splits_until+node_index)*(Config.c.node_height+Config.c.track_spacing);
  363. foreach (Node n in this.childnodes) {
  364. n.recalculate_y(this);
  365. }
  366. }
  367. }
  368. /**
  369. * Returns this Node's previous node if any.
  370. * If it returns null, this node is a root node.
  371. */
  372. public Node? get_previous() {
  373. return this.previous;
  374. }
  375. /**
  376. * Deletes any reference to this node from a parent node and destroys
  377. * the Connector that connects the two.
  378. */
  379. private void detach_childnodes() {
  380. foreach (Node n in this.childnodes) {
  381. n.detach_childnodes();
  382. }
  383. this.get_parent().remove_child(this);
  384. this.connector.destroy();
  385. }
  386. /**
  387. * Ensures that this Node and its Connector have colors according to their Track
  388. */
  389. protected void adapt_to_track() {
  390. if (this.previous != null) {
  391. this.connector = new Connector(this.previous, this);
  392. }
  393. }
  394. /**
  395. * Serialize this nodes and its childnodes recursively into JSON
  396. */
  397. public void to_json(Json.Builder b) {
  398. b.set_member_name("nodes");
  399. b.begin_array();
  400. foreach (Node n in this.childnodes) {
  401. if (n is SiteNode)
  402. (n as SiteNode).to_json(b);
  403. }
  404. b.end_array();
  405. }
  406. /**
  407. * Tries to return the next sibling that resides on the
  408. * same tree-depth as this node.
  409. */
  410. public Node? get_next_on_same_level(Node node, uint level=0) {
  411. if (this.childnodes.size > this.index_of_child(node)+1) {
  412. Node n = this.childnodes[this.index_of_child(node)+1];
  413. for (int i = 0; i < level; i++) {
  414. if (n.childnodes.size > 0)
  415. n = n.childnodes[0];
  416. else
  417. return null;
  418. }
  419. return n;
  420. } else {
  421. if (this.previous != null)
  422. return this.previous.get_next_on_same_level(this, ++level);
  423. else
  424. return null;
  425. }
  426. }
  427. /**
  428. * Tries to return the previous sibling that resides on the
  429. * same tree-depth as this node.
  430. */
  431. public Node? get_previous_on_same_level(Node node, uint level=0) {
  432. if (this.index_of_child(node) > 0) {
  433. Node n = this.childnodes[this.index_of_child(node)-1];
  434. for (int i = 0; i < level; i++) {
  435. if (n.childnodes.size > 0)
  436. n = n.childnodes[n.childnodes.size-1];
  437. else
  438. return null;
  439. }
  440. return n;
  441. } else {
  442. if (this.previous != null)
  443. return this.previous.get_previous_on_same_level(this, ++level);
  444. else
  445. return null;
  446. }
  447. }
  448. public override Focusable? get_left_focusable() {
  449. return this.previous;
  450. }
  451. public override Focusable? get_right_focusable() {
  452. if (this.childnodes.size > 0)
  453. return this.childnodes[0];
  454. else
  455. return null;
  456. }
  457. public override Focusable? get_down_focusable() {
  458. Node? n = this.previous.get_next_on_same_level(this);
  459. if (n != null)
  460. return n;
  461. else {
  462. Track? t = (this.track.get_next_sibling() as Track);
  463. if (t != null && t is HistoryTrack) {
  464. return (t as HistoryTrack).current_node;
  465. } else {
  466. return Application.S().tracklist.get_empty_track().get_go_button();
  467. }
  468. }
  469. }
  470. public override Focusable? get_up_focusable() {
  471. Node? n = this.previous.get_previous_on_same_level(this);
  472. if (n != null)
  473. return n;
  474. else {
  475. Track? t = (this.track.get_previous_sibling() as Track);
  476. if (t != null && t is HistoryTrack) {
  477. return (t as HistoryTrack).current_node;
  478. } else
  479. return null;
  480. }
  481. }
  482. public override void focus_activate() {
  483. if (this is SiteNode) {
  484. this.track.current_node = this;
  485. Application.S().state = NormalState.S();
  486. }
  487. }
  488. }
  489. }