github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/macros/macros.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"html/template"
     7  	"io/ioutil"
     8  	"log"
     9  	"os"
    10  	"os/exec"
    11  )
    12  
    13  // Code generation for contollers and tabs
    14  
    15  type ct struct {
    16  	GoName, Label string
    17  	source        string
    18  }
    19  
    20  var (
    21  	CONTROLLERS = [...]ct{
    22  		{"Personnes", "Personnes", "cont_personnes.go"},
    23  		{"Inscriptions", "Nouvelles inscriptions", "cont_inscriptions.go"},
    24  		{"Camps", "Séjours", "cont_camps.go"},
    25  		{"SuiviDossiers", "Suivi des dossiers", "cont_suivi_dossiers.go"},
    26  		{"Aides", "Aides", "cont_aides.go"},
    27  		{"Paiements", "Paiements", "cont_paiements.go"},
    28  		{"SuiviCamps", "Suivi des séjours", "cont_suivi_camps.go"},
    29  		{"Equipiers", "Equipiers", "cont_equipiers.go"},
    30  		{"Dons", "Dons", "cont_dons.go"},
    31  	}
    32  
    33  	templateControllers = `
    34  	// ResetRender rafraichit l'affichage,
    35  	// sans rafraichir les données internes
    36  	func (c *%s) ResetRender() {
    37  		c.Onglet.Render()
    38  		c.Onglet.UpdateToolbar()
    39  	}
    40  
    41  	// Reset met à jour les données internes
    42  	// en s'appuyant sur l'état, puis appelle Onglet.render
    43  	// pour synchroniser l'affichage graphique
    44  	func (c *%s) Reset() {
    45  		c.resetData()
    46  		if c.Onglet != nil && c.Onglet.IsActif() {
    47  			c.ResetRender()
    48  		}
    49  	}
    50  	
    51  	//  Main expose le controller principal
    52  	func (c %s) Main() *MainController {
    53  		return c.main
    54  	}
    55  	`
    56  
    57  	templateTabs = `
    58  	func (t %s) IsActif() bool {
    59  		if t.parent == nil {
    60  			return false
    61  		}
    62  		return t.parent.IndexOf(t) == t.parent.CurrentIndex()
    63  	}
    64  	
    65  	func (t %s) GrabFocus() {
    66  		if t.parent != nil {
    67  			t.parent.SetCurrentWidget(t)
    68  		}
    69  	}
    70  	`
    71  
    72  	templateTestTabs = template.Must(template.New("").Parse(`
    73  	package controllers
    74  	
    75  	// DO NOT EDIT - 
    76  
    77  	func setupOnglets(main *controllerPool) {
    78  		{{ range . -}}
    79  			main.{{ .GoName }}.Onglet = onglet{{ .GoName }}{}
    80  		{{ end }}
    81  	}
    82  
    83  	{{ range . }}
    84  	type onglet{{ .GoName }} struct {
    85  		Onglet{{ .GoName }}
    86  	}
    87  	func (onglet{{ .GoName }}) IsActif() bool {
    88  		return true
    89  	}
    90  	func (onglet{{ .GoName }}) UpdateToolbar() {
    91  	}
    92  	func (onglet{{ .GoName }}) Render() {
    93  	}
    94  	func (onglet{{ .GoName }}) GrabFocus() {
    95  	}
    96  	{{ end }}
    97  	`))
    98  
    99  	templateSetupOne = template.Must(template.New("").Parse(`
   100  		tab{{ .GoName }} := new{{ .GoName }}(mainC.Controllers.{{ .GoName }})
   101  		tab{{ .GoName }}.toolbar = tb
   102  		mainC.Controllers.{{ .GoName }}.Onglet = tab{{ .GoName }}
   103  		if modules.{{ .GoName }} > 0 {
   104  			w.AddTab(tab{{ .GoName }}, "{{ .Label }}")
   105  			tab{{ .GoName }}.parent = w 
   106  			tabs = append(tabs, tab{{ .GoName }})
   107  		}
   108  	`))
   109  
   110  	templateSetup = `
   111  	// NewTabs crée les onglets et les transmet au controller.
   112  	// Les ajoute aussi sur le QTabWidget renvoyé.
   113  	func InitTabWidget(mainC controllers.MainController, modules rawdata.Modules, tb *widgets.QToolBar) *widgets.QTabWidget {
   114  		w := widgets.NewQTabWidget(nil)
   115  
   116  		type withToolbar interface {
   117  			UpdateToolbar()
   118  			Render()
   119  		}
   120  		var tabs []withToolbar
   121  
   122  		%s
   123  		
   124  		w.ConnectCurrentChanged(func(index int) {
   125  			tabs[index].UpdateToolbar()
   126  			tabs[index].Render()
   127  		})
   128  		if len(tabs) > 0 {
   129  			tabs[0].UpdateToolbar()
   130  			tabs[0].Render()
   131  		}
   132  		return w
   133  	}
   134  	`
   135  )
   136  
   137  func main() {
   138  	var codeControllers, codeTabs string
   139  	var setups, tests bytes.Buffer
   140  	for _, ct := range CONTROLLERS {
   141  		codeControllers += fmt.Sprintf(templateControllers, ct.GoName, ct.GoName, ct.GoName)
   142  		codeTabs += fmt.Sprintf(templateTabs, ct.GoName, ct.GoName)
   143  
   144  		if err := templateSetupOne.Execute(&setups, ct); err != nil {
   145  			log.Fatal(err)
   146  		}
   147  
   148  	}
   149  	codeSetup := fmt.Sprintf(templateSetup, setups.String())
   150  
   151  	if err := templateTestTabs.Execute(&tests, CONTROLLERS); err != nil {
   152  		log.Fatal(err)
   153  	}
   154  
   155  	codeControllers = `
   156  	package controllers
   157  	
   158  	// DO NOT EDIT - autogenerated 
   159  	` + codeControllers
   160  
   161  	if err := ioutil.WriteFile("controllers/resets.go", []byte(codeControllers), os.ModePerm); err != nil {
   162  		log.Fatal(err)
   163  	}
   164  
   165  	codeGui := `
   166  	package onglets
   167  	
   168  	// DO NOT EDIT - autogenerated 
   169  	` + codeTabs + codeSetup
   170  	if err := ioutil.WriteFile("GUI/onglets/auto.go", []byte(codeGui), os.ModePerm); err != nil {
   171  		log.Fatal(err)
   172  	}
   173  
   174  	if err := ioutil.WriteFile("controllers/cont_test.go", tests.Bytes(), os.ModePerm); err != nil {
   175  		log.Fatal(err)
   176  	}
   177  
   178  	_ = exec.Command("goimports", "-w", "controllers/resets.go").Run()
   179  	_ = exec.Command("goimports", "-w", "controllers/cont_test.go").Run()
   180  	_ = exec.Command("goimports", "-w", "GUI/onglets/auto.go").Run()
   181  }