1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4# opens QtDesigner to creae a UI, converts it to python code, makes necessary changes to the code to be ready to use in CsoundQt
5# you need to have QtDesigner (part of Qt devel tools) ann pyuic4 (part of python-qt4-devel or similar) to use this srcipy
6
7# by Tarmo Johannes, tarmo@otsakool.edu.ee 2013
8
9import string,os
10from subprocess import call
11
12
13designerCommand = "designer"
14pyuic4Command = "pyuic4"
15
16
17def makeReplacements(pyQt4File, addPyrun = False):
18    pycode = ""
19    pyfile=open(pyQt4File,"r")
20    for line in pyfile.readlines():
21	if ( string.find(line,"import sys")==-1 and  string.find(line,"sys.argv")==-1  and string.find(line,"sys.exit")==-1 and string.find(line,"QtCore.QMetaObject")==-1): # leave out unwanted lines
22		if ( string.find(line,"def setupUi(self, ")!=-1 ): # find out the name of the main widget
23			mainWidget = line[string.find(line,'self,')+6 :string.find(line,')')] # hopefully gets the name from line like 'def setupUi(self, TabWidget):'
24		if (string.find(line,"QtCore.QObject.connect") != -1):
25			line = string.replace(line, mainWidget+'.','self.')
26		pycode += line
27
28    pycode = string.replace(pycode,"from PyQt4","from PythonQt")
29    #print pycode
30    pyfile.close()
31    if (addPyrun):
32		pycode = "pyinit\npyruni {{\n\n" + pycode + "\n}}"
33    return pycode
34
35#--- uus
36
37# -*- coding: utf-8 -*-
38
39# Form implementation generated from reading ui file '/home/tarmo/tarmo/csound/scripts/qtd_import.ui'
40#
41# Created by: PyQt4 UI code generator 4.9.6
42#
43# WARNING! All changes made in this file will be lost!
44
45from PythonQt import QtCore, QtGui
46
47try:
48    _fromUtf8 = QtCore.QString.fromUtf8
49except AttributeError:
50    def _fromUtf8(s):
51        return s
52
53try:
54    _encoding = QtGui.QApplication.UnicodeUTF8
55    def _translate(context, text, disambig):
56        return QtGui.QApplication.translate(context, text, disambig, _encoding)
57except AttributeError:
58    def _translate(context, text, disambig):
59        return QtGui.QApplication.translate(context, text, disambig)
60
61class Ui_Dialog(object):
62    def setupUi(self, Dialog):
63        Dialog.setObjectName(_fromUtf8("Create UI with QtDesigner"))
64        Dialog.setMinimumSize(QtCore.QSize(300, 200))
65        self.frame = QtGui.QFrame(Dialog)
66        self.frame.setGeometry(QtCore.QRect(4, 4, 268, 63))
67        self.frame.setObjectName(_fromUtf8("frame"))
68        self.keepPyBox = QtGui.QCheckBox(Dialog)
69        self.keepPyBox.setGeometry(QtCore.QRect(166, 98, 117, 21))
70        self.keepPyBox.setObjectName(_fromUtf8("keepPyBox"))
71        self.addPyrunBox = QtGui.QCheckBox(Dialog)
72        self.addPyrunBox.setGeometry(QtCore.QRect(21, 98, 141, 21))
73        self.addPyrunBox.setObjectName(_fromUtf8("addPyrunBox"))
74        self.convertButton = QtGui.QCommandLinkButton(Dialog)
75        self.convertButton.setGeometry(QtCore.QRect(70, 130, 168, 41))
76        self.convertButton.setObjectName(_fromUtf8("convertButton"))
77        self.line = QtGui.QFrame(Dialog)
78        self.line.setGeometry(QtCore.QRect(21, 80, 262, 16))
79        self.line.setFrameShape(QtGui.QFrame.HLine)
80        self.line.setFrameShadow(QtGui.QFrame.Sunken)
81        self.line.setObjectName(_fromUtf8("line"))
82        self.layoutWidget = QtGui.QWidget(Dialog)
83        self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 260, 53))
84        self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
85        self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
86        self.gridLayout.setMargin(0)
87        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
88        self.label = QtGui.QLabel(self.layoutWidget)
89        self.label.setObjectName(_fromUtf8("label"))
90        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
91        self.uiNameEdit = QtGui.QLineEdit(self.layoutWidget)
92        self.uiNameEdit.setObjectName(_fromUtf8("uiNameEdit"))
93        self.gridLayout.addWidget(self.uiNameEdit, 0, 1, 1, 3)
94        self.newButton = QtGui.QPushButton(self.layoutWidget)
95        icon = QtGui.QIcon.fromTheme(_fromUtf8("document-new"))
96        self.newButton.setIcon(icon)
97        self.newButton.setObjectName(_fromUtf8("newButton"))
98        self.gridLayout.addWidget(self.newButton, 1, 0, 1, 2)
99        self.openButton = QtGui.QPushButton(self.layoutWidget)
100        icon = QtGui.QIcon.fromTheme(_fromUtf8("document-open"))
101        self.openButton.setIcon(icon)
102        self.openButton.setObjectName(_fromUtf8("openButton"))
103        self.gridLayout.addWidget(self.openButton, 1, 2, 1, 1)
104        self.designButton = QtGui.QPushButton(self.layoutWidget)
105        self.designButton.setToolTip(_fromUtf8(""))
106        self.designButton.setWhatsThis(_fromUtf8(""))
107        icon = QtGui.QIcon.fromTheme(_fromUtf8("applications-engineering"))
108        self.designButton.setIcon(icon)
109        self.designButton.setObjectName(_fromUtf8("designButton"))
110        self.gridLayout.addWidget(self.designButton, 1, 3, 1, 1)
111        self.layoutWidget1 = QtGui.QWidget(Dialog)
112        self.layoutWidget1.setGeometry(QtCore.QRect(0, 0, 2, 2))
113        self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
114        self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget1)
115        self.verticalLayout.setMargin(0)
116        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
117        self.splitter = QtGui.QSplitter(Dialog)
118        self.splitter.setGeometry(QtCore.QRect(0, 0, 0, 0))
119        self.splitter.setOrientation(QtCore.Qt.Horizontal)
120        self.splitter.setObjectName(_fromUtf8("splitter"))
121
122        self.retranslateUi(Dialog)
123        QtCore.QObject.connect(self.designButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.runDesigner)
124        QtCore.QObject.connect(self.convertButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.convert)
125        QtCore.QObject.connect(self.openButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.openDialog)
126        QtCore.QObject.connect(self.newButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.newDialog)
127
128    def runDesigner(self):
129		uiFile = "\""+self.uiNameEdit.text+"\""
130    		if (len(uiFile)<3):
131    			self.newDialog()
132    		os.system("designer "+uiFile + "&")
133
134    def openDialog(self):
135        #fd = QtGui.QFileDialog()
136        fileName = QtGui.QFileDialog.getOpenFileName()
137        self.uiNameEdit.setText(fileName)
138
139    def newDialog(self):
140        fileName = QtGui.QFileDialog.getSaveFileName(0,"New UI","*.ui")
141        self.uiNameEdit.setText(fileName)
142
143    def convert(self):
144		uiFile = self.uiNameEdit.text
145		if (len(uiFile)<3): #nothing inserted
146    			QtGui.QMessageBox.critical(0,"Error","ui file name is not inserted! I don't know, what to handle. Sorry.")
147    			return
148
149		self.insertMessageBox=QtGui.QMessageBox(QtGui.QMessageBox.Information, "Insert", "Place cursor where you would like the code to be pasted")
150		self.insertMessageBox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
151		self.insertMessageBox.setModal(False) # don't block
152		QtCore.QObject.connect( self.insertMessageBox.button(self.insertMessageBox.Ok), QtCore.SIGNAL("clicked()"), self.insertText)
153		self.insertMessageBox.setWindowFlags(self.insertMessageBox.windowFlags() | QtCore.Qt.WindowStaysOnTopHint) # on top until a buttin is pressed
154		self.insertMessageBox.show()
155
156
157    def insertText(self):
158		#uiFile = "\'"+self.uiNameEdit.text+"\'"
159		uiFile = self.uiNameEdit.text
160		tmpPyfile = string.replace(uiFile,".ui","_tmp.py")
161
162		cmd = pyuic4Command + " " + uiFile + " -x -o " + tmpPyfile #?? kui tühikud?
163		print cmd
164		ret = call([pyuic4Command,uiFile,"-x","-o",tmpPyfile])	# parem popen
165		print ret
166
167		if (ret == 0):
168    			pythonQtCode = makeReplacements(tmpPyfile,self.addPyrunBox.checkState()) #, ui.addPyrunBox.checkState())
169			q.insertText(pythonQtCode)
170			if (ui.keepPyBox.checkState()== False):
171    				os.remove(tmpPyfile)
172    		else:
173        		QtGui.QMessageBox.critical(0,"Error","Could not execute " + pyuic4Command +" with ui file" + uiFile)
174
175    def retranslateUi(self, Dialog):
176        Dialog.setWindowTitle(_translate("Dialog", "Create UI with QtDesigner", None))
177        self.keepPyBox.setText(_translate("Dialog", "Keep PyQt4 file", None))
178        self.addPyrunBox.setText(_translate("Dialog", "Add pyrun {{ ... }}", None))
179        self.convertButton.setText(_translate("Dialog", "Convert and insert", None))
180        self.label.setText(_translate("Dialog", "UI file:", None))
181        self.newButton.setText(_translate("Dialog", "New", None))
182        self.openButton.setText(_translate("Dialog", "Select", None))
183        self.designButton.setText(_translate("Dialog", "Design", None))
184
185
186
187
188
189if __name__ == "__main__":
190    Dialog = QtGui.QDialog()
191    ui = Ui_Dialog()
192    ui.setupUi(Dialog)
193    Dialog.show()
194
195
196
197
198
199
200