Skip to content

Commit

Permalink
Implement a faster FileDialog.getOpenFileName() #61
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltrails committed Sep 23, 2023
1 parent bc241f9 commit b4b31e8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Version History
* 1.11.2
* Encode translations in plain text rather than escaped XML (for easier editing).
* Tidy up of app-icon's auto-lux indicator and transitioning-preset indicator.
* Icon/device-chooser-dialog: init-time reduced from 30 to 5 seconds for users with large home folders (issue #61).

* 1.11.1
* Fix Preset text size in tray icon.
Expand Down
1 change: 1 addition & 0 deletions vdu_controls.changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Tue 21 Sep 2023 03:59:00 UTC - Michael Hamilton <michael@actrix.gen.nz>
- Version1.11.2
* Encode translations in plain text rather than escaped XML (for easier editing).
* Tidy up of app-icon's auto-lux indicator and transitioning-preset indicator.
* Icon/device-chooser-dialog: init-time reduced from 30 to 5 seconds for users with large home folders (issue #61).

-------------------------------------------------------------------
Tue 12 Sep 2023 20:59:00 UTC - Michael Hamilton <michael@actrix.gen.nz>
Expand Down
23 changes: 20 additions & 3 deletions vdu_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@
from typing import List, Tuple, Mapping, Type, Dict, Callable, Any, NewType
from urllib.error import URLError

from PyQt5 import QtCore
from PyQt5 import QtNetwork
from PyQt5.QtCore import Qt, QCoreApplication, QThread, pyqtSignal, QProcess, QRegExp, QPoint, QObject, QEvent, \
QSettings, QSize, QTimer, QTranslator, QLocale, QT_TR_NOOP, QVariant
Expand Down Expand Up @@ -3549,6 +3550,22 @@ def get_preset(self, preset_number: int) -> Preset | None:
return None


class FasterFileDialog(QFileDialog): # Takes 5 seconds versus 30+ seconds for QFileDilog.getOpenFileName() on KDE.
os.putenv('QT_LOGGING_RULES', 'kf.kio.widgets.kdirmodel.warning=false') # annoying KDE message

@staticmethod
def getOpenFileName(parent: QWidget | None = None, caption: str = '', directory: str = '', filter: str = '',
initial_filter: str = '', options: QFileDialog.Options | QFileDialog.Option = 0) -> Tuple[str, str]:
try: # Get rid of annoying message: 'qtimeline::start: already running'
original_handler = QtCore.qInstallMessageHandler(lambda mode, context, message: None)
dialog = QFileDialog(parent=parent, caption=caption, directory=directory, filter=filter, options=options)
dialog.setOption(QFileDialog.ReadOnly | options) # Makes no difference
dialog.setFileMode(QFileDialog.ExistingFile)
return (dialog.selectedFiles()[0], filter) if dialog.exec() else ('', '') # match QFileDilog.getOpenFileName()
finally:
QtCore.qInstallMessageHandler(original_handler)


class MessageBox(QMessageBox):
def __init__(self, icon: QIcon, buttons: QMessageBox.StandardButtons = QMessageBox.NoButton,
default: QMessageBox.StandardButton | None = None) -> None:
Expand Down Expand Up @@ -3749,8 +3766,8 @@ def choose_preset_icon_action(self) -> None:
PresetsDialog.get_instance().setDisabled(True)
PresetsDialog.get_instance().status_message(TIME_CLOCK_SYMBOL + ' ' + tr("Select an icon..."))
QApplication.processEvents()
icon_file = QFileDialog.getOpenFileName(self, tr('Icon SVG or PNG file'), self.last_icon_dir.as_posix(),
'SVG or PNG (*.svg *.png)')
icon_file = FasterFileDialog.getOpenFileName(self, tr('Icon SVG or PNG file'), self.last_icon_dir.as_posix(),
'SVG or PNG (*.svg *.png)')
self.last_selected_icon_path = Path(icon_file[0]) if icon_file[0] != '' else None
if self.last_selected_icon_path:
self.last_icon_dir = self.last_selected_icon_path.parent
Expand Down Expand Up @@ -5989,7 +6006,7 @@ def lux_changed(lux: int) -> None:
self.status_layout.addWidget(self.status_bar)

def choose_device() -> None:
device_name = QFileDialog.getOpenFileName(self, tr("Select a tty device or fifo"), "/dev/ttyUSB0")[0]
device_name = FasterFileDialog.getOpenFileName(self, tr("Select a tty device or fifo"), "/dev/ttyUSB0")[0]
device_name = self.validate_device(device_name)
if device_name != '':
if device_name != self.lux_config.get('lux-meter', 'lux-device', fallback=''):
Expand Down

0 comments on commit b4b31e8

Please sign in to comment.