SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ConfigureBlocks.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 
10 
11 namespace SeciUserInterface.Dialogs.Blocks
12 {
13  public partial class ConfigureBlocks : Form
14  {
15  private Boolean _changesMade = false;
16  private Boolean _canEditSubBlocks = false;
17  public Seci.BlockDictionary BlocksCopy { get; set; }
18  public Boolean CanEditSubBlocks { set { _canEditSubBlocks = value; } }
19  public List<string> GroupOrder = new List<string>();
20 
21  public ConfigureBlocks()
22  {
23  InitializeComponent();
24  }
25 
26  private void configureBlocks_Load(object sender, EventArgs e)
27  {
28  //Disable datagrid sorting
29  foreach (DataGridViewColumn i in dgv1.Columns)
30  {
31  i.SortMode = DataGridViewColumnSortMode.NotSortable;
32  }
33 
34  //Check if tempblocks is null, i.e. no blocks in config
35  if (BlocksCopy == null)
36  {
37  //declare new instance
38  BlocksCopy = new Seci.BlockDictionary();
39  }
40 
41  GroupOrder = new List<string>();
42 
43  for (int i = 0; i < Seci.Managers.BlockMgr.Groups.Count; ++i)
44  {
45  GroupOrder.Add(Seci.Managers.BlockMgr.Groups[i].Name);
46  }
47 
48  updateDataGrid();
49 
50  }
51 
52  private void updateDataGrid()
53  {
54  //Get the groups
55  DataGridViewComboBoxColumn groups = (DataGridViewComboBoxColumn)dgv1.Columns["colGroup"];
56 
57  //for (int i = 0; i < BlocksCopy.Count; ++i)
58  //{
59  // if (!String.IsNullOrEmpty(BlocksCopy[i].Group) && !groups.Items.Contains(BlocksCopy[i].Group))
60  // {
61  // groups.Items.Add(BlocksCopy[i].Group);
62  // }
63  //}
64 
65  for (int i = 0; i < Seci.Managers.BlockMgr.Groups.Count; ++i)
66  {
67  groups.Items.Add(Seci.Managers.BlockMgr.Groups[i].Name);
68  }
69 
70  dgv1.Rows.Clear();
71 
72  //Main config blocks
73  if (BlocksCopy != null)
74  {
75  for (int i = 0; i < BlocksCopy.Count; ++i)
76  {
77  Seci.Definitions.BlockInfo temp = BlocksCopy[i];
78 
79  if (String.IsNullOrEmpty(temp.Group))
80  {
81  temp.Group = "None";
82  }
83 
85 
86  try
87  {
88  type = Seci.Managers.LabViewMgr.GetControlType(temp.ParentVI, temp.ReadControl);
89  }
90  catch
91  {
92  //The block is probably in error - maybe the VI has been changed?
93  //Assume it is a numeric
94  type = Seci.Definitions.ControlType.NUMERIC;
95  }
96 
97  if (type == Seci.Definitions.ControlType.BOOLEAN || type == Seci.Definitions.ControlType.STRING)
98  {
99  dgv1.Rows.Add(temp.BlockName, temp.BlockUnits, temp.OwningComponent,
100  temp.ParentVI.Substring(temp.ParentVI.LastIndexOf('\\') + 1),
101  temp.UnderRunControl, temp.RunControlValue, "",
102  temp.BlockEnabled, temp.Group);
103  }
104  else
105  {
106  dgv1.Rows.Add(temp.BlockName, temp.BlockUnits, temp.OwningComponent,
107  temp.ParentVI.Substring(temp.ParentVI.LastIndexOf('\\') + 1),
108  temp.UnderRunControl, temp.LowerLimit, temp.UpperLimit,
109  temp.BlockEnabled, temp.Group);
110  }
111  }
112  }
113  }
114 
115  private void btnAdd_Click(object sender, EventArgs e)
116  {
117  AddEditBlock addBlock = new AddEditBlock();
118  addBlock.Dictionary = BlocksCopy;
119 
120  if (addBlock.ShowDialog() == DialogResult.OK)
121  {
122  _changesMade = true;
123  //update datagrid
124  updateDataGrid();
125  }
126  }
127 
128  private void btnEdit_Click(object sender, EventArgs e)
129  {
130  if (dgv1.CurrentRow != null)
131  {
132  int selectedRow;
133  int selectedColumn;
134  bool selected;
135  getSelectedRow(out selectedRow, out selectedColumn, out selected);
136 
137  AddEditBlock editBlock = new AddEditBlock();
138  editBlock.Dictionary = BlocksCopy;
139 
140  editBlock.Block = (Seci.Definitions.BlockInfo)BlocksCopy[dgv1.CurrentRow.Cells[colName.Name].Value.ToString()];
141 
142  //Check block is not part of a component, as any changes will not be saved
143  if (dgv1.CurrentRow.Cells[2].Value == null || _canEditSubBlocks)
144  {
145  if (editBlock.ShowDialog() == DialogResult.OK)
146  {
147  _changesMade = true;
148  //update datagrid
149  updateDataGrid();
150  setSelectedRow(selectedRow, selectedColumn, selected);
151  }
152  }
153  else
154  {
155  MessageBox.Show("The selected Block is part of a sub-configuration - any changes made will not be saved."
156  , "Editing Sub-configuration Block", MessageBoxButtons.OK, MessageBoxIcon.Warning);
157  editBlock.IsComponent = true;
158 
159  if (editBlock.ShowDialog() == DialogResult.OK)
160  {
161  //update datagrid
162  updateDataGrid();
163  setSelectedRow(selectedRow, selectedColumn, selected);
164  }
165  }
166  }
167  }
168 
169  private void getSelectedRow(out int selectedRow, out int selectedColumn, out bool selected)
170  {
171  selectedRow = -1;
172  selectedColumn = -1;
173  selected = false;
174 
175  if (dgv1.CurrentRow != null)
176  {
177  selected = dgv1.CurrentRow.Selected;
178  selectedRow = dgv1.CurrentRow.Index;
179  selectedColumn = dgv1.CurrentCell.ColumnIndex;
180  }
181  }
182 
183  private void setSelectedRow(int selectedRow, int selectedColumn, bool selected)
184  {
185  if (dgv1.Rows.Count > selectedRow && dgv1.Rows.Count > 0 && selectedRow >= 0)
186  {
187  if (selected)
188  {
189  dgv1.CurrentCell = dgv1.Rows[selectedRow].Cells[selectedColumn];
190  dgv1.CurrentRow.Selected = selected;
191  }
192  else
193  {
194  dgv1.CurrentCell = dgv1.Rows[selectedRow].Cells[selectedColumn];
195  }
196  }
197  }
198 
199  private void btnDelete_Click(object sender, EventArgs e)
200  {
201  if (dgv1.CurrentRow != null)
202  {
203  //Check block is not part of a component
204  if (dgv1.CurrentRow.Cells[2].Value == null || _canEditSubBlocks)
205  {
206  //delete the currently selected data row
207  if (dgv1.CurrentRow != null)
208  {
209  BlocksCopy.Remove(dgv1.CurrentRow.Cells[colName.Name].Value.ToString());
210  dgv1.Rows.RemoveAt(dgv1.CurrentRow.Index);
211  _changesMade = true;
212  }
213  }
214  else
215  {
216  MessageBox.Show("The selected block cannot be deleted as it is part of a sub-configuration."
217  , "Cannot delete component block", MessageBoxButtons.OK, MessageBoxIcon.Warning);
218  }
219  }
220  }
221 
222  private void btnSetNexus_Click(object sender, EventArgs e)
223  {
224  //SetNexusInfo nexus = new SetNexusInfo();
225 
226  //if (_tempBlocks != null)
227  //{
228  // //Copy/clone current config blocks on to tempblocks
229  // nexus.blocksNexus = (Seci.BlockDictionary)_tempBlocks.Clone();
230  //}
231 
232  //if (nexus.ShowDialog() == DialogResult.OK)
233  //{
234  // _tempBlocks = nexus.blocksNexus;
235  // _changesMade = true;
236  //}
237  }
238 
239  private void btnOK_Click(object sender, EventArgs e)
240  {
241  if (_changesMade)
242  {
243  DialogResult = DialogResult.OK;
244  }
245  else
246  {
247  DialogResult = DialogResult.Cancel;
248  }
249 
250  Close();
251  }
252 
253  //Values changed
254  private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
255  {
256  //It is a component which means the group cannot be changed
257  if (dgv1.CurrentRow.Cells[2].Value != null && dgv1.CurrentCell == dgv1.CurrentRow.Cells["colGroup"] && !_canEditSubBlocks)
258  {
259  MessageBox.Show("The selected block cannot be edited as it is part of a component. However, the run-control settings can be changed via the table."
260  , "Cannot edit component block", MessageBoxButtons.OK, MessageBoxIcon.Warning);
261  dgv1.CurrentRow.Cells["colGroup"].Value = ((Seci.Definitions.BlockInfo)BlocksCopy[dgv1.CurrentRow.Cells[colName.Name].Value.ToString()]).Group;
262  return;
263  }
264 
265  doChanges((Seci.Definitions.BlockInfo)BlocksCopy[dgv1.CurrentRow.Cells[colName.Name].Value.ToString()]);
266  }
267 
268  private void doChanges(Seci.Definitions.BlockInfo block)
269  {
270  //make changes to run-control, visibility and enabled only
272 
273  try
274  {
275  type = Seci.Managers.LabViewMgr.GetControlType(block.ParentVI, block.ReadControl);
276  }
277  catch
278  {
279  type = Seci.Definitions.ControlType.NUMERIC;
280  }
281 
282  if (dgv1.CurrentCell == dgv1.CurrentRow.Cells["colRuncontrol"])
283  {
284  block.UnderRunControl = (bool)dgv1.CurrentRow.Cells["colRuncontrol"].Value;
285  }
286  else if (dgv1.CurrentCell == dgv1.CurrentRow.Cells["colLower"])
287  {
288  if (type == Seci.Definitions.ControlType.STRING || type == Seci.Definitions.ControlType.BOOLEAN)
289  {
290  bool underRunControl = (bool)dgv1.CurrentRow.Cells["colRuncontrol"].Value;
291  string lowerLimit = dgv1.CurrentRow.Cells["colLower"].Value.ToString();
292  block.SetRunControlLimits(underRunControl, lowerLimit);
293  }
294  else
295  {
296  setLimits(block, true);
297  }
298  }
299  else if (dgv1.CurrentCell == dgv1.CurrentRow.Cells["colUpper"])
300  {
301  if (type == Seci.Definitions.ControlType.STRING || type == Seci.Definitions.ControlType.BOOLEAN)
302  {
303  //Ignore it
304  }
305  else
306  {
307  setLimits(block, false);
308  }
309  }
310  else if (dgv1.CurrentCell == dgv1.CurrentRow.Cells["colEnabled"])
311  {
312  block.BlockEnabled = (bool)dgv1.CurrentRow.Cells["colEnabled"].Value;
313  }
314  else if (dgv1.CurrentCell == dgv1.CurrentRow.Cells["colGroup"])
315  {
316  block.Group = dgv1.CurrentRow.Cells["colGroup"].Value.ToString();
317  }
318 
319  _changesMade = true;
320  }
321 
322  private void setLimits(Seci.Definitions.BlockInfo block, Boolean lowchanged)
323  {
324  try
325  {
326  String low = dgv1.CurrentRow.Cells["colLower"].Value.ToString();
327  String high = dgv1.CurrentRow.Cells["colUpper"].Value.ToString();
328 
329  low = CheckForInfinity(low);
330  high = CheckForInfinity(high);
331 
332  double lowerLimit = Convert.ToDouble(low);
333  double upperLimit = Convert.ToDouble(high);
334  bool underRunControl = (bool)dgv1.CurrentRow.Cells["colRuncontrol"].Value;
335  block.SetRunControlLimits(underRunControl, lowerLimit, upperLimit);
336  }
337  catch
338  {
339  MessageBox.Show("Invalid value enter; value reset to previous value.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
340  if (lowchanged)
341  {
342  dgv1.CurrentRow.Cells["colLower"].Value = block.LowerLimit;
343  }
344  else
345  {
346  dgv1.CurrentRow.Cells["colUpper"].Value = block.UpperLimit;
347  }
348  }
349  }
350 
351  public static String CheckForInfinity(String value)
352  {
353  if (value.ToLower() == "inf" || value.ToLower() == "infinity")
354  {
355  value = Double.PositiveInfinity.ToString();
356  }
357 
358  if (value.ToLower() == "-inf" || value.ToLower() == "-infinity")
359  {
360  value = Double.NegativeInfinity.ToString();
361  }
362  return value;
363  }
364 
365  private void btnUp_Click(object sender, EventArgs e)
366  {
367  if (dgv1.CurrentRow.Index > 0)
368  {
369  int oldIndex = dgv1.CurrentRow.Index;
370  BlocksCopy.Swap(dgv1.CurrentRow.Index, dgv1.CurrentRow.Index - 1);
371  updateDataGrid();
372  dgv1.CurrentCell = dgv1.Rows[oldIndex - 1].Cells[0];
373  _changesMade = true;
374  }
375  }
376 
377  private void btnDown_Click(object sender, EventArgs e)
378  {
379  if (dgv1.CurrentRow.Index < dgv1.Rows.Count - 1)
380  {
381  int oldIndex = dgv1.CurrentRow.Index;
382  BlocksCopy.Swap(dgv1.CurrentRow.Index, dgv1.CurrentRow.Index + 1);
383  updateDataGrid();
384  dgv1.CurrentCell = dgv1.Rows[oldIndex + 1].Cells[0];
385  _changesMade = true;
386  }
387  }
388 
389  private void btnAddGroup_Click(object sender, EventArgs e)
390  {
391  AddGroup add = new AddGroup();
392 
393  if (add.ShowDialog() == DialogResult.OK)
394  {
395  if (!Seci.Managers.BlockMgr.BlockGroupExists(add.GroupName))
396  {
397  DataGridViewComboBoxColumn groups = (DataGridViewComboBoxColumn)dgv1.Columns["colGroup"];
398  groups.Items.Add(add.GroupName);
399  GroupOrder.Add(add.GroupName);
400  }
401  }
402  }
403 
404  private void btnOrderGroups_Click(object sender, EventArgs e)
405  {
406  OrderGroups ord = new OrderGroups();
407  ord.Order = GroupOrder;
408 
409  if (ord.ShowDialog() == DialogResult.OK)
410  {
411  _changesMade = true;
412  GroupOrder = ord.Order;
413  }
414  }
415  }
416 }
void btnEdit_Click(object sender, EventArgs e)
void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
This class is used for storing the information for any blocks that are created and provides methods f...
Definition: BlockInfo.cs:18
void configureBlocks_Load(object sender, EventArgs e)
void btnDelete_Click(object sender, EventArgs e)
void btnOK_Click(object sender, EventArgs e)
void btnUp_Click(object sender, EventArgs e)
void setLimits(Seci.Definitions.BlockInfo block, Boolean lowchanged)
void btnSetNexus_Click(object sender, EventArgs e)
void btnOrderGroups_Click(object sender, EventArgs e)
void setSelectedRow(int selectedRow, int selectedColumn, bool selected)
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...
void btnAddGroup_Click(object sender, EventArgs e)
void btnAdd_Click(object sender, EventArgs e)
void btnDown_Click(object sender, EventArgs e)
void getSelectedRow(out int selectedRow, out int selectedColumn, out bool selected)
void doChanges(Seci.Definitions.BlockInfo block)