From ff294f723f02fc2fef41afda6c5fdacc24747ba5 Mon Sep 17 00:00:00 2001 From: buhtz Date: Fri, 23 Feb 2024 09:03:01 +0100 Subject: [PATCH] fix: Respect dark mode using color roles for "2nd level list headings" Hard coded color codes are replaced by Qt's color roles. This works in light and dark mode themes. The term "2nd level list headings" refer to the snapshot timeline widget with headings like "Now" or "Yesterday" and to the "Shortcuts" list in the file list view with headings like "Global" and "Backup folders". Fix #1601 --- CHANGES | 1 + FAQ.md | 9 +++++++++ qt/app.py | 16 +++++++++++++--- qt/qttools.py | 17 +++++++++++++---- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 7118112fc..72a205d11 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ Back In Time Version 1.4.4-dev (development of upcoming release) +* Fix: Respect dark mode using color roles (#1601) * Dependency: Migration to PyQt6 * Build: PyLint unit test is skipped if PyLint isn't installed, but will always run on TravisCI (#1634) * Feature: Support rsync '--one-file-system' in Expert Options (#1598) diff --git a/FAQ.md b/FAQ.md index 3e8c4a0c0..f9c80cd20 100644 --- a/FAQ.md +++ b/FAQ.md @@ -454,6 +454,15 @@ Otherwise, kill the process. After that look into the folder For more details see the developer documentation: [Usage of control files (locks, flocks, logs and others)](common/doc-dev/4_Control_files_usage_(locks_flocks_logs_and_others).md) +### Switching to dark or light mode in the desktop environment is ignored by BIT +After restart _Back In Time_ it should addapt to the desktops current used +color theme. + +It happens because Qt does not detect theme modifications out of the +box. [Workarounds are known](https://stackoverflow.com/q/75457687), but +generate a relatively large amount of code and in our opinion are not worth +the effort. + ## Error Handling ### What happens if I hibernate the computer while a backup is running? diff --git a/qt/app.py b/qt/app.py index 05e081d78..69a4b7c55 100644 --- a/qt/app.py +++ b/qt/app.py @@ -51,6 +51,7 @@ from PyQt6.QtGui import (QAction, QShortcut, QDesktopServices, + QPalette, QColor, QIcon, QFileSystemModel) @@ -1058,6 +1059,10 @@ def placesChanged(self, item, previous): self.updateFilesView(3) def addPlace(self, name, path, icon): + """ + Dev note (buhtz, 2024-01-14): Parts of that code are redundant with + qttools.py::HeaderItem.__init__(). + """ item = QTreeWidgetItem() item.setText(0, name) @@ -1069,9 +1074,13 @@ def addPlace(self, name, path, icon): if not path: item.setFont(0, qttools.fontBold(item.font(0))) - item.setFlags(Qt.ItemFlag.ItemIsEnabled) - item.setBackground(0, QColor(196, 196, 196)) - item.setForeground(0, QColor(60, 60, 60)) + + # item.setFlags(Qt.ItemFlag.ItemIsEnabled) + item.setFlags(Qt.ItemFlag.NoItemFlags) + item.setForeground( + 0, self.palette().color(QPalette.ColorRole.PlaceholderText)) + item.setBackground( + 0, self.palette().color(QPalette.ColorRole.Window)) self.places.addTopLevelItem(item) @@ -1088,6 +1097,7 @@ def updatePlaces(self): # add backup folders include_folders = self.config.include() + if include_folders: folders = [] for item in include_folders: diff --git a/qt/qttools.py b/qt/qttools.py index 81be86feb..e78a2b8f0 100644 --- a/qt/qttools.py +++ b/qt/qttools.py @@ -31,14 +31,14 @@ import os import sys -from PyQt6.QtGui import (QAction, QFont, QColor, QIcon) +from PyQt6.QtGui import (QAction, QFont, QPalette, QIcon) from PyQt6.QtCore import (QDir, Qt, pyqtSlot, pyqtSignal, QModelIndex, QTranslator, QLocale, QLibraryInfo, QT_VERSION_STR) from PyQt6.QtWidgets import (QFileDialog, QAbstractItemView, QListView, QTreeView, QDialog, QApplication, QStyleFactory, QTreeWidget, QTreeWidgetItem, QComboBox, - QSystemTrayIcon, QWidget) + QSystemTrayIcon) from datetime import (datetime, date, timedelta) from calendar import monthrange from packaging.version import Version @@ -560,11 +560,20 @@ def updateText(self): class HeaderItem(TimeLineItem): def __init__(self, name, sid): + """ + Dev note (buhtz, 2024-01-14): Parts of that code are redundant with + app.py::MainWindow.addPlace(). + """ super(HeaderItem, self).__init__() self.setText(0, name) self.setFont(0, fontBold(self.font(0))) - self.setBackground(0, QColor(196, 196, 196)) - self.setForeground(0, QColor(60, 60, 60)) + + palette = QApplication.instance().palette() + self.setForeground( + 0, palette.color(QPalette.ColorRole.PlaceholderText)) + self.setBackground( + 0, palette.color(QPalette.ColorRole.Window)) + self.setFlags(Qt.ItemFlag.NoItemFlags) self.setData(0, Qt.ItemDataRole.UserRole, sid)