# Path: pyweed/gui/uic/PreferencesDialog.py
# class Ui_PreferencesDialog(object):
#     def setupUi(self, PreferencesDialog):
#     def retranslateUi(self, PreferencesDialog):
# 
# Path: pyweed/preferences.py
# def safe_int(s, default=0):
#     try:
#         return int(s)
#     except:
#         return default
# 
# Path: pyweed/gui/Adapters.py
# class ComboBoxAdapter(QtCore.QObject):
#     """
#     Adapter for handling the fact that QComboBox can only display strings, but usually the "real" value
#     is something else.
#     """
# 
#     #: Signal emitted when the QComboBox value changes, passing the actual value
#     changed = QtCore.pyqtSignal(object)
# 
#     def __init__(self, comboBox, options, default=None):
#         """
#         :param comboBox: A QComboBox
#         :param options: A list of options in the form of (value, label)
#         :param default: If nothing is selected, what should be returned?
#         """
#         super(ComboBoxAdapter, self).__init__()
#         self.comboBox = comboBox
#         self.values = [option[0] for option in options]
#         if default is None:
#             default = self.values[0]
#         self.default = default
#         labels = [option[1] for option in options]
#         self.comboBox.addItems(labels)
#         self.comboBox.currentIndexChanged.connect(self.onChanged)
# 
#     def setValue(self, value):
#         self.comboBox.setCurrentIndex(self.values.index(value))
# 
#     def getValue(self, index=None):
#         if index is None:
#             index = self.comboBox.currentIndex()
#         if index < 0:
#             return self.default
#         else:
#             return self.values[index]
# 
#     @pyqtSlot(int)
#     def onChanged(self, index):
#         self.changed.emit(self.getValue(index))


from PyQt5 import QtWidgets
from pyweed.gui.uic import PreferencesDialog
from logging import getLogger
from obspy.clients.fdsn.header import URL_MAPPINGS
from pyweed.preferences import safe_int
from pyweed.gui.Adapters import ComboBoxAdapter
class PreferencesDialog(QtWidgets.QDialog, PreferencesDialog.Ui_PreferencesDialog):
    """
    Dialog window for editing preferences.
    """
    def __init__(self, pyweed, parent=None):
        super(PreferencesDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.pyweed = pyweed

        # Ordered list of all available data centers
        dcs = sorted(URL_MAPPINGS.keys())

        self.eventDataCenterAdapter = ComboBoxAdapter(
            self.eventDataCenterComboBox,
            [(dc, URL_MAPPINGS[dc]) for dc in dcs]
        )

        self.stationDataCenterAdapter = ComboBoxAdapter(
            self.stationDataCenterComboBox,
            [(dc, URL_MAPPINGS[dc]) for dc in dcs]
        )

        self.okButton.pressed.connect(self.accept)
        self.cancelButton.pressed.connect(self.reject)

    def showEvent(self, *args, **kwargs):
        """
        Perform any necessary initialization each time the dialog is opened
        """

# Path: pyweed/gui/uic/PreferencesDialog.py
# class Ui_PreferencesDialog(object):
#     def setupUi(self, PreferencesDialog):
#     def retranslateUi(self, PreferencesDialog):
# 
# Path: pyweed/preferences.py
# def safe_int(s, default=0):
#     try:
#         return int(s)
#     except:
#         return default
# 
# Path: pyweed/gui/Adapters.py
# class ComboBoxAdapter(QtCore.QObject):
#     """
#     Adapter for handling the fact that QComboBox can only display strings, but usually the "real" value
#     is something else.
#     """
# 
#     #: Signal emitted when the QComboBox value changes, passing the actual value
#     changed = QtCore.pyqtSignal(object)
# 
#     def __init__(self, comboBox, options, default=None):
#         """
#         :param comboBox: A QComboBox
#         :param options: A list of options in the form of (value, label)
#         :param default: If nothing is selected, what should be returned?
#         """
#         super(ComboBoxAdapter, self).__init__()
#         self.comboBox = comboBox
#         self.values = [option[0] for option in options]
#         if default is None:
#             default = self.values[0]
#         self.default = default
#         labels = [option[1] for option in options]
#         self.comboBox.addItems(labels)
#         self.comboBox.currentIndexChanged.connect(self.onChanged)
# 
#     def setValue(self, value):
#         self.comboBox.setCurrentIndex(self.values.index(value))
# 
#     def getValue(self, index=None):
#         if index is None:
#             index = self.comboBox.currentIndex()
#         if index < 0:
#             return self.default
#         else:
#             return self.values[index]
# 
#     @pyqtSlot(int)
#     def onChanged(self, index):
#         self.changed.emit(self.getValue(index))


# Path: pyweed/gui/PreferencesDialog.py
from PyQt5 import QtWidgets
from pyweed.gui.uic import PreferencesDialog
from logging import getLogger
from obspy.clients.fdsn.header import URL_MAPPINGS
from pyweed.preferences import safe_int
from pyweed.gui.Adapters import ComboBoxAdapter

class PreferencesDialog(QtWidgets.QDialog, PreferencesDialog.Ui_PreferencesDialog):
    """
    Dialog window for editing preferences.
    """
    def __init__(self, pyweed, parent=None):
        super(PreferencesDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.pyweed = pyweed

        # Ordered list of all available data centers
        dcs = sorted(URL_MAPPINGS.keys())

        self.eventDataCenterAdapter = ComboBoxAdapter(
            self.eventDataCenterComboBox,
            [(dc, URL_MAPPINGS[dc]) for dc in dcs]
        )

        self.stationDataCenterAdapter = ComboBoxAdapter(
            self.stationDataCenterComboBox,
            [(dc, URL_MAPPINGS[dc]) for dc in dcs]
        )

        self.okButton.pressed.connect(self.accept)
        self.cancelButton.pressed.connect(self.reject)

    def showEvent(self, *args, **kwargs):
        """
        Perform any necessary initialization each time the dialog is opened
        """

        super(PreferencesDialog, self).showEvent(*args, **kwargs)