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