Canvas et Json comment écrire une bdd dans un canvas

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Canvas et Json comment écrire une bdd dans un canvas

Message par Micoute »

Joyeuses fêtes à tous,

j'aimerais me créer un carnet d'adresses dans un PanelGadget() contenant 2 canvas conteneurs dont chacun est une page du carnet d'adresses.
La question que je pose est comment afficher un contact dans la bonne page ? Je vais bien sûr trier ma liste chaînée structurée.
Je pense aussi que je vais ajouter des gadgets, c'est pour cette raison que j'ai choisi ce type de canvas.
Je vous remercie par avance de l'aide bienvillante que vous allez m'apporter.

Code : Tout sélectionner

;Carnet d'adresses

Enumeration
  #FichierJSON
EndEnumeration

Enumeration
  #Fenetre_principale
EndEnumeration

Enumeration gadgets
  #Onglet_principal
  #Cnv_AB1
  #Cnv_AB2
  #Cnv_CD1
  #Cnv_CD2
  #Cnv_EF1
  #Cnv_EF2
  #Cnv_GH1
  #Cnv_GH2
  #Cnv_IJ1
  #Cnv_IJ2
  #Cnv_KL1
  #Cnv_KL2
  #Cnv_MN1
  #Cnv_MN2
  #Cnv_OP1
  #Cnv_OP2
  #Cnv_QR1
  #Cnv_QR2
  #Cnv_ST1
  #Cnv_ST2
  #Cnv_UV1
  #Cnv_UV2
  #Cnv_WX1
  #Cnv_WX2
  #Cnv_YZ1
  #Cnv_YZ2
  
  #Nom         ;Saisie du nom du contact
  #Prenom      ;Saisie du prénom du contact
  #Telephone_F ;Saisie du téléphone fixe d'un contact
  #Telephone_M ;Saisie du téléphone mobile d'un contact
  #Adresse     ;Saisie de l'adresse d'un contact
  #CodePostal  ;Saisie du code postal d'un contact
  #Ville       ;Saisie de la ville d'un contact
EndEnumeration

Enumeration Polices
  #Police_Lettrines
  #Police_Onglets
EndEnumeration  

;Structure d'un contact
Structure Contact
  Nom.s
  Prenom.s
  Tel_F.s
  Tel_M.s
  Adresse.s
  Code_Postal.s
  Ville.s
EndStructure

Global Evenement, NewList Contacts.Contact(), Fichier_Contacts.s = "D:\Programmation\Donnees\contacts.json"
Global OldProc

