github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI/fields/strings.go (about) 1 package fields 2 3 import ( 4 "fmt" 5 "sort" 6 "strconv" 7 8 dm "github.com/benoitkugler/goACVE/server/core/datamodel" 9 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 10 11 "github.com/therecipe/qt/core" 12 "github.com/therecipe/qt/widgets" 13 ) 14 15 var ( 16 departements []string 17 modePaiements []Choice 18 sexes []Choice 19 diplomes []Choice 20 appros []Choice 21 rangMembreAssos []Choice 22 RoleEquipes []Choice 23 buss []Choice 24 materielSkiNeeds []Choice 25 materielSkiModes []Choice 26 schemaPaiements []Choice 27 semaines []Choice 28 statutAttente = []ChoiceInt{ 29 {Field: int(rd.Inscrit), Label: rd.StatutAttenteLabels[rd.Inscrit]}, 30 {}, // séparateur 31 {Field: int(rd.AttenteReponse), Label: rd.StatutAttenteLabels[rd.AttenteReponse]}, 32 {Field: int(rd.Attente), Label: rd.StatutAttenteLabels[rd.Attente]}, 33 {}, // séparateur 34 {Field: int(rd.Refuse), Label: rd.StatutAttenteLabels[rd.Refuse]}, 35 } 36 ) 37 38 func sortLabelSlice(l []Choice) { 39 sort.Slice(l, func(i, j int) bool { 40 return l[i].Label < l[j].Label 41 }) 42 } 43 44 func sortFieldSlice(l []Choice) { 45 sort.Slice(l, func(i, j int) bool { 46 return l[i].Field < l[j].Field 47 }) 48 } 49 50 func init() { 51 for key, nom := range rd.DEPARTEMENTS { 52 prop1 := fmt.Sprintf("%s - %s", nom, string(key)) 53 prop2 := fmt.Sprintf("%s - %s", string(key), nom) 54 departements = append(departements, prop1, prop2) 55 } 56 for key, value := range rd.ModePaimentLabels { 57 modePaiements = append(modePaiements, Choice{Field: string(key), Label: value}) 58 } 59 for key, value := range rd.SexeLabels { 60 sexes = append(sexes, Choice{Field: string(key), Label: value}) 61 } 62 for key, value := range rd.DiplomeLabels { 63 diplomes = append(diplomes, Choice{Field: string(key), Label: value}) 64 } 65 for key, value := range rd.ApprofondissementLabels { 66 appros = append(appros, Choice{Field: string(key), Label: value}) 67 } 68 for key, value := range rd.RangMembreAssoLabels { 69 rangMembreAssos = append(rangMembreAssos, Choice{Field: string(key), Label: value}) 70 } 71 for key, value := range rd.RoleLabels { 72 RoleEquipes = append(RoleEquipes, Choice{Field: string(key), Label: value}) 73 } 74 for key, value := range rd.BusLabels { 75 buss = append(buss, Choice{Field: string(key), Label: value}) 76 } 77 for key, value := range rd.MaterielSkiNeed { 78 materielSkiNeeds = append(materielSkiNeeds, Choice{Field: key, Label: value.String()}) 79 } 80 for key, value := range rd.MaterielSkiModes { 81 materielSkiModes = append(materielSkiModes, Choice{Field: key, Label: value}) 82 } 83 for key, value := range rd.SchemaPaiementLabels { 84 schemaPaiements = append(schemaPaiements, Choice{Field: string(key), Label: value}) 85 } 86 for key, value := range rd.SemaineLabels { 87 semaines = append(semaines, Choice{Field: string(key), Label: value}) 88 } 89 sort.Strings(departements) 90 sortLabelSlice(modePaiements) 91 sortLabelSlice(sexes) 92 sortLabelSlice(diplomes) 93 sortFieldSlice(appros) 94 sortFieldSlice(rangMembreAssos) 95 sortLabelSlice(RoleEquipes) 96 sortLabelSlice(buss) 97 sortFieldSlice(materielSkiNeeds) 98 sortFieldSlice(materielSkiModes) 99 sortLabelSlice(schemaPaiements) 100 sortFieldSlice(semaines) 101 } 102 103 type Choice = struct { 104 Field string 105 Label string 106 } 107 108 type ChoiceInt = struct { 109 Field int 110 Label string 111 } 112 113 type MultiLineString struct { 114 *widgets.QPlainTextEdit 115 } 116 117 func NewMultiLineString(editable bool, placeholder string) MultiLineString { 118 w := MultiLineString{widgets.NewQPlainTextEdit(nil)} 119 w.SetPlaceholderText(placeholder) 120 w.SetMinimumWidth(100) 121 w.SetMinimumHeight(50) 122 w.SetReadOnly(!editable) 123 return w 124 } 125 126 func (f MultiLineString) GetData() rd.String { 127 return rd.String(f.ToPlainText()).TrimSpace() 128 } 129 130 func (f MultiLineString) SetData(data rd.String) { 131 f.SetPlainText(string(data)) 132 } 133 134 type String struct { 135 *widgets.QLineEdit 136 } 137 138 func NewString2(editable bool, maxLength uint, completions []string) String { 139 w := String{widgets.NewQLineEdit(nil)} 140 if maxLength > 0 { 141 w.SetMaxLength(int(maxLength)) 142 } 143 if completions != nil { 144 c := widgets.NewQCompleter3(completions, nil) 145 c.SetCaseSensitivity(core.Qt__CaseInsensitive) 146 w.SetCompleter(c) 147 } 148 w.SetReadOnly(!editable) 149 return w 150 } 151 152 func NewString(editable bool) String { 153 return NewString2(editable, 0, nil) 154 } 155 156 func (f String) GetData() rd.String { 157 return rd.String(f.Text()) 158 } 159 160 func (f String) SetData(data rd.String) { 161 f.SetText(string(data)) 162 } 163 164 type Enum struct { 165 *widgets.QComboBox 166 } 167 168 func NewEnum(items []Choice, editable bool) Enum { 169 w := Enum{widgets.NewQComboBox(nil)} 170 w.setChoice(items) 171 w.SetEnabled(editable) 172 return w 173 } 174 175 func (w Enum) setChoice(items []Choice) { 176 currentItem := w.GetData() 177 w.Clear() 178 for _, item := range items { 179 if item.Label == "" { 180 w.InsertSeparator(w.Count()) 181 } else { 182 w.AddItem(item.Label, core.NewQVariant15(item.Field)) 183 } 184 } 185 w.SetData(currentItem) 186 } 187 188 func (w Enum) GetData() string { 189 return w.CurrentData(int(core.Qt__UserRole)).ToString() 190 } 191 192 func (w Enum) SetData(data string) { 193 index := w.FindData(core.NewQVariant15(data), int(core.Qt__UserRole), core.Qt__MatchExactly) 194 w.SetCurrentIndex(index) 195 } 196 197 type enumInt struct { 198 *widgets.QComboBox 199 } 200 201 func newEnumInt(items []ChoiceInt, editable bool) enumInt { 202 w := enumInt{widgets.NewQComboBox(nil)} 203 w.setChoice(items) 204 w.SetEnabled(editable) 205 return w 206 } 207 208 func (w enumInt) setChoice(items []ChoiceInt) { 209 currentItem := w.GetData() 210 w.Clear() 211 for _, item := range items { 212 if item.Label == "" { 213 w.InsertSeparator(w.Count()) 214 } else { 215 w.AddItem(item.Label, core.NewQVariant5(item.Field)) 216 } 217 } 218 w.SetData(currentItem) 219 } 220 221 func (w enumInt) GetData() int { 222 var ok bool 223 out := w.CurrentData(int(core.Qt__UserRole)).ToInt(&ok) 224 if !ok { 225 out = 0 226 } 227 return out 228 } 229 230 func (w enumInt) SetData(data int) { 231 index := w.FindData(core.NewQVariant5(data), int(core.Qt__UserRole), core.Qt__MatchExactly) 232 w.SetCurrentIndex(index) 233 } 234 235 type Departement String 236 237 func NewDepartement(editable bool) Departement { 238 return Departement(NewString2(editable, 0, departements)) 239 } 240 241 func (f Departement) GetData() rd.Departement { 242 return rd.Departement(String(f).GetData()) 243 } 244 245 func (f Departement) SetData(data rd.Departement) { 246 String(f).SetData(rd.String(data)) 247 } 248 249 type ModePaiment Enum 250 251 func NewModePaiment(editable bool) ModePaiment { 252 return ModePaiment(NewEnum(modePaiements, editable)) 253 } 254 255 func (f ModePaiment) GetData() rd.ModePaiment { 256 return rd.ModePaiment(Enum(f).GetData()) 257 } 258 259 func (f ModePaiment) SetData(data rd.ModePaiment) { 260 Enum(f).SetData(string(data)) 261 } 262 263 type Sexe Enum 264 265 func NewSexe(editable bool) Sexe { 266 return Sexe(NewEnum(sexes, editable)) 267 } 268 269 func (f Sexe) GetData() rd.Sexe { 270 return rd.Sexe(Enum(f).GetData()) 271 } 272 273 func (f Sexe) SetData(data rd.Sexe) { 274 Enum(f).SetData(string(data)) 275 } 276 277 type CategorieDocument Enum 278 279 func getChoixCategorieDocument(base *dm.BaseLocale) []Choice { 280 var fields []Choice 281 for _, contrainte := range base.Contraintes { 282 if contrainte.Builtin != "" { 283 fields = append(fields, Choice{Field: string(contrainte.Builtin), Label: contrainte.Nom.String()}) 284 } 285 } 286 sortLabelSlice(fields) 287 return fields 288 } 289 290 func NewCategorieDocument(base *dm.BaseLocale, editable bool) CategorieDocument { 291 fields := getChoixCategorieDocument(base) 292 return CategorieDocument(NewEnum(fields, editable)) 293 } 294 295 // NewOptionnalCategorieDocument ajoute un champ "Indifférent" et 296 // se restreint aux personnes. 297 func NewOptionnalCategorieDocument(base *dm.BaseLocale) CategorieDocument { 298 fields := append([]Choice{{ 299 Field: "", 300 Label: "Indifférent", 301 }}, getChoixCategorieDocument(base)...) 302 return CategorieDocument(NewEnum(fields, true)) 303 } 304 305 func (f CategorieDocument) GetData() rd.BuiltinContrainte { 306 return rd.BuiltinContrainte(Enum(f).GetData()) 307 } 308 309 func (f CategorieDocument) SetData(data rd.BuiltinContrainte) { 310 Enum(f).SetData(string(data)) 311 } 312 313 type Diplome Enum 314 315 func NewDiplome(editable bool) Diplome { 316 return Diplome(NewEnum(diplomes, editable)) 317 } 318 319 func (f Diplome) GetData() rd.Diplome { 320 return rd.Diplome(Enum(f).GetData()) 321 } 322 323 func (f Diplome) SetData(data rd.Diplome) { 324 Enum(f).SetData(string(data)) 325 } 326 327 type Approfondissement Enum 328 329 func NewApprofondissement(editable bool) Approfondissement { 330 return Approfondissement(NewEnum(appros, editable)) 331 } 332 333 func (f Approfondissement) GetData() rd.Approfondissement { 334 return rd.Approfondissement(Enum(f).GetData()) 335 } 336 337 func (f Approfondissement) SetData(data rd.Approfondissement) { 338 Enum(f).SetData(string(data)) 339 } 340 341 type RangMembreAsso Enum 342 343 func NewRangMembreAsso(editable bool, showIndifferent bool) RangMembreAsso { 344 var choices []Choice 345 if showIndifferent { 346 for _, v := range rangMembreAssos { 347 if rd.RangMembreAsso(v.Field) == rd.RMANonMembre { 348 v.Label = "Indifférent" 349 } 350 choices = append(choices, v) 351 } 352 } else { 353 choices = rangMembreAssos 354 } 355 return RangMembreAsso(NewEnum(choices, editable)) 356 } 357 358 func (f RangMembreAsso) GetData() rd.RangMembreAsso { 359 return rd.RangMembreAsso(Enum(f).GetData()) 360 } 361 362 func (f RangMembreAsso) SetData(data rd.RangMembreAsso) { 363 Enum(f).SetData(string(data)) 364 } 365 366 type StatutAttente enumInt 367 368 func NewStatutAttente(editable bool) StatutAttente { 369 return StatutAttente(newEnumInt(statutAttente, editable)) 370 } 371 372 func (f StatutAttente) GetData() rd.StatutAttente { 373 return rd.StatutAttente(enumInt(f).GetData()) 374 } 375 376 func (f StatutAttente) SetData(data rd.StatutAttente) { 377 enumInt(f).SetData(int(data)) 378 } 379 380 type Bus Enum 381 382 func NewBus(editable bool) Bus { 383 return Bus(NewEnum(buss, editable)) 384 } 385 386 func (f Bus) GetData() rd.Bus { 387 return rd.Bus(Enum(f).GetData()) 388 } 389 390 func (f Bus) SetData(data rd.Bus) { 391 Enum(f).SetData(string(data)) 392 } 393 394 type materielSkiNeed Enum 395 396 func newMaterielSkiNeed(editable bool) materielSkiNeed { 397 return materielSkiNeed(NewEnum(materielSkiNeeds, editable)) 398 } 399 400 func (f materielSkiNeed) GetData() string { 401 return Enum(f).GetData() 402 } 403 404 func (f materielSkiNeed) SetData(data string) { 405 Enum(f).SetData(data) 406 } 407 408 type materielSkiMode Enum 409 410 func newMaterielSkiMode(editable bool) materielSkiMode { 411 return materielSkiMode(NewEnum(materielSkiModes, editable)) 412 } 413 414 func (f materielSkiMode) GetData() string { 415 return Enum(f).GetData() 416 } 417 418 func (f materielSkiMode) SetData(data string) { 419 Enum(f).SetData(data) 420 } 421 422 type SchemaPaiement Enum 423 424 func NewSchemaPaiement(editable bool) SchemaPaiement { 425 return SchemaPaiement(NewEnum(schemaPaiements, editable)) 426 } 427 428 func (f SchemaPaiement) GetData() rd.SchemaPaiement { 429 return rd.SchemaPaiement(Enum(f).GetData()) 430 } 431 432 func (f SchemaPaiement) SetData(data rd.SchemaPaiement) { 433 Enum(f).SetData(string(data)) 434 } 435 436 type Semaine Enum 437 438 func NewSemaine(editable bool) Semaine { 439 return Semaine(NewEnum(semaines, editable)) 440 } 441 442 func (f Semaine) GetData() rd.Semaine { 443 return rd.Semaine(Enum(f).GetData()) 444 } 445 446 func (f Semaine) SetData(data rd.Semaine) { 447 Enum(f).SetData(string(data)) 448 } 449 450 type ChoixStatut struct { 451 *widgets.QComboBox 452 } 453 454 func NewChoixStatut(editable bool) ChoixStatut { 455 c := ChoixStatut{widgets.NewQComboBox(nil)} 456 c.SetEnabled(editable) 457 return c 458 } 459 func (f *ChoixStatut) SetChoix(choices []rd.PrixParStatut) { 460 current := f.GetData() 461 f.Clear() 462 f.AddItem("", core.NewQVariant7(0)) 463 for _, statut := range choices { 464 f.AddItem(string(statut.Statut), core.NewQVariant7(statut.Id)) 465 } 466 f.SetData(current) 467 } 468 469 func (f ChoixStatut) GetData() int64 { 470 var err bool 471 return f.CurrentData(int(core.Qt__UserRole)).ToLongLong(&err) 472 } 473 474 func (f ChoixStatut) SetData(data int64) { 475 in := f.FindData(core.NewQVariant7(data), int(core.Qt__UserRole), core.Qt__MatchExactly) 476 f.SetCurrentIndex(in) 477 } 478 479 type Camp struct { 480 Enum 481 base *dm.BaseLocale 482 showNull bool 483 showTerminated bool 484 } 485 486 func NewCamp(editable bool, base *dm.BaseLocale, showNull bool) Camp { 487 c := Camp{Enum: NewEnum(nil, editable), base: base, showNull: showNull} 488 return c 489 } 490 491 func (f Camp) SetChoices(camps []dm.AccesCamp) { 492 var offset int 493 if f.showNull { 494 offset = 1 495 } 496 items := make([]Choice, len(camps)+offset) 497 if f.showNull { 498 items[0] = Choice{Field: "", Label: "Indifférent"} 499 } 500 for index, c := range camps { 501 items[index+offset].Label = c.RawData().Label().String() 502 items[index+offset].Field = strconv.Itoa(int(c.Id)) 503 } 504 f.setChoice(items) 505 } 506 507 func (f *Camp) Update() { 508 var isTerminated rd.OptionnalBool 509 if !f.showTerminated { 510 isTerminated = rd.OBNon 511 } 512 camps := f.base.GetCamps(false, isTerminated) 513 f.SetChoices(camps) 514 } 515 516 func (f *Camp) ShowTerminated(show bool) { 517 f.showTerminated = show 518 f.Update() 519 } 520 521 func (f Camp) GetData() *dm.AccesCamp { 522 s := f.Enum.GetData() 523 id, err := strconv.Atoi(s) 524 if err != nil { 525 return nil 526 } 527 ac := f.base.NewCamp(int64(id)) 528 return &ac 529 } 530 531 func (f Camp) SetData(data rd.OptionnalId) { 532 field := "" 533 if data.Valid { 534 field = strconv.Itoa(int(data.Int64)) 535 } 536 f.Enum.SetData(field) 537 } 538 539 // Year propose le choix parmis un nombre 540 // fixe d'année, ou indifférent. 541 type Year enumInt 542 543 func NewYear() Year { return Year(newEnumInt(nil, true)) } 544 545 func (f Year) SetYears(years []int) { 546 items := make([]ChoiceInt, len(years)+1) 547 items[0] = ChoiceInt{Label: "Indifférent"} 548 for i, year := range years { 549 items[i+1] = ChoiceInt{Label: fmt.Sprintf("%d", year), Field: year} 550 } 551 enumInt(f).setChoice(items) 552 } 553 554 func (f Year) GetData() int { 555 return enumInt(f).GetData() 556 } 557 558 func (f Year) SetData(data int) { 559 enumInt(f).SetData(data) 560 }