tvplanit: Refactor ical and vcard import forms to reduce duplicate code.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8397 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-08-19 13:57:51 +00:00
parent 33b73bbc8b
commit 259d689c42
12 changed files with 120 additions and 202 deletions

View File

@ -6,6 +6,7 @@ object VpImportPreviewForm: TVpImportPreviewForm
Caption = 'VpImportPreviewForm' Caption = 'VpImportPreviewForm'
ClientHeight = 295 ClientHeight = 295
ClientWidth = 603 ClientWidth = 603
LCLVersion = '2.3.0.0'
object Grid: TDrawGrid object Grid: TDrawGrid
Left = 0 Left = 0
Height = 258 Height = 258
@ -13,7 +14,7 @@ object VpImportPreviewForm: TVpImportPreviewForm
Width = 603 Width = 603
Align = alClient Align = alClient
AutoFillColumns = True AutoFillColumns = True
ColCount = 2 ColCount = 3
Columns = < Columns = <
item item
Alignment = taCenter Alignment = taCenter
@ -24,8 +25,14 @@ object VpImportPreviewForm: TVpImportPreviewForm
end end
item item
ReadOnly = True ReadOnly = True
Title.Caption = 'Items' Title.Caption = 'Title'
Width = 566 Width = 410
end
item
ButtonStyle = cbsPickList
SizePriority = 0
Title.Caption = 'Title'
Width = 160
end> end>
ExtendedSelect = False ExtendedSelect = False
FixedCols = 0 FixedCols = 0
@ -37,7 +44,8 @@ object VpImportPreviewForm: TVpImportPreviewForm
OnSetCheckboxState = GridSetCheckboxState OnSetCheckboxState = GridSetCheckboxState
ColWidths = ( ColWidths = (
33 33
566 410
160
) )
end end
object ButtonPanel: TPanel object ButtonPanel: TPanel

View File

@ -1,7 +1,8 @@
{"version":1,"strings":[ {"version":1,"strings":[
{"hash":185343357,"name":"tvpimportpreviewform.caption","sourcebytes":[86,112,73,109,112,111,114,116,80,114,101,118,105,101,119,70,111,114,109],"value":"VpImportPreviewForm"}, {"hash":185343357,"name":"tvpimportpreviewform.caption","sourcebytes":[86,112,73,109,112,111,114,116,80,114,101,118,105,101,119,70,111,114,109],"value":"VpImportPreviewForm"},
{"hash":4294967295,"name":"tvpimportpreviewform.grid.columns[0].title.caption","sourcebytes":[],"value":""}, {"hash":4294967295,"name":"tvpimportpreviewform.grid.columns[0].title.caption","sourcebytes":[],"value":""},
{"hash":5286979,"name":"tvpimportpreviewform.grid.columns[1].title.caption","sourcebytes":[73,116,101,109,115],"value":"Items"}, {"hash":5966629,"name":"tvpimportpreviewform.grid.columns[1].title.caption","sourcebytes":[84,105,116,108,101],"value":"Title"},
{"hash":5966629,"name":"tvpimportpreviewform.grid.columns[2].title.caption","sourcebytes":[84,105,116,108,101],"value":"Title"},
{"hash":202226323,"name":"tvpimportpreviewform.btnexecute.caption","sourcebytes":[73,109,112,111,114,116,32,99,104,101,99,107,101,100,32,105,116,101,109,115],"value":"Import checked items"}, {"hash":202226323,"name":"tvpimportpreviewform.btnexecute.caption","sourcebytes":[73,109,112,111,114,116,32,99,104,101,99,107,101,100,32,105,116,101,109,115],"value":"Import checked items"},
{"hash":77089212,"name":"tvpimportpreviewform.btncancel.caption","sourcebytes":[67,97,110,99,101,108],"value":"Cancel"} {"hash":77089212,"name":"tvpimportpreviewform.btncancel.caption","sourcebytes":[67,97,110,99,101,108],"value":"Cancel"}
]} ]}

View File

