mattercontrol/PrinterControls/PrinterConnections/SetupStepMakeModelName.cs

273 lines
7.7 KiB
C#
Raw Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
2015-04-08 15:20:10 -07:00
using System;
using MatterHackers.MatterControl.SettingsManagement;
2015-04-08 15:20:10 -07:00
using System.Collections.Generic;
using System.Linq;
using MatterHackers.MatterControl.CustomWidgets;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration;
using MatterHackers.Agg.PlatformAbstract;
using System.IO;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
2015-04-08 15:20:10 -07:00
{
//Normally step one of the setup process
2016-06-08 09:31:26 -07:00
public class SetupStepMakeModelName : ConnectionWizardPage
2015-04-08 15:20:10 -07:00
{
private FlowLayoutWidget printerModelContainer;
private FlowLayoutWidget printerMakeContainer;
private MHTextEditWidget printerNameInput;
private TextWidget printerNameError;
private Button nextButton;
private bool usingDefaultName;
private static BorderDouble elementMargin = new BorderDouble(top: 3);
private BoundDropList printerManufacturerSelector;
private BoundDropList printerModelSelector;
string activeMake;
string activeModel;
string activeName;
public SetupStepMakeModelName()
2015-04-08 15:20:10 -07:00
{
printerManufacturerSelector = new BoundDropList(string.Format("- {0} -", "Select Make".Localize()), maxHeight: 200)
2015-04-08 15:20:10 -07:00
{
HAnchor = HAnchor.ParentLeftRight,
Margin = elementMargin,
Name = "Select Make",
ListSource = OemSettings.Instance.AllOems
};
2015-04-08 15:20:10 -07:00
printerManufacturerSelector.SelectionChanged += ManufacturerDropList_SelectionChanged;
printerMakeContainer = CreateSelectionContainer(
"Make".Localize() + ":",
"Select the printer manufacturer".Localize(),
printerManufacturerSelector);
if (printerManufacturerSelector.MenuItems.Count == 1)
{
activeMake = printerManufacturerSelector.SelectedValue;
2015-04-08 15:20:10 -07:00
}
printerModelSelector = new BoundDropList(string.Format("- {0} -", "Select Model".Localize()), maxHeight: 200)
2015-04-08 15:20:10 -07:00
{
Name = "Select Model",
HAnchor = HAnchor.ParentLeftRight,
Margin = elementMargin,
};
printerModelSelector.SelectionChanged += ModelDropList_SelectionChanged;
if (printerModelSelector.MenuItems.Count == 1)
{
// SelectIfOnlyOneModel
printerModelSelector.SelectedIndex = 0;
2015-04-08 15:20:10 -07:00
}
printerModelContainer = CreateSelectionContainer("Model".Localize(), "Select the printer model".Localize(), printerModelSelector);
2015-04-08 15:20:10 -07:00
//Add inputs to main container
contentRow.AddChild(printerMakeContainer);
contentRow.AddChild(printerModelContainer);
contentRow.AddChild(createPrinterNameContainer());
2015-04-08 15:20:10 -07:00
//Construct buttons
nextButton = textImageButtonFactory.Generate("Save & Continue".Localize());
nextButton.Name = "Save & Continue Button";
2016-04-18 11:31:31 -07:00
nextButton.Click += (s, e) =>
{
bool canContinue = this.OnSave();
if (canContinue)
{
#if __ANDROID__
2016-06-08 09:31:26 -07:00
WizardWindow.ChangeToPage<AndroidConnectDevicePage>();
#else
2016-06-08 09:31:26 -07:00
WizardWindow.ChangeToPage<SetupStepInstallDriver>();
#endif
2016-04-18 11:31:31 -07:00
}
};
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
//Add buttons to buttonContainer
footerRow.AddChild(nextButton);
footerRow.AddChild(new HorizontalSpacer());
2015-04-08 15:20:10 -07:00
footerRow.AddChild(cancelButton);
2014-01-29 19:09:30 -08:00
usingDefaultName = true;
2016-04-18 11:31:31 -07:00
SetElementVisibility();
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
private void SetElementVisibility()
2015-04-08 15:20:10 -07:00
{
nextButton.Visible = (activeModel != null && this.activeMake != null);
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private FlowLayoutWidget createPrinterNameContainer()
{
2016-04-18 11:31:31 -07:00
TextWidget printerNameLabel = new TextWidget("Name".Localize() + ":", 0, 0, 12)
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
HAnchor = HAnchor.ParentLeftRight,
Margin = new BorderDouble(0, 0, 0, 1)
};
2014-01-29 19:09:30 -08:00
printerNameInput = new MHTextEditWidget("")
2016-04-18 11:31:31 -07:00
{
HAnchor = HAnchor.ParentLeftRight,
};
printerNameInput.KeyPressed += (s, e) => this.usingDefaultName = false;
2014-01-29 19:09:30 -08:00
printerNameError = new TextWidget("", 0, 0, 10)
2016-04-18 11:31:31 -07:00
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
HAnchor = HAnchor.ParentLeftRight,
Margin = new BorderDouble(top: 3)
};
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
FlowLayoutWidget container = new FlowLayoutWidget(FlowDirection.TopToBottom);
container.Margin = new BorderDouble(0, 5);
2015-04-08 15:20:10 -07:00
container.AddChild(printerNameLabel);
container.AddChild(printerNameInput);
container.AddChild(printerNameError);
container.HAnchor = HAnchor.ParentLeftRight;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
return container;
}
2016-06-03 18:11:51 -07:00
private FlowLayoutWidget CreateSelectionContainer(string labelText, string validationMessage, Agg.UI.DropDownList selector)
2015-04-08 15:20:10 -07:00
{
var sectionLabel = new TextWidget(labelText, 0, 0, 12)
2016-04-18 11:31:31 -07:00
{
TextColor = ActiveTheme.Instance.PrimaryTextColor,
HAnchor = HAnchor.ParentLeftRight,
Margin = elementMargin
};
2014-01-29 19:09:30 -08:00
var validationTextWidget = new TextWidget(validationMessage, 0, 0, 10)
2016-04-18 11:31:31 -07:00
{
TextColor = ActiveTheme.Instance.SecondaryAccentColor,
2016-04-18 11:31:31 -07:00
HAnchor = HAnchor.ParentLeftRight,
Margin = elementMargin
};
2014-01-29 19:09:30 -08:00
selector.SelectionChanged += (s, e) =>
2016-04-18 11:31:31 -07:00
{
validationTextWidget.Visible = selector.SelectedLabel.StartsWith("-"); // The default values have "- Title -"
2016-04-18 11:31:31 -07:00
};
2014-01-29 19:09:30 -08:00
var container = new FlowLayoutWidget(FlowDirection.TopToBottom)
2016-04-18 11:31:31 -07:00
{
Margin = new BorderDouble(0, 5),
HAnchor = HAnchor.ParentLeftRight
2016-04-18 11:31:31 -07:00
};
2015-04-08 15:20:10 -07:00
container.AddChild(sectionLabel);
container.AddChild(selector);
container.AddChild(validationTextWidget);
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
return container;
}
private void ManufacturerDropList_SelectionChanged(object sender, EventArgs e)
{
activeMake = ((Agg.UI.DropDownList)sender).SelectedValue;
activeModel = null;
2015-04-08 15:20:10 -07:00
Dictionary<string, string> printers;
if (!OemSettings.Instance.OemProfiles.TryGetValue(activeMake, out printers))
{
printers = new Dictionary<string, string>();
}
var models = printers.Select(profile => new KeyValuePair<string, string>(profile.Key, profile.Value)).ToList();
// sort by key (model name)
models.Sort(
delegate (KeyValuePair<string, string> pair1,
KeyValuePair<string, string> pair2)
{
return pair1.Key.CompareTo(pair2.Key);
}
);
printerModelSelector.ListSource = models;
if (printerModelSelector.MenuItems.Count == 1)
{
// SelectIfOnlyOneModel
printerModelSelector.SelectedIndex = 0;
}
2015-04-08 15:20:10 -07:00
contentRow.Invalidate();
SetElementVisibility();
2015-04-08 15:20:10 -07:00
}
private void ModelDropList_SelectionChanged(object sender, EventArgs e)
{
UiThread.RunOnIdle(() =>
2015-04-08 15:20:10 -07:00
{
2016-06-03 18:11:51 -07:00
DropDownList dropList = (DropDownList) sender;
activeModel = dropList.SelectedLabel;
2016-04-18 11:31:31 -07:00
SetElementVisibility();
2015-04-08 15:20:10 -07:00
if (usingDefaultName)
{
// Use ManufacturerDropList.SelectedLabel instead of activeMake to ensure the mapped Unicode values are picked up
string mappedMakeText = printerManufacturerSelector.SelectedLabel;
string printerInputName = string.Format("{0} {1}", mappedMakeText, activeModel);
var names = ProfileManager.Instance.ActiveProfiles.Where(p => p.Name.StartsWith(printerInputName)).Select(p => p.Name).ToList();
2016-04-18 11:31:31 -07:00
if (!names.Contains(printerInputName))
{
printerNameInput.Text = printerInputName;
}
else
{
int printerModelCount = 1; //Used to keep track of how many of the printer models we run into before and empty one
string possiblePrinterName;
do
{
2016-06-03 18:11:51 -07:00
possiblePrinterName = string.Format("{0} ({1})", printerInputName, printerModelCount++);
2016-04-18 11:31:31 -07:00
} while (names.Contains(possiblePrinterName));
printerNameInput.Text = possiblePrinterName;
}
2015-04-08 15:20:10 -07:00
}
});
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private bool OnSave()
{
2016-04-18 11:31:31 -07:00
if (!string.IsNullOrEmpty(printerNameInput.Text))
2015-04-08 15:20:10 -07:00
{
activeName = printerNameInput.Text;
2016-04-18 11:31:31 -07:00
if (this.activeMake == null || activeModel == null)
2015-04-08 15:20:10 -07:00
{
return false;
}
else
{
ProfileManager.AcquireNewProfile(activeMake, activeModel, activeName);
2015-04-08 15:20:10 -07:00
return true;
}
}
else
{
this.printerNameError.TextColor = RGBA_Bytes.Red;
this.printerNameError.Text = "Printer name cannot be blank";
2015-04-08 15:20:10 -07:00
this.printerNameError.Visible = true;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
return false;
}
}
}
}