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
###############################################################################
# Copyright (c) 2008-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @plugin
#       Edit And Show Comments
#   @namespace
#       Comments
#   @section
#       Miscellaneous Userware Examples
#   @description
#       This plugin extends the Popup menu to add comments to the selected
#       object.
#
#       From the extended main menu a report can be displayed or saved as a
#       text file.
#
#       Also, all objects with a comment can be loaded to the Mem window.
#
#       The plugin also lets the user toggle the display of comments at nets
#       in the schematic view.
#   @files
#       comments.tcl
#   @example
#       demo/rtl/aquarius/aquarius.f
#   @cmdline
#       -F @example[0]
#       -userware @files[0]
#   @tag
#       zdb gui
###############################################################################


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc Comments:Init {} {
    global Comments

    set Comments(netComments) 1
    set Comments(attribute)   @comment

    gui menu submenu {"Userware" "Comments"}
    gui menu command \
        {"Userware" "Comments" "Show Report"} \
        Comments:_showReport
    gui menu command \
        {"Userware" "Comments" "Save Report"} \
        Comments:_saveReport
    gui menu command \
        {"Userware" "Comments" "Load Objects With Comments Into Mem Window"} \
        Comments:_loadMem
    gui menu checkbutton \
        {"Userware" "Comments" "Hover Comments"} \
        [list gui settings changed] \
        [gui settings variable "nlv:hoverComments"]
    gui menu checkbutton \
        {"Userware" "Comments" "Show Comments At Nets"} \
        [list Comments:_toggleNetComments] \
        Comments(netComments)

    gui database runOrRegisterChangedCallback [list Comments:_toggleNetComments]

    gui popup append "Edit Comment" "Comments:_editComment"
}


# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc Comments:Finit {} {
    gui menu removeEntry {"Userware" "Comments"}

    gui database removeChangedCallback Comments:_toggleNetComments

    gui popup remove "Edit Comment"
}


# -----------------------------------------------------------------------------
# _editComment - Show add comment dialog window.
# -----------------------------------------------------------------------------
#
proc Comments:_editComment {oidList} {
    global Comments

    if {$oidList == {}} {
        return
    }

    set db [gui database get]
    if {$db == {}} {
        return
    }

    ##
    # Only one selected object is allowed.
    #
    if {[llength $oidList] > 1} {
        zmessage print ERR "Too many selected objects (only one is used)"
    }
    set oid [lindex $oidList 0]

    set w .showComments_addComment
    if {[winfo exists $w]} {
        destroy $w
    }
    toplevel $w

    ttk::label $w.header \
        -text "Comment for $oid:"

    ttk::frame $w.f
    set text $w.f.t
    text $text \
        -font TkFixedFont \
        -wrap none \
        -xscrollcommand "$w.f.x set" \
        -yscrollcommand "$w.f.y set"
    ttk::scrollbar $w.f.x \
        -orient horizontal \
        -command "$text xview"
    ttk::scrollbar $w.f.y \
        -orient vertical \
        -command "$text yview"
    grid $text  -row 0 -column 0 -sticky news
    grid $w.f.y -row 0 -column 1 -sticky ns
    grid $w.f.x -row 1 -column 0 -sticky we
    grid columnconfigure $w.f 0 -weight 1
    grid rowconfigure    $w.f 0 -weight 1
    $text insert 1.0 [$db flatattr $oid getValue $Comments(attribute)]

    ttk::frame $w.buttons
    ttk::button $w.buttons.cancel \
        -text "Cancel"
    ttk::button $w.buttons.ok \
        -text "OK"
    pack $w.buttons.ok     -side right -padx 5 -pady 2
    pack $w.buttons.cancel -side right -padx 5 -pady 2

    grid $w.header  -row 0 -column 0 -sticky news -padx 4 -pady 4
    grid $w.f       -row 1 -column 0 -sticky news -padx 4 -pady 4
    grid $w.buttons -row 2 -column 0 -sticky news -padx 4 -pady 4
    grid columnconfigure $w 0 -weight 1
    grid rowconfigure    $w 1 -weight 1

    set button [gui window dialog \
        $w \
        "Edit Comment"\
        -place "MOUSE" \
        -focus $text \
        -buttons [list $w.buttons.cancel $w.buttons.ok] \
    ]

    if {$button == 1} {
        set comment [$text get 1.0 end]
        set comment [string trimright $comment \n]
        $db flatattr $oid set $Comments(attribute)=$comment
        gui attribute changed
    }

    destroy $w
}


# -----------------------------------------------------------------------------
# _generateReport - Generate a report of all objects with comments.
# -----------------------------------------------------------------------------
#
proc Comments:_generateReport {} {
    set oidComments [Comments:_getOidComments]
    if {$oidComments == {}} {
        zmessage print WAR "No objects with comments."
        return {}
    }

    set length1 6
    set length2 7
    foreach {oid comment} $oidComments {
        set l [string length $oid]
        if {$l > $length1} {
            set length1 $l
        }
        set cList [split $comment \n]
        foreach c $cList {
            set l [string length $c]
            if {$l > $length2} {
                set length2 $l
            }
        }
    }
    set sep [string repeat " " $length1]
    set report [format "| %-*s | %-*s |" $length1 "Object" $length2 "Comment"]
    append report \n
    append report +-[string repeat - $length1]-+-[string repeat - $length2]-+\n
    foreach {oid comment} $oidComments {
        set c [format "| %-*s | " $length1 $oid]
        append report $c
        set i 0
        foreach c [split $comment \n] {
            if {[incr i] == 1} {
                append report [format "%-*s |\n" $length2 $c]
            } else {
                append report [format "| %s | %-*s |\n" $sep $length2 $c]
            }
        }
    }
    return $report
}


