# Path: friture/plotting/coordinateTransform.py
# class CoordinateTransform(QtCore.QObject):
# 
#     def __init__(self, coord_min, coord_max, length, startBorder, endBorder, parent=None):
#         super(CoordinateTransform, self).__init__(parent)
# 
#         self.coord_min = coord_min
#         self.coord_max = coord_max
#         self.coord_clipped_min = max(1e-20, self.coord_min)
#         self.coord_clipped_max = max(self.coord_clipped_min, self.coord_max)
#         self.coord_ratio_log = np.log10(self.coord_clipped_max / self.coord_clipped_min)
# 
#         self.length = length
#         self.startBorder = startBorder
#         self.endBorder = endBorder
#         self.scale = fscales.Linear
# 
#     def setRange(self, coord_min, coord_max):
#         self.coord_min = coord_min
#         self.coord_max = coord_max
#         self.coord_clipped_min = max(1e-20, self.coord_min)
#         self.coord_clipped_max = max(self.coord_clipped_min, self.coord_max)
#         self.coord_ratio_log = np.log10(self.coord_clipped_max / self.coord_clipped_min)
# 
#     def setLength(self, length):
#         self.length = length
# 
#     def setBorders(self, start, end):
#         self.startBorder = start
#         self.endBorder = end
# 
#     def setScale(self, scale):
#         self.scale = scale
# 
#     @pyqtSlot(float, result=float)
#     def toScreen(self, x):
#         if self.scale is fscales.Logarithmic:
#             if self.coord_clipped_min == self.coord_clipped_max:
#                 return self.startBorder + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#             x = (x < 1e-20) * 1e-20 + (x >= 1e-20) * x
#             return (np.log10(x / self.coord_clipped_min)
#                     * (self.length - self.startBorder - self.endBorder)
#                     / self.coord_ratio_log
#                     + self.startBorder)
#         else:
#             if self.coord_max == self.coord_min:
#                 return self.startBorder + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#             trans_x = self.scale.transform(x)
#             trans_min = self.scale.transform(self.coord_min)
#             trans_max = self.scale.transform(self.coord_max)
# 
#             return ((trans_x - trans_min)
#                     * (self.length - self.startBorder - self.endBorder)
#                     / (trans_max - trans_min)
#                     + self.startBorder)
# 
#     @pyqtSlot(float, result=float)
#     def toPlot(self, x):
#         if self.length == self.startBorder + self.endBorder:
#             return self.coord_min + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#         if self.scale is fscales.Logarithmic:
#             return 10 ** ((x - self.startBorder) * self.coord_ratio_log / (self.length - self.startBorder - self.endBorder)) * self.coord_min
#         else:
#             trans_min = self.scale.transform(self.coord_min)
#             trans_max = self.scale.transform(self.coord_max)
# 
#             trans_x = (x - self.startBorder) * (trans_max - trans_min) / (self.length - self.startBorder - self.endBorder) + trans_min
#             return self.scale.inverse(trans_x)


from PyQt5 import QtCore
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtQml import QQmlListProperty
from friture.plotting.coordinateTransform import CoordinateTransform
import friture.plotting.frequency_scales as fscales
# You should have received a copy of the GNU General Public License
# along with Friture.  If not, see <http://www.gnu.org/licenses/>.



class Tick(QtCore.QObject):
    def __init__(self, value, logical_value, parent=None):
        super().__init__(parent)
        self._value = value
        self._logical_value = logical_value
    
    @pyqtProperty(str, constant = True)
    def value(self):
        return self._value

    @pyqtProperty(float, constant = True)
    def logicalValue(self):
        return self._logical_value

# takes min/max of the scale, and returns appropriate ticks (selected for proper number, rounding)
class ScaleDivision(QtCore.QObject):
    logical_major_ticks_changed = QtCore.pyqtSignal()
    logical_minor_ticks_changed = QtCore.pyqtSignal()

    def __init__(self, scale_min, scale_max, parent=None):
        super().__init__(parent)

        self.scale_min = scale_min
        self.scale_max = scale_max
        self.scale = fscales.Linear

