Skip to content

Commit

Permalink
Dark theme
Browse files Browse the repository at this point in the history
See #15
  • Loading branch information
Nodd committed Apr 2, 2022
1 parent e35739e commit ecb45da
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lineprofilergui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .utils import translate as _, MONOSPACE_FONT, ICONS, PIXMAPS
from .process import KernprofRun
from . import __version__
from .theme import update_theme

LINE_PROFILER_GUI_GITHUB_URL = "https://github.com/Nodd/lineprofilergui"
LINE_PROFILER_DOC_URL = "https://github.com/pyutils/line_profiler#id2"
Expand Down Expand Up @@ -153,6 +154,7 @@ def setup_ui(self):
self.retranslate_ui()
self.read_settings()
self.set_running_state(False)
update_theme()

def connect(self):
self.actionCollapse_all.triggered.connect(self.resultsTreeWidget.collapseAll)
Expand All @@ -177,6 +179,7 @@ def connect(self):
self.kernprof_run.output_text.connect(self.dockOutputWidget.append_log_text)
self.kernprof_run.output_error.connect(self.dockOutputWidget.append_log_error)
self.settingsDialog.accepted.connect(self.resultsTreeWidget.updateColonsVisible)
self.settingsDialog.accepted.connect(update_theme)
self.historyCombo.currentIndexChanged.connect(self.load_history)

def retranslate_ui(self):
Expand Down
30 changes: 30 additions & 0 deletions lineprofilergui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def setup_ui(self):
self.columnsLayout.addWidget(self.checkBox_5)
self.mainLayout.addWidget(self.columnsGroupBox)

# Theme
self.themeGroupBox = QtWidgets.QGroupBox(self)
self.themeLayout = QtWidgets.QVBoxLayout(self.themeGroupBox)
self.themeOS = QtWidgets.QRadioButton(self.themeGroupBox)
self.themeLayout.addWidget(self.themeOS)
self.themeLight = QtWidgets.QRadioButton(self.themeGroupBox)
self.themeLayout.addWidget(self.themeLight)
self.themeDark = QtWidgets.QRadioButton(self.themeGroupBox)
self.themeLayout.addWidget(self.themeDark)
self.mainLayout.addWidget(self.themeGroupBox)

# Button box
self.buttonBox = QtWidgets.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
Expand Down Expand Up @@ -87,6 +98,11 @@ def retranslate_ui(self):
self.checkBox_4.setText(_("Per Hit (ms)"))
self.checkBox_5.setText(_("% Time"))

self.themeGroupBox.setTitle(_("GUI theme"))
self.themeOS.setText(_("OS setting"))
self.themeLight.setText(_("Light"))
self.themeDark.setText(_("Dark"))

@QtCore.Slot()
def accept(self):
settings = QtCore.QSettings()
Expand All @@ -95,6 +111,13 @@ def accept(self):
settings.setValue("column3Visible", self.checkBox_3.isChecked())
settings.setValue("column4Visible", self.checkBox_4.isChecked())
settings.setValue("column5Visible", self.checkBox_5.isChecked())
if self.themeOS.isChecked():
theme = "os"
elif self.themeLight.isChecked():
theme = "light"
elif self.themeDark.isChecked():
theme = "dark"
settings.setValue("theme", theme)
QtWidgets.QDialog.accept(self)

@QtCore.Slot()
Expand All @@ -105,4 +128,11 @@ def reject(self):
self.checkBox_3.setChecked(settings.value("column3Visible", True, bool))
self.checkBox_4.setChecked(settings.value("column4Visible", True, bool))
self.checkBox_5.setChecked(settings.value("column5Visible", True, bool))
theme = settings.value("theme", "os", str)
if theme == "light":
self.themeLight.setChecked(True)
elif theme == "dark":
self.themeDark.setChecked(True)
else:
self.themeOS.setChecked(True)
QtWidgets.QDialog.reject(self)
77 changes: 77 additions & 0 deletions lineprofilergui/theme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os

from qtpy import QtCore, QtGui, QtWidgets
from qtpy.QtCore import Qt


def apply_dark_theme():
# taken from https://stackoverflow.com/questions/15035767/is-the-qt-5-dark-fusion-theme-available-for-windows

QtWidgets.QApplication.setStyle("Fusion")

# Now use a palette to switch to dark colors: 2A82DA
gray25 = QtGui.QColor(25, 25, 25)
gray35 = QtGui.QColor(35, 35, 35)
gray53 = QtGui.QColor(53, 53, 53)
grayblue = QtGui.QColor(42, 130, 218)
dark_palette = QtGui.QPalette()
dark_palette.setColor(QtGui.QPalette.Window, gray53)
dark_palette.setColor(QtGui.QPalette.WindowText, Qt.white)
dark_palette.setColor(QtGui.QPalette.Base, gray35)
dark_palette.setColor(QtGui.QPalette.AlternateBase, gray53)
dark_palette.setColor(QtGui.QPalette.ToolTipBase, gray25)
dark_palette.setColor(QtGui.QPalette.ToolTipText, Qt.white)
dark_palette.setColor(QtGui.QPalette.Text, Qt.white)
dark_palette.setColor(QtGui.QPalette.Button, gray53)
dark_palette.setColor(QtGui.QPalette.ButtonText, Qt.white)
dark_palette.setColor(QtGui.QPalette.BrightText, Qt.red)
dark_palette.setColor(QtGui.QPalette.Link, grayblue)
dark_palette.setColor(QtGui.QPalette.Highlight, grayblue)
dark_palette.setColor(QtGui.QPalette.HighlightedText, gray35)
dark_palette.setColor(QtGui.QPalette.Active, QtGui.QPalette.Button, gray53)
dark_palette.setColor(
QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, Qt.darkGray
)
dark_palette.setColor(
QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, Qt.darkGray
)
dark_palette.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Text, Qt.darkGray)
dark_palette.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Light, gray53)
QtWidgets.QApplication.setPalette(dark_palette)


def apply_default_theme():
styles = QtWidgets.QStyleFactory.keys()
if "WindowsVista" in styles:
style = "WindowsVista"
else:
style = "Fusion"
QtWidgets.QApplication.setPalette(QtWidgets.QApplication.style().standardPalette())
QtWidgets.QApplication.setStyle(style)


def is_windows_dark_theme():
"""Detect Windows theme"""
# From https://successfulsoftware.net/2021/03/31/how-to-add-a-dark-theme-to-your-qt-application/
settings = QtCore.QSettings(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
QtCore.QSettings.NativeFormat,
)
return settings.value("AppsUseLightTheme", 1) == 0


def update_theme():
settings = QtCore.QSettings()
theme = settings.value("theme", "os", str)

if theme not in ("light", "dark"):
if os.name == "nt":
theme = "dark" if is_windows_dark_theme() else "light"
else:
# TODO: Check on mac and linux ?
theme = "light"

if theme == "light":
apply_default_theme()
elif theme == "dark":
apply_dark_theme()

0 comments on commit ecb45da

Please sign in to comment.