# -----------------------------------------------------------------------------
# _showReport - Show a window to display the comment report.
# -----------------------------------------------------------------------------
#
proc Comments:_showReport {} {
    set w .showComments_report
    if {[winfo exists $w]} {
        destroy $w
    }

    set report [Comments:_generateReport]
    if {$report == {}} {
        return
    }

    toplevel $w

    ttk::frame $w.f
    set text $w.f.t
    text $text \
        -font TkFixedFont \
        -wrap none \
        -cursor {} \
        -xscrollcommand "$w.f.x set" \
        -yscrollcommand "$w.f.y set"
    ttk::scrollbar $w.f.x \
        -orient horizontal \
        -command "$text xview"
    ttk::scrollbar $w.f.y \
        -orient vertical \
        -command "$text yview"
    grid $text  -row 0 -column 0 -sticky news
    grid $w.f.y -row 0 -column 1 -sticky ns
    grid $w.f.x -row 1 -column 0 -sticky we
    grid rowconfigure    $w.f 0 -weight 1
    grid columnconfigure $w.f 0 -weight 1
    $text insert 1.0 $report
    $text configure -state disabled

    ttk::frame $w.buttons
    ttk::button $w.buttons.save \
        -text "Save"
    ttk::button $w.buttons.close \
        -text "Close"
    pack $w.buttons.close -side right -padx 5 -pady 2
    pack $w.buttons.save  -side right -padx 5 -pady 2

    grid $w.f       -row 0 -column 0 -sticky news -padx 4 -pady 4
    grid $w.buttons -row 1 -column 0 -sticky news -padx 4 -pady 4
    grid rowconfigure    $w 0 -weight 1
    grid columnconfigure $w 0 -weight 1

    set button [gui window dialog \
        $w \
        "Comments" \
        -place "MOUSE" \
        -buttons [list $w.buttons.close $w.buttons.save] \
    ]

    destroy $w

    if {$button == 1} {
        Comments:_saveReport
    }
}


# -----------------------------------------------------------------------------
# _saveReport - Save the comment report to a text file.
# -----------------------------------------------------------------------------
#
proc Comments:_saveReport {} {
    set f [gui window fileDialog saveFile "Save Report" {{"Text Files" .txt}}]
    if {$f == ""} {
        return
    }

    set   out  [open $f w]
    puts  $out [Comments:_generateReport]
    close $out
}


# -----------------------------------------------------------------------------
# _loadMem - Load all annotated object into the Mem Window.
# -----------------------------------------------------------------------------
#
proc Comments:_loadMem {} {
    set oids {}
    foreach {oid comment} [Comments:_getOidComments] {
        lappend oids $oid
    }

    gui window show Mem
    gui mem append $oids
}


# -----------------------------------------------------------------------------
# _getOidComments - Get a list of pairs of oids with their comments.
# -----------------------------------------------------------------------------
#
proc Comments:_getOidComments {} {
    global Comments

    set db [gui database get]
    if {$db == {}} {
        return {}
    }

    set result {}
    $db foreach top top {
        $db flat foreach obj $top oid {
            set comment [$db flatattr $oid getValue $Comments(attribute)]
            if {$comment == ""} {
                continue
            }
            lappend result $oid $comment
        }
    }
    return $result
}


# -----------------------------------------------------------------------------
# _toggleHoverComments - Toggle hovering of comment graphic comments.
# -----------------------------------------------------------------------------
#
proc Comments:_toggleHoverComments {} {
    gui settings changed
}


# -----------------------------------------------------------------------------
# _toggleNetComments - Toggle display of comments at nets.
# -----------------------------------------------------------------------------
#
proc Comments:_toggleNetComments {{db {}}} {
    global Comments

    ##
    # Return if the database is empty.
    #
    if {$db == {}} {
        set db [gui database get]
        if {$db == {}} {
            return
        }
    }

    if {$Comments(netComments)} {
        if {(![gui settings get "nlv:netattrwire"]) ||
              [gui settings get "nlv:netattrpin"]}   \
        {
            gui settings set "nlv:netattrwire" 1
            gui settings set "nlv:netattrpin"  0
            gui settings changed
        }
    }

    ##
    # Set the value for the comment display attribute.
    #
    set commentAttr "\n%$Comments(attribute)"

    set currentAttrValue [$db attr -db getValue "@nlv:net"]

    if {$Comments(netComments)} {
        set newAttrValue $currentAttrValue
        if {![string match "*$commentAttr*" $currentAttrValue]} {
            append newAttrValue $commentAttr
        }
    } else {
        set newAttrValue [string map [list $commentAttr ""] $currentAttrValue]
    }

    if {$newAttrValue != $currentAttrValue} {
        $db attr -db set "@nlv:net=$newAttrValue"

        ##
        # Inform the GUI that the attributes has changed.
        #
        gui attribute changed
    }
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
Comments:Init