Skip to content

File TableContainer.xaml.cs

File List > PGRControls > TableContainer.xaml.cs

Go to the documentation of this file

//=============================================================================
// Copyright (c) 2026 FLIR Integrated Imaging Solutions, Inc. All Rights Reserved.
//
// This software is the confidential and proprietary information of FLIR
// Integrated Imaging Solutions, Inc. ("Confidential Information"). You
// shall not disclose such Confidential Information and shall use it only in
// accordance with the terms of the license agreement you entered into
// with FLIR Integrated Imaging Solutions, Inc. (FLIR)
//
// FLIR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
// SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE, OR NON-INFRINGEMENT. FLIR SHALL NOT BE LIABLE FOR ANY DAMAGES
// SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
// THIS SOFTWARE OR ITS DERIVATIVES.
//=============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SpinnakerNET.GenApi;
using System.Diagnostics;

namespace SpinnakerNET.GUI
{
    namespace WPFControls
    {
#region HELPERS
        internal struct ItemPosition
        {
            public int rowIndex;
            public int colIndex;

            public ItemPosition(int row, int col)
            {
                rowIndex = row;
                colIndex = col;
            }
        };

        internal struct ControlPositionInfo
        {
            public ICameraControl CameraControl;
            public ItemPosition Position;

            public ControlPositionInfo(ICameraControl control, ItemPosition position)
            {
                this.CameraControl = control;
                this.Position = position;
            }
        }
#endregion



        public sealed partial class TableContainer : BaseClass
        {
#region FIELDS
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(TableContainer));
            private List<ControlPositionInfo>_children;
            // Since control might not be created when parsing its alignment information,
            // we use ControlID(string) to identify them in string list
            private List<System.Windows.HorizontalAlignment>_cellHorizontalAlignmentStringList;
            private List<System.Windows.VerticalAlignment>_cellVerticalAlignmentStringList;

            // Following two dictionary is used to store alignment information for actual control object
            private Dictionary<ControlPositionInfo, System.Windows.HorizontalAlignment>_cellHorizontalAlignment;
            private Dictionary<ControlPositionInfo, System.Windows.VerticalAlignment>_cellVerticalAlignment;

            // Row and column span list for all children
            private List<int>_rowSpanList;
            private List<int>_columnSpanList;

            private List<string>_childrenStringList;
            private List<ItemPosition>_childrenPositionList;
            private int _numOfRows, _numOfCols;
            private Dictionary<int, string>_rowHeights;
            private Dictionary<int, string>_colWidths;
            private bool _borderVisible = false;
            private bool _nodemapRefreshAllowed = true;
            private XMLParser _parser;
            private List<string>_requiredChildren;
            private bool _isComplete = true;
#endregion

#region PROPERTIES
            public bool IsComplete
            {
                get
                {
                    return _isComplete;
                }
            }

            internal List<string>RequiredChildren
            {
                get
                {
                    return _requiredChildren;
                }
                set
                {
                    _requiredChildren = value;
                }
            }

            internal XMLParser Parser
            {
                set
                {
                    this._parser = value;
                }
                get
                {
                    return _parser;
                }
            }

            internal Grid LayOutGrid
            {
                get
                {
                    return this.grid;
                }
            }

            public override bool NameLabelBoldness
            {
                get
                {
                    return _nameLabelBoldness;
                }
                set
                {
                    _nameLabelBoldness = value;
                    if (_nameLabelBoldness)
                    {
                        nameLabel.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        nameLabel.FontWeight = FontWeights.Normal;
                    }
                }
            }

            public List<string>ChildrenStringList
            {
                get
                {
                    return _childrenStringList;
                }
                set
                {
                    this._childrenStringList = value;
                }
            }

            internal List<ItemPosition>ChildrenPositionList
            {
                get
                {
                    return _childrenPositionList;
                }
                set
                {
                    this._childrenPositionList = value;
                }
            }

