github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI/fields/ids.go (about) 1 package fields 2 3 import ( 4 "fmt" 5 6 dm "github.com/benoitkugler/goACVE/server/core/datamodel" 7 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 8 9 "github.com/benoitkugler/goACVE/client/GUI/lists" 10 "github.com/therecipe/qt/widgets" 11 ) 12 13 type BoutonId struct { 14 *widgets.QPushButton 15 16 // simple rd.Id ou, quand il y a besoin 17 // de plusieurs types, des rd.Idxxx spécifiques 18 id rd.IId 19 20 idToString func(id rd.IId) string // appellée avec id != nil 21 newDialog func() *lists.RechercheId 22 23 DataChanged func() 24 } 25 26 func newBoutonId(editable bool) *BoutonId { 27 b := BoutonId{QPushButton: widgets.NewQPushButton(nil)} 28 b.ConnectClicked(func(checked bool) { 29 dialog := b.newDialog() 30 dialog.Exec() 31 if dialog.Out != nil { 32 b.SetData(dialog.Out) 33 } 34 if b.DataChanged != nil { 35 b.DataChanged() 36 } 37 }) 38 b.updateText() 39 b.SetEnabled(editable) 40 return &b 41 } 42 43 func (b BoutonId) updateText() { 44 id := b.GetData() 45 if id == nil { 46 b.SetText("...") 47 } else { 48 b.SetText(b.idToString(id)) 49 } 50 } 51 52 func (b BoutonId) GetData() rd.IId { return b.id } 53 54 func (b *BoutonId) SetData(id rd.IId) { 55 b.id = id 56 b.updateText() 57 } 58 59 func NewBoutonIdPersonne(editable bool, base *dm.BaseLocale, withOrganismes bool) *BoutonId { 60 b := newBoutonId(editable) 61 b.newDialog = func() *lists.RechercheId { 62 return lists.NewRecherchePersonne(base, withOrganismes) 63 } 64 b.idToString = func(id rd.IId) string { 65 switch id := id.(type) { 66 // quand withOrganisme est faux, le bouton est implicitement pour les personnes 67 // et Id peut être utilisé 68 case rd.IdPersonne, rd.Id: 69 return base.Personnes[id.Int64()].NomPrenom().String() 70 case rd.IdOrganisme: 71 return base.Organismes[id.Int64()].Nom.String() 72 default: 73 return "" 74 } 75 } 76 return b 77 } 78 79 func NewBoutonIdAide(editable bool, base *dm.BaseLocale) *BoutonId { 80 b := newBoutonId(editable) 81 b.newDialog = func() *lists.RechercheId { 82 return lists.NewRechercheAide(base) 83 } 84 b.idToString = func(id rd.IId) string { 85 return base.NewAide(id.Int64()).Label() 86 } 87 return b 88 } 89 90 func NewBoutonIdStructureaide(editable bool, base *dm.BaseLocale) *BoutonId { 91 b := newBoutonId(editable) 92 b.newDialog = func() *lists.RechercheId { 93 return lists.NewRechercheStructureaide(base) 94 } 95 b.idToString = func(id rd.IId) string { 96 return string(base.Structureaides[id.Int64()].Nom) 97 } 98 return b 99 } 100 101 func NewBoutonIdParticipant(editable bool, base *dm.BaseLocale) *BoutonId { 102 b := newBoutonId(editable) 103 b.newDialog = func() *lists.RechercheId { 104 return lists.NewRechercheParticipant(base) 105 } 106 b.idToString = func(id rd.IId) string { 107 part := base.NewParticipant(id.Int64()) 108 pers := part.GetPersonne().RawData().NomPrenom() 109 camp := part.GetCamp().RawData().Label() 110 return fmt.Sprintf("%s ; %s", pers, camp) 111 } 112 return b 113 } 114 115 func NewBoutonIdFacture(editable bool, base *dm.BaseLocale) *BoutonId { 116 b := newBoutonId(editable) 117 b.newDialog = func() *lists.RechercheId { 118 return lists.NewRechercheFacture(base) 119 } 120 b.idToString = func(id rd.IId) string { 121 ac := base.NewFacture(id.Int64()) 122 _, camps := ac.Camps(nil) 123 pers := ac.GetPersonne().RawData().NomPrenom() 124 return fmt.Sprintf("%s ; %s", pers, camps) 125 } 126 return b 127 } 128 129 func NewBoutonIdCamp(editable bool, base *dm.BaseLocale) *BoutonId { 130 b := newBoutonId(editable) 131 b.newDialog = func() *lists.RechercheId { 132 return lists.NewRechercheCamp(base) 133 } 134 b.idToString = func(id rd.IId) string { 135 return base.Camps[id.Int64()].Label().String() 136 } 137 return b 138 } 139 140 func NewBoutonIdGroupe(editable bool, base *dm.BaseLocale) *BoutonId { 141 b := newBoutonId(editable) 142 b.newDialog = func() *lists.RechercheId { 143 return lists.NewRechercheGroupe(base) 144 } 145 b.idToString = func(id rd.IId) string { 146 return base.NewGroupe(id.Int64()).Label().String() 147 } 148 return b 149 }