Changeset 103 for trunk/Forms/UFormContact.pas
- Timestamp:
- Feb 9, 2022, 3:51:26 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormContact.pas
r102 r103 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 9 ComCtrls, ActnList, Menus, ExtCtrls, ExtDlgs, Buttons, UContact, LCLIntf, 10 UFormProperties, DateUtils{$IFDEF LCLGTK2}, Gtk2Globals{$ENDIF} ;10 UFormProperties, DateUtils{$IFDEF LCLGTK2}, Gtk2Globals{$ENDIF}, UContactImage; 11 11 12 12 type … … 15 15 16 16 TFormContact = class(TForm) 17 APhotoSetUrl: TAction; 18 APhotoShow: TAction; 17 19 APhotoClear: TAction; 18 20 APhotoSave: TAction; … … 172 174 MenuItem2: TMenuItem; 173 175 MenuItem3: TMenuItem; 176 MenuItem4: TMenuItem; 177 MenuItem5: TMenuItem; 174 178 OpenPictureDialog1: TOpenPictureDialog; 175 179 PageControlContact: TPageControl; … … 194 198 procedure APhotoLoadExecute(Sender: TObject); 195 199 procedure APhotoSaveExecute(Sender: TObject); 200 procedure APhotoSetUrlExecute(Sender: TObject); 201 procedure APhotoShowExecute(Sender: TObject); 196 202 procedure ButtonHomeAddressShowClick(Sender: TObject); 197 203 procedure ButtonNextClick(Sender: TObject); 198 204 procedure ButtonPreviousClick(Sender: TObject); 199 205 procedure ButtonWorkAddressShowClick(Sender: TObject); 206 procedure EditFullNameChange(Sender: TObject); 200 207 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 201 208 procedure FormCreate(Sender: TObject); 202 209 procedure FormDestroy(Sender: TObject); 203 210 procedure FormShow(Sender: TObject); 211 procedure ImagePhotoClick(Sender: TObject); 204 212 procedure SpeedButtonHomeWebClick(Sender: TObject); 205 213 procedure SpeedButtonAniversaryClick(Sender: TObject); … … 224 232 procedure TabSheetWorkShow(Sender: TObject); 225 233 private 226 FProfilePhotoActive: Boolean; 227 FProfilePhotoLoaded: Boolean; 228 FProfilePhotoModified: Boolean; 229 procedure SetProfilePhotoActive(AValue: Boolean); 234 FPhoto: TContactImage; 235 procedure PhotoChange(Sender: TObject); 230 236 private 231 237 FContact: TContact; … … 235 241 procedure SetContact(AValue: TContact); 236 242 procedure ReloadAllPropertiesTab; 237 property ProfilePhotoActive: Boolean read FProfilePhotoActive238 write SetProfilePhotoActive;239 243 public 240 244 procedure UpdateInterface; … … 253 257 254 258 uses 255 UCore, UCommon; 259 UCore, UCommon, UFormImage; 260 261 resourcestring 262 SContact = 'Contact'; 263 SPhotoUrl = 'Photo URL'; 264 SPhotoUrlQuery = 'Enter URL for profile photo'; 256 265 257 266 function DateToISO(Date: TDateTime): string; … … 274 283 275 284 {$IF FPC_FULLVERSION<30200} 276 function TryISOStrToDate(const aString: string; out outDate: TDateTime): Boolean;285 function TryISOStrToDate(const aString: string; out OutDate: TDateTime): Boolean; 277 286 var 278 287 xYear, xMonth, xDay: LongInt; … … 280 289 case Length(aString) of 281 290 8: Result := 282 283 284 285 TryEncodeDate(xYear, xMonth, xDay, outDate);291 TryStrToInt(Copy(aString, 1, 4), xYear) and 292 TryStrToInt(Copy(aString, 5, 2), xMonth) and 293 TryStrToInt(Copy(aString, 7, 2), xDay) and 294 TryEncodeDate(xYear, xMonth, xDay, OutDate); 286 295 10: Result := 287 288 289 290 TryEncodeDate(xYear, xMonth, xDay, outDate);296 TryStrToInt(Copy(aString, 1, 4), xYear) and 297 TryStrToInt(Copy(aString, 6, 2), xMonth) and 298 TryStrToInt(Copy(aString, 9, 2), xDay) and 299 TryEncodeDate(xYear, xMonth, xDay, OutDate); 291 300 else 292 301 Result := False; 293 302 end; 294 303 if not Result then 295 outDate := 0;304 OutDate := 0; 296 305 end; 297 306 {$ENDIF} … … 311 320 FormProperties.Show; 312 321 313 FProfilePhotoLoaded := False; 314 315 // Force to load default profile image 316 ProfilePhotoActive := True; 317 ProfilePhotoActive := False; 322 PhotoChange(nil); 318 323 319 324 PageControlContact.TabIndex := Core.LastContactTabIndex; 320 325 UpdateInterface; 326 end; 327 328 procedure TFormContact.ImagePhotoClick(Sender: TObject); 329 begin 330 APhotoShow.Execute; 321 331 end; 322 332 … … 417 427 418 428 procedure TFormContact.TabSheetGeneralHide(Sender: TObject); 419 var420 Photo: string;421 PhotoProperty: TContactProperty;422 Stream: TMemoryStream;423 JpegImage: TJpegImage;424 GifImage: TGIFImage;425 PngImage: TPortableNetworkGraphic;426 429 begin 427 430 Contact.Fields[cfFullName] := EditFullName.Text; … … 443 446 Contact.Fields[cfCategories] := EditCategories.Text; 444 447 445 // Photo 446 if FProfilePhotoModified then begin 447 if ProfilePhotoActive then begin 448 PhotoProperty := Contact.GetProperty(cfPhoto); 449 if not Assigned(PhotoProperty) then begin 450 PhotoProperty := TContactProperty.Create; 451 PhotoProperty.Name := 'PHOTO'; 452 PhotoProperty.Attributes.DelimitedText := 'JPEG'; 453 Contact.Properties.Add(PhotoProperty); 454 end; 455 PhotoProperty.Encoding := 'BASE64'; 456 Stream := TMemoryStream.Create; 457 try 458 if PhotoProperty.Attributes.IndexOf('JPEG') <> -1 then begin 459 JpegImage := TJPEGImage.Create; 460 try 461 try 462 JpegImage.SetSize(ImagePhoto.Picture.Bitmap.Width, ImagePhoto.Picture.Bitmap.Height); 463 JpegImage.Canvas.Draw(0, 0, ImagePhoto.Picture.Bitmap); 464 JpegImage.SaveToStream(Stream); 465 except 466 end; 467 finally 468 JpegImage.Free; 469 end; 470 end else 471 if PhotoProperty.Attributes.IndexOf('PNG') <> -1 then begin 472 PngImage := TPortableNetworkGraphic.Create; 473 try 474 try 475 PngImage.SetSize(ImagePhoto.Picture.Bitmap.Width, ImagePhoto.Picture.Bitmap.Height); 476 PngImage.Canvas.Draw(0, 0, ImagePhoto.Picture.Bitmap); 477 PngImage.SaveToStream(Stream); 478 except 479 end; 480 finally 481 PngImage.Free; 482 end; 483 end else 484 if PhotoProperty.Attributes.IndexOf('GIF') <> -1 then begin 485 GifImage := TGIFImage.Create; 486 try 487 try 488 GifImage.SetSize(ImagePhoto.Picture.Bitmap.Width, ImagePhoto.Picture.Bitmap.Height); 489 GifImage.Canvas.Draw(0, 0, ImagePhoto.Picture.Bitmap); 490 GifImage.SaveToStream(Stream); 491 except 492 end; 493 finally 494 GifImage.Free; 495 end; 496 end else begin 497 try 498 ImagePhoto.Picture.SaveToStream(Stream); 499 except 500 end; 501 end; 502 503 SetLength(Photo, Stream.Size); 504 Stream.Position := 0; 505 Stream.Read(Photo[1], Length(Photo)); 506 Contact.Fields[cfPhoto] := Photo; 507 finally 508 Stream.Free; 509 end; 510 end else begin 511 PhotoProperty := Contact.GetProperty(cfPhoto); 512 if Assigned(PhotoProperty) then 513 Contact.Properties.Remove(PhotoProperty); 514 end; 515 FProfilePhotoModified := False; 516 end; 448 FPhoto.Contact := Contact; 449 FPhoto.Save; 517 450 518 451 ReloadAllPropertiesTab; … … 520 453 521 454 procedure TFormContact.TabSheetGeneralShow(Sender: TObject); 522 var523 Photo: string;524 JpegImage: TJpegImage;525 PngImage: TPortableNetworkGraphic;526 GifImage: TGIFImage;527 Stream: TMemoryStream;528 PhotoProperty: TContactProperty;529 455 begin 530 456 EditFullName.Text := Contact.Fields[cfFullName]; … … 546 472 EditCategories.Text := Contact.Fields[cfCategories]; 547 473 548 // Photo 549 PhotoProperty := Contact.GetProperty(cfPhoto); 550 if not FProfilePhotoLoaded then begin 551 if Assigned(PhotoProperty) then begin 552 FProfilePhotoLoaded := True; 553 FProfilePhotoModified := True; 554 Photo := Contact.Fields[cfPhoto]; 555 if (Photo <> '') and (PhotoProperty.Encoding <> '') then begin 556 Stream := TMemoryStream.Create; 557 try 558 Stream.Write(Photo[1], Length(Photo)); 559 Stream.Position := 0; 560 if (PhotoProperty.Attributes.IndexOf('JPEG') <> -1) or 561 (PhotoProperty.Attributes.IndexOf('jpeg') <> -1) then begin 562 JpegImage := TJPEGImage.Create; 563 try 564 try 565 JpegImage.LoadFromStream(Stream); 566 with ImagePhoto.Picture.Bitmap do begin 567 PixelFormat := pf24bit; 568 SetSize(JpegImage.Width, JpegImage.Height); 569 Canvas.Draw(0, 0, JpegImage); 570 end; 571 ProfilePhotoActive := True; 572 except 573 ProfilePhotoActive := False; 574 end; 575 finally 576 JpegImage.Free; 577 end; 578 end else 579 if (PhotoProperty.Attributes.IndexOf('PNG') <> -1) or 580 (PhotoProperty.Attributes.IndexOf('png') <> -1) then begin 581 PngImage := TPortableNetworkGraphic.Create; 582 try 583 try 584 PngImage.LoadFromStream(Stream); 585 with ImagePhoto.Picture.Bitmap do begin 586 PixelFormat := pf24bit; 587 SetSize(PngImage.Width, PngImage.Height); 588 Canvas.Draw(0, 0, PngImage); 589 end; 590 ProfilePhotoActive := True; 591 except 592 ProfilePhotoActive := False; 593 end; 594 finally 595 PngImage.Free; 596 end; 597 end else 598 if (PhotoProperty.Attributes.IndexOf('GIF') <> -1) or 599 (PhotoProperty.Attributes.IndexOf('gif') <> -1) then begin 600 GifImage := TGIFImage.Create; 601 try 602 try 603 GifImage.LoadFromStream(Stream); 604 with ImagePhoto.Picture.Bitmap do begin 605 PixelFormat := pf24bit; 606 SetSize(GifImage.Width, GifImage.Height); 607 Canvas.Draw(0, 0, GifImage); 608 end; 609 ProfilePhotoActive := True; 610 except 611 ProfilePhotoActive := False; 612 end; 613 finally 614 GifImage.Free; 615 end; 616 end else begin 617 try 618 ImagePhoto.Picture.LoadFromStream(Stream); 619 ProfilePhotoActive := True; 620 except 621 ProfilePhotoActive := False; 622 end; 623 end; 624 finally 625 Stream.Free; 626 end; 627 end else ProfilePhotoActive := False; 628 end else ProfilePhotoActive := False; 629 end; 474 FPhoto.Contact := Contact; 475 FPhoto.Load; 630 476 end; 631 477 … … 750 596 end; 751 597 752 procedure TFormContact.SetProfilePhotoActive(AValue: Boolean); 753 begin 754 if FProfilePhotoActive = AValue then Exit; 755 FProfilePhotoActive := AValue; 756 if not FProfilePhotoActive then begin 757 ImagePhoto.Picture.Assign(Core.GetProfileImage.Picture); 758 end; 598 procedure TFormContact.PhotoChange(Sender: TObject); 599 begin 600 if FPhoto.Used and (FPhoto.Url = '') then 601 ImagePhoto.Picture.Bitmap.Assign(FPhoto.Bitmap) 602 else ImagePhoto.Picture.Assign(Core.GetProfileImage.Picture); 759 603 UpdateInterface; 760 604 end; … … 791 635 procedure TFormContact.APhotoLoadExecute(Sender: TObject); 792 636 begin 637 OpenPictureDialog1.FileName := Core.LastPhotoFileName; 793 638 if OpenPictureDialog1.Execute then begin 794 ImagePhoto.Picture.LoadFromFile(OpenPictureDialog1.FileName); 795 FProfilePhotoModified := True; 796 FProfilePhotoLoaded := True; 797 ProfilePhotoActive := True; 639 FPhoto.LoadFromFile(OpenPictureDialog1.FileName); 640 Core.LastPhotoFileName := OpenPictureDialog1.FileName; 798 641 end; 799 642 end; … … 801 644 procedure TFormContact.APhotoClearExecute(Sender: TObject); 802 645 begin 803 FProfilePhotoModified := True; 804 ProfilePhotoActive := False; 646 FPhoto.Clear; 805 647 end; 806 648 807 649 procedure TFormContact.APhotoSaveExecute(Sender: TObject); 808 650 begin 651 SavePictureDialog1.FileName := Core.LastPhotoFileName; 809 652 if SavePictureDialog1.Execute then begin 810 653 ImagePhoto.Picture.SaveToFile(SavePictureDialog1.FileName); 654 Core.LastPhotoFileName := SavePictureDialog1.FileName; 655 end; 656 end; 657 658 procedure TFormContact.APhotoSetUrlExecute(Sender: TObject); 659 begin 660 FPhoto.Url := InputBox(SPhotoUrl, SPhotoUrlQuery, FPhoto.Url); 661 end; 662 663 procedure TFormContact.APhotoShowExecute(Sender: TObject); 664 begin 665 with TFormImage.Create(nil) do 666 try 667 Image.Assign(FPhoto); 668 if ShowModal = mrOK then begin 669 FPhoto.Assign(Image); 670 UpdateInterface; 671 end; 672 finally 673 Free; 811 674 end; 812 675 end; … … 844 707 end; 845 708 709 procedure TFormContact.EditFullNameChange(Sender: TObject); 710 begin 711 UpdateInterface; 712 end; 713 846 714 procedure TFormContact.FormCreate(Sender: TObject); 847 715 begin … … 850 718 FContact := nil; 851 719 FormProperties := TFormProperties.Create(nil); 720 FPhoto := TContactImage.Create; 721 FPhoto.FieldIndex := cfPhoto; 722 FPhoto.OnChange := PhotoChange; 852 723 end; 853 724 854 725 procedure TFormContact.FormDestroy(Sender: TObject); 855 726 begin 727 FreeAndNil(FPhoto); 856 728 FreeAndNil(FormProperties); 857 729 end; … … 859 731 procedure TFormContact.UpdateInterface; 860 732 begin 861 APhotoSave.Enabled := ProfilePhotoActive; 862 APhotoClear.Enabled := ProfilePhotoActive; 733 Caption := EditFullName.Text + ' - ' + SContact; 734 APhotoSave.Enabled := FPhoto.Used; 735 APhotoClear.Enabled := FPhoto.Used; 863 736 //ButtonNext.Enabled := Assigned(FOnGetNext) and Assigned(FOnGetNext(Contact)); 864 737 //ButtonPrevious.Enabled := Assigned(FOnGetPrevious) and Assigned(FOnGetPrevious(Contact));
Note:
See TracChangeset
for help on using the changeset viewer.