Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/ui/gui_main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://github.com/secondfry/shortcircuit/blob/master/README.md#security-prioritization&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;[?]&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://github.com/secondfry/shortcircuit/blob/master/README.md#security-prioritization&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;[?]&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
Expand Down
2 changes: 1 addition & 1 deletion resources/ui/gui_tripwire.ui
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<item row="6" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://tripwire.eve-apps.com/&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;Don't have a Tripwire account, yet?&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://tripwire.eve-apps.com/&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Don't have a Tripwire account, yet?&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
Expand Down
2 changes: 1 addition & 1 deletion resources/ui/gui_wanderer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<item row="4" column="1">
<widget class="QLabel" name="label_help">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Generate the token in Wanderer under Map Settings → Public Api → Generate. &lt;a href=&quot;https://github.com/wanderer-industries/wanderer&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;About Wanderer&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Generate the token in Wanderer under Map Settings → Public Api → Generate. &lt;a href=&quot;https://github.com/wanderer-industries/wanderer&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;About Wanderer&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down
115 changes: 106 additions & 9 deletions src/shortcircuit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import sys
from functools import partial
from typing import Dict, List, Optional, TypedDict, Union
from typing import Dict, List, Optional, Tuple, TypedDict, Union

from PySide6 import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -71,6 +71,29 @@ def size_restriction_for_index(combo_index: int) -> Dict[WormholeSize, bool]:
return {size: i < combo_index for i, size in enumerate(KNOWN_WH_SIZES)}


# Saved View > Appearance choice -> the colour scheme Qt is asked for;
# Unknown hands the choice back to the OS.
APPEARANCE_KEY = "appearance"
COLOR_SCHEMES = {
"system": QtCore.Qt.ColorScheme.Unknown,
"light": QtCore.Qt.ColorScheme.Light,
"dark": QtCore.Qt.ColorScheme.Dark,
}

# The light backgrounds come from gui_main.ui; only dark ones live here.
DARK_PRIORITY_BACKGROUNDS = {
"spinBox_prio_hs": "#2d4a33",
"spinBox_prio_ns": "#4a2d2d",
"spinBox_prio_wh": "#27374a",
"spinBox_prio_ls": "#4a452d",
}


def read_appearance(settings: QtCore.QSettings) -> str:
appearance = settings.value(APPEARANCE_KEY, "system")
return appearance if appearance in COLOR_SCHEMES else "system"