LoadFont(#Police_Lettrines, "Times New Roman", 30, #PB_Font_Bold)
LoadFont(#Police_Onglets, "Arial", 30, #PB_Font_Bold)

;Chargement des contacts existants
Procedure Charger_Contacts()
  If ReadFile(#FichierJSON, Fichier_Contacts)
    CloseFile(#FichierJSON)
    
    ;Lecture du fichier JSON
    LoadJSON(#FichierJSON, Fichier_Contacts, #PB_JSON_NoCase)
    
    ;Extraction de la chaine JSON vers la liste chainée Contacts
    ExtractJSONList(JSONValue(#FichierJSON), Contacts())
    
  EndIf
EndProcedure

Procedure maGestionOnglets(hwnd, msg, wParam, lParam) 
  Resultat = CallWindowProc_(OldProc, hwnd, msg, wParam, lParam) 
  
  Select msg 
    Case #WM_DRAWITEM 
      
      *AffichageOnglet.DRAWITEMSTRUCT = lParam 
      If *AffichageOnglet\CtlType = #ODT_TAB 
        
        Gadget = GetProp_(*AffichageOnglet\hwnditem, "PB_ID")
        
        If *AffichageOnglet\itemID = GetGadgetState(Gadget)
          
          Couleur = #Red
          
          TexteOnglet$ = GetGadgetItemText(Gadget, *AffichageOnglet\itemID)
          FillRect_(*AffichageOnglet\hdc, *AffichageOnglet\rcItem, defaultBrush)
          SetTextColor_(*AffichageOnglet\hdc, Couleur) 
          SetBkMode_(*AffichageOnglet\hdc, #TRANSPARENT) 
          DrawText_(*AffichageOnglet\hdc,TexteOnglet$, Len(TexteOnglet$), *AffichageOnglet\rcItem, #DT_CENTER | #DT_SINGLELINE | #DT_VCENTER | #DT_NOCLIP) 
        Else 
          TexteOnglet$ = GetGadgetItemText(Gadget, *AffichageOnglet\itemID)
          FillRect_(*AffichageOnglet\hdc, *AffichageOnglet\rcItem, defaultBrush)
          SetTextColor_(*AffichageOnglet\hdc, $C0C0C0) 
          SetBkMode_(*AffichageOnglet\hdc, #TRANSPARENT) 
          DrawText_(*AffichageOnglet\hdc,TexteOnglet$, Len(TexteOnglet$), *AffichageOnglet\rcItem, #DT_CENTER | #DT_SINGLELINE | #DT_VCENTER | #DT_NOCLIP) 
        EndIf 
      EndIf 
  EndSelect 
  ProcedureReturn  Resultat
EndProcedure

Procedure DessinerPage(Lettres.s,Cnv1,Cnv2)
  ; Canvas 1
  CanvasGadget(Cnv1, 10, 10, 820, 630, #PB_Canvas_Container)
  StartDrawing(CanvasOutput(Cnv1))
  DrawingFont(FontID(#Police_Lettrines))
  For i = 0 To 3
    LineXY(0,i,820,i,0)
    LineXY(0,i+627,820,i+627,0)
    LineXY(i, 0, i, 630, 0)
    LineXY(i+817, 0, i+817, 820, 0)
  Next  i
  DrawText(20, 10, Left(Lettres,1), #Black, #White)
  LineXY(60, 45, 780, 45, #Black)
  StopDrawing()
  Canvas_pair = CanvasGadget(#PB_Any, 20, 60, 780, 550)
  StartDrawing(CanvasOutput(Canvas_pair))
  Box(0, 0, GadgetWidth(Canvas_pair), GadgetHeight(Canvas_pair), $EEFFED)
  StopDrawing()
  CloseGadgetList()
  
  ;Canvas 2
  CanvasGadget(Cnv2, 840, 10, 820, 630, #PB_Canvas_Container)
  StartDrawing(CanvasOutput(Cnv2))
  DrawingFont(FontID(#Police_Lettrines))
  For i = 0 To 3
    LineXY(0,i,820,i,0)
    LineXY(0,i+627,820,i+627,0)
    LineXY(i, 0, i, 630, 0)
    LineXY(i+817, 0, i+817, 820, 0)
  Next  i
  DrawText(20, 10, Right(Lettres,1), #Black, #White)
  LineXY(60, 45, 780, 45, #Black)
  StopDrawing()
  Canvas_impair = CanvasGadget(#PB_Any, 20, 60, 780, 550)
  StartDrawing(CanvasOutput(Canvas_impair))
  Box(0, 0, GadgetWidth(Canvas_impair), GadgetHeight(Canvas_impair), $EEFFED)
  StopDrawing()
  CloseGadgetList()
EndProcedure

Procedure Programme_principal()
  If OpenWindow(#Fenetre_principale, 0, 0, 1700, 730, "Carnet d'adresses", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
    PanelGadget(#Onglet_principal, 10, 10, 1680, 710)
    For i = 1 To 26 Step 2
      AddGadgetItem(#Onglet_principal, -1, Chr(i + 64) + Chr(i + 65))
      DessinerPage(Chr(i + 64) + Chr(i + 65), i, i + 1)
    Next i
    SetWindowLong_(GadgetID(#Onglet_principal), #GWL_STYLE, GetWindowLong_(GadgetID(#Onglet_principal), #GWL_STYLE) | #TCS_OWNERDRAWFIXED) 
    OldProc = SetWindowLong_(GetParent_(GadgetID(#Onglet_principal)), #GWL_WNDPROC, @maGestionOnglets())
    CloseGadgetList()
    SetGadgetFont(#Onglet_principal, FontID(#Police_Onglets))
  EndIf
EndProcedure

Charger_Contacts()
Programme_principal()

;- boucle
Repeat
  Evenement= WaitWindowEvent()
  Select Evenement
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Onglet_principal
      EndSelect
      
    Case #PB_Event_CloseWindow
      Select EventWindow()
        Case #Fenetre_principale
          CloseWindow(#Fenetre_principale)
          Break
      EndSelect
  EndSelect
ForEver
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
microdevweb
Messages : 1800
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: Canvas et Json comment écrire une bdd dans un canvas

Message par microdevweb »

Bonjour Micoute,
voici un exemple

Code : Tout sélectionner

;***************************************************************************
; Exemple de constacts dessiner dans une canvas
; AUTHOR          : MicrodevWeb
; DESIGNED WIDTH  : PB 5.61 x64
; REQUIERED       : NONE
; DATE            : 2017/12/24  merry christmas :)
;***************************************************************************
EnableExplicit
#MAIN_FORM   = 0
#MAIN_CANVAS = 0
#SCROLL_VERCTICAL = 1
#PEOPLE_FONT = 0
Structure people
  firstName.s
  lastName.s
EndStructure
Global NewList myPeople.people()
LoadFont(#PEOPLE_FONT,"arial",12,#PB_Font_HighQuality)

Declare EventToScrool()
Declare drawPeople()

;{ numberPeopleBySheet
;*************************
; retourne le nombre de contacts affichable par page
;}
Procedure numberPeopleBySheet()
  Protected n
  StartVectorDrawing(CanvasVectorOutput(#MAIN_CANVAS))
  VectorFont(FontID(#PEOPLE_FONT))
  n = Round(((GadgetHeight(#MAIN_CANVAS) -10) / (VectorTextHeight("W") + 10)),#PB_Round_Down)
  StopVectorDrawing()
  ProcedureReturn n
EndProcedure

;{ OpenForm
;*************************
; Création de la fenêtre principale
;}
Procedure OpenForm()  
  ; création de la fenêtre
  OpenWindow(#MAIN_FORM,0,0,800,600,"Gestion de contact",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ; création du canvas
  CanvasGadget(#MAIN_CANVAS,0,0,800,600,#PB_Canvas_Container)
  ; création de l'ascensseur pour l'affichage des contact
  ScrollBarGadget(#SCROLL_VERCTICAL,780,0,20,600,1,ListSize(myPeople())-12,10,#PB_ScrollBar_Vertical)
  CloseGadgetList()
  BindGadgetEvent(#SCROLL_VERCTICAL,@EventToScrool())
  drawPeople()
EndProcedure

;{ makePeople
;*************************
; création de quelques contacts pour le teste
;}
Procedure makePeople()
  Protected i
  For i=1 To 201
    AddElement(myPeople())
    With myPeople()
      \firstName = "Firstname " + Str(i)
      \lastName = "Lastname " + Str(i)
    EndWith
  Next
EndProcedure
;{ drawPeople
;*************************
; dessin des contacts dans le canvas
;}
Procedure drawPeople()
  Protected x = 10 ,y = 10,numberPeople = numberPeopleBySheet(),i
  ; début du dessin
  StartVectorDrawing(CanvasVectorOutput(#MAIN_CANVAS))
  ; couleur de fond
  VectorSourceColor($FFCDFAFF)
  FillVectorOutput()
  ; je défini la police
  VectorFont(FontID(#PEOPLE_FONT))
  ; je défini la couleur
  VectorSourceColor($FF808080)
  ; on débute à la valeur de l'ascensseur
  For i = 0 To numberPeople
    If SelectElement(myPeople(),(GetGadgetState(#SCROLL_VERCTICAL)+i)-1)
      MovePathCursor(x,y)
      With myPeople()
        DrawVectorText(\firstName+" "+\lastName)
      EndWith
    EndIf
    y + VectorTextHeight("W") + 10
  Next
  StopVectorDrawing()
EndProcedure

;{ EventToCrool
;*************************
; gestion des événement de l'ascensseur
;}
Procedure EventToScrool()
  drawPeople()
EndProcedure

makePeople()

OpenForm()

Repeat
  WaitWindowEvent()
Until Event()=#PB_Event_CloseWindow

Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Canvas et Json comment écrire une bdd dans un canvas

Message par Micoute »

Quelle ne fut pas ma surprise en m'apercevant qu'il y avait déjà une réponse, je te remercie énormément et bien je te souhaite par la même occasion de passer de merveilleuses fêtes de fin d'année, ainsi qu'à tous les PureBasiciens.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Répondre