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 | ###############################################################################
# Copyright (c) 2014-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
# Annotate Cone
# @namespace
# Annotate
# @section
# Miscellaneous Userware Examples
# @description
# This is a prototype implementation of a Cone window annotation feature.
# To access this feature the main menu is extended by an `Annotate Cone`
# button.
#
# The `Start Annotation` menu entry will activate the annotation mode and
# disables the normal mouse actions like zoom, double-click, etc.
# Now simple elements like rectangles, circles, lines and text boxes can
# be added on top of the schematic.
#
# Annotation items can be moved or deleted using the corresponding menu
# entry. The `Clear Annotation` menu entry will remove all created
# annotation objects. The `End Annotation` entry will switch back to
# normal mode an re-install all normal mouse bindings. This will also
# remove all created annotation objects.
#
# @files
# commentGraphic.tcl
# @example
# demo/rtl/aquarius/aquarius.f
# @cmdline
# -F @example[0]
# -userware @files[0]
# @tag
# gui
###############################################################################
# =============================================================================
# Initialize the Annotate array variables.
# =============================================================================
#
set Annotate(linewidth) 3
set Annotate(linecolor) 3
set Annotate(linecolorValue) "#FF0000"
set Annotate(textcolor) 4
set Annotate(textcolorValue) "#000000"
set Annotate(fillcolor) 5
set Annotate(fillcolorValue) "#DDDDDD"
# =============================================================================
# Init -
# =============================================================================
#
proc Annotate:Init {} {
global Annotate
set Annotate(selectedCone) [gui window defaultClassWindow Cone]
gui menu command \
{"Annotate Cone" "Start Annotation"} \
[list Annotate:_start]
gui menu submenu {"Annotate Cone" "Select Cone Window"}
gui menu customizeEntry {"Annotate Cone" "Select Cone Window"} \
Annotate:_fillSelectCone
gui menu command \
{"Annotate Cone" "End Annotation"} \
[list Annotate:_end]
gui menu command \
{"Annotate Cone" "Clear Annotation"} \
[list Annotate:_clear]
gui menu separator {"Annotate Cone"}
gui menu command \
{"Annotate Cone" "Draw Rectangle"} \
[list Annotate:_do Rectangle]
gui menu command \
{"Annotate Cone" "Draw Circle"} \
[list Annotate:_do Circle]
gui menu command \
{"Annotate Cone" "Draw Line"} \
[list Annotate:_do Line]
gui menu command \
{"Annotate Cone" "Add Text"} \
[list Annotate:_textStart]
gui menu separator {"Annotate Cone"}
gui menu command \
{"Annotate Cone" "Move Object"} \
[list Annotate:_move]
gui menu command \
{"Annotate Cone" "Delete Annotation Object"} \
[list Annotate:_delete]
gui menu customizeEntry \
{"Annotate Cone" "Start Annotation"} \
Annotate:_initMenuState
set menuCb {Annotate:_changeMenuState}
gui menu customizeEntry {"Annotate Cone" "End Annotation"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Clear Annotation"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Draw Rectangle"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Draw Circle"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Draw Line"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Add Text"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Move Object"} $menuCb
gui menu customizeEntry {"Annotate Cone" "Delete Annotation Object"} $menuCb
set Annotate(menuState) "disabled"
##
# Init variables.
#
set Annotate(bindings) {}
set Annotate(action) {}
}
# =============================================================================
# Finit - Remove all menu entries.
# =============================================================================
#
proc Annotate:Finit {} {
global Annotate
gui menu removeEntry {"Annotate Cone"}
if {$Annotate(menuState) eq "normal"} {
Annotate:_end
}
array unset Annotate
}
# -----------------------------------------------------------------------------
# _start - Start annotating the Cone window.
# -----------------------------------------------------------------------------
#
proc Annotate:_start {} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
if {[$w page -nopageok] == 0} {
zmessage print WAR "Cone not initialized."
return
}
set Annotate(menuState) "normal"
$w property boxcolor$Annotate(linecolor) $Annotate(linecolorValue)
$w property boxcolor$Annotate(textcolor) $Annotate(textcolorValue)
$w property fillcolor$Annotate(fillcolor) $Annotate(fillcolorValue)
##
# Save bindings and selected settings.
#
set Annotate(bindings) [$w bind]
set Annotate(action) [$w cget -bindcallback]
set Annotate(hoverComments) [::Settings::Get "nlv:hoverComments"]
::Settings::Set "nlv:hoverComments" 0
$w bind -delete_all
}
# -----------------------------------------------------------------------------
# _end - End annotating the Cone window.
# -----------------------------------------------------------------------------
#
proc Annotate:_end {} {
global Annotate
if {$Annotate(menuState) ne "normal"} {
return
}
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
set Annotate(menuState) "disabled"
Annotate:_clear
##
# Restore bindings
#
$w bind -delete_all
foreach binding $Annotate(bindings) {
eval {$w} bind $binding
}
$w configure -bindcallback $Annotate(action)
##
# Restore settings.
#
::Settings::Set "nlv:hoverComments" $Annotate(hoverComments)
}
# -----------------------------------------------------------------------------
# _clear - Clear all created annotations.
# -----------------------------------------------------------------------------
#
proc Annotate:_clear {} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
foreach obj [$w search cgraphic *] {
Annotate:_unload $w $obj
}
}
# -----------------------------------------------------------------------------
# _fillSelectCone -
# -----------------------------------------------------------------------------
#
proc Annotate:_fillSelectCone {menuId} {
global Annotate
set subMenu [$menuId entrycget end -menu]
::WindowManager::Foreach Cone w {
$subMenu add radiobutton \
-label [gui window name $w] \
-variable Annotate(selectedCone) \
-value $w
}
}
# -----------------------------------------------------------------------------
# _changeMenuState -
# -----------------------------------------------------------------------------
#
proc Annotate:_changeMenuState {menuId} {
global Annotate
$menuId entryconfigure end -state $Annotate(menuState)
}
# -----------------------------------------------------------------------------
# _initMenuState -
# -----------------------------------------------------------------------------
#
proc Annotate:_initMenuState {menuId} {
global Annotate
if {$Annotate(menuState) == "normal"} {
set startState "disabled"
} else {
set startState "normal"
}
$menuId entryconfigure end -state $startState
}
# -----------------------------------------------------------------------------
# _do -
# -----------------------------------------------------------------------------
#
proc Annotate:_do {what} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
$w bind -delete_all
switch -- $what {
"Rectangle" {
set echoText "Draw Rectangle"
set echoType rectangle
set cbAction _drawRectangle
}
"Circle" {
set echoText "Draw Circle (diameter)"
set echoType line
set cbAction _drawCircle
}
"Line" {
set echoText "Draw Line"
set echoType line
set cbAction _drawLine
}
default {}
}
$w configure -bindcallback [list Annotate:$cbAction %W %x %y %X %Y]
$w bind stroke-1 -action callback -echo_type $echoType -echo_text $echoText
}
# -----------------------------------------------------------------------------
# _convertXCoord - Convert window X coordinates into database coordinates.
# -----------------------------------------------------------------------------
#
proc Annotate:_convertXCoord {w x} {
return [lindex [$w convertcoords $x 0] 0]
}
# -----------------------------------------------------------------------------
# _convertYCoord -Convert window Y coordinates into database coordinates.
# -----------------------------------------------------------------------------
#
proc Annotate:_convertYCoord {w y} {
return [lindex [$w convertcoords 0 $y] 1]
}
# -----------------------------------------------------------------------------
# _drawRectangle - Draw a rectangle.
# -----------------------------------------------------------------------------
#
proc Annotate:_drawRectangle {w x y X Y} {
global Annotate
set x [Annotate:_convertXCoord $w $x]
set y [Annotate:_convertYCoord $w $y]
set X [Annotate:_convertXCoord $w $X]
set Y [Annotate:_convertYCoord $w $Y]
set originX $x
set originY $y
set x [expr {$X - $x}]
set y [expr {$Y - $y}]
$w load cgraphic [Annotate:_getObjId] linkto [$w page] \
place abs $originX $originY \
linewidth $Annotate(linewidth) \
linecolor $Annotate(linecolor) \
path 0 0 $x 0 $x $y 0 $y 0 0
}
# -----------------------------------------------------------------------------
# _drawCircle - Draw a circle.
# -----------------------------------------------------------------------------
#
proc Annotate:_drawCircle {w x y X Y} {
global Annotate
set x [Annotate:_convertXCoord $w $x]
set y [Annotate:_convertYCoord $w $y]
set X [Annotate:_convertXCoord $w $X]
set Y [Annotate:_convertYCoord $w $Y]
set originX $x
set originY $y
set x [expr {$X - $x}]
set y [expr {$Y - $y}]
$w load cgraphic [Annotate:_getObjId] linkto [$w page] \
place abs $originX $originY \
linewidth $Annotate(linewidth) \
linecolor $Annotate(linecolor) \
path 0 0 ${x}a ${y}a 0 0
}
# -----------------------------------------------------------------------------
# _drawLine - Draw a line.
# -----------------------------------------------------------------------------
#
proc Annotate:_drawLine {w x y X Y} {
global Annotate
set x [Annotate:_convertXCoord $w $x]
set y [Annotate:_convertYCoord $w $y]
set X [Annotate:_convertXCoord $w $X]
set Y [Annotate:_convertYCoord $w $Y]
set originX $x
set originY $y
set x [expr {$X - $x}]
set y [expr {$Y - $y}]
$w load cgraphic [Annotate:_getObjId] linkto [$w page] \
place abs $originX $originY \
linewidth $Annotate(linewidth) \
linecolor $Annotate(linecolor) \
path 0 0 $x $y
}
# -----------------------------------------------------------------------------
# _textStart - Start creating a text.
# -----------------------------------------------------------------------------
#
proc Annotate:_textStart {} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
$w bind -delete_all
$w configure -bindcallback [list Annotate:_showTextBox %W %x %y]
$w bind click-1 -action callback
}
# -----------------------------------------------------------------------------
# _showTextBox - Show a text widget to enter the text.
# -----------------------------------------------------------------------------
#
proc Annotate:_showTextBox {w x y} {
destroy $w.cgraphic
ttk::frame $w.cgraphic
text $w.cgraphic.text -width 20 -height 5
ttk::button $w.cgraphic.ok -text OK \
-command [list Annotate:_drawText $w $x $y]
grid $w.cgraphic.text -row 0 -column 0 -sticky news
grid $w.cgraphic.ok -row 1 -column 0 -sticky we
grid columnconfigure $w.cgraphic 0 -weight 1
grid rowconfigure $w.cgraphic 0 -weight 1
grid rowconfigure $w.cgraphic 1 -weight 1
place $w.cgraphic -x $x -y $y
focus $w.cgraphic.text
}
# -----------------------------------------------------------------------------
# _drawText - Draw a text box.
# -----------------------------------------------------------------------------
#
proc Annotate:_drawText {w x y} {
global Annotate
set textValue [$w.cgraphic.text get 1.0 end]
destroy $w.cgraphic
if {$textValue == ""} {
return
}
set originX [Annotate:_convertXCoord $w $x]
set originY [Annotate:_convertYCoord $w $y]
$w load cgraphic [Annotate:_getObjId] linkto [$w page] \
place abs $originX $originY \
linewidth $Annotate(linewidth) \
linecolor $Annotate(linecolor) \
textcolor $Annotate(textcolor) \
fillcolor $Annotate(fillcolor) \
autofpath 2 \
text $textValue -ul 0 0 12
}
# -----------------------------------------------------------------------------
# _move - Move a created annotation object.
# -----------------------------------------------------------------------------
#
proc Annotate:_move {} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
$w bind -delete_all
$w bind stroke-1 -action instdrag -echo_type instdrag -autoscroll
}
# -----------------------------------------------------------------------------
# _delete - Delete an object in the cone.
# -----------------------------------------------------------------------------
#
proc Annotate:_delete {} {
global Annotate
set cone $Annotate(selectedCone)
set w [gui cone nlv -window $cone]
$w bind -delete_all
$w configure -bindcallback [list Annotate:_unload %W %o]
$w bind click-1 -action callback
}
# -----------------------------------------------------------------------------
# _unload - Unload a cgraphic object.
# -----------------------------------------------------------------------------
#
proc Annotate:_unload {w obj} {
if {![string match "*cgraphic*" $obj]} {
return
}
if {[string match "comment_*" [lindex $obj 1]]} {
return
}
$w unload [lindex $obj end-1] [lindex $obj end]
}
# -----------------------------------------------------------------------------
# _getObjId - Get a new unique number used as the object identification.
# -----------------------------------------------------------------------------
#
proc Annotate:_getObjId {} {
global Annotate
if {![info exists Annotate(id)]} {
set Annotate(id) 0
} else {
incr Annotate(id)
}
return "cgraphic_$Annotate(id)"
}
# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
Annotate:Init
|