Esempio n. 1
0
    def keyhandler(self, event):
        """
        's': mark current selection
        'p': play current selection
        'x': subtract current region from all polygons
        'a': merge current region with all polygons
        """
        key = event.GetKeyCode()
        ptr = self.painter
        if hasattr(ptr, "xvalues") and ptr.xvalues[0] is not None:
            val = ptr.xvalues
        elif hasattr(ptr, "vertices") and len(ptr.vertices) > 0:
            val = geom.vertices_to_polygon(ptr.vertices)
        else:
            val = None

        if key > 255:
            event.Skip()
        elif chr(key) == 's':
            self.add_geometry(val)
        elif chr(key) == 'p' and self.handler.signal is not None:
            if not hasattr(audio, 'play_wave'):
                print "chirp requires pyaudio to play audio"
                return
            if isinstance(val, geom.Polygon):
                tlim = self.axes.get_xlim()
            else:
                tlim = val
            i0, i1 = (int(x * self.handler.Fs) for x in tlim)
            audio.play_wave(self.handler.signal[i0:i1], self.handler.Fs)
        elif chr(key) == 'x':
            if not isinstance(val, geom.Polygon): return
            newgeoms = []
            for p in self.selections:
                p2 = wxgeom.path_to_poly(p).difference(val)
                if not p2.is_empty: newgeoms.extend(geom.polygon_components(p2))
            self.delete_selections()
            for p in newgeoms: self.add_geometry(p)
        elif chr(key) == 'a':
            if not isinstance(val, geom.Polygon): return
            newgeoms = []
            for p in self.selections:
                p2 = wxgeom.path_to_poly(p)
                if p2.intersects(val):
                    p2 = p2.union(val)
                    newgeoms.extend(geom.polygon_components(p2))
            self.delete_selections()
            for p in newgeoms: self.add_geometry(p)
        else:
            event.Skip()
Esempio n. 2
0
 def OnSubtract(self, event):
     """ Subtract smaller polygon(s) from larger """
     polys = self.get_selected(wxgeom.polypatch)
     if len(polys) < 2:
         self.status.SetStatusText("Select at least 2 spectrotemporal segments.")
     else:
         i1, p1 = zip(*polys)
         i2, p2 = geom.subtract_polygons([wxgeom.path_to_poly(p) for p in p1])
         self.spec.delete_selection(*i1)
         new_elem = [self.spec.add_geometry(p) for p in geom.polygon_components(p2)]
         self.status.SetStatusText("Subtracted %d elements from element %d" % (len(i1) - 1, i1[i2]))
Esempio n. 3
0
 def OnMerge(self, event):
     polys = self.get_selected(wxgeom.polypatch)
     if len(polys) < 2:
         self.status.SetStatusText("Select at least 2 spectrotemporal segments to merge.")
     else:
         i, p1 = zip(*polys)
         p2 = geom.merge_polygons([wxgeom.path_to_poly(p) for p in p1])
         self.spec.delete_selection(*i)
         # if polygons are disjoint, may return a multipolygon; split into separate segments
         new_elem = [self.spec.add_geometry(p) for p in geom.polygon_components(p2)]
         self.spec.draw()
         self.status.SetStatusText("Merged elements %s into %s" % (list(i for i, p in polys), new_elem))
Esempio n. 4
0
 def OnSplit(self, event):
     polys = self.get_selected(wxgeom.polypatch)
     if len(polys) != 2:
         self.status.SetStatusText("Select two spectrotemporal segments to split.")
     else:
         p1, p2 = (wxgeom.path_to_poly(p) for i, p in polys)
         if p1.disjoint(p2):
             self.status.SetStatusText("Segments do not intersect.")
         else:
             new_polys = geom.split_polygons(p1, p2)
             new_elem = [self.spec.add_geometry(p) for p in geom.polygon_components(*new_polys)]
             self.spec.delete_selection(polys[1][0], polys[0][0])
             self.spec.draw()
             self.status.SetStatusText("Split elements %s into %s" % (list(i for i, p in polys), new_elem))