@ -6,7 +6,7 @@ interface
uses uses
Classes, SysUtils, Graphics, Types, Classes, SysUtils, Graphics, Types,
Forms, Controls, Dialogs, Grids, ExtCtrls, StdCtrls; Forms, Controls, Dialogs, Grids, ExtCtrls, StdCtrls, VpBaseDS;
type type
{ TVpImportPreviewForm } { TVpImportPreviewForm }
@ -25,7 +25,9 @@ type
procedure GridSetCheckboxState(Sender: TObject; {%H-}ACol, ARow: Integer; procedure GridSetCheckboxState(Sender: TObject; {%H-}ACol, ARow: Integer;
const Value: TCheckboxState); const Value: TCheckboxState);
private private
FDatastore: TVpCustomDatastore;
procedure SetDatastore(const AValue: TVpCustomDatastore);
protected protected
FItems: TFPList; FItems: TFPList;
procedure CalcRowHeights; procedure CalcRowHeights;
@ -38,6 +40,7 @@ type
procedure CheckItem({%H-}ARow: Integer; {%H-}AChecked: Boolean); virtual; procedure CheckItem({%H-}ARow: Integer; {%H-}AChecked: Boolean); virtual;
function Execute: Boolean; function Execute: Boolean;
function IsChecked({%H-}ARow: Integer): Boolean; virtual; function IsChecked({%H-}ARow: Integer): Boolean; virtual;
property Datastore: TVpCustomDatastore read FDatastore write SetDatastore;
end; end;
@ -176,5 +179,14 @@ begin
CalcRowHeights; CalcRowHeights;
end; end;
procedure TVpImportPreviewForm.SetDatastore(const AValue: TVpCustomDatastore);
begin
if AValue <> FDatastore then
begin
FDatastore := AValue;
PrepareItems;
end;
end;
end. end.

View File

@ -4,31 +4,6 @@ inherited VpImportPreviewICalEventForm: TVpImportPreviewICalEventForm
Caption = '' Caption = ''
ClientHeight = 400 ClientHeight = 400
ClientWidth = 667 ClientWidth = 667
inherited Grid: TDrawGrid
Height = 363
Width = 667
Columns = <
item
Alignment = taCenter
ButtonStyle = cbsCheckboxColumn
PickList.Strings = ( )
SizePriority = 0
Title.Caption = ''
Width = 33
end
item
PickList.Strings = ( )
ReadOnly = True
Title.Caption = 'Items'
Width = 634
end>
OnGetEditText = GridGetEditText
OnSetEditText = GridSetEditText
ColWidths = (
33
634
)
end
inherited ButtonPanel: TPanel inherited ButtonPanel: TPanel
Top = 369 Top = 369
Width = 655 Width = 655

View File

@ -1,5 +1,3 @@
{"version":1,"strings":[ {"version":1,"strings":[
{"hash":4294967295,"name":"tvpimportpreviewicaleventform.caption","sourcebytes":[],"value":""}, {"hash":4294967295,"name":"tvpimportpreviewicaleventform.caption","sourcebytes":[],"value":""}
{"hash":4294967295,"name":"tvpimportpreviewicaleventform.grid.columns[0].title.caption","sourcebytes":[],"value":""},
{"hash":5286979,"name":"tvpimportpreviewicaleventform.grid.columns[1].title.caption","sourcebytes":[73,116,101,109,115],"value":"Items"}
]} ]}

View File

