Skip to content

File StackPanelContainer.xaml.cs

File List > PGRControls > StackPanelContainer.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
    {


        public sealed partial class StackPanelContainer : BaseClass
        {
#region FIELDS
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(StackPanelContainer));
            // 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<ICameraControl, System.Windows.HorizontalAlignment>_cellHorizontalAlignment;
            private Dictionary<ICameraControl, System.Windows.VerticalAlignment>_cellVerticalAlignment;

            private List<string>_childrenStringList;
            private CameraControlCollection _children;
            private bool _borderVisible = false;
            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 StackPanel RootPanel
            {
                get
                {
                    return this.panelLayout;
                }
            }

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

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

            public int NumberOfChildren
            {
                get
                {
                    return panelLayout.Children.Count;
                }
            }

            public Orientation LayoutOrientation
            {
                get
                {
                    return panelLayout.Orientation;
                }
                set
                {
                    this.panelLayout.Orientation = 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 override bool NameLabelBoldness
            {
                get
                {
                    return _nameLabelBoldness;
                }
                set
                {
                    _nameLabelBoldness = value;
                    if (_nameLabelBoldness)
                    {
                        nameLabel.FontWeight = FontWeights.Bold;
                    }
                    else
                    {
                        nameLabel.FontWeight = FontWeights.Normal;
                    }
                }
            }

            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);
                    }
                }
            }

#endregion

#region CONSTRUCTORS
            public StackPanelContainer()
            {
                InitializeComponent();
                _requiredChildren = new List<string>();
                _children = new CameraControlCollection();
                _cellVerticalAlignment = new Dictionary<ICameraControl, VerticalAlignment>();
                _cellHorizontalAlignment = new Dictionary<ICameraControl, HorizontalAlignment>();
                _cellHorizontalAlignmentStringList = new List<HorizontalAlignment>();
                _cellVerticalAlignmentStringList = new List<VerticalAlignment>();
            }

            internal StackPanelContainer(StackPanelContainer originalControl)
            {
                InitializeComponent();
                _requiredChildren = new List<string>();
                _children = new CameraControlCollection();
                _cellVerticalAlignment = new Dictionary<ICameraControl, VerticalAlignment>();
                _cellHorizontalAlignment = new Dictionary<ICameraControl, HorizontalAlignment>();
                _cellHorizontalAlignmentStringList = new List<HorizontalAlignment>();
                _cellVerticalAlignmentStringList = new List<VerticalAlignment>();

                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());
                this.LayoutOrientation = originalControl.LayoutOrientation;
                this.Parser = originalControl.Parser;

                // Stackpanel specific properties
                this.ChildrenStringList = originalControl.ChildrenStringList;
                this.CellHorizontalAlignmentStringList = originalControl._cellHorizontalAlignmentStringList;
                this.CellVerticalAlignmentStringList = originalControl._cellVerticalAlignmentStringList;
                this.ShowBorder = originalControl.ShowBorder;
            }
#endregion

#region POPULATION
            public bool PopulateStackPanel(CameraControlCollection childControls)
            {
                try
                {
                    // Find and add child controls to dictionary container
                    if (_childrenStringList != null && _childrenStringList.Count > 0)
                    {
                        // Clear children list
                        _children.Clear();

                        for (int i = 0; i < _childrenStringList.Count; i++)
                        {
                            bool childFound = false;

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

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

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

                                    // Find and add control's Horizontal alignment information
                                    _cellHorizontalAlignment.Add(control, horizontalalignment);

                                    // Find and add control's Vertical alignment information
                                    _cellVerticalAlignment.Add(control, verticalAlignment);

                                    childFound = true;

                                    break;
                                }
                            }

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

                                // If its an required control, stackpanel population will stop
                                // unless debug mode was active
                                if (_requiredChildren.Contains(_childrenStringList[i]))
                                {
                                    if (_parser != null && _parser.DebugModeEnabled)
                                    {
                                        log.Warn(string.Format(
                                            "{2}Essential child \"{0}\" was missing from \"{1}\" StackPanel container.",
                                            _childrenStringList[i],
                                            ControlID,
                                            FormattedSerialNumber));

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

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

                                        _children.Add(dummyControl);

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

                                        // Find and add control's Horizontal alignment information
                                        _cellHorizontalAlignment.Add(dummyControl, horizontalalignment);

                                        // Find and add control's Vertical alignment information
                                        _cellVerticalAlignment.Add(dummyControl, verticalAlignment);
                                    }
                                    else
                                    {
                                        // Empty StackPanel
                                        log.Warn(string.Format(
                                            "{2}Essential child \"{0}\" was missing from \"{1}\" StackPanel container control. Returning empty container.",
                                            _childrenStringList[i],
                                            ControlID,
                                            FormattedSerialNumber));

                                        _featureAvailable = false;
                                        return false;
                                    }
                                }
                                else
                                {
                                    // Its an optional control
                                    if (_parser != null && _parser.DebugModeEnabled)
                                    {
                                        log.Warn(string.Format(
                                            "{2}Creating dummy child \"{0}\" for \"{1}\" StackPanel container.",
                                            _childrenStringList[i],
                                            ControlID,
                                            FormattedSerialNumber));

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

                                        _children.Add(dummyControl);

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

                                        // Find and add control's Horizontal alignment information
                                        _cellHorizontalAlignment.Add(dummyControl, horizontalalignment);

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

                    // Position Child controls
                    for (int i = 0; i < _children.Count; i++)
                    {
                        // Set Row and Column Index
                        UserControl control = (UserControl) _children[i];
                        if (control == null)
                        {
                            continue;
                        }

                        if (control.Parent != null)
                        {
                            UserControl newDuplicatedControl = Parser.GetControlByName(_children[i].GetControlID());
                            control = newDuplicatedControl;
                        }

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

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

                        panelLayout.Children.Add(control);
                    }

                    if (panelLayout.Children.Count == 0 && (_parser != null && !_parser.DebugModeEnabled))
                    {
                        // Empty container is not return in release mode
                        // Empty table
                        log.Debug(string.Format(
                            "{1}Empty StackPanel 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)
                {
#if DEBUG
                    // throw new InvalidOperationException(ex.Message);
#endif
                    log.Error(string.Format("{0}Problem building StackPanel Container.", FormattedSerialNumber), ex);
                    _featureAvailable = false;
                    return false;
                }
            }
#endregion

#region ICameraControl Interface
            public override void Connect(INodeMap nodemap, string nodeName, string nameLabel)
            {
                // throw new NotImplementedException("User Build or Populate methods instead for Container controls.");
            }

            public override void Disconnect()
            {
                if (_children != null && _children.Count > 0)
                {
                    foreach(ICameraControl control in _children)
                    {
                        try
                        {
                            control.Disconnect();
                        }
                        catch (System.Exception /*ex*/)
                        {
                        }
                    }
                }
            }

            public override void SetToolTip(string tooltip)
            {
                this.panelLayout.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 Refresh()
            {
                if (_children != null && _children.Count > 0)
                {
                    foreach(ICameraControl control in _children)
                    {
                        control.Refresh();
                    }
                }
            }

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

            // public string GetControlNameLabel()
            //{
            //    return this.nameLabel.Content.ToString();
            //}
            public override System.Windows.Visibility GetNameLabelVisibility()
            {
                return this.nameLabel.Visibility;
            }

            public override int GetRefreshTime()
            {
                return 0;
            }

            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 override string GetNodeToControl()
            {
                return string.Empty;
            }
#endregion
        }

    }
}