| 1 | #! /usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | ''' |
|---|
| 4 | /**************************************************************************** |
|---|
| 5 | |
|---|
| 6 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 7 | |
|---|
| 8 | This file is part of the OSSIE GraphPy. |
|---|
| 9 | |
|---|
| 10 | OSSIE GraphPy is free software; you can redistribute it and/or modify |
|---|
| 11 | it under the terms of the GNU General Public License as published by |
|---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | (at your option) any later version. |
|---|
| 14 | |
|---|
| 15 | OSSIE GraphPy is distributed in the hope that it will be useful, |
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | GNU General Public License for more details. |
|---|
| 19 | |
|---|
| 20 | You should have received a copy of the GNU General Public License |
|---|
| 21 | along with OSSIE GraphPy; if not, write to the Free Software |
|---|
| 22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | |
|---|
| 24 | ****************************************************************************/ |
|---|
| 25 | |
|---|
| 26 | ''' |
|---|
| 27 | |
|---|
| 28 | from omniORB import CORBA |
|---|
| 29 | from omniORB import URI |
|---|
| 30 | import CosNaming |
|---|
| 31 | import CF, CF__POA |
|---|
| 32 | import standardInterfaces__POA |
|---|
| 33 | import customInterfaces__POA |
|---|
| 34 | import sys |
|---|
| 35 | |
|---|
| 36 | import threading |
|---|
| 37 | import time |
|---|
| 38 | |
|---|
| 39 | import wx |
|---|
| 40 | |
|---|
| 41 | #------------------------------------------------------------- |
|---|
| 42 | # GraphPy_i class definition (main component class) |
|---|
| 43 | #------------------------------------------------------------- |
|---|
| 44 | class GraphPy_i(CF__POA.Resource): |
|---|
| 45 | def __init__(self, app_ref, uuid, label, poa): |
|---|
| 46 | CF._objref_Resource.__init__(self._this()) |
|---|
| 47 | print "GraphPy_i __init__: " + label |
|---|
| 48 | self.naming_service_name = label |
|---|
| 49 | self.poa = poa |
|---|
| 50 | self.app_ref |
|---|
| 51 | self.inPort0_servant = dataIn_complexShort_i(self, "GraphDataIn") |
|---|
| 52 | self.inPort0_var = self.inPort0_servant._this() |
|---|
| 53 | |
|---|
| 54 | self.GUI_started = False |
|---|
| 55 | |
|---|
| 56 | def start(self): |
|---|
| 57 | print "GraphPy start called" |
|---|
| 58 | |
|---|
| 59 | def stop(self): |
|---|
| 60 | print "GraphPy stop called" |
|---|
| 61 | |
|---|
| 62 | def getPort(self, id): |
|---|
| 63 | if str(id) == "GraphDataIn": |
|---|
| 64 | return self.inPort0_var |
|---|
| 65 | |
|---|
| 66 | return None #port not found in available ports list |
|---|
| 67 | |
|---|
| 68 | def initialize(self): |
|---|
| 69 | print "GraphPy initialize called" |
|---|
| 70 | |
|---|
| 71 | def configure(self, props): |
|---|
| 72 | print "GraphPy configure called" |
|---|
| 73 | |
|---|
| 74 | # start the gui thread only once |
|---|
| 75 | if not self.GUI_started: |
|---|
| 76 | self.app_thread = threading.Thread(target=app.MainLoop) |
|---|
| 77 | self.app_thread.start() |
|---|
| 78 | self.GUI_started = True |
|---|
| 79 | |
|---|
| 80 | def query(self, props): |
|---|
| 81 | return self.propertySet |
|---|
| 82 | |
|---|
| 83 | def releaseObject(self): |
|---|
| 84 | # release the main work module |
|---|
| 85 | self.work_mod.Release() |
|---|
| 86 | |
|---|
| 87 | # release the main process threads for the ports |
|---|
| 88 | self.outPort0_servant.releasePort() |
|---|
| 89 | |
|---|
| 90 | # deactivate the ports |
|---|
| 91 | iid0 = self.poa.reference_to_id(self.inPort0_var) |
|---|
| 92 | oid0 = self.poa.reference_to_id(self.outPort0_var) |
|---|
| 93 | |
|---|
| 94 | self.poa.deactivate_object(iid0) |
|---|
| 95 | self.poa.deactivate_object(oid0) |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | #------------------------------------------------------------------ |
|---|
| 99 | # dataIn_complexShort_i class definition |
|---|
| 100 | #------------------------------------------------------------------ |
|---|
| 101 | class dataIn_complexShort_i(standardInterfaces__POA.complexShort): |
|---|
| 102 | def __init__(self, parent, name): |
|---|
| 103 | self.parent = parent |
|---|
| 104 | self.name = name |
|---|
| 105 | |
|---|
| 106 | def pushPacket(self, I, Q): |
|---|
| 107 | self.parent.app_ref.frame.my_local_plot.PushPacket(I, Q) |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | #------------------------------------------------------------------- |
|---|
| 113 | # ORB_Init class definition |
|---|
| 114 | #------------------------------------------------------------------- |
|---|
| 115 | class ORB_Init: |
|---|
| 116 | """Takes care of initializing the ORB and bind the object""" |
|---|
| 117 | |
|---|
| 118 | def __init__(self, uuid, label): |
|---|
| 119 | self.app = app # the app reference is the global app reference |
|---|
| 120 | # initialize the orb |
|---|
| 121 | self.orb = CORBA.ORB_init() |
|---|
| 122 | |
|---|
| 123 | # get the POA |
|---|
| 124 | obj_poa = self.orb.resolve_initial_references("RootPOA") |
|---|
| 125 | poaManager = obj_poa._get_the_POAManager() |
|---|
| 126 | poaManager.activate() |
|---|
| 127 | |
|---|
| 128 | ns_obj = self.orb.resolve_initial_references("NameService") |
|---|
| 129 | rootContext = ns_obj._narrow(CosNaming.NamingContext) |
|---|
| 130 | |
|---|
| 131 | # create the main component object |
|---|
| 132 | self.GraphPy_Obj = GraphPy_i(self.app, uuid, label, obj_poa) |
|---|
| 133 | GraphPy_var = self.GraphPy_Obj._this() |
|---|
| 134 | |
|---|
| 135 | name = URI.stringToName(label) |
|---|
| 136 | rootContext.rebind(name, GraphPy_var) |
|---|
| 137 | |
|---|
| 138 | self.orb.run() |
|---|
| 139 | |
|---|
| 140 | def ORB_Init_fun(): |
|---|
| 141 | orb = ORB_Init(app, uuid, label) |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | class GraphFrame(wx.Frame): |
|---|
| 146 | def __init__(self, parent): |
|---|
| 147 | wx.Frame.__init__(self, parent, id, title, |
|---|
| 148 | wx.DefaultPosition, (600, 400)) |
|---|
| 149 | |
|---|
| 150 | self.parent = parent |
|---|
| 151 | self.orb = 'arbitrary_for_this_class' |
|---|
| 152 | |
|---|
| 153 | self.my_local_plot = plot.my_graph_structure_short(self.orb, self) |
|---|
| 154 | |
|---|
| 155 | # Now Create the menu bar and items |
|---|
| 156 | self.mainmenu = wx.MenuBar() |
|---|
| 157 | |
|---|
| 158 | menu = wx.Menu() |
|---|
| 159 | menu.Append(205, 'E&xit', 'Enough of this already!') |
|---|
| 160 | self.Bind(wx.EVT_MENU, self.OnFileExit, id=205) |
|---|
| 161 | self.mainmenu.Append(menu, '&File') |
|---|
| 162 | |
|---|
| 163 | menu = wx.Menu() |
|---|
| 164 | menu.Append(206, 'I/Q', 'Make a scatter plot of the I/Q data') |
|---|
| 165 | self.Bind(wx.EVT_MENU,self.OnIQDraw, id=206) |
|---|
| 166 | menu.Append(207, 'Spectrum', 'Plot and FFT of the signal') |
|---|
| 167 | self.Bind(wx.EVT_MENU,self.OnSpectrumDraw, id=207) |
|---|
| 168 | self.mainmenu.Append(menu, "&Plotting") |
|---|
| 169 | |
|---|
| 170 | menu = wx.Menu() |
|---|
| 171 | menu.Append(300, '&About', 'About this thing...') |
|---|
| 172 | self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=300) |
|---|
| 173 | self.mainmenu.Append(menu, '&Help') |
|---|
| 174 | |
|---|
| 175 | self.SetMenuBar(self.mainmenu) |
|---|
| 176 | |
|---|
| 177 | # A status bar to tell people what's happening |
|---|
| 178 | self.CreateStatusBar(1) |
|---|
| 179 | |
|---|
| 180 | self.client = PlotCanvas(self) |
|---|
| 181 | #define the function for drawing pointLabels |
|---|
| 182 | self.client.SetPointLabelFunc(self.DrawPointLabel) |
|---|
| 183 | # Create mouse event for showing cursor coords in status bar |
|---|
| 184 | self.client.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) |
|---|
| 185 | # Show closest point when enabled |
|---|
| 186 | self.client.Bind(wx.EVT_MOTION, self.OnMotion) |
|---|
| 187 | |
|---|
| 188 | # Initialize the plot to display the spectrum |
|---|
| 189 | self.DrawMode = 2 |
|---|
| 190 | self.client.SetXSpec('min') |
|---|
| 191 | self.client.SetYSpec('min') |
|---|
| 192 | self.client.SetEnableZoom(True) |
|---|
| 193 | |
|---|
| 194 | # Bind the close event so we can disconnect the ports |
|---|
| 195 | self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) |
|---|
| 196 | |
|---|
| 197 | self.Show(True) |
|---|
| 198 | |
|---|
| 199 | def DrawPointLabel(self, dc, mDataDict): |
|---|
| 200 | """This is the fuction that defines how the pointLabels are plotted |
|---|
| 201 | dc - DC that will be passed |
|---|
| 202 | mDataDict - Dictionary of data that you want to use for the pointLabel |
|---|
| 203 | |
|---|
| 204 | As an example I have decided I want a box at the curve point |
|---|
| 205 | with some text information about the curve plotted below. |
|---|
| 206 | Any wxDC method can be used. |
|---|
| 207 | """ |
|---|
| 208 | # ---------- |
|---|
| 209 | dc.SetPen(wx.Pen(wx.BLACK)) |
|---|
| 210 | dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) ) |
|---|
| 211 | |
|---|
| 212 | sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point |
|---|
| 213 | dc.DrawRectangle( sx-5,sy-5, 10, 10) #10by10 square centered on point |
|---|
| 214 | px,py = mDataDict["pointXY"] |
|---|
| 215 | cNum = mDataDict["curveNum"] |
|---|
| 216 | pntIn = mDataDict["pIndex"] |
|---|
| 217 | legend = mDataDict["legend"] |
|---|
| 218 | #make a string to display |
|---|
| 219 | s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn) |
|---|
| 220 | dc.DrawText(s, sx , sy+1) |
|---|
| 221 | # ----------- |
|---|
| 222 | |
|---|
| 223 | def OnMouseLeftDown(self,event): |
|---|
| 224 | s= "Left Mouse Down at Point: (%.4f, %.4f)" % self.client.GetXY(event) |
|---|
| 225 | self.SetStatusText(s) |
|---|
| 226 | event.Skip() #allows plotCanvas OnMouseLeftDown to be called |
|---|
| 227 | |
|---|
| 228 | def OnMotion(self, event): |
|---|
| 229 | #show closest point (when enbled) |
|---|
| 230 | if self.client.GetEnablePointLabel() == True: |
|---|
| 231 | #make up dict with info for the pointLabel |
|---|
| 232 | #I've decided to mark the closest point on the closest curve |
|---|
| 233 | dlst= self.client.GetClosetPoint( self.client.GetXY(event), pointScaled= True) |
|---|
| 234 | if dlst != []: #returns [] if none |
|---|
| 235 | curveNum, legend, pIndex, pointXY, scaledXY, distance = dlst |
|---|
| 236 | #make up dictionary to pass to my user function (see DrawPointLabel) |
|---|
| 237 | mDataDict= {"curveNum":curveNum, "legend":legend, "pIndex":pIndex,\ |
|---|
| 238 | "pointXY":pointXY, "scaledXY":scaledXY} |
|---|
| 239 | #pass dict to update the pointLabel |
|---|
| 240 | self.client.UpdatePointLabel(mDataDict) |
|---|
| 241 | event.Skip() #go to next handler |
|---|
| 242 | |
|---|
| 243 | def OnFileExit(self, event): |
|---|
| 244 | self.Close() |
|---|
| 245 | |
|---|
| 246 | def OnIQDraw(self, event): |
|---|
| 247 | self.DrawMode = 1 |
|---|
| 248 | self.my_local_plot.first_draw = True |
|---|
| 249 | self.my_local_plot.update_draw = False |
|---|
| 250 | |
|---|
| 251 | def OnSpectrumDraw(self, event): |
|---|
| 252 | self.DrawMode = 2 |
|---|
| 253 | self.my_local_plot.first_draw = True |
|---|
| 254 | self.client.SetXSpec('min') |
|---|
| 255 | self.client.SetYSpec('min') |
|---|
| 256 | |
|---|
| 257 | def OnPlotRedraw(self,event): |
|---|
| 258 | self.client.Redraw() |
|---|
| 259 | |
|---|
| 260 | def OnPlotClear(self,event): |
|---|
| 261 | self.client.Clear() |
|---|
| 262 | |
|---|
| 263 | def OnPlotScale(self, event): |
|---|
| 264 | if self.client.last_draw != None: |
|---|
| 265 | graphics, xAxis, yAxis= self.client.last_draw |
|---|
| 266 | self.client.Draw(graphics,(1,3.05),(0,1)) |
|---|
| 267 | |
|---|
| 268 | def OnEnableZoom(self, event): |
|---|
| 269 | self.client.SetEnableZoom(event.IsChecked()) |
|---|
| 270 | |
|---|
| 271 | def OnEnableGrid(self, event): |
|---|
| 272 | self.client.SetEnableGrid(event.IsChecked()) |
|---|
| 273 | |
|---|
| 274 | def OnEnableLegend(self, event): |
|---|
| 275 | self.client.SetEnableLegend(event.IsChecked()) |
|---|
| 276 | |
|---|
| 277 | def OnEnablePointLabel(self, event): |
|---|
| 278 | self.client.SetEnablePointLabel(event.IsChecked()) |
|---|
| 279 | |
|---|
| 280 | def OnScrUp(self, event): |
|---|
| 281 | self.client.ScrollUp(1) |
|---|
| 282 | |
|---|
| 283 | def OnScrRt(self,event): |
|---|
| 284 | self.client.ScrollRight(2) |
|---|
| 285 | |
|---|
| 286 | def OnReset(self,event): |
|---|
| 287 | self.client.Reset() |
|---|
| 288 | |
|---|
| 289 | def OnHelpAbout(self, event): |
|---|
| 290 | from wx.lib.dialogs import ScrolledMessageDialog |
|---|
| 291 | about = ScrolledMessageDialog(self, "This is a simple plotting widget", "About...") |
|---|
| 292 | about.ShowModal() |
|---|
| 293 | |
|---|
| 294 | def resetDefaults(self): |
|---|
| 295 | """Just to reset the fonts back to the PlotCanvas defaults""" |
|---|
| 296 | self.client.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.NORMAL)) |
|---|
| 297 | self.client.SetFontSizeAxis(10) |
|---|
| 298 | self.client.SetFontSizeLegend(7) |
|---|
| 299 | self.client.SetXSpec('auto') |
|---|
| 300 | self.client.SetYSpec('auto') |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | def updateWaveformData(self, data): |
|---|
| 304 | if data.has_key('center_freq'): |
|---|
| 305 | self.my_local_plot.center_freq = data['center_freq'] |
|---|
| 306 | if data.has_key('sample_rate'): |
|---|
| 307 | self.my_local_plot.sample_rate = data['sample_rate'] |
|---|
| 308 | if data.has_key('decimation'): |
|---|
| 309 | self.my_local_plot.decimation = data['decimation'] |
|---|
| 310 | |
|---|
| 311 | self.my_local_plot.updatePlotData() |
|---|
| 312 | |
|---|
| 313 | def OnCloseWindow(self,event): |
|---|
| 314 | if hasattr(self.parent, 'removeToolFrame'): |
|---|
| 315 | self.parent.removeToolFrame(self) |
|---|
| 316 | self = None |
|---|
| 317 | event.Skip() |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | #------------------------------------------------------------------- |
|---|
| 331 | # wx.App for the GUI |
|---|
| 332 | #------------------------------------------------------------------- |
|---|
| 333 | class App(wx.App): |
|---|
| 334 | def OnInit(self): |
|---|
| 335 | wx.InitAllImageHandlers() |
|---|
| 336 | self.frame = GraphFrame(self) |
|---|
| 337 | self.SetTopWindow(self.frame) |
|---|
| 338 | return True |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | #------------------------------------------------------------------- |
|---|
| 343 | # Code run when this file is executed |
|---|
| 344 | # |
|---|
| 345 | # Defines some important global variables |
|---|
| 346 | #------------------------------------------------------------------- |
|---|
| 347 | if __name__ == "__main__": |
|---|
| 348 | if len(sys.argv) != 3: |
|---|
| 349 | print sys.argv[0] + " <id> <usage name> " |
|---|
| 350 | |
|---|
| 351 | uuid = str(sys.argv[1]) |
|---|
| 352 | label = str(sys.argv[2]) |
|---|
| 353 | |
|---|
| 354 | print "Identifier - " + uuid + " Label - " + label |
|---|
| 355 | |
|---|
| 356 | app = App() # need an app for the gui stuff |
|---|
| 357 | |
|---|
| 358 | # start the orb in a thread that will run in parallel to the GUI |
|---|
| 359 | orb_thread = threading.Thread(target=ORB_Init_fun) |
|---|
| 360 | orb_thread.start() |
|---|
| 361 | |
|---|