# Copyright (c) James Percent, Byron Galbraith and Unlock contributors.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of Unlock nor the names of its contributors may be used
#       to endorse or promote products derived from this software without
#       specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from .view import UnlockView


class TimeScopeView(UnlockView):
    def __init__(self, model, canvas, labels=None):
        """
        the scope view has n_channel traces w/ labels and a single cursor bar
        additionally having range indicators would be good
        """
        super(TimeScopeView, self).__init__()

        self.model = model

        self.xlim = (canvas.width*0.05, canvas.width*0.95)
        self.ylim = (canvas.height*0.05, canvas.height*0.95)

        self.cursor = self.drawLine(self.xlim[0], self.ylim[0],
                                    self.xlim[0], self.ylim[1],
                                    canvas.batch, color=(255, 0, 0))

    def render(self):
        cursor_pos, traces = self.model.get_state()
        self.cursor.vertices[::2] = (self.xlim[0] + cursor_pos,)*2


class LinePlotView(UnlockView):
    def __init__(self, model, canvas):
        super(LinePlotView, self).__init__()
        self.model = model

    def render(self):
        state = self.model.get_state()
        if not state:
            return
        #self.line.vertices[1::2] = [i + self.y for i in lines]




@@ -2057,17 +2057,16 @@
 t*0.95)%0A
-%0A
         
@@ -2074,31 +2074,33 @@
 elf.
-cursor
+xscale
  = 
+(
 self.
-drawLine(
+xlim%5B1%5D - 
 self
@@ -2111,73 +2111,142 @@
 m%5B0%5D
-, self.ylim%5B0%5D,%0A                                    self.x
+) / float(self.model.n_samples)%0A%0A        x0 = self.scale_width(self.xlim%5B0%5D)%0A        self.cursor = self.drawLine(x0, self.y
 lim%5B0%5D,
+ x0,
  sel
@@ -2326,16 +2326,194 @@
 0, 0))%0A%0A
+    def scale_width(self, x):%0A        %22%22%22%0A        Scale the position of the data points to fit the width of the screen%0A        %22%22%22%0A        return x * self.xscale + self.xlim%5B0%5D%0A%0A
     def 
@@ -2570,32 +2570,73 @@
 del.get_state()%0A
+        x = self.scale_width(cursor_pos)%0A
         self.cur
@@ -2660,33 +2660,9 @@
  = (
-self.xlim%5B0%5D + cursor_pos
+x
 ,)*2

scale x values to fit screen width