            public override string ControlNameLabel
            {
                get
                {
                    if (nameLabel.Content != null)
                    {
                        return nameLabel.Content.ToString();
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                set
                {
                    nameLabel.Content = value;
                }
            }

            public int NumberOfRows
            {
                get
                {
                    return _numOfRows;
                }
                set
                {
                    this._numOfRows = value;
                }
            }

            public int NumberOfColumns
            {
                get
                {
                    return _numOfCols;
                }
                set
                {
                    this._numOfCols = value;
                }
            }

            public bool ShowBorder
            {
                get
                {
                    return _borderVisible;
                }
                set
                {
                    _borderVisible = value;
                    if (_borderVisible)
                    {
                        border.BorderThickness = new System.Windows.Thickness(1);
                    }
                    else
                    {
                        border.BorderThickness = new System.Windows.Thickness(0);
                    }
                }
            }

            public bool ShowGridLine
            {
                get
                {
                    return grid.ShowGridLines;
                }
                set
                {
                    grid.ShowGridLines = value;
                }
            }

            public bool NodemapRefreshAllowed
            {
                get
                {
                    return _nodemapRefreshAllowed;
                }
                set
                {
                    _nodemapRefreshAllowed = value;
                }
            }

            public Dictionary<int, string>RowHeights
            {
                get
                {
                    return _rowHeights;
                }
                set
                {
                    this._rowHeights = value;
                }
            }

            public Dictionary<int, string>ColumnWidths
            {
                get
                {
                    return _colWidths;
                }
                set
                {
                    this._colWidths = value;
                }
            }

            public List<System.Windows.HorizontalAlignment>CellHorizontalAlignmentStringList
            {
                get
                {
                    return _cellHorizontalAlignmentStringList;
                }
                set
                {
                    this._cellHorizontalAlignmentStringList = value;
                }
            }

            public List<System.Windows.VerticalAlignment>CellVerticalAlignmentStringList
            {
                get
                {
                    return _cellVerticalAlignmentStringList;
                }
                set
                {
                    this._cellVerticalAlignmentStringList = value;
                }
            }

            public double ControlMargin
            {
                get
                {
                    return this.Margin.Left;
                }
                set
                {
                    this.Margin = new Thickness(value);
                }
            }

            public List<int>RowSpanList
            {
                get
                {
                    return _rowSpanList;
                }
                set
                {
                    _rowSpanList = value;
                }
            }

            public List<int>ColumnSpanList
            {
                get
                {
                    return _columnSpanList;
                }
                set
                {
                    _columnSpanList = value;
                }
            }

            public CameraControlCollection Dependency
            {
                get
                {
                    return _dependencyControlList;
                }
                set
                {
                    this._dependencyControlList = value;
                }
            }

#endregion

#region CONSTRUCTORS
            public TableContainer()
            {
                InitializeComponent();
                _requiredChildren = new List<string>();
                _children = new List<ControlPositionInfo>();
                _cellVerticalAlignment = new Dictionary<ControlPositionInfo, VerticalAlignment>();
                _cellHorizontalAlignment = new Dictionary<ControlPositionInfo, HorizontalAlignment>();
                _cellHorizontalAlignmentStringList = new List<HorizontalAlignment>();
                _cellVerticalAlignmentStringList = new List<VerticalAlignment>();
                _columnSpanList = new List<int>();
                _rowSpanList = new List<int>();
            }

            internal TableContainer(TableContainer originalControl)
            {
                InitializeComponent();
                _requiredChildren = new List<string>();
                _children = new List<ControlPositionInfo>();
                _cellVerticalAlignment = new Dictionary<ControlPositionInfo, VerticalAlignment>();
                _cellHorizontalAlignment = new Dictionary<ControlPositionInfo, HorizontalAlignment>();
                _cellHorizontalAlignmentStringList = new List<HorizontalAlignment>();
                _cellVerticalAlignmentStringList = new List<VerticalAlignment>();
                _columnSpanList = new List<int>();
                _rowSpanList = new List<int>();

                // Common Properties
                this.SetMapper(originalControl.GetMapper());
                this.SetNameLabelBoldness(originalControl.NameLabelBoldness);
                this.SetVisibility(originalControl.Visibility);
                this.SetControlID(originalControl.ControlID);
                this.SetMin(originalControl.GetMin());
                this.SetMax(originalControl.GetMax());
                this.SetControlNameLabel(originalControl.ControlNameLabel);
                this.SetNameLabelVisibility(originalControl.GetNameLabelVisibility());
                this.SetNameLabelBoldness(originalControl.GetNameLabelBoldness());
                try
                {
                    this.SetToolTip(originalControl.GetToolTip());
                }
                catch (System.Exception /*ex*/)
                {
                }
                this.SetMaximumControlSize(originalControl.GetMaximumControlSize());
                this.SetMinimumControlSize(originalControl.GetMinimumControlSize());
                this.SetControlHeight(originalControl.GetControlHeight());
                this.SetControlWidth(originalControl.GetControlWidth());
                this.SetRefreshTime(originalControl.GetRefreshTime());
                this.SetMargin(originalControl.GetMargin());

                // Table specific properties
                this.Parser = originalControl.Parser;
                this.NumberOfColumns = originalControl.NumberOfColumns;
                this.NumberOfRows = originalControl.NumberOfRows;
                this.ChildrenStringList = originalControl.ChildrenStringList;
                this.ChildrenPositionList = originalControl.ChildrenPositionList;
                this.RowHeights = originalControl.RowHeights;
                this.ColumnWidths = originalControl.ColumnWidths;
                this.CellHorizontalAlignmentStringList = originalControl._cellHorizontalAlignmentStringList;
                this.CellVerticalAlignmentStringList = originalControl._cellVerticalAlignmentStringList;
                this.ShowBorder = originalControl.ShowBorder;
                this.ShowGridLine = originalControl.ShowGridLine;
                this.NodemapRefreshAllowed = originalControl.NodemapRefreshAllowed;
                this.RowSpanList = originalControl.RowSpanList;
                this.ColumnSpanList = originalControl.ColumnSpanList;

                // Caller will call BuildTable for population
            }
#endregion

#region TABLE_BEHAVIOR_CONTROL
            public void SetBorderVisibility(bool visible)
            {
                if (visible)
                {
                    _borderVisible = true;
                    border.BorderThickness = new System.Windows.Thickness(1);
                }
                else
                {
                    _borderVisible = false;
                    border.BorderThickness = new System.Windows.Thickness(0);
                }
            }

            public bool GetBorderVisibility()
            {
                return _borderVisible;
            }

            public bool BuildTable(CameraControlCollection childControls)
            {
                try
                {
                    // Build Columns
                    if (ColumnWidths != null && ColumnWidths.Count > 0)
                    {
                        for (int i = 0; i < _numOfCols; i++)
                        {
                            bool foundWidth = false;
                            // Build using exact column width
                            foreach(KeyValuePair<int, string>pair in _colWidths)
                            {
                                if (pair.Key == i)
                                {
                                    ColumnDefinition col = new ColumnDefinition();
                                    if (pair.Value.ToLower() == "*")
                                    {
                                        col.Width = new GridLength(1, GridUnitType.Star);
                                    }
                                    else if (pair.Value.ToLower() == "auto")
                                    {
                                        col.Width = new GridLength(1, GridUnitType.Auto);
                                    }
                                    else
                                    {
                                        double tempWidth = 0;
                                        if (double.TryParse(pair.Value, out tempWidth))
                                        {
                                            col.Width = new GridLength(tempWidth);
                                        }
                                    }

                                    grid.ColumnDefinitions.Add(col);
                                    foundWidth = true;
                                    break;
                                }
                            }

                            if (!foundWidth)
                            {
                                ColumnDefinition col = new ColumnDefinition();
                                col.Width = new GridLength(1, GridUnitType.Auto);
                                grid.ColumnDefinitions.Add(col);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < _numOfCols; i++)
                        {
                            ColumnDefinition col = new ColumnDefinition();
                            col.Width = new GridLength(1, GridUnitType.Star);
                            grid.ColumnDefinitions.Add(col);
                        }
                    }

                    // Build Rows
                    if (_rowHeights != null && _rowHeights.Count > 0)
                    {
                        for (int i = 0; i < _numOfRows; i++)
                        {
                            bool foundWidth = false;
                            // Build using exact column width or auto or *
                            foreach(KeyValuePair<int, string>pair in _rowHeights)
                            {
                                if (pair.Key == i)
                                {
                                    RowDefinition row = new RowDefinition();
                                    if (pair.Value.ToLower() == "*")
                                    {
                                        row.Height = new GridLength(1, GridUnitType.Star);
                                    }
                                    else if (pair.Value.ToLower() == "auto")
                                    {
                                        row.Height = new GridLength(1, GridUnitType.Auto);
                                    }
                                    else
                                    {
                                        double tempHeight = 0;
                                        if (double.TryParse(pair.Value, out tempHeight))
                                        {
                                            row.Height = new GridLength(tempHeight);
                                        }
                                    }

                                    grid.RowDefinitions.Add(row);
                                    foundWidth = true;
                                    break;
                                }
                            }

                            if (!foundWidth)
                            {
                                RowDefinition row = new RowDefinition();
                                row.Height = new GridLength(1, GridUnitType.Auto);
                                grid.RowDefinitions.Add(row);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < _numOfRows; i++)
                        {
                            RowDefinition row = new RowDefinition();
                            row.Height = new GridLength(1, GridUnitType.Star);
                            grid.RowDefinitions.Add(row);
                        }
                    }

                    // Find and add child controls to dictionary container
                    if (_children.Count == 0)
                    {
                        if (_childrenStringList != null && _childrenStringList.Count > 0 &&
                            _childrenPositionList != null && _childrenPositionList.Count > 0)
                        {
                            for (int i = 0; i < _childrenStringList.Count; i++)
                            {
                                bool childFound = false;

                                foreach(ICameraControl control in childControls)
                                {
                                    if (control == null)
                                    {
                                        continue;
                                    }

                                    if (_childrenStringList[i].Equals(control.GetControlID()))
                                    {
                                        // Found child control in collection.
                                        // Add it to dictionary
                                        _children.Add(new ControlPositionInfo(control, _childrenPositionList[i]));

                                        HorizontalAlignment horizontalalignment = _cellHorizontalAlignmentStringList[i];
                                        VerticalAlignment verticalAlignment = _cellVerticalAlignmentStringList[i];

                                        // Find and add control's Horizontal alignment information
                                        _cellHorizontalAlignment.Add(
                                            new ControlPositionInfo(control, _childrenPositionList[i]),
                                            horizontalalignment);

                                        // Find and add control's Vertical alignment information
                                        _cellVerticalAlignment.Add(
                                            new ControlPositionInfo(control, _childrenPositionList[i]),
                                            verticalAlignment);

                                        childFound = true;

                                        break;
                                    }
                                }

                                if (!childFound)
                                {
                                    // Indicate that table contains missing child
                                    _isComplete = false;

                                    // Check whether this is a required control
                                    if (_requiredChildren.Contains(_childrenStringList[i]))
                                    {
                                        // It is required. Table building will stop unless in debug mode
                                        if (_parser != null && _parser.DebugModeEnabled)
                                        {
                                            log.Warn(string.Format(
                                                "{2}Essential child \"{0}\" was missing from \"{1}\" table container.",
                                                _childrenStringList[i],
                                                ControlID,
                                                FormattedSerialNumber));

                                            log.Warn(string.Format(
                                                "{2}Creating dummy child \"{0}\" for \"{1}\" table container.",
                                                _childrenStringList[i],
                                                ControlID,
                                                FormattedSerialNumber));

                                            // Get dummy control for this empty child
                                            BaseClass dummyControl =
                                                _parser.CreateDummyControl(_childrenStringList[i], true);

                                            _children.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]));

                                            HorizontalAlignment horizontalalignment =
                                                _cellHorizontalAlignmentStringList[i];
                                            VerticalAlignment verticalAlignment = _cellVerticalAlignmentStringList[i];

                                            // Find and add control's Horizontal alignment information
                                            _cellHorizontalAlignment.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]),
                                                horizontalalignment);

                                            // Find and add control's Vertical alignment information
                                            _cellVerticalAlignment.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]),
                                                verticalAlignment);
                                        }
                                        else
                                        {
                                            // Empty table
                                            log.Warn(string.Format(
                                                "{2}Essential child \"{0}\" was missing from \"{1}\" table control. Returning empty table.",
                                                _childrenStringList[i],
                                                ControlID,
                                                FormattedSerialNumber));
                                            _featureAvailable = false;
                                            return false;
                                        }
                                    }
                                    else
                                    {
                                        // It's an optional control.
                                        // Let's create dummy control if debug mode was active
                                        // It is required. Table building will stop unless in debug mode
                                        if (_parser != null && _parser.DebugModeEnabled)
                                        {
                                            log.Warn(string.Format(
                                                "{2}Creating dummy child \"{0}\" for \"{1}\" table container.",
                                                _childrenStringList[i],
                                                ControlID,
                                                FormattedSerialNumber));

                                            // Get dummy control for this empty child
                                            BaseClass dummyControl = _parser.CreateDummyControl(_childrenStringList[i]);

                                            _children.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]));

