1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2016, 2017 Frank Fischer <frank-fischer@shadow-soft.de>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see  <http://www.gnu.org/licenses/>

//! Visualizing graphs.

use Graph;

#[derive(Clone, Copy, Default, Debug)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

#[derive(Clone, Default, Debug)]
pub struct NodeInfo {
    pub name: Option<String>,
    pub point: Option<Point>,
    pub label: Option<usize>,
    pub value: Option<String>,
}

impl NodeInfo {
    pub fn update(&mut self, info: NodeInfo) {
        if info.name.is_some() {
            self.name = info.name;
        }
        if info.point.is_some() {
            self.point = info.point;
        }
        if info.label.is_some() {
            self.label = info.label;
        }
        if info.value.is_some() {
            self.value = info.value;
        }
    }
}

#[derive(Clone, Default, Debug)]
pub struct EdgeInfo {
    pub label: Option<usize>,
    pub value: Option<String>,
}

impl EdgeInfo {
    pub fn update(&mut self, info: EdgeInfo) {
        if info.label.is_some() {
            self.label = info.label;
        }
        if info.value.is_some() {
            self.value = info.value;
        }
    }
}

/// Trait for drawing sequences of graphs.
pub trait GraphDrawer<'a, G>
    where G: Graph<'a>
{
    /// Update node attributes.
    fn set_node(&mut self, u: G::Node, info: NodeInfo);

    /// Set the point of a node.
    fn set_node_point(&mut self, u: G::Node, point: Point) {
        self.set_node(u, NodeInfo { point: Some(point), ..Default::default() });
    }

    /// Set the name of a node.
    fn set_node_name(&mut self, u: G::Node, name: String) {
        self.set_node(u, NodeInfo { name: Some(name), ..Default::default() });
    }

    /// Set the label of a node.
    fn set_node_label(&mut self, u: G::Node, label: usize) {
        self.set_node(u, NodeInfo { label: Some(label), ..Default::default() });
    }

    /// Set the value of a node.
    fn set_node_value(&mut self, u: G::Node, value: String) {
        self.set_node(u, NodeInfo { value: Some(value), ..Default::default() });
    }

    /// Update edge attributes.
    fn set_edge(&mut self, a: G::Edge, info: EdgeInfo);

    /// Set the label of an edge.
    fn set_edge_label(&mut self, u: G::Edge, label: usize) {
        self.set_edge(u, EdgeInfo { label: Some(label), ..Default::default() });
    }

    /// Set the value of an edge.
    fn set_edge_value(&mut self, e: G::Edge, value: String) {
        self.set_edge(e, EdgeInfo { value: Some(value), ..Default::default() });
    }

    /// Add drawing with current attributes.
    fn add_drawing(&mut self);
}