RtmFrame.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 #
00004 #  @file RtmFrame.py
00005 #  @brief rtc-link main frame class
00006 #  @date $Date: 2005-05-27 15:51:31 $
00007 #  @author Noriaki Ando <n-ando@aist.go.jp>
00008 # 
00009 #  Copyright (C) 2003-2005
00010 #      Task-intelligence Research Group,
00011 #      Intelligent Systems Research Institute,
00012 #      National Institute of
00013 #          Advanced Industrial Science and Technology (AIST), Japan
00014 #      All rights reserved.
00015 # 
00016 #  $Id: RtmFrame.py 775 2008-07-28 16:14:45Z n-ando $
00017 # 
00018 
00019 import sys, os, time
00020 import wx                  # This module uses the new wx namespace
00021 import wx.html
00022 import rtimages
00023 from wxPython.wx import *
00024 #import ScrolledWindow
00025 #import OGL
00026 import RtmSystemDraw
00027 import wx.ogl  as  ogl
00028 import os
00029 import cPickle
00030 #from wxPython.wx import *
00031 #import wx
00032 
00033 import threading
00034 
00035 import RtmAbout
00036 
00037 from RtmCompData import *
00038 
00039 import RTM
00040 
00041 #ID_NEW     = 100
00042 #ID_OPEN    = 101
00043 #ID_CONNECT = 102
00044 #ID_EXIT    = 103
00045 #ID_COPY    = 104
00046 #ID_CUT     = 105
00047 #ID_PASTE   = 106
00048 #ID_HELP    = 107
00049 #ID_ABOUT   = 108
00050 
00051 ID_WINDOW_LEFT          = 5000
00052 ID_WINDOW_CENTER_TOP    = 5001
00053 ID_WINDOW_CENTER_BOTTOM = 5002
00054 ID_WINDOW_RIGHT         = 5003
00055 
00056 class MenuFactory:
00057         def __init__(self, parent, menulist):
00058                 self.parent = parent
00059                 self.menu = wx.Menu()
00060                 for item_name, handler in menulist:
00061                         if item_name == "---":
00062                                 self.menu.AppendSeparator()
00063                         else:
00064                                 new_id = wx.NewId()
00065                                 parent.Bind(wx.EVT_MENU, handler, id=new_id)
00066                                 item = wx.MenuItem(self.menu, new_id, item_name)
00067                                 self.menu.AppendItem(item)
00068         def GetMenu(self):
00069                 return self.menu
00070 
00071 class RTComponentDropTarget(wx.DropTarget):
00072         def __init__(self, window):
00073                 wx.DropTarget.__init__(self)
00074                 self.window = window
00075 
00076                 self.df = wx.CustomDataFormat("RTComponent")
00077 #               print self.df, dir(self.df)
00078                 print "RTComponentDropTarget", self.df.GetId(), self.df.GetType()
00079                 self.data = wx.CustomDataObject(self.df)
00080                 self.SetDataObject(self.data)
00081                 
00082         def OnDragOver(self, x, y, d):
00083                 print "OnDragOver"
00084                 self.window.AppendText("OnDragOver\n")
00085                 return wx.DragLink
00086 
00087         def OnDrop(self, x, y):
00088                 print "OnDrop"
00089                 print x, y
00090                 return True
00091 
00092                 
00093         def OnData(self, x, y, d):
00094                 print "OnData"
00095                 if self.GetData():
00096                         data = self.data.GetData()
00097                         cdata = cPickle.loads(data)
00098                         self.window.AppendText(cdata + "\n")
00099 #                       self.window.AppendText(type(cdata[1]) + "\n")
00100 
00101 #                       return wx.DragNone
00102                 #               url = self.data.GetURL()
00103                 return d
00104 
00105 
00106 class RtmFileMenu(MenuFactory):
00107         def __init__(self, parent):
00108                 menu_list = [("&New System", self.OnNew),
00109                                          ("&Open System", self.OnOpen),
00110                                          ("&Save System", self.OnSave),
00111                                          ("Save System &as", self.OnSaveAs),
00112                                          ("---", None),
00113                                          ("&Import Component", self.OnImport),
00114                                          ("&Export Component", self.OnExport),
00115                                          ("---", None),
00116                                          ("Print Pre&view", self.OnPrintPreview),
00117                                          ("&Print System", self.OnPrint),
00118                                          ("---", None),
00119                                          ("&Connect Naming Server", self.OnConnect),
00120                                          ("---", None),
00121                                          ("E&xit", self.OnExit)]
00122                 MenuFactory.__init__(self, parent, menu_list)
00123 
00124         def OnNew(self, event):
00125                 self.parent.OnNewSystemClick(None)
00126                 print "OnNew"
00127 
00128         def OnOpen(self, event):
00129                 self.parent.drawWin[self.parent.drawCurNum].loadXML()
00130                 print "OnOpen"
00131 
00132         def OnSave(self, event):
00133                 filename = "System%d.xml" % self.parent.drawCurNum
00134                 self.parent.drawWin[self.parent.drawCurNum].saveXML(filename)
00135                 print "OnSave"
00136 
00137         def OnSaveAs(self, event):
00138                 self.parent.drawWin[self.parent.drawCurNum].saveAsXML()
00139                 print "OnSaveAs"
00140 
00141         def OnImport(self, event):
00142                 self.parent.OnFileOpen(event)
00143                 print "OnImport"
00144 
00145         def OnExport(self, event):
00146                 self.parent.OnFileOpen(event)
00147                 print "OnExport"
00148 
00149         def OnPrint(self, event):
00150                 dlg = wx.PrintDialog(self.parent, None)
00151                 if dlg.ShowModal() == wx.ID_OK:
00152                         data = dlg.GetPrintDialogData()
00153                 print "OnPrint"
00154 
00155         def OnPrintPreview(self, event):
00156                 self.parent.OnFileOpen(event)
00157                 print "OnOpen"
00158 
00159         def OnConnect(self, event):
00160                 self.parent.treepanel.OnConnectNSClick(None)
00161                 print "OnConnect"
00162 
00163         def OnExit(self, event):
00164                 print "OnExit"
00165                 self.parent.treepanel.threadloop = 0
00166                 self.parent.close_evt.wait()
00167                 self.parent.Close(true)
00168 
00169 
00170 class RtmEditMenu(MenuFactory):
00171         def __init__(self, parent):
00172                 menu_list = [("&Copy", self.OnCopy),
00173                                          ("C&ut", self.OnCut),
00174                                          ("&Paste", self.OnPaste)]
00175                 MenuFactory.__init__(self, parent, menu_list)
00176 
00177         def OnCopy(self, event):
00178                 print "OnCopy"
00179 
00180         def OnCut(self, event):
00181                 print "OnCut"
00182 
00183         def OnPaste(self, event):
00184                 print "OnPaste"
00185 
00186 class RtmDisplayMenu(MenuFactory):
00187         def __init__(self, parent):
00188                 menu_list = [("Long Name &and Alias", self.OnAllDisp),
00189                                          ("&Long Name", self.OnLongDisp),
00190                                          ("&Alias", self.OnAliasDisp)]
00191                 MenuFactory.__init__(self, parent, menu_list)
00192 
00193         def OnAllDisp(self, event):
00194                 self.parent.kindDispMode = 'all'
00195                 print "OnAllDisp"
00196 
00197         def OnLongDisp(self, event):
00198                 self.parent.kindDispMode = 'long'
00199                 print "OnLongDisp"
00200 
00201         def OnAliasDisp(self, event):
00202                 self.parent.kindDispMode = 'alias'
00203                 print "OnAliasDisp"
00204 
00205 
00206 class RtmHelpMenu(MenuFactory):
00207         def __init__(self, parent):
00208                 menu_list = [("&Help", self.OnHelp),
00209                                          ("---", None),
00210                                          ("&About", self.OnAbout)]
00211                 MenuFactory.__init__(self, parent, menu_list)
00212 
00213         def OnHelp(self, event):
00214                 print "OnHelp"
00215 
00216         def OnAbout(self, event):
00217                 print "OnAbout"
00218                 dlg = RtmAbout.RtdAboutBox(None)
00219                 dlg.ShowModal()
00220                 dlg.Destroy()
00221 
00222 
00223 
00224 
00225 
00226 class Log:
00227         def WriteText(self, text):
00228                 if text[-1:] == '\n':
00229                         text = text[:-1]
00230 #               wxLogMessage(text)
00231         write = WriteText
00232         
00233 #class RtdLog(wx.PyLog):
00234 #       def __init__(self, textCtrl, logTime=0):
00235 #               wx.PyLog.__init__(self)
00236 #               self.tc = textCtrl
00237 #               self.logTime = logTime
00238 #               
00239 #       def DoLogString(self, message, timeStamp):
00240 #               if self.logTime:
00241 #                       message = time.strftime("%X", time.localtime(timeStamp)) + \
00242 #                                         ": " + message
00243 #               if self.tc:
00244 #                       self.tc.AppendText(message + '\n')
00245 
00246 
00247 
00248 class RtdFrame(wxMDIParentFrame):
00249         def __del__(self):
00250 #               for sys_no in self.drawWin.keys():
00251 #                       del self.drawWin[sys_no]
00252 
00253                 ogl.OGLCleanUp()
00254 
00255         def __init__(self, parent, ID, title):
00256                 wxMDIParentFrame.__init__(self, parent, -1, title, size = (800, 600),
00257                                                   style=wx.DEFAULT_FRAME_STYLE|wx.HSCROLL | wx.VSCROLL
00258                                                                   ##wx.NO_FULL_REPAINT_ON_RESIZE
00259                                                                   )
00260                 #               wx.Frame.__init__(self, parent, ID, title,
00261                 #                                                wxDefaultPosition, wxSize(800, 600))
00262                 self.winCount = 0
00263                 self.drawWin = {}
00264                 self.drawWin2 = {}
00265                 self.drawWinID = {}
00266                 self.drawCurNum = 0
00267                 self.cwd = os.getcwd()
00268                 self.curOverview = ""
00269                 self.window = None
00270                 self.log = Log()
00271                 ogl.OGLInitialize()
00272                 self.close_evt = threading.Event()
00273                 self.kindDispMode = 'all'
00274                 #------------------------------------------------------------
00275                 # Basic window frame settings
00276                 #------------------------------------------------------------
00277                 # Set window icon image
00278                 self.SetIcon(rtimages.getRT_iconIcon())
00279 
00280                 # Status Bar
00281                 self.status_bar = self.CreateStatusBar()
00282 
00283                 # Tool Bar
00284                 self.toolbar = self.CreateToolBar(wxTB_HORIZONTAL
00285                                                                          | wxTB_DOCKABLE 
00286                                                                          | wx.NO_BORDER
00287                                                                          | wx.TB_FLAT
00288 #                                                                        | wx.TB_TEXT
00289                                                                          )
00290 
00291                 self.exitdoorID = wx.NewId()
00292                 self.toolbar.AddSimpleTool(self.exitdoorID, rtimages.getExitDoorBitmap(),
00293                                                                    "Exit", "Exit from rtc-link.")
00294                 self.Bind(wx.EVT_TOOL, self.TimeToQuit, id=self.exitdoorID)
00295 
00296                 self.toolbar.AddSeparator()
00297 
00298                 self.connectID = wx.NewId()
00299                 self.toolbar.AddSimpleTool(self.connectID,
00300                                                                    rtimages.getConnect2Bitmap(),
00301                                                                    "Connect",
00302                                                                    "Connect to a Naming Server.")
00303 #               self.toolbar.EnableTool(self.connectID, False)
00304 
00305                 self.alldispID = wx.NewId()
00306                 self.toolbar.AddSimpleTool(self.alldispID, rtimages.getMixNameBitmap(),
00307                                                                    "Long Name and Alias", "Display long name and alias on the naming tree window.")
00308                 self.Bind(wx.EVT_TOOL, self.OnAllDisp, id=self.alldispID)
00309 
00310 
00311                 self.longdispID = wx.NewId()
00312                 self.toolbar.AddSimpleTool(self.longdispID, rtimages.getLongNameBitmap(),
00313                                                                    "Long Name", "Display only long name on the naming tree window.")
00314                 self.Bind(wx.EVT_TOOL, self.OnLongnameDisp, id=self.longdispID)
00315 
00316                 self.aliasdispID = wx.NewId()
00317                 self.toolbar.AddSimpleTool(self.aliasdispID, rtimages.getShortNameBitmap(),
00318                                                                    "Alias", "Display only alias on the naming tree window.")
00319                 self.Bind(wx.EVT_TOOL, self.OnAliasDisp, id=self.aliasdispID)
00320                 #               self.Bind(wx.EVT_TOOL, self.OnToolClick, id=40)
00321                 #               self.Bind(wx.EVT_TOOL_RCLICKED, self.OnToolRClick, id=40)
00322 
00323                 self.toolbar.AddSeparator()
00324 
00325                 self.newsysID = wx.NewId()
00326                 self.toolbar.AddSimpleTool(self.newsysID,
00327                                                                    rtimages.getNewRTSystemBitmap(),
00328                                                                    "New",
00329                                                                    "Open.: Open a new system draw window.")
00330                 self.Bind(wx.EVT_TOOL, self.OnNewSystemClick, id=self.newsysID)
00331 
00332 #               cbID = wx.NewId()
00333 #               self.toolbar.AddControl(
00334 #                       wx.ComboBox(
00335 #                       self.toolbar, cbID, "", choices=["", "This", "is a", "wxComboBox"],
00336 #                       size=(150,-1), style=wx.CB_DROPDOWN
00337 #                       ))
00338                 self.toolbar.Realize()
00339 
00340                 # Menu bar
00341                 self.SetMenuBar(self.CreateMenu())
00342 
00343 
00344                 #------------------------------------------------------------
00345                 # Menu event binding
00346                 #------------------------------------------------------------
00347                 #               EVT_MENU(self, ID_ABOUT, self.OnAbout)
00348                 #               EVT_MENU(self, ID_EXIT, self.TimeToQuit)
00349                 #               EVT_MENU(self, ID_OPEN, self.OnFileOpen)
00350 
00351                 #------------------------------------------------------------
00352                 # Dictionary 
00353                 #------------------------------------------------------------
00354                 self.myDict = RtmCompData(self)
00355 
00356                 #------------------------------------------------------------
00357                 # Naming Tree Window
00358                 #------------------------------------------------------------
00359                 self.nametreeW_ID = wxNewId()
00360                 win = wxSashLayoutWindow(self, self.nametreeW_ID,
00361                                                                  style=wx.NO_BORDER|wx.SW_3D)
00362                 win.SetDefaultSize((200, 600))
00363                 win.SetOrientation(wxLAYOUT_VERTICAL)
00364                 win.SetAlignment(wxLAYOUT_LEFT)
00365                 win.SetBackgroundColour(wxColour(200, 200, 200))
00366                 win.SetSashVisible(wxSASH_RIGHT, True)
00367 
00368                 # Tree
00369                 tID = wxNewId()
00370                 self.treeMap = {}
00371 
00372                 import RtmTreeCtrl
00373                 log = Log()
00374 #               self.treepanel = RtmTreeCtrl.RtmTreeCtrlPanel(win, log)
00375                 self.treepanel = RtmTreeCtrl.RtmTreeCtrlPanel(self,win, log)
00376                 self.Bind(wx.EVT_TOOL,
00377                                   self.treepanel.OnConnectNSClick,
00378                                   id=self.connectID)
00379                 self.treepanelW = win
00380 
00381                 #------------------------------------------------------------
00382                 # Profile Window
00383                 #------------------------------------------------------------
00384                 self.profileW_ID = wxNewId()
00385                 win = wxSashLayoutWindow(self, self.profileW_ID,
00386                                                                  style=wx.NO_BORDER|wx.SW_3D)
00387                 win.SetDefaultSize((200, 600))
00388                 win.SetOrientation(wxLAYOUT_VERTICAL)
00389                 win.SetAlignment(wxLAYOUT_RIGHT)
00390                 win.SetBackgroundColour(wxColour(200, 200, 200))
00391                 win.SetSashVisible(wxSASH_LEFT, True)
00392 
00393                 import RtmProfileList
00394                 self.profilepanel = RtmProfileList.RtmProfileListPanel(win)
00395                 self.profilepanelW = win
00396 
00397                 #------------------------------------------------------------
00398                 # Log Window
00399                 #------------------------------------------------------------
00400                 self.logW_ID = wxNewId()
00401                 win =  wxSashLayoutWindow(self, self.logW_ID,
00402                                                                   style=wx.NO_BORDER|wx.SW_3D)
00403                 win.SetDefaultSize((400, 100))
00404                 win.SetOrientation(wx.LAYOUT_HORIZONTAL)
00405                 win.SetAlignment(wx.LAYOUT_BOTTOM)
00406                 win.SetBackgroundColour(wx.Colour(200, 200, 200))
00407                 win.SetSashVisible(wx.SASH_TOP, True)
00408 
00409                 self.textWindow = wx.TextCtrl(win, -1, "",
00410                                                                           style=wx.TE_MULTILINE|wx.SUNKEN_BORDER)
00411                 self.textWindow.SetValue("A sub window")
00412                 
00413                 self.logW = win
00414                 self.textWindow.SetDropTarget(RTComponentDropTarget(self.textWindow))
00415                 
00416                 self.bg_bmp = rtimages.getGridBGBitmap()
00417                 self.GetClientWindow().Bind(
00418                         wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground
00419                         )
00420 
00421                 self.Bind(wx.EVT_CLOSE, self.OnClose)
00422                 self.Bind(
00423                         wx.EVT_SASH_DRAGGED_RANGE, self.OnSashDrag,
00424                         id=self.nametreeW_ID, 
00425                         id2=self.logW_ID
00426                         )
00427                 self.Bind(wx.EVT_SIZE, self.OnSize)
00428 
00429 
00430         def OnAllDisp(self, event):
00431                 self.kindDispMode = 'all'
00432 
00433         def OnLongnameDisp(self, event):
00434                 self.kindDispMode = 'long'
00435 
00436         def OnAliasDisp(self, event):
00437                 self.kindDispMode = 'alias'
00438 
00439         def OnNewSystemClick(self, event):
00440                 self.winCount = self.winCount + 1
00441                 new_id = wx.NewId()
00442                 win = wxMDIChildFrame(self, new_id , "System: %d" % self.winCount)
00443                 #               canvas = ScrolledWindow.MyCanvas(win)
00444                 self.drawWin[self.winCount] = RtmSystemDraw.RtdSystemDraw(win, self.log, self)
00445                 self.drawWin2[self.winCount] = win
00446                 win.Show(True)
00447                 win.SetIcon(rtimages.getRTIcon())
00448                 self.drawWinID[self.winCount] = self.drawWin[self.winCount].GetId()
00449                 self.drawCurNum = self.winCount
00450 
00451                 os_check = sys.platform
00452                 if os_check == 'win32':
00453                         self.drawWin[self.winCount].Bind(wx.EVT_SET_FOCUS, self.OnChangeDraw)
00454                 else:
00455                         self.drawWin[self.winCount].Bind(wx.EVT_ENTER_WINDOW, self.OnChangeDraw)
00456                 self.drawWin2[self.winCount].Bind(wx.EVT_CLOSE, self.OnChildClose)
00457 
00458         def OnChildClose(self, event):
00459                 curID = event.GetId()
00460                 for n in self.drawWinID.keys():
00461                         if self.drawWinID[n] == curID:
00462                                 self.drawCurNum = n
00463                                 break
00464                 del self.drawWin[self.drawCurNum]
00465                 del self.drawWinID[self.drawCurNum]
00466                 event.Skip()
00467                 
00468         def OnChangeDraw(self, event):
00469                 curID = event.GetId()
00470                 for n in self.drawWinID.keys():
00471                         if self.drawWinID[n] == curID:
00472                                 self.drawCurNum = n
00473                                 break
00474 
00475         def OnSize(self, event):
00476                 wxLayoutAlgorithm().LayoutMDIFrame(self)
00477 
00478 
00479         def OnSashDrag(self, event):
00480                 if event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE:
00481                         return
00482 
00483                 eID = event.GetId()
00484                 if eID == self.nametreeW_ID:
00485                         self.treepanelW.SetDefaultSize((event.GetDragRect().width, 200))
00486 
00487                 elif eID == self.profileW_ID:
00488                         self.profilepanelW.SetDefaultSize((event.GetDragRect().width, 200))
00489 
00490                 elif eID == self.logW_ID:
00491                         print "logW_ID", self.logW_ID
00492                         self.logW.SetDefaultSize((1000, event.GetDragRect().height))
00493 
00494                 wx.LayoutAlgorithm().LayoutMDIFrame(self)
00495                 self.GetClientWindow().Refresh()
00496 
00497 
00498         def OnClose(self, event):
00499                 if hasattr(self, "treepanel"):
00500                         print 'OnClose:'
00501                         self.treepanel.threadloop = 0
00502                         self.close_evt.wait()
00503                 event.Skip()
00504                 
00505         def TimeToQuit(self, event):
00506                 self.Close(true)
00507                 
00508 
00509         def CreateMenu(self):
00510                 menu_file = RtmFileMenu(self).GetMenu()
00511                 menu_edit = RtmEditMenu(self).GetMenu()
00512                 menu_disp = RtmDisplayMenu(self).GetMenu()
00513                 menu_help = RtmHelpMenu(self).GetMenu()
00514 
00515                 menuBar = wxMenuBar()
00516                 menuBar.Append(menu_file, "&File");
00517                 menuBar.Append(menu_edit, "&Edit");
00518                 menuBar.Append(menu_disp, "&Display");
00519                 menuBar.Append(menu_help, "&Help");
00520 
00521                 # Disable to menu item
00522                 id = menuBar.FindMenuItem("Edit","Copy")
00523                 menuBar.FindItemById(id).Enable(false)
00524                 id = menuBar.FindMenuItem("Edit","Cut")
00525                 menuBar.FindItemById(id).Enable(false)
00526                 id = menuBar.FindMenuItem("Edit","Paste")
00527                 menuBar.FindItemById(id).Enable(false)
00528 
00529 # assembly enable
00530 #               id = menuBar.FindMenuItem("File","Open System")
00531 #               menuBar.FindItemById(id).Enable(false)
00532 #               id = menuBar.FindMenuItem("File","Save System")
00533 #               menuBar.FindItemById(id).Enable(false)
00534 #               id = menuBar.FindMenuItem("File","Save System as")
00535 #               menuBar.FindItemById(id).Enable(false)
00536 #
00537                 id = menuBar.FindMenuItem("File","Import Component")
00538                 menuBar.FindItemById(id).Enable(false)
00539                 id = menuBar.FindMenuItem("File","Export Component")
00540                 menuBar.FindItemById(id).Enable(false)
00541 
00542                 id = menuBar.FindMenuItem("File","Print Preview")
00543                 menuBar.FindItemById(id).Enable(false)
00544                 id = menuBar.FindMenuItem("File","Print System")
00545                 menuBar.FindItemById(id).Enable(false)
00546 
00547                 return menuBar
00548 
00549         def OnFileOpen(self, event):
00550                 dlg = wx.FileDialog(
00551                         self, message="Choose a file", defaultDir=os.getcwd(), 
00552                         defaultFile="", wildcard="*.*", style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
00553                         )
00554         
00555                 if dlg.ShowModal() == wx.ID_OK:
00556                         # This returns a Python list of files that were selected.
00557                         self.paths = dlg.GetPaths()
00558 
00559                         #               for path in paths:
00560                         #            log.WriteText('           %s\n' % path)
00561 
00562                         # Compare this with the debug above; did we change working dirs?
00563 
00564                         # Destroy the dialog. Don't do this until you are done with it!
00565                         # BAD things can happen otherwise!
00566                 dlg.Destroy()
00567 
00568 
00569 
00570         def OnEraseBackground(self, evt):
00571                 dc = evt.GetDC()
00572 
00573                 if not dc:
00574                         dc = wx.ClientDC(self.GetClientWindow())
00575 
00576                         # tile the background bitmap
00577                 sz = self.GetClientSize()
00578                 w = self.bg_bmp.GetWidth()
00579                 h = self.bg_bmp.GetHeight()
00580                 x = 0
00581 
00582                 while x < sz.width:
00583                         y = 0
00584 
00585                         while y < sz.height:
00586                                 dc.DrawBitmap(self.bg_bmp, (x, y))
00587                                 y = y + h
00588 
00589                         x = x + w


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17