Using tool profiles

Tutorials ››
Parent Previous Next

As tools may contain properties that shape the resulting object in many ways, you will probably see the need for subclassing such tools into different user commands. A typical example is the Line tool, which has the potential of drawing a double arrow, a single arrow or no arrow at all. It makes sense to present these three options as different tool types for the end user.

This can be done by means of the BeginDraw event handler, by for instance checking the state of a command button or an action:

If btn_SinglArrow.Down then
 DrawObject.EndArrow := True
else if btn_DoubleArrow.Down then, etc.

However, there is a simpler and more elegant way to handle this issue, namely by using tool profiles.

A tool profile couples a tool and its configuration. It is a simple record containing the tool name and an identifier that you refer to when setting the tool properties on the resulting object. This is how it looks:

TToolProfile = record
 Tool : TToolType;
 Profile : TToolType;
end;

And this is how to put it in action:

Const
atDblArrow = ‘DblArrow’;
atSglArrw = ‘SglArrow’;
atNoArrow = ‘NoArrow’;

The profile identifier must be unique and it must not conflict with any tool names.

When you have made up your mind about which profiles you need, you tell ImagePainter by calling the RegisterToolProfile method:

RegisterToolProfile(atLine, atDblArrow);
RegisterToolProfile(atLine, atSglArrow);
RegisterToolProfile(atLine, atNoArrow);

Now, to make it work, define the configuration associated with the profile identifiers. This you do in the OnToolProfile event handler.

TProfileEvent = procedure(Sender: TObject; Figure: TFigure; Profile,  Tool : TTooltype) of object;

TForm1.ImagePainter1lToolProfile( Sender: TObject; Figure: TFigure; Profile,  Tool : TTooltype);
begin
 if Figure is TLine
 with Figure as TLine do
 begin
   if Profile = atDblArrow then
   begin
     StartArrow := True;
     EndArrow := True;
   end
   else if Profile = atSglArrow  then
   begin
     EndArrow := True;  
     StartArrow := false;
   end
   else if Profile = atNoArrow  then
   begin
     EndArrow := False;  
     StartArrow := false;
  end;
end;
end;

By now, you are ready to use the profile. You do it as you activate a regular tool, except you set ActiveToolProfile instead of ActiveTool:

ActiveToolProfile := DblArrow;

This activates the profile and makes TLine the active tool.

ActiveToolProfile := atLine is also legal, but then the tool is not profiled. It is the same as coding
ActiveTool := atLine