Example #1
0
class Interpreter(QWidget):
	lastCommands = []
	lastIndex = 0


	def __init__(self, context):
		QWidget.__init__(self)
		self.context = context

		self.setupUI()
		self.setWindowTitle('Interpreter')
		self.setMinimumSize(400, 250)

	def setupUI(self):
		layout = QVBoxLayout()
		layout.setContentsMargins(0, 0, 0, 0)

		self.out = QTextEdit()
		self.out.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
		self.out.setTextInteractionFlags(Qt.TextSelectableByMouse | Qt.TextSelectableByKeyboard)
		self.highlighter = PythonSyntaxHighlighter( self.out, prefix = ">>>" )

		self.input = QLineEdit()
		self.input.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum )
		layout.addWidget(self.out, 1)
		layout.addWidget(self.input, 0)

		self.setLayout(layout)

		QWidget.connect(self.input, SIGNAL('returnPressed()'), self.run)

	def run(self):
		scode = str(self.input.text()).strip()
		self.out.append(">>> " + scode + "\n")
		self.input.setText('')

		name = threading.current_thread().name
		try:
			sys.stdout.flush(name)
			if len(self.lastCommands) == 100:
				del self.lastCommands[0]
			self.lastCommands.append(scode)
			self.lastIndex = 0

			try:
				out = eval(scode, self.context)
				if out:
					print out
			except:
				code = compile(scode, '', 'exec')
				exec code in self.context
		except:
			traceback.print_exc()
		finally:
			self.write( sys.stdout.getOutput(name) )

	def keyPressEvent(self, event):

		# Prev in command history
		if event.key() == Qt.Key_Up:
			try:
				self.lastIndex -= 1
				self.input.setText(self.lastCommands[self.lastIndex])
			except:
				self.lastIndex += 1

		# Next in command history
		elif event.key() == Qt.Key_Down:
			try:
				self.lastIndex += 1
				if self.lastIndex >= 0:
					self.lastIndex = 0
					self.input.setText('')
				else:
					self.input.setText(self.lastCommands[self.lastIndex])
			except:
				self.lastIndex -= 1

		# Clear screen
		elif event.key() == Qt.Key_L:
			if event.modifiers() == Qt.ControlModifier:
				lines = self.out.height() / self.out.fontMetrics().height()
				#for i in xrange(lines):
				#	self.out.append('')
				self.write('\n' * lines)
				self.out.moveCursor( QTextCursor.End )

	#def getOutStream(self):
	#	return self.TextOutput(self.out)

	def write(self, data):
		self.out.moveCursor( QTextCursor.End )
		try:
			self.out.insertPlainText( unicode(data) )
		except:
			self.out.insertPlainText( data )
		self.out.moveCursor( QTextCursor.End )
Example #2
0
class Ui_RightWidget(object):
    def setupUi(self, RightWidget, server):
        RightWidget.setMinimumHeight(500)

        main_layout = QVBoxLayout(RightWidget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(10)

        # We hide all the main elements, to allow the proper viewer to enable
        # (only) the elements that uses.

        if hasattr(server, 'wild_chars'):
            self.text_map = QTextEdit()
            self.text_map.setObjectName('text_map')
            self.text_map.setVisible(False)
            self.text_map.setFocusPolicy(Qt.NoFocus)
            self.text_map.setAutoFillBackground(True)
            self.text_map.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.text_map.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.text_map.setUndoRedoEnabled(False)
            self.text_map.setReadOnly(True)
            # for some unknown reason, the fontmetrics of the text map is wrong
            # if the font is set with a global stylesheet, so we set it on the
            # specific widget.
            self.text_map.setStyleSheet("QTextEdit { font: 13px \"Courier\";}")
            main_layout.addWidget(self.text_map)

            # We calculate the map area size using the size of the font used. We
            # assume that the font is a monospace ones.
            font_metrics = self.text_map.fontMetrics()
            self.text_map.setFixedWidth(font_metrics.width('#' * server.map_width))
            self.text_map.setFixedHeight(font_metrics.height() * server.map_height)

            # The rightwidget width is determined by the map area size
            RightWidget.setMinimumWidth(self.text_map.width())
        else:
            RightWidget.setMinimumWidth(220)

        self.box_status = QWidget()
        self.box_status.setVisible(False)
        main_layout.addWidget(self.box_status)

        status_layout = QGridLayout(self.box_status)
        status_layout.setContentsMargins(5, 5, 5, 0)
        status_layout.setHorizontalSpacing(0)
        status_layout.setVerticalSpacing(15)

        label_health = QLabel()
        label_health.setMinimumWidth(80)
        label_health.setText(QApplication.translate("RightWidget", "Health"))
        status_layout.addWidget(label_health, 0, 0)

        self.bar_health = QProgressBar()
        self.bar_health.setObjectName("bar_health")
        self.bar_health.setFixedHeight(22)
        self.bar_health.setProperty("value", QVariant(100))
        self.bar_health.setTextVisible(False)
        status_layout.addWidget(self.bar_health, 0, 1)

        label_mana = QLabel()
        label_mana.setMinimumWidth(80)
        label_mana.setText(QApplication.translate("RightWidget", "Mana"))
        status_layout.addWidget(label_mana, 1, 0)

        self.bar_mana = QProgressBar()
        self.bar_mana.setObjectName("bar_mana")
        self.bar_mana.setFixedHeight(22)
        self.bar_mana.setProperty("value", QVariant(100))
        self.bar_mana.setTextVisible(False)
        status_layout.addWidget(self.bar_mana, 1, 1)

        label_movement = QLabel()
        label_movement.setMinimumWidth(80)
        label_movement.setText(QApplication.translate("RightWidget", "Movement"))
        status_layout.addWidget(label_movement, 2, 0)

        self.bar_movement = QProgressBar()
        self.bar_movement.setObjectName("bar_movement")
        self.bar_movement.setFixedHeight(22)
        self.bar_movement.setProperty("value", QVariant(100))
        self.bar_movement.setTextVisible(False)
        status_layout.addWidget(self.bar_movement, 2, 1)

        main_layout.addStretch()