root/branch/twedit/twedit.py

Revision 1595, 4.1 kB (checked in by mswat, 16 months ago)
Line 
1from PyQt4.QtCore import *
2from PyQt4.QtGui import *
3from PyQt4.QtNetwork import *
4from PyQt4.Qsci import *
5
6
7import sys
8
9from CQt.CQApplication import CQApplication
10from EditorWindow import EditorWindow
11
12from DataSocketCommunicators import FileNameSender
13
14import sys, os, errno, tempfile
15
16from Messaging import stdMsg, dbgMsg, errMsg, setDebugging
17# this globally enables/disables debug statements
18setDebugging(0)
19
20
21# the call has to be showTweditWindowInForeground has to be done from the twedit (foregraound process) and not from EditorWindow (not a foreground process)
22# because otherwise windows will not allow this call to succees and will return error code
23def win_enum_callback_twedit(hwnd, results):
24    import win32gui
25    import re
26    exprChecker=re.compile(".*CC3D-Twedit$")
27    if exprChecker.match(win32gui.GetWindowText(hwnd)):
28
29        # dbgMsg("GOT TWEDIT ",win32gui.GetWindowText(hwnd))
30        results.append(hwnd)
31
32
33def showTweditWindowInForeground():
34    # documentaiton of win32con window flags and win32gui showWindow functions
35    # style=win32con.SW_SHOWNORMAL : int
36
37    # Specifies how the window is to be shown. It must be one of win32con.SW_HIDE, win32con.SW_MINIMIZE, win32con.SW_RESTORE, win32con.SW_SHOW, win32con.SW_SHOWMAXIMIZED win32con.SW_SHOWMINIMIZED, win32con.SW_SHOWMINNOACTIVE, win32con.SW_SHOWNA, win32con.SW_SHOWNOACTIVATE, or win32con.SW_SHOWNORMAL
38    import win32gui
39    import win32con
40    handle = []
41    win32gui.EnumWindows(win_enum_callback_twedit, handle)
42    dbgMsg("this is twedit window handle,",handle[0]        )
43    if len(handle):
44
45        win32gui.SetActiveWindow(handle[0])
46        win32gui.BringWindowToTop(handle[0])
47        win32gui.SetFocus(handle[0])
48        win32gui.SetForegroundWindow(handle[0])
49        win32gui.ShowWindow(handle[0],win32con.SW_RESTORE)
50       
51
52
53class Twedit(object):
54    def __init__(self):
55   
56        import sys
57        self.lockfile = os.path.normpath(tempfile.gettempdir() + '/' + os.path.basename(__file__) + '.lock')
58        if sys.platform == 'win32':
59            try:
60                # file already exists, we try to remove (in case previous execution was interrupted)
61                if(os.path.exists(self.lockfile)):
62                    os.unlink(self.lockfile)
63                self.fd =  os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
64            except OSError, e:
65                if e.errno == 13:
66                    dbgMsg("Another instance is already running, quitting.")
67                    raise
68                    # sys.exit(-1)
69                dbgMsg(e.errno)
70                raise
71        else: # non Windows
72            import fcntl, sys
73            self.fp = open(self.lockfile, 'w')
74            try:
75                fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
76            except IOError:
77                dbgMsg("Another instance is already running, quitting.")
78                sys.exit(-1)
79               
80    def __del__(self):
81        import sys
82        if sys.platform == 'win32':
83            if hasattr(self, 'fd'):
84                os.close(self.fd)
85                os.unlink(self.lockfile)
86
87
88   
89    def main(self,argv):
90       
91
92        #global mainWindow
93        app = CQApplication(argv)
94        #app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
95        self.mainWindow = EditorWindow()
96        self.mainWindow.setArgv(argv) # passing command line to the code
97
98        self.mainWindow.show()
99        self.mainWindow.processCommandLine()
100        app.exec_()
101
102       
103
104
105if __name__ == '__main__':
106
107    try:
108        twedit=Twedit()
109    except OSError,e:
110        # dbgMsg("GOT OS ERROR")
111           
112        # argvSendSocket=QUdpSocket()
113        for fileName in sys.argv[1:]:
114            datagram=fileName
115            # argvSendSocket.writeDatagram(datagram,QHostAddress.LocalHost,47405)       
116            fileSender=FileNameSender(datagram) 
117            fileSender.send()
118           
119        if sys.platform == 'win32':   
120            showTweditWindowInForeground()
121           
122        sys.exit()   
123
124   
125    twedit.main(sys.argv[1:])
126   
127   
Note: See TracBrowser for help on using the browser.