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
   ClientWidth = 713
   DesignTimePPI = 120
+  LCLVersion = '4.99.0.0'
   OnCreate = FormCreate
   object ifJpeg: TBCPanel
     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
  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.
  In this case the user is responsible for releasing the TBGRAFormatUIContainer class.
 }
@@ -23,7 +23,7 @@ interface
 
 uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Buttons,
-  TypInfo, Rtti, FpImage, Laz2_XMLCfg,
+  TypInfo, Rtti, FpImage,
   BCPanel, BCTrackbarUpdown, BCFluentSlider,
   BGRABitmapTypes;
 
@@ -124,6 +124,9 @@ type
                          var AWriter: TFPCustomImageWriter;
                          var APanel: TBCPanel): Boolean;
 
+    class function BuildSaveFormats(const AControl: TControl;
+                                    const ASelectFormat: TBGRAImageFormat=ifUnknown): Integer;
+
     //Set TFPCustomImageWriter Properties from UI
     procedure SetWriterProperties(var AWriter: TFPCustomImageWriter);
 
@@ -138,6 +141,8 @@ implementation
 
 {$R *.lfm}
 
+uses BCComboBox;
+
 const
   BMP_BitsValidValues: array[0..6] of Integer = (1,4,8,15,16,24,32);
 
@@ -206,6 +211,61 @@ begin
   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;
 var
    minVal, maxVal, intVal,
@@ -223,11 +283,11 @@ var
        0: begin
             iIndex:= intVal;
             Result:= True;
-          end;
+       end;
        1, -1: begin
             iIndex:= AItems.IndexOfObject(TObject(PtrUInt(intVal)));
             Result := (iIndex > -1);
-          end;
+       end;
        2: if (AValue.Kind = tkEnumeration)
           then begin
                  Result:= False;
@@ -294,7 +354,7 @@ begin
             Value:= intVal;
           end
      else
-     if (AControl is TTrackbar)
+     (*if (AControl is TTrackbar)
      then with TTrackbar(AControl) do
           begin
             if (AValue.Kind = tkEnumeration) then
@@ -304,7 +364,7 @@ begin
             end;
             Position:= intVal;
           end
-     else
+     else*)
      if (AControl is TComboBox)
      then with TComboBox(AControl) do
           begin
@@ -355,9 +415,9 @@ begin
      if (AControl is TBCTrackbarUpdown)
      then AValue:= TBCTrackbarUpdown(AControl).Value
      else
-     if (AControl is TTrackbar)
+     (*if (AControl is TTrackbar)
      then AValue:= TTrackbar(AControl).Position
-     else
+     else*)
      if (AControl is TComboBox)
      then with TComboBox(AControl) do
           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,
   FpImage,
   BGRABitmapTypes,
-  BCPanel, BCLabel, BCRoundedImage, BCButton, BGRADialogs;
+  BCPanel, BCRoundedImage, BCButton, BGRADialogs;
 
 type
   { TSaveFile_Settings }
@@ -45,15 +45,11 @@ type
     procedure FormShow(Sender: TObject);
 
   private
-    SaveFormat,
-    UserSaveFormat: TBGRAImageFormat;
-    SaveWriter,
-    UserSaveWriter: TFPCustomImageWriter;
+    SaveFormat: TBGRAImageFormat;
+    SaveWriter: TFPCustomImageWriter;
     SavePath: String;
     panelFormatUI: TBCPanel;
-    createdWriter: Boolean;
 
-    procedure BuildSaveFormats;
     procedure AdjustFormatPanel;
 
   public
@@ -71,25 +67,6 @@ uses Math, BGRAFormatUI, BGRAReadLzp, BGRAWriteLzp, BGRAPaintNet;
 
 { 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;
 begin
   lbExt.Caption:= '.'+SuggestImageExtension(SaveFormat);
@@ -136,6 +113,9 @@ begin
 end;
 
 procedure TSaveFile_Settings.btSaveClick(Sender: TObject);
+var
+   ext: String;
+
 begin
   if not(bcImage.Bitmap.Empty) then
   begin
@@ -143,8 +123,11 @@ begin
        (panelFormatUI <> nil)
     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);
+
+    MessageDlg('Saved as '#13#10+edFileName.Text+ext+#13#10'Class='+SaveWriter.ClassName, mtInformation, [mbOk], 0);
   end;
 end;
 
@@ -163,16 +146,17 @@ procedure TSaveFile_Settings.FormShow(Sender: TObject);
 begin
   try
      panelFormatUI:= nil;
-     BuildSaveFormats;
 
      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
     AdjustFormatPanel;