Browse Source

Added FormaUI BuildSaveFormats; code cleanup

Massimo Magnano 8 months ago
parent
commit
bb33167950
3 changed files with 87 additions and 42 deletions
  1. 1 0
      bgraformatui.lfm
  2. 68 8
      bgraformatui.pas
  3. 18 34
      test/test_formatui/test_formatui_main.pas

+ 1 - 0
bgraformatui.lfm

@@ -7,6 +7,7 @@ object BGRAFormatUIContainer: TBGRAFormatUIContainer
   ClientHeight = 530
   ClientHeight = 530
   ClientWidth = 713
   ClientWidth = 713
   DesignTimePPI = 120
   DesignTimePPI = 120
+  LCLVersion = '4.99.0.0'
   OnCreate = FormCreate
   OnCreate = FormCreate
   object ifJpeg: TBCPanel
   object ifJpeg: TBCPanel
     Left = 0
     Left = 0

+ 68 - 8
bgraformatui.pas

@@ -10,7 +10,7 @@
  When it is executed calling Execute ONLY the panel of the selected format will be
  When it is executed calling Execute ONLY the panel of the selected format will be
  visible and the form will be resized accordingly.
  visible and the form will be resized accordingly.
 
 
- Another way to use it is to call the GetUI method to take ONLY the panel of the
+ Another way to use it is to call the GetUI method to take the panel of the
  selected format, so that you can change its parent and use it in another form.
  selected format, so that you can change its parent and use it in another form.
  In this case the user is responsible for releasing the TBGRAFormatUIContainer class.
  In this case the user is responsible for releasing the TBGRAFormatUIContainer class.
 }
 }
@@ -23,7 +23,7 @@ interface
 
 
 uses
 uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Buttons,
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Buttons,
-  TypInfo, Rtti, FpImage, Laz2_XMLCfg,
+  TypInfo, Rtti, FpImage,
   BCPanel, BCTrackbarUpdown, BCFluentSlider,
   BCPanel, BCTrackbarUpdown, BCFluentSlider,
   BGRABitmapTypes;
   BGRABitmapTypes;
 
 
@@ -124,6 +124,9 @@ type
                          var AWriter: TFPCustomImageWriter;
                          var AWriter: TFPCustomImageWriter;
                          var APanel: TBCPanel): Boolean;
                          var APanel: TBCPanel): Boolean;
 
 
+    class function BuildSaveFormats(const AControl: TControl;
+                                    const ASelectFormat: TBGRAImageFormat=ifUnknown): Integer;
+
     //Set TFPCustomImageWriter Properties from UI
     //Set TFPCustomImageWriter Properties from UI
     procedure SetWriterProperties(var AWriter: TFPCustomImageWriter);
     procedure SetWriterProperties(var AWriter: TFPCustomImageWriter);
 
 
@@ -138,6 +141,8 @@ implementation
 
 
 {$R *.lfm}
 {$R *.lfm}
 
 
+uses BCComboBox;
+
 const
 const
   BMP_BitsValidValues: array[0..6] of Integer = (1,4,8,15,16,24,32);
   BMP_BitsValidValues: array[0..6] of Integer = (1,4,8,15,16,24,32);
 
 
@@ -206,6 +211,61 @@ begin
   end;
   end;
 end;
 end;
 
 
