UI Theme switcher working

This commit is contained in:
Donnie 2020-06-26 00:39:04 -04:00
parent cdf8c3b6e5
commit d3d5160861
7 changed files with 136 additions and 6 deletions

1
liberation_theme.json Normal file
View File

@ -0,0 +1 @@
{"theme_index": 1}

View File

@ -1,6 +1,8 @@
from userdata import logging_config from userdata import logging_config
# Logging setup # Logging setup
from userdata.liberation_theme import get_theme_file
VERSION_STRING = "2.0RC7" VERSION_STRING = "2.0RC7"
logging_config.init_logging(VERSION_STRING) logging_config.init_logging(VERSION_STRING)
@ -18,18 +20,17 @@ from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
from qt_ui.windows.QLiberationWindow import QLiberationWindow from qt_ui.windows.QLiberationWindow import QLiberationWindow
from qt_ui.windows.preferences.QLiberationFirstStartWindow import QLiberationFirstStartWindow from qt_ui.windows.preferences.QLiberationFirstStartWindow import QLiberationFirstStartWindow
from userdata import liberation_install, persistency from userdata import liberation_install, persistency
from userdata import liberation_theme, persistency
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
liberation_theme.init();
css = "" css = ""
with open("./resources/stylesheets/style-dcs.css") as stylesheet: with open("./resources/stylesheets/"+get_theme_file()) as stylesheet:
app.setStyleSheet(stylesheet.read()) app.setStyleSheet(stylesheet.read())
# Inject custom payload in pydcs framework # Inject custom payload in pydcs framework
custom_payloads = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..\\resources\\customized_payloads") custom_payloads = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..\\resources\\customized_payloads")
if os.path.exists(custom_payloads): if os.path.exists(custom_payloads):

View File

