unit ProductDetailScherm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Subscherm, Main, ApplicationContext, ObserverPattern, Vcl.ComCtrls, Vcl.CheckLst, CheckCombo, Vcl.ExtCtrls; resourceString GEEN_DATA_CAPTION = 'Geen data om te tonen'; NERGENS_DEEL_VAN_CAPTION = '--Nergens deel van--'; type TFormProductDetail = class(TFormSubscherm) LabelOmschrijving1: TLabel; LabelOmschrijving2: TLabel; Label1: TLabel; LabelProductNaam: TLabel; ListViewVerpakkingen: TListView; GroupBox1: TGroupBox; ButtonNieuw: TButton; ButtonVerwijder: TButton; Label2: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; Label10: TLabel; Label11: TLabel; Label12: TLabel; Label13: TLabel; EditMeta: TEdit; ComboBoxDeelVan: TComboBox; EditBreedte: TEdit; EditDiepte: TEdit; EditHoogte: TEdit; EditMaxCombAantal: TEdit; EditAantal: TEdit; ComboBoxPlaatsing: TComboBox; ComboBoxPositie: TComboBox; ComboBoxRichting: TComboBox; ButtonMeta: TButton; MemoParameters: TMemo; PanelVerpakkingControls: TPanel; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ListViewVerpakkingenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); private FProductDetailsSubject: TSubject; FProductDetailsSubjectObserver: TSubjectObserver; LabelGeenData: TLabel; CheckComboBoxPositie: TCheckedComboBox; CheckComboBoxRichting: TCheckedComboBox; procedure UpdateGui(Sender: TObject); procedure ToonProductVerpakking(ProductVerpakking: TProductVerpakking); procedure UpdateControlsEnableStatus(); procedure UpdateGuiDataAanwezigheid(Aanwezig: Boolean); public Constructor Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductDetailsSubject: TSubject); end; implementation uses OptiServerService, Util, System.StrUtils; {$R *.dfm} Constructor TFormProductDetail.Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductDetailsSubject: TSubject); begin inherited Create(AOwner, Navigator, ApplicationContext); self.FProductDetailsSubject := ProductDetailsSubject; self.FProductDetailsSubjectObserver := TSubjectObserver.Create(self); self.FProductDetailsSubjectObserver.OnChange := UpdateGui; end; procedure TFormProductDetail.FormCreate(Sender: TObject); begin FProductDetailsSubject.RegisterObserver(FProductDetailsSubjectObserver); // Vervang sommige comboboxen door TCheckedComboBox CheckComboBoxPositie := TCheckedComboBox.Create(self); with CheckComboBoxPositie do CheckComboBoxPositie.Delimiter := ';'; TUtil.VervangControl(ComboBoxPositie, CheckComboBoxPositie); CheckComboBoxRichting := TCheckedComboBox.Create(self); with CheckComboBoxRichting do CheckComboBoxRichting.Delimiter := ';'; TUtil.VervangControl(ComboBoxRichting, CheckComboBoxRichting); end; procedure TFormProductDetail.FormDestroy(Sender: TObject); begin FProductDetailsSubject.UnregisterObserver(FProductDetailsSubjectObserver); end; procedure TFormProductDetail.ListViewVerpakkingenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); begin if Selected then if (ListViewVerpakkingen.Selected.Data <> nil) then ToonProductVerpakking(TProductVerpakking(ListViewVerpakkingen.Selected.Data)); end; procedure TFormProductDetail.UpdateGui(Sender: TObject); var ProductVerpakking: TProductVerpakking; VorigeIndexInList: Integer; begin if (ApplicationContext.ProductDetailContext.GeselecteerdProduct = nil) or (ApplicationContext.ProductDetailContext.ProductVerpakkingen.Count = 0) then begin UpdateGuiDataAanwezigheid(False); exit; end else UpdateGuiDataAanwezigheid(True); // Update met data uit ApplicationContext LabelOmschrijving1.Caption := ApplicationContext.ProductDetailContext.GeselecteerdProduct.Omschrijving1; LabelOmschrijving2.Caption := ApplicationContext.ProductDetailContext.GeselecteerdProduct.Omschrijving2; LabelProductNaam.Caption := ApplicationContext.ProductDetailContext.GeselecteerdProduct.Kol1; VorigeIndexInList := ListViewVerpakkingen.ItemIndex; // List view (grid) ListViewVerpakkingen.Clear(); for ProductVerpakking in ApplicationContext.ProductDetailContext.ProductVerpakkingen do begin With ListViewVerpakkingen.Items.Add Do begin // Caption := IntToStr(Product.ProductNr); // Deze casting geeft geen access violation Caption := IntToStr(ProductVerpakking.CutOrder); SubItems.Add(ProductVerpakking.MetaCaption); SubItems.Add(ProductVerpakking.Breedte + ' x ' + ProductVerpakking.Diepte + ' x ' + ProductVerpakking.Hoogte); SubItems.Add(ProductVerpakking.Aantal); SubItems.Add(ProductVerpakking.MaxCombinAantal); SubItems.Add(ifthen(ProductVerpakking.DeelVan <> nil, ProductVerpakking.DeelVan.MetaCaption, '')); Data := ProductVerpakking; end; end; // Kijk of dezelfde verpakking als voor de update opnieuw geselecteerd kan worden if (VorigeIndexInList >= 0) and (VorigeIndexInList < ApplicationContext.ProductDetailContext.ProductVerpakkingen.Count) then ProductVerpakking := ApplicationContext.ProductDetailContext.ProductVerpakkingen[VorigeIndexInList] else if ApplicationContext.ProductDetailContext.ProductVerpakkingen.Count > 0 then ProductVerpakking := ApplicationContext.ProductDetailContext.ProductVerpakkingen[0] // Default eerste verpakking selecteren else ProductVerpakking := nil; // Geen verpakking aanwezig if ProductVerpakking <> nil then ToonProductVerpakking(ProductVerpakking); UpdateControlsEnableStatus() end; procedure TFormProductDetail.ToonProductVerpakking(ProductVerpakking: TProductVerpakking); var I: Integer; TempStr: string; Item: string; begin // Linkerhelft EditMeta.Text := ProductVerpakking.MetaCaption; // DeelVan ComboBoxDeelVan.Items.Clear(); // ComboBox vullen en item selecteren. for TempStr in ApplicationContext.ProductDetailContext.GetDeelVanOpties(ProductVerpakking) do begin ComboBoxDeelVan.Items.Add(TempStr); end; ComboBoxDeelVan.Items.Add(NERGENS_DEEL_VAN_CAPTION); // Item selecteren if ProductVerpakking.DeelVan <> nil then // Index zoeken in lijst van producten ComboBoxDeelVan.ItemIndex := ApplicationContext.ProductDetailContext.ProductVerpakkingen.IndexOf(ProductVerpakking.DeelVan) else ComboBoxDeelVan.ItemIndex := ComboBoxDeelVan.Items.Count - 1; // Default waarde: 'niks' optie selecteren if ProductVerpakking.Breedte <> '' then EditBreedte.Text := ProductVerpakking.Breedte else EditBreedte.Text := ProductVerpakking.DefaultBreedte; // Default if ProductVerpakking.Diepte <> '' then EditDiepte.Text := ProductVerpakking.Diepte else EditDiepte.Text := ProductVerpakking.DefaultDiepte; // Default if ProductVerpakking.Hoogte <> '' then EditHoogte.Text := ProductVerpakking.Hoogte else EditHoogte.Text := ProductVerpakking.DefaultHoogte; EditMaxCombAantal.Text := ProductVerpakking.MaxCombinAantal; EditAantal.Text := ProductVerpakking.Aantal; // Rechterhelft // Plaatsing ComboBoxPlaatsing.Items.Clear(); // Combobox vullen en item selecteren. Indien item niet aanwezig is, eerste item selecteren. for Item in ApplicationContext.UiInstellingenContext.Plaatsing.Values do begin ComboBoxPlaatsing.Items.Add(Item); end; if (ApplicationContext.UiInstellingenContext.Plaatsing.TryGetValue(ProductVerpakking.Plaatsing, Item) = True) then ComboBoxPlaatsing.ItemIndex := ComboBoxPlaatsing.Items.IndexOf(Item) else if ComboBoxPlaatsing.Items.Count > 0 then ComboBoxPlaatsing.ItemIndex := 0; // Positie CheckComboBoxPositie.Items.Clear(); // CheckComboBox vullen en items selecteren. { TODO : Vertaling naar gewone string waarden uit Context halen en tonen in checked combobox } for Item in ApplicationContext.UiInstellingenContext.Positie.Keys do begin CheckComboBoxPositie.Items.Add(Item); end; if ProductVerpakking.Positie = '' then // Lege positie betekent alles aangeduid for I := 0 to CheckComboBoxPositie.Items.Count - 1 do CheckComboBoxPositie.Checked[I] := True else for TempStr in SplitString(ProductVerpakking.Positie, '') do if CheckComboBoxPositie.Items.IndexOf(TempStr) >= 0 then CheckComboBoxPositie.Checked[CheckComboBoxPositie.Items.IndexOf(TempStr)] := True; // Richting CheckComboBoxRichting.Items.Clear(); // CheckComboBox vullen en item selecteren. Als waarde niet aanwezig is, uit default halen. { TODO : Vertaling naar gewone string waarden uit Context halen en tonen in checked combobox } for Item in ApplicationContext.UiInstellingenContext.Richting.Keys do begin CheckComboBoxRichting.Items.Add(Item); end; if ProductVerpakking.Richting <> '' then begin for TempStr in SplitString(ProductVerpakking.Richting, '') do if CheckComboBoxRichting.Items.IndexOf(TempStr) >= 0 then CheckComboBoxRichting.Checked[CheckComboBoxRichting.Items.IndexOf(TempStr)] := True; end; end; procedure TFormProductDetail.UpdateControlsEnableStatus(); var VerpakkingAanwezigEnGeselecteerd: Boolean; I: Integer; begin // Details controls enable status aanpassen naargelang er een verpakking geselecteerd is. VerpakkingAanwezigEnGeselecteerd := (ListViewVerpakkingen.Items.Count = 0) or (ListViewVerpakkingen.ItemIndex < 0); for I := 0 to PanelVerpakkingControls.ControlCount - 1 do if Assigned(PanelVerpakkingControls.Controls[I]) then PanelVerpakkingControls.Controls[I].Enabled := VerpakkingAanwezigEnGeselecteerd; ButtonVerwijder.Enabled := VerpakkingAanwezigEnGeselecteerd; // Linkerdeel ComboBoxDeelVan.Enabled := (ComboBoxDeelVan.Items.Count > 0) and VerpakkingAanwezigEnGeselecteerd; end; procedure TFormProductDetail.UpdateGuiDataAanwezigheid(Aanwezig: Boolean); var I: Integer; begin if Aanwezig then begin // Skip update als alles al zichtbaar staat. if Assigned(LabelGeenData) = False then exit; // Wel resultaten, toon alles. for I := 0 to self.ControlCount - 1 do if Assigned(self.Controls[I]) then self.Controls[I].Visible := True; LabelGeenData.Visible := False; PanelVerpakkingControls.Refresh(); // Anders verdwijnt label enkel op resize FreeAndNil(LabelGeenData); end else begin // Skip update als melding al getoond wordt en alles onzichtbaar staat. if Assigned(LabelGeenData) then exit; // Geen resultaten, verberg kinderen, toon melding (label) en stop. for I := 0 to self.ControlCount - 1 do if Assigned(self.Controls[I]) then self.Controls[I].Visible := False; // Label tonen LabelGeenData := TLabel.Create(self); with LabelGeenData do begin Parent := self; Caption := GEEN_DATA_CAPTION; Font.Size := 15; // Centreren: https://stackoverflow.com/questions/18545937/make-a-component-appear-in-the-middle-of-form-no-matter-what-screen-resolution Left := (self.Width - Width) div 2; Top := (self.Height - Height) div 2; Anchors := []; // Relatief tegenover parent end; end end; end.