class TripwireDialog(QtWidgets.QDialog, Ui_TripwireDialog):
"""
Per-instance Tripwire edit dialog, opened from MappersDialog.
Expand Down Expand Up @@ -403,6 +426,12 @@ def __init__(self, parent=None):
self.global_proxy: str = ""
self.mapper_configs: List[MapperConfig] = []
self.mapper_states: Dict[str, StateMapper] = {}
self.appearance: str = "system"
self._label_states: Dict[QtWidgets.QLabel, Tuple[str, MessageType]] = {}
self._light_priority_sheets: Dict[str, str] = {
name: getattr(self, name).styleSheet()
for name in DARK_PRIORITY_BACKGROUNDS
}

self.state_eve_connection = StateEVEConnection({
"connected": False, "char_name": None, "error": None
Expand Down Expand Up @@ -555,6 +584,53 @@ def additional_gui_setup(self):
self.table_item_selection_changed
)

# View menu
menu_appearance = self.menubar.addMenu("&View").addMenu("&Appearance")
self.appearance_group = QtGui.QActionGroup(self)
for appearance in COLOR_SCHEMES:
action = menu_appearance.addAction(appearance.capitalize())
action.setCheckable(True)
action.setData(appearance)
self.appearance_group.addAction(action)
self.appearance_group.triggered.connect(
lambda action: self.set_appearance(action.data())
)
self.set_appearance(self.appearance)

def set_appearance(self, appearance: str):
self.appearance = appearance
for action in self.appearance_group.actions():
action.setChecked(action.data() == appearance)
QtGui.QGuiApplication.styleHints().setColorScheme(
COLOR_SCHEMES[appearance]
)
# No palette change event when the drawn palette stays the same.
self._apply_scheme_colors()

@staticmethod
def _is_dark() -> bool:
# Judge by what is drawn: some styles (windowsvista, Windows 10's
# default) report a Dark scheme yet keep a light palette.
palette = QtGui.QGuiApplication.palette()
window = palette.color(QtGui.QPalette.ColorRole.Window)
return window.lightness() < 128

def _apply_scheme_colors(self):
dark = self._is_dark()
for name, background in DARK_PRIORITY_BACKGROUNDS.items():
getattr(self, name).setStyleSheet(
"QSpinBox { background-color: %s; }" % background
if dark else self._light_priority_sheets[name]
)
for label, (message, message_type) in self._label_states.items():
self._label_message(label, message, message_type)
# A rich-text label takes its link colour from the palette only when its
# text is set, and setText() ignores unchanged text. label_10 is the only
# link label in the main window.
link_text = self.label_10.text()
self.label_10.clear()
self.label_10.setText(link_text)

def read_settings_mappers(self):
self.global_proxy = self.settings.value('proxy') or ""
migrated = migrate_legacy(self.settings)
Expand Down Expand Up @@ -617,6 +693,9 @@ def read_settings(self):
self.spinBox_prio_ns.setValue(int(self.settings.value("prio_ns", "1")))
self.spinBox_prio_wh.setValue(int(self.settings.value("prio_wh", "1")))

# Appearance
self.appearance = read_appearance(self.settings)

self.settings.endGroup()

def write_settings_mappers(self):
Expand Down Expand Up @@ -670,6 +749,9 @@ def write_settings(self):
self.settings.setValue("prio_ns", self.spinBox_prio_ns.value())
self.settings.setValue("prio_wh", self.spinBox_prio_wh.value())

# Appearance
self.settings.setValue(APPEARANCE_KEY, self.appearance)

self.settings.endGroup()

def _message_box(self, title, text):
Expand All @@ -678,28 +760,33 @@ def _message_box(self, title, text):
msg_box.setText(text)
return msg_box.exec()

@staticmethod
def _label_message(label, message, message_type):
def _label_message(self, label, message, message_type):
# FIXME(secondfry): set only color, not entire stylesheet.
self._label_states[label] = (message, message_type)
dark = self._is_dark()
if message_type == MessageType.OK:
label.setStyleSheet("QLabel {color: green;}")
label.setStyleSheet(
"QLabel {color: %s;}" % ("#81c784" if dark else "green")
)
elif message_type == MessageType.ERROR:
label.setStyleSheet("QLabel {color: red;}")
label.setStyleSheet(
"QLabel {color: %s;}" % ("#e57373" if dark else "red")
)
else:
label.setStyleSheet("QLabel {color: black;}")
label.setStyleSheet("")
label.setText(message)

def _avoid_message(self, message, message_type):
self.statusBar().showMessage(message, 5000)

def _path_message(self, message, message_type):
MainWindow._label_message(self.label_status, message, message_type)
self._label_message(self.label_status, message, message_type)

def _status_eve_connection(self, message, message_type=MessageType.INFO):
MainWindow._label_message(self.status_eve_connection, message, message_type)
self._label_message(self.status_eve_connection, message, message_type)

def _status_mappers(self, message, message_type=MessageType.INFO):
MainWindow._label_message(self.status_mappers, message, message_type)
self._label_message(self.status_mappers, message, message_type)

def avoidance_enabled(self) -> bool:
return self.groupBox_avoidance.isChecked()
Expand Down Expand Up @@ -775,6 +862,9 @@ def add_data_to_table(self, route):
item.setIcon(self.icon_wormhole)

item.setBackground(color)
# The row colors above are fixed light pastels regardless of theme,
# so force dark text to keep them legible in dark mode.
item.setForeground(QtGui.QColor(0, 0, 0))
self.tableWidget_path.setItem(route_step_id, ui_col_id, item)
ui_col_id += 1

Expand Down Expand Up @@ -1205,6 +1295,13 @@ def version_check_done(self, latest):
)
)

def changeEvent(self, event):
# The window follows the application palette, so a scheme switch (or a
# live OS switch under System) arrives here as PaletteChange.
if event.type() == QtCore.QEvent.Type.PaletteChange:
self._apply_scheme_colors()
super().changeEvent(event)

# event: QCloseEvent
def closeEvent(self, event):
self.write_settings()
Expand Down
23 changes: 23 additions & 0 deletions src/shortcircuit/model/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

# The suite is headless: select the Qt platform plugin before Qt is imported.
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")

import pytest
from PySide6 import QtCore, QtWidgets


@pytest.fixture(scope="session")
def qapp():
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
yield app


@pytest.fixture
def make_settings(tmp_path):
def make(**values):
settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat)
for key, value in values.items():
settings.setValue(key, value)
return settings
return make
25 changes: 8 additions & 17 deletions src/shortcircuit/model/test_app_settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# test_app_settings.py
"""The "Wormhole size at least" combo: its saved index and the sizes it blocks."""

from PySide6 import QtCore

from shortcircuit.app import (
LEGACY_WHSIZE_KEY,
WHSIZE_KEY,
Expand All @@ -14,13 +12,6 @@
FREIGHTER, CAPITAL, IGNORE_WORMHOLES = 3, 4, 5


def _settings(tmp_path, **values):
settings = QtCore.QSettings(str(tmp_path / 'settings.ini'), QtCore.QSettings.IniFormat)
for key, value in values.items():
settings.setValue(key, value)
return settings


def _allowed(combo_index):
return {size for size, blocked in size_restriction_for_index(combo_index).items() if not blocked}

Expand All @@ -38,18 +29,18 @@ def test_ignore_wormholes_blocks_every_size():
assert len(_allowed(0)) == 5


def test_legacy_ignore_wormholes_is_kept(tmp_path):
assert read_whsize_index(_settings(tmp_path, **{LEGACY_WHSIZE_KEY: 4})) == IGNORE_WORMHOLES
def test_legacy_ignore_wormholes_is_kept(make_settings):
assert read_whsize_index(make_settings(**{LEGACY_WHSIZE_KEY: 4})) == IGNORE_WORMHOLES


def test_legacy_xlarge_becomes_freighter(tmp_path):
assert read_whsize_index(_settings(tmp_path, **{LEGACY_WHSIZE_KEY: 3})) == FREIGHTER
def test_legacy_xlarge_becomes_freighter(make_settings):
assert read_whsize_index(make_settings(**{LEGACY_WHSIZE_KEY: 3})) == FREIGHTER


def test_new_key_wins_over_legacy(tmp_path):
settings = _settings(tmp_path, **{LEGACY_WHSIZE_KEY: 4, WHSIZE_KEY: CAPITAL})
def test_new_key_wins_over_legacy(make_settings):
settings = make_settings(**{LEGACY_WHSIZE_KEY: 4, WHSIZE_KEY: CAPITAL})
assert read_whsize_index(settings) == CAPITAL


def test_no_saved_index_is_small(tmp_path):
assert read_whsize_index(_settings(tmp_path)) == 0
def test_no_saved_index_is_small(make_settings):
assert read_whsize_index(make_settings()) == 0
Loading
Loading