1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Alexander Klenin
}
unit TADataPointsEditor;
{$MODE ObjFPC}{$H+}
interface
uses
ButtonPanel, Classes, ExtCtrls, Grids, Menus, SysUtils, Forms, Controls,
Graphics, Dialogs, TASources;
type
TDataPointsEditorOption = (dpeHideColorColumn, dpeHideTextColumn);
TDataPointsEditorOptions = set of TDataPointsEditorOption;
{ TDataPointsEditorForm }
TDataPointsEditorForm = class(TForm)
ButtonPanel1: TButtonPanel;
cdItemColor: TColorDialog;
miMoveDown: TMenuItem;
miMoveUp: TMenuItem;
miSeparator: TMenuItem;
miInsertRow: TMenuItem;
miDeleteRow: TMenuItem;
pmRows: TPopupMenu;
sgData: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure miDeleteRowClick(Sender: TObject);
procedure miInsertRowClick(Sender: TObject);
procedure miMoveDownClick(Sender: TObject);
procedure miMoveUpClick(Sender: TObject);
procedure OKButtonClick(Sender: TObject);
procedure pmRowsPopup(Sender: TObject);
procedure sgDataButtonClick(ASender: TObject; ACol, ARow: Integer);
procedure sgDataDrawCell(
ASender: TObject; ACol, ARow: Integer; ARect: TRect;
AState: TGridDrawState);
procedure sgDataPrepareCanvas(sender: TObject; aCol, aRow: Integer;
aState: TGridDrawState);
strict private
FCurrentRow: Integer;
FDataPoints: TStrings;
FXCount: Integer;
FYCount: Integer;
FOptions: TDataPointsEditorOptions;
procedure UpdateCmds;
function ValidData(out ACol, ARow: Integer; out AMsg: String): Boolean;
public
procedure InitData(AXCount, AYCount: Integer; ADataPoints: TStrings;
AOptions: TDataPointsEditorOptions = []);
procedure ExtractData(out AModified: Boolean);
property Options: TDatapointsEditorOptions read FOptions;
end;
function DataPointsEditor(AListChartSource: TListChartsource;
AOptions: TDataPointsEditorOptions = []): Boolean;
implementation
uses
LCLIntf, LCLType, Math, StdCtrls,
TAChartStrConsts, TAChartUtils;
{$R *.lfm}
function DataPointsEditor(AListChartSource: TListChartsource;
AOptions: TDataPointsEditorOptions = []): Boolean;
var
F: TDataPointsEditorForm;
wasSorted: Boolean;
begin
Result := false;
F := TDataPointsEditorForm.Create(Application);
try
wasSorted := AListChartSource.Sorted;
AListChartSource.Sorted := false;
F.InitData(
AListChartSource.XCount,
AListChartSource.YCount,
AListChartSource.DataPoints,
AOptions
);
if F.ShowModal = mrOK then begin
F.ExtractData(Result);
if wasSorted then AListChartSource.Sorted := true;
end;
finally
F.Free;
end;
end;
function EditText(var AText: String): Boolean;
var
F: TForm;
memo: TMemo;
begin
F := TForm.CreateNew(Application);
try
F.Caption := 'Data point text';
F.Position := poScreenCenter;
memo := TMemo.Create(F);
with memo do begin
Parent := F;
Align := alClient;
BorderSpacing.Around := 6;
Lines.Text := AText;
end;
with TButtonPanel.Create(F) do begin
Parent := F;
Align := alBottom;
BorderSpacing.Around := 6;
ShowButtons := [pbOK, pbCancel];
end;
Result := F.ShowModal = mrOK;
if Result then AText := memo.Lines.Text;
finally
F.Free;
end;
end;
{ TDataPointsEditorForm }
procedure TDataPointsEditorForm.ExtractData(out AModified: Boolean);
var
i: Integer;
s: String;
oldDataPoints: String;
begin
oldDataPoints := FDataPoints.Text;
FDataPoints.BeginUpdate;
try
FDataPoints.Clear;
for i := 1 to sgData.RowCount - 1 do begin
with sgData.Rows[i] do begin
Delimiter := '|';
StrictDelimiter := true;
s := DelimitedText;
end;
if Length(s) >= sgData.ColCount then
FDataPoints.Add(Copy(s, 2, MaxInt));
end;
finally
FDataPoints.EndUpdate;
AModified := FDataPoints.Text <> oldDataPoints;
end;
end;
procedure TDataPointsEditorForm.InitData(AXCount, AYCount: Integer;
ADataPoints: TStrings; AOptions: TDataPointsEditorOptions = []);
var
i: Integer;
w: Integer;
begin
FXCount := AXCount;
FYCount := AYCount;
FDataPoints := ADataPoints;
FOptions := AOptions;
sgData.RowCount := Max(ADataPoints.Count + 1, 2);
for i := 1 to AYCount do
with sgData.Columns.Add do begin
Assign(sgData.Columns[0]); // Columns[0] is a template column
if AYCount = 1 then
Title.Caption := 'Y'
else
Title.Caption := 'Y' + IntToStr(i);
Index := i;
end;
for i := 1 to AXCount do
with sgData.Columns.Add do begin
Assign(sgData.Columns[0]); // Columns[0] is a template column
if AXCount = 1 then
Title.Caption := 'X'
else
Title.Caption := 'X' + IntToStr(i);
Index := i;
end;
sgData.Columns.Delete(0); // remove the template column
sgData.Columns[sgData.Columns.Count-2].Visible := not (dpeHideColorColumn in FOptions);
sgData.Columns[sgData.Columns.Count-1].Visible := not (dpeHideTextColumn in FOptions);
for i := 0 to ADataPoints.Count - 1 do
Split('|' + ADataPoints[i], sgData.Rows[i + 1]);
// Adjust column widths
w := sgData.Canvas.TextWidth('$000000') + 3*varCellPadding + sgData.DefaultRowHeight;
for i := 0 to sgData.Columns.Count-2 do
sgData.Columns[i].Width := w;
sgData.Columns[sgData.Columns.Count-1].Width := 3*w;
w := sgData.ColWidths[0] + sgData.Left * 2;
for i := 0 to sgData.Columns.Count-1 do
inc(w, sgData.Columns[i].Width);
{$IFDEF WINDOWS}
Width := Min(Screen.Width, w + 1 + IfThen(sgData.BorderStyle = bsNone, 0, 3));
{$ELSE}
Width := Min(Screen.Width, w + sgData.GridLineWidth * (sgData.Columns.Count-1));
{$ENDIF}
end;
procedure TDataPointsEditorForm.miDeleteRowClick(Sender: TObject);
begin
if sgData.RowCount <= 2 then begin
sgData.Rows[1].Clear;
exit;
end;
if InRange(FCurrentRow, 1, sgData.RowCount - 1) then
sgData.DeleteRow(FCurrentRow);
end;
procedure TDataPointsEditorForm.FormCreate(Sender: TObject);
begin
Caption := desDatapointEditor;
sgData.Columns[1].Title.Caption := desColor;
sgData.Columns[2].Title.Caption := desText;
miInsertRow.Caption := desInsertRow;
miDeleteRow.Caption := desDeleteRow;
miMoveUp.Caption := desMoveUp;
miMoveDown.Caption := desMoveDown;
if IsRightToLeft then
sgData.AutoAdvance := aaLeftDown
else
sgData.AutoAdvance := aaRightDown;
end;
procedure TDataPointsEditorForm.miInsertRowClick(Sender: TObject);
begin
sgData.InsertColRow(false, FCurrentRow);
end;
procedure TDataPointsEditorForm.miMoveDownClick(Sender: TObject);
begin
if sgData.Row < sgData.RowCount-1 then
sgData.ExchangeColRow(false, sgData.Row, sgData.Row+1);
end;
procedure TDataPointsEditorForm.miMoveUpClick(Sender: TObject);
begin
if sgData.Row > 1 then
sgData.ExchangeColRow(false, sgData.Row, sgData.Row-1);
end;
procedure TDataPointsEditorForm.OKButtonClick(Sender: TObject);
var
c, r: Integer;
msg: String;
begin
if not ValidData(c, r, msg) then begin
sgData.Row := r;
sgData.Col := c;
sgData.SetFocus;
MessageDlg(msg, mtError, [mbOK], 0);
ModalResult := mrNone;
end;
end;
procedure TDataPointsEditorForm.pmRowsPopup(Sender: TObject);
begin
FCurrentRow := sgData.MouseToCell(sgData.ScreenToClient(Mouse.CursorPos)).Y;
if not InRange(FCurrentRow, 1, sgData.RowCount - 1) then
Abort;
sgData.Row := FCurrentRow;
UpdateCmds;
end;
procedure TDataPointsEditorForm.sgDataButtonClick(
ASender: TObject; ACol, ARow: Integer);
var
s: String;
begin
Unused(ASender);
if (ARow < 1) then exit;
if (ACol = FXCount + FYCount + 1) then begin
cdItemColor.Color := StrToIntDef(sgData.Cells[ACol, ARow], clRed);
if cdItemColor.Execute then
sgData.Cells[ACol, ARow] := IntToColorHex(cdItemColor.Color);
end else
if (ACol = FXCount + FYCount + 2) then begin
s := sgData.Cells[ACol, ARow];
if EditText(s) then sgData.Cells[ACol, ARow] := s;
end;
end;
procedure TDataPointsEditorForm.sgDataDrawCell(
ASender: TObject; ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
var
c: Integer;
begin
Unused(ASender, AState);
if (ARow < 1) or (ACol <> FXCount + FYCount + 1) then exit;
if not TryStrToInt(sgData.Cells[ACol, ARow], c) then exit;
sgData.Canvas.Pen.Color := clBlack;
sgData.Canvas.Brush.Color := c;
InflateRect(ARect, -varCellPadding, -varCellPadding);
ARect.Left := ARect.Right - (ARect.Bottom - ARect.Top);;
sgData.Canvas.Rectangle(ARect);
end;
procedure TDataPointsEditorForm.sgDataPrepareCanvas(sender: TObject; aCol,
aRow: Integer; aState: TGridDrawState);
var
ts: TTextStyle;
begin
Unused(aRow, aState);
if ACol = 0 then begin
ts := TStringGrid(Sender).Canvas.TextStyle;
ts.Alignment := taRightJustify;
TStringGrid(Sender).Canvas.TextStyle := ts;
end;
end;
procedure TDataPointsEditorForm.UpdateCmds;
begin
miDeleteRow.Enabled := sgData.Row > 0;
miMoveUp.Enabled := sgData.Row > 1;
miMovedown.Enabled := sgData.Row < sgData.RowCount-1;
end;
function TDataPointsEditorForm.ValidData(out ACol, ARow: Integer;
out AMsg: String): Boolean;
var
x: Double;
i: Integer;
r, c: Integer;
s: String;
begin
Result := false;
for r := 1 to sgData.RowCount-1 do begin
for c := 1 to sgData.ColCount-3 do begin
s := sgData.Cells[c, r];
if (s <> '') and not TryStrToFloat(s, x) and not TryStrToFloat(s, x, DefSeparatorSettings) then
begin
ACol := c;
ARow := r;
AMsg := desNoNumber;
exit;
end;
end;
s := sgData.Cells[sgData.ColCount - 2, r];
if (s <> '') and (s <> '?') and not TryStrToInt(s, i) then begin
ACol := sgData.ColCount - 2;
ARow := r;
AMsg := desNoInteger;
exit;
end;
end;
Result := true;
end;
end.
|