Özel bir StringGrid - narkotik - 08-09-2019
Merhaba,
Keyfe keder 1 gün içinde mobil için aşağıdaki gridi yazdım. Zamanla geliştirmeyi düşünüyorum. Sizlerden de görüş ve önerilerinizi bekliyorum. Özellikle Webservis uygulamalarında desteği için JSONToDataset ve DatasetToJSON yapısına ihtiyacım var. XML olarak yakında boş bir vaktimde kendim desteklemeyi düşünüyorum.
Örnek


unit SKStringGrid;
{****************************
SKStringGrid.pas 2019
S.B.
*****************************}
interface
uses SysUtils, Classes, Types, Rtti, UITypes, Generics.Collections, Data.DB, FMX.Grid, FMX.Graphics,
FMX.Presentation.Factory, FMX.Presentation.Style, FMX.Controls, FMX.Grid.Style, FMX.Types;
type TSKStringGridColumnType = (SKColumnString,SKColumnFloat,SKColumnInteger,SKColumnDate,SKColumnDateTime,SKColumnNumber);
type
TSKStringColumn = class(TStringColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKFloatColumn = class(TFloatColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKIntegerColumn = class(TIntegerColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKDateColumn = class(TStringColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKDateTimeColumn = class(TStringColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKNumberColumn = class(TStringColumn)
strict private
FSKStringGridColumnType : TSKStringGridColumnType;
private
//
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property SKStringGridColumnType : TSKStringGridColumnType read FSKStringGridColumnType write FSKStringGridColumnType;
end;
type
TSKStringGrid = class(TStringGrid)
strict private
FNotLoadFields : String;
ListWithColumn : TList<String>;
ListMaxWithColumn : TList<Single>;
function GetSKStringGridColumnType(const AColumnIndex:Integer) : TSKStringGridColumnType;
private
procedure CstDrawColumnCell(Sender: TObject;const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;const Row: Integer; const Value: TValue; const State: TGridDrawStates);
procedure CstDrawColumnHeader(Sender: TObject;const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF);
protected
//
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure LoadFromDataset(ADataset:TDataSet);
procedure SQLFieldNameToCaption;
procedure AutoSizeColumn;
function GetColumnAsString(const AColumnName:String):String;
function GetColumnAsInteger(const AColumnName:String):Integer;
function GetColumnAsFloat(const AColumnName:String) ouble;
function GetColumnAsDate(const AColumnName:String):TDate;
function GetColumnAsDateTime(const AColumnName:String):TDateTime;
procedure SetColumnVisible(const AColumnName:String;const AVisible:Boolean);
procedure SetColumnWidth(const AColumnName:String;const AWidth:Integer);
procedure SetColumnCaption(const AColumnName,ACaption:String);
published
property NotLoadFields:String read FNotLoadFields write FNotLoadFields; // Yüklenmesi istenmeyen alanlar virgülle yazılır Örn:'STOK_ADI,FIYAT'
end;
var
// Const initialization
HeaderFontSize : Integer;
RowFontSize : Integer;
RowZebraColor : TAlphaColorRec;
ColumnHeaderColor : TAlphaColorRec;
ColumnSiraColor : TAlphaColorRec;
RowSelectedColor : TAlphaColorRec;
implementation
{ TSKStringGrid }
procedure TSKStringGrid.AutoSizeColumn;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
begin
Columns[Ind].BeginUpdate;
Columns[Ind].Width := ListMaxWithColumn[ListWithColumn.IndexOf(Columns[Ind].Name)] + 10;
Columns[Ind].EndUpdate;
end;
end;
procedure TSKStringGrid.SetColumnCaption(const AColumnName, ACaption: String);
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
begin
Columns[Ind].BeginUpdate;
if Columns[Ind].Name = 'Clmn' + AColumnName then
begin
Columns[Ind].Header := ACaption;
Exit;
end;
Columns[Ind].EndUpdate;
end;
end;
procedure TSKStringGrid.SetColumnVisible(const AColumnName: String; const AVisible: Boolean);
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
begin
Columns[Ind].BeginUpdate;
if Columns[Ind].Name = 'Clmn' + AColumnName then
begin
Columns[Ind].Visible := AVisible;
Exit;
end;
Columns[Ind].EndUpdate;
end;
end;
procedure TSKStringGrid.SetColumnWidth(const AColumnName: String; const AWidth: Integer);
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
begin
Columns[Ind].BeginUpdate;
if Columns[Ind].Name = 'Clmn' + AColumnName then
begin
Columns[Ind].Width := AWidth;
Exit;
end;
Columns[Ind].EndUpdate;
end;
end;
procedure TSKStringGrid.SQLFieldNameToCaption;
Var
Ind,Ind2 : Integer;
xNewCaption : String;
begin
for Ind := 0 to Pred(ColumnCount) do
begin
if Trim(Columns[Ind].Header) = '' then
Continue;
Columns[Ind].BeginUpdate;
xNewCaption := '';
Columns[Ind].Header := StringReplace(Columns[Ind].Header,'_',' ',[rfReplaceAll]);
Columns[Ind].Header := AnsiLowerCase(Columns[Ind].Header);
xNewCaption := AnsiUpperCase(Columns[Ind].Header[Low(Columns[Ind].Header)]);
for Ind2 := Succ(Low(Columns[Ind].Header)) to Length(Columns[Ind].Header) do
begin
if (Columns[Ind].Header[Pred(Ind2)] = ' ') then
xNewCaption := Concat(xNewCaption,AnsiUpperCase(Columns[Ind].Header[Ind2]))
else
xNewCaption := Concat(xNewCaption,Columns[Ind].Header[Ind2]);
end;
Columns[Ind].Header := xNewCaption;
Columns[Ind].EndUpdate;
end;
end;
constructor TSKStringGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Options := [TGridOption.ColLines,TGridOption.RowLines,TGridOption.RowSelect,TGridOption.Tabs,TGridOption.Header,TGridOption.HeaderClick,TGridOption.AutoDisplacement];
RowHeight := 20;
OnDrawColumnCell := CstDrawColumnCell;
OnDrawColumnHeader := CstDrawColumnHeader;
StyledSettings := [TStyledSetting.FontColor,TStyledSetting.Style];
TextSettings.Font.Size := RowFontSize;
ListMaxWithColumn := TList<Single>.Create;
ListWithColumn := TList<String>.Create;
end;
procedure TSKStringGrid.CstDrawColumnCell(Sender: TObject; const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF; const Row: Integer; const Value: TValue; const State: TGridDrawStates);
Var
aRowColor : TBrush;
aNewRectF : TRectF;
begin
aRowColor := TBrush.Create(TBrushKind.Solid, TAlphaColors.Alpha);
if Selected = Row then
aRowColor.Color := TAlphaColor(RowSelectedColor)
else if Column.Index = 0 then // Sıra No
aRowColor.Color := TAlphaColor(ColumnSiraColor)
else if Odd(Row) then
aRowColor.Color := TAlphaColor(RowZebraColor)
else
aRowColor.Color := TAlphaColors.White;
aNewRectF := Bounds;
aNewRectF.Inflate(3, 3);
Canvas.FillRect(aNewRectF, 0, 0, [], 1, aRowColor);
// Column.DefaultDrawCell(Canvas, Bounds, Row, Value, State);
Canvas.Font.Size := RowFontSize;
if Selected = Row then
Canvas.Fill.Color := TAlphaColorRec.White
else
Canvas.Fill.Color := TAlphaColorRec.Black;
case GetSKStringGridColumnType(Column.Index) of
SKColumnString :
begin
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Leading);
end;
SKColumnFloat :
begin
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Trailing);
end;
SKColumnInteger :
begin
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Trailing);
end;
SKColumnDate :
begin
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Leading);
end;
SKColumnDateTime :
begin
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Leading);
end;
SKColumnNumber :
begin
Canvas.Fill.Color := TAlphaColorRec.White;
Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Center);
end;
end;
aRowColor.Free;
end;
procedure TSKStringGrid.CstDrawColumnHeader(Sender: TObject; const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF);
Var
aNewRectF : TRectF;
begin
Canvas.Fill.Color := TAlphaColor(ColumnHeaderColor);
aNewRectF := Bounds;
aNewRectF.Inflate(-0.5, 3);
Canvas.FillRect(aNewRectF, 0, 0, [], 1);
Canvas.Font.Size := HeaderFontSize;
Canvas.Fill.Color := TAlphaColorRec.White;
aNewRectF.Inflate(-3, 3);
case GetSKStringGridColumnType(Column.Index) of
SKColumnString :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Leading);
end;
SKColumnFloat :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Trailing);
end;
SKColumnInteger :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Trailing);
end;
SKColumnDate :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Leading);
end;
SKColumnDateTime :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Leading);
end;
SKColumnNumber :
begin
Canvas.FillText(aNewRectF, Column.Header , False, 1, [] , TTextAlign.Center);
end;
end;
end;
destructor TSKStringGrid.Destroy;
begin
ListWithColumn.Free;
ListMaxWithColumn.Free;
inherited;
end;
function TSKStringGrid.GetColumnAsDate(const AColumnName: String): TDate;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
if Columns[Ind].Name = 'Clmn' + AColumnName then
Exit(StrToDate(Cells[Columns[Ind].Index,Selected]));
Result := now;
end;
function TSKStringGrid.GetColumnAsDateTime(const AColumnName: String): TDateTime;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
if Columns[Ind].Name = 'Clmn' + AColumnName then
Exit(StrToDateTime(Cells[Columns[Ind].Index,Selected]));
Result := now;
end;
function TSKStringGrid.GetColumnAsFloat(const AColumnName: String): Double;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
if Columns[Ind].Name = 'Clmn' + AColumnName then
Exit(StrToFloat(Cells[Columns[Ind].Index,Selected]));
Result := -1;
end;
function TSKStringGrid.GetColumnAsInteger(const AColumnName: String): Integer;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
if Columns[Ind].Name = 'Clmn' + AColumnName then
Exit(StrToInt(Cells[Columns[Ind].Index,Selected]));
Result := -1;
end;
function TSKStringGrid.GetColumnAsString(const AColumnName: String): String;
Var
Ind : Integer;
begin
for Ind := 0 to Pred(ColumnCount) do
if Columns[Ind].Name = 'Clmn' + AColumnName then
Exit(Cells[Columns[Ind].Index,Selected]);
Result := '';
end;
function TSKStringGrid.GetSKStringGridColumnType(const AColumnIndex:Integer): TSKStringGridColumnType;
begin
Result := SKColumnString;
if Columns[AColumnIndex] is TSKStringColumn then
Result := TSKStringColumn(Columns[AColumnIndex]).SKStringGridColumnType
else if Columns[AColumnIndex] is TSKFloatColumn then
Result := TSKFloatColumn(Columns[AColumnIndex]).SKStringGridColumnType
else if Columns[AColumnIndex] is TSKIntegerColumn then
Result := TSKIntegerColumn(Columns[AColumnIndex]).SKStringGridColumnType
else if Columns[AColumnIndex] is TSKDateColumn then
Result := TSKDateColumn(Columns[AColumnIndex]).SKStringGridColumnType
else if Columns[AColumnIndex] is TSKDateTimeColumn then
Result := TSKDateTimeColumn(Columns[AColumnIndex]).SKStringGridColumnType
else if Columns[AColumnIndex] is TSKNumberColumn then
Result := TSKNumberColumn(Columns[AColumnIndex]).SKStringGridColumnType;
end;
procedure TSKStringGrid.LoadFromDataset(ADataset: TDataSet);
Var
Ind : Integer;
xRow : Integer;
xColumnIndex : Integer;
xStringColumn : TSKStringColumn;
xFloatColumn : TSKFloatColumn;
xIntegerColumn : TSKIntegerColumn;
xDateColumn : TSKDateColumn;
xDateTimeColumn : TSKDateTimeColumn;
xSKNumberColumn : TSKNumberColumn;
xNotLoadList : TStringList;
procedure ColumnOrtakProperty(AFields:TField;AColumn:TColumn);
begin
if (AFields = nil) and (AColumn is TSKNumberColumn) then
begin
AColumn.Header := 'Sıra No';
AColumn.Name := 'SIRA_NO';
end
else
begin
AColumn.Header := AFields.FieldName;
AColumn.Name := 'Clmn' + AFields.FieldName;
AFields.Tag := AColumn.Index;
end;
ListWithColumn.Add(AColumn.Name);
AColumn.Canvas.Font.Size := HeaderFontSize;
ListMaxWithColumn.Add(AColumn.Canvas.TextWidth(AColumn.Header));
end;
begin
ListMaxWithColumn.Clear;
RowCount := 0;
if (not ADataset.Active) or (ADataset.RecordCount = 0) then
Exit;
xNotLoadList := TStringList.Create;
xNotLoadList.BeginUpdate;
xNotLoadList.Text := NotLoadFields;
xNotLoadList.Text := StringReplace(xNotLoadList.Text,',',sLineBreak,[rfReplaceAll]);
BeginUpdate;
{$REGION 'Column Create'}
{$REGION 'Sıra Numarası'}
xSKNumberColumn := TSKNumberColumn.Create(Self);
xSKNumberColumn.BeginUpdate;
AddObject(xSKNumberColumn);
ColumnOrtakProperty(nil,xSKNumberColumn);
xSKNumberColumn.EndUpdate;
{$ENDREGION}
for Ind := 0 to Pred(ADataset.FieldCount) do
begin
if xNotLoadList.IndexOf(ADataset.Fields[Ind].FieldName) <> -1 then
Continue;
ADataset.Fields[Ind].Tag := -1;
case ADataset.Fields[Ind].DataType of
ftFloat,ftBCD,ftCurrency :
begin
xFloatColumn := TSKFloatColumn.Create(Self);
xFloatColumn.BeginUpdate;
AddObject(xFloatColumn);
ColumnOrtakProperty(ADataset.Fields[Ind],xFloatColumn);
xFloatColumn.EndUpdate;
end;
ftString,ftWideString:
begin
xStringColumn := TSKStringColumn.Create(Self);
xStringColumn.BeginUpdate;
AddObject(xStringColumn);
ColumnOrtakProperty(ADataset.Fields[Ind],xStringColumn);
xStringColumn.EndUpdate;
end;
ftInteger,ftSmallint,ftWord,ftAutoInc,ftLargeint :
begin
xIntegerColumn := TSKIntegerColumn.Create(Self);
xIntegerColumn.BeginUpdate;
AddObject(xIntegerColumn);
ColumnOrtakProperty(ADataset.Fields[Ind],xIntegerColumn);
xIntegerColumn.EndUpdate;
end;
ftDate :
begin
xDateColumn := TSKDateColumn.Create(Self);
xDateColumn.BeginUpdate;
AddObject(xDateColumn);
ColumnOrtakProperty(ADataset.Fields[Ind],xDateColumn);
xDateColumn.EndUpdate;
end;
ftDateTime :
begin
xDateTimeColumn := TSKDateTimeColumn.Create(Self);
xDateTimeColumn.BeginUpdate;
AddObject(xDateTimeColumn);
ColumnOrtakProperty(ADataset.Fields[Ind],xDateTimeColumn);
xDateTimeColumn.EndUpdate;
end;
end;
end;
{$ENDREGION}
{$REGION 'Load Data'}
xRow := 0;
RowCount := ADataset.RecordCount;
ADataset.First;
while not ADataset.Eof do
begin
Cells[0,xRow] := IntToStr(Succ(xRow)); // Sıra No
for Ind := 0 to Pred(ADataset.FieldCount) do
begin
xColumnIndex := ADataset.Fields[Ind].Tag;
if xColumnIndex <> -1 then // Kolon oluşturulmuşsa
begin
case GetSKStringGridColumnType(xColumnIndex) of
SKColumnString :
begin
Cells[xColumnIndex,xRow] := ADataset.Fields[Ind].AsString;
end;
SKColumnFloat :
begin
Cells[xColumnIndex,xRow] := Format('%2f',[ADataset.Fields[Ind].AsFloat]);
end;
SKColumnInteger :
begin
Cells[xColumnIndex,xRow] := IntToStr(ADataset.Fields[Ind].AsInteger);
end;
SKColumnDate :
begin
Cells[xColumnIndex,xRow] := FormatDateTime('dd.mm.yyyy',ADataset.Fields[Ind].AsDateTime);
end;
SKColumnDateTime :
begin
Cells[xColumnIndex,xRow] := FormatDateTime('dd.mm.yyyy hh:nn',ADataset.Fields[Ind].AsDateTime);
end;
end;
Columns[xColumnIndex].Canvas.Font.Size := RowFontSize;
if Columns[xColumnIndex].Canvas.TextWidth(Cells[xColumnIndex,xRow]) > ListMaxWithColumn[ListWithColumn.IndexOf(Columns[xColumnIndex].Name)] then
ListMaxWithColumn[ListWithColumn.IndexOf(Columns[xColumnIndex].Name)] := Columns[xColumnIndex].Canvas.TextWidth(Cells[xColumnIndex,xRow]);
end;
end;
Inc(xRow);
ADataset.Next;
end;
{$ENDREGION}
EndUpdate;
xNotLoadList.EndUpdate;
xNotLoadList.Free;
end;
{ TSKStringColumn }
constructor TSKStringColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnString;
end;
destructor TSKStringColumn.Destroy;
begin
inherited;
end;
{ TSKFloatColumn }
constructor TSKFloatColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnFloat;
end;
destructor TSKFloatColumn.Destroy;
begin
inherited;
end;
{ TSKIntegerColumn }
constructor TSKIntegerColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnInteger;
end;
destructor TSKIntegerColumn.Destroy;
begin
inherited;
end;
{ TSKNumberColumn }
constructor TSKNumberColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnNumber;
end;
destructor TSKNumberColumn.Destroy;
begin
inherited;
end;
{ TSKDateColumn }
constructor TSKDateColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnDate;
end;
destructor TSKDateColumn.Destroy;
begin
inherited;
end;
{ TSKDateTimeColumn }
constructor TSKDateTimeColumn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SKStringGridColumnType := SKColumnDateTime;
end;
destructor TSKDateTimeColumn.Destroy;
begin
inherited;
end;
initialization
HeaderFontSize := 10;
RowFontSize := 10;
with RowZebraColor do
begin
A := 255;
R := 173;
G := 203;
B := 227;
end;
with ColumnHeaderColor do
begin
A := 255;
R := 42;
G := 77;
B := 105;
end;
with ColumnSiraColor do
begin
A := 255;
R := 75;
G := 134;
B := 180;
end;
with RowSelectedColor do
begin
A := 255;
R := 101;
G := 115;
B := 126;
end;
TPresentationProxyFactory.Current.Register(TSKStringGrid, TControlType.Styled, TStyledPresentationProxy<TStyledGrid>);
finalization
TPresentationProxyFactory.Current.Unregister(TSKStringGrid, TControlType.Styled, TStyledPresentationProxy<TStyledGrid>);
end.
Var
xGrd : TSKStringGrid;
begin
xGrd := TSKStringGrid.Create(Self);
xGrd.Parent := Self;
xGrd.Align := TAlignLayout.Client;
xGrd.LoadFromDataset(UniQuery1);
xGrd.SQLFieldNameToCaption;
xGrd.AutoSizeColumn;
end;
|