# Path: OpenNumismat/Tools/DialogDecorators.py
# def storeDlgSizeDecorator(original_class):
#     # Make a copy of original methods
#     orig_init = original_class.__init__
#     orig_done = original_class.done
# 
#     def __init__(self, *args, **kws):
#         orig_init(self, *args, **kws)  # call the original __init__
# 
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         size = settings.value('%s/size' % orig_class_name)
#         if size:
#             self.resize(size)
#         if settings.value('%s/maximized' % orig_class_name, False, type=bool):
#             self.showMaximized()
# 
#     def done(self, r):
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         settings.setValue('%s/maximized' % orig_class_name, self.isMaximized())
#         if not self.isMaximized():
#             settings.setValue('%s/size' % orig_class_name, self.size())
# 
#         orig_done(self, r)  # call the original done
# 
#     # Set the class' methods to the new one
#     original_class.__init__ = __init__
#     original_class.done = done
#     return original_class
# 
# def storeDlgPositionDecorator(original_class):
#     # Make a copy of original methods
#     orig_init = original_class.__init__
#     orig_done = original_class.done
# 
#     def __init__(self, *args, **kws):
#         orig_init(self, *args, **kws)  # call the original __init__
# 
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         position = settings.value('%s/position' % orig_class_name)
#         if position:
#             screen = QApplication.desktop().screenGeometry()
#             if screen.contains(self.rect().center() + position):
#                 self.move(position)
# 
#     def done(self, r):
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         settings.setValue('%s/position' % orig_class_name, self.pos())
# 
#         orig_done(self, r)  # call the original done
# 
#     # Set the class' methods to the new one
#     original_class.__init__ = __init__
#     original_class.done = done
#     return original_class
# 
# Path: OpenNumismat/Tools/Gui.py
# def getSaveFileName(parent, name, filename, dir_, filters):
#     if isinstance(filters, str):
#         filters = (filters,)
#     settings = QSettings()
#     keyDir = name + '/last_dir'
#     keyFilter = name + '/last_filter'
#     lastExportDir = settings.value(keyDir, dir_)
#     defaultFileName = os.path.join(lastExportDir, filename)
#     defaultFilter = settings.value(keyFilter)
#     caption = QApplication.translate("GetSaveFileName", "Save as")
# 
#     fileName, selectedFilter = QFileDialog.getSaveFileName(
#         parent, caption, defaultFileName, filter=';;'.join(filters),
#         initialFilter=defaultFilter)
#     if fileName:
#         lastExportDir = os.path.dirname(fileName)
#         settings.setValue(keyDir, lastExportDir)
#         settings.setValue(keyFilter, selectedFilter)
# 
#     return fileName, selectedFilter


import math
import OpenNumismat
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from OpenNumismat.Tools import TemporaryDir
from OpenNumismat.Tools.DialogDecorators import storeDlgSizeDecorator, storeDlgPositionDecorator
from OpenNumismat.Tools.Gui import getSaveFileName

    def showEvent(self, _e):
        self.setFixedSize(self.size())

    def tabChanged(self, index):
        settings = QSettings()
        settings.setValue('crop_dialog/crop_tool', self.currentTool())

        self.currentToolChanged.emit(index)

    def currentTool(self):
        return self.tab.currentIndex()
    
    def setAutoBorders(self):
        if self.currentTool() == 0:
            self.xSpin.setValue(self.auto_rect[0])
            self.ySpin.setValue(self.auto_rect[1])
            self.widthSpin.setValue(self.auto_rect[2])
            self.heightSpin.setValue(self.auto_rect[3])
        elif self.currentTool() == 1:
            self.xCircleSpin.setValue(self.auto_rect[0])
            self.yCircleSpin.setValue(self.auto_rect[1])
            self.widthCircleSpin.setValue(self.auto_rect[2])
            self.heightCircleSpin.setValue(self.auto_rect[3])


class DoubleSlider(QSlider):
    def setValue(self, value):
        super().setValue(int(value))