@ -19,7 +19,6 @@ type
const Value: string); const Value: string);
private private
FCalendar: TVpICalendar; FCalendar: TVpICalendar;
FDatastore: TVpCustomDatastore;
FDefaultCategory: String; FDefaultCategory: String;
FTimeFormat: String; FTimeFormat: String;
function GetEventText(AEvent: TVpICalEvent): String; function GetEventText(AEvent: TVpICalEvent): String;
@ -35,7 +34,6 @@ type
procedure CheckItem(ARow: Integer; AChecked: Boolean); override; procedure CheckItem(ARow: Integer; AChecked: Boolean); override;
function IsChecked(ARow: Integer): Boolean; override; function IsChecked(ARow: Integer): Boolean; override;
property Calendar: TVpICalendar read FCalendar write SetCalendar; property Calendar: TVpICalendar read FCalendar write SetCalendar;
property Datastore: TVpCustomDatastore read FDatastore write FDatastore;
property DefaultCategory: String read FDefaultCategory write FDefaultCategory; property DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end; end;
@ -52,6 +50,10 @@ uses
constructor TVPImportPreviewICalEventForm.Create(AOwner: TComponent); constructor TVPImportPreviewICalEventForm.Create(AOwner: TComponent);
begin begin
inherited; inherited;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
Caption := RSImportICalEvent; Caption := RSImportICalEvent;
FTimeFormat := 'c'; // short date + long time format FTimeFormat := 'c'; // short date + long time format
end; end;
@ -114,7 +116,7 @@ begin
RSDescriptionLbl + ' ' + AEvent.Summary; RSDescriptionLbl + ' ' + AEvent.Summary;
// Categories // Categories
if Assigned(FDatastore) then if Assigned(Datastore) then
begin begin
cat := AEvent.Categories.CommaText; cat := AEvent.Categories.CommaText;
if cat = '' then cat := RSNoneStr; if cat = '' then cat := RSNoneStr;
@ -216,45 +218,37 @@ var
event: TVpICalEvent; event: TVpICalEvent;
cat: String; cat: String;
begin begin
Grid.Columns[1].Title.Caption := RSEventItems;
Grid.Columns[2].Title.Caption := RSAssignedCategory;
// Populate picklist in column 2
L := TStringList.Create;
try
for i := 0 to 9 do
L.Add(Datastore.CategoryColorMap.GetName(i));
Grid.Columns[2].PickList.Assign(L);
finally
L.Free;
end;
FItems.Clear; FItems.Clear;
if FCalendar <> nil then if (FCalendar <> nil) and (Datastore <> nil) then
for i := 0 to FCalendar.Count-1 do for i := 0 to FCalendar.Count-1 do
if (FCalendar.Entry[i] is TVpICalEvent) then if (FCalendar.Entry[i] is TVpICalEvent) then
FItems.Add(FCalendar.Entry[i]); begin
// Add ical event
event := TVpIcalEvent(FCalendar.Entry[i]);
FItems.Add(event);
// Select best category in picklist column
cat := Datastore.FindBestEventCategory(event.Categories);
if cat = '' then cat := FDefaultCategory;
if cat <> '' then
event.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else
event.PickedCategory := 0;
end;
inherited; inherited;
if (FCalendar <> nil) and (FDataStore <> nil) and (Grid.Columns.Count = 2) then
begin
Grid.Columns[1].Title.Caption := RSEventItems;
with Grid.Columns.Add do
begin
SizePriority := 0;
Width := 160;
Title.Caption := RSAssignedCategory;
ButtonStyle := cbsPickList;
L := TStringList.Create;
try
for i := 0 to 9 do
L.Add(FDatastore.CategoryColorMap.GetName(i));
PickList.Assign(L);
finally
L.Free;
end;
end;
for i := 0 to FItems.Count-1 do
begin
event := TVpICalEvent(FItems[i]);
cat := FDatastore.FindBestEventCategory(event.Categories);
if cat = '' then cat := FDefaultCategory;
if cat <> '' then
event.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else
event.PickedCategory := 0;
end;
end;
end; end;
procedure TVpImportPreviewICalEventForm.SetCalendar(const AValue: TVpICalendar); procedure TVpImportPreviewICalEventForm.SetCalendar(const AValue: TVpICalendar);

View File

@ -4,31 +4,6 @@ inherited VpImportPreviewICalTaskForm: TVpImportPreviewICalTaskForm
Caption = '' Caption = ''
ClientHeight = 400 ClientHeight = 400
ClientWidth = 667 ClientWidth = 667
inherited Grid: TDrawGrid
Height = 363
Width = 667
Columns = <
item
Alignment = taCenter
ButtonStyle = cbsCheckboxColumn
PickList.Strings = ( )
SizePriority = 0
Title.Caption = ''
Width = 33
end
item
PickList.Strings = ( )
ReadOnly = True
Title.Caption = 'Items'
Width = 634
end>
OnGetEditText = GridGetEditText
OnSetEditText = GridSetEditText
ColWidths = (
33
634
)
end
inherited ButtonPanel: TPanel inherited ButtonPanel: TPanel
Top = 369 Top = 369
Width = 655 Width = 655

View File

@ -1,5 +1,3 @@
{"version":1,"strings":[ {"version":1,"strings":[
{"hash":4294967295,"name":"tvpimportpreviewicaltaskform.caption","sourcebytes":[],"value":""}, {"hash":4294967295,"name":"tvpimportpreviewicaltaskform.caption","sourcebytes":[],"value":""}
{"hash":4294967295,"name":"tvpimportpreviewicaltaskform.grid.columns[0].title.caption","sourcebytes":[],"value":""},
{"hash":5286979,"name":"tvpimportpreviewicaltaskform.grid.columns[1].title.caption","sourcebytes":[73,116,101,109,115],"value":"Items"}
]} ]}

