Skip to content

Commit

Permalink
Merge pull request bitcraze#565 from bitcraze/jonasdn/564
Browse files Browse the repository at this point in the history
LogTab: Adapt to dark mode
  • Loading branch information
jonasdn authored Jan 25, 2022
2 parents 0988dfe + efcbb7f commit 4deaa64
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/cfclient/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from PyQt5.QtWidgets import QActionGroup
from PyQt5.QtWidgets import QShortcut
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QMenu
from PyQt5.QtWidgets import QMessageBox
Expand Down Expand Up @@ -142,6 +143,15 @@ def __init__(self, *args):
" fly.")
self.statusBar().addWidget(self._statusbar_label)

#
# We use this hacky-trick to find out if we are in dark-mode and
# figure out what bgcolor to set from that. We always use the current
# palette forgreound.
#
self.textColor = self._statusbar_label.palette().color(QPalette.WindowText)
self.bgColor = self._statusbar_label.palette().color(QPalette.Background)
self.isDark = self.textColor.value() > self.bgColor.value()

self.joystickReader = JoystickReader()
self._active_device = ""
# self.configGroup = QActionGroup(self._menu_mappings, exclusive=True)
Expand Down
6 changes: 5 additions & 1 deletion src/cfclient/ui/tabs/LogTab.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def __init__(self, tabWidget, helper, *args):

# Init the tree widget
self.logTree.setAlternatingRowColors(True)
self.logTree.setStyleSheet('QTreeWidget { alternate-background-color: #e9e9e9; }')
if helper.mainUI.isDark:
self.logTree.setStyleSheet('QTreeWidget { alternate-background-color: #3c3c3c; }')
else:
self.logTree.setStyleSheet('QTreeWidget { alternate-background-color: #e9e9e9; }')

self.logTree.setHeaderLabels(['Name', 'ID', 'Unpack', 'Storage', 'Description'])
self.logTree.header().resizeSection(0, 150)
self.logTree.setSortingEnabled(True)
Expand Down
29 changes: 10 additions & 19 deletions src/cfclient/ui/tabs/ParamTab.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
from PyQt5 import uic, QtCore
from PyQt5.QtCore import QSortFilterProxyModel, Qt, pyqtSignal
from PyQt5.QtCore import QAbstractItemModel, QModelIndex, QVariant
from PyQt5.QtGui import QBrush, QColor, QPalette
from PyQt5.QtWidgets import QHeaderView, QLabel
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtWidgets import QHeaderView

import cfclient
from cfclient.ui.tab import Tab
Expand Down Expand Up @@ -105,13 +105,14 @@ def child_count(self):
class ParamBlockModel(QAbstractItemModel):
"""Model for handling the parameters in the tree-view"""

def __init__(self, parent):
def __init__(self, parent, mainUI):
"""Create the empty model"""
super(ParamBlockModel, self).__init__(parent)
self._nodes = []
self._column_headers = ['Name', 'Type', 'Access', 'Persistent', 'Value']
self._red_brush = QBrush(QColor("red"))
self._enabled = False
self._mainUI = mainUI

def set_enabled(self, enabled):
if self._enabled != enabled:
Expand Down Expand Up @@ -189,26 +190,16 @@ def index(self, row, column, parent):
def data(self, index, role):
"""Re-implemented method to get the data for a given index and role"""

#
# We use this hacky-trick to find out if we are in dark-mode and
# figure out what bgcolor to set from that. We always use the current
# palette forgreound.
#
label = QLabel('dummy')
text_color = label.palette().color(QPalette.WindowText)
bg_color = label.palette().color(QPalette.Background)
is_dark = text_color.value() > bg_color.value()

if role == Qt.BackgroundColorRole:
if index.row() % 2 == 0:
return QVariant(bg_color)
return QVariant(self._mainUI.bgColor)
else:
multiplier = 1.15 if is_dark else 0.95
multiplier = 1.15 if self._mainUI.isDark else 0.95
return QVariant(
QColor(
int(bg_color.red() * multiplier),
int(bg_color.green() * multiplier),
int(bg_color.blue() * multiplier)
int(self._mainUI.bgColor.red() * multiplier),
int(self._mainUI.bgColor.green() * multiplier),
int(self._mainUI.bgColor.blue() * multiplier)
)
)

Expand Down Expand Up @@ -294,7 +285,7 @@ def __init__(self, tabWidget, helper, *args):
self.cf.disconnected.add_callback(self._disconnected_signal.emit)
self._disconnected_signal.connect(self._disconnected)

self._model = ParamBlockModel(None)
self._model = ParamBlockModel(None, self.helper.mainUI)

self.proxyModel = ParamTreeFilterProxy(self.paramTree)
self.proxyModel.setSourceModel(self._model)
Expand Down

0 comments on commit 4deaa64

Please sign in to comment.