summaryrefslogtreecommitdiffstats
path: root/awlsim/gui/valuelineedit.py
blob: 369f2ed454022effdae96e70d87fe7292d748636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI text value line edit widget
#
# Copyright 2014-2015 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *

from awlsim.gui.util import *


class ValidatorCallback(QValidator):
	def __init__(self, callback, parent=None):
		QValidator.__init__(self, parent)
		self.callback = callback

	def validate(self, inputString, pos):
		state = self.callback(inputString, pos)
		if isPySide:
			return state
		elif isPyQt:
			return (state, inputString, pos)
		assert(0)

class ValueLineEdit(QLineEdit):
	valueChanged = Signal(str)

	def __init__(self, validatorCallback=None, parent=None):
		QLineEdit.__init__(self, parent)

		self.setAlignment(Qt.AlignRight)
		if validatorCallback:
			self.__validatorCallback = validatorCallback
			self.setValidator(ValidatorCallback(self.__validator, self))

		self.__editing = False
		self.__activeText = ""

		self.setReadOnly(True)
		self.__setColors(True)

		self.returnPressed.connect(self.__handleReturnPressed)

	def __setColors(self, validInput):
		pal = self.palette()
		if self.isEnabledTo(None):
			if self.__editing:
				pal.setColor(QPalette.Base, QColor("#FFFFC0"))
			else:
				pal.setColor(QPalette.Base, Qt.white)
		else:
			pal.setColor(QPalette.Base, pal.color(QPalette.Window))
		if validInput:
			pal.setColor(QPalette.Text, Qt.black)
		else:
			pal.setColor(QPalette.Text, Qt.red)
		self.setPalette(pal)

	def changeEvent(self, ev):
		QLineEdit.changeEvent(self, ev)
		if not self.isEnabledTo(None):
			self.__setColors(True)

	def __validator(self, inputString, pos):
		res = self.__validatorCallback(inputString, pos)
		self.__setColors(res == QValidator.Acceptable)
		return res

	def __handleReturnPressed(self):
		if self.__editing:
			self.__editing = False
			self.__setColors(True)
			self.setReadOnly(True)
			self.__activeText = QLineEdit.text(self)
			self.valueChanged.emit(self.__activeText)

	def __cancelEdit(self):
		if self.__editing:
			self.__editing = False
			self.__setColors(True)
			self.setReadOnly(True)
			QLineEdit.setText(self, self.__activeText)

	def focusOutEvent(self, event):
		QLineEdit.focusOutEvent(self, event)
		self.__cancelEdit()

	def mousePressEvent(self, ev):
		QLineEdit.mousePressEvent(self, ev)
		if self.__editing:
			return
		if ev.button() & (Qt.LeftButton |
				  Qt.MidButton |
				  Qt.RightButton):
			self.__editing = True
			self.setReadOnly(False)
			self.__setColors(True)

	def keyPressEvent(self, ev):
		QLineEdit.keyPressEvent(self, ev)
		if ev.key() == Qt.Key_Escape:
			self.__cancelEdit()

	def text(self):
		return self.__activeText

	def setText(self, newText):
		self.__activeText = newText
		if not self.__editing:
			QLineEdit.setText(self, newText)

	def clear(self):
		self.setText("")
bues.ch cgit interface