View File

@ -19,7 +19,6 @@ type
const Value: string); const Value: string);
private private
FCalendar: TVpICalendar; FCalendar: TVpICalendar;
FDatastore: TVpCustomDatastore;
FDefaultCategory: String; FDefaultCategory: String;
FTimeFormat: String; FTimeFormat: String;
function GetTaskText(AToDo: TVpICalToDo): String; function GetTaskText(AToDo: TVpICalToDo): String;
@ -34,7 +33,6 @@ type
procedure CheckItem(ARow: Integer; AChecked: Boolean); override; procedure CheckItem(ARow: Integer; AChecked: Boolean); override;
function IsChecked(ARow: Integer): Boolean; override; function IsChecked(ARow: Integer): Boolean; override;
property Calendar: TVpICalendar read FCalendar write SetCalendar; property Calendar: TVpICalendar read FCalendar write SetCalendar;
property Datastore: TVpCustomDatastore read FDatastore write FDatastore;
property DefaultCategory: String read FDefaultCategory write FDefaultCategory; property DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end; end;
@ -52,6 +50,10 @@ uses
constructor TVPImportPreviewICalTaskForm.Create(AOwner: TComponent); constructor TVPImportPreviewICalTaskForm.Create(AOwner: TComponent);
begin begin
inherited; inherited;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
Caption := RSImportICalTask; Caption := RSImportICalTask;
FTimeFormat := 'c'; // short date + long time format FTimeFormat := 'c'; // short date + long time format
end; end;
@ -149,51 +151,43 @@ end;
procedure TVpImportPreviewICalTaskForm.PrepareItems; procedure TVpImportPreviewICalTaskForm.PrepareItems;
var var
i: Integer; i, j: Integer;
ct: TVpCategoryType; ct: TVpCategoryType;
cat: String; cat: String;
L: TStrings; L: TStrings;
task: TVpICalToDo; task: TVpICalToDo;
begin begin
FItems.Clear; Grid.Columns[1].Title.Caption := RSTaskItems;
if FCalendar <> nil then Grid.Columns[2].Title.Caption := RSAssignedCategory;
for i := 0 to FCalendar.Count-1 do // Populate picklist in column 2
if (FCalendar.Entry[i] is TVpICalToDo) then L := TStringList.Create;
FItems.Add(FCalendar.Entry[i]); try
for ct in TVpCategoryType do
inherited; L.Add(CategoryLabel(ct));
Grid.Columns[2].PickList.Assign(L);
if (FCalendar <> nil) and (FDataStore <> nil) and (Grid.Columns.Count = 2) then finally
begin L.Free;
Grid.Columns[1].Title.Caption := RSTaskItems;
with Grid.Columns.Add do
begin
SizePriority := 0;
Width := 160;
Title.Caption := RSAssignedCategory;
ButtonStyle := cbsPickList;
L := TStringList.Create;
try
for ct in TVpCategoryType do
L.Add(CategoryLabel(ct));
PickList.Assign(L);
finally
L.Free;
end;
end;
for i := 0 to FItems.Count-1 do
begin
task := TVpICalToDo(FItems[i]);
cat := FDatastore.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;
end; 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; end;
procedure TVpImportPreviewICalTaskForm.SetCalendar(const AValue: TVpICalendar); procedure TVpImportPreviewICalTaskForm.SetCalendar(const AValue: TVpICalendar);

View File

@ -4,31 +4,6 @@ inherited VpImportPreviewVCardForm: TVpImportPreviewVCardForm
Caption = '' Caption = ''
ClientHeight = 400 ClientHeight = 400
ClientWidth = 667 ClientWidth = 667
inherited Grid: TDrawGrid
Height = 363
Width = 667
Columns = <
item
Alignment = taCenter
ButtonStyle = cbsCheckboxColumn
PickList.Strings = ( )
SizePriority = 0
Title.Caption = ''
Width = 33
end
item
PickList.Strings = ( )
ReadOnly = True
Title.Caption = 'Items'
Width = 630
end>
OnGetEditText = GridGetEditText
OnSetEditText = GridSetEditText
ColWidths = (
33
630
)
end
inherited ButtonPanel: TPanel inherited ButtonPanel: TPanel
Top = 369 Top = 369
Width = 655 Width = 655

