-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.go
341 lines (323 loc) · 11.9 KB
/
mainwindow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
)
// NewMainWindow creates and lays out the main window
func NewMainWindow() (*widgets.QApplication, *widgets.QMainWindow) {
// Create basic Qt application
app := widgets.NewQApplication(len(os.Args), os.Args)
// Create window
window := widgets.NewQMainWindow(nil, 0)
// Set minimum and initial size
window.SetMinimumSize2(960, 540)
window.Resize2(1280, 720)
// Center window on screen
window.SetGeometry(widgets.QStyle_AlignedRect(
core.Qt__LeftToRight,
core.Qt__AlignCenter,
window.Size(),
gui.QGuiApplication_Screens()[0].AvailableGeometry()))
// Set a window title
window.SetWindowTitle("OpenRQ")
// Return app and window for use in the main function
return app, window
}
// Temporary global pointer to the validation engine window for the hide/show button
var dockValidation *widgets.QDockWidget
func AddMenuBar(window *widgets.QMainWindow) {
// Main menu bar
menuBar := window.MenuBar()
// File options
fileMenu := widgets.NewQMenu2("File", nil)
// New
fileNew := fileMenu.AddAction2(GetIcon("file-new"), "New...")
fileNew.SetShortcut(gui.NewQKeySequence5(gui.QKeySequence__New))
fileNew.ConnectTriggered(func(checked bool) {
fileName := widgets.QFileDialog_GetSaveFileName(window, "New Project",
core.QStandardPaths_Locate(core.QStandardPaths__DocumentsLocation, "", 1),
"OpenRQ Project(*.orq)", "", 0)
if len(fileName) > 0 {
NewProject(fileName)
ReloadProject(window)
}
})
// Open
fileOpen := fileMenu.AddAction2(GetIcon("file-open"), "Open...")
fileOpen.SetShortcut(gui.NewQKeySequence5(gui.QKeySequence__Open))
fileOpen.ConnectTriggered(func(checked bool) {
fileName := widgets.QFileDialog_GetOpenFileName(window, "Open Project",
core.QStandardPaths_Locate(core.QStandardPaths__DocumentsLocation, "", 1),
"OpenRQ Project(*.orq *.orqz);;JavaScript Object Notation(*.json)", "", 0)
if len(fileName) > 0 {
if strings.HasSuffix(fileName, ".orqz") {
result := widgets.QMessageBox_Question(window, "Compressed Project",
"The project you are trying to load is compressed. "+
"In order to load the project, it first needs to be decompressed. "+
"This will replace the compressed project with a decompressed project.\n"+
"Are you sure you want to continue?",
widgets.QMessageBox__Yes|widgets.QMessageBox__No, widgets.QMessageBox__Yes)
if result == widgets.QMessageBox__No {
return
}
// Load compressed project
if _, err := NewCompressedProject(fileName); err != nil {
widgets.QMessageBox_Critical(window, "Failed to Load Project", err.Error(),
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
}
} else if strings.HasSuffix(fileName, ".json") {
// Ask to open
result := widgets.QMessageBox_Question(window, "Convert Project",
"The project you are trying to load needs to be converted before it can be opened. " +
"Changes made to the converted project will not affect the original document " +
"unless manually saved. Are you sure you want to continue?",
widgets.QMessageBox__Yes|widgets.QMessageBox__No, widgets.QMessageBox__Yes)
if result == widgets.QMessageBox__No {
return
}
// Parse JSON
if _, err := NewJSONProject(fileName); err != nil {
widgets.QMessageBox_Critical(window, "Failed to Load Project", err.Error(),
widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
}
} else {
// If not compressed, just load normal project
NewProject(fileName)
}
ReloadProject(window)
}
})
// Save as
fileSaveAs := fileMenu.AddAction2(GetIcon("file-save-as"), "Save As...")
fileSaveAs.SetShortcut(gui.NewQKeySequence5(gui.QKeySequence__SaveAs))
fileSaveAs.ConnectTriggered(func(checked bool) {
// Check if project is loaded to save
if currentProject == nil {
widgets.QMessageBox_Information(window, "No Project Loaded",
"No project is current loaded to save", widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
return
}
fileName := widgets.QFileDialog_GetSaveFileName(window, "Save Project",
filepath.Dir(currentProject.path),
"OpenRQ Project(*.orq);;OpenRQ Compressed Project(*.orqz);;JavaScript Object Notation(*.json)",
"", 0)
if len(fileName) > 0 {
if strings.HasSuffix(fileName, ".json") {
if data, err := json.MarshalIndent(map[string]interface{}{
"ProjectName": currentProject.Name(),
"Tree": Roots(),
}, "", "\t"); err != nil {
fmt.Println(err)
} else {
if err := ioutil.WriteFile(fileName, data, 0644); err != nil {
fmt.Println(err)
}
}
return
}
if err := currentProject.CopyTo(fileName); err != nil {
widgets.QMessageBox_Critical(window, "Failed to Save Project",
err.Error(), widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
}
}
})
// Quit
fileMenu.AddSeparator()
fileQuit := fileMenu.AddAction2(GetIcon("file-quit"), "Quit")
fileQuit.SetShortcut(gui.NewQKeySequence5(gui.QKeySequence__Quit))
fileQuit.ConnectTriggered(func(checked bool) {
window.Close()
})
menuBar.AddMenu(fileMenu)
// Edit menu
editMenu := widgets.NewQMenu2("Edit", nil)
editMenu.AddAction2(GetIcon("edit-rename"),
"Rename Project...").ConnectTriggered(func(checked bool) {
// Check if project is loaded to rename
if currentProject == nil {
widgets.QMessageBox_Information(window, "No Project Loaded",
"No project is current loaded to rename", widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
return
}
db := currentProject.Data()
defer db.Close()
name := widgets.QInputDialog_GetText(window, "Rename Project", "New project name",
widgets.QLineEdit__Normal, db.ProjectName(), nil, 0, 0)
if len(name) > 0 {
db.SetProjectName(name)
UpdateWindowTitle(window)
}
})
editMenu.AddAction2(GetIcon("edit-reload"),
"Reload Project").ConnectTriggered(func(checked bool) {
ReloadProject(window)
})
// Add to main toolbar
menuBar.AddMenu(editMenu)
// View
viewMenu := widgets.NewQMenu2("View", nil)
validate := viewMenu.AddAction("Validation Engine")
validate.SetCheckable(true)
validate.ConnectTriggered(func(checked bool) {
if checked {
dockValidation.Show()
} else {
dockValidation.Hide()
}
})
menuBar.AddMenu(viewMenu)
// About
aboutMenu := widgets.NewQMenu2("About", nil)
aboutMenu.AddAction2(
GetIcon("about-app"), "About OpenRQ").ConnectTriggered(func(checked bool) {
// Add app version information
aboutMessage := fmt.Sprintf("Version %v", versionTagName[1:])
// Add useless version and memory information
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
aboutMessage += fmt.Sprintf("\n\nQt %v, Go %v, %v\nMemory usage: %.2f mb (%.2f mb allocated)",
core.QLibraryInfo_Version().ToString(), runtime.Version()[2:], runtime.GOARCH,
float64(mem.TotalAlloc)/1000000, float64(mem.Sys)/1000000)
// Show simple dialog for now
widgets.QMessageBox_About(window, "About OpenRQ", aboutMessage)
})
aboutMenu.AddAction2(GetIcon("about-qt"), "About Qt").ConnectTriggered(func(checked bool) {
widgets.QMessageBox_AboutQt(window, "About Qt")
})
aboutMenu.AddAction2(GetIcon("about-licenses"), "Licenses").ConnectTriggered(func(checked bool) {
gui.QDesktopServices_OpenUrl(
core.NewQUrl3("https://github.com/kraxarn/OpenRQ/blob/golang/third_party.md", 0))
})
aboutMenu.AddSeparator()
aboutMenu.AddAction2(
GetIcon("about-update"), "Check for updates").ConnectTriggered(func(checked bool) {
// Actually check for updates
if isLatest, err := IsLatestVersion(); isLatest {
widgets.QMessageBox_Information(
window, "Updater", "You are running the latest version",
widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
return
} else if err != nil {
widgets.QMessageBox_Warning(
window, "Updater", fmt.Sprintf("Failed to check for updates: %v", err),
widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
return
}
// New update was found
if widgets.QMessageBox_Question(
window, "Updater",
"New update found, do you want to update now?",
widgets.QMessageBox__Yes|widgets.QMessageBox__No, widgets.QMessageBox__Yes) == widgets.QMessageBox__Yes {
gui.QDesktopServices_OpenUrl(
core.NewQUrl3("https://github.com/kraxarn/OpenRQ/releases", 0))
}
})
aboutMenu.AddAction2(GetIcon("about-gc"), "Run GC").ConnectTriggered(func(checked bool) {
// Get memory information
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
// Fetch how much memory we used before
memBefore := mem.TotalAlloc
// Run garbage collector
runtime.GC()
// Show dialog how much memory we saved
runtime.ReadMemStats(&mem)
widgets.QMessageBox_Information(
window, "Memory Info", fmt.Sprintf("Freed %.2f kb of memory", float64(mem.TotalAlloc-memBefore)/1000.0),
widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
})
menuBar.AddMenu(aboutMenu)
}
// CreateLayout creates the main layout widgets
func CreateLayout(window *widgets.QMainWindow) {
// Set view as central widget
linkBtn := widgets.NewQToolButton(nil)
view := CreateView(window, linkBtn)
window.SetCentralWidget(view)
view.Show()
// Create validation engine dock widget
dockValidation = widgets.NewQDockWidget("Validation Engine", window, 0)
dockValidation.SetWidget(CreateValidationEngineLayout())
// Hide by default
dockValidation.Hide()
// Hide close button as that's done from the tool bar
dockValidation.SetFeatures(widgets.QDockWidget__DockWidgetMovable | widgets.QDockWidget__DockWidgetFloatable)
// Add dock to main window
window.AddDockWidget(core.Qt__RightDockWidgetArea, dockValidation)
// Create item type dock widget
dockItemType := widgets.NewQDockWidget("Tools", window, 0)
dockItemType.SetWidget(CreateItemTypeCreator(linkBtn))
// Hide close button as there's no reason to close it
dockItemType.SetFeatures(widgets.QDockWidget__DockWidgetMovable | widgets.QDockWidget__DockWidgetFloatable)
// Add dock to main window
window.AddDockWidget(core.Qt__LeftDockWidgetArea, dockItemType)
// Create item shape dock widget
dockItemShape := widgets.NewQDockWidget("Shape", window, 0)
dockItemShape.SetWidget(CreateItemShapeCreator())
// Hide close button as there's no reason to close it
dockItemShape.SetFeatures(widgets.QDockWidget__DockWidgetMovable | widgets.QDockWidget__DockWidgetFloatable)
window.AddDockWidget(core.Qt__LeftDockWidgetArea, dockItemShape)
}
func CreateVBoxWidget(children ...widgets.QWidget_ITF) *widgets.QWidget {
layout := widgets.NewQVBoxLayout()
for _, child := range children {
layout.AddWidget(child, 1, 0)
}
widget := widgets.NewQWidget(nil, 0)
widget.SetLayout(layout)
return widget
}
func LayoutToWidget(vBox *widgets.QVBoxLayout) *widgets.QWidget {
widget := widgets.NewQWidget(nil, 0)
widget.SetLayout(vBox)
widget.SetMaximumWidth(200)
widget.SetMinimumWidth(150)
return widget
}
func CreateItemTypeCreator(linkBtn *widgets.QToolButton) *widgets.QToolBar {
layout := widgets.NewQToolBar2(nil)
// Requirement/solution selection
moveBtn := widgets.NewQToolButton(nil)
moveBtn.SetIcon(GetIcon("tools-move"))
moveBtn.SetText("Move")
moveBtn.SetCheckable(true)
moveBtn.SetChecked(true)
moveBtn.SetToolTip("Move an already created item")
layout.AddWidget(moveBtn)
linkBtn.SetIcon(GetIcon("tools-link"))
linkBtn.SetText("Draw line")
linkBtn.SetCheckable(true)
linkBtn.SetToolTip("Create a new link between already created items")
layout.AddWidget(linkBtn)
moveBtn.ConnectHitButton(func(pos *core.QPoint) bool {
if moveBtn.IsChecked() {
return false
}
linkBtn.SetChecked(false)
return true
})
linkBtn.ConnectHitButton(func(pos *core.QPoint) bool {
if linkBtn.IsChecked() {
return false
}
moveBtn.SetChecked(false)
return true
})
return layout
}
func CreateItemShapeCreator() *widgets.QWidget {
layout := widgets.NewQVBoxLayout()
shapeList := widgets.NewQListWidget(nil)
shapeList.SetDragEnabled(true)
shapeList.AddItem("Square")
layout.AddWidget(CreateVBoxWidget(shapeList), 0, 0)
return LayoutToWidget(layout)
}