This commit is contained in:
Phyxion
2019-09-13 15:34:42 +02:00
committed by GitHub
parent d92d88d905
commit 22a229eda1
55 changed files with 8818 additions and 9312 deletions
+148 -155
View File
@@ -1,173 +1,166 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
// Mozilla Public License 2.0
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) LibreHardwareMonitor and Contributors
// All Rights Reserved
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using Aga.Controls.Tree;
namespace OpenHardwareMonitor.GUI {
public class Node {
namespace OpenHardwareMonitor.GUI
{
public class Node
{
private Node _parent;
private readonly NodeCollection _nodes;
private string _text;
private bool _visible;
private TreeModel treeModel;
private Node parent;
private NodeCollection nodes;
public delegate void NodeEventHandler(Node node);
public event NodeEventHandler IsVisibleChanged;
public event NodeEventHandler NodeAdded;
public event NodeEventHandler NodeRemoved;
private string text;
private Image image;
private bool visible;
private TreeModel RootTreeModel() {
Node node = this;
while (node != null) {
if (node.Model != null)
return node.Model;
node = node.parent;
}
return null;
}
public Node() : this(string.Empty) { }
public Node(string text) {
this.text = text;
this.nodes = new NodeCollection(this);
this.visible = true;
}
public TreeModel Model {
get { return treeModel; }
set { treeModel = value; }
}
public Node Parent {
get { return parent; }
set {
if (value != parent) {
if (parent != null)
parent.nodes.Remove(this);
if (value != null)
value.nodes.Add(this);
}
}
}
public Collection<Node> Nodes {
get { return nodes; }
}
public virtual string Text {
get { return text; }
set {
if (text != value) {
text = value;
}
}
}
public Image Image {
get { return image; }
set {
if (image != value) {
image = value;
}
}
}
public virtual bool IsVisible {
get { return visible; }
set {
if (value != visible) {
visible = value;
TreeModel model = RootTreeModel();
if (model != null && parent != null) {
int index = 0;
for (int i = 0; i < parent.nodes.Count; i++) {
Node node = parent.nodes[i];
if (node == this)
break;
if (node.IsVisible || model.ForceVisible)
index++;
private TreeModel RootTreeModel()
{
Node node = this;
while (node != null)
{
if (node.Model != null)
return node.Model;
node = node._parent;
}
if (model.ForceVisible) {
model.OnNodeChanged(parent, index, this);
} else {
if (value)
model.OnNodeInserted(parent, index, this);
else
model.OnNodeRemoved(parent, index, this);
return null;
}
public Node() : this(string.Empty) { }
public Node(string text)
{
_text = text;
_nodes = new NodeCollection(this);
_visible = true;
}
public TreeModel Model { get; set; }
public Node Parent
{
get { return _parent; }
set
{
if (value != _parent)
{
_parent?._nodes.Remove(this);
value?._nodes.Add(this);
}
}
}
if (IsVisibleChanged != null)
IsVisibleChanged(this);
}
}
}
public delegate void NodeEventHandler(Node node);
public event NodeEventHandler IsVisibleChanged;
public event NodeEventHandler NodeAdded;
public event NodeEventHandler NodeRemoved;
private class NodeCollection : Collection<Node> {
private Node owner;
public NodeCollection(Node owner) {
this.owner = owner;
}
protected override void ClearItems() {
while (this.Count != 0)
this.RemoveAt(this.Count - 1);
}
protected override void InsertItem(int index, Node item) {
if (item == null)
throw new ArgumentNullException("item");
if (item.parent != owner) {
if (item.parent != null)
item.parent.nodes.Remove(item);
item.parent = owner;
base.InsertItem(index, item);
TreeModel model = owner.RootTreeModel();
if (model != null)
model.OnStructureChanged(owner);
if (owner.NodeAdded != null)
owner.NodeAdded(item);
public Collection<Node> Nodes
{
get { return _nodes; }
}
}
protected override void RemoveItem(int index) {
Node item = this[index];
item.parent = null;
base.RemoveItem(index);
public virtual string Text
{
get { return _text; }
set
{
_text = value;
}
}
TreeModel model = owner.RootTreeModel();
if (model != null)
model.OnStructureChanged(owner);
if (owner.NodeRemoved != null)
owner.NodeRemoved(item);
}
public Image Image { get; set; }
protected override void SetItem(int index, Node item) {
if (item == null)
throw new ArgumentNullException("item");
public virtual bool IsVisible
{
get { return _visible; }
set
{
if (value != _visible)
{
_visible = value;
TreeModel model = RootTreeModel();
if (model != null && _parent != null)
{
int index = 0;
for (int i = 0; i < _parent._nodes.Count; i++)
{
Node node = _parent._nodes[i];
if (node == this)
break;
if (node.IsVisible || model.ForceVisible)
index++;
}
if (model.ForceVisible)
{
model.OnNodeChanged(_parent, index, this);
}
else
{
if (value)
model.OnNodeInserted(_parent, index, this);
else
model.OnNodeRemoved(_parent, index, this);
}
}
IsVisibleChanged?.Invoke(this);
}
}
}
RemoveAt(index);
InsertItem(index, item);
}
private class NodeCollection : Collection<Node>
{
private readonly Node _owner;
public NodeCollection(Node owner)
{
_owner = owner;
}
protected override void ClearItems()
{
while (Count != 0)
RemoveAt(Count - 1);
}
protected override void InsertItem(int index, Node item)
{
if (item == null)
throw new ArgumentNullException(nameof(item));
if (item._parent != _owner)
{
item._parent?._nodes.Remove(item);
item._parent = _owner;
base.InsertItem(index, item);
TreeModel model = _owner.RootTreeModel();
model?.OnStructureChanged(_owner);
_owner.NodeAdded?.Invoke(item);
}
}
protected override void RemoveItem(int index)
{
Node item = this[index];
item._parent = null;
base.RemoveItem(index);
TreeModel model = _owner.RootTreeModel();
model?.OnStructureChanged(_owner);
_owner.NodeRemoved?.Invoke(item);
}
protected override void SetItem(int index, Node item)
{
if (item == null)
throw new ArgumentNullException(nameof(item));
RemoveAt(index);
InsertItem(index, item);
}
}
}
}
}