View File

@ -1,5 +1,3 @@
{"version":1,"strings":[ {"version":1,"strings":[
{"hash":4294967295,"name":"tvpimportpreviewvcardform.caption","sourcebytes":[],"value":""}, {"hash":4294967295,"name":"tvpimportpreviewvcardform.caption","sourcebytes":[],"value":""}
{"hash":4294967295,"name":"tvpimportpreviewvcardform.grid.columns[0].title.caption","sourcebytes":[],"value":""},
{"hash":5286979,"name":"tvpimportpreviewvcardform.grid.columns[1].title.caption","sourcebytes":[73,116,101,109,115],"value":"Items"}
]} ]}

View File

@ -19,7 +19,6 @@ type
const Value: string); const Value: string);
private private
FVCards: TVpVCards; FVCards: TVpVCards;
FDatastore: TVpCustomDatastore;
FDefaultCategory: String; FDefaultCategory: String;
function GetCardText(ACard: TVpVCard): String; function GetCardText(ACard: TVpVCard): String;
procedure SetVCards(const AValue: TVpVCards); procedure SetVCards(const AValue: TVpVCards);
@ -33,7 +32,6 @@ type
procedure CheckItem(ARow: Integer; AChecked: Boolean); override; procedure CheckItem(ARow: Integer; AChecked: Boolean); override;
function IsChecked(ARow: Integer): Boolean; override; function IsChecked(ARow: Integer): Boolean; override;
property VCards: TVpVCards read FVCards write SetVCards; property VCards: TVpVCards read FVCards write SetVCards;
property Datastore: TVpCustomDatastore read FDatastore write FDatastore;
property DefaultCategory: String read FDefaultCategory write FDefaultCategory; property DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end; end;
@ -51,6 +49,8 @@ constructor TVPImportPreviewVCardForm.Create(AOwner: TComponent);
begin begin
inherited; inherited;
Caption := RSImportVCard; Caption := RSImportVCard;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
end; end;
procedure TVpImportPreviewVCardForm.CheckItem(ARow: Integer; AChecked: Boolean); procedure TVpImportPreviewVCardForm.CheckItem(ARow: Integer; AChecked: Boolean);
@ -146,47 +146,37 @@ var
ct: TVpCategoryType; ct: TVpCategoryType;
L: TStrings; L: TStrings;
begin begin
FItems.Clear;
if FVCards <> nil then
for i := 0 to FVCards.Count-1 do
FItems.Add(FVCards.Card[i]);
inherited;
Grid.Columns[1].Title.Caption := RSContactItems; Grid.Columns[1].Title.Caption := RSContactItems;
Grid.Columns[2].Title.Caption := RSAssignedCategory;
if (FVCards <> nil) and (FDataStore <> nil) and (Grid.Columns.Count = 2) then // Populate picklist in column 2
begin L := TStringList.Create;
Grid.Columns[1].Title.Caption := RSContactItems; try
for ct in TVpCategoryType do
L.Add(CategoryLabel(ct));
Grid.Columns[2].PickList.Assign(L);
finally
L.Free;
end;
with Grid.Columns.Add do FItems.Clear;
if (FVCards <> nil) and (Datastore <> nil) then
for i := 0 to FVCards.Count-1 do
begin begin
SizePriority := 0; // Add vcard
Width := 160; card := FVCards.Card[i];
Title.Caption := RSAssignedCategory; FItems.Add(card);
ButtonStyle := cbsPickList;
L := TStringList.Create;
try
for ct in TVpCategoryType do
L.Add(CategoryLabel(ct));
PickList.Assign(L);
finally
L.Free;
end;
end;
for i := 0 to FItems.Count-1 do // Select best category in picklist columns
begin cat := Datastore.FindBestCategory(card.Categories);
card := TVpVCard(FItems[i]);
cat := FDatastore.FindBestCategory(card.Categories);
if cat = '' then cat := FDefaultCategory; if cat = '' then cat := FDefaultCategory;
if cat <> '' then if cat <> '' then
card.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat) card.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else else
card.PickedCategory := ord(ctOther); card.PickedCategory := ord(ctOther);
end; end;
end;
inherited;
end; end;
procedure TVpImportPreviewVCardForm.SetVCards(const AValue: TVpVCards); procedure TVpImportPreviewVCardForm.SetVCards(const AValue: TVpVCards);