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 590 591 592 593 594 595 596 597 598 599
|
// REM : #include <math.h>
__fastcall jNode::jNode(TComponent *AOwner)
: TComponent(AOwner)
{
Targets = new TList;
}
//----------
__fastcall jNode::jNode(TComponent *AOwner, TPoint APos,
AnsiString ACaption)
: TComponent(AOwner)
{
Targets = new TList;
Selected = true;
Pos =APos;
Caption = ACaption;
}
//----------
__fastcall jNode::~jNode()
{
Targets->Clear();
delete Targets;
}
//---------- grossier mais ça fonctionne
bool __fastcall jNode::HasFocus(TRect *R)
{
int left = Pos.x - ra;
int top = Pos.y - ra;
int right = Pos.x + ra;
int bottom = Pos.y + ra;
return((left < R->Right)&&
(right >= R->Left)&&
(top < R->Bottom)&&
(bottom >= R->Top));
}
//----------
void __fastcall jNode::PaintTo(TCanvas *C)
{
//dessin de l'objet
if(Selected == false)
{
C->Brush->Style = bsSolid;
C->Brush->Color = clYellow;
}
else
{
C->Brush->Style = bsSolid;
C->Brush->Color = clSilver;
}
C->Ellipse(Pos.x - ra, Pos.y - ra, Pos.x + ra, Pos.y + ra);
// dessin du Caption
C->Brush->Style = bsClear;
int w = C->TextWidth(Caption) / 2;
C->TextOut(Pos.x - w, Pos.y + ra + 8, Caption);
}
//----- la flèche ... et sa pointe orientée ...
void __fastcall jNode::PaintLinksTo(TCanvas *C)
{
int node;
jNode *Target;
int x;
int y;
double h;
int x0;
int y0;
double h1;
int x1;
int y1;
int x2;
int y2;
double cosa=1.0;
double sina=0.0;
double cosb;
double sinb;
for(node = 0; node < Targets->Count; node++)
{
Target = (jNode*)Targets->Items[node];
//Calcul des sommets orientés du triangle
x = Target->Pos.x - Pos.x;
y = Target->Pos.y - Pos.y;
h = hypot(x,y);
if(h != 0.0)
{
sina = y / h;
cosa = x / h;
}
h = ra;
x0 = Pos.x + (cosa * h);
y0 = Pos.y + (sina * h);
h = h + 12;
h1 = hypot(h, 6);
sinb = 0.0;
cosb = 1.0;
if(h1 != 0.0)
{
sinb = 6 / h1;
cosb = h / h1;
}
y1 = Pos.y + (((sina * cosb) + (sinb * cosa)) * h1);
x1 = Pos.x + (((cosa * cosb) - (sina * sinb)) * h1);
sinb = 0.0;
if(h1 != 0.0)
{
sinb = -6 / h1;
}
y2 = Pos.y + (((sina * cosb) + (sinb * cosa)) * h1);
x2 = Pos.x + (((cosa * cosb) - (sina * sinb)) * h1);
//Contour du triangle
C->Pen->Color = TColor(0x010101);
C->MoveTo(x0,y0);
C->LineTo(x1,y1);
C->LineTo(x2,y2);
C->LineTo(x0,y0);
// iso-bary-centre du triangle
x = (x0 + x1 + x2) / 3;
y = (y0 + y1 + y2) / 3;
//remplissage
C->Brush->Color = TColor(0x010101);
C->FloodFill(x,y, (TColor)0x010101, fsBorder);
//limite du corps de la flèche coté cible sur son périmètre
x = Target->Pos.x + (cosa * -ra);
y = Target->Pos.y + (sina * -ra);
//Dessin du corps de la flèche
C->Pen->Color = clBlack;
C->MoveTo(Pos.x, Pos.y);
C->LineTo(x, y);
}
}
//----------
jNode* __fastcall jNode::LinkTo(jNode *Target)
{
if((Targets->IndexOf(Target) == -1) && (Target != this))
{
Targets->Add(Target);
return Target;
}
return NULL;
}
//----------
void __fastcall jNode::Unlink(jNode *Target)
{
int n = Targets->IndexOf(Target);
if( n!= -1)
{
Targets->Delete(n);
}
}
//----------
//----------
/*
Cette classe (jTree) sert non seulement à gérer les noeuds
mais aussi à soulager le code des événementielles de la form
Ceci dit, on pourrait s'en passer...
Seulement moi ... j'aime bien que ce soit organisé !
*/
//----------
//----------
__fastcall jTree::jTree(TComponent *AOwner)
: TComponent(AOwner)
{
Nodes = new TList;
}
//----------
__fastcall jTree::~jTree()
{
Nodes->Clear();
delete Nodes;
}
//----------
jNode* __fastcall jTree::Add(TPoint APos)
{
jNode *Node = new jNode(this, APos, "NODE");
Nodes->Add(Node);
return Node;
}
//----------
void __fastcall jTree::Select(TRect *R)
{
int node;
jNode *Node;
for(node = 0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->HasFocus(R)) Node->Selected = true;
}
}
//----------
void __fastcall jTree::Unselect()
{
int node;
for(node=0; node < Nodes->Count; node++)
{
((jNode*)Nodes->Items[node])->Selected = false;
}
}
//----------
jNode* __fastcall jTree::NodeHasFocus(TRect *R)
{
int max=Nodes->Count-1;
int node;
jNode *Node;
for(node = max; node >= 0; node--)
{
Node = (jNode*)Nodes->Items[node];
if(Node->HasFocus(R)) return Node;
}
return NULL;
}
//----------
void __fastcall jTree::PaintTo(TCanvas *C)
{
int node;
jNode *Node;
for(node=0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->Selected == false) Node->PaintLinksTo(C);
}
for(node=0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->Selected == false) Node->PaintTo(C);
}
for(node=0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->Selected == true) Node->PaintLinksTo(C);
}
for(node=0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->Selected == true) Node->PaintTo(C);
}
}
//----------
void __fastcall jTree::DragTo(int Ox, int Oy)
{
int node;
jNode *Node;
for(node=0; node < Nodes->Count; node++)
{
Node = (jNode*)Nodes->Items[node];
if(Node->Selected == true)
{
Node->Pos.x = Node->Pos.x + Ox;
Node->Pos.y = Node->Pos.y + Oy;
}
}
}
//----------
void __fastcall jTree::Delete(jNode *Target)
{
int node;
for(node=0; node < Nodes->Count; node++)
{
((jNode*)Nodes->Items[node])->Unlink(Target);
}
node = Nodes->IndexOf(Target);
if(node != -1) Nodes->Delete(node);
delete Target;
}
//----------
/*
Quelques déclarations en global
*/
Graphics::TBitmap *Bitmap;
TRect SelRect;
TPoint Start;
TPoint End;
/*
Les méthodes de la classe TForm1 (la fiche principale)
*/
//----
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//Deux curseurs personnalisés en ressources que l'on va utiliser
Screen->Cursors[crPen] = LoadCursor(HInstance, "Pen");
Screen->Cursors[crRubber] = LoadCursor(HInstance, "Rubber");
// crArrow et crCross appartiennent au bestiaire de BCB3 donc ...
Bitmap = new Graphics::TBitmap;
//Instance de l'arbre
Tree = new jTree(this);
//s'assurer que EditNode (un TEdit sur la fiche) est invisible
EditNode->Visible = false;
}
//----
void __fastcall TForm1::Quitter1Click(TObject *Sender)
{
Close();
}
//----
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete Bitmap;
Bitmap = NULL;
}
//----
void __fastcall TForm1::FormPaint(TObject *Sender)
{
DrawBitmap();
}
//----
void __fastcall TForm1::FormResize(TObject *Sender)
{
DrawBitmap();
}
//----
void __fastcall TForm1::Arrow1Click(TObject *Sender)
{
Cursor = crArrow; // pour sélectionner ou faire glisser
}
//----
void __fastcall TForm1::Cross1Click(TObject *Sender)
{
Cursor = crCross; // pour insérer des nodes
}
//----
void __fastcall TForm1::Pen1Click(TObject *Sender)
{
Cursor = (TCursor)crPen; // pour lier ou délier deux nodes
}
//----
void __fastcall TForm1::Rubber1Click(TObject *Sender)
{
Cursor = (TCursor)crRubber; // pour supprimer un node
}
//----
void __fastcall TForm1::DrawBitmap()
{
if(Bitmap != NULL)
{
//On dessine sur un bitmap d'arrière plan
int he = ClientHeight;
int we = ClientWidth;
if(Bitmap->Width != we)Bitmap->Width = we;
if(Bitmap->Height != he)Bitmap->Height = he;
TCanvas *C = Bitmap->Canvas;
C->Brush->Style = bsSolid;
C->Brush->Color = Color;
C->FillRect(Rect(0,0,we,he));
if(Tree != NULL) Tree->PaintTo(C);
// On dessine le bitmap sur la form
Canvas->Draw(0,0,Bitmap);
}
}
//----
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
// On quitte immédiatement si l'éditeur des noeuds est visible
if(EditNode->Visible) return;
SelRect = Rect(X,Y,X,Y);
Start = Point(X,Y);
End = Point(X,Y);
SrceNode = Tree->NodeHasFocus(&SelRect);
if(Button == mbRight)
{
if((SrceNode != NULL) && (Cursor == crArrow))
{
//Donc on édite
EditNode->Left = X;
EditNode->Top = Y;
EditNode->Text = SrceNode->Caption;
EditNode->Visible = true;
}
}
else
{
if(SrceNode != NULL)
{
switch(Cursor)
{
case crArrow :
if(SrceNode->Selected == false)
{
if(Shift.Contains(ssShift) == false)
{
Tree->Unselect();
}
SrceNode->Selected = true;
DrawBitmap();
}
OnMouseMove = IsMoving;
OnMouseUp = MovingEnds;
break;
case crPen :
Canvas->Pen->Mode = pmNot;
Canvas->Pen->Style = psDot;
Canvas->Pen->Color = clBlack;
OnMouseMove = IsLinking;
OnMouseUp = LinkingEnds;
break;
case crRubber :
Tree->Delete(SrceNode);
DrawBitmap();
break;
}
}
else
{
switch(Cursor)
{
case crArrow :
if(Shift.Contains(ssShift)==false)
{
Tree->Unselect();
DrawBitmap();
}
Canvas->Brush->Style = bsClear;
Canvas->Pen->Mode = pmNot;
Canvas->Pen->Style = psDot;
Canvas->Pen->Color = clBlack;
OnMouseMove = IsSelecting;
OnMouseUp = SelectingEnds;
break;
case crCross :
Tree->Add(Point(X,Y));
DrawBitmap();
break;
}
}
}
}
//----
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
//
}
//----
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
//
}
//----
/*
Les méthodes qui suivent viennent se rajouter à celles de la classe
TForm1.
Ce sont soit des méthodes simples soit des événementielles liées aux
différentes taches à accomplir à un instant donné et donc suite à
une action de l'utilisateur.
*/
//----
void __fastcall TForm1::IsMoving(TObject *Sender, TShiftState Shift,
int X, int Y)
{
int ox = X - Start.x;
int oy = Y - Start.y;
Tree->DragTo(ox,oy);
DrawBitmap();
Start = Point(X,Y);
}
//----
void __fastcall TForm1::MovingEnds(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
OnMouseMove = FormMouseMove;
OnMouseUp = FormMouseUp;
}
//----
//----
void __fastcall TForm1::DrawSelBox()
{
Canvas->Rectangle(SelRect.Left, SelRect.Top, SelRect.Right, SelRect.Bottom);
}
//----
void __fastcall TForm1::IsSelecting(TObject *Sender, TShiftState Shift,
int X, int Y)
{
DrawSelBox();
SelRect.Right = X;
SelRect.Bottom = Y;
DrawSelBox();
}
//----
void __fastcall TForm1::SelectingEnds(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
DrawSelBox();
if(SelRect.Left > SelRect.Right)
{
int w = SelRect.Left; SelRect.Left = SelRect.Right; SelRect.Right = w;
}
if(SelRect.Top > SelRect.Bottom)
{
int w = SelRect.Top; SelRect.Top = SelRect.Bottom; SelRect.Bottom = w;
}
Tree->Select(&SelRect);
DrawBitmap();
OnMouseMove = FormMouseMove;
OnMouseUp = FormMouseUp;
}
//----
//----
void __fastcall TForm1::DrawLink()
{
Canvas->MoveTo(Start.x, Start.y);
Canvas->LineTo(End.x, End.y);
}
//----
void __fastcall TForm1::IsLinking(TObject *Sender, TShiftState Shift,
int X, int Y)
{
DrawLink();
End = Point(X,Y);
DrawLink();
}
//----
void __fastcall TForm1::LinkingEnds(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
int n;
SelRect=Rect(X,Y,X,Y);
DrawLink();
DestNode = Tree->NodeHasFocus(&SelRect);
if(DestNode != NULL)
{
if(Shift.Contains(ssAlt))
{
// >>>>>>>>> Suppression d'un lien <<<<<<<<<<<<
n = DestNode->Targets->IndexOf(SrceNode);
if(n != -1) DestNode->Targets->Delete(n);
}
else
{
// >>>>>>>>> Créaton d'un lien <<<<<<<<<<<<<<
DestNode->LinkTo(SrceNode);
}
DrawBitmap();
}
OnMouseMove = FormMouseMove;
OnMouseUp = FormMouseUp;
}
//-----
void __fastcall TForm1::EditNodeKeyPress(TObject *Sender, char &Key)
{
if(Key == VK_RETURN)
{
SrceNode->Caption = EditNode->Text;
// rend la form à nouveau active
EditNode->Visible = false;
// on affiche le résultat
DrawBitmap();
// rien de plus à faire
Key = 0;
}
}
//---- |
Partager