Henning Schröder avatar Henning Schröder committed db57ee3

added simple console

Comments (0)

Files changed (3)

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import sys
+from PyQt4.QtCore import Qt
+from PyQt4.QtGui import QDialog, QPlainTextEdit, QVBoxLayout, QSplitter, QPushButton, QTextCursor
+
+
+class EditorWidget(QPlainTextEdit):
+
+	
+	def __init__(self, parent):
+		super(EditorWidget, self).__init__(parent)
+
+
+
+class OutputWidget(QPlainTextEdit):
+
+	
+	def __init__(self, parent):
+		super(OutputWidget, self).__init__(parent)
+		self.setReadOnly(True)
+		sys.stdout = self
+		sys.stderr = self
+		self.buf = u""
+		self.startTimer(500)
+
+		
+	def write(self, s):
+		sys.__stdout__.write(s)
+		self.buf += unicode(s)
+
+		
+	def writeline(self, s):
+		self.write(u"%s\n" % s)
+
+		
+	def timerEvent(self, event):
+		self.flush()
+
+		
+	def flush(self):
+		if self.buf:
+			if not self.window().isVisible():
+				self.window().show()
+			end_block = self.document().end()
+			cursor = QTextCursor(end_block)
+			self.setTextCursor(cursor)
+			self.moveCursor(QTextCursor.EndOfBlock, QTextCursor.MoveAnchor)
+			cursor.insertText(self.buf)			
+			self.ensureCursorVisible()
+			self.buf = ""
+
+
+	
+class Console(QDialog):
+
+
+	def __init__(self, namespace=None):
+		QDialog.__init__(self)
+		if namespace is None:
+			namespace = {}
+		self.namespace = namespace
+		self.vblayout = QVBoxLayout(self)
+		self.splitter = QSplitter(Qt.Vertical, self)
+		self.layout().addWidget(self.splitter)
+		self.edit = EditorWidget(self.splitter)
+		self.edit.setPlainText("print 'Hello, world'")
+		self.output = OutputWidget(self.splitter)		
+		self.runButton = QPushButton(self, text="&Run")
+		self.runButton.clicked.connect(self.run)
+		self.layout().addWidget(self.runButton)
+		self.edit.setFocus()
+
+		
+	def run(self):
+		code = unicode(self.edit.toPlainText())
+		try:
+			exec code in self.namespace
+		except Exception, e:
+			print e
+			import traceback
+			traceback.print_exc()
+	
+		
+
+		
+
+		
+if __name__ == "__main__":
+	import sys
+	from PyQt4.QtGui import QApplication
+	app = QApplication(sys.argv)
+	win = Console()
+	win.show()
+	app.exec_()
+	
 
 from pyqtproxy import class_from_metaobject, wrap_instance
 
-
+namespace = {}
 
 embpyqt = wrap_instance(qApp.property("embedded_pyqt").toPyObject())
+namespace.update(embpyqt=embpyqt)
+
+from console import Console
+embpyqt_console = Console(namespace)
 
 
 def get_class(name):
 print "embpyqt:", embpyqt, dir(embpyqt)
 
 Test = get_class("Test")
+namespace.update(Test=Test)
+
 print "Test:", Test, dir(Test)
 t = Test()
 t.test()
 
 from pyqtstubs import StubCreator
 stubs = StubCreator()
-stubs.add_class(embpyqt._qt.__class__)
-stubs.add_class(t._qt.__class__)
+stubs.add_class(embpyqt.__class__)
+stubs.add_class(t)
 print stubs
 
 embpyqt.setException("foo", "baz")
 # -*- coding: utf-8 -*-
 import keyword
 
-from PyQt4.QtCore import QMetaMethod
+from PyQt4.QtCore import QMetaMethod, QObject
 from pyqtproxy import class_from_metaobject
 
 
 
 
 	def __str__(self):
-		return "\n".join(self.output)
+		return "\n".join(["# coding: utf-8 -*-", "# created stub"] + self.output)
 	
 
 
 		self.codegen.newline()
 
 	def add_class(self, cls):
-		pcls = class_from_metaobject(cls.staticMetaObject)
+		if isinstance(cls, QObject):
+			pcls = class_from_metaobject(cls.staticMetaObject)
+		else:
+			pcls = cls
 		self.codegen.newline()
 		self.codegen.begin("class %s(object):" % pcls._metaobject.className())
 		self.codegen.newline()
 		qt_types = set()
 		# -- methods --
 		for method_name, methods in pcls._methods.items():
-			if method_name.startswith("_"):
+			if method_name.startswith("_") or method_name in ("deleteLater",):
 				continue # skip private methods
 			for signature, method in methods.items():
 				param_names = map(unicode, method.param_names)
 				break
 		# -- properties --
 		for property_name, prop in pcls._properties.items():
+			if property_name in ("destroyed",):
+				continue
 			ptype = self.fix_type(prop.type)
 			if ptype.startswith("Q"):
 				qt_types.add(ptype)
 		self.codegen.begin("def __assert_property_types(self):")
 		self.codegen.emit("# gives source code analyzers hints about assumed type of property")
 		for property_name, prop in pcls._properties.items():
+			if property_name in ("destroyed",):
+				continue
 			ptype = self.fix_type(prop.type)
 			self.codegen.emit("assert isinstance(self.%s, %s)" % (property_name, ptype))
 		self.codegen.end()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.