fpspreadsheet: Improved increasing/decreasing of decimal count by means of action control.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3726 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-11-14 21:18:16 +00:00
parent 235340289a
commit 0a34c6314a
3 changed files with 75 additions and 33 deletions

View File

@ -638,14 +638,12 @@ object Form1: TForm1
Category = 'FPSpreadsheet' Category = 'FPSpreadsheet'
WorkbookSource = WorkbookSource WorkbookSource = WorkbookSource
Caption = 'Decimals' Caption = 'Decimals'
Hint = 'Decimal places'
ImageIndex = 21 ImageIndex = 21
end end
object AcDecDecimals: TsDecimalsAction object AcDecDecimals: TsDecimalsAction
Category = 'FPSpreadsheet' Category = 'FPSpreadsheet'
WorkbookSource = WorkbookSource WorkbookSource = WorkbookSource
Caption = 'Decimals' Caption = 'Decimals'
Hint = 'Decimal places'
ImageIndex = 20 ImageIndex = 20
Delta = -1 Delta = -1
end end

View File

@ -232,18 +232,17 @@ type
private private
FDecimals: Integer; FDecimals: Integer;
FDelta: Integer; FDelta: Integer;
procedure SetDecimals(AValue: Integer);
procedure SetDelta(AValue: Integer); procedure SetDelta(AValue: Integer);
protected protected
procedure ApplyFormatToCell(ACell: PCell); override; procedure ApplyFormatToCell(ACell: PCell); override;
procedure ExtractFromCell(ACell: PCell); override; procedure ExtractFromCell(ACell: PCell); override;
property Decimals: Integer
read FDecimals write SetDecimals default 2;
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
published published
property Caption stored false;
property Delta: Integer property Delta: Integer
read FDelta write SetDelta default +1; read FDelta write SetDelta default +1;
property Hint stored false;
end; end;
@ -321,6 +320,7 @@ end;
procedure TsWorksheetAction.UpdateTarget(Target: TObject); procedure TsWorksheetAction.UpdateTarget(Target: TObject);
begin begin
Unused(Target);
Enabled := inherited Enabled and (Worksheet <> nil); Enabled := inherited Enabled and (Worksheet <> nil);
end; end;
@ -460,6 +460,7 @@ end;
specified cell. Must be overridden by descendants. } specified cell. Must be overridden by descendants. }
procedure TsCellFormatAction.ApplyFormatToCell(ACell: PCell); procedure TsCellFormatAction.ApplyFormatToCell(ACell: PCell);
begin begin
Unused(ACell);
end; end;
procedure TsCellFormatAction.ExecuteTarget(Target: TObject); procedure TsCellFormatAction.ExecuteTarget(Target: TObject);
@ -486,6 +487,7 @@ end;
specified cell. Must be overridden by descendants. } specified cell. Must be overridden by descendants. }
procedure TsCellFormatAction.ExtractFromCell(ACell: PCell); procedure TsCellFormatAction.ExtractFromCell(ACell: PCell);
begin begin
Unused(ACell);
end; end;
function TsCellFormatAction.HandlesTarget(Target: TObject): Boolean; function TsCellFormatAction.HandlesTarget(Target: TObject): Boolean;
@ -497,6 +499,8 @@ procedure TsCellFormatAction.UpdateTarget(Target: TObject);
var var
cell: PCell; cell: PCell;
begin begin
Unused(Target);
Enabled := inherited Enabled and (Worksheet <> nil) and (Length(GetSelection) > 0); Enabled := inherited Enabled and (Worksheet <> nil) and (Length(GetSelection) > 0);
if not Enabled then if not Enabled then
exit; exit;
@ -531,7 +535,6 @@ end;
procedure TsFontStyleAction.ExtractFromCell(ACell: PCell); procedure TsFontStyleAction.ExtractFromCell(ACell: PCell);
var var
fnt: TsFont; fnt: TsFont;
fs: TsFontStyles;
begin begin
if (ACell = nil) then if (ACell = nil) then
Checked := false Checked := false
@ -796,45 +799,46 @@ constructor TsDecimalsAction.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
Caption := 'Decimals'; Caption := 'Decimals';
Hint := 'Decimal places'; Delta := +1;
FDelta := +1;
end; end;
procedure TsDecimalsAction.ApplyFormatToCell(ACell: PCell); procedure TsDecimalsAction.ApplyFormatToCell(ACell: PCell);
var var
decs: Integer; decs: Integer;
currSym: String;
begin begin
if not (uffNumberFormat in ACell^.UsedFormattingFields) or
(ACell^.NumberFormat = nfGeneral)
then
Worksheet.WriteNumberFormat(ACell, nfFixed, '0')
else
if IsDateTimeFormat(ACell^.NumberFormat) then if IsDateTimeFormat(ACell^.NumberFormat) then
exit exit;
if (ACell^.ContentType in [cctEmpty, cctNumber]) and (
(not (uffNumberFormat in ACell^.UsedFormattingFields)) or
(ACell^.NumberFormat = nfGeneral)
) then
decs := Worksheet.GetDisplayedDecimals(ACell)
else else
begin decs := FDecimals;
decs := Decimals + FDelta; inc(decs, FDelta);
if decs < 0 then decs := 0; if decs < 0 then decs := 0;
Worksheet.WriteDecimals(ACell, decs); Worksheet.WriteDecimals(ACell, decs);
end; end;
end;
procedure TsDecimalsAction.ExtractFromCell(ACell: PCell); procedure TsDecimalsAction.ExtractFromCell(ACell: PCell);
var var
csym: String; csym: String;
decs: Byte; decs: Byte;
begin begin
decs := 2; if ACell = nil then begin
if (ACell <> nil) and (uffNumberFormat in ACell^.UsedFormattingFields) then FDecimals := 2;
Worksheet.GetNumberFormatAttributes(ACell, decs, csym); exit;
Decimals := decs
end; end;
procedure TsDecimalsAction.SetDecimals(AValue: Integer); if (ACell^.ContentType in [cctEmpty, cctNumber]) and (
begin (not (uffNumberFormat in ACell^.UsedFormattingFields)) or
FDecimals := AValue; (ACell^.NumberFormat = nfGeneral)
if FDecimals < 0 then FDecimals := 0; ) then
decs := Worksheet.GetDisplayedDecimals(ACell)
else
Worksheet.GetNumberFormatAttributes(ACell, decs, csym);
FDecimals := decs;
end; end;
procedure TsDecimalsAction.SetDelta(AValue: Integer); procedure TsDecimalsAction.SetDelta(AValue: Integer);