+class function TBGRAFormatUIContainer.BuildSaveFormats(const AControl: TControl;
+                                                       const ASelectFormat: TBGRAImageFormat): Integer;
+var
+  iFormat: TBGRAImageFormat;
+  aItems: TStrings;
+
+  procedure SetItemIndex(AValue: Integer);
+  begin
+    if (AControl is TComboBox)
+    then TComboBox(AControl).ItemIndex:= AValue
+    else
+    if (AControl is TBCComboBox)
+    then TBCComboBox(AControl).ItemIndex:= AValue
+    else
+    if (AControl is TRadioGroup)
+    then TRadioGroup(AControl).ItemIndex:= AValue;
+  end;
+
+begin
+  Result:= 0;
+
+  if (AControl is TComboBox)
+  then with TComboBox(AControl) do
+       begin
+         Clear;
+         aItems:= Items;
+       end
+  else
+  if (AControl is TBCComboBox)
+  then with TBCComboBox(AControl) do
+       begin
+         Clear;
+         aItems:= Items;
+       end
+  else
+  if (AControl is TRadioGroup)
+  then with TRadioGroup(AControl) do
+       begin
+         aItems:= Items;
+         aItems.Clear;
+       end
+  else exit;
+
+  for iFormat:=Low(TBGRAImageFormat) to High(TBGRAImageFormat) do
+    if (iFormat <> ifUnknown) and (DefaultBGRAImageWriter[iFormat] <> nil) then
+      aItems.AddObject(BGRAImageFormat[iFormat].TypeName+' (.'+SuggestImageExtension(iFormat)+')',
+                                TObject(PTRUInt(iFormat)));
+
+  Result:= aItems.Count;
+  if (Result > 0)
+  then if (ASelectFormat = ifUnknown)
+       then SetItemIndex(0)
+       else SetItemIndex(aItems.IndexOfObject(TObject(PTRUInt(ASelectFormat))));
+end;
+
 function TBGRAFormatUIContainer.SetControlValue(const AValue: TValue; const AControl: TControl): Boolean;
 function TBGRAFormatUIContainer.SetControlValue(const AValue: TValue; const AControl: TControl): Boolean;
 var
 var
    minVal, maxVal, intVal,
    minVal, maxVal, intVal,
@@ -223,11 +283,11 @@ var
        0: begin
        0: begin
             iIndex:= intVal;
             iIndex:= intVal;
             Result:= True;
             Result:= True;
-          end;
+       end;
        1, -1: begin
        1, -1: begin
             iIndex:= AItems.IndexOfObject(TObject(PtrUInt(intVal)));
             iIndex:= AItems.IndexOfObject(TObject(PtrUInt(intVal)));
             Result := (iIndex > -1);
             Result := (iIndex > -1);
-          end;
+       end;
        2: if (AValue.Kind = tkEnumeration)
        2: if (AValue.Kind = tkEnumeration)
           then begin
           then begin
                  Result:= False;
                  Result:= False;
@@ -294,7 +354,7 @@ begin
             Value:= intVal;
             Value:= intVal;
           end
           end
      else
      else
-     if (AControl is TTrackbar)
+     (*if (AControl is TTrackbar)
      then with TTrackbar(AControl) do
      then with TTrackbar(AControl) do
           begin
           begin
             if (AValue.Kind = tkEnumeration) then
             if (AValue.Kind = tkEnumeration) then
@@ -304,7 +364,7 @@ begin
             end;
             end;
             Position:= intVal;
             Position:= intVal;
           end
           end
-     else
+     else*)
      if (AControl is TComboBox)
      if (AControl is TComboBox)
      then with TComboBox(AControl) do
      then with TComboBox(AControl) do
           begin
           begin
@@ -355,9 +415,9 @@ begin
      if (AControl is TBCTrackbarUpdown)
      if (AControl is TBCTrackbarUpdown)
      then AValue:= TBCTrackbarUpdown(AControl).Value
      then AValue:= TBCTrackbarUpdown(AControl).Value
      else
      else
-     if (AControl is TTrackbar)
+     (*if (AControl is TTrackbar)
      then AValue:= TTrackbar(AControl).Position
      then AValue:= TTrackbar(AControl).Position
-     else
+     else*)
      if (AControl is TComboBox)
      if (AControl is TComboBox)
      then with TComboBox(AControl) do
      then with TComboBox(AControl) do
           begin
           begin

+ 18 - 34
test/test_formatui/test_formatui_main.pas

@@ -17,7 +17,7 @@ uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, EditBtn, Buttons,
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, EditBtn, Buttons,
   FpImage,
   FpImage,
   BGRABitmapTypes,
   BGRABitmapTypes,
-  BCPanel, BCLabel, BCRoundedImage, BCButton, BGRADialogs;
+  BCPanel, BCRoundedImage, BCButton, BGRADialogs;
 
 
 type
 type
   { TSaveFile_Settings }
   { TSaveFile_Settings }
@@ -45,15 +45,11 @@ type
     procedure FormShow(Sender: TObject);
     procedure FormShow(Sender: TObject);
 
 
   private
   private
-    SaveFormat,
-    UserSaveFormat: TBGRAImageFormat;
-    SaveWriter,
-    UserSaveWriter: TFPCustomImageWriter;
+    SaveFormat: TBGRAImageFormat;
+    SaveWriter: TFPCustomImageWriter;
     SavePath: String;
     SavePath: String;
     panelFormatUI: TBCPanel;
     panelFormatUI: TBCPanel;
