main.go (2740B)
1 // Copyright © 2018 MTRNord <info@nordgedanken.de> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package app 16 17 import ( 18 "database/sql" 19 "github.com/Nordgedanken/Morpheusv2/pkg/loginUI" 20 "github.com/Nordgedanken/Morpheusv2/pkg/mainUI" 21 "github.com/Nordgedanken/Morpheusv2/pkg/util" 22 "github.com/matrix-org/gomatrix" 23 "github.com/therecipe/qt/core" 24 "github.com/therecipe/qt/gui" 25 "github.com/therecipe/qt/widgets" 26 "log" 27 ) 28 29 var args []string 30 var cli *gomatrix.Client 31 var windowHeight = 600 32 var windowWidth = 950 33 var window *widgets.QMainWindow 34 35 // Start prepares the Main QT Window and opens it 36 func Start(argsArg []string) error { 37 args = argsArg 38 log.Println("Starting Morpheus v2") 39 40 initApp() 41 42 _, err := util.DB.GetCurrentUser() 43 // We special case ErrNoRows because this is expected to happen if user is missing 44 if err == sql.ErrNoRows { 45 loginUIs := loginUI.NewLoginUI(windowWidth, windowHeight, window) 46 SetNewWindow(loginUIs, window) 47 } else if err != nil { 48 return err 49 } else { 50 mainUIs := mainUI.NewMainUI(windowWidth, windowHeight, window) 51 SetNewWindow(mainUIs, window) 52 } 53 54 widgets.QApplication_Exec() 55 56 return nil 57 } 58 59 func initApp() { 60 log.Println("Create QApp") 61 app := widgets.NewQApplication(len(args), args) 62 63 app.SetAttribute(core.Qt__AA_UseHighDpiPixmaps, true) 64 app.SetApplicationName("Morpheus") 65 app.SetApplicationVersion("0.1.0") 66 appIcon := gui.NewQIcon5(":/qml/resources/logos/MorpheusBig.png") 67 app.SetWindowIcon(appIcon) 68 window = widgets.NewQMainWindow(nil, 0) 69 app.SetActiveWindow(window) 70 71 desktopApp := app.Desktop() 72 primaryScreen := desktopApp.PrimaryScreen() 73 screen := desktopApp.Screen(primaryScreen) 74 windowX := (screen.Width() - windowHeight) / 2 75 windowY := (screen.Height() - windowWidth) / 2 76 77 window.Resize2(windowWidth, windowHeight) 78 window.Move2(windowX, windowY) 79 80 window.ConnectCloseEvent(func(event *gui.QCloseEvent) { 81 log.Println("Morpheus closed") 82 }) 83 84 } 85 86 // SetNewWindow loads the new UI into the QMainWindow 87 func SetNewWindow(ui ui, window *widgets.QMainWindow) error { 88 ui.SetCli(cli) 89 uiErr := ui.NewUI() 90 if uiErr != nil { 91 return uiErr 92 } 93 ui.GetWidget().Resize2(windowWidth, windowHeight) 94 window.SetCentralWidget(ui.GetWidget()) 95 window.Show() 96 return nil 97 }