-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregion_color_selector.py
279 lines (227 loc) · 10.1 KB
/
region_color_selector.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python
windowSize = (760, 1050)
windowName = 'Color Selector'
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import nett_python as nett
from float_message_pb2 import *
from float_vector_message_pb2 import *
from color_table_message_pb2 import *
import helper
class AreaTable(QtGui.QDialog):
def __init__(self, parent=None):
super(AreaTable, self).__init__(parent)
self.setWindowTitle('Region Browser')
self.resize(windowSize[0], windowSize[1])
self.AreaTableIndexMap = []
self.Table = QtGui.QTableWidget()
self.Table.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers);
self.Table.cellClicked.connect(self.choose_color)
self.color_table = {}
self.color_slot = nett.slot_out_color_table_message('color_table')
def choose_color(self):
region_state = {}
selectionIndex = self.Table.currentRow()
selectionIndexColumn = self.Table.currentColumn()
currentSelection = self.Table.selectedIndexes()[0].row()
if currentSelection not in self.color_table:
self.color_table[currentSelection] = list()
if selectionIndexColumn == 0:
return
if selectionIndexColumn == 1: #choose color i
for idx in self.Table.selectedIndexes():
color = QtGui.QColorDialog.getColor()
item = QtGui.QTableWidgetItem()
item.setBackgroundColor(color)
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['color_i'] = (color.red(), color.green(), color.blue())
if selectionIndexColumn == 4: #choose color e
for idx in self.Table.selectedIndexes():
color = QtGui.QColorDialog.getColor()
item = QtGui.QTableWidgetItem()
item.setBackgroundColor(color)
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['color_e'] = (color.red(), color.green(), color.blue())
if selectionIndexColumn == 3: #choose thickness i
for idx in self.Table.selectedIndexes():
num, ok = QInputDialog.getInt(self, "Thickness","Enter a number")
if ok:
item = QtGui.QTableWidgetItem()
item.setText(str(num))
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['thickness_i'] = num
else:
return
if selectionIndexColumn == 6: #choose thickness e
for idx in self.Table.selectedIndexes():
num, ok = QInputDialog.getInt(self, "Thickness","Enter a number")
if ok:
item = QtGui.QTableWidgetItem()
item.setText(str(num))
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['thickness_e'] = num
if selectionIndexColumn == 2: #choose style i
for idx in self.Table.selectedIndexes():
items = ("SolidLine", "DashLine", "DashDotLine", "DashDotDotLine")
itemChoice, ok = QInputDialog.getItem(self, "Style I",
"Styles:", items, 0, False)
if ok:
item = QtGui.QTableWidgetItem()
item.setText(str(itemChoice))
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['style_i'] = str(itemChoice)
else:
return
if selectionIndexColumn == 5: #choose style e
for idx in self.Table.selectedIndexes():
items = ("SolidLine", "DashLine", "DashDotLine", "DashDotDotLine")
itemChoice, ok = QInputDialog.getItem(self, "Style E",
"Styles:", items, 0, False)
if ok:
item = QtGui.QTableWidgetItem()
item.setText(str(itemChoice))
self.Table.setItem(idx.row(), selectionIndexColumn, item)
region_state['style_e'] = str(itemChoice)
else:
return
self.color_table[currentSelection].append(region_state)
def encode_color_table(self):
#print self.color_table
msg = color_table_message()
print '---------'
for key in self.color_table:
region_msg = region_color_message()
region_msg.region_number = int(key)
for value in self.color_table[key]:
for k in value:
if k == 'color_i':
region_msg.color_i_r = value[k][0]
region_msg.color_i_g = value[k][1]
region_msg.color_i_b = value[k][2]
if k == 'color_e':
region_msg.color_e_r = value[k][0]
region_msg.color_e_g = value[k][1]
region_msg.color_e_b = value[k][2]
if k == 'thickness_i':
region_msg.thickness_i = value[k]
if k == 'thickness_e':
region_msg.thickness_e = value[k]
if k == 'style_i':
region_msg.style_i = str(value[k])
if k == 'style_e':
region_msg.style_e = str(value[k])
msg.value.extend([region_msg])
print msg
f = open('color_table.bin', "wb")
f.write(msg.SerializeToString())
f.close()
self.color_slot.send(msg.SerializeToString())
def load_color_table(self):
f = open('color_table.bin', "rb")
msg = color_table_message()
msg.ParseFromString(f.read())
f.close()
for x in range(0, len(msg.value)):
region_state = {}
item = QtGui.QTableWidgetItem()
color = QColor(msg.value[x].color_i_r, msg.value[x].color_i_g, msg.value[x].color_i_b)
item.setBackgroundColor(color)
self.Table.setItem(msg.value[x].region_number, 1, item)
region_state['color_i'] = (msg.value[x].color_i_r, msg.value[x].color_i_g, msg.value[x].color_i_b)
item = QtGui.QTableWidgetItem()
color = QColor(msg.value[x].color_e_r, msg.value[x].color_e_g, msg.value[x].color_e_b)
item.setBackgroundColor(color)
self.Table.setItem(msg.value[x].region_number, 4, item)
region_state['color_e'] = (msg.value[x].color_e_r, msg.value[x].color_e_g, msg.value[x].color_e_b)
item = QtGui.QTableWidgetItem()
item.setText(msg.value[x].style_i)
self.Table.setItem(msg.value[x].region_number, 2, item)
region_state['style_i'] = msg.value[x].style_i
item = QtGui.QTableWidgetItem()
item.setText(msg.value[x].style_e)
self.Table.setItem(msg.value[x].region_number, 5, item)
region_state['style_e'] = msg.value[x].style_e
item = QtGui.QTableWidgetItem()
item.setText(str(msg.value[x].thickness_i))
self.Table.setItem(msg.value[x].region_number, 3, item)
region_state['thickness_i'] = msg.value[x].thickness_i
item = QtGui.QTableWidgetItem()
item.setText(str(msg.value[x].thickness_e))
self.Table.setItem(msg.value[x].region_number, 6, item)
region_state['thickness_e'] = msg.value[x].thickness_e
if msg.value[x].region_number not in self.color_table:
self.color_table[msg.value[x].region_number] = list()
self.color_table[msg.value[x].region_number].append(region_state)
self.color_slot.send(msg.SerializeToString())
def updateTable(self, areaList):
self.Table.setRowCount(len(areaList))
self.Table.setColumnCount(7)
self.Table.setHorizontalHeaderLabels( ['Region', 'Color I', 'Style I', 'Thickness I', 'Color E', 'Style E', 'Thickness E'] )
i = 0
for k in areaList:
nameOfArea = str(k)
self.Table.setItem(i, 0, QtGui.QTableWidgetItem(nameOfArea))
self.AreaTableIndexMap.append( (i, nameOfArea) )
i = i + 1
layout = QtGui.QGridLayout()
layout.addWidget(self.Table)
self.setLayout(layout)
self.load_color_table()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainWindow,self).__init__(parent)
self.resize(windowSize[0], windowSize[1])
self.setWindowTitle(windowName)
self.setDockOptions(QtGui.QMainWindow.AnimatedDocks)
self.centralwidget = QtGui.QWidget(self)
self.centralwidget.hide()
self.setCentralWidget(self.centralwidget)
self.createAreaBrowserWindow()
self.initActions()
def initActions(self):
self.exitAction = QtGui.QAction('Quit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.setStatusTip('Exit application')
self.connect(self.exitAction, QtCore.SIGNAL('triggered()'), self.close)
self.addAction(self.exitAction)
self.saveAction = QtGui.QAction('Save and send', self)
self.saveAction.setShortcut('Ctrl+S')
self.saveAction.setStatusTip('save and send')
self.connect(self.saveAction, QtCore.SIGNAL('triggered()'), self.AreaBrowserTable.encode_color_table)
self.addAction(self.saveAction)
self.loadAction = QtGui.QAction('Load', self)
self.loadAction.setShortcut('Ctrl+L')
self.loadAction.setStatusTip('load')
self.connect(self.loadAction, QtCore.SIGNAL('triggered()'), self.AreaBrowserTable.load_color_table)
self.addAction(self.loadAction)
def createAreaBrowserWindow(self):
areaBrowserWidget = QtGui.QDockWidget(self)
areaBrowserWidget.setFeatures(QtGui.QDockWidget.AllDockWidgetFeatures)
areaBrowserWidget.setWindowTitle(QtGui.QApplication.translate("self", "Regions:", None, QtGui.QApplication.UnicodeUTF8))
self.AreaBrowserTable = AreaTable()
areaBrowserWidget.setWidget(self.AreaBrowserTable)
self.addDockWidget(QtCore.Qt.DockWidgetArea(1), areaBrowserWidget)
def close(self):
QtGui.qApp.quit()
nett.initialize('tcp://127.0.0.1:2015')
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
ip = helper.obtain_ip_address_compute()
area_list_slot_in = nett.slot_in_float_message()
area_list_slot_in.connect('tcp://'+ip+':8000', 'num_regions')
print 'waiting for region list...'
msg = float_message()
msg.ParseFromString(area_list_slot_in.receive())
areaList = []
for i in range(0, int(msg.value)):
areaList.append(str(i))
print 'received!'
mainWindow.AreaBrowserTable.updateTable(areaList)
if __name__ == "__main__":
app.exec_()
sys.exit()