# Path: OpenNumismat/Tools/DialogDecorators.py
# def storeDlgSizeDecorator(original_class):
#     # Make a copy of original methods
#     orig_init = original_class.__init__
#     orig_done = original_class.done
# 
#     def __init__(self, *args, **kws):
#         orig_init(self, *args, **kws)  # call the original __init__
# 
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         size = settings.value('%s/size' % orig_class_name)
#         if size:
#             self.resize(size)
#         if settings.value('%s/maximized' % orig_class_name, False, type=bool):
#             self.showMaximized()
# 
#     def done(self, r):
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         settings.setValue('%s/maximized' % orig_class_name, self.isMaximized())
#         if not self.isMaximized():
#             settings.setValue('%s/size' % orig_class_name, self.size())
# 
#         orig_done(self, r)  # call the original done
# 
#     # Set the class' methods to the new one
#     original_class.__init__ = __init__
#     original_class.done = done
#     return original_class
# 
# def storeDlgPositionDecorator(original_class):
#     # Make a copy of original methods
#     orig_init = original_class.__init__
#     orig_done = original_class.done
# 
#     def __init__(self, *args, **kws):
#         orig_init(self, *args, **kws)  # call the original __init__
# 
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         position = settings.value('%s/position' % orig_class_name)
#         if position:
#             screen = QApplication.desktop().screenGeometry()
#             if screen.contains(self.rect().center() + position):
#                 self.move(position)
# 
#     def done(self, r):
#         settings = QSettings()
#         orig_class_name = self.__class__.__name__
#         settings.setValue('%s/position' % orig_class_name, self.pos())
# 
#         orig_done(self, r)  # call the original done
# 
#     # Set the class' methods to the new one
#     original_class.__init__ = __init__
#     original_class.done = done
#     return original_class
# 
# Path: OpenNumismat/Tools/Gui.py
# def getSaveFileName(parent, name, filename, dir_, filters):
#     if isinstance(filters, str):
#         filters = (filters,)
#     settings = QSettings()
#     keyDir = name + '/last_dir'
#     keyFilter = name + '/last_filter'
#     lastExportDir = settings.value(keyDir, dir_)
#     defaultFileName = os.path.join(lastExportDir, filename)
#     defaultFilter = settings.value(keyFilter)
#     caption = QApplication.translate("GetSaveFileName", "Save as")
# 
#     fileName, selectedFilter = QFileDialog.getSaveFileName(
#         parent, caption, defaultFileName, filter=';;'.join(filters),
#         initialFilter=defaultFilter)
#     if fileName:
#         lastExportDir = os.path.dirname(fileName)
#         settings.setValue(keyDir, lastExportDir)
#         settings.setValue(keyFilter, selectedFilter)
# 
#     return fileName, selectedFilter


# Path: OpenNumismat/ImageViewer.py
import math
import OpenNumismat
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from OpenNumismat.Tools import TemporaryDir
from OpenNumismat.Tools.DialogDecorators import storeDlgSizeDecorator, storeDlgPositionDecorator
from OpenNumismat.Tools.Gui import getSaveFileName

    def showEvent(self, _e):
        self.setFixedSize(self.size())

    def tabChanged(self, index):
        settings = QSettings()
        settings.setValue('crop_dialog/crop_tool', self.currentTool())

        self.currentToolChanged.emit(index)

    def currentTool(self):
        return self.tab.currentIndex()
    
    def setAutoBorders(self):
        if self.currentTool() == 0:
            self.xSpin.setValue(self.auto_rect[0])
            self.ySpin.setValue(self.auto_rect[1])
            self.widthSpin.setValue(self.auto_rect[2])
            self.heightSpin.setValue(self.auto_rect[3])
        elif self.currentTool() == 1:
            self.xCircleSpin.setValue(self.auto_rect[0])
            self.yCircleSpin.setValue(self.auto_rect[1])
            self.widthCircleSpin.setValue(self.auto_rect[2])
            self.heightCircleSpin.setValue(self.auto_rect[3])


class DoubleSlider(QSlider):
    def setValue(self, value):
        super().setValue(int(value))



@storeDlgPositionDecorator
