From b407b7b0ec7a2ba41bff1f450cf1bc1934869b5c Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 6 Nov 2019 17:24:45 +0100 Subject: [PATCH] working on selectors --- Belegscanner.lpi | 2 +- Belegscanner.lpr | 2 +- Belegscanner.lps | 95 +++++++++++++--------------- scanner.lfm | 5 +- scanner.pas | 161 ++++++++++++++++++++++++++++++++++++----------- 5 files changed, 173 insertions(+), 92 deletions(-) diff --git a/Belegscanner.lpi b/Belegscanner.lpi index 1d24afe..b5120fc 100644 --- a/Belegscanner.lpi +++ b/Belegscanner.lpi @@ -37,7 +37,7 @@ - + diff --git a/Belegscanner.lpr b/Belegscanner.lpr index 69b9086..cbb3821 100644 --- a/Belegscanner.lpr +++ b/Belegscanner.lpr @@ -15,7 +15,7 @@ uses begin RequireDerivedFormResource:=True; Application.Initialize; - Application.CreateForm(TForm1, Form1); + Application.CreateForm(TScanForm, ScanForm); Application.Run; end. diff --git a/Belegscanner.lps b/Belegscanner.lps index cb1b1e9..18ca75e 100644 --- a/Belegscanner.lps +++ b/Belegscanner.lps @@ -3,24 +3,22 @@ - + - - - + - + - - + + @@ -28,8 +26,8 @@ - - + + @@ -40,139 +38,136 @@ + + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - diff --git a/scanner.lfm b/scanner.lfm index 9bbbb7e..259cd80 100644 --- a/scanner.lfm +++ b/scanner.lfm @@ -1,11 +1,12 @@ -object Form1: TForm1 +object ScanForm: TScanForm Left = 2207 Height = 95 Top = 221 Width = 664 - Caption = 'Form1' + Caption = 'Belegscanner' ClientHeight = 95 ClientWidth = 664 + OnCreate = FormCreate LCLVersion = '1.8.2.0' object DateView: TLabel Left = 8 diff --git a/scanner.pas b/scanner.pas index eecb511..916446a 100644 --- a/scanner.pas +++ b/scanner.pas @@ -10,9 +10,9 @@ uses type - { TForm1 } + { TScanForm } - TForm1 = class(TForm) + TScanForm = class(TForm) ScnaButton: TButton; Product: TComboBox; Destination: TComboBox; @@ -22,14 +22,19 @@ type dateButton: TButton; DateSelector: TCalendarDialog; DateView: TLabel; + + procedure FormCreate(Sender: TObject); procedure ProductChange(Sender: TObject); procedure DestinationChange(Sender: TObject); procedure dateButtonClick(Sender: TObject); procedure OriginChange(Sender: TObject); procedure ScnaButtonClick(Sender: TObject); procedure TypeSelectorChange(Sender: TObject); + function ExportConfig(): string; + function ExportOptions(dropDown: TComboBox): string; procedure UpdateTypes(); procedure UpdateFolder(); + procedure MixLocations(d1: TComboBox; d2: TComboBox); private date: TDateTime; public @@ -37,91 +42,171 @@ type end; var - Form1: TForm1; + ScanForm: TScanForm; implementation {$R *.lfm} -{ TForm1 } +{ TScanForm } + +procedure TScanForm.MixLocations(d1: TComboBox; d2: TComboBox); +var + list: TStringList; +begin + list := TStringList.Create; + list.Sorted := True; + d1.Items.Add(d1.Text); + d2.Items.Add(d2.Text); + list.Assign(d1.Items); + list.AddStrings(d2.Items); + d1.Items.Assign(list); + d2.Items.Assign(list); +end; + +function TScanForm.ExportConfig(): string; +begin + Result := 'types:' + ExportOptions(TypeSelector) + #13; + Result := Result + 'items:' + ExportOptions(Product) + #13; + MixLocations(Origin, Destination); + Result := Result + 'locations:' + ExportOPtions(Origin) + #13; +end; + +function TScanForm.ExportOptions(dropDown: TComboBox): string; +var + list: TStringList; +begin + // make sure the current text appears in future lists + dropDown.Items.Add(dropDown.Text); + // create a StringList to sort and concatenate + list := TStringList.Create; + list.StrictDelimiter := True; + list.Sorted := True; + + // put all items in the list, will sort and remove duplicates + list.Assign(dropDown.Items); + + // update dropdown + dropDown.Items.Assign(list); + + // return json array + Result := list.DelimitedText +end; -procedure TForm1.TypeSelectorChange(Sender: TObject); +procedure TScanForm.TypeSelectorChange(Sender: TObject); var state: boolean; begin - TypeSelector.Tag:=1; + TypeSelector.Tag := 1; state := TypeSelector.Text = 'Ticket'; - Origin.Enabled:=state; - Origin.Visible:=state; - Destination.Enabled:=state; - Product.Enabled:=not state; - Product.Visible:=not state; + Origin.Enabled := state; + Origin.Visible := state; + Destination.Enabled := state; + Product.Enabled := not state; + Product.Visible := not state; UpdateFolder(); end; -procedure TForm1.dateButtonClick(Sender: TObject); +procedure TScanForm.dateButtonClick(Sender: TObject); begin DateSelector.Execute; date := DateSelector.Date; - DateView.Caption:=FormatDateTime('YYYY-MM-DD',date); + DateView.Caption := FormatDateTime('YYYY-MM-DD', date); UpdateTypes(); - TypeSelector.enabled := true; + TypeSelector.Enabled := True; UpdateFolder(); end; -procedure TForm1.OriginChange(Sender: TObject); +procedure TScanForm.OriginChange(Sender: TObject); begin Origin.Tag := 1; UpdateFolder(); end; -procedure TForm1.ScnaButtonClick(Sender: TObject); +procedure TScanForm.ScnaButtonClick(Sender: TObject); var config: TFileStream; - filename: String; + filename: string; + json: string; begin filename := GetEnvironmentVariable('HOME') + '/.config/belegscanner.conf'; - FolderName.Caption:=filename; + json := ExportConfig(); try - config :=TFilestream.Create(filename, fmCreate); - config.Write(filename[1],filename.Length); + config := TFilestream.Create(filename, fmCreate); + config.Write(json[1], json.Length); finally config.Free; end; - end; -procedure TForm1.DestinationChange(Sender: TObject); +procedure TScanForm.DestinationChange(Sender: TObject); begin Destination.Tag := 1; UpdateFolder(); end; -procedure TForm1.ProductChange(Sender: TObject); +procedure TScanForm.ProductChange(Sender: TObject); begin - Product.Tag := 1; - UpdateFolder(); + Product.Tag := 1; + UpdateFolder(); end; -procedure TForm1.UpdateFolder(); +procedure TScanForm.FormCreate(Sender: TObject); var - tx: String; + filename: String; + lines: TStringList; + line: String; + index: integer; begin - tx := FormatDateTime('YYYY-MM-DD',date); - if (TypeSelector.Tag > 0) then tx := tx + ' - ' + TypeSelector.Text; - if (Origin.Enabled AND (Origin.Tag > 0)) then tx := tx + ': ' + Origin.Text; - if (Destination.Enabled AND (Destination.Tag > 0)) then tx := tx + ' - ' + Destination.Text; - if (Product.Enabled AND (Product.Tag > 0)) then tx := tx + ' von ' + Product.Text; - FolderName.Caption:=tx; + filename := GetEnvironmentVariable('HOME') + '/.config/belegscanner.conf'; + lines := TStringList.Create; + try + lines.LoadFromFile(filename); + for index := 0 to lines.Count-1 do + begin + line:=lines.Strings[index]; + if line.StartsWith('types:') then + begin + line := line.Substring(6); + TypeSelector.Items.AddStrings(line.Split(',')); + end; + if line.StartsWith('items:') then + begin + line := line.Substring(6); + Product.Items.AddStrings(line.Split(',')); + end; + if line.StartsWith('locations:') then + begin + line := line.Substring(10); + Origin.Items.AddStrings(line.Split(',')); + Destination.Items.AddStrings(line.Split(',')); + end; + end + finally + end; end; -procedure TForm1.UpdateTypes(); + +procedure TScanForm.UpdateFolder(); +var + tx: string; begin - WriteLn('Test'); - TypeSelector.Items.Clear; - TypeSelector.Items.Add('Ticket'); + tx := FormatDateTime('YYYY-MM-DD', date) + ' - '; + if (Product.Enabled and (Product.Tag > 0)) then + tx := tx + Product.Text + ' von '; + if (TypeSelector.Tag > 0) then + tx := tx + TypeSelector.Text; + if (Origin.Enabled and (Origin.Tag > 0)) then + tx := tx + ': ' + Origin.Text; + if (Destination.Enabled and (Destination.Tag > 0)) then + tx := tx + ' - ' + Destination.Text; + FolderName.Caption := tx; end; -end. +procedure TScanForm.UpdateTypes(); +begin +end; + +end.