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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
unit Unit8;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,
Contnrs;
type
TChantierEntity = class(TObject)
private
FChantierID: Integer;
FChantierName: string;
public
property ChantierID: Integer read FChantierID write FChantierID;
property ChantierName: string read FChantierName write FChantierName;
end;
TChantierList = class(TObjectList)
private
public
procedure Load();
procedure Save();
end;
TEmployeEntity = class(TObject)
private
FEmployeID: Integer;
FEmployeName: string;
public
property EmployeID: Integer read FEmployeID write FEmployeID;
property EmployeName: string read FEmployeName write FEmployeName;
end;
TEmployeList = class(TObjectList)
private
public
procedure Load(AChantier: TChantierEntity);
procedure Save();
end;
type
TChantiersController = class;
TChantierControl = class;
TEmployeControl = class;
TDragDockMainForm = class(TForm)
grpEntities: TGroupBox;
btnEntityAddChantier: TButton;
btnEntityAddEmploye: TButton;
sbChantiers: TScrollBox;
ckbDragDropOnOver: TCheckBox;
procedure btnEntityAddChantierClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnEntityAddEmployeClick(Sender: TObject);
procedure ckbDragDropOnOverClick(Sender: TObject);
private
FChantiers: TChantiersController;
public
{ Déclarations publiques }
end;
TChantiersController = class(TObject)
private
FContainer: TScrollBox;
FChantiers: TChantierList;
FDropOnOver: Boolean;
procedure ShowChantiersEnterEventHandler(Sender: TObject);
procedure ShowChantiersCloseUpEventHandler(Sender: TObject);
procedure ShowChantiersExitEventHandler(Sender: TObject);
procedure SetDropOnOver(const Value: Boolean);
public
constructor Create(AContainer: TScrollBox);
destructor Destroy(); override;
function AddChantier(AChantier: TChantierEntity): TChantierControl; overload;
function AddChantier(const AChantierName: string): TChantierControl; overload;
function AddEmployee(const AEmployeName: string): TEmployeControl;
class function AddEmployeInChantier(AEmploye: TEmployeControl; AChantier: TChantierControl): TEmployeControl; overload;
class function AddEmployeInChantier(const AEmployeName: string; AChantier: TChantierControl): TEmployeControl; overload;
property DropOnOver: Boolean read FDropOnOver write SetDropOnOver;
end;
TChantierControl = class(TPanel)
private
FBorder: TShape;
FTitle: TLabel;
FEntity: TChantierEntity;
FEmployes: TEmployeList;
FDropOnOver: Boolean;
function GetChantierID(): Integer;
procedure SetChantierID(const Value: Integer);
function GetChantierName(): string;
procedure SetChantierName(const Value: string);
procedure ToggleSizeableClickEventHandler(Sender: TObject);
procedure ChantierDragOverEventHandler(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
procedure ChantierDragDropEventHandler(Sender, Source: TObject; X, Y: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
property ChantierID: Integer read GetChantierID write SetChantierID;
property ChantierName: string read GetChantierName write SetChantierName;
property Entity: TChantierEntity read FEntity;
property Employes: TEmployeList read FEmployes;
property DropOnOver: Boolean read FDropOnOver write FDropOnOver;
end;
TEmployeControl = class(TPanel)
private
FBorder: TShape;
FTitle: TLabel;
FContrat: TLabel;
FImage: TImage;
FEntity: TEmployeEntity;
FDragImageList: TDragImageList;
function GetEmployeID(): Integer;
procedure SetEmployeID(const Value: Integer);
function GetEmployeName(): string;
procedure SetEmployeName(const Value: string);
procedure EmployeDragSubStartEventHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
procedure ChangeChantier(ANewChantier: TChantierControl);
property EmployeID: Integer read GetEmployeID write SetEmployeID;
property EmployeName: string read GetEmployeName write SetEmployeName;
property Entity: TEmployeEntity read FEntity;
end;
var
DragDockMainForm: TDragDockMainForm;
DummyID: Integer;
implementation
uses Math, StrUtils;
{$R *.dfm}
{ TChantierTDragDockMainFormsController }
procedure TDragDockMainForm.FormCreate(Sender: TObject);
begin
FChantiers := TChantiersController.Create(sbChantiers);
ckbDragDropOnOver.Checked := FChantiers.DropOnOver;
end;
procedure TDragDockMainForm.FormDestroy(Sender: TObject);
begin
FreeAndNil(FChantiers);
end;
procedure TDragDockMainForm.btnEntityAddChantierClick(Sender: TObject);
var
S: string;
begin
S := TimeToStr(Now());
if InputQuery('Nouveau Chantier', 'Nom du nouveau chantier', S) then
FChantiers.AddChantier(S);
end;
procedure TDragDockMainForm.btnEntityAddEmployeClick(Sender: TObject);
var
S: string;
begin
S := TimeToStr(Now());
if InputQuery('Nouvel Employé', 'Nom du nouvel employé', S) then
FChantiers.AddEmployee(S);
end;
procedure TDragDockMainForm.ckbDragDropOnOverClick(Sender: TObject);
begin
FChantiers.DropOnOver := ckbDragDropOnOver.Checked;
end;
{ TChantiersController }
constructor TChantiersController.Create(AContainer: TScrollBox);
var
I: Integer;
begin
inherited Create();
FDropOnOver := True;
FContainer := AContainer;
FChantiers := TChantierList.Create(False);
FChantiers.Load();
for I := 0 to FChantiers.Count - 1 do
AddChantier(TChantierEntity(FChantiers[I]));
end;
destructor TChantiersController.Destroy();
begin
if Assigned(FChantiers) then
FChantiers.Save();
FreeAndNil(FChantiers);
inherited Destroy();
end;
function TChantiersController.AddChantier(AChantier: TChantierEntity): TChantierControl;
begin
Result := AddChantier(AChantier.ChantierName);
Result.ChantierID := AChantier.ChantierID;
end;
function TChantiersController.AddChantier(const AChantierName: string): TChantierControl;
begin
Result := TChantierControl.Create(FContainer);
Result.Left := 8 * FContainer.ControlCount + 300 * (FContainer.ControlCount - 1);
Result.Width := 300;
Result.Height := Max(Result.Height, FContainer.ClientHeight - 100);
Result.ChantierName := AChantierName;
Result.DropOnOver := Self.DropOnOver;
FChantiers.Add(Result.Entity);
end;
function TChantiersController.AddEmployee(const AEmployeName: string): TEmployeControl;
function SelectChantiers(): TChantierControl;
var
Form: TForm;
Editor: TComboBox;
I: Integer;
begin
Result := nil;
Form := TForm.Create(nil);
try
Form.BorderStyle := bsNone;
Form.AutoSize := True;
Form.Position := poDesigned;
Form.Left := Mouse.CursorPos.X;
Form.Top := Mouse.CursorPos.Y;
Editor := TComboBox.Create(Form);
Editor.Style := csDropDownList;
Editor.Parent := Form;
Editor.OnEnter := ShowChantiersEnterEventHandler;
Editor.OnExit := ShowChantiersExitEventHandler;
Editor.OnCloseUp := ShowChantiersCloseUpEventHandler;
for I := 0 to FContainer.ControlCount - 1 do
Editor.Items.AddObject(TChantierControl(FContainer.Controls[I]).ChantierName, FContainer.Controls[I]);
Editor.Items.Add('Cancel');
Form.ShowModal();
if Editor.ItemIndex >= 0 then
Result := TChantierControl(Editor.Items.Objects[Editor.ItemIndex]);
finally
Form.Free();
end;
end;
var
LChantier: TChantierControl;
begin
LChantier := SelectChantiers();
if Assigned(LChantier) then
Result := AddEmployeInChantier(AEmployeName, LChantier)
else
Result := nil;
end;
class function TChantiersController.AddEmployeInChantier(AEmploye: TEmployeControl; AChantier: TChantierControl): TEmployeControl;
begin
Result := AddEmployeInChantier(AEmploye.EmployeName, AChantier);
Result.EmployeID := AEmploye.EmployeID;
end;
class function TChantiersController.AddEmployeInChantier(const AEmployeName: string; AChantier: TChantierControl): TEmployeControl;
begin
Result := TEmployeControl.Create(AChantier);
Result.Left := 8;
Result.Top := 8 * AChantier.ControlCount + 50 * (AChantier.ControlCount - 3);
Result.Width := 150;
Result.EmployeName := AEmployeName;
AChantier.Employes.Add(Result.Entity);
AChantier.Height := Max(AChantier.Height, 50 * AChantier.Employes.Count);
end;
procedure TChantiersController.ShowChantiersEnterEventHandler(Sender: TObject);
begin
TComboBox(Sender).DroppedDown := True;
end;
procedure TChantiersController.ShowChantiersCloseUpEventHandler(Sender: TObject);
begin
TForm(TComboBox(Sender).Owner).ModalResult := mrOK;
end;
procedure TChantiersController.ShowChantiersExitEventHandler(Sender: TObject);
begin
TForm(TComboBox(Sender).Owner).ModalResult := mrCancel;
end;
procedure TChantiersController.SetDropOnOver(const Value: Boolean);
var
I: Integer;
C: TControl;
begin
for I := 0 to FContainer.ControlCount - 1 do
begin
C := FContainer.Controls[I];
if C is TChantierControl then
TChantierControl(C).DropOnOver := Value;
end;
end;
{ TChantierControl }
constructor TChantierControl.Create(AOwner: TComponent);
var
I: Integer;
begin
inherited Create(AOwner);
Parent := AOwner as TWinControl;
BevelOuter := bvNone;
OnDragOver := ChantierDragOverEventHandler;
OnDragDrop := ChantierDragDropEventHandler;
FEntity := TChantierEntity.Create();
FEmployes := TEmployeList.Create(False);
FBorder := TShape.Create(Self);
FBorder.Parent := Self;
FBorder.Top := 8;
FBorder.Left := 0;
FBorder.Width := Self.Width;
FBorder.Height := Self.Height - 8;
FBorder.Anchors := [akLeft, akTop, akRight, akBottom];
FBorder.Pen.Color := Random($FFFFFF);
FBorder.Enabled := False;
FTitle := TLabel.Create(Self);
FTitle.Parent := Self;
FTitle.Left := 8;
FTitle.OnDblClick := ToggleSizeableClickEventHandler;
FEmployes.Load(FEntity);
for I := 0 to FEmployes.Count - 1 do
TChantiersController.AddEmployeInChantier(TEmployeEntity(FEmployes[I]).EmployeName, Self);
end;
destructor TChantierControl.Destroy();
begin
if Assigned(FEmployes) then
FEmployes.Save();
FreeAndNil(FBorder);
FreeAndNil(FEmployes);
FreeAndNil(FEntity);
inherited Destroy();
end;
procedure TChantierControl.ToggleSizeableClickEventHandler(Sender: TObject);
begin
SetWindowLong(Self.Handle, GWL_STYLE, GetWindowLong(Self.Handle, GWL_STYLE) xor WS_SIZEBOX);
Self.Width := Self.Width + IfThen(LongBool(GetWindowLong(Self.Handle, GWL_STYLE) and WS_SIZEBOX), +8, -8);
Self.Height := Self.Height + IfThen(LongBool(GetWindowLong(Self.Handle, GWL_STYLE) and WS_SIZEBOX), +8, -8);
end;
procedure TChantierControl.ChantierDragOverEventHandler(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := (Sender is TChantierControl) and (Source is TEmployeControl) ;
if Accept and TChantierControl(Sender).DropOnOver then
ChantierDragDropEventHandler(Sender, Source, X, Y);
end;
procedure TChantierControl.ChantierDragDropEventHandler(Sender, Source: TObject; X, Y: Integer);
begin
TEmployeControl(Source).ChangeChantier(TChantierControl(Sender));
TEmployeControl(Source).Left := X;
TEmployeControl(Source).Top := Y;
end;
function TChantierControl.GetChantierID(): Integer;
begin
Result := FEntity.ChantierID;
end;
procedure TChantierControl.SetChantierID(const Value: Integer);
begin
FEntity.ChantierID := Value;
end;
function TChantierControl.GetChantierName(): string;
begin
Result := FEntity.ChantierName;
end;
procedure TChantierControl.SetChantierName(const Value: string);
begin
FEntity.ChantierName := Value;
FTitle.Caption := Value;
end;
{ TEmployeControl }
constructor TEmployeControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 50;
Parent := AOwner as TWinControl;
BevelOuter := bvNone;
DragMode := dmAutomatic;
FEntity := TEmployeEntity.Create();
FBorder := TShape.Create(Self);
FBorder.Parent := Self;
FBorder.Top := 0;
FBorder.Left := 0;
FBorder.Width := Self.Width;
FBorder.Height := Self.Height - 8;
FBorder.Anchors := [akLeft, akTop, akRight, akBottom];
FBorder.Pen.Color := Random($FFFFFF);
FBorder.Pen.Style := psDashDotDot;
FBorder.Enabled := False; // IMPORTANT !
FTitle := TLabel.Create(Self);
FTitle.Parent := Self;
FTitle.Top := 4;
FTitle.Left := 48;
FTitle.OnMouseDown := EmployeDragSubStartEventHandler; // IMPORTANT !
FContrat := TLabel.Create(Self);
FContrat.Parent := Self;
FContrat.Top := 4 + FTitle.Top + FTitle.Height;
FContrat.Left := 48;
FContrat.OnMouseDown := EmployeDragSubStartEventHandler; // IMPORTANT !
FImage := TImage.Create(Self);
FImage.Parent := Self;
FImage.Top := 6;
FImage.Left := 8;
FImage.Width := 32;
FImage.Height := 32;
FImage.Enabled := False; // IMPORTANT !
with FImage.Picture.Bitmap do
begin
Width := 32;
Height := 32;
Canvas.Pen.Color := Random($FFFFFF);
Canvas.Brush.Color := Random($FFFFFF);
Canvas.Rectangle(0, 0, 31, 31);
end;
end;
destructor TEmployeControl.Destroy();
begin
FreeAndNil(FDragImageList);
FreeAndNil(FBorder);
FreeAndNil(FEntity);
inherited Destroy();
end;
procedure TEmployeControl.ChangeChantier(ANewChantier: TChantierControl);
var
LOldChantier: TChantierControl;
begin
LOldChantier := TChantierControl(Self.Parent);
LOldChantier.Employes.Extract(Self);
ANewChantier.Employes.Add(Self);
Self.Parent := ANewChantier;
end;
procedure TEmployeControl.EmployeDragSubStartEventHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if (Sender is TLabel) and (TLabel(Sender).Parent is TEmployeControl) then
TLabel(Sender).Parent.BeginDrag(True); // IMPORTANT !
end;
function TEmployeControl.GetEmployeID(): Integer;
begin
Result := FEntity.EmployeID;
end;
procedure TEmployeControl.SetEmployeID(const Value: Integer);
begin
FEntity.EmployeID := Value;
end;
function TEmployeControl.GetEmployeName(): string;
begin
Result := FEntity.EmployeName;
end;
procedure TEmployeControl.SetEmployeName(const Value: string);
begin
FEntity.EmployeName := Value;
FTitle.Caption := Value;
FContrat.Caption := IfThen(LongBool(Random(2)), 'Avec Contrat', 'Sans Contrat');
end;
procedure TChantierList.Load();
var
I: Integer;
Entity: TChantierEntity;
begin
// SELECT CHANTIER_ID, CHANTIER_NAME, ... FROM T_CHANTIER
for I := 0 to 5 do
begin
Entity := TChantierEntity.Create();
Add(Entity);
Inc(DummyID);
Entity.ChantierID := DummyID;
Entity.ChantierName := FormatDateTime('hh:nn:ss:zzz.', Now()) + IntToStr(DummyID);
end;
end;
procedure TChantierList.Save();
begin
// for Items.Count
// if ID = 0
// INSERT INTO T_CHANTIER
// else
// UPDATE SET ... T_CHANTIER WHERE CHANTIER_ID = Items[I].ChantierID
end;
{ TEmployeList }
procedure TEmployeList.Load(AChantier: TChantierEntity);
var
I: Integer;
Entity: TEmployeEntity;
begin
// SELECT EMPLOYE_ID, EMPLOYE_NAME ... FROM T_EMPLOYE WHERE CHANTIER_ID = AChantier.ChantierID
for I := 0 to 1000 do
begin
Entity := TEmployeEntity.Create();
Add(Entity);
Inc(DummyID);
Entity.EmployeID := DummyID;
Entity.EmployeName := FormatDateTime('hh:nn:ss:zzz.', Now()) + IntToStr(DummyID);
end;
end;
procedure TEmployeList.Save();
begin
// for Items.Count
// if ID = 0
// INSERT INTO T_EMPLOYE
// else
// UPDATE SET ... T_EMPLOYE WHERE EMPLOYE_ID = Items[I].EmployeID
end;
end. |
Partager