void context_UpdateFillSettingsEvent(FillSettingsModel fillSettings)
 {
     lbSampleTypes.DataContext = fillSettings;
     spGroupNumEditor.DataContext = fillSettings;
     if (_controlSettings != null)
     {
         lbSampleTypes.IsEnabled = _controlSettings.AllowFillMode;
         spGroupNumEditor.Visibility = _controlSettings.AllowFillMode ?
             Visibility.Visible : Visibility.Collapsed;
     }
 }
        private void ModifyLayoutPosEditorForPosition(LayoutPos layoutPos, ref LayoutPosEditor layoutPosEditor, FillSettingsModel fillSettings)
        {
            layoutPosEditor.LayoutPos = layoutPos;
            SampleType sampleType = fillSettings.GetSampleTypeById(layoutPos.TypeId);

            string hoverText = string.Empty;
            if (layoutPos.IsUsed)
            {
                hoverText = string.Format("{0}{1}", sampleType.Name, layoutPos.Group);
            }
            layoutPosEditor.HoverText = hoverText;
            layoutPosEditor.Colour = ColourTransformer.GetColorFromName(sampleType.Colour);
        }
 private LayoutPosEditor FillPosition(LayoutPosEditor layoutPosEditor, int typeId, int group, FillSettingsModel fillSettings)
 {
     LayoutPos layoutPos = layoutPosEditor.LayoutPos;
     layoutPos.TypeId = typeId;
     layoutPos.Group = group;
     ModifyLayoutPosEditorForPosition(layoutPos, ref layoutPosEditor, fillSettings);
     return layoutPosEditor;
 }
        // Returns the new group number
        public int FillFirstAndLast(int pos1, int pos2, int typeId, int group, Direction fillDirection, int replicates, bool rectangleMode, RepDirection replicateDirection, FillSettingsModel fillSettings)
        {
            CurrentState.CheckPositionInRangeOneBased(pos1);
            CurrentState.CheckPositionInRangeOneBased(pos2);

            // Create a copy for the new state (this is necessary as changes to the layout are made in this way to avoid unnecessary updates)
            SingleLayoutEditor newState = CurrentState.Clone();
            if (pos1 == pos2)
            {
                FillPosition(newState.LayoutPositions[pos1 - 1], typeId, group, fillSettings);
                var numPositionsOfFilledGroupType = newState.GetPositionsOfGroupType(group, typeId).Count();
                if (numPositionsOfFilledGroupType == fillSettings.Replicates)
                {
                    if (group < fillSettings.MaxGroupNum)
                        group++;
                }
            }
            else
            {
                if (rectangleMode == true)
                {
                    //group = FillRectangleMode(pos1, pos2, typeId, group, fillDirection, replicates, replicateDirection, fillSettings, newState);
                    IEnumerable<LayoutPosEditor> fillEnumerable;
                    if (fillDirection == Direction.Across)
                        fillEnumerable = newState.GetEnumerableRectangleAcross(pos1, pos2);
                    else
                        fillEnumerable = newState.GetEnumerableRectangleDown(pos1, pos2);

                    int startCol = newState.GetColFromPos(pos1);
                    int endCol = newState.GetColFromPos(pos2);

                    int startRow = newState.GetRowFromPos(pos1);
                    int endRow = newState.GetRowFromPos(pos2);

                    int colWidth = Math.Abs(endCol - startCol) + 1;
                    int rowWidth = Math.Abs(endRow - startRow) + 1;

                    int replicateCount = 1;
                    int i = 1, j = 1;
                    foreach (LayoutPosEditor layoutPosEditor in fillEnumerable)
                    {
                        FillPosition(layoutPosEditor, typeId, group, fillSettings);
                        if (replicateDirection == RepDirection.ByCol && fillDirection == Direction.Down)
                        {
                            if (replicateCount == replicates || i % rowWidth == 0)
                            {
                                replicateCount = 1;
                                group++;
                            }
                            else
                                replicateCount++;
                            if (i % rowWidth == 0)
                                i = 1;
                            else
                                i++;
                        }
                        else if (replicateDirection == RepDirection.ByCol && fillDirection == Direction.Across)
                        {
                            if (i == colWidth)
                            {
                                i = 1;
                                if (j % replicates == 0)
                                {
                                    j = 1;
                                    group++;
                                }
                                else
                                {
                                    j++;
                                    group -= colWidth - 1;
                                }
                            }
                            else
                            {
                                i++;
                                group++;
                            }
                        }
                        else if (replicateDirection == RepDirection.ByRow && fillDirection == Direction.Across)
                        {
                            if (replicateCount == replicates || i % colWidth == 0)
                            {
                                replicateCount = 1;
                                group++;
                            }
                            else
                                replicateCount++;
                            if (i % colWidth == 0)
                                i = 1;
                            else
                                i++;
                        }
                        else if (replicateDirection == RepDirection.ByRow && fillDirection == Direction.Down)
                        {
                            if (i == rowWidth)
                            {
                                i = 1;
                                if (j % replicates == 0)
                                {
                                    j = 1;
                                    group++;
                                }
                                else
                                {
                                    j++;
                                    group -= rowWidth - 1;
                                }
                            }
                            else
                            {
                                i++;
                                group++;
                            }
                        }
                    }
                }
                else
                {
                    // Get the enumerable which is the fill ordering
                    IEnumerable<LayoutPosEditor> fillEnumerable;
                    if (fillDirection == Direction.Across)
                    {
                        fillEnumerable = newState.GetEnumerableAcross(pos1, pos2);
                    }
                    else
                    {
                        fillEnumerable = newState.GetEnumerableDown(pos1, pos2);
                    }

                    // Fill with the enumerable using the specified number of replicates
                    int replicateCount = 1;
                    foreach (LayoutPosEditor layoutPosEditor in fillEnumerable)
                    {
                        FillPosition(layoutPosEditor, typeId, group, fillSettings);

                        if (replicateCount == replicates)
                        {
                            replicateCount = 1;
                            group++;
                        }
                        else
                            replicateCount++;
                    }
                }
            }
            // Store the new state - this is necessary and applies only the changes between the previous state and the new state
            CurrentState = newState;
            return group;
        }
        // Creates a SingleLayoutEditor from a SingleLayoutLight object
        // This is used when data is received from the server, it is converted to a form more suitable
        // for working with the PlateControl.
        // The main differences are that:
        // 1. A SingleLayoutLight only lists used wells, whereas a SingleLayoutEditor has an entry for every position
        public SingleLayoutEditor CreateSingleLayoutEditor(SingleLayoutLight singleLayoutLight, int width, int height, FillSettingsModel fillSettings)
        {
            SingleLayoutEditor singleLayoutEditor = new SingleLayoutEditor(width, height);
            foreach (LayoutPos layoutPos in singleLayoutLight.LayoutPositions)
            {
                int position = layoutPos.Id;
                singleLayoutEditor.CheckPositionInRangeOneBased(position);

                LayoutPosEditor layoutPosEditor = singleLayoutEditor[position - 1];
                ModifyLayoutPosEditorForPosition(layoutPos, ref layoutPosEditor, fillSettings);
            }
            return singleLayoutEditor;
        }
        public void InitializePlateControl(LayoutEditorPopulation layoutEditorPopulation)
        {
            if (layoutEditorPopulation == null)
                return;
            if (Equals(_userSettings, null))
                throw new InvalidOperationException("Please bind the UserSettings firstly");

            _layoutEditorPopulation = layoutEditorPopulation;
            ControlSettings = new ControlSettingsModel();

            _plateControl.PosMouseCursor = Cursors.Arrow;
            _plateControl.SkipCheckForFontSizeRecalc = true;

            _plateControl.LocationMouseButtonUp += new PlateControl2DSilverlight.MouseEvents.LocationMouseButtonUpEventHandler(plateControl_LocationMouseButtonUp);
            _plateControl.LocationMouseButtonDown += new PlateControl2DSilverlight.MouseEvents.LocationMouseButtonDownEventHandler(plateControl_LocationMouseButtonDown);
            _plateControl.LocationMouseOver += new PlateControl2DSilverlight.MouseEvents.LocationMouseOverEventHandler(plateControl2D_LocationMouseOver);

            FillSettings = new FillSettingsModel(_userSettings) { GroupNum = 1, SelectedSampleTypeIndex = -1 };
            FillSettings.PropertyChanged += _fillSettings_PropertyChanged;

            FillSettings.SampleTypes = _layoutEditorPopulation.SampleTypes;
            FillSettings.SelectedSampleTypeIndex = FillSettings.SampleTypes.Count - 1;

            _editorStateHelper = new EditorStateHelper(_layoutEditorPopulation, _userSettings) { };
            _editorStateHelper.PropertyChanged += _editorStateHelper_PropertyChanged;

            // When using RackMode, force fill mode to be across (down doesn't make much sense in rack mode)
            if (_layoutEditorPopulation.RackMode)
            {
                FillSettings.FillDirection = Direction.Across;
                FillSettings.ShowNextTime = true;
            }
            // Fill mode is allowed if we are NOT in EraseOnly and we are NOT in flag mode
            ControlSettings.AllowFillMode = !_layoutEditorPopulation.EraseOnly && !_userSettings.IsFlagMode;
            ControlSettings.IsFlagMode = _userSettings.IsFlagMode;
            // Clear button is available unless in Erase only mode (although it is only enabled when there is something to clear).
            ControlSettings.AllowClearButton = !_layoutEditorPopulation.EraseOnly;
            // If Fill mode is not allowed then set SelectedSampleTypeIndex to -1 so ListBox has no items selected (i.e. it looks just like a legend display)
            if (!ControlSettings.AllowFillMode)
                FillSettings.SelectedSampleTypeIndex = -1;
            ControlSettings.SelectionCommand = ControlSettings.IsFlagMode ? SelectionCommand.Flag : SelectionCommand.Erase;

            if (LoadUserLayoutEvent != null)
            {
                var loadUserLayoutmodel = new LoadUserLayoutModel();
                loadUserLayoutmodel.UserSettings = _userSettings;
                loadUserLayoutmodel.IsFlagMode = ControlSettings.IsFlagMode;
                LoadUserLayoutEvent(loadUserLayoutmodel);
            }
            OnUpdateControlSettings();
        }