summaryrefslogtreecommitdiff
path: root/src/kicadtoNgspice/Model.py
blob: 1270f0727caf3c41a346b12a8efd80366d5891c1 (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
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
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QObject, pyqtSlot
from . import TrackWidget
from xml.etree import ElementTree as ET
import os
import sys

class Model(QtWidgets.QWidget):
    """
    - This class creates Model Tab of KicadtoNgspice window.
      The widgets are created dynamically in the Model Tab.
    """
    #by Sumanto and Jay
    def addHex(self):
        """
        This function is use to keep track of all Device Model widget
        """
        #print("Calling Track Device Model Library funtion")
       

        init_path = '../../../'
        if os.name == 'nt':
            init_path = ''

        self.hexfile = QtCore.QDir.toNativeSeparators(
            QtWidgets.QFileDialog.getOpenFileName(
                self, "Open Hex Directory",
                init_path + "home", "*.hex"
            )[0]
        )
        self.text = open(self.hexfile).read()
        chosen_file_path = os.path.abspath(self.hexfile)
        print(os.path.abspath(self.hexfile))

    #By Sumanto and Jay
    def uploadHex(self):
        """
        This function is use to keep track of all Device Model widget
        """
        #print("Calling Track Device Model Library funtion")
        path1 = os.path.expanduser('~')
        path2 = '/ngspice-nghdl/src/xspice/icm/ghdl'
        init_path = path1 + path2
        if os.name == 'nt':
            init_path = ''

        self.hexloc = QtWidgets.QFileDialog.getExistingDirectory(
                self, "Open Hex Directory",
                init_path)
        print(self.hexloc)
        self.file = open(self.hexloc+"/hex.txt","w")
        self.file.write(self.text)
        self.file.close()
        
    def __init__(self, schematicInfo, modelList, clarg1):

        QtWidgets.QWidget.__init__(self)

        # Processing for getting previous values
        kicadFile = clarg1
        (projpath, filename) = os.path.split(kicadFile)
        project_name = os.path.basename(projpath)
        check = 1
        try:
            f = open(
                os.path.join(
                    projpath,
                    project_name +
                    "_Previous_Values.xml"),
                'r')
            tree = ET.parse(f)
            parent_root = tree.getroot()
            for child in parent_root:
                if child.tag == "model":
                    root = child

        except BaseException:
            check = 0
            print("Model Previous Values XML is Empty")

        # Creating track widget object
        self.obj_trac = TrackWidget.TrackWidget()

        # for increasing row and counting/tracking line edit widget
        self.nextrow = 0
        self.nextcount = 0

        # for storing line edit details position details
        self.start = 0
        self.end = 0
        self.entry_var = {}
        self.text = ""
        # Creating GUI dynamically for Model tab
        self.grid = QtWidgets.QGridLayout()
        self.setLayout(self.grid)

        for line in modelList:
            # print "ModelList Item:",line
            # Adding title label for model
            # Key: Tag name,Value:Entry widget number
            tag_dict = {}
            modelbox = QtWidgets.QGroupBox()
            modelgrid = QtWidgets.QGridLayout()
            modelbox.setTitle(line[5])
            self.start = self.nextcount
           
           
            # line[7] is parameter dictionary holding parameter tags.
            i = 0
            for key, value in line[7].items():
                # Check if value is iterable
                if not isinstance(value, str) and hasattr(value, '__iter__'):
                    # For tag having vector value
                    temp_tag = []
                    for item in value:
                        paramLabel = QtWidgets.QLabel(item)
                        modelgrid.addWidget(paramLabel, self.nextrow, 0)
                        self.obj_trac.model_entry_var[self.nextcount] = (
                            QtWidgets.QLineEdit()
                        )
                        modelgrid.addWidget(
                            self.obj_trac.model_entry_var
                            [self.nextcount], self.nextrow, 1)

                        try:
                            for child in root:
                                if child.text == line[2] \
                                        and child.tag == line[3]:
                                    self.obj_trac.model_entry_var
                                    [self.nextcount].setText(child[i].text)
                                    i = i + 1
                        except BaseException:
                            pass

                        temp_tag.append(self.nextcount)
                        self.nextcount = self.nextcount + 1
                        self.nextrow = self.nextrow + 1
                        if 'upload_hex_file:1' in tag_dict:
                            self.addbtn = QtWidgets.QPushButton("Add Hex File")
                            self.addbtn.setObjectName("%d" % self.nextcount)
                            self.addbtn.clicked.connect(self.addHex)
                            modelgrid.addWidget(self.addbtn, self.nextrow, 2)
                            modelbox.setLayout(modelgrid)

                            # CSS
                            modelbox.setStyleSheet(" \
                            QGroupBox { border: 1px solid gray; border-radius:\
                            9px; margin-top: 0.5em; } \
                            QGroupBox::title { subcontrol-origin: margin; left:\
                            10px; padding: 0 3px 0 3px; } \
                            ")

                            self.grid.addWidget(modelbox)
                            self.addbtn = QtWidgets.QPushButton("Upload Hex File")
                            self.addbtn.setObjectName("%d" % self.nextcount)
                            self.addbtn.clicked.connect(self.uploadHex)
                            modelgrid.addWidget(self.addbtn, self.nextrow, 3)
                            modelbox.setLayout(modelgrid)

                            # CSS
                            modelbox.setStyleSheet(" \
                            QGroupBox { border: 1px solid gray; border-radius:\
                            9px; margin-top: 0.5em; } \
                            QGroupBox::title { subcontrol-origin: margin; left:\
                            10px; padding: 0 3px 0 3px; } \
                            ")

                            self.grid.addWidget(modelbox)

                    tag_dict[key] = temp_tag

                else:
                    paramLabel = QtWidgets.QLabel(value)
                    modelgrid.addWidget(paramLabel, self.nextrow, 0)
                    self.obj_trac.model_entry_var[self.nextcount] = (
                        QtWidgets.QLineEdit()
                    )
                    modelgrid.addWidget(
                        self.obj_trac.model_entry_var[self.nextcount],
                        self.nextrow, 1
                    )
                 

                    try:
                        for child in root:
                            if child.text == line[2] and child.tag == line[3]:
                                self.obj_trac.model_entry_var[self.nextcount] \
                                                .setText(child[i].text)
                                i = i + 1
                    except BaseException:
                        pass
                   
                   
                    tag_dict[key] = self.nextcount
                    self.nextcount = self.nextcount + 1
                    self.nextrow = self.nextrow + 1
                    if 'upload_hex_file:1' in tag_dict:
                        self.addbtn = QtWidgets.QPushButton("Add Hex File")
                        self.addbtn.setObjectName("%d" % self.nextcount)
                        self.addbtn.clicked.connect(self.addHex)
                        modelgrid.addWidget(self.addbtn, self.nextrow, 2)
                        modelbox.setLayout(modelgrid)

                        # CSS
                        modelbox.setStyleSheet(" \
                        QGroupBox { border: 1px solid gray; border-radius:\
                        9px; margin-top: 0.5em; } \
                        QGroupBox::title { subcontrol-origin: margin; left:\
                        10px; padding: 0 3px 0 3px; } \
                        ")

                        self.grid.addWidget(modelbox)
                        self.addbtn = QtWidgets.QPushButton("Upload Hex File")
                        self.addbtn.setObjectName("%d" % self.nextcount)
                        self.addbtn.clicked.connect(self.uploadHex)
                        modelgrid.addWidget(self.addbtn, self.nextrow, 3)
                        modelbox.setLayout(modelgrid)

                        # CSS
                        modelbox.setStyleSheet(" \
                        QGroupBox { border: 1px solid gray; border-radius:\
                        9px; margin-top: 0.5em; } \
                        QGroupBox::title { subcontrol-origin: margin; left:\
                        10px; padding: 0 3px 0 3px; } \
                        ")

                        self.grid.addWidget(modelbox)

            self.end = self.nextcount - 1
            modelbox.setLayout(modelgrid)

            # CSS
            modelbox.setStyleSheet(" \
            QGroupBox { border: 1px solid gray; border-radius: \
            9px; margin-top: 0.5em; } \
            QGroupBox::title { subcontrol-origin: margin; left:\
             10px; padding: 0 3px 0 3px; } \
            ")

            self.grid.addWidget(modelbox)

            '''
            Listing all
            line[0] = index
            line[1] = compLine
            line[2] = modelname  #Change from compType to modelname
            line[3] = compName
            line[4] = comment
            line[5] = title
            line[6] = type i.e analog or digital
            Now adding start,end and tag_dict which will be line[7],
            line[8] and line[9] respectively
            '''

            # This keeps the track of Model Tab Widget
            lst = [
                line[0],
                line[1],
                line[2],
                line[3],
                line[4],
                line[5],
                line[6],
                self.start,
                self.end,
                tag_dict]
            check = 0
            for itr in self.obj_trac.modelTrack:
                if itr == lst:
                    check = 1
            if check == 0:
                self.obj_trac.modelTrack.append(lst)

        self.show()