From cc260412e1d0716bfd53300e953fd1f8e128593d Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Mon, 20 Jun 2016 11:00:59 +0000 Subject: [PATCH] tvplanit: Refactor granularity-related code git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4782 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/tvplanit/source/vpdayview.pas | 52 +++----------------- components/tvplanit/source/vpmisc.pas | 62 +++++++++++++----------- components/tvplanit/source/vpprtfmt.pas | 22 ++++----- 3 files changed, 52 insertions(+), 84 deletions(-) diff --git a/components/tvplanit/source/vpdayview.pas b/components/tvplanit/source/vpdayview.pas index 036461ceb..07a857e5e 100644 --- a/components/tvplanit/source/vpdayview.pas +++ b/components/tvplanit/source/vpdayview.pas @@ -404,7 +404,7 @@ type {$IFDEF DRAGDROP} procedure DragDrop(Source: TObject; X, Y: Integer); override; {$ENDIF} - function HourToLine(const Value: TVpHours; const UseGran: TVpGranularity): Integer; +// function HourToLine(const Value: TVpHours; const UseGran: TVpGranularity): Integer; procedure Invalidate; override; procedure LinkHandler(Sender: TComponent; NotificationType: TVpNotificationType; const Value: Variant); override; @@ -1346,16 +1346,7 @@ begin Result := Result + TextMargin * 2; Result := Round(Result * Scale); - - case UseGran of - gr60Min : dvClientVArea := Result * 24; - gr30Min : dvClientVArea := Result * 48; - gr20Min : dvClientVArea := Result * 72; - gr15Min : dvClientVArea := Result * 96; - gr10Min : dvClientVArea := Result * 144; - gr06Min : dvClientVArea := Result * 240; - gr05Min : dvClientVArea := Result * 288; - end; + dvClientVArea := Result * MinutesInDay div GranularityMinutes[UseGran]; dvRowHeight := Result; end; {=====} @@ -1383,21 +1374,12 @@ begin Result := FNumDays; end; {=====} - + (* function TVpDayView.HourToLine(const Value: TVpHours; const UseGran: TVpGranularity): Integer; begin - case UseGran of - gr60Min : Result := Ord (Value); - gr30Min : Result := Ord (Value) * 2; - gr20Min : Result := Ord (Value) * 3; - gr15Min : Result := Ord (Value) * 4; - gr10Min : Result := Ord (Value) * 6; - gr06Min : Result := Ord (Value) * 10; - gr05Min : Result := Ord (Value) * 12; - else Result := Ord (Value) * 2; { Default to 30 minutes } - end; -end; + Result := Ord(Value) * 60 div GranularityMinutes[UseGran]; +end; *) procedure TVpDayView.SetDrawingStyle(Value: TVpDrawingStyle); begin @@ -1430,10 +1412,7 @@ procedure TVpDayView.SetTopLine(Value: Integer); begin if Value <> FTopLine then begin if Value + VisibleLines >= pred(LineCount) then begin - if Granularity = gr60Min then - FTopLine := pred(LineCount) - VisibleLines + 2 - else - FTopLine := pred(LineCount) - VisibleLines + 2; + FTopLine := pred(LineCount) - VisibleLines + 2; { prevent the control from hanging at the bottom } if (Value < FTopLine) and (Value > 0) then FTopLine := Value; @@ -1507,7 +1486,7 @@ begin FGranularity := Value; SetTimeIntervals (FGranularity); - FTopLine := HourToLine (FTopHour, FGranularity); + FTopLine := HourToLine(FTopHour, FGranularity); Invalidate; end; @@ -2561,23 +2540,6 @@ var end; {=====} - - { Returns the time duration of one row of the DayView } - function RowDuration: Double; - begin - case Granularity of - gr60Min : result := 24 / MinutesInDay; - gr30Min : result := 30 / MinutesInDay; - gr20Min : result := 20 / MinutesInDay; - gr15Min : result := 15 / MinutesInDay; - gr10Min : result := 10 / MinutesInDay; - gr06Min : result := 6 / MinutesInDay; - gr05Min : result := 5 / MinutesInDay; - else - result := 0.0; - end; - end; - { Draws the all-day events at the top of the DayView in a special manner } procedure DrawAllDayEvents; var diff --git a/components/tvplanit/source/vpmisc.pas b/components/tvplanit/source/vpmisc.pas index a6f4a616f..be1007e68 100644 --- a/components/tvplanit/source/vpmisc.pas +++ b/components/tvplanit/source/vpmisc.pas @@ -113,23 +113,19 @@ function IncYear (TheDate : TDateTime; NumYears : Integer) : TDateTime; function GetJulianDate(Date: TDateTime): Word; -function HourToLine (const Value : TVpHours; - const Granularity : TVpGranularity) : Integer; +function HourToLine(const Value: TVpHours; const Granularity: TVpGranularity): Integer; -function GetStartLine (StartTime: TDateTime; - Granularity: TVpGranularity): Integer; +function GetStartLine(StartTime: TDateTime; Granularity: TVpGranularity): Integer; -function GetEndLine (EndTime: TDateTime; - Granularity: TVpGranularity): Integer; +function GetEndLine (EndTime: TDateTime; Granularity: TVpGranularity): Integer; -function TimeInRange(Time, StartTime, EndTime: TDateTime; - Inclusive: Boolean): Boolean; +function TimeInRange(Time, StartTime, EndTime: TDateTime; Inclusive: Boolean): Boolean; function LineToStartTime(Line: Integer; Granularity: TVpGranularity): TDateTime; function GetLineDuration(Granularity: TVpGranularity): Double; -function GetLabelWidth(ALabel : TLabel) : Integer; +function GetLabelWidth(ALabel: TLabel): Integer; implementation @@ -455,9 +451,10 @@ begin end; {=====} -function HourToLine (const Value : TVpHours; - const Granularity : TVpGranularity) : Integer; +function HourToLine(const Value: TVpHours; const Granularity: TVpGranularity): Integer; begin + Result := Ord(Value) * 60 div GranularityMinutes[Granularity]; + (* case Granularity of gr60Min : Result := Ord (Value); gr30Min : Result := Ord (Value) * 2; @@ -469,18 +466,20 @@ begin else Result := Ord (Value) * 2; { Default to 30 minutes } end; + *) end; {=====} -function GetStartLine (StartTime: TDateTime; - Granularity: TVpGranularity): Integer; +function GetStartLine(StartTime: TDateTime; Granularity: TVpGranularity): Integer; var - LineDuration : Double; { the percentage of a day covered by each line } - Time : Double; + LineDuration: Double; { the percentage of a day covered by each line } + Time: Double; begin { remove the date part, and add one minute to the time } - Time := StartTime - trunc(StartTime) + (1 / MinutesInDay); - +// Time := StartTime - trunc(StartTime) + (1 / MinutesInDay); + Time := frac(StartTime) + 1 / MinutesInDay; + LineDuration := GranularityMinutes[Granularity] / MinutesInDay; + (* case Granularity of gr60Min : LineDuration := 60 / MinutesInDay; gr30Min : LineDuration := 30 / MinutesInDay; @@ -492,19 +491,22 @@ begin else LineDuration := 30 / MinutesInDay; end; + *) result := trunc(Time / LineDuration); end; {=====} -function GetEndLine (EndTime: TDateTime; - Granularity: TVpGranularity): Integer; +function GetEndLine(EndTime: TDateTime; Granularity: TVpGranularity): Integer; var - LineDuration : Double; { the percentage of a day covered by each line } - Time : Double; + LineDuration: Double; { the percentage of a day covered by each line } + Time: Double; begin { remove the date part, and subtract one minute from the time } - Time := EndTime - trunc(EndTime) - (1 / MinutesInDay); + Time := frac(EndTime) - 1 / MinutesInDay; +// Time := EndTime - trunc(EndTime) - (1 / MinutesInDay); + LineDuration := GranularityMinutes[Granularity] / MinutesInDay; + { case Granularity of gr60Min : LineDuration := 60 / MinutesInDay; @@ -517,7 +519,7 @@ begin else LineDuration := 30 / MinutesInDay; end; - + } result := trunc(Time / LineDuration); end; {=====} @@ -528,7 +530,7 @@ begin result := 0.0; case AdvanceType of atMinutes : result := Advance / MinutesInDay; - atHours : result := (Advance * 60) / MinutesInDay; + atHours : result := Advance * 60 / MinutesInDay; atDays : result := Advance; end; end; @@ -553,8 +555,10 @@ end; function LineToStartTime(Line: Integer; Granularity: TVpGranularity): TDateTime; begin + Result := frac(Line * GranularityMinutes[Granularity] / MinutesInDay); + (* case Granularity of - gr60Min : result := (Line * 24) / MinutesInDay; + gr60Min : result := (Line * 24) / MinutesInDay; // shouldn't this be 60? gr30Min : result := (Line * 30) / MinutesInDay; gr20Min : result := (Line * 20) / MinutesInDay; gr15Min : result := (Line * 15) / MinutesInDay; @@ -566,13 +570,16 @@ begin end; {chop off the date portion} result := result - trunc(Result); + *) end; {=====} function GetLineDuration(Granularity: TVpGranularity): Double; begin + Result := GranularityMinutes[Granularity] / MinutesInDay; + (* case Granularity of - gr60Min : result := 24 / MinutesInDay; + gr60Min : result := 24 / MinutesInDay; // shouldn't this be 60? gr30Min : result := 30 / MinutesInDay; gr20Min : result := 20 / MinutesInDay; gr15Min : result := 15 / MinutesInDay; @@ -584,10 +591,11 @@ begin end; { chop off the date portion } result := result - trunc(result); + *) end; {=====} -function GetLabelWidth(ALabel : TLabel) : Integer; +function GetLabelWidth(ALabel: TLabel): Integer; var canvas: TControlCanvas; begin diff --git a/components/tvplanit/source/vpprtfmt.pas b/components/tvplanit/source/vpprtfmt.pas index 533a5aa42..6e591f855 100644 --- a/components/tvplanit/source/vpprtfmt.pas +++ b/components/tvplanit/source/vpprtfmt.pas @@ -1344,11 +1344,17 @@ end; {=====} procedure TVpPrinter.AddDefaultVariables (Date : TDateTime); + function HourToStr (Hour : TVpHours; Mil : Boolean) : string; begin if Mil then Result := IntToStr(ord(Hour)) else + if ord(Hour) mod 12 = 0 then + Result := '12' + else + Result := IntToStr(ord(Hour) mod 12); + { case Hour of h_00 : Result := '12'; h_01 : Result := '1'; @@ -1375,23 +1381,15 @@ procedure TVpPrinter.AddDefaultVariables (Date : TDateTime); h_22 : Result := '10'; h_23 : Result := '11'; end; + } end; - function GranularityToStr (Gran : TVpGranularity) : string; + function GranularityToStr(Gran: TVpGranularity): string; begin - Result := ''; - case Gran of - gr60Min : Result := '60'; - gr30Min : Result := '30'; - gr20Min : Result := '20'; - gr15Min : Result := '15'; - gr10Min : Result := '10'; - gr06Min : Result := '6'; - gr05Min : Result := '5'; - end; + Result := IntToStr(GranularityMinutes[Gran]); end; - function HourToAMPM (Hour : TVpHours) : string; + function HourToAMPM(Hour: TVpHours): string; begin if (Hour >= H_00) and (Hour <= h_11) then Result := 'AM'