Skip to content

Commit

Permalink
Backport PR #23524 on branch 6.x (PR : Add validation for theme color…
Browse files Browse the repository at this point in the history
… changes (Preferences)) (#23579)
  • Loading branch information
meeseeksmachine authored Jan 29, 2025
1 parent f9200a5 commit 3bc6ebd
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions spyder/plugins/appearance/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@

"""Appearance entry in Preferences."""

import re

from qtpy.QtCore import Qt
from qtpy.QtWidgets import (QDialog, QDialogButtonBox, QGridLayout, QGroupBox,
QHBoxLayout, QVBoxLayout, QWidget)
from qtpy.QtWidgets import (
QDialog,
QDialogButtonBox,
QGridLayout,
QGroupBox,
QHBoxLayout,
QMessageBox,
QVBoxLayout,
QWidget,
)

from spyder.api.translations import _
from spyder.api.widgets.dialogs import SpyderDialogButtonBox
Expand Down Expand Up @@ -42,7 +52,7 @@ def __init__(self, parent=None, stack=None):
self.setLayout(layout)

# Signals
bbox.accepted.connect(self.accept)
bbox.accepted.connect(self.validate_colors)
bbox.accepted.connect(self.get_edited_color_scheme)
bbox.rejected.connect(self.reject)

Expand All @@ -60,6 +70,43 @@ def get_scheme_name(self):
"""
return self.scheme_name_textbox[self.last_used_scheme].text()

def validate_colors(self):
"""
Validate the current color scheme and display a message box listing
any invalid colors.
"""
invalid_colors = {}
scheme_name = self.last_used_scheme
pattern = (
r"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$"
)
for key in self.widgets[scheme_name]:
items = self.widgets[scheme_name][key]

if not bool(re.match(pattern, items[0].text())):
invalid_colors[key] = items[0].text()

if invalid_colors:
message = _("The following properties have invalid colors:\n\n")
for property_name, color in invalid_colors.items():
name = syntaxhighlighters.COLOR_SCHEME_KEYS[property_name]
clean_name = name[:-1].replace("<br>", "")
message += _(
"The property <b>{}</b> has an invalid color: {}\n"
).format(clean_name, color)

msgbox = QMessageBox(
QMessageBox.Warning,
_('Error setting colors'),
message,
QMessageBox.Ok,
self
)
msgbox.exec_()
else:
self.accept()


def get_edited_color_scheme(self):
"""
Get the values of the last edited color scheme to be used in an instant
Expand Down

0 comments on commit 3bc6ebd

Please sign in to comment.