Skip to content

Commit

Permalink
wire "use selection" checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc W committed Jan 27, 2024
1 parent ccd6998 commit 5ab867c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions component_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def draw(self): # return a widget
# wire events
self.batch_mode_combobox.currentIndexChanged.connect(self.batch_mode_change)
self.source_field_combobox.currentIndexChanged.connect(self.source_field_change)
self.use_selection_checkbox.stateChanged.connect(self.use_selection_checkbox_change)
self.hypertts.anki_utils.wire_typing_timer(self.simple_template_input, self.simple_template_change)
self.hypertts.anki_utils.wire_typing_timer(self.advanced_template_input, self.advanced_template_change)

Expand Down Expand Up @@ -90,6 +91,9 @@ def draw_source_config(self, overall_layout):
self.source_field_combobox.addItems(self.field_list)
stack_vlayout.addWidget(self.source_field_label)
stack_vlayout.addWidget(self.source_field_combobox)
self.use_selection_checkbox = aqt.qt.QCheckBox(constants.GUI_TEXT_SOURCE_USE_SELECTION)
stack_vlayout.addWidget(aqt.qt.QLabel('Additional Settings:'))
stack_vlayout.addWidget(self.use_selection_checkbox)
stack_vlayout.addStretch()
simple_stack.setLayout(stack_vlayout)

Expand Down Expand Up @@ -150,6 +154,11 @@ def source_field_change(self, current_index):
self.batch_source_model = config_models.BatchSource(mode=constants.BatchMode.simple, source_field=field_name)
self.notify_model_update()

def use_selection_checkbox_change(self):
use_selection = self.use_selection_checkbox.isChecked()
self.batch_source_model.use_selection = use_selection
self.notify_model_update()

def simple_template_change(self):
simple_template_text = self.simple_template_input.text()
self.batch_source_model = config_models.BatchSource(mode=constants.BatchMode.template, source_template=simple_template_text)
Expand Down
1 change: 1 addition & 0 deletions config_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class BatchSource():
source_field: Optional[str] = None
source_template: Optional[str] = None
template_format_version: constants.TemplateFormatVersion = constants.TemplateFormatVersion.v1
use_selection: Optional[bool] = False

def validate(self):
if self.mode == constants.BatchMode.simple:
Expand Down
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class MappingRuleType(enum.Enum):
<b>Advanced Template:</b> fields can be combined in complex ways using Python."""

GUI_TEXT_SOURCE_FIELD_NAME = """Source Field:"""
GUI_TEXT_SOURCE_USE_SELECTION = """If text is selected, use selection instead of the full field."""
GUI_TEXT_SOURCE_SIMPLE_TEMPLATE = """Enter template using syntax {Field1} {Field2}:"""
GUI_TEXT_SOURCE_ADVANCED_TEMPLATE = """Enter template using Python syntax (advanced users only):
a simple example:
Expand Down
10 changes: 10 additions & 0 deletions test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,16 @@ def test_batch_source_1(qtbot):
expected_source_model.source_field = 'English'

assert batch_source.batch_source_model == expected_source_model

# enable "use selection"
batch_source.use_selection_checkbox.setChecked(True)
expected_source_model.use_selection = True
assert batch_source.batch_source_model == expected_source_model

# disable "use selection"
batch_source.use_selection_checkbox.setChecked(False)
expected_source_model.use_selection = False
assert batch_source.batch_source_model == expected_source_model

# select template mode
batch_source.batch_mode_combobox.setCurrentText('template')
Expand Down

0 comments on commit 5ab867c

Please sign in to comment.