                                            HorizontalAlignment horizontalalignment =
                                                _cellHorizontalAlignmentStringList[i];
                                            VerticalAlignment verticalAlignment = _cellVerticalAlignmentStringList[i];

                                            // Find and add control's Horizontal alignment information
                                            _cellHorizontalAlignment.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]),
                                                horizontalalignment);

                                            // Find and add control's Vertical alignment information
                                            _cellVerticalAlignment.Add(
                                                new ControlPositionInfo(dummyControl, _childrenPositionList[i]),
                                                verticalAlignment);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            // Empty table
                            _featureAvailable = false;
                            return false;
                        }
                    }

                    for (int i = 0; i < _children.Count; i++)
                    {
                        // Position Child controls
                        ControlPositionInfo child = _children[i];

                        // Set Row and Column Index
                        UserControl control = (UserControl) child.CameraControl;

                        if (control == null)
                        {
                            continue;
                        }

                        if (control.Parent != null)
                        {
                            UserControl newDuplicatedControl =
                                Parser.GetControlByName(child.CameraControl.GetControlID());
                            control = newDuplicatedControl;
                        }
                        Grid.SetRow(control, child.Position.rowIndex);
                        Grid.SetColumn(control, child.Position.colIndex);

                        // Set Horizontal Alignment
                        System.Windows.HorizontalAlignment horizontalAlignment;
                        if (_cellHorizontalAlignment.TryGetValue(child, out horizontalAlignment))
                        {
                            control.HorizontalAlignment = horizontalAlignment;
                        }
                        else
                        {
                            control.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                        }

                        // Set Vertical Alignment
                        System.Windows.VerticalAlignment verticalAlignment;
                        if (_cellVerticalAlignment.TryGetValue(child, out verticalAlignment))
                        {
                            control.VerticalAlignment = verticalAlignment;
                        }
                        else
                        {
                            control.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                        }

                        // Set Row and Column Span
                        Grid.SetRowSpan(control, _rowSpanList[i]);
                        Grid.SetColumnSpan(control, _columnSpanList[i]);

                        // Add Control to Grid
                        grid.Children.Add(control);
                    }

                    if (grid.Children.Count == 0 && (_parser != null && !_parser.DebugModeEnabled))
                    {
                        // Empty container is not return in release mode
                        log.Debug(string.Format(
                            "{1}Empty table container \"{0}\" is not returned due to Debug Flag = false",
                            ControlID,
                            FormattedSerialNumber));
                        _featureAvailable = false;
                        return false;
                    }
                    else
                    {
                        // Empty container is returned in debug mode or if container is non-empty
                        _featureAvailable = true;
                        return true;
                    }
                }
                catch (System.Exception ex)
                {
                    _featureAvailable = false;
                    log.Error(string.Format("{0}Problem building grid layout.", FormattedSerialNumber), ex);
                    throw new Exception(string.Format("Problem building Grid layout! {0}", ex.Message));
                }
            }
