tbf/TBF/UI/Bench/EditSchDrawing/EditSchDrawingDlg.cs
2021-04-03 00:54:18 +02:00

975 lines
40 KiB
C#

///
/// Copyright (c) 2020 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using log4net;
using NHibernate;
using SchematicDrawing;
using TBF.Rig.Generic;
using TBF.Resources;
using TBF.UI.Shared;
using TBF.UI.Bench.Components;
namespace TBF.UI.Bench.EditSchDrawing
{
public partial class EditSchDrawingDlg : Form
{
static readonly ILog log = LogManager.GetLogger(typeof(EditSchDrawingDlg));
enum Selection
{
Auto,
Body,
Label,
MeasuredVal,
Setpoint,
PipeS,
PipeM,
PipeL,
PipeXL,
Count
}
bool unlocked;
ISession session;
IList<Config.Entities.Component> cmpntEntities;
TBF.Rig.Various.Drawing.Pipes.Configuration pipesScfg;
TBF.Rig.Various.Drawing.Pipes.Configuration pipesMcfg;
TBF.Rig.Various.Drawing.Pipes.Configuration pipesLcfg;
TBF.Rig.Various.Drawing.Pipes.Configuration pipesXLcfg;
SelectComponentClassDlg selectComponentTypeDlg; /// Constructed once, the selection is kept between dialog usages
public EditSchDrawingDlg()
{
InitializeComponent();
Controls.Add(new TextBox() { Visible = false });
selectComponentTypeDlg = new SelectComponentClassDlg("Various.Drawing.");
sharedButtons.RequiredGroupMembership = new Users.Entities.GID[] { Users.Entities.GID.Administrators };
sharedButtons.OptionalButtons = SharedButtons.Buttons.Add;
sharedButtons.Unlocked += Unlocked;
sharedButtons.OKClicked += okButton_Click;
sharedButtons.CancelClicked += cancelButton_Click;
sharedButtons.AddClicked += addButton_Click;
radioButton1.Checked = true; /// Auto selection mode
UpdateTitle();
}
private void EditSchDrawingDlg_Load(object sender, EventArgs e)
{
LoadFormPosition();
try
{
session = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config);
cmpntEntities = session.QueryOver<Config.Entities.Component>()
.OrderBy(x => x.ItemNr).Asc
.List();
}
catch (Exception exc)
{
log.ErrorFormat("Exception when loading components : {1}", exc.Message);
cmpntEntities = new List<Config.Entities.Component>();
}
drawingCtrl.SupressRedraws = true;
drawingCtrl.EditMode = true;
int measuredValuesCount = 0;
int setpointsCount = 0;
pipesScfg = null;
pipesMcfg = null;
pipesLcfg = null;
pipesXLcfg = null;
///
foreach (var c in cmpntEntities)
{
IComponentCfg cmpCfg = TBF.Rig.TbfComponents.CmpntCfgFromCmpntEntity(c);
if (cmpCfg is IDrawingItem)
{
/// Component is an item to be drawn in the schematic drawing and added to the list box
var item = cmpCfg as IDrawingItem;
itemsListBox.Items.Add(item.Name);
drawingCtrl.AddItem(item);
if (cmpCfg is IDrawingItemWithMeasuredVal) measuredValuesCount++;
if (cmpCfg is IDrawingItemWithSetpoint) setpointsCount++;
}
else if (cmpCfg is TBF.Rig.Various.Drawing.Pipes.Configuration)
{
/// Component is Various.Drawing.Edges component
var eCfg = cmpCfg as TBF.Rig.Various.Drawing.Pipes.Configuration;
switch (eCfg.Sz)
{
case Sz.S: if (pipesScfg == null) pipesScfg = eCfg; break;
case Sz.M: if (pipesMcfg == null) pipesMcfg = eCfg; break;
case Sz.L: if (pipesLcfg == null) pipesLcfg = eCfg; break;
case Sz.XL: if (pipesXLcfg == null) pipesXLcfg = eCfg; break;
}
}
}
///
drawingCtrl.PipesS = pipesScfg.Pipes;
drawingCtrl.PipesM = pipesMcfg.Pipes;
drawingCtrl.PipesL = pipesLcfg.Pipes;
drawingCtrl.PipesXL = pipesXLcfg.Pipes;
drawingCtrl.Route = 0;
drawingCtrl.MsrmntAvailableFlags = new bool[measuredValuesCount];
drawingCtrl.MeasuredValues = new double[measuredValuesCount];
drawingCtrl.AltStrings = new string[measuredValuesCount];
drawingCtrl.Setpoints = new double[setpointsCount];
drawingCtrl.SupressRedraws = false;
}
void UpdateTitle()
{
Pipe edge;
var slctdItem = drawingCtrl.SelectedItem as IDrawingItem;
if (slctdItem != null)
{
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
Text = string.Format("{0}: {1} {2} [{3}, {4}] {5} {6} {7}", Strings.Schematic_drawing,
slctdItem.Name, slctdItem.Shape, slctdItem.X, slctdItem.Y,
slctdItem.Sz, slctdItem.Orient, slctdItem.Flip ? "flip" : "");
return;
case Element.Label:
Text = string.Format("{0}: {1} {2} [{3}, {4}]", Strings.Schematic_drawing,
slctdItem.Name, "Label", slctdItem.X + slctdItem.LblX, slctdItem.Y + slctdItem.LblY);
return;
case Element.MeasuredVal:
var m = drawingCtrl.SelectedItem as IDrawingItemWithMeasuredVal;
int mx = (m != null) ? m.MsrdX : 0;
int my = (m != null) ? m.MsrdY : 0;
Text = string.Format("{0}: {1} {2} [{3}, {4}]", Strings.Schematic_drawing,
slctdItem.Name, slctdItem.Shape, slctdItem.X + mx, slctdItem.Y + my);
return;
case Element.Setpoint:
var s = drawingCtrl.SelectedItem as IDrawingItemWithSetpoint;
int sx = (s != null) ? s.SetpX : 0;
int sy = (s != null) ? s.SetpY : 0;
Text = string.Format("{0}: {1} {2} [{3}, {4}]", Strings.Schematic_drawing,
slctdItem.Name, slctdItem.Shape, slctdItem.X + sx, slctdItem.Y + sy);
return;
}
}
if (null != (edge = drawingCtrl.GetTempPipe()))
{
Text = string.Format("{0}: Pipe {1} size={2}", Strings.Schematic_drawing, edge.ToString(), edge.Sz);
return;
}
Text = Strings.Schematic_drawing;
}
private void Unlocked(object sender, EventArgs e)
{
unlocked = true;
deletePipeButton.Enabled = true;
cwButton.Enabled = true;
ccwButton.Enabled = true;
upButton.Enabled = true;
downButton.Enabled = true;
leftButton.Enabled = true;
rightButton.Enabled = true;
mirrorButton.Enabled = true;
sizeButton.Enabled = true;
radioButton1.Enabled = true;
radioButton2.Enabled = true;
radioButton3.Enabled = true;
radioButton4.Enabled = true;
radioButton5.Enabled = true;
radioButton6.Enabled = true;
radioButton7.Enabled = true;
radioButton8.Enabled = true;
radioButton9.Enabled = true;
itemsListBox.Enabled = true;
drawingCtrl.EditMode = true;
drawingCtrl.Redraw();
}
private void okButton_Click(object sender, EventArgs e)
{
SaveDBChanges(session);
SaveUISettings();
DialogResult = DialogResult.OK;
Close();
return;
}
private void cancelButton_Click(object sender, EventArgs e)
{
SaveUISettings();
DialogResult dr = MessageBox.Show(Strings.Changes_will_be_lost_Do_you_want_to_proceed,
Strings.Warning,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
private void addButton_Click(object sender, EventArgs e)
{
DialogResult dialogRslt = selectComponentTypeDlg.ShowDialog();
if (dialogRslt != DialogResult.OK) return;
IComponentFactory factory = selectComponentTypeDlg.SlctdCmpntFactory;
IComponentCfg cfg = factory.DefaultConfig();
IComponentCfgCtrl cfgControl = cfg.GetControl(cmpntEntities);
cfgControl.Config = cfg;
cfgControl.Config.ItemNr = (cmpntEntities.Count > 0) ? (cmpntEntities[cmpntEntities.Count - 1].ItemNr + 1) : 1;
ComponentParametersDlg cfgForm = new ComponentParametersDlg();
cfgForm.CmpntEntities = cmpntEntities;
cfgForm.ComponentCfgCtrl = cfgControl;
cfgForm.UnlockAfterStart = true;
dialogRslt = cfgForm.ShowDialog();
if (dialogRslt == DialogResult.OK)
{
Config.Entities.Component newComponent = cfgForm.Config.CreateDbEntity();
cmpntEntities.Add(newComponent);
IDrawingItem item = cfgForm.Config as IDrawingItem;
if (item != null)
{
drawingCtrl.AddItem(item);
itemsListBox.Items.Add(item.Name);
itemsListBox.SelectedIndex = itemsListBox.Items.Count - 1;
}
}
}
private void LoadFormPosition()
{
LocalSettings ls = Program.LocalSettings;
if (ls != null)
{
Width = (ls.SchDrawingDlgWidth > 0) ? ls.SchDrawingDlgWidth : 850;
Height = (ls.SchDrawingDlgHeight > 0) ? ls.SchDrawingDlgHeight : 500;
Left = (ls.SchDrawingDlgLeft != 0) ? ls.SchDrawingDlgLeft : 200;
Top = (ls.SchDrawingDlgTop != 0) ? ls.SchDrawingDlgTop : 100;
}
}
/// <summary>
/// Save the dialog position and ListViewEx column widths
/// </summary>
void SaveUISettings()
{
/// Obtain ComponentsmanagerDlg dimensions, etc.
bool isMaximized = (WindowState == FormWindowState.Maximized);
int left = (WindowState == FormWindowState.Normal) ? Location.X : RestoreBounds.Left;
int top = (WindowState == FormWindowState.Normal) ? Location.Y : RestoreBounds.Top;
int width = (WindowState == FormWindowState.Normal) ? Size.Width : RestoreBounds.Width;
int height = (WindowState == FormWindowState.Normal) ? Size.Height : RestoreBounds.Height;
LocalSettings ls = Program.LocalSettings;
if (ls != null && (ls.SchDrawingDlgMaximized != isMaximized ||
ls.SchDrawingDlgLeft != left ||
ls.SchDrawingDlgTop != top ||
ls.SchDrawingDlgWidth != width ||
ls.SchDrawingDlgHeight != height))
{
/// At least one ComponentsManagerDlg dimension differs => Update local settings and save them
///
ls.SchDrawingDlgMaximized = isMaximized;
ls.SchDrawingDlgLeft = left;
ls.SchDrawingDlgTop = top;
ls.SchDrawingDlgWidth = width;
ls.SchDrawingDlgHeight = height;
ls.Save();
}
}
bool SaveDBChanges(ISession session)
{
using (ITransaction transaction = session.BeginTransaction())
{
try
{
int i = 0;
foreach (var original in cmpntEntities)
{
IComponentCfg cmpCfg = TBF.Rig.TbfComponents.CmpntCfgFromCmpntEntity(original);
if ((cmpCfg is IDrawingItem) && (i < drawingCtrl.GetItems().Count))
{
Config.Entities.Component modified = (drawingCtrl.GetItems()[i++] as TBF.Rig.Generic.IComponentCfg).CreateDbEntity();
if (original.Id == 0 || (original.Name == modified.Name && original.Parameters != modified.Parameters))
{
original.Parameters = modified.Parameters;
session.SaveOrUpdate(original);
}
}
if (cmpCfg is TBF.Rig.Various.Drawing.Pipes.Configuration)
{
var eCfg = cmpCfg as TBF.Rig.Various.Drawing.Pipes.Configuration;
switch (eCfg.Sz)
{
case Sz.S: eCfg.Pipes = drawingCtrl.PipesS; break;
case Sz.M: eCfg.Pipes = drawingCtrl.PipesM; break;
case Sz.L: eCfg.Pipes = drawingCtrl.PipesL; break;
case Sz.XL: eCfg.Pipes = drawingCtrl.PipesXL; break;
}
Config.Entities.Component modified = (eCfg as TBF.Rig.Generic.IComponentCfg).CreateDbEntity();
if (original.Id == 0 || (original.Name == modified.Name && original.Parameters != modified.Parameters))
{
original.Parameters = modified.Parameters;
session.SaveOrUpdate(original);
}
}
}
transaction.Commit();
session.Flush();
return true;
}
catch (Exception exc)
{
transaction.Rollback();
log.ErrorFormat("Exception when saving components : {1}", exc.Message);
return false;
}
}
}
private void itemsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (!unlocked) return;
string selectedStr = itemsListBox.SelectedItem.ToString();
IDrawingItem item;
if (drawingCtrl.Name2Item.TryGetValue(selectedStr, out item))
{
drawingCtrl.SelectedItem = item;
switch (GetSelection())
{
case Selection.Label:
drawingCtrl.SelectedElement = Element.Label;
break;
case Selection.MeasuredVal:
drawingCtrl.SelectedElement = (item is IDrawingItemWithMeasuredVal) ? Element.MeasuredVal : Element.Body;
break;
case Selection.Setpoint:
drawingCtrl.SelectedElement = (item is IDrawingItemWithSetpoint) ? Element.Setpoint : Element.Body;
break;
default:
drawingCtrl.SelectedElement = Element.Body;
break;
}
}
else
{
drawingCtrl.SelectedItem = null;
}
UpdateTitle();
}
Selection GetSelection()
{
if (radioButton2.Checked) return Selection.Body;
if (radioButton3.Checked) return Selection.Label;
if (radioButton4.Checked) return Selection.MeasuredVal;
if (radioButton5.Checked) return Selection.Setpoint;
if (radioButton6.Checked) return Selection.PipeS;
if (radioButton7.Checked) return Selection.PipeM;
if (radioButton8.Checked) return Selection.PipeL;
if (radioButton9.Checked) return Selection.PipeXL;
return Selection.Auto;
}
/// <summary>
/// Return orientation so that object/label/value rotates clockwise
/// </summary>
/// <param name="orient">New orientation</param>
Orient RotateCW(Orient orient)
{
if (orient == Orient.Up) return Orient.R;
return ++orient;
}
/// <summary>
/// Update orientation so that object/label/value rotates counterclockwise
/// </summary>
/// <param name="orient">Ref. to orienation to be updated</param>
Orient RotateCCW(Orient orient)
{
if (orient == Orient.R) return Orient.Up;
return --orient;
}
///
/// Moving items on the screen
///
/// Either on-screen left/right/up/down buttons (xyzButton_click handlers)
/// or up/down/left/right arrow keys on the keyboard can be used.
/// Step size (small or large) can eb switched over by Shift and Ctrl keys.
/// Shit button selects large steps.
/// Ctrl key selects small step.
///
static bool isLargeStep = false;
///
private void leftButton_Click(object sender, EventArgs e) { StepLeft(isLargeStep); }
private void rightButton_Click(object sender, EventArgs e) { StepRight(isLargeStep); }
private void upButton_Click(object sender, EventArgs e) { StepUp(isLargeStep); }
private void downButton_Click(object sender, EventArgs e) { StepDown(isLargeStep); }
///
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!unlocked)
return base.ProcessCmdKey(ref msg, keyData); /// Do not process a key if not unlocked
if (keyData == Keys.Up) StepUp(isLargeStep); /// Process Up arrow key
else if (keyData == Keys.Down) StepDown(isLargeStep); /// Process Down arrow key
else if (keyData == Keys.Left) StepLeft(isLargeStep); /// Process Left arrow key
else if (keyData == Keys.Right) StepRight(isLargeStep); /// Process Right arrow key
else
{
if ((keyData & Keys.Shift) != 0)
{
isLargeStepLabel.Visible = isLargeStep = true;
}
if ((keyData & Keys.Control) != 0)
{
isLargeStepLabel.Visible = isLargeStep = false;
}
return base.ProcessCmdKey(ref msg, keyData); /// Return in case of non-arrow keys
}
return true; /// Return (true) after an arrow key
}
void StepLeft(bool large = false)
{
IDrawingItem item = drawingCtrl.SelectedItem;
if (item == null) return;
int m = large ? 8 : 1;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
item.X -= m * SchematicDrawing.Const.GridSize;
if (item.X < 0) item.X = 0;
break;
case Element.Label:
if (item is IDrawingItemWithMeasuredVal &&
(item.Shape == Shape.TempM || item.Shape == Shape.PressM || item.Shape == Shape.Ambient))
{
/// Move measured value together with the label, keep label nearby the (nonexisting) body
if (!large && item.LblX > 0)
{
item.LblX--;
(item as IDrawingItemWithMeasuredVal).MsrdX--;
}
else if (!large && item.X >= SchematicDrawing.Const.GridSize)
{
item.X -= SchematicDrawing.Const.GridSize;
item.LblX += (SchematicDrawing.Const.GridSize - 1);
(item as IDrawingItemWithMeasuredVal).MsrdX += (SchematicDrawing.Const.GridSize - 1);
}
else if (item.X >= SchematicDrawing.Const.GridSize)
{
item.X -= SchematicDrawing.Const.GridSize;
}
}
else
{
/// Body exists, move just the label
item.LblX -= m;
}
break;
case Element.MeasuredVal:
var msrdValItem = item as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdX -= m;
break;
case Element.Setpoint:
var setpointItem = item as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpX -= m;
break;
default:
return; /// Edge selected -> do nothing
}
UpdateTitle();
drawingCtrl.Redraw();
}
void StepRight(bool large = false)
{
IDrawingItem item = drawingCtrl.SelectedItem;
if (item == null) return;
int m = large ? 8 : 1;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
item.X += m * SchematicDrawing.Const.GridSize;
break;
case Element.Label:
if (item is IDrawingItemWithMeasuredVal &&
(item.Shape == Shape.TempM || item.Shape == Shape.PressM || item.Shape == Shape.Ambient))
{
/// Move measured value together with the label, keep label nearby the (nonexisting) body
if (!large && item.LblX < SchematicDrawing.Const.GridSize - 1)
{
item.LblX++;
(item as IDrawingItemWithMeasuredVal).MsrdX++;
}
else if (!large)
{
item.X += SchematicDrawing.Const.GridSize;
item.LblX -= (SchematicDrawing.Const.GridSize - 1);
(item as IDrawingItemWithMeasuredVal).MsrdX -= (SchematicDrawing.Const.GridSize - 1);
}
else
{
item.X += SchematicDrawing.Const.GridSize;
}
}
else
{
/// Body exists, move just the label
item.LblX += m;
}
break;
case Element.MeasuredVal:
var msrdValItem = item as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdX += m;
break;
case Element.Setpoint:
var setpointItem = item as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpX += m;
break;
default:
return; /// Edge selected -> do nothing
}
UpdateTitle();
drawingCtrl.Redraw();
}
void StepUp(bool large = false)
{
IDrawingItem item = drawingCtrl.SelectedItem;
if (item == null) return;
int m = large ? 8 : 1;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
item.Y -= m * SchematicDrawing.Const.GridSize;
if (item.Y < 0) item.Y = 0;
break;
case Element.Label:
if (item is IDrawingItemWithMeasuredVal &&
(item.Shape == Shape.TempM || item.Shape == Shape.PressM || item.Shape == Shape.Ambient))
{
/// Move measured value together with the label, keep label nearby the (nonexisting) body
if (!large && item.LblY > 0)
{
item.LblY--;
(item as IDrawingItemWithMeasuredVal).MsrdY--;
}
else if (!large && item.Y >= SchematicDrawing.Const.GridSize)
{
item.Y -= SchematicDrawing.Const.GridSize;
item.LblY += (SchematicDrawing.Const.GridSize - 1);
(item as IDrawingItemWithMeasuredVal).MsrdY += (SchematicDrawing.Const.GridSize - 1);
}
else if (item.Y >= SchematicDrawing.Const.GridSize)
{
item.Y -= SchematicDrawing.Const.GridSize;
}
}
else
{
/// Body exists, move just the label
item.LblY -= m;
}
break;
case Element.MeasuredVal:
var msrdValItem = item as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdY -= m;
break;
case Element.Setpoint:
var setpointItem = item as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpY -= m;
break;
default:
return; /// Edge selected -> do nothing
}
UpdateTitle();
drawingCtrl.Redraw();
}
void StepDown(bool large = false)
{
IDrawingItem item = drawingCtrl.SelectedItem;
if (item == null) return;
int m = large ? 8 : 1;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
item.Y += m * SchematicDrawing.Const.GridSize;
break;
case Element.Label:
if (item is IDrawingItemWithMeasuredVal &&
(item.Shape == Shape.TempM || item.Shape == Shape.PressM || item.Shape == Shape.Ambient))
{
/// Move measured value together with the label, keep label nearby the (nonexisting) body
if (!large && item.LblY < SchematicDrawing.Const.GridSize - 1)
{
item.LblY++;
(item as IDrawingItemWithMeasuredVal).MsrdY++;
}
else if (!large)
{
item.Y += SchematicDrawing.Const.GridSize;
item.LblY -= (SchematicDrawing.Const.GridSize - 1);
(item as IDrawingItemWithMeasuredVal).MsrdY -= (SchematicDrawing.Const.GridSize - 1);
}
else
{
item.Y += SchematicDrawing.Const.GridSize;
}
}
else
{
/// Body exists, move just the label
item.LblY += m;
}
break;
case Element.MeasuredVal:
var msrdValItem = item as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdY += m;
break;
case Element.Setpoint:
var setpointItem = item as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpY += m;
break;
default:
return; /// Edge selected -> do nothing
}
UpdateTitle();
drawingCtrl.Redraw();
}
///
/// Rotating and flipping items on the screen
///
private void cwButton_Click(object sender, EventArgs e)
{
if (drawingCtrl.SelectedItem == null) return;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
drawingCtrl.SelectedItem.Orient = RotateCW(drawingCtrl.SelectedItem.Orient);
break;
case Element.Label:
drawingCtrl.SelectedItem.LblOrient = RotateCW(drawingCtrl.SelectedItem.LblOrient);
break;
case Element.MeasuredVal:
var msrdValItem = drawingCtrl.SelectedItem as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdOrient = RotateCW(msrdValItem.MsrdOrient);
break;
case Element.Setpoint:
var setpointItem = drawingCtrl.SelectedItem as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpOrient = RotateCW(setpointItem.SetpOrient);
break;
default: return; /// Edge
}
UpdateTitle();
drawingCtrl.Redraw();
}
///
private void ccwButton_Click(object sender, EventArgs e)
{
if (drawingCtrl.SelectedItem == null) return;
switch (drawingCtrl.SelectedElement)
{
case Element.Body:
drawingCtrl.SelectedItem.Orient = RotateCCW(drawingCtrl.SelectedItem.Orient);
break;
case Element.Label:
drawingCtrl.SelectedItem.LblOrient = RotateCCW(drawingCtrl.SelectedItem.LblOrient);
break;
case Element.MeasuredVal:
var msrdValItem = drawingCtrl.SelectedItem as IDrawingItemWithMeasuredVal;
if (msrdValItem != null) msrdValItem.MsrdOrient = RotateCCW(msrdValItem.MsrdOrient);
break;
case Element.Setpoint:
var setpointItem = drawingCtrl.SelectedItem as IDrawingItemWithSetpoint;
if (setpointItem != null) setpointItem.SetpOrient = RotateCCW(setpointItem.SetpOrient);
break;
default: return; /// Edge
}
UpdateTitle();
drawingCtrl.Redraw();
}
///
private void mirrorButton_Click(object sender, EventArgs e)
{
if (drawingCtrl.SelectedItem != null && drawingCtrl.SelectedElement == Element.Body)
{
drawingCtrl.SelectedItem.Flip = !drawingCtrl.SelectedItem.Flip;
UpdateTitle();
drawingCtrl.Redraw();
}
}
/// <summary>
/// Changes enum Sz values in selectedItem.Sz variable as follows:
/// S -> M -> L -> XL -> S ...
/// K1 -> K2 -> K3 -> K4 -> K5 -> K6 -> K7 -> K1 ...
/// </summary>
private void sizeButton_Click(object sender, EventArgs e)
{
if (drawingCtrl.SelectedItem != null && drawingCtrl.SelectedElement == Element.Body)
{
drawingCtrl.SelectedItem.Sz++;
if (drawingCtrl.SelectedItem.Sz == Sz.Count)
{
if (drawingCtrl.SelectedItem is TBF.Rig.Various.Drawing.Junction.Configuration)
{
drawingCtrl.SelectedItem.Sz = Sz.None;
}
else
{
drawingCtrl.SelectedItem.Sz = Sz.S;
}
}
UpdateTitle();
drawingCtrl.Redraw();
}
}
private void deletePipeButton_Click(object sender, EventArgs e)
{
DeleteSelectedPipe();
}
void DeleteSelectedPipe()
{
Pipe pipe = drawingCtrl.GetTempPipe();
if (pipe != null)
{
/// Delete selected pipe
TBF.Rig.Various.Drawing.Pipes.Configuration pipesCfg = null;
switch (pipe.Sz)
{
case Sz.S: pipesCfg = pipesScfg; break;
case Sz.M: pipesCfg = pipesMcfg; break;
case Sz.L: pipesCfg = pipesLcfg; break;
case Sz.XL: pipesCfg = pipesXLcfg; break;
}
if (pipesCfg != null)
{
string[] oriEdges = pipesCfg.Pipes;
int ix = -1;
for (int i = 0; i < oriEdges.Length; i++)
{
if (oriEdges[i] == pipe.ToString())
{
ix = i;
break;
}
}
if (ix >= 0)
{
pipesCfg.Pipes = new string[oriEdges.Length - 1];
int j = 0;
for (int i = 0; i < oriEdges.Length; i++)
{
if (i != ix)
{
pipesCfg.Pipes[j++] = oriEdges[i];
}
}
switch (pipe.Sz)
{
case Sz.S: drawingCtrl.PipesS = pipesCfg.Pipes; break;
case Sz.M: drawingCtrl.PipesM = pipesCfg.Pipes; break;
case Sz.L: drawingCtrl.PipesL = pipesCfg.Pipes; break;
case Sz.XL: drawingCtrl.PipesXL = pipesCfg.Pipes; break;
}
}
}
drawingCtrl.ClearTempPipe();
}
}
private void drawingCtrl_MouseClick(object sender, MouseEventArgs e)
{
IDrawingItem item;
Pipe pipe;
int nodeId;
Selection sel = GetSelection();
Element findArgument;
switch (sel)
{
default:
case Selection.Auto:
findArgument = Element.Body | Element.Label | Element.MeasuredVal | Element.Setpoint | Element.Pipe | Element.Node;
break;
case Selection.Body:
findArgument = Element.Body;
break;
case Selection.Label:
findArgument = Element.Label;
break;
case Selection.MeasuredVal:
findArgument = Element.MeasuredVal;
break;
case Selection.Setpoint:
findArgument = Element.Setpoint;
break;
case Selection.PipeS:
case Selection.PipeM:
case Selection.PipeL:
case Selection.PipeXL:
findArgument = Element.Pipe;
break;
}
Element searchResult = drawingCtrl.FindElement(e.X, e.Y, findArgument, out item, out pipe, out nodeId);
if (searchResult == Element.Pipe)
{
drawingCtrl.SupressRedraws = true;
drawingCtrl.SelectedItem = null;
drawingCtrl.ShowTempPipe(pipe);
drawingCtrl.SupressRedraws = false;
}
else if ((searchResult & (Element.Body | Element.Label | Element.MeasuredVal | Element.Setpoint)) != 0)
{
/// Mouse pointer is inside item image rectangle or within 'tolerance' pixels from the item origin
drawingCtrl.SupressRedraws = true;
drawingCtrl.SelectedItem = item;
drawingCtrl.SelectedElement = searchResult;
drawingCtrl.ClearTempPipe();
drawingCtrl.SupressRedraws = false;
}
else if (drawingCtrl.SelectedItem != null || drawingCtrl.GetTempPipe() != null)
{
/// Nothing or Element.Node was found
/// Clear all selections
drawingCtrl.SupressRedraws = true;
drawingCtrl.SelectedItem = null;
drawingCtrl.ClearTempPipe();
drawingCtrl.SupressRedraws = false;
}
UpdateTitle();
}
private void drawingCtrl_MouseDown(object sender, MouseEventArgs e)
{
Selection sel = GetSelection();
if (sel == Selection.PipeS || sel == Selection.PipeM || sel == Selection.PipeL || sel == Selection.PipeXL)
{
IDrawingItem item;
Pipe dummyPipe;
int nodeId;
if (drawingCtrl.FindElement(e.X, e.Y, Element.Node, out item, out dummyPipe, out nodeId) == Element.Node)
{
drawingCtrl.ShowStartNode(item, nodeId);
}
}
}
private void drawingCtrl_MouseUp(object sender, MouseEventArgs e)
{
Selection sel = GetSelection();
if (sel == Selection.PipeS || sel == Selection.PipeM || sel == Selection.PipeL || sel == Selection.PipeXL)
{
Sz sz = (sel == Selection.PipeS) ? Sz.S : (sel == Selection.PipeM) ? Sz.M : (sel == Selection.PipeL) ? Sz.L : Sz.XL;
int startNodeId;
IDrawingItem startItem = drawingCtrl.GetStartNode(out startNodeId);
if (startItem != null)
{
int endNodeId;
IDrawingItem endItem;
Pipe dummyPipe;
bool found = (drawingCtrl.FindElement(e.X, e.Y, Element.Node, out endItem, out dummyPipe, out endNodeId) == Element.Node);
if (found && (startItem != endItem))
{
Pipe newPipe = new Pipe(startItem, startNodeId, endItem, endNodeId, sz);
drawingCtrl.SupressRedraws = true;
drawingCtrl.ClearStartNode();
drawingCtrl.ShowTempPipe(newPipe);
drawingCtrl.SupressRedraws = false;
if (MessageBox.Show("Do you want to save this edge?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
/// Save the edge entered in the dialog
TBF.Rig.Various.Drawing.Pipes.Configuration pipesCfg = null;
switch (sz)
{
case Sz.S: pipesCfg = pipesScfg; break;
case Sz.M: pipesCfg = pipesMcfg; break;
case Sz.L: pipesCfg = pipesLcfg; break;
case Sz.XL: pipesCfg = pipesXLcfg; break;
}
if (pipesCfg != null)
{
string[] oriEdges = pipesCfg.Pipes;
pipesCfg.Pipes = new string[oriEdges.Length + 1];
for (int i = 0; i < oriEdges.Length; i++)
{
pipesCfg.Pipes[i] = oriEdges[i];
}
pipesCfg.Pipes[oriEdges.Length] = newPipe.ToString();
switch (sz)
{
case Sz.S: drawingCtrl.PipesS = pipesCfg.Pipes; break;
case Sz.M: drawingCtrl.PipesM = pipesCfg.Pipes; break;
case Sz.L: drawingCtrl.PipesL = pipesCfg.Pipes; break;
case Sz.XL: drawingCtrl.PipesXL = pipesCfg.Pipes; break;
}
}
}
drawingCtrl.ClearTempPipe();
}
else
{
drawingCtrl.ClearStartNode();
}
}
}
}
}
}