Cleanup
[carla.git] / source / frontend / carla_app.py
blobd14d1cbb83b79e86eb7f2e5b77124d6515803adf
1 #!/usr/bin/env python3
2 # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ------------------------------------------------------------------------------------------------------------
6 # Imports (Global)
8 import os
9 import sys
11 # ------------------------------------------------------------------------------------------------------------
12 # Imports (ctypes)
14 from ctypes import CDLL, RTLD_GLOBAL
16 # ------------------------------------------------------------------------------------------------------------
17 # Imports (PyQt)
19 from qt_compat import qt_config
21 if qt_config == 5:
22 from PyQt5.QtCore import QT_VERSION, Qt, QCoreApplication, QLibraryInfo, QLocale, QTranslator
23 from PyQt5.QtGui import QColor, QIcon, QPalette
24 from PyQt5.QtWidgets import QApplication
25 elif qt_config == 6:
26 from PyQt6.QtCore import QT_VERSION, Qt, QCoreApplication, QLibraryInfo, QLocale, QTranslator
27 from PyQt6.QtGui import QColor, QIcon, QPalette
28 from PyQt6.QtWidgets import QApplication
30 # ------------------------------------------------------------------------------------------------------------
31 # Imports (Custom)
33 from carla_backend import kIs64bit, LINUX, MACOS, WINDOWS
35 from carla_shared import (
36 CARLA_KEY_MAIN_USE_PRO_THEME,
37 CARLA_KEY_MAIN_PRO_THEME_COLOR,
38 CARLA_DEFAULT_MAIN_USE_PRO_THEME,
39 CARLA_DEFAULT_MAIN_PRO_THEME_COLOR,
40 CWD, VERSION,
41 getPaths,
42 gCarla
45 from utils import QSafeSettings
47 # ------------------------------------------------------------------------------------------------------------
49 class CarlaApplication():
50 def __init__(self, appName = "Carla2", libPrefix = None):
51 pathBinaries, pathResources = getPaths(libPrefix)
53 # Needed for MacOS and Windows
54 if os.path.exists(CWD) and (MACOS or WINDOWS):
55 QApplication.addLibraryPath(CWD)
57 # Needed for local wine build
58 if WINDOWS and CWD.endswith(("frontend", "resources")) and os.getenv("CXFREEZE") is None:
59 if kIs64bit:
60 path = "H:\\PawPawBuilds\\targets\\win64\\lib\\qt5\\plugins"
61 else:
62 path = "H:\\PawPawBuilds\\targets\\win32\\lib\\qt5\\plugins"
63 QApplication.addLibraryPath(path)
65 # Try resource dir as library path first
66 if os.path.exists(os.path.join(pathResources, "styles", "carlastyle.json")):
67 QApplication.addLibraryPath(pathResources)
68 stylesDir = pathResources
70 # Use binary dir as library path
71 elif os.path.exists(pathBinaries):
72 QApplication.addLibraryPath(pathBinaries)
73 stylesDir = pathBinaries
75 # If style is not available we can still fake it
76 else:
77 stylesDir = ""
79 # Set up translations
80 currentLocale = QLocale()
81 appTranslator = QTranslator()
82 sysTranslator = None
83 pathTranslations = os.path.join(pathResources, "translations")
84 if appTranslator.load(currentLocale, "carla", "_", pathTranslations):
85 sysTranslator = QTranslator()
86 pathSysTranslations = pathTranslations
87 if not sysTranslator.load(currentLocale, "qt", "_", pathSysTranslations):
88 pathSysTranslations = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
89 sysTranslator.load(currentLocale, "qt", "_", pathSysTranslations)
90 else:
91 appTranslator = None
92 self.fAppTranslator = appTranslator
93 self.fSysTranslator = sysTranslator
95 # base settings
96 settings = QSafeSettings("falkTX", appName)
97 useProTheme = MACOS or settings.value(CARLA_KEY_MAIN_USE_PRO_THEME, CARLA_DEFAULT_MAIN_USE_PRO_THEME, bool)
99 if not useProTheme:
100 self.createApp(appName)
101 return
103 # set style
104 QApplication.setStyle("carla" if stylesDir else "fusion")
106 # create app
107 self.createApp(appName)
109 if gCarla.nogui:
110 return
112 self.fApp.setStyle("carla" if stylesDir else "fusion")
114 if WINDOWS:
115 carlastyle1 = os.path.join(pathBinaries, "styles", "carlastyle.dll")
116 carlastyle2 = os.path.join(pathResources, "styles", "carlastyle.dll")
117 carlastyle = carlastyle2 if os.path.exists(carlastyle2) else carlastyle1
118 self._stylelib = CDLL(carlastyle, RTLD_GLOBAL)
119 self._stylelib.set_qt_app_style.argtypes = None
120 self._stylelib.set_qt_app_style.restype = None
121 self._stylelib.set_qt_app_style()
123 # set palette
124 proThemeColor = settings.value(CARLA_KEY_MAIN_PRO_THEME_COLOR, CARLA_DEFAULT_MAIN_PRO_THEME_COLOR, str).lower()
126 if MACOS or proThemeColor == "black":
127 self.createPaletteBlack()
129 elif proThemeColor == "blue":
130 self.createPaletteBlue()
132 def createApp(self, appName):
133 if qt_config == 5 and LINUX:
134 # AA_X11InitThreads is not available on old PyQt versions
135 try:
136 attr = Qt.AA_X11InitThreads
137 except:
138 attr = 10
139 QApplication.setAttribute(attr)
141 if QT_VERSION >= 0x50600:
142 QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
143 QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
145 if MACOS:
146 QApplication.setAttribute(Qt.AA_DontShowIconsInMenus)
148 args = sys.argv[:]
150 if WINDOWS:
151 args += ["-platform", "windows:fontengine=freetype"]
153 self.fApp = QCoreApplication(args) if gCarla.nogui else QApplication(args)
154 self.fApp.setApplicationName(appName)
155 self.fApp.setApplicationVersion(VERSION)
156 self.fApp.setOrganizationName("falkTX")
158 if self.fAppTranslator is not None:
159 self.fApp.installTranslator(self.fAppTranslator)
160 self.fApp.installTranslator(self.fSysTranslator)
162 if gCarla.nogui:
163 return
165 if appName == "Carla2-Control":
166 if QT_VERSION >= 0x50700:
167 self.fApp.setDesktopFileName("carla-control")
168 self.fApp.setWindowIcon(QIcon(":/scalable/carla-control.svg"))
169 else:
170 if QT_VERSION >= 0x50700:
171 self.fApp.setDesktopFileName("carla")
172 self.fApp.setWindowIcon(QIcon(":/scalable/carla.svg"))
174 def createPaletteBlack(self):
175 self.fPalBlack = QPalette()
176 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Window, QColor(14, 14, 14))
177 self.fPalBlack.setColor(QPalette.Active, QPalette.Window, QColor(17, 17, 17))
178 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Window, QColor(17, 17, 17))
179 self.fPalBlack.setColor(QPalette.Disabled, QPalette.WindowText, QColor(83, 83, 83))
180 self.fPalBlack.setColor(QPalette.Active, QPalette.WindowText, QColor(240, 240, 240))
181 self.fPalBlack.setColor(QPalette.Inactive, QPalette.WindowText, QColor(240, 240, 240))
182 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Base, QColor(6, 6, 6))
183 self.fPalBlack.setColor(QPalette.Active, QPalette.Base, QColor(7, 7, 7))
184 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Base, QColor(7, 7, 7))
185 self.fPalBlack.setColor(QPalette.Disabled, QPalette.AlternateBase, QColor(12, 12, 12))
186 self.fPalBlack.setColor(QPalette.Active, QPalette.AlternateBase, QColor(14, 14, 14))
187 self.fPalBlack.setColor(QPalette.Inactive, QPalette.AlternateBase, QColor(14, 14, 14))
188 self.fPalBlack.setColor(QPalette.Disabled, QPalette.ToolTipBase, QColor(4, 4, 4))
189 self.fPalBlack.setColor(QPalette.Active, QPalette.ToolTipBase, QColor(4, 4, 4))
190 self.fPalBlack.setColor(QPalette.Inactive, QPalette.ToolTipBase, QColor(4, 4, 4))
191 self.fPalBlack.setColor(QPalette.Disabled, QPalette.ToolTipText, QColor(230, 230, 230))
192 self.fPalBlack.setColor(QPalette.Active, QPalette.ToolTipText, QColor(230, 230, 230))
193 self.fPalBlack.setColor(QPalette.Inactive, QPalette.ToolTipText, QColor(230, 230, 230))
194 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Text, QColor(74, 74, 74))
195 self.fPalBlack.setColor(QPalette.Active, QPalette.Text, QColor(230, 230, 230))
196 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Text, QColor(230, 230, 230))
197 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Button, QColor(24, 24, 24))
198 self.fPalBlack.setColor(QPalette.Active, QPalette.Button, QColor(28, 28, 28))
199 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Button, QColor(28, 28, 28))
200 self.fPalBlack.setColor(QPalette.Disabled, QPalette.ButtonText, QColor(90, 90, 90))
201 self.fPalBlack.setColor(QPalette.Active, QPalette.ButtonText, QColor(240, 240, 240))
202 self.fPalBlack.setColor(QPalette.Inactive, QPalette.ButtonText, QColor(240, 240, 240))
203 self.fPalBlack.setColor(QPalette.Disabled, QPalette.BrightText, QColor(255, 255, 255))
204 self.fPalBlack.setColor(QPalette.Active, QPalette.BrightText, QColor(255, 255, 255))
205 self.fPalBlack.setColor(QPalette.Inactive, QPalette.BrightText, QColor(255, 255, 255))
206 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Light, QColor(191, 191, 191))
207 self.fPalBlack.setColor(QPalette.Active, QPalette.Light, QColor(191, 191, 191))
208 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Light, QColor(191, 191, 191))
209 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Midlight, QColor(155, 155, 155))
210 self.fPalBlack.setColor(QPalette.Active, QPalette.Midlight, QColor(155, 155, 155))
211 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Midlight, QColor(155, 155, 155))
212 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Dark, QColor(129, 129, 129))
213 self.fPalBlack.setColor(QPalette.Active, QPalette.Dark, QColor(129, 129, 129))
214 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Dark, QColor(129, 129, 129))
215 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Mid, QColor(94, 94, 94))
216 self.fPalBlack.setColor(QPalette.Active, QPalette.Mid, QColor(94, 94, 94))
217 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Mid, QColor(94, 94, 94))
218 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Shadow, QColor(155, 155, 155))
219 self.fPalBlack.setColor(QPalette.Active, QPalette.Shadow, QColor(155, 155, 155))
220 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Shadow, QColor(155, 155, 155))
221 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Highlight, QColor(14, 14, 14))
222 self.fPalBlack.setColor(QPalette.Active, QPalette.Highlight, QColor(60, 60, 60))
223 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Highlight, QColor(34, 34, 34))
224 self.fPalBlack.setColor(QPalette.Disabled, QPalette.HighlightedText, QColor(83, 83, 83))
225 self.fPalBlack.setColor(QPalette.Active, QPalette.HighlightedText, QColor(255, 255, 255))
226 self.fPalBlack.setColor(QPalette.Inactive, QPalette.HighlightedText, QColor(240, 240, 240))
227 self.fPalBlack.setColor(QPalette.Disabled, QPalette.Link, QColor(34, 34, 74))
228 self.fPalBlack.setColor(QPalette.Active, QPalette.Link, QColor(100, 100, 230))
229 self.fPalBlack.setColor(QPalette.Inactive, QPalette.Link, QColor(100, 100, 230))
230 self.fPalBlack.setColor(QPalette.Disabled, QPalette.LinkVisited, QColor(74, 34, 74))
231 self.fPalBlack.setColor(QPalette.Active, QPalette.LinkVisited, QColor(230, 100, 230))
232 self.fPalBlack.setColor(QPalette.Inactive, QPalette.LinkVisited, QColor(230, 100, 230))
233 self.fApp.setPalette(self.fPalBlack)
235 def createPaletteBlue(self):
236 self.fPalBlue = QPalette()
237 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Window, QColor(32, 35, 39))
238 self.fPalBlue.setColor(QPalette.Active, QPalette.Window, QColor(37, 40, 45))
239 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Window, QColor(37, 40, 45))
240 self.fPalBlue.setColor(QPalette.Disabled, QPalette.WindowText, QColor(89, 95, 104))
241 self.fPalBlue.setColor(QPalette.Active, QPalette.WindowText, QColor(223, 237, 255))
242 self.fPalBlue.setColor(QPalette.Inactive, QPalette.WindowText, QColor(223, 237, 255))
243 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Base, QColor(48, 53, 60))
244 self.fPalBlue.setColor(QPalette.Active, QPalette.Base, QColor(55, 61, 69))
245 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Base, QColor(55, 61, 69))
246 self.fPalBlue.setColor(QPalette.Disabled, QPalette.AlternateBase, QColor(60, 64, 67))
247 self.fPalBlue.setColor(QPalette.Active, QPalette.AlternateBase, QColor(69, 73, 77))
248 self.fPalBlue.setColor(QPalette.Inactive, QPalette.AlternateBase, QColor(69, 73, 77))
249 self.fPalBlue.setColor(QPalette.Disabled, QPalette.ToolTipBase, QColor(182, 193, 208))
250 self.fPalBlue.setColor(QPalette.Active, QPalette.ToolTipBase, QColor(182, 193, 208))
251 self.fPalBlue.setColor(QPalette.Inactive, QPalette.ToolTipBase, QColor(182, 193, 208))
252 self.fPalBlue.setColor(QPalette.Disabled, QPalette.ToolTipText, QColor(42, 44, 48))
253 self.fPalBlue.setColor(QPalette.Active, QPalette.ToolTipText, QColor(42, 44, 48))
254 self.fPalBlue.setColor(QPalette.Inactive, QPalette.ToolTipText, QColor(42, 44, 48))
255 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Text, QColor(96, 103, 113))
256 self.fPalBlue.setColor(QPalette.Active, QPalette.Text, QColor(210, 222, 240))
257 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Text, QColor(210, 222, 240))
258 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Button, QColor(51, 55, 62))
259 self.fPalBlue.setColor(QPalette.Active, QPalette.Button, QColor(59, 63, 71))
260 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Button, QColor(59, 63, 71))
261 self.fPalBlue.setColor(QPalette.Disabled, QPalette.ButtonText, QColor(98, 104, 114))
262 self.fPalBlue.setColor(QPalette.Active, QPalette.ButtonText, QColor(210, 222, 240))
263 self.fPalBlue.setColor(QPalette.Inactive, QPalette.ButtonText, QColor(210, 222, 240))
264 self.fPalBlue.setColor(QPalette.Disabled, QPalette.BrightText, QColor(255, 255, 255))
265 self.fPalBlue.setColor(QPalette.Active, QPalette.BrightText, QColor(255, 255, 255))
266 self.fPalBlue.setColor(QPalette.Inactive, QPalette.BrightText, QColor(255, 255, 255))
267 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Light, QColor(59, 64, 72))
268 self.fPalBlue.setColor(QPalette.Active, QPalette.Light, QColor(63, 68, 76))
269 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Light, QColor(63, 68, 76))
270 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Midlight, QColor(48, 52, 59))
271 self.fPalBlue.setColor(QPalette.Active, QPalette.Midlight, QColor(51, 56, 63))
272 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Midlight, QColor(51, 56, 63))
273 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Dark, QColor(18, 19, 22))
274 self.fPalBlue.setColor(QPalette.Active, QPalette.Dark, QColor(20, 22, 25))
275 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Dark, QColor(20, 22, 25))
276 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Mid, QColor(28, 30, 34))
277 self.fPalBlue.setColor(QPalette.Active, QPalette.Mid, QColor(32, 35, 39))
278 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Mid, QColor(32, 35, 39))
279 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Shadow, QColor(13, 14, 16))
280 self.fPalBlue.setColor(QPalette.Active, QPalette.Shadow, QColor(15, 16, 18))
281 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Shadow, QColor(15, 16, 18))
282 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Highlight, QColor(32, 35, 39))
283 self.fPalBlue.setColor(QPalette.Active, QPalette.Highlight, QColor(14, 14, 17))
284 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Highlight, QColor(27, 28, 33))
285 self.fPalBlue.setColor(QPalette.Disabled, QPalette.HighlightedText, QColor(89, 95, 104))
286 self.fPalBlue.setColor(QPalette.Active, QPalette.HighlightedText, QColor(217, 234, 253))
287 self.fPalBlue.setColor(QPalette.Inactive, QPalette.HighlightedText, QColor(223, 237, 255))
288 self.fPalBlue.setColor(QPalette.Disabled, QPalette.Link, QColor(79, 100, 118))
289 self.fPalBlue.setColor(QPalette.Active, QPalette.Link, QColor(156, 212, 255))
290 self.fPalBlue.setColor(QPalette.Inactive, QPalette.Link, QColor(156, 212, 255))
291 self.fPalBlue.setColor(QPalette.Disabled, QPalette.LinkVisited, QColor(51, 74, 118))
292 self.fPalBlue.setColor(QPalette.Active, QPalette.LinkVisited, QColor(64, 128, 255))
293 self.fPalBlue.setColor(QPalette.Inactive, QPalette.LinkVisited, QColor(64, 128, 255))
294 self.fApp.setPalette(self.fPalBlue)
296 def exec_(self):
297 return self.fApp.exec_()
299 def exit_exec(self):
300 return sys.exit(self.fApp.exec_())
302 def getApp(self):
303 return self.fApp
305 def quit(self):
306 self.fApp.quit()
308 # ------------------------------------------------------------------------------------------------------------