github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI/common/widgets.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/benoitkugler/goACVE/client/GUI/basic" 7 "github.com/benoitkugler/goACVE/client/controllers" 8 "github.com/therecipe/qt/core" 9 "github.com/therecipe/qt/widgets" 10 ) 11 12 // A setup par la main app. 13 var MainWindow *widgets.QMainWindow 14 15 type MonitorDownload struct { 16 *widgets.QDialog 17 18 progressBar *widgets.QProgressBar 19 } 20 21 // NewMonitorDownload initialise le dialog et applique `Show` 22 func NewMonitorDownload(label string) MonitorDownload { 23 dialog := MonitorDownload{QDialog: basic.Dialog2("Téléchargement")} 24 dialog.SetObjectName("download") 25 dialog.SetModal(true) 26 dialog.progressBar = basic.NewProgressBar("") 27 lay := widgets.NewQHBoxLayout2(dialog) 28 lay.AddWidget(basic.Label(label), 1, 0) 29 lay.AddWidget(dialog.progressBar, 1, 0) 30 dialog.Resize(dialog.SizeHint()) 31 dialog.Show() 32 dialog.Monitor(0) 33 return dialog 34 } 35 36 func (m MonitorDownload) Monitor(i uint8) { 37 m.progressBar.SetValue(int(i)) 38 core.QCoreApplication_ProcessEvents(core.QEventLoop__AllEvents) 39 } 40 41 type ListePiecesJointes struct { 42 *widgets.QGroupBox 43 44 main *controllers.MainController 45 } 46 47 func NewListePiecesJointes(main *controllers.MainController, title string, pjs []controllers.DocumentCamp) ListePiecesJointes { 48 viewPjs := ListePiecesJointes{QGroupBox: widgets.NewQGroupBox2(title, nil), main: main} 49 layPjs := widgets.NewQGridLayout(viewPjs) 50 if len(pjs) == 0 { 51 layPjs.AddWidget3(basic.Label("<i>Aucun document n'est disponible.</i>"), 0, 0, 1, 2, 0) 52 } 53 for index, pj := range pjs { 54 pj := pj // attention au closure dans les boucles 55 getButton := widgets.NewQToolButton(nil) 56 _, needDownload := pj.(controllers.RegisteredDocument) 57 icon := basic.Icons.Preview 58 if needDownload { 59 icon = basic.Icons.Download 60 } 61 getButton.SetIcon(icon) 62 getButton.ConnectClicked(func(_ bool) { 63 viewPjs.getDocument(pj) 64 }) 65 layPjs.AddWidget2(basic.Label(pj.FileName()), index, 0, 0) 66 layPjs.AddWidget2(getButton, index, 1, 0) 67 } 68 return viewPjs 69 } 70 71 func (l ListePiecesJointes) getDocument(pj controllers.DocumentCamp) { 72 var path string 73 l.SetEnabled(false) 74 l.Repaint() 75 switch pj := pj.(type) { 76 case controllers.RegisteredDocument: 77 mon := NewMonitorDownload("Téléchargement du document...") 78 basic.Delay(func() { 79 path = l.main.DownloadDocument(pj.Id, mon.Monitor) 80 mon.Close() 81 mon.DestroyQDialog() 82 basic.ShowFile(path) 83 l.SetEnabled(true) 84 }) 85 case controllers.DynamicDocument: 86 byt, err := pj.Generate() 87 if err != nil { 88 basic.ShowError(err) 89 return 90 } 91 path = l.main.SaveFile(byt, pj.FileName()) 92 basic.ShowFile(path) 93 l.SetEnabled(true) 94 } 95 } 96 97 type Statistics struct { 98 *widgets.QFrame 99 layouts []*widgets.QFormLayout 100 } 101 102 func NewStatistics(horizontal bool) Statistics { 103 s := Statistics{QFrame: basic.Frame()} 104 s.SetObjectName("cadre") 105 mainLay := widgets.NewQHBoxLayout2(s) 106 nbCols := 1 107 if horizontal { 108 nbCols = 3 109 } 110 s.layouts = make([]*widgets.QFormLayout, nbCols) 111 for index := range s.layouts { 112 s.layouts[index] = widgets.NewQFormLayout(nil) 113 s.layouts[index].SetSpacing(3) 114 mainLay.AddLayout(s.layouts[index], 1) 115 } 116 return s 117 } 118 119 func (s Statistics) SetData(stats []controllers.Stat) { 120 for _, col := range s.layouts { 121 for col.RowCount() > 0 { 122 col.RemoveRow(0) 123 } 124 } 125 nbCols, currentCol := len(s.layouts), 0 126 for _, stat := range stats { 127 lab := basic.Label("<b>" + stat.Value + "</b>") 128 s.layouts[currentCol].AddRow3(stat.Label, lab) 129 currentCol += 1 130 if currentCol%nbCols == 0 { 131 currentCol = 0 132 } 133 } 134 } 135 136 // Permet d'envoyer un texte libre. 137 // A utiliser avec parcimonie, surtout pour la communication interne. 138 type MailLibre struct { 139 *widgets.QDialog 140 141 Text *widgets.QTextEdit 142 } 143 144 func NewMailLibre(to, html string) *MailLibre { 145 m := MailLibre{QDialog: basic.Dialog("Envoyer un email")} 146 m.SetMinimumWidth(400) 147 m.Text = widgets.NewQTextEdit(nil) 148 m.Text.SetHtml(html) 149 150 label := basic.Label(fmt.Sprintf("Mail à envoyer à <b>%s</b> :", to)) 151 152 valid := basic.Button("Envoyer") 153 valid.SetObjectName(basic.ONAction) 154 valid.ConnectClicked(func(_ bool) { 155 m.Accept() 156 }) 157 158 m.Layout().AddWidget(label) 159 m.Layout().AddWidget(m.Text) 160 m.Layout().AddWidget(valid) 161 return &m 162 }