Skip to content

Commit

Permalink
Added generic SingletonDialog class for managing a single dialog inst…
Browse files Browse the repository at this point in the history
…ance

Use this for the options dialog instead of the custom implementation in MainWindow.
  • Loading branch information
phw committed Dec 12, 2019
1 parent 8621372 commit 086da47
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
22 changes: 22 additions & 0 deletions picard/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ def save_geometry(self):
config.persist[self.opt_name()] = self.saveGeometry()


class SingletonDialog:
_instance = None

@classmethod
def get_instance(cls, *args, **kwargs):
if not cls._instance:
cls._instance = cls(*args, **kwargs)
cls._instance.finished.connect(cls._on_dialog_finished)
return cls._instance

@classmethod
def show_instance(cls, *args, **kwargs):
instance = cls.get_instance(*args, **kwargs)
instance.show()
instance.raise_()
instance.activateWindow()

@classmethod
def _on_dialog_finished(cls):
cls._instance = None


class PicardDialog(QtWidgets.QDialog, PreserveGeometry):

flags = QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint
Expand Down
11 changes: 1 addition & 10 deletions picard/ui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def setupUi(self):

self.log_dialog = LogView(self)
self.history_dialog = HistoryView(self)
self.optionsDialog = None

bottomLayout = QtWidgets.QHBoxLayout()
bottomLayout.setContentsMargins(0, 0, 0, 0)
Expand Down Expand Up @@ -907,15 +906,7 @@ def show_about(self):
self.show_options("about")

def show_options(self, page=None):
if not self.optionsDialog:
self.optionsDialog = OptionsDialog(page, self)
self.optionsDialog.finished.connect(self.on_options_closed)
self.optionsDialog.show()
self.optionsDialog.raise_()
self.optionsDialog.activateWindow()

def on_options_closed(self):
self.optionsDialog = None
OptionsDialog.show_instance(page, self)

def show_help(self):
webbrowser2.goto('documentation')
Expand Down
3 changes: 2 additions & 1 deletion picard/ui/options/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from picard.ui import (
HashableTreeWidgetItem,
PicardDialog,
SingletonDialog,
)
from picard.ui.options import ( # noqa: F401 # pylint: disable=unused-import
OptionsCheckError,
Expand Down Expand Up @@ -63,7 +64,7 @@
from picard.ui.util import StandardButton


class OptionsDialog(PicardDialog):
class OptionsDialog(PicardDialog, SingletonDialog):

autorestore = False

Expand Down

0 comments on commit 086da47

Please sign in to comment.