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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
# -*- coding: utf-8 -*-
#
# AWL simulator - Block interface table model
#
# Copyright 2016-2017 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.common.xmlfactory import *
from awlsim.core.datatypes import *
from awlsim.gui.interfedit.interfdef import *
from awlsim.gui.util import *
class AwlInterfaceModel_factory(XmlFactory):
def parser_open(self, tag=None):
self.inSection = "interface"
self.model.beginResetModel()
self.model.clear()
if tag:
self.model.configure(
haveIn=tag.getAttrBool("allow_inputs", False),
haveOut=tag.getAttrBool("allow_outputs", False),
haveInOut=tag.getAttrBool("allow_inouts", False),
haveStat=tag.getAttrBool("allow_stats", False),
haveTemp=tag.getAttrBool("allow_temps", False),
haveRetVal=tag.getAttrBool("allow_retval", False),
haveInitValue=tag.getAttrBool("allow_initvalue", False))
XmlFactory.parser_open(self, tag)
def parser_close(self):
self.model.endResetModel()
XmlFactory.parser_close(self)
def parser_beginTag(self, tag):
interf = self.model.interf
def mkField(tag):
return AwlInterfFieldDef(
name=tag.getAttr("name", ""),
typeStr=tag.getAttr("type", ""),
initValueStr=tag.getAttr("init", ""),
comment=tag.getAttr("comment", ""),
uuid=tag.getAttr("uuid", None))
if self.inSection == "interface":
if (tag.name == "inputs" and self.model.haveIn) or\
(tag.name == "outputs" and self.model.haveOut) or\
(tag.name == "inouts" and self.model.haveInOut) or\
(tag.name == "stats" and self.model.haveStat) or\
(tag.name == "temps" and self.model.haveTemp) or\
(tag.name == "retval" and self.model.haveRetVal):
self.inSection = tag.name
return
elif self.inSection == "inputs":
if tag.name == "field":
interf.inFields.append(mkField(tag))
return
elif self.inSection == "outputs":
if tag.name == "field":
interf.outFields.append(mkField(tag))
return
elif self.inSection == "inouts":
if tag.name == "field":
interf.inOutFields.append(mkField(tag))
return
elif self.inSection == "stats":
if tag.name == "field":
interf.statFields.append(mkField(tag))
return
elif self.inSection == "temps":
if tag.name == "field":
interf.tempFields.append(mkField(tag))
return
elif self.inSection == "retval":
if tag.name == "field":
interf.retValField = mkField(tag)
return
XmlFactory.parser_beginTag(self, tag)
def parser_endTag(self, tag):
if self.inSection == "interface":
if tag.name == self.inSection:
self.parser_finish()
return
else:
if tag.name == self.inSection:
self.inSection = "interface"
return
if tag.name == "field":
return
XmlFactory.parser_endTag(self, tag)
def composer_getTags(self):
model = self.model
interf = model.interf
def makeFields(fields):
tags = []
for field in fields:
if not field:
continue
tags.append(self.Tag(name="field",
attrs={
"name" : str(field.name),
"type" : str(field.typeStr),
"init" : str(field.initValueStr),
"comment" : str(field.comment),
"uuid" : str(field.uuid),
}))
return tags
inputTags = makeFields(interf.inFields)
outputTags = makeFields(interf.outFields)
inOutTags = makeFields(interf.inOutFields)
statTags = makeFields(interf.statFields)
tempTags = makeFields(interf.tempFields)
retValTags = makeFields([interf.retValField])
return [
self.Tag(name="interface",
attrs={
"allow_inputs" : str(int(model.haveIn)),
"allow_outputs" : str(int(model.haveOut)),
"allow_inouts" : str(int(model.haveInOut)),
"allow_stats" : str(int(model.haveStat)),
"allow_temps" : str(int(model.haveTemp)),
"allow_retval" : str(int(model.haveRetVal)),
"allow_initvalue" : str(int(model.haveInitValue)),
},
attrLineBreak=True,
tags=[
self.Tag(name="inputs",
tags=inputTags),
self.Tag(name="outputs",
tags=outputTags),
self.Tag(name="inouts",
tags=inOutTags),
self.Tag(name="stats",
tags=statTags),
self.Tag(name="temps",
tags=tempTags),
self.Tag(name="retval",
tags=retValTags),
]),
]
class AbstractTableModel(QAbstractTableModel):
def __init__(self, *args, **kwargs):
self.__resetCount = 0
QAbstractTableModel.__init__(self, *args, **kwargs)
def beginResetModel(self):
try:
if self.__resetCount <= 0:
QAbstractTableModel.beginResetModel(self)
finally:
self.__resetCount += 1
def endResetModel(self):
self.__resetCount -= 1
if self.__resetCount <= 0:
try:
QAbstractTableModel.endResetModel(self)
finally:
self.__resetCount = 0
class AwlInterfaceModel(AbstractTableModel):
factory = AwlInterfaceModel_factory
# Signal: Emitted, if some data in the model changed.
contentChanged = Signal()
def __init__(self,
haveIn=True, haveOut=True, haveInOut=True,
haveStat=True, haveTemp=True, haveRetVal=True,
haveInitValue=True):
AbstractTableModel.__init__(self)
self.interf = AwlInterfDef()
self.configure(haveIn, haveOut, haveInOut,
haveStat, haveTemp, haveRetVal,
haveInitValue)
def clear(self):
self.beginResetModel()
self.interf.clear()
self.endResetModel()
self.contentChanged.emit()
def configure(self,
haveIn=True, haveOut=True, haveInOut=True,
haveStat=True, haveTemp=True, haveRetVal=True,
haveInitValue=True):
self.beginResetModel()
self.haveIn = haveIn
if not haveIn:
self.interf.inFields = []
self.haveOut = haveOut
if not haveOut:
self.interf.outFields = []
self.haveInOut = haveInOut
if not haveInOut:
self.interf.inOutFields = []
self.haveStat = haveStat
if not haveStat:
self.interf.statFields = []
self.haveTemp = haveTemp
if not haveTemp:
self.interf.tempFields = []
self.haveRetVal = haveRetVal
if haveRetVal:
self.interf.retValField = AwlInterfFieldDef("RET_VAL", "VOID")
else:
self.interf.retValField = None
self.haveInitValue = haveInitValue
if not haveInitValue:
for field in self.interf.allFields:
field.initValueStr = ""
self.endResetModel()
self.contentChanged.emit()
def isEmpty(self):
return self.interf.isEmpty()
@property
def __nrRows_IN(self):
if self.haveIn:
return len(self.interf.inFields) + 1
return 0
@property
def __nrRows_OUT(self):
if self.haveOut:
return len(self.interf.outFields) + 1
return 0
@property
def __nrRows_INOUT(self):
if self.haveInOut:
return len(self.interf.inOutFields) + 1
return 0
@property
def __nrRows_STAT(self):
if self.haveStat:
return len(self.interf.statFields) + 1
return 0
@property
def __nrRows_TEMP(self):
if self.haveTemp:
return len(self.interf.tempFields) + 1
return 0
@property
def __nrRows_RETVAL(self):
if self.haveRetVal:
return 1
return 0
def __isRow_IN(self, row):
return self.haveIn and\
row < self.__nrRows_IN - 1
def __isRow_newIN(self, row):
return self.haveIn and\
row == self.__nrRows_IN - 1
def __isRow_OUT(self, row):
return self.haveOut and\
row >= self.__nrRows_IN and\
row < self.__nrRows_IN +\
self.__nrRows_OUT - 1
def __isRow_newOUT(self, row):
return self.haveOut and\
row == self.__nrRows_IN +\
self.__nrRows_OUT - 1
def __isRow_INOUT(self, row):
return self.haveInOut and\
row >= self.__nrRows_IN +\
self.__nrRows_OUT and\
row < self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT - 1
def __isRow_newINOUT(self, row):
return self.haveInOut and\
row == self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT - 1
def __isRow_STAT(self, row):
return self.haveStat and\
row >= self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT and\
row < self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT - 1
def __isRow_newSTAT(self, row):
return self.haveStat and\
row == self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT - 1
def __isRow_TEMP(self, row):
return self.haveTemp and\
row >= self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT and\
row < self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT +\
self.__nrRows_TEMP - 1
def __isRow_newTEMP(self, row):
return self.haveTemp and\
row == self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT +\
self.__nrRows_TEMP - 1
def __isRow_RETVAL(self, row):
return self.haveRetVal and\
row == self.__nrRows_IN +\
self.__nrRows_OUT +\
self.__nrRows_INOUT +\
self.__nrRows_STAT +\
self.__nrRows_TEMP +\
self.__nrRows_RETVAL - 1
def __row2field(self, row):
interf = self.interf
localRow = row
if self.__isRow_IN(row):
return interf.inFields[localRow]
localRow -= self.__nrRows_IN
if self.__isRow_OUT(row):
return interf.outFields[localRow]
localRow -= self.__nrRows_OUT
if self.__isRow_INOUT(row):
return interf.inOutFields[localRow]
localRow -= self.__nrRows_INOUT
if self.__isRow_STAT(row):
return interf.statFields[localRow]
localRow -= self.__nrRows_STAT
if self.__isRow_TEMP(row):
return interf.tempFields[localRow]
if self.__isRow_RETVAL(row):
return interf.retValField
return None
def __isColumn_name(self, column):
return column == 0
def __isColumn_type(self, column):
return column == 1
def __isColumn_initValue(self, column):
return self.haveInitValue and column == 2
def __isColumn_comment(self, column):
return column == 2 + (1 if self.haveInitValue else 0)
def deleteRow(self, row):
localRow = row
if self.__isRow_IN(row):
self.beginResetModel()
del self.interf.inFields[localRow]
self.endResetModel()
self.contentChanged.emit()
return True
localRow -= self.__nrRows_IN
if self.__isRow_OUT(row):
self.beginResetModel()
del self.interf.outFields[localRow]
self.endResetModel()
self.contentChanged.emit()
return True
localRow -= self.__nrRows_OUT
if self.__isRow_INOUT(row):
self.beginResetModel()
del self.interf.inOutFields[localRow]
self.endResetModel()
self.contentChanged.emit()
return True
localRow -= self.__nrRows_INOUT
if self.__isRow_STAT(row):
self.beginResetModel()
del self.interf.statFields[localRow]
self.endResetModel()
self.contentChanged.emit()
return True
localRow -= self.__nrRows_STAT
if self.__isRow_TEMP(row):
self.beginResetModel()
del self.interf.tempFields[localRow]
self.endResetModel()
self.contentChanged.emit()
return True
return False
def deleteRows(self, rows):
offset = 0
for row in sorted(rows):
if self.deleteRow(row + offset):
offset -= 1
def moveEntry(self, fromRow, toRow):
if fromRow == toRow:
return
if toRow > fromRow:
toRow += 1
fromList = fromListIndex = None
toList = toListIndex = None
# Get the from-list
localRow = fromRow
if self.__isRow_IN(fromRow):
fromList = self.interf.inFields
fromListIndex = localRow
localRow -= self.__nrRows_IN
if self.__isRow_OUT(fromRow):
fromList = self.interf.outFields
fromListIndex = localRow
localRow -= self.__nrRows_OUT
if self.__isRow_INOUT(fromRow):
fromList = self.interf.inOutFields
fromListIndex = localRow
localRow -= self.__nrRows_INOUT
if self.__isRow_STAT(fromRow):
fromList = self.interf.statFields
fromListIndex = localRow
localRow -= self.__nrRows_STAT
if self.__isRow_TEMP(fromRow):
fromList = self.interf.tempFields
fromListIndex = localRow
# Get the to-list
localRow = toRow
if self.__isRow_IN(toRow):
toList = self.interf.inFields
toListIndex = localRow
if self.__isRow_newIN(toRow):
toList = self.interf.inFields
toListIndex = len(toList)
localRow -= self.__nrRows_IN
if self.__isRow_OUT(toRow):
toList = self.interf.outFields
toListIndex = localRow
if self.__isRow_newOUT(toRow):
toList = self.interf.outFields
toListIndex = len(toList)
localRow -= self.__nrRows_OUT
if self.__isRow_INOUT(toRow):
toList = self.interf.inOutFields
toListIndex = localRow
if self.__isRow_newINOUT(toRow):
toList = self.interf.inOutFields
toListIndex = len(toList)
localRow -= self.__nrRows_INOUT
if self.__isRow_STAT(toRow):
toList = self.interf.statFields
toListIndex = localRow
if self.__isRow_newSTAT(toRow):
toList = self.interf.statFields
toListIndex = len(toList)
localRow -= self.__nrRows_STAT
if self.__isRow_TEMP(toRow):
toList = self.interf.tempFields
toListIndex = localRow
if self.__isRow_newTEMP(toRow):
toList = self.interf.tempFields
toListIndex = len(toList)
# If we have both a from-list and a to-list
# remove the field from the from-list
# and insert it into the to-list.
if fromList is not None and toList is not None:
self.beginResetModel()
if fromList is toList and\
fromListIndex < toListIndex:
toListIndex -= 1
field = fromList.pop(fromListIndex)
toList.insert(toListIndex, field)
self.endResetModel()
self.contentChanged.emit()
def rowCount(self, parent=QModelIndex()):
return sum((self.__nrRows_IN,
self.__nrRows_OUT,
self.__nrRows_INOUT,
self.__nrRows_STAT,
self.__nrRows_TEMP,
self.__nrRows_RETVAL))
def columnCount(self, parent=QModelIndex()):
return 1 + 1 + (1 if self.haveInitValue else 0) + 1
def data(self, index, role=Qt.DisplayRole):
if not index:
return None
row, column = index.row(), index.column()
if role in (Qt.DisplayRole, Qt.EditRole):
field = self.__row2field(row)
if field:
if self.__isColumn_name(column):
return field.name
if self.__isColumn_type(column):
return field.typeStr
if self.__isColumn_initValue(column):
return field.initValueStr
if self.__isColumn_comment(column):
return field.comment
assert(0)
elif role in {Qt.BackgroundRole,
Qt.ForegroundRole}:
field = self.__row2field(row)
if field:
if (self.__isRow_RETVAL(row) and self.__isColumn_name(column)) or\
(self.__isRow_TEMP(row) and self.__isColumn_initValue(column)):
if role == Qt.BackgroundRole:
return QBrush(QColor("#C0C0C0"))
return QBrush(QColor("black"))
if not field.isValid() and\
not self.__isColumn_comment(column):
if role == Qt.BackgroundRole:
return QBrush(QColor("red"))
return QBrush(QColor("black"))
else:
if role == Qt.BackgroundRole:
return QBrush(QColor("#E0E0E0"))
return QBrush(QColor("black"))
elif role in (Qt.ToolTipRole, Qt.WhatsThisRole):
if self.__isRow_newIN(row):
return "Create a new INPUT field here..."
elif self.__isRow_newOUT(row):
return "Create a new OUTPUT field here..."
elif self.__isRow_newINOUT(row):
return "Create a new IN_OUT field here..."
elif self.__isRow_newSTAT(row):
return "Create a new STAT field here..."
elif self.__isRow_newTEMP(row):
return "Create a new TEMP field here..."
else:
if self.__isColumn_name(column):
if self.__isRow_RETVAL(row):
return "Function (FC) return value."
else:
return "The interface field name."
elif self.__isColumn_type(column):
return "The interface field data type.\nFor example: BOOL or INT"
elif self.__isColumn_initValue(column):
return "The initial value in the associated DB."
elif self.__isColumn_comment(column):
return "Comment"
assert(0)
return None
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role != Qt.DisplayRole:
return None
if orientation == Qt.Horizontal:
column = section
if self.__isColumn_name(column):
return "Interface field name"
elif self.__isColumn_type(column):
return "Data type"
elif self.__isColumn_initValue(column):
return "Init value"
elif self.__isColumn_comment(column):
return "Comment"
assert(0)
else:
row = section
interf = self.interf
localRow = row
if self.__isRow_IN(row):
return "IN %d" % (localRow + 1)
if self.__isRow_newIN(row):
return "IN ..."
localRow -= self.__nrRows_IN
if self.__isRow_OUT(row):
return "OUT %d" % (localRow + 1)
if self.__isRow_newOUT(row):
return "OUT ..."
localRow -= self.__nrRows_OUT
if self.__isRow_INOUT(row):
return "IN_OUT %d" % (localRow + 1)
if self.__isRow_newINOUT(row):
return "IN_OUT ..."
localRow -= self.__nrRows_INOUT
if self.__isRow_STAT(row):
return "STAT %d" % (localRow + 1)
if self.__isRow_newSTAT(row):
return "STAT ..."
localRow -= self.__nrRows_STAT
if self.__isRow_TEMP(row):
return "TEMP %d" % (localRow + 1)
if self.__isRow_newTEMP(row):
return "TEMP ..."
if self.__isRow_RETVAL(row):
return "RET_VAL"
assert(0)
def setData(self, index, value, role=Qt.EditRole):
if not index:
return False
if role == Qt.EditRole:
row, column = index.row(), index.column()
# Handle NEW field creation
field = None
insertRow = self.__nrRows_IN
if self.__isRow_newIN(row):
field = AwlInterfFieldDef()
self.interf.inFields.append(field)
self.rowsInserted.emit(QModelIndex(),
insertRow - 1, insertRow - 1)
insertRow += self.__nrRows_OUT
if self.__isRow_newOUT(row):
field = AwlInterfFieldDef()
self.interf.outFields.append(field)
self.rowsInserted.emit(QModelIndex(),
insertRow - 1, insertRow - 1)
insertRow += self.__nrRows_INOUT
if self.__isRow_newINOUT(row):
field = AwlInterfFieldDef()
self.interf.inOutFields.append(field)
self.rowsInserted.emit(QModelIndex(),
insertRow - 1, insertRow - 1)
insertRow += self.__nrRows_STAT
if self.__isRow_newSTAT(row):
field = AwlInterfFieldDef()
self.interf.statFields.append(field)
self.rowsInserted.emit(QModelIndex(),
insertRow - 1, insertRow - 1)
insertRow += self.__nrRows_TEMP
if self.__isRow_newTEMP(row):
field = AwlInterfFieldDef()
self.interf.tempFields.append(field)
self.rowsInserted.emit(QModelIndex(),
insertRow - 1, insertRow - 1)
# Handle existing field modification
if not field:
field = self.__row2field(row)
assert(field)
# Set the field data
if self.__isColumn_name(column):
name = value.strip()
if name.startswith("#"):
# Permit the user to add the # prefix,
# but remove it automatically.
name = name[1:]
field.name = name
elif self.__isColumn_type(column):
# Try to parse the type and use the actual type name
# But only do this, if the type does not start with
# a space. This way the user can disable
# this automatic matching.
if not value.startswith(" "):
try:
dataType = AwlDataType.makeByName(value.split())
if dataType:
value = str(dataType)
except AwlSimError as e:
pass
field.typeStr = value
elif self.__isColumn_initValue(column):
field.initValueStr = value
elif self.__isColumn_comment(column):
field.comment = value
else:
assert(0)
self.contentChanged.emit()
return True
return False
def flags(self, index):
if not index:
return Qt.ItemIsEnabled
row, column = index.row(), index.column()
if (self.__isRow_RETVAL(row) and self.__isColumn_name(column)) or\
(self.__isRow_TEMP(row) and self.__isColumn_initValue(column)):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
|