This repository was archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelementHandler.py
More file actions
141 lines (111 loc) · 4.39 KB
/
elementHandler.py
File metadata and controls
141 lines (111 loc) · 4.39 KB
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
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
import sys
from PyQt4 import QtCore, QtGui
from threading import Timer
import settings
import os
class ElementHandler(QtGui.QWidget):
def __init__(self, statusHandler=None):
"""
Creates a list for storing the individual gui elements
Might need restructuring later down the line for the math stuff,
but this will do for now.
"""
super(ElementHandler,self).__init__()
self.elements = []
# Create a vertical layout so we can scroll.
self.elementLayout = QtGui.QVBoxLayout(self)
self.setLayout(self.elementLayout)
self.elementLayout.setAlignment(QtCore.Qt.AlignBottom)
self.appendElement()
def appendElement(self):
"""
Appends a new element and finalizes the previous one.
"""
newElement = GuiMathElement(self)
self.elementLayout.addWidget(newElement)
# Finalize the previous element so weird stuff doesn't happen.
if len(self.elements) != 0:
self.elements[-1].finalize()
self.elements.append(newElement)
self.connect(self.elements[-1], QtCore.SIGNAL("final"), self.appendElement)
def resizeEvent(self,event):
"""
If this is resized, ensure that the inner widgets are properly
resized as well.
"""
super(ElementHandler, self).resizeEvent(event)
self.resize(event.size())
for i in self.elements:
# TODO: FIX MAGIC NUMBER HERE
i.setFixedWidth(self.width()-18)
# Have to do this to redraw the layout.
self.elementLayout.activate()
class GuiMathElement(QtGui.QWidget):
# This should be about 1-2 lines for our text size.
INITIAL_HEIGHT = 37
text = None
sendButton = None
layout = None
def __init__(self, parent=None):
"""
Init stuff to be used in this gui element.
"""
super(GuiMathElement,self).__init__(parent)
self.__initChildren()
def __initChildren(self):
"""
Inits child widgets.
self.text is the textbox.
"""
self.text = QtGui.QTextEdit()
# Magic number comes from added gui elements and text.
self.text.setFixedHeight(self.INITIAL_HEIGHT)
# connect textchanges signal to our own private slot.
# This resizes the textBox as new text is added.
self.text.textChanged.connect(self.__textChanged)
self.text.setSizePolicy(QtGui.QSizePolicy.Expanding, \
QtGui.QSizePolicy.Fixed)
self.sendButton = QtGui.QPushButton('&Send',self)
self.sendButton.clicked.connect(self.sendClicked)
self.vLayout = QtGui.QVBoxLayout(self)
self.hLayout = QtGui.QHBoxLayout()
self.hLayout.addWidget(self.text)
self.hLayout.addWidget(self.sendButton)
self.vLayout.addLayout(self.hLayout)
self.setLayout(self.vLayout)
def __textChanged(self):
"""
This function is only called by the signal textChanged from the self.text
textbox. It resizes the textbox and the containing widget to make sure there are no
scroll bars as they are terrible on a touchscreen.
"""
# base resizing of the widget on the changing text.
# A sprinkling of magic numbers to keep things nice looking.
baseHeight = self.text.document().size().height()
if baseHeight >= self.INITIAL_HEIGHT-5:
self.text.setFixedHeight(baseHeight+5)
self.sendButton.setFixedHeight(baseHeight+5)
self.text.ensureCursorVisible()
def finalize(self):
"""
This finalizes the code in the text box.
Should only be called once the code is rendered.
Also greys out the box so that text cannot be edited.
"""
self.text.setReadOnly(True)
self.sendButton.setEnabled(False)
def sendClicked(self):
if len(self.text.toPlainText()) > 0:
self.emit(QtCore.SIGNAL("final"))
def loadImage(self, imagePath):
# load the image from the path given (has to point to a file.)
if os.path.exists(imagePath) is not True:
# nothing loaded!
return False
# load image from path given.
print(imagePath)
self.tex = QtGui.QLabel(self)
self.tex.setPixmap(QtGui.QPixmap(imagePath))
print(self.tex)
self.vLayout.insertWidget(0,self.tex)