@ -19,6 +19,24 @@ URLS : Dict[str, str] = {
LABELS_OPTIONS = ["Full", "Abbreviated", "Dot Only", "Off"] LABELS_OPTIONS = ["Full", "Abbreviated", "Dot Only", "Off"]
SKILL_OPTIONS = ["Average", "Good", "High", "Excellent"] SKILL_OPTIONS = ["Average", "Good", "High", "Excellent"]
# new themes can be added here
THEMES: Dict[int, Dict[str, str]] = {
0: {'themeName': 'Windows',
'themeFile': 'windows-style.css',
'themeIcons': 'medium',
},
1: {'themeName': 'DCS World',
'themeFile': 'style-dcs.css',
'themeIcons': 'light',
},
2: {'themeName': 'Blue',
'themeFile': 'style.css',
'themeIcons': 'dark',
},
}
COLORS: Dict[str, QColor] = { COLORS: Dict[str, QColor] = {
"dark_red": QColor(140, 20, 20), "dark_red": QColor(140, 20, 20),
"red": QColor(200, 80, 80), "red": QColor(200, 80, 80),

View File

@ -1,11 +1,16 @@
import os import os
from PySide2 import QtWidgets from PySide2 import QtWidgets
from PySide2.QtCore import QFile
from PySide2.QtGui import Qt from PySide2.QtGui import Qt
from PySide2.QtWidgets import QFrame, QLineEdit, QGridLayout, QVBoxLayout, QLabel, QPushButton, \ from PySide2.QtWidgets import QFrame, QLineEdit, QGridLayout, QVBoxLayout, QLabel, QPushButton, \
QFileDialog, QMessageBox, QDialog QFileDialog, QMessageBox, QDialog, QComboBox, QApplication
import qt_ui.uiconstants as CONST
import sys
from userdata import liberation_install import userdata
from userdata import liberation_install, liberation_theme
from userdata.liberation_theme import get_theme_file, set_theme_name, get_theme_index, set_theme_index
class QLiberationPreferences(QFrame): class QLiberationPreferences(QFrame):
@ -28,9 +33,14 @@ class QLiberationPreferences(QFrame):
self.browse_saved_game.clicked.connect(self.on_browse_saved_games) self.browse_saved_game.clicked.connect(self.on_browse_saved_games)
self.browse_install_dir = QPushButton("Browse...") self.browse_install_dir = QPushButton("Browse...")
self.browse_install_dir.clicked.connect(self.on_browse_installation_dir) self.browse_install_dir.clicked.connect(self.on_browse_installation_dir)
self.themeSelect = QComboBox()
for x, y in CONST.THEMES.items():
self.themeSelect.addItem(y['themeName'])
self.initUi() self.initUi()
def initUi(self): def initUi(self):
main_layout = QVBoxLayout() main_layout = QVBoxLayout()
layout = QGridLayout() layout = QGridLayout()
@ -40,6 +50,9 @@ class QLiberationPreferences(QFrame):
layout.addWidget(QLabel("<strong>DCS installation directory:</strong>"), 2, 0, alignment=Qt.AlignLeft) layout.addWidget(QLabel("<strong>DCS installation directory:</strong>"), 2, 0, alignment=Qt.AlignLeft)
layout.addWidget(self.edit_dcs_install_dir, 3, 0, alignment=Qt.AlignRight) layout.addWidget(self.edit_dcs_install_dir, 3, 0, alignment=Qt.AlignRight)
layout.addWidget(self.browse_install_dir, 3, 1, alignment=Qt.AlignRight) layout.addWidget(self.browse_install_dir, 3, 1, alignment=Qt.AlignRight)
layout.addWidget(QLabel("<strong>Theme (Requires Restart)</strong>"), 4, 0)
layout.addWidget(self.themeSelect, 4, 1, alignment=Qt.AlignRight)
self.themeSelect.setCurrentIndex(get_theme_index())
main_layout.addLayout(layout) main_layout.addLayout(layout)
main_layout.addStretch() main_layout.addStretch()
@ -63,6 +76,7 @@ class QLiberationPreferences(QFrame):
print("Applying changes") print("Applying changes")
self.saved_game_dir = self.edit_saved_game_dir.text() self.saved_game_dir = self.edit_saved_game_dir.text()
self.dcs_install_dir = self.edit_dcs_install_dir.text() self.dcs_install_dir = self.edit_dcs_install_dir.text()
set_theme_index(self.themeSelect.currentIndex())
if not os.path.isdir(self.saved_game_dir): if not os.path.isdir(self.saved_game_dir):
error_dialog = QMessageBox.critical(self, "Wrong DCS Saved Games directory.", error_dialog = QMessageBox.critical(self, "Wrong DCS Saved Games directory.",
@ -87,6 +101,7 @@ class QLiberationPreferences(QFrame):
liberation_install.setup(self.saved_game_dir, self.dcs_install_dir) liberation_install.setup(self.saved_game_dir, self.dcs_install_dir)
liberation_install.save_config() liberation_install.save_config()
liberation_theme.set_theme_file()
return True return True

View File

@ -40,6 +40,9 @@ QMenuBar::item:pressed {
background: #1D2731; background: #1D2731;
} }
QLabel{
font-weight:bold;
}
/*QWidget*/ /*QWidget*/

View File

@ -0,0 +1,3 @@
/*
windows basis styles
*/

View File

@ -0,0 +1,89 @@
import json
import os
from shutil import copyfile
import dcs
import qt_ui.uiconstants as CONST
from userdata import persistency
global __theme_index
global __theme_name
global __theme_file
global __theme_icons
THEME_PREFERENCES_FILE_PATH = "liberation_theme.json"
DEFAULT_THEME_INDEX = 0
def init():
global __theme_index
__theme_index = DEFAULT_THEME_INDEX
print("init setting theme index to " + str(__theme_index))
if os.path.isfile(THEME_PREFERENCES_FILE_PATH):
try:
with(open(THEME_PREFERENCES_FILE_PATH)) as prefs:
pref_data = json.loads(prefs.read())
__theme_index = pref_data["theme_index"]
print(__theme_index)
set_theme_index(__theme_index)
set_theme_file()
print("file setting theme index to " + str(__theme_index))
except:
set_theme_index(DEFAULT_THEME_INDEX)
print("except setting theme index to " + str(__theme_index))
else:
set_theme_index(DEFAULT_THEME_INDEX)
print("else setting theme index to " + str(__theme_index))
def set_theme_index(x):
global __theme_index
__theme_index = x
def get_theme_index():
global __theme_index
return __theme_index
# get or set current theme index number
def set_theme_name(x):
global __theme_name
__theme_name = str(x)
def get_theme_name():
global __theme_name
return __theme_name
# get or set current theme icons based on the theme name
def set_theme_icons():
global __theme_icons
__theme_icons = CONST.THEMES[get_theme_name()]["themeIcons"]
def get_theme_icons():
global __theme_icons
return str(__theme_icons)
# get or set theme from json file
def set_theme_file():
theme_file = CONST.THEMES[get_theme_index()]['themeFile']
global __theme_file
__theme_file = theme_file
pref_data = {
"theme_index": get_theme_index()
}
with(open(THEME_PREFERENCES_FILE_PATH, "w")) as prefs:
prefs.write(json.dumps(pref_data))
def get_theme_file():
global __theme_file
return str(__theme_file)