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
3 changes: 2 additions & 1 deletion src/addon/ankiaddonconfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ conf.clone() # returns a deepcopy of the config dictionary

## Contributing

Please run mypy and black before creating a pull request. You may need to run `python -m pip install aqt PyQt5-stubs` for mypy checks to work.
Please run mypy and black before creating a pull request. You may need to run `python -m pip install aqt` for mypy checks to work. You may need to uninstall `PyQt5-stubs` and `PyQt5` packages for mypy checks to work.

```
python -m mypy .
python -m black .
Expand Down
45 changes: 41 additions & 4 deletions src/addon/ankiaddonconfig/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class ConfigWindow(QDialog):
def __init__(self, conf: "ConfigManager") -> None:
QDialog.__init__(self, mw, Qt.WindowType.Window) # type: ignore
self.setModal(True)
self.conf = conf
self.mgr = mw.addonManager
self.widget_updates: List[Callable[[], None]] = []
Expand All @@ -27,7 +28,6 @@ def __init__(self, conf: "ConfigManager") -> None:
self.geom_key = f"addonconfig-{conf.addon_name}"

self.setWindowTitle(f"Config for {conf.addon_name}")
self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
self.setup()

def setup(self) -> None:
Expand All @@ -45,7 +45,6 @@ def setup(self) -> None:
self.setup_buttons(self.btn_layout)

def setup_buttons(self, btn_box: "ConfigLayout") -> None:

self.advanced_btn = QPushButton("Advanced")
self.advanced_btn.clicked.connect(self.on_advanced)
btn_box.addWidget(self.advanced_btn)
Expand Down Expand Up @@ -212,7 +211,7 @@ def update() -> None:
checkbox.stateChanged.connect(
lambda s: self.conf.set(
key,
s == (Qt.CheckState.Checked.value if QT6 else Qt.CheckState.Checked),
s == (Qt.CheckState.Checked.value if QT6 else Qt.CheckState.Checked), # type: ignore
)
)
self.addWidget(checkbox)
Expand Down Expand Up @@ -459,6 +458,44 @@ def get_path() -> None:

return (line_edit, button)

def shortcut_input(
self, key: str, description: Optional[str] = None, tooltip: Optional[str] = None
) -> Tuple[QKeySequenceEdit, QPushButton]:
edit = QKeySequenceEdit()

if description is not None:
row = self.hlayout()
row.text(description, tooltip=tooltip)

def update() -> None:
val = self.conf.get(key)
if not isinstance(val, str):
raise InvalidConfigValueError(key, "str", val)
val = val.replace(" ", "")
edit.setKeySequence(val)

self.widget_updates.append(update)

edit.keySequenceChanged.connect( # type: ignore
lambda s: self.conf.set(key, edit.keySequence().toString())
)

def on_shortcut_clear_btn_click() -> None:
edit.clear()

shortcut_clear_btn = QPushButton("Clear")
shortcut_clear_btn.setSizePolicy(
QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed
)
shortcut_clear_btn.clicked.connect(on_shortcut_clear_btn_click) # type: ignore

layout = QHBoxLayout()
layout.addWidget(edit)
layout.addWidget(shortcut_clear_btn)

self.addLayout(layout)
return edit, shortcut_clear_btn

# Layout widgets

def text(
Expand Down Expand Up @@ -631,7 +668,7 @@ def scroll_layout(
"""Legacy. Adds QScrollArea > QWidget*2 > ConfigLayout, returns the layout."""
return self._scroll_layout(
QSizePolicy.Policy.Expanding if horizontal else QSizePolicy.Policy.Minimum,
QSizePolicy.Policy.Expanding if vertical else QSizePolicy.Minimum,
QSizePolicy.Policy.Expanding if vertical else QSizePolicy.Policy.Minimum,
Qt.ScrollBarPolicy.ScrollBarAsNeeded,
Qt.ScrollBarPolicy.ScrollBarAsNeeded,
)