forked from openlp/openlp
Pathedit line endings
This commit is contained in:
parent
03bcc194ea
commit
a7750e680b
@ -1,222 +1,222 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# OpenLP - Open Source Lyrics Projection #
|
# OpenLP - Open Source Lyrics Projection #
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
# Copyright (c) 2008-2017 OpenLP Developers #
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
# This program is free software; you can redistribute it and/or modify it #
|
# This program is free software; you can redistribute it and/or modify it #
|
||||||
# under the terms of the GNU General Public License as published by the Free #
|
# under the terms of the GNU General Public License as published by the Free #
|
||||||
# Software Foundation; version 2 of the License. #
|
# Software Foundation; version 2 of the License. #
|
||||||
# #
|
# #
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
||||||
# more details. #
|
# more details. #
|
||||||
# #
|
# #
|
||||||
# You should have received a copy of the GNU General Public License along #
|
# You should have received a copy of the GNU General Public License along #
|
||||||
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
||||||
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
||||||
###############################################################################
|
###############################################################################
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from PyQt5 import QtCore, QtWidgets
|
from PyQt5 import QtCore, QtWidgets
|
||||||
|
|
||||||
from openlp.core.common import UiStrings, translate
|
from openlp.core.common import UiStrings, translate
|
||||||
from openlp.core.common.path import path_to_str, str_to_path
|
from openlp.core.common.path import path_to_str, str_to_path
|
||||||
from openlp.core.lib import build_icon
|
from openlp.core.lib import build_icon
|
||||||
from openlp.core.ui.lib.filedialogpatches import PQFileDialog
|
from openlp.core.ui.lib.filedialogpatches import PQFileDialog
|
||||||
|
|
||||||
|
|
||||||
class PathType(Enum):
|
class PathType(Enum):
|
||||||
Files = 1
|
Files = 1
|
||||||
Directories = 2
|
Directories = 2
|
||||||
|
|
||||||
|
|
||||||
class PathEdit(QtWidgets.QWidget):
|
class PathEdit(QtWidgets.QWidget):
|
||||||
"""
|
"""
|
||||||
The :class:`~openlp.core.ui.lib.pathedit.PathEdit` class subclasses QWidget to create a custom widget for use when
|
The :class:`~openlp.core.ui.lib.pathedit.PathEdit` class subclasses QWidget to create a custom widget for use when
|
||||||
a file or directory needs to be selected.
|
a file or directory needs to be selected.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pathChanged = QtCore.pyqtSignal(Path)
|
pathChanged = QtCore.pyqtSignal(Path)
|
||||||
|
|
||||||
def __init__(self, parent=None, path_type=PathType.Files, default_path=None, dialog_caption=None, show_revert=True):
|
def __init__(self, parent=None, path_type=PathType.Files, default_path=None, dialog_caption=None, show_revert=True):
|
||||||
"""
|
"""
|
||||||
Initialise the PathEdit widget
|
Initialise the PathEdit widget
|
||||||
|
|
||||||
:param parent: The parent of the widget. This is just passed to the super method.
|
:param parent: The parent of the widget. This is just passed to the super method.
|
||||||
:type parent: QWidget or None
|
:type parent: QWidget or None
|
||||||
|
|
||||||
:param dialog_caption: Used to customise the caption in the QFileDialog.
|
:param dialog_caption: Used to customise the caption in the QFileDialog.
|
||||||
:type dialog_caption: str
|
:type dialog_caption: str
|
||||||
|
|
||||||
:param default_path: The default path. This is set as the path when the revert button is clicked
|
:param default_path: The default path. This is set as the path when the revert button is clicked
|
||||||
:type default_path: pathlib.Path
|
:type default_path: pathlib.Path
|
||||||
|
|
||||||
:param show_revert: Used to determine if the 'revert button' should be visible.
|
:param show_revert: Used to determine if the 'revert button' should be visible.
|
||||||
:type show_revert: bool
|
:type show_revert: bool
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.default_path = default_path
|
self.default_path = default_path
|
||||||
self.dialog_caption = dialog_caption
|
self.dialog_caption = dialog_caption
|
||||||
self._path_type = path_type
|
self._path_type = path_type
|
||||||
self._path = None
|
self._path = None
|
||||||
self.filters = '{all_files} (*)'.format(all_files=UiStrings().AllFiles)
|
self.filters = '{all_files} (*)'.format(all_files=UiStrings().AllFiles)
|
||||||
self._setup(show_revert)
|
self._setup(show_revert)
|
||||||
|
|
||||||
def _setup(self, show_revert):
|
def _setup(self, show_revert):
|
||||||
"""
|
"""
|
||||||
Set up the widget
|
Set up the widget
|
||||||
:param show_revert: Show or hide the revert button
|
:param show_revert: Show or hide the revert button
|
||||||
:type show_revert: bool
|
:type show_revert: bool
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
widget_layout = QtWidgets.QHBoxLayout()
|
widget_layout = QtWidgets.QHBoxLayout()
|
||||||
widget_layout.setContentsMargins(0, 0, 0, 0)
|
widget_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
self.line_edit = QtWidgets.QLineEdit(self)
|
self.line_edit = QtWidgets.QLineEdit(self)
|
||||||
widget_layout.addWidget(self.line_edit)
|
widget_layout.addWidget(self.line_edit)
|
||||||
self.browse_button = QtWidgets.QToolButton(self)
|
self.browse_button = QtWidgets.QToolButton(self)
|
||||||
self.browse_button.setIcon(build_icon(':/general/general_open.png'))
|
self.browse_button.setIcon(build_icon(':/general/general_open.png'))
|
||||||
widget_layout.addWidget(self.browse_button)
|
widget_layout.addWidget(self.browse_button)
|
||||||
self.revert_button = QtWidgets.QToolButton(self)
|
self.revert_button = QtWidgets.QToolButton(self)
|
||||||
self.revert_button.setIcon(build_icon(':/general/general_revert.png'))
|
self.revert_button.setIcon(build_icon(':/general/general_revert.png'))
|
||||||
self.revert_button.setVisible(show_revert)
|
self.revert_button.setVisible(show_revert)
|
||||||
widget_layout.addWidget(self.revert_button)
|
widget_layout.addWidget(self.revert_button)
|
||||||
self.setLayout(widget_layout)
|
self.setLayout(widget_layout)
|
||||||
# Signals and Slots
|
# Signals and Slots
|
||||||
self.browse_button.clicked.connect(self.on_browse_button_clicked)
|
self.browse_button.clicked.connect(self.on_browse_button_clicked)
|
||||||
self.revert_button.clicked.connect(self.on_revert_button_clicked)
|
self.revert_button.clicked.connect(self.on_revert_button_clicked)
|
||||||
self.line_edit.editingFinished.connect(self.on_line_edit_editing_finished)
|
self.line_edit.editingFinished.connect(self.on_line_edit_editing_finished)
|
||||||
self.update_button_tool_tips()
|
self.update_button_tool_tips()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path(self):
|
def path(self):
|
||||||
"""
|
"""
|
||||||
A property getter method to return the selected path.
|
A property getter method to return the selected path.
|
||||||
|
|
||||||
:return: The selected path
|
:return: The selected path
|
||||||
:rtype: pathlib.Path
|
:rtype: pathlib.Path
|
||||||
"""
|
"""
|
||||||
return self._path
|
return self._path
|
||||||
|
|
||||||
@path.setter
|
@path.setter
|
||||||
def path(self, path):
|
def path(self, path):
|
||||||
"""
|
"""
|
||||||
A Property setter method to set the selected path
|
A Property setter method to set the selected path
|
||||||
|
|
||||||
:param path: The path to set the widget to
|
:param path: The path to set the widget to
|
||||||
:type path: pathlib.Path
|
:type path: pathlib.Path
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
self._path = path
|
self._path = path
|
||||||
text = path_to_str(path)
|
text = path_to_str(path)
|
||||||
self.line_edit.setText(text)
|
self.line_edit.setText(text)
|
||||||
self.line_edit.setToolTip(text)
|
self.line_edit.setToolTip(text)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path_type(self):
|
def path_type(self):
|
||||||
"""
|
"""
|
||||||
A property getter method to return the path_type. Path type allows you to sepecify if the user is restricted to
|
A property getter method to return the path_type. Path type allows you to sepecify if the user is restricted to
|
||||||
selecting a file or directory.
|
selecting a file or directory.
|
||||||
|
|
||||||
:return: The type selected
|
:return: The type selected
|
||||||
:rtype: PathType
|
:rtype: PathType
|
||||||
"""
|
"""
|
||||||
return self._path_type
|
return self._path_type
|
||||||
|
|
||||||
@path_type.setter
|
@path_type.setter
|
||||||
def path_type(self, path_type):
|
def path_type(self, path_type):
|
||||||
"""
|
"""
|
||||||
A Property setter method to set the path type
|
A Property setter method to set the path type
|
||||||
|
|
||||||
:param path_type: The type of path to select
|
:param path_type: The type of path to select
|
||||||
:type path_type: PathType
|
:type path_type: PathType
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
self._path_type = path_type
|
self._path_type = path_type
|
||||||
self.update_button_tool_tips()
|
self.update_button_tool_tips()
|
||||||
|
|
||||||
def update_button_tool_tips(self):
|
def update_button_tool_tips(self):
|
||||||
"""
|
"""
|
||||||
Called to update the tooltips on the buttons. This is changing path types, and when the widget is initalised
|
Called to update the tooltips on the buttons. This is changing path types, and when the widget is initalised
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
if self._path_type == PathType.Directories:
|
if self._path_type == PathType.Directories:
|
||||||
self.browse_button.setToolTip(translate('OpenLP.PathEdit', 'Browse for directory.'))
|
self.browse_button.setToolTip(translate('OpenLP.PathEdit', 'Browse for directory.'))
|
||||||
self.revert_button.setToolTip(translate('OpenLP.PathEdit', 'Revert to default directory.'))
|
self.revert_button.setToolTip(translate('OpenLP.PathEdit', 'Revert to default directory.'))
|
||||||
else:
|
else:
|
||||||
self.browse_button.setToolTip(translate('OpenLP.PathEdit', 'Browse for file.'))
|
self.browse_button.setToolTip(translate('OpenLP.PathEdit', 'Browse for file.'))
|
||||||
self.revert_button.setToolTip(translate('OpenLP.PathEdit', 'Revert to default file.'))
|
self.revert_button.setToolTip(translate('OpenLP.PathEdit', 'Revert to default file.'))
|
||||||
|
|
||||||
def on_browse_button_clicked(self):
|
def on_browse_button_clicked(self):
|
||||||
"""
|
"""
|
||||||
A handler to handle a click on the browse button.
|
A handler to handle a click on the browse button.
|
||||||
|
|
||||||
Show the QFileDialog and process the input from the user
|
Show the QFileDialog and process the input from the user
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
caption = self.dialog_caption
|
caption = self.dialog_caption
|
||||||
path = None
|
path = None
|
||||||
if self._path_type == PathType.Directories:
|
if self._path_type == PathType.Directories:
|
||||||
if not caption:
|
if not caption:
|
||||||
caption = translate('OpenLP.PathEdit', 'Select Directory')
|
caption = translate('OpenLP.PathEdit', 'Select Directory')
|
||||||
path = PQFileDialog.getExistingDirectory(self, caption, self._path, PQFileDialog.ShowDirsOnly)
|
path = PQFileDialog.getExistingDirectory(self, caption, self._path, PQFileDialog.ShowDirsOnly)
|
||||||
elif self._path_type == PathType.Files:
|
elif self._path_type == PathType.Files:
|
||||||
if not caption:
|
if not caption:
|
||||||
caption = self.dialog_caption = translate('OpenLP.PathEdit', 'Select File')
|
caption = self.dialog_caption = translate('OpenLP.PathEdit', 'Select File')
|
||||||
path, filter_used = PQFileDialog.getOpenFileName(self, caption, self._path, self.filters)
|
path, filter_used = PQFileDialog.getOpenFileName(self, caption, self._path, self.filters)
|
||||||
if path:
|
if path:
|
||||||
self.on_new_path(path)
|
self.on_new_path(path)
|
||||||
|
|
||||||
def on_revert_button_clicked(self):
|
def on_revert_button_clicked(self):
|
||||||
"""
|
"""
|
||||||
A handler to handle a click on the revert button.
|
A handler to handle a click on the revert button.
|
||||||
|
|
||||||
Set the new path to the value of the default_path instance variable.
|
Set the new path to the value of the default_path instance variable.
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
self.on_new_path(self.default_path)
|
self.on_new_path(self.default_path)
|
||||||
|
|
||||||
def on_line_edit_editing_finished(self):
|
def on_line_edit_editing_finished(self):
|
||||||
"""
|
"""
|
||||||
A handler to handle when the line edit has finished being edited.
|
A handler to handle when the line edit has finished being edited.
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
path = str_to_path(self.line_edit.text())
|
path = str_to_path(self.line_edit.text())
|
||||||
self.on_new_path(path)
|
self.on_new_path(path)
|
||||||
|
|
||||||
def on_new_path(self, path):
|
def on_new_path(self, path):
|
||||||
"""
|
"""
|
||||||
A method called to validate and set a new path.
|
A method called to validate and set a new path.
|
||||||
|
|
||||||
Emits the pathChanged Signal
|
Emits the pathChanged Signal
|
||||||
|
|
||||||
:param path: The new path
|
:param path: The new path
|
||||||
:type path: pathlib.Path
|
:type path: pathlib.Path
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
"""
|
"""
|
||||||
if self._path != path:
|
if self._path != path:
|
||||||
self.path = path
|
self.path = path
|
||||||
self.pathChanged.emit(path)
|
self.pathChanged.emit(path)
|
||||||
|
Loading…
Reference in New Issue
Block a user