New searchable Add Printer Wizard

- Issue MatterHackers/MCCentral#5519
Finish new Add Printer wizard
- Issue MatterHackers/MCCentral#3439
Improve printer Make / Model selection for Pulse
This commit is contained in:
jlewin 2019-05-14 15:18:08 -07:00
parent 27e38d2608
commit 8d6a2cec59
3 changed files with 427 additions and 180 deletions

View file

@ -28,12 +28,12 @@ either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.SettingsManagement;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrintLibrary;
using MatterHackers.MatterControl.SlicerConfiguration;
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
@ -41,85 +41,39 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
// Normally step one of the setup process
public class SetupStepMakeModelName : DialogPage
{
private FlowLayoutWidget printerModelContainer;
private FlowLayoutWidget printerMakeContainer;
private MHTextEditWidget printerNameInput;
private TextWidget printerNameError;
private GuiWidget nextButton;
private TextButton nextButton;
private AddPrinterWidget printerPanel;
private bool usingDefaultName;
private static BorderDouble elementMargin = new BorderDouble(top: 3);
private BoundDropList printerManufacturerSelector;
private BoundDropList printerModelSelector;
private string activeMake;
private string activeModel;
private string activeName;
private RadioButton createPrinterRadioButton = null;
private RadioButton signInRadioButton;
public SetupStepMakeModelName()
{
bool userIsLoggedIn = !ApplicationController.GuestUserActive?.Invoke() ?? false;
this.HeaderText = this.WindowTitle = "Printer Setup".Localize();
this.WindowSize = new VectorMath.Vector2(800, 600);
var addPrinterColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
contentRow.BackgroundColor = theme.SectionBackgroundColor;
nextButton = theme.CreateDialogButton("Next".Localize());
printerPanel = new AddPrinterWidget(theme, nextButton)
{
HAnchor = HAnchor.Stretch,
VAnchor = VAnchor.Fit,
VAnchor = VAnchor.Stretch
};
printerManufacturerSelector = new BoundDropList(string.Format("- {0} -", "Select Make".Localize()), theme, maxHeight: 200)
{
HAnchor = HAnchor.Stretch,
Margin = elementMargin,
Name = "Select Make",
ListSource = OemSettings.Instance.AllOems,
TabStop = true
};
printerManufacturerSelector.SelectionChanged += ManufacturerDropList_SelectionChanged;
printerMakeContainer = CreateSelectionContainer(
"Make".Localize() + ":",
"Select the printer manufacturer".Localize(),
printerManufacturerSelector);
printerModelSelector = new BoundDropList(string.Format("- {0} -", "Select Model".Localize()), theme, maxHeight: 200)
{
Name = "Select Model",
HAnchor = HAnchor.Stretch,
Margin = elementMargin,
TabStop = true
};
printerModelSelector.SelectionChanged += ModelDropList_SelectionChanged;
printerModelContainer = CreateSelectionContainer(
"Model".Localize() + ":",
"Select the printer model".Localize(),
printerModelSelector);
// Add inputs to main container
addPrinterColumn.AddChild(printerMakeContainer);
addPrinterColumn.AddChild(printerModelContainer);
addPrinterColumn.AddChild(CreatePrinterNameContainer());
RadioButton signInRadioButton = null;
if (userIsLoggedIn)
{
contentRow.AddChild(addPrinterColumn);
contentRow.AddChild(printerPanel);
}
else
{
contentRow.Padding = 10;
addPrinterColumn.Margin = new BorderDouble(28, 15, 15, 5);
contentRow.Padding = theme.DefaultContainerPadding;
printerPanel.Margin = new BorderDouble(left: 15, top: theme.DefaultContainerPadding);
var commonMargin = new BorderDouble(4, 2);
@ -145,14 +99,13 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
createPrinterRadioButton.CheckedStateChanged += (s, e) =>
{
addPrinterColumn.Enabled = createPrinterRadioButton.Checked;
printerPanel.Enabled = createPrinterRadioButton.Checked;
this.SetElementVisibility();
};
contentRow.AddChild(addPrinterColumn);
contentRow.AddChild(printerPanel);
}
nextButton = theme.CreateDialogButton("Next".Localize());
nextButton.Name = "Next Button";
nextButton.Click += (s, e) => UiThread.RunOnIdle(async () =>
{
@ -168,14 +121,14 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
}
else
{
bool controlsValid = this.ValidateControls();
if (controlsValid)
bool controlsValid = printerPanel.ValidateControls();
if (controlsValid
&& printerPanel.SelectedPrinter is AddPrinterWidget.MakeModelInfo selectedPrinter)
{
var printer = await ProfileManager.CreatePrinterAsync(activeMake, activeModel, activeName);
var printer = await ProfileManager.CreatePrinterAsync(selectedPrinter.Make, selectedPrinter.Model, printerPanel.NewPrinterName);
if (printer == null)
{
this.printerNameError.Text = "Error creating profile".Localize();
this.printerNameError.Visible = true;
printerPanel.SetError("Error creating profile".Localize());
return;
}
@ -191,53 +144,13 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
usingDefaultName = true;
if (printerManufacturerSelector.MenuItems.Count == 1)
{
printerManufacturerSelector.SelectedIndex = 0;
}
SetElementVisibility();
}
private void SetElementVisibility()
{
nextButton.Enabled = createPrinterRadioButton == null
|| !createPrinterRadioButton.Checked
|| (createPrinterRadioButton.Checked && (activeModel != null && this.activeMake != null));
}
private FlowLayoutWidget CreatePrinterNameContainer()
{
TextWidget printerNameLabel = new TextWidget("Name".Localize() + ":", 0, 0, 12)
{
TextColor = theme.TextColor,
HAnchor = HAnchor.Stretch,
Margin = new BorderDouble(0, 4, 0, 1)
};
printerNameInput = new MHTextEditWidget("", theme)
{
HAnchor = HAnchor.Stretch,
};
printerNameInput.KeyPressed += (s, e) => this.usingDefaultName = false;
printerNameError = new TextWidget("", 0, 0, 10)
{
TextColor = theme.TextColor,
HAnchor = HAnchor.Stretch,
Margin = new BorderDouble(top: 3)
};
var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
{
Margin = new BorderDouble(0, 5),
HAnchor = HAnchor.Stretch
};
container.AddChild(printerNameLabel);
container.AddChild(printerNameInput);
container.AddChild(printerNameError);
return container;
nextButton.Enabled = signInRadioButton?.Checked == true
|| printerPanel.SelectedPrinter != null;
}
private FlowLayoutWidget CreateSelectionContainer(string labelText, string validationMessage, DropDownList selector)
@ -273,74 +186,5 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
return container;
}
private void ManufacturerDropList_SelectionChanged(object sender, EventArgs e)
{
activeMake = ((DropDownList)sender).SelectedValue;
activeModel = null;
// Select the dictionary containing the printerName->printerToken mappings for the current OEM
Dictionary<string, PublicDevice> printers;
if (!OemSettings.Instance.OemProfiles.TryGetValue(activeMake, out printers))
{
// Fall back to an empty dictionary if no match
printers = new Dictionary<string, PublicDevice>();
}
// Models - sort dictionary results by key and assign to .ListSource
printerModelSelector.ListSource = printers.OrderBy(p => p.Key).Select(p => new KeyValuePair<string, string>(p.Key, p.Value.ProfileToken)).ToList();
if (printerModelSelector.MenuItems.Count == 1)
{
printerModelSelector.SelectedIndex = 0;
}
contentRow.Invalidate();
SetElementVisibility();
}
private void ModelDropList_SelectionChanged(object sender, EventArgs e)
{
UiThread.RunOnIdle(() =>
{
DropDownList dropList = (DropDownList)sender;
activeModel = dropList.SelectedLabel;
SetElementVisibility();
if (usingDefaultName)
{
// Use ManufacturerDropList.SelectedLabel instead of activeMake to ensure the mapped Unicode values are picked up
string mappedMakeText = printerManufacturerSelector.SelectedLabel;
var existingPrinterNames = ProfileManager.Instance.ActiveProfiles.Select(p => p.Name);
printerNameInput.Text = agg_basics.GetNonCollidingName($"{mappedMakeText} {activeModel}", existingPrinterNames);
}
});
}
private bool ValidateControls()
{
if (!string.IsNullOrEmpty(printerNameInput.Text))
{
activeName = printerNameInput.Text;
if (this.activeMake == null || activeModel == null)
{
return false;
}
else
{
return true;
}
}
else
{
this.printerNameError.TextColor = Color.Red;
this.printerNameError.Text = "Printer name cannot be blank".Localize();
this.printerNameError.Visible = true;
return false;
}
}
}
}