diff --git a/Controls/PreFlight/CheckListEditor.Designer.cs b/Controls/PreFlight/CheckListEditor.Designer.cs index f4340f74ce..f327ec0396 100644 --- a/Controls/PreFlight/CheckListEditor.Designer.cs +++ b/Controls/PreFlight/CheckListEditor.Designer.cs @@ -59,7 +59,7 @@ private void InitializeComponent() this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(200, -14); this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(29, 13); + this.label4.Size = new System.Drawing.Size(37, 16); this.label4.TabIndex = 5; this.label4.Text = "Field"; // @@ -70,6 +70,7 @@ private void InitializeComponent() this.BUT_Add.Size = new System.Drawing.Size(75, 23); this.BUT_Add.TabIndex = 1; this.BUT_Add.Text = "+"; + this.BUT_Add.TextColorNotEnabled = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(87)))), ((int)(((byte)(4))))); this.BUT_Add.UseVisualStyleBackColor = true; this.BUT_Add.Click += new System.EventHandler(this.BUT_Add_Click); // @@ -80,6 +81,7 @@ private void InitializeComponent() this.BUT_save.Size = new System.Drawing.Size(75, 23); this.BUT_save.TabIndex = 2; this.BUT_save.Text = "Save"; + this.BUT_save.TextColorNotEnabled = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(87)))), ((int)(((byte)(4))))); this.BUT_save.UseVisualStyleBackColor = true; this.BUT_save.Click += new System.EventHandler(this.BUT_save_Click); // @@ -88,7 +90,7 @@ private void InitializeComponent() this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(7, 39); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(29, 13); + this.label1.Size = new System.Drawing.Size(37, 16); this.label1.TabIndex = 0; this.label1.Text = "Field"; // @@ -97,7 +99,7 @@ private void InitializeComponent() this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(131, 39); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(62, 13); + this.label2.Size = new System.Drawing.Size(80, 16); this.label2.TabIndex = 3; this.label2.Text = "Comparison"; // @@ -106,7 +108,7 @@ private void InitializeComponent() this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(199, 39); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(40, 13); + this.label3.Size = new System.Drawing.Size(51, 16); this.label3.TabIndex = 4; this.label3.Text = "Trigger"; // @@ -115,7 +117,7 @@ private void InitializeComponent() this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(501, 39); this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(56, 13); + this.label5.Size = new System.Drawing.Size(70, 16); this.label5.TabIndex = 6; this.label5.Text = "Color True"; // @@ -124,7 +126,7 @@ private void InitializeComponent() this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(271, 39); this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(95, 13); + this.label6.Size = new System.Drawing.Size(116, 16); this.label6.TabIndex = 7; this.label6.Text = "Desciption / Value"; // @@ -133,14 +135,12 @@ private void InitializeComponent() this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(572, 39); this.label7.Name = "label7"; - this.label7.Size = new System.Drawing.Size(59, 13); + this.label7.Size = new System.Drawing.Size(76, 16); this.label7.TabIndex = 8; this.label7.Text = "Color False"; // // CheckListEditor // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.ClientSize = new System.Drawing.Size(751, 238); this.Controls.Add(this.label7); this.Controls.Add(this.label6); @@ -153,6 +153,7 @@ private void InitializeComponent() this.Controls.Add(this.panel1); this.Name = "CheckListEditor"; this.Text = "PreFlight CheckListEditor"; + this.Load += new System.EventHandler(this.CheckListEditor_Load); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.ResumeLayout(false); diff --git a/Controls/PreFlight/CheckListEditor.cs b/Controls/PreFlight/CheckListEditor.cs index faf9c6da71..3c5f0ceef5 100644 --- a/Controls/PreFlight/CheckListEditor.cs +++ b/Controls/PreFlight/CheckListEditor.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Windows.Forms; namespace MissionPlanner.Controls.PreFlight @@ -7,11 +9,34 @@ namespace MissionPlanner.Controls.PreFlight public partial class CheckListEditor : Form { CheckListControl _parent; + //Initialize new bool for when pressing the Add button. + private bool addButtonClick = false; + //Checklist Input variable + private CheckListInput wrnctl; + //Initialize List of Controls' desc textboxes + private List wrnctlDescList = new List(); + //Initialize List of Controls' text textboxes + private List wrnctlTextList = new List(); + + //bool to see if the item has been removed + private bool removed = false; + //List for Panel1 controls - used to distinguish between the PArent and Child checklistItems (Parent Items being the items with visible textboxes, Child Items are the items with textboxes not showing) + List PanelOneControls = new List(); + List PanelOneChildControls = new List(); + //used for the _parent.CheckListItems Child, removing from the Preflight ChecklistItem list + CheckListItem currentChildItem = new CheckListItem(); + + //List of All the Panel1 Controls in the Editor Form + List EditorChecklistInputList = new List(); + + //Parent item Control - used for setting the panel1 Control item needed + Control ParentItem = new Control(); + //Bool to check when the secondary add button is clicked to add a child ChecklistInput Item + private bool secondaryAddButton = false; public CheckListEditor(CheckListControl parent) { _parent = parent; - InitializeComponent(); if (DesignMode) @@ -22,11 +47,38 @@ public CheckListEditor(CheckListControl parent) reload(); } + public void AddNewCheck() + { + try + { + //Initialize the y for the new checklist item. + int y = 0; + + lock (_parent.CheckListItems) + { + //Variable for the last item in the checklist items list. + var lastItem = _parent.CheckListItems.LastOrDefault(); + //Set the Y for the checklist item as the bottom of the previous input. + y = panel1.Controls[panel1.Controls.Count - 1].Bottom; + //Set the input using the lastItem variable. + wrnctl = addwarningcontrol(5, y, lastItem); + wrnctl.Visible = false; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + + } public void reload() { panel1.Visible = false; panel1.Controls.Clear(); - + //clear the list of wrnctl desc textboxes. + wrnctlDescList.Clear(); + //clear the list of wrnctl text textboxes. + wrnctlTextList.Clear(); int y = 0; lock (_parent.CheckListItems) @@ -38,7 +90,7 @@ public void reload() y = wrnctl.Bottom; } } - + Utilities.ThemeManager.ApplyThemeTo(this); panel1.PerformLayout(); panel1.Visible = true; @@ -46,8 +98,9 @@ public void reload() CheckListInput addwarningcontrol(int x, int y, CheckListItem item, bool hideforchild = false) { - CheckListInput wrnctl = new CheckListInput(_parent, item); - + //Set instance of the Checklist Input + wrnctl = new CheckListInput(_parent, item); + wrnctl.ReloadList += wrnctl_ChildAdd; wrnctl.Location = new Point(x, y); @@ -59,26 +112,291 @@ CheckListInput addwarningcontrol(int x, int y, CheckListItem item, bool hideforc wrnctl.CMB_colour1.Visible = false; wrnctl.CMB_colour2.Visible = false; } - + panel1.Controls.Add(wrnctl); + //Add the wrnctl's desc textbox to the list of desc textboxes. + wrnctlDescList.Add(wrnctl.TXT_desc); + //Add the wrnctl's text textbox to the list of text textboxes. + wrnctlTextList.Add(wrnctl.TXT_text); + //If the add button was clicked, then add PlaceHolder text for both the desc and text textboxes for the wrnctl input. + if (addButtonClick == true) + { + //set the text for the textboxes accordingly. + wrnctl.TXT_desc.Text = "add description"; + wrnctl.TXT_text.Text = "add value"; + } y = wrnctl.Bottom; + //used for when there is a secondary + button click if (item.Child != null) { wrnctl = addwarningcontrol(x += 5, y, item.Child, true); } - return wrnctl; } + //remove selected item + private void RemoveSelectedItem(int i, int k, Control _ChecklistControl, CheckListInput _ChecklistInputItem, CheckListItem _ChecklistItemSelected) + { + //Remove Control that is clicked on + if ((panel1.Controls[i] == _ChecklistControl)) + { + removed = true; + //If the selected item is not a child (but a parent that has textboxes, etc. visible), then remove the item + if (_ChecklistInputItem.TXT_text.Visible == true || _ChecklistInputItem.TXT_desc.Visible == true || _ChecklistInputItem.CMB_colour1.Visible == true || _ChecklistInputItem.CMB_colour2.Visible == true) + { + //Variable for the Selected item where the parent Checklist item is equal to the Checklist item needed + var PanelChecklistItemSelected = _parent.CheckListItems.Select(x => _ChecklistItemSelected).FirstOrDefault(); + //Remove the checklist item - Preflight Checks + _parent.CheckListItems.Remove(PanelChecklistItemSelected); + //Remove from the checklist Controls, Edit screen + panel1.Controls.RemoveAt(i); + //Remove the item from the list of desc textboxes. + wrnctlDescList.RemoveAt(i); + //Remove the item from the list of text textboxes. + wrnctlTextList.RemoveAt(i); + //If the selected checklist items' child item is not null, then do the following + if (_ChecklistItemSelected.Child != null) + { + //Removing the item from the checklistinput list, which was removed in the block above + EditorChecklistInputList.RemoveAt(i); + //For the items that are after the currently selected item + for (int j = i; j < panel1.Controls.Count; j++) + { + if (j < panel1.Controls.Count) + { + //Variable for the next Checklist Input in the list of all the Checklist Inputs + var nextChecklistInput = EditorChecklistInputList[j]; + + //If the nextChecklistInput variable is a Child Checklist Input, remove the item from the panel controls and the full ChecklistInput list at the 'j' position. + if (nextChecklistInput.TXT_desc.Visible == false) + { + //remove the item in teh Editor Checklist Input List + EditorChecklistInputList.RemoveAt(j); + //Remove the Control at position 'j'. + panel1.Controls.RemoveAt(j); + //Make the selected ChecklistItem's Child null + _ChecklistItemSelected.Child = null; + //Make 'j' j-1 to be used in the loop again for the removed item from the list + j -= 1; + } + else + { + break; + } + } + } + } + } + //If the Item selected is a Child Item. + else if (_ChecklistInputItem.TXT_text.Visible == false || _ChecklistInputItem.TXT_desc.Visible == false || _ChecklistInputItem.CMB_colour1.Visible == false || _ChecklistInputItem.CMB_colour2.Visible == false) + { + //Variable for the previous ChecklistItem before the selected Checklist Item + var previousItemBefore_ChecklistItemSelected = new CheckListItem(); + //Get and Set the previous checklist Item to _checklistitemSelected using for loop t + for (int t = i; t >= 0; t--) + { + //if the position of t is at i-1, then set the previous Item as the ParentItem Control variable at t. + if (t == (i - 1)) + { + ParentItem = panel1.Controls[t]; + var TheItemSelected = (ParentItem as CheckListInput); + previousItemBefore_ChecklistItemSelected = TheItemSelected.CheckListItem; + break; + } + } + for (int p = i; p < panel1.Controls.Count; p++) + { + //variable for the checklist input at the position 'p' + ParentItem = panel1.Controls[p]; + //Variable to set the Parent Item as a Checklist input. + var TheItemSelected = (ParentItem as CheckListInput); + + //Variable to make a new ChecklistItem - Used for _parent.Checklistitem + var ParentChecklistItem = new CheckListItem(); + //Set the checklist item selected + ParentChecklistItem = TheItemSelected.CheckListItem; + //If the item selected has no textboxes(A Child Item), then remove the item + if (TheItemSelected.TXT_desc.Visible == false) + { + //Remove Control item from panel1 Controls at position 'p' + panel1.Controls.RemoveAt(p); + //Remove the item from the list of desc textboxes. + wrnctlDescList.RemoveAt(p); + //Remove the item from the list of text textboxes. + wrnctlTextList.RemoveAt(p); + //Remove the ChecklistInput Item from the full list of checklist inputs at position 'p' + EditorChecklistInputList.RemoveAt(p); + + //Make the selected Checklist Item's Child null - This is where the ChecklistItem Child becomes null but the one selected is not null yet + _ChecklistItemSelected.Child = null; + //Make 'p' p-1 to be used in the loop again for the removed item from the list. + p -= 1; + } + //If the Selected Item textbox is visible or the item is the last in the list of items, then Set the _Parent ChecklistItem's relevant child to null, so that it is no longer visible in the Items list. + if (TheItemSelected.TXT_desc.Visible == true || (p == panel1.Controls.Count - 1)) + { + //Variable to count the amount of Children Items there are before the selected item. + int countPreviousChildrenItems = 0; + //Count the amount of Child Items which occur before the selected item + for (int r = p; r > 0; r--) + { + //Set the Parent Item to be the panel1 Control at position 'r'. + ParentItem = panel1.Controls[r]; + var ThePreviousItemSelected = (ParentItem as CheckListInput); + if (ThePreviousItemSelected.TXT_desc.Visible == false) + { + countPreviousChildrenItems += 1; + } + } + //Get the count for the parent ChecklistItem that is required to remove the child item from. + int parentChecklistItemNeededCount = 0; + //Calculate the Parent Item needed. + parentChecklistItemNeededCount = p - countPreviousChildrenItems; + + //Loop through the Children Items of the Parent Item + //Set the currentChildItem variable for the Children Items that willl be looped through in the for loop 's'. + currentChildItem = _parent.CheckListItems[parentChecklistItemNeededCount - 1].Child; + for (int s = parentChecklistItemNeededCount - 1; s <= p; s++) + { + //If the currentChildItem is equal to the Selected ChecklistItem + //, then make the currentChild Item, the selected checklist Item and the previousItemBefore_ChecklistItemNeeded.Child set to null + if (currentChildItem == _ChecklistItemSelected) + { + currentChildItem = null; + _ChecklistItemSelected = null; + previousItemBefore_ChecklistItemSelected.Child = null; + //Break once this has been completed. + break; + } + else + { + //If the CurrentChildItem and its Child Item are not null,make the currentChildItem the Child of the currentChildItem + if (currentChildItem != null && currentChildItem.Child != null) + { + currentChildItem = currentChildItem.Child; + } + //If the currentChildItem is not null and the Child of the currentChildItem is null + //, then make the currentChildItem, the selected checklist Item and the previousItemBefore_ChecklistItemNeeded.Child set to null + if ((currentChildItem != null && currentChildItem.Child == null)) + { + currentChildItem = null; + _ChecklistItemSelected = null; + previousItemBefore_ChecklistItemSelected.Child = null; + } + } + } + //If the last Item is being looped + //, then make the currentChild Item, the selected checklist Item and the previousItemBefore_ChecklistItemNeeded.Child set to null + if (p == (panel1.Controls.Count - 1)) + { + currentChildItem = null; + _ChecklistItemSelected = null; + previousItemBefore_ChecklistItemSelected.Child = null; + } + break; + } + } + } + } + } + void wrnctl_ChildAdd(object sender, EventArgs e) { - reload(); + //Variable for the Checklist Item + var ChecklistItemSelected = (sender as CheckListInput).CheckListItem; + //Variable for the ChecklistInput + var ChecklistInputItem = (sender as CheckListInput); + //Variable for the Checklist Input Text + var ChecklistInputText = (sender as CheckListInput).ActiveControl.Text; + //Variable for the Checklist Control + var ChecklistControl = (sender as Control); + + //Add panel1 Controls to the PanelOneControls lists + foreach (var item in panel1.Controls) + { + var checklistInputs = (item as CheckListInput); + //If checklist input description textbox is visible, then add the item to the PanelOneControlsList + if (checklistInputs.TXT_desc.Visible == true) + { + //Add parent Controls + PanelOneControls.Add(item); + } + else + { + //Add Child Controls + PanelOneChildControls.Add(item); + } + } + //Add all checklist Inputs into the editor Checklist List + foreach (var item in panel1.Controls) + { + var checklistInput = (item as CheckListInput); + EditorChecklistInputList.Add(checklistInput); + } + //List for panel1Controls + int k = 0; + int i = 0; + if (ChecklistInputText == "-" || (secondaryAddButton == true && ChecklistInputText == "-")) + { + //For the controls on the Panel One Controls list + for (i = 0; i <= panel1.Controls.Count - 1; i++) + { + try + { + RemoveSelectedItem(i, k, ChecklistControl, ChecklistInputItem, ChecklistItemSelected); + //Once the Item Removal Method has finished, continue with the relocation of the Control Items. + if (removed == true) + { + //Set k equal to i - used for setting locations of the Controls below the removed item/s. + k = i; + //If k == 0, then move the next first item to position 0,5 + if (k == 0) + { + panel1.Controls[i].Location = new Point(0, 5); + k = 1; + } + removed = false; + } + //If k is greater than i, relocate the Controls to be in the corrrect space + if (i >= k && k > 0 && i <= panel1.Controls.Count) + { + if (i < panel1.Controls.Count) + { + panel1.Controls[i].Location = new Point(panel1.Controls[i].Location.X, panel1.Controls[i - 1].Bottom); + } + else + { + break; + } + } + } + catch (Exception ex) + { + CustomMessageBox.Show(ex.Message); + } + } + } + else if (ChecklistInputText == "+") + { + //When the + button is clicked, the secondary Addbutton bool is set to true - used for removal function above. + secondaryAddButton = true; + //The original reload method is used for the secondary Add Button. + reload(); + } + //Clear the lists necessary: containing the Panel One Controls. + PanelOneControls.Clear(); + PanelOneChildControls.Clear(); + EditorChecklistInputList.Clear(); + //ClearPlaceholder text method. + ClearPlaceHolderText(); } private void BUT_Add_Click(object sender, EventArgs e) { + //Set the addButtonClick bool to true when the Add button is clicked. + addButtonClick = true; var newcw = new CheckListItem(); CheckListItem.defaultsrc = MainV2.comPort.MAV.cs; @@ -88,13 +406,88 @@ private void BUT_Add_Click(object sender, EventArgs e) { _parent.CheckListItems.Add(newcw); } + //Add the new checklist item with the AddNewCheck method. + AddNewCheck(); + //Event Handler for entering the warning controls' Text and Description Fields - needed in the button add method for when the add button is clicked. + wrnctl.TXT_desc.Enter += new EventHandler(TXT_desc_Enter); + wrnctl.TXT_text.Enter += new EventHandler(TXT_text_Enter); - reload(); + wrnctl.Visible = true; + //Set the bool to false at the end of the Add button method + addButtonClick = false; + } + + //Used for PlaceHolder Text of the Checklist Input + private void TXT_desc_Enter(object sender, EventArgs e) + { + //string for the Textbox's Text (The clicked on textbox) + string itemClickedText = (sender as TextBox).Text; + //Check if the string equals the text of a new desc Textbox. If the same, make its value an empty string. + if (itemClickedText == "add description") + { + itemClickedText = string.Empty; + } + //loop until the textbox which was clicked on is reached. + for (int i = 0; i < wrnctlDescList.Count; i++) + { + if ((sender as TextBox) == wrnctlDescList[i]) + { + //change the text of the textbox to the itemClickedText string value(Empty string). + wrnctlDescList[i].Text = itemClickedText; + return; + } + } + } + private void TXT_text_Enter(object sender, EventArgs e) + { + //string for the Textbox's Text (The clicked on textbox) + string itemClickedText = (sender as TextBox).Text; + //Check if the string equals the text of a new text Textbox. If the same, make its value an empty string. + if (itemClickedText == "add value") + { + itemClickedText = string.Empty; + } + //loop until the textbox which was clicked on is reached. + for (int i = 0; i < wrnctlDescList.Count; i++) + { + if ((sender as TextBox) == wrnctlTextList[i]) + { + //change the text of the textbox to the itemClickedText string value(Empty string). + wrnctlTextList[i].Text = itemClickedText; + return; + } + } + } + + private void ClearPlaceHolderText() + { + //Eventhandlers for the desc and text texboxes are added when entering the text boxes + //checks if the PlaceHolder text is in the textboxes for all checklist inputs + for (int i = 0; i < wrnctlDescList.Count; i++) + { + //if the place holder text is the text in the textboxes, then the relevant event handlers are used. + if (wrnctlDescList[i].Text == "add description") + { + wrnctlDescList[i].Enter += new EventHandler(TXT_desc_Enter); + } + if (wrnctlTextList[i].Text == "add value") + { + wrnctlTextList[i].Enter += new EventHandler(TXT_text_Enter); + } + } } private void BUT_save_Click(object sender, EventArgs e) { _parent.SaveConfig(); + //Clear placeholder text method + ClearPlaceHolderText(); + } + + private void CheckListEditor_Load(object sender, EventArgs e) + { + //Clear placeholder text method + ClearPlaceHolderText(); } } } \ No newline at end of file diff --git a/Controls/PreFlight/CheckListInput.cs b/Controls/PreFlight/CheckListInput.cs index 5293a8f57f..6a31363bcd 100644 --- a/Controls/PreFlight/CheckListInput.cs +++ b/Controls/PreFlight/CheckListInput.cs @@ -234,15 +234,16 @@ private void but_addchild_Click(object sender, EventArgs e) private void but_remove_Click(object sender, EventArgs e) { - lock (_parent.CheckListItems) - { - _parent.CheckListItems.Remove(CheckListItem); - - foreach (var item in _parent.CheckListItems) - { - removewarning(item, CheckListItem); - } - } + // Commented out the below, as a quicker remove method has been added into the ChecklistEditor.cs file + //lock (_parent.CheckListItems) + //{ + // //_parent.CheckListItems.Remove(CheckListItem); + + // foreach (var item in _parent.CheckListItems) + // { + // removewarning(item, CheckListItem); + // } + //} if (ReloadList != null) ReloadList(this, null);