private void SampleProbePoints()
        {
            if (waitingToCompleteNextSample)
            {
                return;
            }

            double startProbeHeight = printer.Settings.GetValue <double>(SettingsKey.print_leveling_probe_start);

            if (activeProbeIndex < positionsToSample.Count)
            {
                var validProbePosition2D = PrintLevelingWizard.EnsureInPrintBounds(printer, positionsToSample[activeProbeIndex]);
                positionToSample = new Vector3(validProbePosition2D, startProbeHeight);

                this.SampleNextPoint();
            }
            else
            {
                SaveSamplePoints();
                CancelValidation();
            }
        }
        public static void ShowPrintLevelWizard(PrinterConfig printer, ThemeConfig theme)
        {
            // turn off print leveling
            PrintLevelingStream.AllowLeveling = false;

            // clear any data that we are going to be acquiring (sampled positions, after z home offset)
            var levelingData = new PrintLevelingData()
            {
                LevelingSystem = printer.Settings.GetValue <LevelingSystem>(SettingsKey.print_leveling_solution)
            };

            printer.Settings.SetValue(SettingsKey.baby_step_z_offset, "0");

            LevelingPlan levelingPlan;

            switch (levelingData.LevelingSystem)
            {
            case LevelingSystem.Probe3Points:
                levelingPlan = new LevelWizard3Point(printer);
                break;

            case LevelingSystem.Probe7PointRadial:
                levelingPlan = new LevelWizard7PointRadial(printer);
                break;

            case LevelingSystem.Probe13PointRadial:
                levelingPlan = new LevelWizard13PointRadial(printer);
                break;

            case LevelingSystem.Probe100PointRadial:
                levelingPlan = new LevelWizard100PointRadial(printer);
                break;

            case LevelingSystem.Probe3x3Mesh:
                levelingPlan = new LevelWizardMesh(printer, 3, 3);
                break;

            case LevelingSystem.Probe5x5Mesh:
                levelingPlan = new LevelWizardMesh(printer, 5, 5);
                break;

            case LevelingSystem.Probe10x10Mesh:
                levelingPlan = new LevelWizardMesh(printer, 10, 10);
                break;

            default:
                throw new NotImplementedException();
            }

            var levelingContext = new PrintLevelingWizard(levelingPlan, printer)
            {
                WindowTitle = $"{ApplicationController.Instance.ProductName} - " + "Print Leveling Wizard".Localize()
            };

            var printLevelWizardWindow = DialogWindow.Show(new LevelingWizardRootPage(levelingContext)
            {
                WindowTitle = levelingContext.WindowTitle
            });

            printLevelWizardWindow.Closed += (s, e) =>
            {
                // If leveling was on when we started, make sure it is on when we are done.
                PrintLevelingStream.AllowLeveling = true;

                printLevelWizardWindow = null;

                // make sure we raise the probe on close
                if (printer.Settings.GetValue <bool>(SettingsKey.has_z_probe) &&
                    printer.Settings.GetValue <bool>(SettingsKey.use_z_probe) &&
                    printer.Settings.GetValue <bool>(SettingsKey.has_z_servo))
                {
                    // make sure the servo is retracted
                    var servoRetract = printer.Settings.GetValue <double>(SettingsKey.z_servo_retracted_angle);
                    printer.Connection.QueueLine($"M280 P0 S{servoRetract}");
                }
            };
        }
        private CalibrationControls(PrinterConfig printer, ThemeConfig theme)
            : base(FlowDirection.TopToBottom)
        {
            this.printer = printer;

            // add in the controls for configuring auto leveling
            {
                SettingsRow settingsRow;

                this.AddChild(settingsRow = new SettingsRow(
                                  "Bed Leveling".Localize(),
                                  null,
                                  theme,
                                  AggContext.StaticData.LoadIcon("leveling_32x32.png", 16, 16, theme.InvertIcons)));

                // run leveling button
                var runWizardButton = new IconButton(AggContext.StaticData.LoadIcon("fa-cog_16.png", theme.InvertIcons), theme)
                {
                    VAnchor = VAnchor.Center,
                    Margin  = theme.ButtonSpacing,
                    Name    = "Run Leveling Button",

                    ToolTipText = "Run Calibration".Localize()
                };
                runWizardButton.Click += (s, e) =>
                {
                    UiThread.RunOnIdle(() =>
                    {
                        PrintLevelingWizard.Start(printer, theme);
                    });
                };
                settingsRow.AddChild(runWizardButton);

                // only show the switch if leveling can be turned off (it can't if it is required).
                if (!printer.Settings.GetValue <bool>(SettingsKey.print_leveling_required_to_print))
                {
                    // put in the switch
                    printLevelingSwitch = new RoundedToggleSwitch(theme)
                    {
                        VAnchor = VAnchor.Center,
                        Margin  = new BorderDouble(left: 16),
                        Checked = printer.Settings.GetValue <bool>(SettingsKey.print_leveling_enabled)
                    };
                    printLevelingSwitch.CheckedStateChanged += (sender, e) =>
                    {
                        printer.Settings.Helpers.DoPrintLeveling(printLevelingSwitch.Checked);
                    };

                    // TODO: Why is this listener conditional? If the leveling changes somehow, shouldn't we be updated the UI to reflect that?
                    // Register listeners
                    printer.Settings.PrintLevelingEnabledChanged += Settings_PrintLevelingEnabledChanged;

                    settingsRow.AddChild(printLevelingSwitch);
                }

                // add in the controls for configuring probe offset
                if (printer.Settings.GetValue <bool>(SettingsKey.has_z_probe) &&
                    printer.Settings.GetValue <bool>(SettingsKey.use_z_probe))
                {
                    this.AddChild(settingsRow = new SettingsRow(
                                      "Calibrate Probe Offset".Localize(),
                                      null,
                                      theme,
                                      AggContext.StaticData.LoadIcon("probing_32x32.png", 16, 16, theme.InvertIcons)));

                    var runCalibrateProbeButton = new IconButton(AggContext.StaticData.LoadIcon("fa-cog_16.png", theme.InvertIcons), theme)
                    {
                        VAnchor     = VAnchor.Center,
                        Margin      = theme.ButtonSpacing,
                        ToolTipText = "Run Calibration".Localize()
                    };
                    runCalibrateProbeButton.Click += (s, e) =>
                    {
                        UiThread.RunOnIdle(() =>
                        {
                            ProbeCalibrationWizard.Start(printer, theme);
                        });
                    };

                    settingsRow.BorderColor = Color.Transparent;
                    settingsRow.AddChild(runCalibrateProbeButton);
                }
            }

            // Register listeners
            printer.Connection.CommunicationStateChanged += PrinterStatusChanged;
            printer.Connection.EnableChanged             += PrinterStatusChanged;

            SetVisibleControls();
        }