Salome HOME
PyQt5/PyQt4 support
[modules/gui.git] / src / GUI_PY / qtsalome.py.in
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 QT_SALOME_VERSION=@QT_SALOME_VERSION@
22
23 try:
24     from PyQt@QT_SALOME_VERSION@.Qt import *
25 except:
26     pass
27
28 __CONNECT__ = 0
29 __DISCONNECT__ = 1
30
31 def Connect(*args):
32     """
33     Connects signal of sender to slot of reciever or function.
34
35     Examples:
36
37     1. Connect(sender, signal, reciever, slot)
38        - sender: sender of the signal
39        - signal: string value in the form 'signal_name(type1,type2,...)'
40        - reciever: reciever of the signal
41        - slot: string value in the form 'slot_name(type1,type2,...)'
42
43     2. Connect(sender, signal, function)
44        - sender: sender of the signal
45        - signal: string value in the form 'signal_name(type1,type2,...)'
46        - function: function instance
47     """
48     if len(args) == 4:
49         _process(args[0], args[1], args[2], args[3], __CONNECT__)
50     elif len(args) == 3:
51         _process(args[0], args[1], None, args[2], __CONNECT__)
52     else:
53         RuntimeError("Bad number of arguments, expected 3 or 4 !!!")
54
55 def Disconnect(*args):
56     """
57     Disconnects signal of sender to slot of reciever or function.
58
59     Examples:
60
61     1. Disconnect(sender, signal, reciever, slot)
62        - sender: sender of the signal
63        - signal: string value in the form 'signal_name(type1,type2,...)'
64        - reciever: reciever of the signal
65        - slot: string value in the form 'slot_name(type1,type2,...)'
66
67     2. Disconnect(sender, signal, function)
68        - sender: sender of the signal
69        - signal: string value in the form 'signal_name(type1,type2,...)'
70        - function: function instance
71     """
72     if len(args) == 4:
73         _process(args[0], args[1], args[2], args[3], __DISCONNECT__)
74     elif len(args) == 3:
75         _process(args[0], args[1], None, args[2], __DISCONNECT__)
76     else:
77         RuntimeError("Bad number of arguments, expected 3 or 4 !!!")
78
79 def _process(sender,signal,reciever,slot,operation):
80     isFunc = False
81     slotname = ""
82     if isinstance(slot, str):
83         n = slot.find("(")
84         if n > 0:
85             slotname = slot[:n]
86
87     if callable(slot):
88         isFunc = True
89
90     if QT_SALOME_VERSION == 4:
91         _call = None
92         if operation == __CONNECT__:
93             _call = QObject.connect
94         elif operation == __DISCONNECT__:
95             _call = QObject.disconnect
96         if isFunc:
97             _call(sender,SIGNAL(signal),slot)
98         else:
99             _call(sender,SIGNAL(signal),reciever,SLOT(slot))               
100     else:
101         signame = ""
102         n = signal.find("(")
103         if n > 0:
104             signame = signal[:n]
105         
106         tmp = signal[(n+1):]
107         m = tmp.rfind(")")
108         types = tmp[:m]
109         
110         if len(signame) > 0 and (len(slotname) > 0 or isFunc):
111             arg1 = ""
112             arg2 = ""
113             op = ""
114             rcv = ""
115             if len(types.strip()) > 0:
116                 arg1 = "[" + types + "]"
117             if operation == __DISCONNECT__:
118                 op = "dis"
119             if reciever is not None and not isFunc:
120                 rcv= "reciever."            
121             if isFunc:
122                 arg2 = "slot"
123             else:
124                 arg2 = slotname
125
126             command = "sender." + signame + arg1 + "." + op + "connect(" + rcv + arg2 + ")"
127             exec(command)
128         else:
129             raise RuntimeError("Bad signal '%s' or slot '%s' format !!!"%(signal, slot))