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
| unit GridG7;
interface
uses
Classes, Windows, Controls, Graphics, Types, SysUtils, StdCtrls,
Dialogs, ExtCtrls, UG7Object, UEtapeInitiale,
messages;//RetourInit, Forms,
//Branches, G7Definition, G7Object;
type
//////////////////////////////Class Pricipale//////////////////////////////////
TGridGrafcet = class(TCustomGridPanel)
const
COLWIDTH = 155;
LINEHEIGHT = 60;
private
procedure insertCase(indexCol, IndexLine : integer);
procedure moveComponent (Col1, Row1, Col2, Row2 : integer);
procedure WndProc(var msg : TMessage); override;
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function getCase(Coord : TPoint): Tpoint; overload;
function getCase : Tpoint; overload;
function getObject(CoordObject : TPoint): TControl; overload;
function getObject(Col, Row : Integer): TControl; overload;
function getClassName(indexCol, indexRow : Integer): String;
function getTypeObj(indexCol, indexRow : Integer) : TTypeGrafObject;
procedure insertline(index : integer);
end;
implementation
////////////////////////////////////////////////////////////////////////////////
//////// ////////
//////// TGridGrafcet ////////
//////// ////////
////////////////////////////////////////////////////////////////////////////////
{constructeur}
constructor TGridGrafcet.Create(AOwner: TComponent);
var
IndexRow, IndexCol : integer;
begin
inherited create(AOwner);
Self.ShowCaption := False;
Self.Color := clWhite;
Self.RowCollection.Add;
for IndexRow := 0 to RowCollection.Count-1 do
begin
RowCollection[IndexRow].SizeStyle := ssAbsolute;
RowCollection[IndexRow].Value := LINEHEIGHT;
end;
for IndexCol := 0 to ColumnCollection.Count-1 do
begin
ColumnCollection[IndexCol].SizeStyle := ssAbsolute;
ColumnCollection[IndexCol].Value := COLWIDTH;
end;
Resize;
Refresh;
end;
//******************************************************************************
{destructeur}
destructor TGridGrafcet.Destroy;
begin
inherited;
end;
//******************************************************************************
{: Procedure d'interception des messages}
procedure TGridGrafcet.WndProc(var msg: TMessage);
var
CellPos : TPoint;
NbControl : integer;
indexCol, indexRow : integer;
indexcontrol : integer;
rowcontrol, columncontrol : integer;
begin
inherited;
//si on est en mode design
if (csDesigning in ComponentState) then
begin
//Si on ajoute un composant
if (msg.Msg = CM_CONTROLCHANGE ) AND (Msg.LParamLo = 1) then
begin
//On enregistre la case dans laquelle on veut mettre l'objet
CellPos := self.getCase(self.CalcCursorPos);
//on récupère l'index du control ajouté
NbControl := self.ControlCount;
indexcontrol := self.ControlCollection.IndexOf(Self.Controls[NbControl-1]);
//On récupère la ligne et la colonne ou le contole est ajouté automatiquement
rowcontrol := self.ControlCollection.Items[indexcontrol].Row;
columncontrol := self.ControlCollection.Items[indexcontrol].Column;
//on déplace le controle la ou on veut qu'il soit placé
moveComponent(columncontrol, rowcontrol, CellPos.X, CellPos.Y);
end;
end;
end;
//******************************************************************************
{:Fonction qui renvoie les coordonnées d'une case des coordonnées pixel
@param Coord : coordonnée en pixel
@return : coordonnée ligne colonne d'une case}
function TGridGrafcet.getCase(Coord : TPoint): Tpoint;
var
Row, Col : integer;
RowValue, ColValue : integer;
CoordCell : Tpoint;
begin
Row := 0;
Col := 0;
RowValue := 0;
CoordCell.X := 0;
CoordCell.Y := 0;
RowValue := Self.CellSize[Col, Row].Y;
//on compte dans quelle ligne se trouve le point
while (Coord.Y >= RowValue) and (Row <= RowCollection.Count -1) do
begin
Row := Row+1;
RowValue := RowValue + CellSize[Col, Row].Y;
end;
CoordCell.Y := Row;
row := 0;
ColValue := 0;
//on compte dans quelle colonne se trouve le point
ColValue := Self.CellSize[Col, Row].X;
while (Coord.X >= ColValue) and (Col<= ColumnCollection.Count -1) do
begin
Col := Col+1;
ColValue := ColValue + CellSize[Col, Row].X;
end;
CoordCell.X := Col;
Result := CoordCell;
end;
//******************************************************************************
{:Fonction qui renvoie les coordonnées d'une case du click souris
@return : coordonnée ligne colonne d'une case}
function TGridGrafcet.getCase: Tpoint;
var
Row, Col : integer;
RowValue, ColValue : integer;
CoordCell : Tpoint;
Coord : Tpoint;
begin
Row := 0;
Col := 0;
Coord := self.CalcCursorPos;
RowValue := 0;
CoordCell.X := 0;
CoordCell.Y := 0;
RowValue := Self.CellSize[Col, Row].Y;
//on compte dans quelle ligne se trouve le point
while (Coord.Y >= RowValue) and (Row <= RowCollection.Count -1) do
begin
Row := Row+1;
RowValue := RowValue + CellSize[Col, Row].Y;
end;
CoordCell.Y := Row;
row := 0;
ColValue := 0;
//on compte dans quelle colonne se trouve le point
ColValue := Self.CellSize[Col, Row].X;
while (Coord.X >= ColValue) and (Col<= ColumnCollection.Count -1) do
begin
Col := Col+1;
ColValue := ColValue + CellSize[Col, Row].X;
end;
CoordCell.X := Col;
Result := CoordCell;
end;
//******************************************************************************
{:Fonction qui renvoie le Tcontrol présent dans un case passé par TPoint
@param CoordObject : coordonnée la case où est l'objet
@return : pointeur vers l'objet}
function TGridGrafcet.getObject(CoordObject: TPoint): TControl;
var
Row, Col : integer;
begin
Col := CoordObject.X;
Row := CoordObject.Y;
if Self.ControlCollection.ControlItems[Col, Row] <> nil then
begin
Result := Self.ControlCollection.ControlItems[Col, Row].Control;
end
else
begin
Result := nil;
end;
end;
//******************************************************************************
{:Fonction surchargée qui renvoie le Tcontrol présent dans un case passé par Integer
@param X : colonne où est l'objet
@param Y : ligne où est l'objet
@return : pointeur vers l'objet}
function TGridGrafcet.getObject(Col, Row: Integer): TControl;
begin
if Self.ControlCollection.ControlItems[Col, Row] <> nil then
Result := Self.ControlCollection.ControlItems[Col, Row].Control
else
Result := nil;
end;
//******************************************************************************
{:Fonction qui renvoie la classe d'un objet
@param indexCol : colonne où est l'objet
indexRow : ligne où est l'objet
@return : nom de classe de l'objet}
function TGridGrafcet.getClassName(indexCol, indexRow: Integer): String;
begin
showmessage('getclassename');
if getObject(indexCol, indexRow)<>nil then
begin
result := getObject(indexCol, indexRow).ClassName;
showmessage('result');
end
else
result := 'nil';
end;
//******************************************************************************
{:Fonction qui renvoie le type d'un objet
@param indexCol : colonne où est l'objet
indexRow : ligne où est l'objet
@return : Type de l'objet}
function TGridGrafcet.getTypeObj(indexCol, indexRow: Integer): TTypeGrafObject;
begin
{if (getObject(indexCol, indexRow)<>nil) then
result := (getObject(indexCol, indexRow) AS TG7GraphicControl).GType
else
result := typNil;}
end;
{******************************************************************************}
{ Fonctions et procédures Privées }
{******************************************************************************}
{:procédure qui insert une ligne vide à la position index
@param index : position de la ligne à ajouter}
procedure TGridGrafcet.insertline(index: integer);
var
Row, Col : integer;
begin
//Insertion d'une ligne à la position index
RowCollection.Insert(index);
RowCollection[index].SizeStyle := ssAbsolute;
RowCollection[index].Value := LINEHEIGHT;
//Décallage vers le bas de tous les objet dans les cases
for Row := RowCollection.Count-1 downto index-1 do
begin
for col := ColumnCollection.Count-1 downto 0 do
begin
if ControlCollection.Controls[col, row] <> nil then
begin
moveComponent(col, row, col, row+1);
end;
end;
end;
Resize;
end;
//******************************************************************************
{:procédure qui insert une case vide à la position indexCol, indexLine
@param indexCol : position de la colonne à ajouter
@param indexLine : position de la ligne à ajouter}
procedure TGridGrafcet.insertCase(indexCol, indexLine: integer);
var
Col : integer;
begin
//Insertion d'une colonne à la position indexCol
ColumnCollection.Insert(indexCol);
ColumnCollection[indexCol].SizeStyle := ssAbsolute;
ColumnCollection[indexCol].Value := 155;
//Décallage vers la droite de tous les objets sur la ligne indexLine
for Col := ColumnCollection.Count-1 downto indexCol do
begin
if (getObject(col, indexLine) <> nil) then
begin
moveComponent(col, indexLine, col+1, indexLine);
end;
end;
end;
//******************************************************************************
{:Permet de déplacer un composant
@param Col1 : Colonne actuelle du composant
Row1 : Line actuelle du composant
Col2 : Nouvelle colonne du composant
Row2 : Nouvelle ligne du composant}
procedure TGridGrafcet.moveComponent(Col1, Row1, Col2, Row2 : integer);
begin
ControlCollection.ControlItems[Col1, Row1].SetLocation(Col2, Row2, true);
end;
procedure TGridGrafcet.Resize;
begin
inherited;
self.Width := COLWIDTH * Self.ColumnCollection.Count;
self.Height:= LINEHEIGHT * self.RowCollection.Count;
end;
end. |
Partager