Add shortcut and tooltip to switch

This commit is contained in:
RumovZ 2021-03-31 18:53:36 +02:00
parent fd4b5dc695
commit 52b66dc985
4 changed files with 29 additions and 7 deletions

View file

@ -101,6 +101,7 @@ browsing-suspended = Suspended
browsing-tag-duplicates = Tag Duplicates browsing-tag-duplicates = Tag Duplicates
browsing-tag-rename-warning-empty = You can't rename a tag that has no notes. browsing-tag-rename-warning-empty = You can't rename a tag that has no notes.
browsing-target-field = Target field: browsing-target-field = Target field:
browsing-toggle-cards-notes-mode = Toggle Cards/Notes Mode
browsing-toggle-mark = Toggle Mark browsing-toggle-mark = Toggle Mark
browsing-toggle-suspend = Toggle Suspend browsing-toggle-suspend = Toggle Suspend
browsing-treat-input-as-regular-expression = Treat input as regular expression browsing-treat-input-as-regular-expression = Treat input as regular expression

View file

@ -376,6 +376,8 @@ class Browser(QMainWindow):
self.table.set_view(self.form.tableView) self.table.set_view(self.form.tableView)
switch = Switch(11, tr.browsing_card_initial(), tr.browsing_note_initial()) switch = Switch(11, tr.browsing_card_initial(), tr.browsing_note_initial())
switch.setChecked(self.table.is_notes_mode()) switch.setChecked(self.table.is_notes_mode())
switch.setToolTip(tr.browsing_toggle_cards_notes_mode())
qconnect(self.form.action_toggle_mode.triggered, switch.toggle)
qconnect(switch.toggled, self.on_table_state_changed) qconnect(switch.toggled, self.on_table_state_changed)
self.form.gridLayout.addWidget(switch, 0, 0) self.form.gridLayout.addWidget(switch, 0, 0)

View file

@ -218,6 +218,8 @@
</property> </property>
<addaction name="actionUndo"/> <addaction name="actionUndo"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="action_toggle_mode"/>
<addaction name="separator"/>
<addaction name="actionSelectAll"/> <addaction name="actionSelectAll"/>
<addaction name="actionSelectNotes"/> <addaction name="actionSelectNotes"/>
<addaction name="actionInvertSelection"/> <addaction name="actionInvertSelection"/>
@ -603,6 +605,14 @@
<string>qt_accel_forget</string> <string>qt_accel_forget</string>
</property> </property>
</action> </action>
<action name="action_toggle_mode">
<property name="text">
<string>browsing_toggle_cards_notes_mode</string>
</property>
<property name="shortcut">
<string notr="true">Ctrl+M</string>
</property>
</action>
</widget> </widget>
<resources> <resources>
<include location="icons.qrc"/> <include location="icons.qrc"/>

View file

@ -9,7 +9,9 @@ from aqt.theme import theme_manager
class Switch(QAbstractButton): class Switch(QAbstractButton):
"""A horizontal slider to toggle between two states which can be denoted by short strings. """A horizontal slider to toggle between two states which can be denoted by short strings.
The left state is the default and corresponds to isChecked=False.
The left state is the default and corresponds to isChecked()=False.
The suppoorted slots are toggle(), for an animated transition, and setChecked().
""" """
_margin: int = 2 _margin: int = 2
@ -104,13 +106,20 @@ class Switch(QAbstractButton):
def mouseReleaseEvent(self, event: QMouseEvent) -> None: def mouseReleaseEvent(self, event: QMouseEvent) -> None:
super().mouseReleaseEvent(event) super().mouseReleaseEvent(event)
if event.button() == Qt.LeftButton: if event.button() == Qt.LeftButton:
self._animate_toggle()
def enterEvent(self, event: QEvent) -> None:
self.setCursor(Qt.PointingHandCursor)
super().enterEvent(event)
def toggle(self) -> None:
super().toggle()
self._animate_toggle()
def _animate_toggle(self) -> None:
animation = QPropertyAnimation(self, b"position", self) animation = QPropertyAnimation(self, b"position", self)
animation.setDuration(100) animation.setDuration(100)
animation.setStartValue(self.start_position) animation.setStartValue(self.start_position)
animation.setEndValue(self.end_position) animation.setEndValue(self.end_position)
# make triggered events execute first so the animation runs smoothly afterwards # make triggered events execute first so the animation runs smoothly afterwards
QTimer.singleShot(50, animation.start) QTimer.singleShot(50, animation.start)
def enterEvent(self, event: QEvent) -> None:
self.setCursor(Qt.PointingHandCursor)
super().enterEvent(event)