# Path: friture/plotting/coordinateTransform.py
# class CoordinateTransform(QtCore.QObject):
# 
#     def __init__(self, coord_min, coord_max, length, startBorder, endBorder, parent=None):
#         super(CoordinateTransform, self).__init__(parent)
# 
#         self.coord_min = coord_min
#         self.coord_max = coord_max
#         self.coord_clipped_min = max(1e-20, self.coord_min)
#         self.coord_clipped_max = max(self.coord_clipped_min, self.coord_max)
#         self.coord_ratio_log = np.log10(self.coord_clipped_max / self.coord_clipped_min)
# 
#         self.length = length
#         self.startBorder = startBorder
#         self.endBorder = endBorder
#         self.scale = fscales.Linear
# 
#     def setRange(self, coord_min, coord_max):
#         self.coord_min = coord_min
#         self.coord_max = coord_max
#         self.coord_clipped_min = max(1e-20, self.coord_min)
#         self.coord_clipped_max = max(self.coord_clipped_min, self.coord_max)
#         self.coord_ratio_log = np.log10(self.coord_clipped_max / self.coord_clipped_min)
# 
#     def setLength(self, length):
#         self.length = length
# 
#     def setBorders(self, start, end):
#         self.startBorder = start
#         self.endBorder = end
# 
#     def setScale(self, scale):
#         self.scale = scale
# 
#     @pyqtSlot(float, result=float)
#     def toScreen(self, x):
#         if self.scale is fscales.Logarithmic:
#             if self.coord_clipped_min == self.coord_clipped_max:
#                 return self.startBorder + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#             x = (x < 1e-20) * 1e-20 + (x >= 1e-20) * x
#             return (np.log10(x / self.coord_clipped_min)
#                     * (self.length - self.startBorder - self.endBorder)
#                     / self.coord_ratio_log
#                     + self.startBorder)
#         else:
#             if self.coord_max == self.coord_min:
#                 return self.startBorder + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#             trans_x = self.scale.transform(x)
#             trans_min = self.scale.transform(self.coord_min)
#             trans_max = self.scale.transform(self.coord_max)
# 
#             return ((trans_x - trans_min)
#                     * (self.length - self.startBorder - self.endBorder)
#                     / (trans_max - trans_min)
#                     + self.startBorder)
# 
#     @pyqtSlot(float, result=float)
#     def toPlot(self, x):
#         if self.length == self.startBorder + self.endBorder:
#             return self.coord_min + 0. * x  # keep x type (this can produce a RunTimeWarning if x contains inf)
# 
#         if self.scale is fscales.Logarithmic:
#             return 10 ** ((x - self.startBorder) * self.coord_ratio_log / (self.length - self.startBorder - self.endBorder)) * self.coord_min
#         else:
#             trans_min = self.scale.transform(self.coord_min)
#             trans_max = self.scale.transform(self.coord_max)
# 
#             trans_x = (x - self.startBorder) * (trans_max - trans_min) / (self.length - self.startBorder - self.endBorder) + trans_min
#             return self.scale.inverse(trans_x)


# Path: friture/plotting/scaleDivision.py
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtQml import QQmlListProperty
from friture.plotting.coordinateTransform import CoordinateTransform
import friture.plotting.frequency_scales as fscales

# You should have received a copy of the GNU General Public License
# along with Friture.  If not, see <http://www.gnu.org/licenses/>.



class Tick(QtCore.QObject):
    def __init__(self, value, logical_value, parent=None):
        super().__init__(parent)
        self._value = value
        self._logical_value = logical_value
    
    @pyqtProperty(str, constant = True)
    def value(self):
        return self._value

    @pyqtProperty(float, constant = True)
    def logicalValue(self):
        return self._logical_value

# takes min/max of the scale, and returns appropriate ticks (selected for proper number, rounding)
class ScaleDivision(QtCore.QObject):
    logical_major_ticks_changed = QtCore.pyqtSignal()
    logical_minor_ticks_changed = QtCore.pyqtSignal()

    def __init__(self, scale_min, scale_max, parent=None):
        super().__init__(parent)

        self.scale_min = scale_min
        self.scale_max = scale_max
        self.scale = fscales.Linear

        self._logical_coordinate_transform = CoordinateTransform(scale_min, scale_max, 1., 0., 0.)