// Get camera with specified name public Camera GetCamera(string name, Group parent) { // find the camera foreach (Camera camera in InnerList) { if ((camera.Name == name) && (camera.Parent == parent)) return camera; } return null; }
// Get group with specified name public Group GetGroup(string name, Group parent) { // find the group foreach (Group group in InnerList) { if ((group.Name == name) && (group.Parent == parent)) return group; } return null; }
// Get view with specified name public View GetView(string name, Group parent) { // find the view foreach (View view in InnerList) { if ((view.Name == name) && (view.Parent == parent)) return view; } return null; }
// Load cameras private void LoadCameras(XmlTextReader reader, Group parent) { // load all groups while (reader.Name == "Group") { int depth = reader.Depth; // create new group Group group = new Group(reader.GetAttribute("name")); group.ID = int.Parse(reader.GetAttribute("id")); group.Description = reader.GetAttribute("desc"); group.Parent = parent; // add group camerasGroups.Add(group); if (group.ID >= nextCamerasGroupID) nextCamerasGroupID = group.ID + 1; // move to next node reader.Read(); // move to next element node while (reader.NodeType == XmlNodeType.EndElement) reader.Read(); // read children if (reader.Depth > depth) LoadCameras(reader, group); // if (reader.Depth < depth) return; } // load all cameras while (reader.Name == "Camera") { int depth = reader.Depth; // create new camera Camera camera = new Camera(reader.GetAttribute("name")); camera.ID = int.Parse(reader.GetAttribute("id")); camera.Description = reader.GetAttribute("desc"); camera.Parent = parent; camera.Provider = providers.GetProviderByName(reader.GetAttribute("provider")); // load configuration if (camera.Provider != null) camera.Configuration = camera.Provider.LoadConfiguration(reader); // add camera cameras.Add(camera); if (camera.ID >= nextCameraID) nextCameraID = camera.ID + 1; // move to next node reader.Read(); // move to next element node while (reader.NodeType == XmlNodeType.EndElement) reader.Read(); if (reader.Depth < depth) return; } }
// Delete views group // delete views group if it is empty or return false otherwise public bool DeleteViewsGroup(Group group) { // check if there are subgroups in the group foreach (Group g in viewsGroups) { if (g.Parent == group) return false; } // check if there are view in the group foreach (View v in views) { if (v.Parent == group) return false; } viewsGroups.Remove(group); // save SaveViews(); return true; }
// Delete cameras group // delete cameras group if it is empty or return false otherwise public bool DeleteCamerasGroup(Group group) { // check if there are subgroups in the group foreach (Group g in camerasGroups) { if (g.Parent == group) return false; } // check if there are cameras in the group foreach (Camera c in cameras) { if (c.Parent == group) return false; } camerasGroups.Remove(group); // save SaveCameras(); return true; }
// Check views group // check if there is already a group with such name // return true, if there is no such group public bool CheckViewsGroup(Group group) { foreach (Group g in viewsGroups) { if ((group.Name == g.Name) && (group.Parent == g.Parent) && ((group.ID == 0) || (group.ID != g.ID))) return false; } return true; }
// Ñheck if the cameras group is already exist private bool CheckCamerasGroup(Group group) { return config.CheckCamerasGroup(group); }
// save views of the parent group private void SaveViews(XmlTextWriter writer, Group parent) { foreach (Group group in viewsGroups) { if (group.Parent == parent) { // new "Group" node writer.WriteStartElement("Group"); // write node writer.WriteAttributeString("id", group.ID.ToString()); writer.WriteAttributeString("name", group.Name); writer.WriteAttributeString("desc", group.Description); // write child groups SaveViews(writer, group); // close "Group" node writer.WriteEndElement(); } } foreach (View view in views) { if (view.Parent == parent) { // new "View" node writer.WriteStartElement("View"); // write node writer.WriteAttributeString("id", view.ID.ToString()); writer.WriteAttributeString("name", view.Name); writer.WriteAttributeString("desc", view.Description); // view size writer.WriteAttributeString("rows", view.Rows.ToString()); writer.WriteAttributeString("cols", view.Cols.ToString()); writer.WriteAttributeString("width", view.CellWidth.ToString()); writer.WriteAttributeString("height", view.CellHeight.ToString()); // write cameras string[] strIDs = new string[View.MaxRows * View.MaxCols]; for (int i = 0, k = 0; i < View.MaxRows; i++) { for (int j = 0; j < View.MaxCols; j++, k++) { strIDs[k] = view.GetCamera(i, j).ToString(); } } writer.WriteAttributeString("cameras", string.Join(",", strIDs)); // close "View" node writer.WriteEndElement(); } } }
// Build views of specified parent private void BuildViewsTree(GroupCollection groups, ViewCollection views, Group parent, TreeNode parentNode) { // build all groups foreach (Group group in groups) { if (group.Parent == parent) { BuildViewsTree(groups, views, group, AddViewsGroup(group, parentNode)); } } // build all views foreach (View view in views) { if (view.Parent == parent) { AddView(view, parentNode); } } }
// Build cameras of specified parent private void BuildCamerasTree(GroupCollection groups, CameraCollection cameras, Group parent, TreeNode parentNode) { // build all groups foreach (Group group in groups) { if (group.Parent == parent) { BuildCamerasTree(groups, cameras, group, AddCamerasGroup(group, parentNode)); } } // build all cameras foreach (Camera camera in cameras) { if (camera.Parent == parent) { AddCamera(camera, parentNode); } } }
// Add views group public TreeNode AddViewsGroup(Group group, TreeNode parentNode) { // create new node TreeNode node = new TreeNode(group.Name, viewsFolderImage, viewsFolderSelectedImage); // add it to tree parentNode.Nodes.Add(node); return node; }
// Add camera group public TreeNode AddCamerasGroup(Group group, TreeNode parentNode) { // create new node TreeNode node = new TreeNode(group.Name, camerasFolderImage, camerasFolderSelectedImage); // add it to tree if (parentNode == null) Nodes.Add(node); else parentNode.Nodes.Add(node); return node; }
// Remove group from the collection public void Remove(Group group) { InnerList.Remove(group); }
// Load views private void LoadViews(XmlTextReader reader, Group parent) { // load all groups while (reader.Name == "Group") { int depth = reader.Depth; // create new group Group group = new Group(reader.GetAttribute("name")); group.ID = int.Parse(reader.GetAttribute("id")); group.Description = reader.GetAttribute("desc"); group.Parent = parent; // add group viewsGroups.Add(group); if (group.ID >= nextViewsGroupID) nextViewsGroupID = group.ID + 1; // move to next node reader.Read(); // move to next element node while (reader.NodeType == XmlNodeType.EndElement) reader.Read(); // read children if (reader.Depth > depth) LoadViews(reader, group); // if (reader.Depth < depth) return; } // load all views while (reader.Name == "View") { int depth = reader.Depth; // create new camera View view = new View(reader.GetAttribute("name")); view.ID = int.Parse(reader.GetAttribute("id")); view.Description = reader.GetAttribute("desc"); view.Parent = parent; // view size view.Rows = short.Parse(reader.GetAttribute("rows")); view.Cols = short.Parse(reader.GetAttribute("cols")); view.CellWidth = short.Parse(reader.GetAttribute("width")); view.CellHeight = short.Parse(reader.GetAttribute("height")); // read cameras string[] strIDs = reader.GetAttribute("cameras").Split(','); for (int i = 0, k = 0; i < View.MaxRows; i++) { for (int j = 0; j < View.MaxCols; j++, k++) { view.SetCamera(i, j, int.Parse(strIDs[k])); } } // add view views.Add(view); if (view.ID >= nextViewID) nextViewID = view.ID + 1; // move to next node reader.Read(); // move to next element node while (reader.NodeType == XmlNodeType.EndElement) reader.Read(); if (reader.Depth < depth) return; } }
// save cameras of the parent group private void SaveCameras(XmlTextWriter writer, Group parent) { foreach (Group group in camerasGroups) { if (group.Parent == parent) { // new "Group" node writer.WriteStartElement("Group"); // write node writer.WriteAttributeString("id", group.ID.ToString()); writer.WriteAttributeString("name", group.Name); writer.WriteAttributeString("desc", group.Description); // write child groups SaveCameras(writer, group); // close "Group" node writer.WriteEndElement(); } } foreach (Camera camera in cameras) { if (camera.Parent == parent) { // new "Camera" node writer.WriteStartElement("Camera"); // write node writer.WriteAttributeString("id", camera.ID.ToString()); writer.WriteAttributeString("name", camera.Name); writer.WriteAttributeString("desc", camera.Description); if (camera.Provider != null) { // write provider name writer.WriteAttributeString("provider", camera.Provider.ProviderName); if (camera.Configuration != null) { // write camera configuration camera.Provider.SaveConfiguration(writer, camera.Configuration); } } // close "Camera" node writer.WriteEndElement(); } } }
// On "Ok" button click private void okButton_Click(object sender, System.EventArgs e) { string name = nameBox.Text.Replace('\\', ' '); if (checkGroupFunction != null) { Group tmpGroup = new Group(name); tmpGroup.ID = group.ID; tmpGroup.Parent = group.Parent; // check group if (checkGroupFunction(tmpGroup) == false) { Color tmp = this.nameBox.BackColor; // highligh name edit box this.nameBox.BackColor = Color.LightCoral; // error message MessageBox.Show(this, "A group with such name is already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); // restore & focus name edit box this.nameBox.BackColor = tmp; this.nameBox.Focus(); return; } } // update groups name and description group.Name = name; group.Description = descriptionBox.Text; // close the dialog this.DialogResult = DialogResult.OK; this.Close(); }
// Add new camera group public void AddCamerasGroup(Group group) { group.ID = nextCamerasGroupID++; camerasGroups.Add(group); // save SaveCameras(); }
// Add new views group public void AddViewsGroup(Group group) { group.ID = nextViewsGroupID++; viewsGroups.Add(group); // save SaveViews(); }
// Ñheck if the views group is already exist private bool CheckViewsGroup(Group group) { return config.CheckViewsGroup(group); }
// Add new group to the collection public void Add(Group group) { InnerList.Add(group); }