GUI/TreeModel.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
parent 111 2b8a8cf92c3a
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
moel@111
     1
/*
moel@111
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@111
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@111
     9
*/
moel@111
    10
moel@111
    11
using System;
moel@111
    12
using System.Collections;
moel@111
    13
using System.Collections.Generic;
moel@111
    14
using System.Collections.ObjectModel;
moel@111
    15
using Aga.Controls.Tree;
moel@111
    16
moel@111
    17
namespace OpenHardwareMonitor.GUI {
moel@111
    18
  public class TreeModel : ITreeModel {
moel@111
    19
moel@111
    20
    private Node root;
moel@111
    21
    private bool forceVisible = false;
moel@111
    22
moel@111
    23
    public TreeModel() {
moel@111
    24
      root = new Node();
moel@111
    25
      root.Model = this;
moel@111
    26
    }
moel@111
    27
moel@111
    28
    public TreePath GetPath(Node node) {
moel@111
    29
      if (node == root)
moel@111
    30
        return TreePath.Empty;
moel@111
    31
      else {
moel@111
    32
        Stack<object> stack = new Stack<object>();
moel@111
    33
        while (node != root) {
moel@111
    34
          stack.Push(node);
moel@111
    35
          node = node.Parent;
moel@111
    36
        }
moel@111
    37
        return new TreePath(stack.ToArray());
moel@111
    38
      }
moel@111
    39
    }
moel@111
    40
moel@111
    41
    public Collection<Node> Nodes {
moel@111
    42
      get { return root.Nodes; }
moel@111
    43
    }
moel@111
    44
moel@111
    45
    private Node GetNode(TreePath treePath) {
moel@111
    46
      Node parent = root;
moel@111
    47
      foreach (object obj in treePath.FullPath) {
moel@111
    48
        Node node = obj as Node;
moel@111
    49
        if (node == null || node.Parent != parent)
moel@111
    50
          return null;
moel@111
    51
        parent = node;
moel@111
    52
      }
moel@111
    53
      return parent;
moel@111
    54
    }
moel@111
    55
moel@111
    56
    public IEnumerable GetChildren(TreePath treePath) {
moel@111
    57
      Node node = GetNode(treePath);
moel@111
    58
      if (node != null) {
moel@111
    59
        foreach (Node n in node.Nodes)
moel@111
    60
          if (forceVisible || n.IsVisible)
moel@111
    61
            yield return n;
moel@111
    62
      } else {
moel@111
    63
        yield break;
moel@111
    64
      }
moel@111
    65
    }
moel@111
    66
moel@111
    67
    public bool IsLeaf(TreePath treePath) {
moel@111
    68
      return false;
moel@111
    69
    }
moel@111
    70
moel@111
    71
    public bool ForceVisible {
moel@111
    72
      get {
moel@111
    73
        return forceVisible;
moel@111
    74
      }
moel@111
    75
      set {
moel@111
    76
        if (value != forceVisible) {
moel@111
    77
          forceVisible = value;
moel@111
    78
          OnStructureChanged(root);
moel@111
    79
        }
moel@111
    80
      }
moel@111
    81
    }
moel@111
    82
moel@111
    83
    #pragma warning disable 67
moel@111
    84
    public event EventHandler<TreeModelEventArgs> NodesChanged;
moel@111
    85
    public event EventHandler<TreePathEventArgs> StructureChanged;
moel@111
    86
    public event EventHandler<TreeModelEventArgs> NodesInserted;
moel@111
    87
    public event EventHandler<TreeModelEventArgs> NodesRemoved;
moel@111
    88
    #pragma warning restore 67
moel@111
    89
moel@111
    90
    public void OnNodeChanged(Node parent, int index, Node node) {
moel@111
    91
      if (NodesChanged != null && parent != null) {
moel@111
    92
        TreePath path = GetPath(parent);
moel@111
    93
        if (path != null) 
moel@111
    94
          NodesChanged(this, new TreeModelEventArgs(
moel@111
    95
            path, new int[] { index }, new object[] { node }));
moel@111
    96
      }
moel@111
    97
    }
moel@111
    98
moel@111
    99
    public void OnStructureChanged(Node node) {
moel@111
   100
      if (StructureChanged != null)
moel@111
   101
        StructureChanged(this,
moel@111
   102
          new TreeModelEventArgs(GetPath(node), new object[0]));
moel@111
   103
    }
moel@111
   104
moel@111
   105
    public void OnNodeInserted(Node parent, int index, Node node) {
moel@111
   106
      if (NodesInserted != null) {
moel@111
   107
        TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent),
moel@111
   108
          new int[] { index }, new object[] { node });
moel@111
   109
        NodesInserted(this, args);
moel@111
   110
      }
moel@111
   111
moel@111
   112
    }
moel@111
   113
moel@111
   114
    public void OnNodeRemoved(Node parent, int index, Node node) {
moel@111
   115
      if (NodesRemoved != null) {
moel@111
   116
        TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), 
moel@111
   117
          new int[] { index }, new object[] { node });
moel@111
   118
        NodesRemoved(this, args);
moel@111
   119
      }
moel@111
   120
    }
moel@111
   121
moel@111
   122
  }
moel@111
   123
}