/script/DADataAnalyzer/DATools/DAToolRectangle.py

https://gitlab.com/liuxin429go/H2D2-tools · Python · 118 lines · 85 code · 20 blank · 13 comment · 6 complexity · e098bd41306bf562d3abd6c1bd13dc43 MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #************************************************************************
  4. # --- Copyright (c) INRS 2016-2017
  5. # --- Institut National de la Recherche Scientifique (INRS)
  6. # ---
  7. # --- Distributed under the GNU Lesser General Public License, Version 3.0.
  8. # --- See accompanying file LICENSE.txt.
  9. #************************************************************************
  10. if __name__ == "__main__":
  11. import os
  12. import sys
  13. selfDir = os.path.dirname( os.path.abspath(__file__) )
  14. supPath = os.path.normpath( os.path.join(selfDir, '..') )
  15. if os.path.isdir(supPath) and supPath not in sys.path: sys.path.append(supPath)
  16. import wx
  17. from CTCommon.CTRectangleSelector import CTRectangleSelector
  18. class DAToolRectangle:
  19. def __init__(self, window, axes, cb):
  20. self.parent = window
  21. self.cb = cb
  22. self.RS = CTRectangleSelector(axes,
  23. self.onSelect,
  24. drawtype='box',
  25. button=[1], # don't use middle/right button
  26. interactive=True,
  27. #minspanx=5,
  28. #minspany=5,
  29. #spancoords='pixels'
  30. )
  31. self.RS.set_active(False)
  32. self.mnu = wx.Menu()
  33. self.mnu_bbox = wx.MenuItem(self.mnu, wx.ID_ANY, "bbox to clipboard", "Copy to clipboard as a bounding box (xmin, ymin, xmax, ymax)", wx.ITEM_NORMAL)
  34. self.mnu_fltr = wx.MenuItem(self.mnu, wx.ID_ANY, "filter to clipboard", "Copy to clipboard as a coordinate filter", wx.ITEM_NORMAL)
  35. #self.mnu_wkt = wx.MenuItem(self.mnu, wx.ID_ANY, "WKT to clipboard", "Copy to clipboard as a WKT string", wx.ITEM_NORMAL)
  36. self.mnu.Append(self.mnu_bbox)
  37. self.mnu.Append(self.mnu_fltr)
  38. self.parent.Bind(wx.EVT_CONTEXT_MENU, self.onPopupMenu)
  39. self.parent.Bind(wx.EVT_MENU, self.onMenuBbox, self.mnu_bbox)
  40. self.parent.Bind(wx.EVT_MENU, self.onMenuFltr, self.mnu_fltr)
  41. self.bbox = None
  42. @staticmethod
  43. def delete(self):
  44. try:
  45. self.RS.set_active(False)
  46. self.RS.clear()
  47. del(self.RS)
  48. self.RS = None
  49. self.parent.Unbind(wx.EVT_CONTEXT_MENU)
  50. except RuntimeError:
  51. pass
  52. def __setBBox(self, eclick, erelease):
  53. x1, y1 = eclick.xdata, eclick.ydata
  54. x2, y2 = erelease.xdata, erelease.ydata
  55. self.bbox = (min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
  56. def onPopupMenu(self, event):
  57. self.parent.PopupMenu(self.mnu)
  58. def onMenuBbox(self, event):
  59. x1, y1, x2, y2 = self.bbox
  60. clipdata = wx.TextDataObject()
  61. clipdata.SetText('(%s, %s, %s, %s)' % (x1, y1, x2, y2))
  62. wx.TheClipboard.Open()
  63. wx.TheClipboard.SetData(clipdata)
  64. wx.TheClipboard.Close()
  65. def onMenuFltr(self, event):
  66. x1, y1, x2, y2 = self.bbox
  67. clipdata = wx.TextDataObject()
  68. clipdata.SetText('{1} >= %s and {2} >= %s and {1} <= %s and {2} <= %s' % (x1, y1, x2, y2))
  69. wx.TheClipboard.Open()
  70. wx.TheClipboard.SetData(clipdata)
  71. wx.TheClipboard.Close()
  72. def onSelect(self, eclick, erelease):
  73. self.__setBBox(eclick, erelease)
  74. x1, y1, x2, y2 = self.bbox
  75. self.cb( (x2-x1)*(y2-y1) )
  76. def activate(self, state = True):
  77. self.RS.set_active(state)
  78. if __name__ == "__main__":
  79. import numpy as np
  80. import matplotlib.pyplot as plt
  81. def cb(l):
  82. print(l)
  83. def main(parent):
  84. fig, current_ax = plt.subplots() # make a new plotingrange
  85. N = 100000 # If N is large one can see
  86. x = np.linspace(0.0, 10.0, N) # improvement by use blitting!
  87. plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something
  88. plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
  89. plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
  90. r = DAToolRectangle(parent, current_ax, cb)
  91. r.activate(True)
  92. plt.show()
  93. app = wx.App(0)
  94. frame = wx.Frame(None, -1, "")
  95. app.SetTopWindow(frame)
  96. frame.Show()
  97. main(frame)
  98. app.MainLoop()