Basic details page connected signals
[resumebuilder.git] / resumebuilder.go
1 package main
2
3 import (
4 "fmt"
5 "os"
6 "strconv"
7
8 "github.com/gotk3/gotk3/gtk"
9 )
10
11 type Applicant struct {
12 FirstName string
13 LastName string
14 Age int
15 Sex Sex
16 Education []Education
17 }
18
19 type Education struct {
20 SchoolName string
21 YearOfPassing int
22 PassPercentage float32
23 }
24
25 type Sex int
26
27 var ui *gtk.Builder
28 var selectedsex Sex
29
30 const (
31 Male Sex = 1
32 Female = 2
33 Other = 3
34 )
35
36 /* func SaveToJson(a *Applicant) {
37 b, err := json.Marshal(a)
38
39 if err != nil {
40 fmt.Println(err.Error())
41 os.Exit(2)
42 }
43 err = ioutil.WriteFile("test.json", b, 0644)
44 if err != nil {
45 fmt.Println(err.Error())
46 os.Exit(3)
47 }
48 } */
49
50 func getBasicDetails() (*Applicant, error) {
51 // Retrieve all the fields.
52 obj, err := ui.GetObject("FirstName")
53 if err != nil {
54 return nil, err
55 }
56 fsname, ok := obj.(*gtk.Entry)
57 if !ok {
58 return nil, err
59 }
60 firstname, err := fsname.GetText()
61 if err != nil {
62 return nil, err
63 }
64 obj, err = ui.GetObject("LastName")
65 if err != nil {
66 return nil, err
67 }
68 lsname, ok := obj.(*gtk.Entry)
69 if !ok {
70 return nil, err
71 }
72 lastname, err := lsname.GetText()
73 if err != nil {
74 return nil, err
75 }
76
77 obj, err = ui.GetObject("Age")
78 if err != nil {
79 return nil, err
80 }
81 agectl, ok := obj.(*gtk.Entry)
82 if !ok {
83 return nil, err
84 }
85 agetxt, err := agectl.GetText()
86 if err != nil {
87 return nil, err
88 }
89 age, err := strconv.Atoi(agetxt)
90 if err != nil {
91 return nil, err
92 }
93 obj, err = ui.GetObject("Male")
94 if err != nil {
95 return nil, err
96 }
97
98 // Store the contents in Applicant object
99 var applicant = new(Applicant)
100
101 applicant.FirstName = firstname
102 applicant.LastName = lastname
103 applicant.Age = age
104 applicant.Sex = selectedsex
105
106 return applicant, nil
107 }
108
109 func getWindow() (*gtk.Window, error) {
110 obj, err := ui.GetObject("MainWindow")
111 if err != nil {
112 fmt.Println(err.Error())
113 return nil, err
114 }
115 wnd, ok := obj.(*gtk.Window)
116 if !ok {
117 return nil, err
118 }
119 return wnd, nil
120 }
121
122 func getSaveButton() (*gtk.Button, error) {
123 btnobj, err := ui.GetObject("SaveButton")
124 if err != nil {
125 fmt.Println(err.Error())
126 return nil, err
127 }
128 btn, ok := btnobj.(*gtk.Button)
129 if !ok {
130 return nil, err
131 }
132 return btn, nil
133 }
134
135 func LoadMain() {
136 gtk.Init(nil)
137 var err error
138 ui, err = gtk.BuilderNew()
139 if err != nil {
140 fmt.Println(err.Error())
141 os.Exit(1)
142 }
143 asset, err := Asset("resources/resumebuilder.glade")
144 if err != nil {
145 fmt.Println(err.Error())
146 os.Exit(1)
147 }
148 ui.AddFromString(string(asset))
149 ui.ConnectSignals(map[string]interface{}{"SexMaleSelected": sexMaleSelected,
150 "SexFemaleSelected": sexFemaleSelected, "SexOtherSelected": sexOtherSelected,
151 "SaveButtonClicked": SaveButtonClicked})
152
153 wnd, err := getWindow()
154 if err != nil {
155 os.Exit(1)
156 }
157
158 wnd.SetTitle("Resume Builder")
159 wnd.ShowAll()
160 selectedsex = Male
161
162 wnd.Connect("destroy", func() {
163 gtk.MainQuit()
164 })
165 gtk.Main()
166 }
167
168 func sexMaleSelected(male *gtk.RadioButton) {
169 if male.GetActive() {
170 selectedsex = Male
171 }
172 }
173
174 func sexFemaleSelected(female *gtk.RadioButton) {
175 if female.GetActive() {
176 selectedsex = Female
177 }
178 }
179
180 func sexOtherSelected(other *gtk.RadioButton) {
181 if other.GetActive() {
182 selectedsex = Other
183 }
184 }
185
186 func SaveButtonClicked() {
187 a, err := getBasicDetails()
188 if err != nil {
189 fmt.Println(err.Error())
190 os.Exit(1)
191 }
192 fmt.Println(*a)
193 }
194
195 func main() {
196 LoadMain()
197 }