Browse Source

support running in window, without system tray

Thomas Buck 3 years ago
parent
commit
3e39a38fa8
2 changed files with 49 additions and 14 deletions
  1. 3
    1
      README.md
  2. 46
    13
      src/octotray.py

+ 3
- 1
README.md View File

9
 
9
 
10
 For more [take a look at OctoTray on my website](https://www.xythobuz.de/octotray.html).
10
 For more [take a look at OctoTray on my website](https://www.xythobuz.de/octotray.html).
11
 
11
 
12
-## Building
12
+If the system tray is not available (or when passing the '-w' parameter) the main menu will instead be shown in a window.
13
+
14
+## Building / Running
13
 
15
 
14
 You have different options of building and running OctoTray:
16
 You have different options of building and running OctoTray:
15
 
17
 

+ 46
- 13
src/octotray.py View File

337
             else:
337
             else:
338
                 print("Error loading image: " + reply.errorString())
338
                 print("Error loading image: " + reply.errorString())
339
 
339
 
340
+class MainWindow(QWidget):
341
+    def __init__(self, parent, *args, **kwargs):
342
+        super(MainWindow, self).__init__(*args, **kwargs)
343
+        self.parent = parent
344
+
345
+        self.mainLayout = QVBoxLayout()
346
+        self.setLayout(self.mainLayout)
347
+        self.mainLayout.addWidget(self.parent.menu)
348
+
349
+        self.parent.menu.aboutToHide.connect(self.aboutToHide)
350
+
351
+    def aboutToHide(self):
352
+        self.parent.menu.show()
353
+
354
+    def closeEvent(self, event):
355
+        self.parent.exit()
356
+        event.accept()
357
+
340
 class OctoTray():
358
 class OctoTray():
341
     name = "OctoTray"
359
     name = "OctoTray"
342
     vendor = "xythobuz"
360
     vendor = "xythobuz"
367
     camWindows = []
385
     camWindows = []
368
     settingsWindow = None
386
     settingsWindow = None
369
 
387
 
370
-    def __init__(self, app):
388
+    def __init__(self, app, inSysTray):
371
         QCoreApplication.setApplicationName(self.name)
389
         QCoreApplication.setApplicationName(self.name)
372
         self.app = app
390
         self.app = app
373
-
374
-        if not QSystemTrayIcon.isSystemTrayAvailable():
375
-            self.showDialog("OctoTray Error", "System Tray is not available on this platform!", "", False, False, True)
376
-            sys.exit(0)
391
+        self.inSysTray = inSysTray
377
 
392
 
378
         self.manager = QtNetwork.QNetworkAccessManager()
393
         self.manager = QtNetwork.QNetworkAccessManager()
379
         self.menu = QMenu()
394
         self.menu = QMenu()
483
         self.pic.load(self.iconPathName)
498
         self.pic.load(self.iconPathName)
484
         self.icon = QIcon(self.pic)
499
         self.icon = QIcon(self.pic)
485
 
500
 
486
-        self.trayIcon = QSystemTrayIcon(self.icon)
487
-        self.trayIcon.setToolTip(self.name + " " + self.version)
488
-        self.trayIcon.setContextMenu(self.menu)
489
-        self.trayIcon.activated.connect(self.showHide)
490
-        self.trayIcon.setVisible(True)
501
+        if self.inSysTray:
502
+            self.trayIcon = QSystemTrayIcon(self.icon)
503
+            self.trayIcon.setToolTip(self.name + " " + self.version)
504
+            self.trayIcon.setContextMenu(self.menu)
505
+            self.trayIcon.activated.connect(self.showHide)
506
+            self.trayIcon.setVisible(True)
507
+        else:
508
+            self.mainWindow = MainWindow(self)
509
+            self.mainWindow.show()
510
+            self.mainWindow.activateWindow()
511
+            screenGeometry = QDesktopWidget().screenGeometry()
512
+            x = (screenGeometry.width() - self.mainWindow.width()) / 2
513
+            y = (screenGeometry.height() - self.mainWindow.height()) / 2
514
+            x += screenGeometry.x()
515
+            y += screenGeometry.y()
516
+            self.mainWindow.setGeometry(int(x), int(y), int(self.mainWindow.width()), int(self.mainWindow.height()))
491
 
517
 
492
     def showHide(self, activationReason):
518
     def showHide(self, activationReason):
493
         if activationReason == QSystemTrayIcon.Trigger:
519
         if activationReason == QSystemTrayIcon.Trigger:
868
         if self.settingsWindow != None:
894
         if self.settingsWindow != None:
869
             self.settingsWindow.close()
895
             self.settingsWindow.close()
870
 
896
 
871
-        self.trayIcon.setVisible(False)
897
+        if self.inSysTray:
898
+            self.trayIcon.setVisible(False)
899
+        else:
900
+            self.mainWindow.setVisible(False)
872
 
901
 
873
 if __name__ == "__main__":
902
 if __name__ == "__main__":
874
     app = QApplication(sys.argv)
903
     app = QApplication(sys.argv)
875
     app.setQuitOnLastWindowClosed(False)
904
     app.setQuitOnLastWindowClosed(False)
876
 
905
 
877
-    tray = OctoTray(app)
906
+    inSysTray = QSystemTrayIcon.isSystemTrayAvailable()
907
+    if ("windowed" in sys.argv) or ("--windowed" in sys.argv) or ("-w" in sys.argv):
908
+        inSysTray = False
909
+
910
+    tray = OctoTray(app, inSysTray)
878
     rc = app.exec_()
911
     rc = app.exec_()
879
 
912
 
880
     while rc == 42:
913
     while rc == 42:
881
         tray.closeAll()
914
         tray.closeAll()
882
-        tray = OctoTray(app)
915
+        tray = OctoTray(app, inSysTray)
883
         rc = app.exec_()
916
         rc = app.exec_()
884
 
917
 
885
     sys.exit(rc)
918
     sys.exit(rc)

Loading…
Cancel
Save