#endregion

#region ICameraControl Interface

            public override void SetToolTip(string tooltip)
            {
                this.grid.ToolTip = tooltip;
            }

            public override void SetControlNameLabel(string nameLabel)
            {
                if (nameLabel != null)
                {
                    this.nameLabel.Content = nameLabel;
                }
            }

            public override void SetNameLabelVisibility(System.Windows.Visibility visibility)
            {
                this.nameLabel.Visibility = visibility;
            }

            public override void SetRefreshTime(int seconds)
            {
                this._refreshTime = seconds;

                // Start timer if an valid refresh time was provided
                if (this._refreshTime > 0)
                {

                    _timer = new System.Windows.Forms.Timer();
                    _timer.Interval = this._refreshTime;
                    _timer.Tick += new EventHandler(timer_Tick);
                    _timer.Start();
                }
            }

            void timer_Tick(object sender, EventArgs e)
            {
                Refresh();
            }

            public override void Refresh()
            {
                if (_children != null && _children.Count > 0)
                {
                    foreach(ControlPositionInfo info in _children)
                    {
                        info.CameraControl.Refresh();
                    }
                }
            }

            public override string GetToolTip()
            {
                return this.grid.ToolTip.ToString();
            }

            public override string GetControlNameLabel()
            {
                return ControlNameLabel;
            }

            public override System.Windows.Visibility GetNameLabelVisibility()
            {
                return this.nameLabel.Visibility;
            }

            public override System.Windows.Visibility GetVisibility()
            {
                return this.Visibility;
            }

            public override string GetNodeToControl()
            {
                return string.Empty;
            }

            public override void Connect(INodeMap nodemap, string nodename, string namelabel = "")
            {
                // Not applicable
            }

            public override void Disconnect()
            {
                if (_timer != null)
                {
                    _timer.Stop();
                    _timer.Dispose();
                }

                if (_children != null && _children.Count > 0)
                {
                    foreach(ControlPositionInfo info in _children)
                    {
                        try
                        {
                            if (info.CameraControl != null)
                            {
                                info.CameraControl.Disconnect();
                            }
                        }
                        catch (System.Exception /*ex*/)
                        {
                        }
                    }
                }
            }

#endregion
        }

    }
}