Skip to content

Commit

Permalink
Shortcut Editor: Various UIX Tweaks (#334)
Browse files Browse the repository at this point in the history
* Shortcut Editor: Move txt_name cursor position to start

* Shortcut Editor: Use custom ShortcutDialogLineEdit for flexible event overrides

* Shortcut Editor: Focus shifting adjustments

* Shortcut Editor: Fix focus with mouse and tab events

* Shortcut Editor: Set tooltip for QLineEdits

* Shortcut Editor: Update tooltip on text changed

* Shortcut Editor: Add placeholder text

* Shortcut Editor: Tooltip text fall back to placeholder text for empty LineEdit text

* Shortcut Editor: Increase dialog and App Name column width
  • Loading branch information
sonic2kk committed Jan 19, 2024
1 parent 82f8a64 commit 2944d03
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
60 changes: 55 additions & 5 deletions pupgui2/pupgui2shortcutdialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pkgutil
from collections import Counter

from PySide6.QtCore import QObject, Signal, QDataStream, QByteArray
from PySide6.QtCore import Qt, QObject, Signal, QDataStream, QByteArray
from PySide6.QtWidgets import QLineEdit
from PySide6.QtUiTools import QUiLoader

Expand All @@ -11,6 +11,43 @@
from pupgui2.util import host_path_exists


class ShortcutDialogLineEdit(QLineEdit):

def __init__(self, parent=None, default_cursor_position: int = -1, *args, **kwargs,):
super(ShortcutDialogLineEdit, self).__init__(parent, *args, **kwargs)

self.default_cursor_position = default_cursor_position
if self.default_cursor_position >= 0:
self.setCursorPosition(self.default_cursor_position)

self.was_focused = False

def focusOutEvent(self, arg__1):
super().focusOutEvent(arg__1) # Super handles focusing events etc
self.was_focused = False

updated_cursor_pos = len(self.text()) if self.default_cursor_position == -1 else self.default_cursor_position
self.setCursorPosition(updated_cursor_pos) # Move cursor back to default position (ex: start for Game Name field)

def focusInEvent(self, arg__1):
super().focusInEvent(arg__1)

# Only call focusWithTextSelection for tab focus, because mousePressEvent will capture select on mousee click focus
# mousePressEvent is fired after focusInEvent, and the click event in mousePressEvent clears the selection if we call focusWithTextSelection here
# We can call focusWithTextSelection here and set self.was_focused because you can only focus onto the same widget with a tab once, pressing tab again moves the focus away
if arg__1.reason() in [ Qt.FocusReason.TabFocusReason, Qt.FocusReason.OtherFocusReason ]:
self.focusWithTextSelection()

def mousePressEvent(self, arg__1):
super().mousePressEvent(arg__1)
self.focusWithTextSelection()

def focusWithTextSelection(self):
if not self.was_focused:
self.selectAll()
self.was_focused = True


class PupguiShortcutDialog(QObject):

def __init__(self, steam_config_folder: str, game_property_changed: Signal, parent=None):
Expand Down Expand Up @@ -43,6 +80,7 @@ def load_ui(self):

def setup_ui(self):
self.ui.tableShortcuts.setHorizontalHeaderLabels([self.tr('App Name'), self.tr('Executable'), self.tr('Start Directory'), self.tr('Icon')])
self.ui.tableShortcuts.setColumnWidth(0, 350)

self.ui.btnSave.clicked.connect(self.btn_save_clicked)
self.ui.btnClose.clicked.connect(self.btn_close_clicked)
Expand All @@ -51,10 +89,20 @@ def setup_ui(self):
self.ui.searchBox.textChanged.connect(self.search_shortcuts)

def prepare_table_row(self, i: int, shortcut: SteamApp):
txt_name = QLineEdit(shortcut.game_name)
txt_exe = QLineEdit(shortcut.shortcut_exe)
txt_path = QLineEdit(shortcut.shortcut_startdir)
txt_icon = QLineEdit(shortcut.shortcut_icon)
txt_name = ShortcutDialogLineEdit(shortcut.game_name, default_cursor_position=0)
txt_exe = ShortcutDialogLineEdit(shortcut.shortcut_exe)
txt_path = ShortcutDialogLineEdit(shortcut.shortcut_startdir)
txt_icon = ShortcutDialogLineEdit(shortcut.shortcut_icon)

txt_name.setToolTip(shortcut.game_name)
txt_exe.setToolTip(shortcut.shortcut_exe)
txt_path.setToolTip(shortcut.shortcut_startdir)
txt_icon.setToolTip(shortcut.shortcut_icon)

txt_name.setPlaceholderText(shortcut.game_name)
txt_exe.setPlaceholderText(shortcut.shortcut_exe)
txt_path.setPlaceholderText(shortcut.shortcut_startdir)
txt_icon.setPlaceholderText(shortcut.shortcut_icon)

txt_name.editingFinished.connect(lambda i=i: self.txt_changed(i, 0))
txt_exe.editingFinished.connect(lambda i=i: self.txt_changed(i, 1))
Expand Down Expand Up @@ -121,6 +169,8 @@ def txt_changed(self, index: int, col: int) -> None:
elif col == 3:
shortcut.shortcut_icon = text

self.ui.tableShortcuts.cellWidget(index, col).setToolTip(text or self.ui.tableShortcuts.cellWidget(index, col).placeholderText())

def btn_save_clicked(self):
# remove all shortcuts that have no name or executable
filtered_shortcuts = list(filter(lambda s: s.game_name != '' and s.shortcut_exe != '', self.shortcuts))
Expand Down
7 changes: 5 additions & 2 deletions pupgui2/resources/ui/pupgui2_shortcutdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
<rect>
<x>0</x>
<y>0</y>
<width>700</width>
<width>899</width>
<height>350</height>
</rect>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<property name="windowTitle">
<string>Steam Shortcut Editor</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="tableShortcuts">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
<enum>Qt::ClickFocus</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
Expand Down

0 comments on commit 2944d03

Please sign in to comment.