Add common functionality to open help to PicardDialog

This commit is contained in:
Philipp Wolfer
2021-05-04 14:19:12 +02:00
parent 2702229bf2
commit 4a87eda65b
3 changed files with 28 additions and 26 deletions

View File

@@ -7,7 +7,7 @@
# Copyright (C) 2014, 2018 Laurent Monin
# Copyright (C) 2016-2018 Sambhav Kothari
# Copyright (C) 2018 Vishal Choudhary
# Copyright (C) 2019-2020 Philipp Wolfer
# Copyright (C) 2019-2021 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -36,11 +36,15 @@ from picard.config import (
Option,
get_config,
)
from picard.const import DOCS_BASE_URL
from picard.const.sys import (
IS_MACOS,
IS_WIN,
)
from picard.util import restore_method
from picard.util import (
restore_method,
webbrowser2,
)
if IS_MACOS:
@@ -105,6 +109,7 @@ class SingletonDialog:
class PicardDialog(QtWidgets.QDialog, PreserveGeometry):
help_url = None
flags = QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint
def __init__(self, parent=None):
@@ -113,9 +118,21 @@ class PicardDialog(QtWidgets.QDialog, PreserveGeometry):
def keyPressEvent(self, event):
if event.matches(QtGui.QKeySequence.Close):
self.close()
elif event.matches(QtGui.QKeySequence.HelpContents) and self.help_url:
self.show_help()
else:
super().keyPressEvent(event)
def show_help(self):
if self.help_url:
url = self.help_url
if url.startswith('/'):
url = DOCS_BASE_URL + url
if url.startswith('goto://'):
webbrowser2.goto(url[7:])
else:
webbrowser2.open(url)
# With py3, QObjects are no longer hashable unless they have
# an explicit __hash__ implemented.

View File

@@ -4,7 +4,7 @@
#
# Copyright (C) 2006-2008, 2011 Lukáš Lalinský
# Copyright (C) 2008-2009 Nikolai Prokoschenko
# Copyright (C) 2008-2009, 2018-2020 Philipp Wolfer
# Copyright (C) 2008-2009, 2018-2021 Philipp Wolfer
# Copyright (C) 2011 Pavan Chander
# Copyright (C) 2011-2012, 2019 Wieland Hoffmann
# Copyright (C) 2011-2013 Michael Wiencek
@@ -31,7 +31,6 @@
from PyQt5 import (
QtCore,
QtGui,
QtWidgets,
)
@@ -42,11 +41,7 @@ from picard.config import (
TextOption,
get_config,
)
from picard.const import DOCS_BASE_URL
from picard.util import (
restore_method,
webbrowser2,
)
from picard.util import restore_method
from picard.ui import (
HashableTreeWidgetItem,
@@ -137,7 +132,7 @@ class OptionsDialog(PicardDialog, SingletonDialog):
self.ui.buttonbox.rejected.connect(self.reject)
self.ui.reset_all_button.clicked.connect(self.confirm_reset_all)
self.ui.reset_button.clicked.connect(self.confirm_reset)
self.ui.buttonbox.helpRequested.connect(self.help)
self.ui.buttonbox.helpRequested.connect(self.show_help)
self.pages = []
for Page in page_classes:
@@ -175,12 +170,6 @@ class OptionsDialog(PicardDialog, SingletonDialog):
self.disable_page(page.NAME)
self.ui.pages_tree.setCurrentItem(self.default_item)
def keyPressEvent(self, event):
if event.matches(QtGui.QKeySequence.HelpContents):
self.help()
else:
super().keyPressEvent(event)
def switch_page(self):
items = self.ui.pages_tree.selectedItems()
if items:
@@ -193,7 +182,8 @@ class OptionsDialog(PicardDialog, SingletonDialog):
item = self.page_to_item[name]
item.setDisabled(True)
def help(self):
@property
def help_url(self):
current_page = self.ui.pages_stack.currentWidget()
url = current_page.HELP_URL
# If URL is empty, use the first non empty parent help URL.
@@ -201,10 +191,8 @@ class OptionsDialog(PicardDialog, SingletonDialog):
current_page = self.item_to_page[self.page_to_item[current_page.PARENT]]
url = current_page.HELP_URL
if not url:
url = DOCS_BASE_URL
elif url.startswith('/'):
url = DOCS_BASE_URL + url
webbrowser2.open(url)
url = '/config/configuration.html'
return url
def accept(self):
for page in self.pages:

View File

@@ -37,7 +37,6 @@ from picard.config import (
get_config,
)
from picard.script.parser import normalize_tagname
from picard.util import webbrowser2
from picard.util.tags import display_tag_name
from picard.ui import PicardDialog
@@ -100,6 +99,7 @@ class TagMatchExpression:
class TagsFromFileNamesDialog(PicardDialog):
autorestore = False
help_url = 'goto://doc_tags_from_filenames'
options = [
TextOption("persist", "tags_from_filenames_format", ""),
@@ -134,7 +134,7 @@ class TagsFromFileNamesDialog(PicardDialog):
self.ui.buttonbox.addButton(StandardButton(StandardButton.CANCEL), QtWidgets.QDialogButtonBox.RejectRole)
self.ui.buttonbox.accepted.connect(self.accept)
self.ui.buttonbox.rejected.connect(self.reject)
self.ui.buttonbox.helpRequested.connect(self.help)
self.ui.buttonbox.helpRequested.connect(self.show_help)
self.ui.preview.clicked.connect(self.preview)
self.ui.files.setHeaderLabels([_("File Name")])
self.files = files
@@ -168,6 +168,3 @@ class TagsFromFileNamesDialog(PicardDialog):
config = get_config()
config.persist["tags_from_filenames_format"] = self.ui.format.currentText()
super().accept()
def help(self):
webbrowser2.goto('doc_tags_from_filenames')