-    createdWriter: Boolean;
 
 
-    procedure BuildSaveFormats;
     procedure AdjustFormatPanel;
     procedure AdjustFormatPanel;
 
 
   public
   public
@@ -71,25 +67,6 @@ uses Math, BGRAFormatUI, BGRAReadLzp, BGRAWriteLzp, BGRAPaintNet;
 
 
 { TSaveFile_Settings }
 { TSaveFile_Settings }
 
 
-procedure TSaveFile_Settings.BuildSaveFormats;
-var
-  iFormat: TBGRAImageFormat;
-
-begin
-  cbSaveFormat.Clear;
-
-  for iFormat:=Low(TBGRAImageFormat) to High(TBGRAImageFormat) do
-  begin
-    if (iFormat <> ifUnknown) and (DefaultBGRAImageWriter[iFormat] <> nil) then
-    begin
-      cbSaveFormat.Items.AddObject(BGRAImageFormat[iFormat].TypeName+' ('+SuggestImageExtension(iFormat)+')',
-                                   TObject(PTRUInt(iFormat)));
-    end;
-  end;
-
-  if (cbSaveFormat.Items.Count > 0) then cbSaveFormat.ItemIndex:= 0;
-end;
-
 procedure TSaveFile_Settings.AdjustFormatPanel;
 procedure TSaveFile_Settings.AdjustFormatPanel;
 begin
 begin
   lbExt.Caption:= '.'+SuggestImageExtension(SaveFormat);
   lbExt.Caption:= '.'+SuggestImageExtension(SaveFormat);
@@ -136,6 +113,9 @@ begin
 end;
 end;
 
 
 procedure TSaveFile_Settings.btSaveClick(Sender: TObject);
 procedure TSaveFile_Settings.btSaveClick(Sender: TObject);
+var
+   ext: String;
+
 begin
 begin
   if not(bcImage.Bitmap.Empty) then
   if not(bcImage.Bitmap.Empty) then
   begin
   begin
@@ -143,8 +123,11 @@ begin
        (panelFormatUI <> nil)
        (panelFormatUI <> nil)
     then BGRAFormatUIContainer.SetWriterProperties(SaveWriter);
     then BGRAFormatUIContainer.SetWriterProperties(SaveWriter);
 
 
-    bcImage.Bitmap.SaveToFile(SavePath+DirectorySeparator+edFileName.Text+'.'+SuggestImageExtension(SaveFormat),
+    ext:= '.'+SuggestImageExtension(SaveFormat);
+    bcImage.Bitmap.SaveToFile(SavePath+DirectorySeparator+edFileName.Text+ext,
                               SaveWriter);
                               SaveWriter);
+
+    MessageDlg('Saved as '#13#10+edFileName.Text+ext+#13#10'Class='+SaveWriter.ClassName, mtInformation, [mbOk], 0);
   end;
   end;
 end;
 end;
 
 
@@ -163,16 +146,17 @@ procedure TSaveFile_Settings.FormShow(Sender: TObject);
 begin
 begin
   try
   try
      panelFormatUI:= nil;
      panelFormatUI:= nil;
-     BuildSaveFormats;
 
 
      dirDestination.Directory:= ExtractFileDir(ParamStr(0));
      dirDestination.Directory:= ExtractFileDir(ParamStr(0));
 
 
-     //Select Current Format, if not found Select Jpeg
-     cbSaveFormat.ItemIndex:= cbSaveFormat.Items.IndexOfObject(TObject(PTRUInt(ifJpeg)));
-     SaveFormat:= ifJpeg;
-     SaveWriter:= CreateBGRAImageWriter(SaveFormat, True);
-
-     TBGRAFormatUIContainer.GetUI(SaveFormat, SaveWriter, panelFormatUI);
+     //Select JPeg as Current Format
+     if (TBGRAFormatUIContainer.BuildSaveFormats(cbSaveFormat, ifJpeg) > 0) then
+     begin
+       SaveFormat:= ifJpeg;
+       SaveWriter:= CreateBGRAImageWriter(SaveFormat, True);
+       TBGRAFormatUIContainer.GetUI(SaveFormat, SaveWriter, panelFormatUI);
+     end
+     else raise Exception.Create('No Writers Registered...');
 
 
   finally
   finally
     AdjustFormatPanel;
     AdjustFormatPanel;