Files
lazarus-ccr/components/tvplanit/source/vpimportpreview_icaltask.pas

207 lines
5.2 KiB
ObjectPascal

unit VpImportPreview_ICalTask;
{$mode objfpc}{$H+}
{$WARN 5024 off : Parameter "$1" not used}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
VpData, VpBaseDS, VpImportPreview, VpICal, Grids;
type
{ TVpImportPreviewICalTaskForm }
TVpImportPreviewICalTaskForm = class(TVpImportPreviewForm)
procedure GridGetEditText(Sender: TObject; {%H-}ACol, {%H-}ARow: Integer;
var Value: string);
procedure GridSetEditText(Sender: TObject; {%H-}ACol, ARow: Integer;
const Value: string);
private
FCalendar: TVpICalendar;
FDefaultCategory: String;
FTimeFormat: String;
function GetTaskText(AToDo: TVpICalToDo): String;
procedure SetCalendar(const AValue: TVpICalendar);
protected
function GetCellText(ACol, ARow: Integer): String; override;
procedure PrepareItems; override;
public
constructor Create(AOwner: TComponent); override;
procedure CheckItem(ARow: Integer; AChecked: Boolean); override;
function IsChecked(ARow: Integer): Boolean; override;
property Calendar: TVpICalendar read FCalendar write SetCalendar;
property DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end;
var
VpImportPreviewICalTaskForm: TVpImportPreviewICalTaskForm;
implementation
{$R *.lfm}
uses
VpSR, VpConst;
constructor TVPImportPreviewICalTaskForm.Create(AOwner: TComponent);
begin
inherited;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
Caption := RSImportICalTask;
FTimeFormat := 'c'; // short date + long time format
end;
procedure TVpImportPreviewICalTaskForm.CheckItem(ARow: Integer; AChecked: Boolean);
var
item: TVpICalEntry;
begin
if ARow < Grid.FixedRows then
exit;
item := TVpICalEntry(FItems[ARow - Grid.FixedRows]);
if item <> nil then
begin
item.Checked := AChecked;
inherited;
end;
end;
function TVpImportPreviewICalTaskForm.GetCellText(ACol, ARow: Integer): String;
var
task: TVpICalToDo;
begin
Result := '';
if (ARow >= Grid.FixedRows) then
begin
task := TVpICalToDo(FItems[ARow - Grid.FixedRows]);
case ACol of
1: Result := GetTaskText(task);
2: Result := Grid.Columns[2].PickList[task.PickedCategory]
end;
end;
end;
procedure TVpImportPreviewICalTaskForm.GridGetEditText(Sender: TObject; ACol,
ARow: Integer; var Value: string);
var
task: TVpICalToDo;
begin
task := TVpICalToDo(FItems[Grid.Row - Grid.FixedRows]);
if task <> nil then
Value := Grid.Columns[2].PickList[task.PickedCategory];
end;
procedure TVpImportPreviewICalTaskForm.GridSetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: string);
var
task: TVpICalToDo;
begin
task:= TVpICalToDo(FItems[Grid.Row - Grid.FixedRows]);
if task <> nil then
task.PickedCategory := Grid.Columns[2].PickList.IndexOf(Value);;
end;
function TVpImportPreviewICalTaskForm.GetTaskText(AToDo: TVpICalToDo): String;
var
s: String;
dt: TDateTime;
begin
Result := RSDescriptionLbl + ' ' + AToDo.Summary;
dt := AToDo.DueTime[false];
if dt <> NO_DATE then
s := FormatDateTime(FTimeFormat, dt)
else
s := RSNoneStr;
Result := Result + LineEnding + RSDueDateLabel + ' ' + s;
dt := AToDo.StartTime[false];
if dt <> NO_DATE then
s := FormatDateTime(FTimeFormat, dt)
else
s := RSNoneStr;
Result := Result + LineEnding + RSCreatedOn + ' ' + s;
case TVpTask.GetTaskPriority(AToDo.Priority) of
tpLow: s := RSLow;
tpNormal: s := RSNormal;
tpHigh: s := RSHigh;
end;
Result := Result + LineEnding + RSPriorityLabel + ' ' + s;
s := AToDo.Categories.CommaText;
if s = '' then s := RSNoneStr;
Result := Result + LineEnding + RSCategoryLabel + ' ' + s;
end;
function TVpImportPreviewICalTaskForm.IsChecked(ARow: Integer): Boolean;
var
item: TVpICalEntry;
begin
Result := false;
if ARow < Grid.FixedRows then
exit;
item := TVpICalEntry(FItems[ARow - Grid.FixedRows]);
if (item <> nil) then
Result := item.Checked;
end;
procedure TVpImportPreviewICalTaskForm.PrepareItems;
var
i: Integer;
ct: TVpCategoryType;
cat: String;
L: TStrings;
task: TVpICalToDo;
begin
Grid.Columns[1].Title.Caption := RSTaskItems;
Grid.Columns[2].Title.Caption := RSAssignedCategory;
// Populate picklist in column 2
L := TStringList.Create;
try
for ct in TVpCategoryType do
L.Add(CategoryLabel(ct));
Grid.Columns[2].PickList.Assign(L);
finally
L.Free;
end;
FItems.Clear;
if (FCalendar <> nil) and (Datastore <> nil) then
for i := 0 to FCalendar.Count-1 do
if (FCalendar.Entry[i] is TVpICalToDo) then
begin
// Add ical todo item
task := TVpICalToDo(FCalendar.Entry[i]);
FItems.Add(task);
// Select best category in picklist column
cat := Datastore.FindBestCategory(task.Categories);
if cat = '' then cat := FDefaultCategory;
if cat <> '' then
task.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else
task.PickedCategory := ord(ctOther);
end;
inherited;
end;
procedure TVpImportPreviewICalTaskForm.SetCalendar(const AValue: TVpICalendar);
begin
if AValue <> FCalendar then
begin
FCalendar := AValue;
PrepareItems;
end;
end;
end.