Programm_Wirl/AA-Forms/FormEinstellung.cs

305 lines
11 KiB
C#

using DatenDB;
using Deckungsbeitrag.AA_Klassen;
using Deckungsbeitrag.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TrelloIntegration;
namespace Deckungsbeitrag
{
public partial class FormEinstellung : Form
{
private readonly UserSettingsManager _settings = new UserSettingsManager();
string varkosten = ConfigurationManager.AppSettings["Variabel"];
string fixlohnkosten = ConfigurationManager.AppSettings["FixLohn"];
string fixmietkosten = ConfigurationManager.AppSettings["FixMiete"];
public bool changed = false;
public bool validating = true;
private Benutzer benutzer;
public FormEinstellung()
{
InitializeComponent();
}
public FormEinstellung(object x) : this()
{
if (x.GetType() == typeof(String))
{
if (x.ToString() == "Sortiment") { ButtonPfad_Click(buttonSortiment, null); this.textBoxSortiment.BackColor = Color.Red; }
}
else
{
if (x.GetType() == typeof(Benutzer))
{
this.benutzer = (Benutzer)x;
switch (this.benutzer.Rolle)
{
case BenutzerRolle.Verwaltung:
break;
case BenutzerRolle.Fahrer:
goto default;
case BenutzerRolle.Admin:
break;
case BenutzerRolle.Waschstrasse:
goto default;
case BenutzerRolle.Master:
break;
case BenutzerRolle.Expedit:
foreach (TabPage tP in this.tabControlEinstellung.TabPages)
{
if (!tP.Name.Contains("Drucker") & !tP.Name.Contains("Allgemein")) this.tabControlEinstellung.Controls.Remove(tP);
}
break;
case BenutzerRolle.Frottee:
foreach (TabPage tP in this.tabControlEinstellung.TabPages)
{
if (!tP.Name.Contains("Drucker") & !tP.Name.Contains("Allgemein")) this.tabControlEinstellung.Controls.Remove(tP);
}
break;
case BenutzerRolle.Flach:
goto default;
default:
foreach (TabPage tP in this.tabControlEinstellung.TabPages)
{
this.tabControlEinstellung.Controls.Remove(tP);
}
break;
}
}
}
}
private void FormEinstellung_Load(object sender, EventArgs e)
{
// Standard Drucker und Kopieanzahl einstellen.
foreach (string printer in PrinterSettings.InstalledPrinters)
{
this.comboBoxEtikettDrucker.Items.Add(printer);
this.comboBoxSWSDrucker.Items.Add(printer);
this.comboBoxTourenlisteDrucker.Items.Add(printer);
}
this.comboBoxSprachen.SelectedIndexChanged -= ComboBoxSprachen_SelectedIndexChanged;
LoadCultures();
Load_Settings();
this.comboBoxSprachen.SelectedIndexChanged += ComboBoxSprachen_SelectedIndexChanged;
}
private void Load_Settings()
{
_settings.Load();
//TODO: Settings hier ändern auf UserSettingsManager Klasse.
this.checkBoxMaxWert.Checked = Settings.Default.MaxChecked;
this.checkBoxMinWert.Checked = Settings.Default.MinChecked;
this.numUpDownAnzahl.Value = Settings.Default.ZAnzahl;
if (checkBoxMaxWert.Checked) { this.numUpDownMaxWert.Value = (int)Settings.Default.RatMax; this.numUpDownMaxWert.Enabled = true; }
if (checkBoxMinWert.Checked) { this.numUpDownMinWert.Value = (int)Settings.Default.RatMin; this.numUpDownMinWert.Enabled = true; }
this.textBoxSortiment.Text = ConfigurationManager.AppSettings["SortimentPfad"];
this.textBoxRatingbild.Text = ConfigurationManager.AppSettings["BildPfad"];
// Ab hier wurde bereits auf UserSettingsManager umgebaut.
this.comboBoxEtikettDrucker.Text = _settings.DruckerEtikett;
this.comboBoxTourenlisteDrucker.Text = _settings.DruckerTourenListe;
this.comboBoxSWSDrucker.Text = _settings.DruckerSWS;
this.numericUpDownSWSKopien.Value = _settings.CopiesSWS;
if (_settings.Culture != null)
{
this.comboBoxSprachen.SelectedValue = _settings.Culture;
}
foreach (Button btn in this.groupBoxFarben.Controls.OfType<Button>())
{
if (btn.Name.Substring(6) == _settings.Presse.Name) btn.FlatAppearance.BorderColor = _settings.Presse;
}
}
private void Save_Settings()
{
if (MessageBox.Show("Möchtest du die Einstellungen speichern?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
else
{
if (this.comboBoxEtikettDrucker.SelectedItem != null) _settings.DruckerEtikett = this.comboBoxEtikettDrucker.SelectedItem.ToString();
if (this.comboBoxSWSDrucker.SelectedItem != null) { _settings.DruckerSWS = this.comboBoxSWSDrucker.SelectedItem.ToString(); _settings.CopiesSWS = this.numericUpDownSWSKopien.Value; }
if (this.comboBoxTourenlisteDrucker.SelectedItem != null) _settings.DruckerTourenListe = this.comboBoxTourenlisteDrucker.ToString();
if (this.comboBoxSprachen.SelectedItem != null) _settings.Culture = this.comboBoxSprachen.SelectedValue.ToString();
_settings.Save();
}
}
private void LoadCultures()
{
var cultures = new []
{
new { Code = "de-AT", Name = "Deutsch" },
new { Code = "en-US", Name = "English (United States) -> Englisch" },
new { Code = "bs-BA", Name = "Bosanski (Bosna) -> Bosnisch" },
new { Code = "bg-BG", Name = "български (България) -> Bulgarisch" },
new { Code = "tr-TR", Name = "Türkçe (Türkiye) -> Türkisch" }
};
comboBoxSprachen.DataSource = cultures;
comboBoxSprachen.DisplayMember = "Name";
comboBoxSprachen.ValueMember = "Code";
}
//Bei Click erscheint Speichern-Button und gefüllte TextBox mit Kosten-KontoNr
private void buttonVarKost_Click(object sender, EventArgs e)
{
this.buttonVarSpeichern.Visible = this.textBoxVarKosten.Visible = true;
this.textBoxVarKosten.Text = varkosten;
this.textBoxVarKosten.Size = new Size((Point)TextRenderer.MeasureText(this.textBoxVarKosten.Text + 20, this.textBoxVarKosten.Font));
Resize_Form();
}
private void buttonFixMietKosten_Click(object sender, EventArgs e)
{
this.buttonFixMietSpeichern.Visible = this.textBoxFixMietKosten.Visible = true;
this.textBoxFixMietKosten.Text = fixmietkosten;
this.textBoxFixMietKosten.Size = new Size((Point)TextRenderer.MeasureText(this.textBoxFixMietKosten.Text + 20, this.textBoxFixMietKosten.Font));
Resize_Form();
}
private void buttonFixLohnKosten_Click(object sender, EventArgs e)
{
this.buttonFixLohnSpeichern.Visible = this.textBoxFixLohnKosten.Visible = true;
this.textBoxFixLohnKosten.Text = fixlohnkosten;
this.textBoxFixLohnKosten.Size = new Size((Point)TextRenderer.MeasureText(this.textBoxFixLohnKosten.Text + 20, this.textBoxFixLohnKosten.Font));
Resize_Form();
}
//Bei Click wird der Inhalt der TextBox in App.Config gespeichert
private void buttonVarSpeichern_Click(object sender, EventArgs e)
{
varkosten = this.textBoxVarKosten.Text.Replace(", ", ",");
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("Variabel");
config.AppSettings.Settings.Add("Variabel", varkosten);
config.Save();
}
private void buttonFixLohnSpeichern_Click(object sender, EventArgs e)
{
fixlohnkosten = this.textBoxFixLohnKosten.Text.Replace(", ", ",");
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("FixLohn");
config.AppSettings.Settings.Add("FixLohn", fixlohnkosten);
config.Save();
}
private void buttonFixMietSpeichern_Click(object sender, EventArgs e)
{
fixmietkosten = this.textBoxFixMietKosten.Text.Replace(", ", ",");
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("FixMiete");
config.AppSettings.Settings.Add("FixMiete", fixmietkosten);
config.Save();
}
//Bei Click auf Farb-Button wird Farb-Dialog geöffnet
private void Color_button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
ColorDialog col = new ColorDialog();
if(col.ShowDialog() == DialogResult.OK) Settings.Default[btn.Name.Substring(6)] = btn.FlatAppearance.BorderColor = col.Color;
}
//Ratingwerte individuell einstellen (Wenn ja dann Checked)
private void checkBoxMinWert_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked) { this.numUpDownMinWert.Enabled = Properties.Settings.Default.MinChecked = true; }
else this.numUpDownMinWert.Enabled = Properties.Settings.Default.MinChecked = false;
}
private void checkBoxMaxWert_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked) this.numUpDownMaxWert.Enabled = Properties.Settings.Default.MaxChecked = true;
else this.numUpDownMaxWert.Enabled = Properties.Settings.Default.MaxChecked = false;
}
//Buttons und TextBoxen für Pfadänderungen.
private void TextBox_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (changed)
{
if (MessageBox.Show($"Möchtest du die Änderungen beibehalten?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) return;
else
{
tb.Focus();
tb.ForeColor = Color.Red;
e.Cancel = true;
}
}
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
changed = true;
}
private void ButtonPfad_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Text == "Sortiment") { this.textBoxSortiment.Visible = true; }
if (btn.Text == "Ratingbild") { this.textBoxRatingbild.Visible = true; }
}
private void textBoxSortiment_Enter(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.BackColor = SystemColors.Window;
}
//Aktuell nicht verwendet
private void Resize_Form()
{
}
//Button Speichern und Abbrechen (Speichern leitet zu Save_Settings weiter)
private void buttonSpeichern_Click(object sender, EventArgs e)
{
Save_Settings();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonAbbrechen_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Möchtest du die Änderungen verwerfen?", "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) this.Close();
else return;
}
private void buttonSaveCredentials_Click(object sender, EventArgs e)
{
TrelloConfig.CreateEncryptedConfigValues(
textBoxApiKey.Text,
textBoxToken.Text);
MessageBox.Show("Verschlüsselte Werte wurden in der Console ausgegeben.");
}
private void ComboBoxSprachen_SelectedIndexChanged(object sender, EventArgs e)
{
TranslationService.SetLanguage(this.comboBoxSprachen.SelectedValue.ToString());
}
}
}