View File

@ -607,6 +607,7 @@ type
function ReadNumericValue(ACell: PCell; out AValue: Double): Boolean; function ReadNumericValue(ACell: PCell; out AValue: Double): Boolean;
{ Reading of cell attributes } { Reading of cell attributes }
function GetDisplayedDecimals(ACell: PCell): Byte;
function GetNumberFormatAttributes(ACell: PCell; out ADecimals: Byte; function GetNumberFormatAttributes(ACell: PCell; out ADecimals: Byte;
out ACurrencySymbol: String): Boolean; out ACurrencySymbol: String): Boolean;
function ReadUsedFormatting(ARow, ACol: Cardinal): TsUsedFormattingFields; overload; function ReadUsedFormatting(ARow, ACol: Cardinal): TsUsedFormattingFields; overload;
@ -2210,6 +2211,32 @@ begin
Result := FCells.Count; Result := FCells.Count;
end; end;
{@@ ----------------------------------------------------------------------------
Determines the number of decimals displayed for the number in the cell
@param ACell Pointer to the cell under investigation
@return Number of decimals places used in the string display of the cell.
-------------------------------------------------------------------------------}
function TsWorksheet.GetDisplayedDecimals(ACell: PCell): Byte;
var
i, p: Integer;
s: String;
begin
Result := 0;
if (ACell <> nil) and (ACell^.ContentType = cctNumber) then
begin
s := ReadAsUTF8Text(ACell);
p := pos(Workbook.FormatSettings.DecimalSeparator, s);
if p > 0 then
begin
i := p+1;
while (i <= Length(s)) and (s[i] in ['0'..'9']) do inc(i);
Result := i - (p+1);
end;
end;
end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
Determines some number format attributes (decimal places, currency symbol) of Determines some number format attributes (decimal places, currency symbol) of
a cell a cell
@ -2234,9 +2261,15 @@ begin
begin begin
parser := TsNumFormatParser.Create(FWorkbook, ACell^.NumberFormatStr); parser := TsNumFormatParser.Create(FWorkbook, ACell^.NumberFormatStr);
try try
if parser.Status = psOK then begin if parser.Status = psOK then
begin
nf := parser.NumFormat; nf := parser.NumFormat;
if (nf = nfGeneral) or IsDateTimeFormat(nf) then if (nf = nfGeneral) and (ACell^.ContentType = cctNumber) then
begin
ADecimals := GetDisplayedDecimals(ACell);
ACurrencySymbol := '';
end else
if IsDateTimeFormat(nf) then
begin begin
ADecimals := 2; ADecimals := 2;
ACurrencySymbol := '?'; ACurrencySymbol := '?';
@ -4422,8 +4455,15 @@ procedure TsWorksheet.WriteDecimals(ACell: PCell; ADecimals: Byte);
var var
parser: TsNumFormatParser; parser: TsNumFormatParser;
begin begin
if (ACell <> nil) and (ACell^.ContentType = cctNumber) and if (ACell = nil) then //or (ACell^.ContentType <> cctNumber) then
(ACell^.NumberFormat <> nfCustom) then exit;
if (uffNumberFormat in ACell^.UsedFormattingFields) or (ACell^.NumberFormat = nfGeneral)
then begin
WriteNumberFormat(ACell, nfFixed, ADecimals);
ChangedCell(ACell^.Row, ACell^.Col);
end else
if (ACell^.NumberFormat <> nfCustom) then
begin begin
parser := TsNumFormatParser.Create(Workbook, ACell^.NumberFormatStr); parser := TsNumFormatParser.Create(Workbook, ACell^.NumberFormatStr);
try try