summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/dialogs/codeeditor.cpp
blob: 33fb743b58312782ef7102ce3a40acf3c615b8d5 (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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231

/***************************************************************************
                          codeviewerdialog.cpp  -  description
                             -------------------
    begin                : Fri Aug 1 2003
    copyright            : (C) 2003 by Brian Thomas
    email                : brian.thomas@gsfc.nasa.gov
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   copyright (C) 2004-2007                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

// own header
#include "codeeditor.h"

// qt/kde includes
#include <tqkeysequence.h>
#include <tqcursor.h>
#include <tqcolor.h>
#include <tqlabel.h>
#include <tqbrush.h>
#include <tqlayout.h>
#include <tqregexp.h>
#include <kdebug.h>
#include <klocale.h>

// local includes
#include "../attribute.h"
#include "../classifier.h"
#include "../umldoc.h"
#include "../umlrole.h"

#include "../codeaccessormethod.h"
#include "../codegenerator.h"
#include "../codeclassfield.h"
#include "../codeclassfielddeclarationblock.h"
#include "../codedocument.h"
#include "../codeoperation.h"
#include "../codemethodblock.h"
#include "../classifiercodedocument.h"
#include "../ownedhierarchicalcodeblock.h"
#include "../codegenerators/codegenfactory.h"

#include "codeviewerdialog.h"
#include "classpropdlg.h"
#include "umlattributedialog.h"
#include "umlroledialog.h"
#include "umloperationdialog.h"

CodeEditor::CodeEditor ( const TQString & text, const TQString & context, CodeViewerDialog * parent, const char * name , CodeDocument * doc)
        : TQTextEdit ( text, context, parent, name)
{
    init(parent, doc);
}

CodeEditor::CodeEditor ( CodeViewerDialog * parent, const char* name, CodeDocument * doc )
        : TQTextEdit ( parent, name )
{
    init(parent, doc);
}

/*
 *  Destroys the object and frees any allocated resources
 */
CodeEditor::~CodeEditor() { }

// clear the display of all text
void CodeEditor::clearText () {

    //        setCaption( tr2i18n("") );
    m_selectedTextBlock = 0;
    m_textBlockList.clear();
    m_tbInfoMap->clear();

    // now call super-class
    clear();

}

Settings::CodeViewerState CodeEditor::getState()
{
    return m_parentDlg->getState();
}

TQLabel * CodeEditor::getComponentLabel() {
    return m_parentDlg->componentLabel;
}


// FIX: used only for debugging right now.. eliminate eventually -b.t.
void CodeEditor::clicked(int para, int pos)
{
    getComponentLabel()->setText("para:"+TQString::number(para)+" pos:"+TQString::number(pos));
}


bool CodeEditor::close ( bool alsoDelete )
{

    // capture last code block, if it exists
    if(m_lastTextBlockToBeEdited)
    {
        updateTextBlockFromText (m_lastTextBlockToBeEdited);
        m_lastTextBlockToBeEdited = 0;
    }

    return TQTextEdit::close(alsoDelete);

}

void CodeEditor::doubleClicked(int para, int pos)
{

    m_lastPara = para;
    m_lastPos = pos;

    // ugh. more ugliness. We want to be able to call up the
    // correct editing dialog for the given attribute.
    TextBlock * tBlock = m_textBlockList.at(para);
    editTextBlock(tBlock, para);

}

// allow us to edit, as appropriate, the parent UMLObject of the
// given text block.
void CodeEditor::editTextBlock(TextBlock * tBlock, int para) {

    if(tBlock)
    {
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        if(info) {
            UMLObject *obj = info->getParent();
            if(obj)
            {

                UMLAttribute * at = 0;
                UMLRole * role = 0;
                UMLOperation * op = 0;

                if( (at = dynamic_cast<UMLAttribute*>(obj)) )
                {
                    UMLAttributeDialog dlg( this, at);
                    if( dlg.exec() ) { rebuildView(para); }
                }
                else if( (dynamic_cast<UMLClassifier*>(obj)) )
                {
                    if (obj->showProperties())
                        rebuildView(para);
                }
                else if( (role = dynamic_cast<UMLRole*>(obj)))
                {
                    UMLRoleDialog dlg(this,role);
                    if( dlg.exec() ) { rebuildView(para); }
                }
                else if( (op = dynamic_cast<UMLOperation*>(obj)) )
                    //else if( (cop = dynamic_cast<CodeOperation*>(tBlock)) )
                {
                    UMLOperationDialog dlg(this,op);
                    if( dlg.exec() ) { rebuildView(para); }
                }
                else
                {
                    kError()<<" CodeViewerDlg ERROR: UNKNOWN parent for textBlock"<<endl;
                }

            }
        }
    }
}

// return whether is empty or just whitespace
bool CodeEditor::StringIsBlank(const TQString &str)
{
    if(str.isEmpty() || str.stripWhiteSpace().isEmpty())
        return true;
    return false;
}

void CodeEditor::keyPressEvent ( TQKeyEvent * e ) {

    // kDebug() <<"KEY PRESS EVENT:["<<e->text().latin1()<<"] ascii CODE:"<<e->ascii();

    if((e->ascii() == 8) ) // || (e->ascii() == 127)) // what about delete?
        m_backspacePressed = true;

    // Q: can the MAC or WIN/DOS sequences occur?
    if((e->ascii() == 10) || (e->ascii() == 13) || (e->text() == "\r\n"))
        m_newLinePressed = true;

    TQTextEdit::keyPressEvent(e);
}

void CodeEditor::loadFromDocument ()
{

    // clear the tool
    clearText();

    // set caption on tool
    TQString caption = m_parentDoc->getFileName() + m_parentDoc->getFileExtension();
    setCaption( tr2i18n( caption.latin1() ) );

    // header for document
    TQString header = m_parentDoc->getHeader()->toString();
    TQString componentName = TQString("header for file ") +caption;
    if(!StringIsBlank(header))
        insert(header,m_parentDoc->getHeader(),false,getState().fontColor,
               getState().nonEditBlockColor,0,componentName);

    // now all the text blocks in the document
    TextBlockList * items = m_parentDoc->getTextBlockList();
    appendText(items);

    setCursorPosition(0,0);

}

void CodeEditor::insert (const TQString & text, TextBlock * parent, bool editable, const TQColor & fgcolor, const TQColor & bgcolor, UMLObject * umlobj, const TQString & displayName, int startLine)
{

    // set some params
    bool isInsert = false;
    setColor(fgcolor);

    // its an append op if startLine is -1, otherwise its
    // an actual insert, which is more complicated
    if(startLine == -1)
    {
        startLine = paragraphs()-1;
        TQTextEdit::append(text); // put actual text in. Use insert instead of append so history is preserved?
    }
    else
    {
        isInsert = true;
        TQTextEdit::insertAt(text, startLine, 0);
    }

    // actual put in text

    // now do 'paragraph' background highlighting
    //        int endLine = paragraphs()-2;
    int endLine = text.contains(TQRegExp("\n")) + startLine -1;
    if(m_isHighlighted)
        for(int para=startLine;para<=endLine;para++)
            setParagraphBackgroundColor(para,bgcolor);

    // record paragraph information
    // Did we already start recording info for this parent object?
    TextBlockInfo * tbinfo;
    if(m_tbInfoMap->contains(parent))
        tbinfo = (*m_tbInfoMap)[parent];
    else {
        tbinfo = new TextBlockInfo();
        tbinfo->displayName = displayName;
        tbinfo->isCodeAccessorMethod = dynamic_cast<CodeAccessorMethod*>(parent) ? true : false;
        m_tbInfoMap->insert(parent,tbinfo);
    }

    // set a parent, if its not already set
    if(umlobj && !tbinfo->getParent())
    {
        tbinfo->displayName = displayName;
        tbinfo->setParent(umlobj);
        tbinfo->isClickable = textBlockIsClickable(umlobj);
    }

    // now mark all lines that we just inserted as belonging to the parent
    for(int para=startLine;para<=endLine;para++)
        m_textBlockList.insert(para,parent);

    // lastly, update the para info
    // start position is relative to the FIRST parent position
    int start = startLine - m_textBlockList.findRef(parent);
    int size = endLine-startLine;

    // create the object that records this particular "paragraph"
    ParaInfo * item = new ParaInfo();
    item->start = start;
    item->size= size;
    item->fgcolor = fgcolor;
    item->bgcolor = bgcolor;
    item->isEditable = editable;

    if(isInsert)
    {
        // now we have to fix the 'start' value for all the para
        // info blocks that coorspond to textblocks that we inserted
        // inside of. This means parent tblock paragraph locations
        // that are greater than zero in that type of textblock

        int increase = size + 1;
        TQMap<TextBlock*,TextBlockInfo*>::Iterator it;
        for ( it = m_tbInfoMap->begin(); it != m_tbInfoMap->end(); ++it )
        {
            TextBlock * tblock = it.key();
            TextBlockInfo * thisTbInfo = it.data();
            int firstLoc = m_textBlockList.findRef(tblock);
            ParaInfo * lastPi = thisTbInfo->m_paraList.last();

            for(ParaInfo * pi = thisTbInfo->m_paraList.first(); pi; pi = thisTbInfo->m_paraList.next())
            {
                int minPara = pi->start+firstLoc;

                // only worth doing if in range of the whole
                // representation
                if(!pi->start && (startLine > (lastPi->start+firstLoc+lastPi->size) || endLine < minPara) )
                    break;

                // now, only for those paraInfo blocks which
                // have exceeded our last line, we increase them
                if(pi->start && minPara >= endLine )
                    pi->start += increase;

            }
        }

    }

    tbinfo->m_paraList.append(item);

}

void CodeEditor::appendText(TextBlockList * items)
{

    for (TextBlock *tb = items->first(); tb; tb = items->next())
    {
        // types of things we may cast our text block into
        // This isnt efficient, and is a vote for recording
        // code block types in an enumerated list somewhere,
        // as well as a generic attribute "blockType" we could
        // quickly access, rather than casting. -b.t.
        HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock *>(tb);
        CodeMethodBlock * mb = 0;
        CodeClassFieldDeclarationBlock * db = 0;
        CodeBlockWithComments * cb = 0;
        // CodeComment * cm = 0;
        if(hb)
            appendText(hb);
        else if ( (mb = dynamic_cast<CodeMethodBlock*>(tb)) )
            appendText(mb);
        else if ( (db = dynamic_cast<CodeClassFieldDeclarationBlock*>(tb)) )
            appendText(db);
        else if ( (cb = dynamic_cast<CodeBlockWithComments*>(tb)) )
            appendText(cb);
        /*
                        // no! shouldn't be any 'naked' comments floating about. Always
                        // are assocated with a parent code block
                        else if ( (cm = dynamic_cast<CodeComment*>(tb)) )
                                appendText(cm);
        */
        else
            appendText(tb); // no cast worked. Just do a text block
    }

}

void CodeEditor::appendText (CodeComment * comment, TextBlock * parent, UMLObject * umlObj , const TQString & componentName)
{

    if(!comment->getWriteOutText() && !m_showHiddenBlocks)
        return;

    TQColor bgcolor = getState().nonEditBlockColor;
    if(!comment->getWriteOutText() && m_showHiddenBlocks)
        bgcolor = getState().hiddenColor;

    TQString indent = comment->getIndentationString();
    TQString text = comment->toString(); // use comment formatting, NOT formatMultiLineText(comment->toString(), indent, "\n");
    if(!StringIsBlank(text))
        insert(text,parent,true,getState().fontColor, bgcolor, umlObj, componentName);

}

void CodeEditor::appendText (CodeBlockWithComments * cb ) {

    if(!cb->getWriteOutText() && !m_showHiddenBlocks)
        return;

    TQString indent = cb->getIndentationString();
    TQString body = cb->formatMultiLineText (cb->getText(), indent, "\n");

    TQColor bgcolor = getState().editBlockColor;
    TQString componentName = TQString("CodeBlock");

    appendText(cb->getComment(), cb, 0, componentName);

    if(!cb->getWriteOutText() && m_showHiddenBlocks)
        bgcolor = getState().hiddenColor;

    if(!StringIsBlank(body))
        insert(body,cb,true,getState().fontColor,bgcolor,0);

}

void CodeEditor::appendText (CodeClassFieldDeclarationBlock * db ) {

    if(!db->getWriteOutText() && !m_showHiddenBlocks)
        return;

    TQString indent = db->getIndentationString();
    TQString body = db->formatMultiLineText (db->getText(), indent, "\n");

    UMLObject * parentObj = db->getParentClassField()->getParentObject();

    TQColor bgcolor = getState().editBlockColor;
    TQString componentName = TQString("");
    if(parentObj)
    {
        if(db->getParentClassField()->parentIsAttribute()) {
            componentName = parentDocName + "::attribute_field(" + parentObj->getName() + ')';
        } else {
            UMLRole * role = dynamic_cast<UMLRole*>(parentObj);
            componentName = parentDocName + "::association_field(" + role->getName() + ')';
        }
        bgcolor = getState().umlObjectColor;
    }

    appendText(db->getComment(), db, parentObj,componentName);

    if(!db->getWriteOutText() && m_showHiddenBlocks)
        bgcolor = getState().hiddenColor;

    if(!StringIsBlank(body))
        insert(body,db,false,getState().fontColor,bgcolor,parentObj);


}

void CodeEditor::appendText (CodeMethodBlock * mb) {

    // Note: IF CodeAccessors are hidden, we DON'T show
    // it even when requested as the hiddeness of these methods
    // should be controled by the class fields, not the user in the editor.
    if(!mb->getWriteOutText() && (!m_showHiddenBlocks || dynamic_cast<CodeAccessorMethod*>(mb)))
        return;

    TQColor bgcolor = getState().umlObjectColor;
    TQString indent = mb->getIndentationString();
    TQString bodyIndent = mb->getIndentationString(mb->getIndentationLevel()+1);

    TQString startText = mb->formatMultiLineText ( mb->getStartMethodText(), indent, "\n");
    TQString body = mb->formatMultiLineText (mb->getText(), bodyIndent, "\n");
    TQString endText = mb->formatMultiLineText( mb->getEndMethodText(), indent, "\n");

    if(body.isEmpty())
        body = " \n";

    if(!mb->getWriteOutText() && m_showHiddenBlocks)
    {
        // it gets the 'hidden' color
        bgcolor = getState().hiddenColor;
    }

    TQString componentName = TQString("<b>parentless method\?</b>");

    // ugly, but we need to know if there is a parent object here.
    CodeOperation * op = dynamic_cast<CodeOperation*>(mb);
    CodeAccessorMethod * accessor = dynamic_cast<CodeAccessorMethod*>(mb);
    UMLObject * parentObj = 0;
    if(op)
    {
        parentObj = op->getParentOperation();
        if(((UMLOperation*)parentObj)->isConstructorOperation())
            componentName = parentDocName + "::operation("+ parentObj->getName()+") constructor method";
        else
            componentName = parentDocName + "::operation("+ parentObj->getName()+") method";
    }
    if(accessor)
    {
        parentObj = accessor->getParentObject();
        if(accessor->getParentClassField()->parentIsAttribute()) {
            componentName = parentDocName + "::attribute_field(" + parentObj->getName() + ") accessor method";
        } else {
            UMLRole * role = dynamic_cast<UMLRole*>(parentObj);
            componentName = parentDocName + "::association_field(" + role->getName() + ") accessor method";
        }

    }

    //appendText(mb->getComment(), mb, parentObj, componentName);
    appendText(mb->getComment(), mb->getComment(), parentObj, componentName);

    if(!StringIsBlank(startText))
        insert(startText,mb,false,getState().fontColor,bgcolor,parentObj);
    // always insert body for methods..IF we don't, we create a
    // situation where the user cant edit the body (!)
    insert(body,mb,true,getState().fontColor,bgcolor,parentObj);
    if(!StringIsBlank(endText))
        insert(endText,mb,false,getState().fontColor,bgcolor,parentObj);

}

void CodeEditor::appendText (TextBlock * tb) {

    if(!tb->getWriteOutText() && !m_showHiddenBlocks)
        return;

    TQColor bgcolor = getState().nonEditBlockColor;
    if(!tb->getWriteOutText() && m_showHiddenBlocks)
        bgcolor = getState().hiddenColor;

    TQString str = tb->toString();
    insert(str,tb,false,getState().fontColor,bgcolor);

}

void CodeEditor::appendText(HierarchicalCodeBlock * hblock)
{

    if(!hblock->getWriteOutText() && !m_showHiddenBlocks)
        return;

    OwnedHierarchicalCodeBlock * test = dynamic_cast<OwnedHierarchicalCodeBlock *>(hblock);
    UMLObject * parentObj = 0;
    TQString componentName = TQString("");
    TQColor paperColor = getState().nonEditBlockColor;
    if(test)
    {
        parentObj = test->getParentObject();
        UMLClassifier *c = dynamic_cast<UMLClassifier*>(parentObj);
        if (c) {
            TQString typeStr;
            if (c->isInterface())
                typeStr = "Interface";
            else
                typeStr = "Class";
            componentName = parentDocName + "::" + typeStr + '(' + parentObj->getName() + ')';
        } else {
            componentName = parentDocName + "::UNKNOWN(" + parentObj->getName() + ')';
        }

        paperColor = getState().umlObjectColor;
    }

    if(!hblock->getWriteOutText() && m_showHiddenBlocks)
        paperColor = getState().hiddenColor;

    TextBlockList * items = hblock->getTextBlockList();
    TQString indent = hblock->getIndentationString();
    TQString startText = hblock->formatMultiLineText ( hblock->getStartText(), indent, "\n");
    TQString endText = hblock->formatMultiLineText( hblock->getEndText(), indent, "\n");

    appendText(hblock->getComment(), hblock, parentObj, componentName);

    if(!StringIsBlank(startText))
        insert(startText,hblock,false,getState().fontColor,paperColor, parentObj);
    appendText(items);
    if(!StringIsBlank(endText))
        insert(endText,hblock,false,getState().fontColor,paperColor);

}

void CodeEditor::insertParagraph ( const TQString & text, int para )
{
    TQTextEdit::insertParagraph(text,para);
}

void CodeEditor::removeParagraph ( int para )
{
    TQTextEdit::removeParagraph(para);
}

// All umlobjects which may have pop-up boxes should return true here
// Yes, a CRAPPY way of doing this. Im not proud. =b.t.
bool CodeEditor::textBlockIsClickable(UMLObject * obj)
{

    if( dynamic_cast<UMLAttribute*>(obj) )
        return true;
    else if( dynamic_cast<UMLClassifier*>(obj) )
        return true;
    else if( dynamic_cast<UMLRole*>(obj) )
        return true;
    else if( dynamic_cast<UMLOperation*>(obj) )
        return true;

    return false;
}

void CodeEditor::slotChangeSelectedBlockView()
{
    TextBlock * tb = m_selectedTextBlock;
    if(tb) {
        tb->setWriteOutText(tb->getWriteOutText() ? false : true );
        rebuildView(m_lastPara);
    }

}

// change the status of the comment writeOutText value to
// opposite of current value
void CodeEditor::slotChangeSelectedBlockCommentView()
{

    TextBlock * tb = m_selectedTextBlock;
    CodeBlockWithComments * cb = 0;
    if(tb && (cb = dynamic_cast<CodeBlockWithComments*>(tb)))
    {
        cb->getComment()->setWriteOutText(cb->getComment()->getWriteOutText() ? false : true );
        rebuildView( m_lastPara );
    }

}

void CodeEditor::slotInsertCodeBlockBeforeSelected()
{

    TextBlock * tb = m_selectedTextBlock;
    CodeBlockWithComments * newBlock = m_parentDoc->newCodeBlockWithComments();
    newBlock->setText("<<INSERT>>");
    newBlock->getComment()->setWriteOutText(false);

    m_parentDoc->insertTextBlock(newBlock, tb, false);

    int location = m_textBlockList.findRef(m_selectedTextBlock); // find first para of selected block

    TQString body = newBlock->formatMultiLineText (newBlock->getText(), newBlock->getIndentationString(), "\n");

    insert(body,newBlock,true,getState().fontColor,
           getState().editBlockColor,0,TQString("CodeBlock"),location);

}

void CodeEditor::slotInsertCodeBlockAfterSelected()
{

    TextBlock * tb = m_selectedTextBlock;
    CodeBlockWithComments * newBlock = m_parentDoc->newCodeBlockWithComments();
    newBlock->setText("<<INSERT>>");
    newBlock->getComment()->setWriteOutText(false);

    m_parentDoc->insertTextBlock(newBlock, tb, true);

    // find last para of selected block
    TextBlockInfo *tbinfo = (*m_tbInfoMap)[m_selectedTextBlock];
    ParaInfo * lastpi = tbinfo->m_paraList.last();
    int location = m_textBlockList.findRef(m_selectedTextBlock) + lastpi->start + lastpi->size + 1;

    TQString body = newBlock->formatMultiLineText (newBlock->getText(), newBlock->getIndentationString(), "\n");

    insert(body,newBlock,true,getState().fontColor,
           getState().editBlockColor,0,TQString("CodeBlock"),location);

}

TQPopupMenu * CodeEditor::createPopupMenu ( const TQPoint & pos )
{

    TextBlock * tb = m_selectedTextBlock;
    m_lastPara = paragraphAt(pos);

    TQPopupMenu * menu = new TQPopupMenu(this);
    // ugh. A bug in the TQt interaction between TQTextEdit and Menu
    // can sometimes trigger a clear() call of the text area after
    // the popup menu is destroyed. The workaround is to disable
    // the behavior by blocking the destroy signal from the menu.
    menu->blockSignals(true);

    if (m_selectedTextBlock)
    {
        if(tb->getWriteOutText())
            menu->insertItem("Hide",this,TQT_SLOT(slotChangeSelectedBlockView()), Key_H, 0);
        else
            menu->insertItem("Show",this,TQT_SLOT(slotChangeSelectedBlockView()), Key_S, 0);

        CodeBlockWithComments * cb = dynamic_cast<CodeBlockWithComments*>(tb);
        if(cb)
            if(cb->getComment()->getWriteOutText())
                menu->insertItem("Hide Comment",this,TQT_SLOT(slotChangeSelectedBlockCommentView()), CTRL+Key_H, 1);
            else
                menu->insertItem("Show Comment",this,TQT_SLOT(slotChangeSelectedBlockCommentView()), CTRL+Key_S, 1);
        menu->insertSeparator();

        menu->insertItem("Insert Code Block Before",this,TQT_SLOT(slotInsertCodeBlockBeforeSelected()), CTRL+Key_B, 2);
        menu->insertItem("Insert Code Block After",this,TQT_SLOT(slotInsertCodeBlockAfterSelected()), CTRL+Key_A, 3);

        menu->insertSeparator();

        menu->insertItem("Copy",this,TQT_SLOT(slotCopyTextBlock()), CTRL+Key_C, 4);
        menu->insertItem("Paste",this,TQT_SLOT(slotPasteTextBlock()), CTRL+Key_V, 5);
        menu->insertItem("Cut",this,TQT_SLOT(slotCutTextBlock()), CTRL+Key_X, 6);

        // enable/disable based on conditions
        if(m_selectedTextBlock == m_parentDoc->getHeader())
            menu->setItemEnabled (2, false);

        if(!m_textBlockToPaste)
            menu->setItemEnabled (5, false);

        if(!tb->canDelete())
            menu->setItemEnabled (6, false);

        // manythings cant be copied. RIght now, lets just limit ourselves to
        // owned things and hierarchicalcodeblocks
        if(dynamic_cast<OwnedCodeBlock*>(m_selectedTextBlock) ||
                dynamic_cast<HierarchicalCodeBlock*>(m_selectedTextBlock))
            menu->setItemEnabled (4, false);

        // TBD
        // m_selectedTextBlock->insertCodeEditMenuItems(menu, this);
    }

    return menu;
}

void CodeEditor::slotCopyTextBlock ( )
{
    // make a copy
    if(dynamic_cast<HierarchicalCodeBlock*>(m_selectedTextBlock))
        m_textBlockToPaste = m_parentDoc->newHierarchicalCodeBlock();
    else if(dynamic_cast<CodeBlockWithComments*>(m_selectedTextBlock))
        m_textBlockToPaste = m_parentDoc->newCodeBlockWithComments();
    else if(dynamic_cast<CodeBlock*>(m_selectedTextBlock))
        m_textBlockToPaste = m_parentDoc->newCodeBlock();
    else if(dynamic_cast<CodeComment*>(m_selectedTextBlock))
        m_textBlockToPaste = CodeGenFactory::newCodeComment(m_parentDoc);
    else
    {
        kError()<<" ERROR: CodeEditor can't copy selected block:"<<m_selectedTextBlock<<" of unknown type"<<endl;
        m_textBlockToPaste = 0;
        return; // error!
    }

    m_textBlockToPaste->setAttributesFromObject(m_selectedTextBlock);

}

void CodeEditor::slotCutTextBlock ( ) {

    // make a copy first
    slotCopyTextBlock();

    // This could cause problems, but we are OK as
    // long as we only try to delete 'canDelete' textblocks
    if(m_selectedTextBlock->canDelete())
    {
        // just in case there are pending edits
        // we don't want to lose them
        if (m_lastTextBlockToBeEdited && m_lastTextBlockToBeEdited == (CodeBlock*) m_selectedTextBlock)
        {
            updateTextBlockFromText (m_lastTextBlockToBeEdited);
            m_lastTextBlockToBeEdited = 0;
        }

        m_parentDoc->removeTextBlock(m_selectedTextBlock);
        rebuildView(m_lastPara);
        // removeTextBlock(m_selectedTextBlock);
        m_selectedTextBlock = 0;
    }

}

void CodeEditor::slotPasteTextBlock ( ) {

    if(m_textBlockToPaste)
    {
        m_parentDoc->insertTextBlock(m_textBlockToPaste, m_selectedTextBlock);
        m_textBlockToPaste = 0;
        rebuildView(m_lastPara);
    }

}

void CodeEditor::slotRedrawText() {
    rebuildView(m_lastPara);
}

void CodeEditor::init ( CodeViewerDialog * parentDlg, CodeDocument * parentDoc ) {

    // safety to insure that we are up to date
    parentDoc->synchronize();

    m_parentDlg = parentDlg;
    m_parentDoc = parentDoc;

    setUndoRedoEnabled( false );
    setCursor( TQCursor( 0 ) );
    setMouseTracking( true );
    setReadOnly (true);
    m_isHighlighted = getState().blocksAreHighlighted;
    m_showHiddenBlocks = getState().showHiddenBlocks;

    m_newLinePressed = false;
    m_backspacePressed = false;
    m_textBlockToPaste = 0;
    m_selectedTextBlock = 0;
    m_lastTextBlockToBeEdited = 0;
    m_tbInfoMap = new TQMap<TextBlock *, TextBlockInfo*>;

    setFont( getState().font );

    // set name of parent doc
    ClassifierCodeDocument * cdoc = dynamic_cast<ClassifierCodeDocument*>(m_parentDoc);
    if(cdoc)
        parentDocName = cdoc->getParentClassifier()->getName();
    else
        parentDocName = "";

    // set some viewability parameters
    //int margin = fontMetrics().height();

    TQBrush pbrush = TQBrush ( getState().paperColor);
    setPaper(pbrush);

    // setMargin(margin);

    //       connect(this,TQT_SIGNAL(newLinePressed()),this,TQT_SLOT(newLinePressed()));
    //       connect(this,TQT_SIGNAL(backspacePressed()),this,TQT_SLOT(backspacePressed()));
    connect(this,TQT_SIGNAL(doubleClicked(int,int)),this,TQT_SLOT(doubleClicked(int,int)));
    connect(this,TQT_SIGNAL(cursorPositionChanged(int,int)),this,TQT_SLOT(cursorPositionChanged(int,int)));

    // do this last
    loadFromDocument();

}

void CodeEditor::updateTextBlockFromText (TextBlock * block) {

    if (block) {

        CodeMethodBlock * cmb = dynamic_cast<CodeMethodBlock*>(block);
        //TQString baseIndent = block->getNewEditorLine(block->getIndentationLevel()+(cmb ? 1 : 0));
        TQString baseIndent = block->getIndentationString(block->getIndentationLevel()+(cmb ? 1 : 0));

        TextBlockInfo *info = (*m_tbInfoMap)[block];
        UMLObject * parentObj = info->getParent();
        int pstart = m_textBlockList.findRef(block);
        TQString content = "";

        // Assemble content from editiable paras
        TQPtrList<ParaInfo> list = info->m_paraList;
        for(ParaInfo * item = list.first(); item; item=list.next())
        {
            if(item->isEditable)
            {
                int lastpara = item->start+pstart+item->size;
                int endEdit = block->lastEditableLine();
                int lastLineToAddNewLine = lastpara + endEdit;
                for(int para=(item->start+pstart);para<=lastpara;para++)
                {
                    TQString line = block->unformatText(text(para), baseIndent);
                    content += line;
                    // \n are implicit in the editor (!) so we should put them
                    // back in, if there is any content from the line
                    if(!line.isEmpty() && para != lastLineToAddNewLine)
                        content += "\n";
                }
            }
        }

        //cerr<<"UPDATE GOT CONTENT:["<<content.latin1()<<"] to block:"<<block<<endl;
        block->setText(content);

        // if a parent for the block, try to set its documentation
        // as long as its NOT an accessor codeblock.
        if(parentObj && !info->isCodeAccessorMethod)
            parentObj->setDoc(content);

        // make note that its now user generated
        if(cmb)
            cmb->setContentType(CodeBlock::UserGenerated);

    }
}

void CodeEditor::cursorPositionChanged(int para, int pos)
{

    // safety.. this is endemic of a 'bad' pointer event and can crash us otherwise
    if(pos < 0)
        return;

    //      bool lastParaIsEditable = isReadOnly() ? false : true;
    bool lastParaIsEditable = isParaEditable(m_lastPara);

    // IF last para where cursor is coming from was editable
    // we have a variety of things to look out for.
    if(lastParaIsEditable)
    {
        // If we got here as the result of a newline, then expansion
        // of a para editablity occurs.
        if((para-1) == m_lastPara && m_newLinePressed )
            expandSelectedParagraph ( m_lastPara );

        // conversely, we contract the zone of editablity IF we
        // got to current position as result of backspace
        if((para+1) == m_lastPara && m_backspacePressed )
            contractSelectedParagraph( para );

    }

    // now check if the current paragraph is really editiable, and if so,
    // so some things
    bool editPara = isParaEditable(para);
    if(editPara) {

        TextBlock * tBlock = m_textBlockList.at(para);
        CodeMethodBlock * cmb = dynamic_cast<CodeMethodBlock*>(tBlock);

        // auto-indent new lines
        TQString currentParaText = text(para);
        TQString baseIndent = tBlock->getNewEditorLine(tBlock->getIndentationLevel()+(cmb ? 1 : 0));
        // cerr<<"AUTO INDENT:["<<baseIndent.latin1()<<"] isMethod?"<<(cmb?"true":"false")<<endl;
        int minPos = baseIndent.length();

        // add indent chars to the current line, if missing
        if(!m_backspacePressed && !currentParaText.contains(TQRegExp('^'+baseIndent)))
        {
            insertAt(baseIndent,para,0);
            setCursorPosition(para,pos+minPos);
            return;
        }

        if(pos<minPos)
        {

            bool priorParaIsEditable = isParaEditable(para-1);
            if(m_backspacePressed && para && priorParaIsEditable)
            {
                int endOfPriorLine = paragraphLength(para-1);
                // IN this case, we remove old (para) line, and tack its
                // contents on the line we are going to.
                TQString contents = text(para);
                contents = contents.right(contents.length()-m_lastPos+1);

                // this next thing happens when we arent deleting last line
                // of editable text, so we want to append whats left of this line
                // onto the one we are backspacing into
                if(paraIsNotSingleLine(para))
                {
                    removeParagraph(para);
                    insertAt(contents,(para-1),endOfPriorLine);
                    setCursorPosition((para-1),endOfPriorLine);
                }

            } else {
                // well, if the following is true, then they
                // are trying to hack away at the last line, which
                // we cant allow to entirely disappear. Lets preserve
                // the indentation
                if(m_backspacePressed && !priorParaIsEditable)
                {
                    TQString contents = text(para);
                    contents = contents.right(contents.length()-m_lastPos+1);
                    contents = baseIndent + contents.left(contents.length()-1); // left is to remove trailing space
                    insertParagraph(contents,para+1);
                    removeParagraph(para);

                    // furthermore, IF its nothing but indentation + whitespace
                    // we switch this back to Auto-Generated.
                    if(cmb && contents.contains(TQRegExp('^'+baseIndent+"\\s$")))
                    {
                        cmb->setContentType(CodeBlock::AutoGenerated);
                        cmb->syncToParent();
                    }

                }

                // send them to the first spot in the line which is editable
                setCursorPosition(para,minPos);

            }
            return;
        }
    }

    // look for changes in editability, if they occur, we need to record
    // the edits which have been made
    if((editPara && !m_lastTextBlockToBeEdited) || (!editPara && m_lastTextBlockToBeEdited)) {

        setReadOnly(editPara ? false : true);

        // IF this is a different text block, update the body of the method
        // it belongs to
        if(m_lastTextBlockToBeEdited && (m_lastTextBlockToBeEdited != m_textBlockList.at(para) || !editPara))
        {
            updateTextBlockFromText (m_lastTextBlockToBeEdited);
            m_lastTextBlockToBeEdited = 0;
        }

        if(editPara)
            m_lastTextBlockToBeEdited = m_textBlockList.at(para);
        else
            m_lastTextBlockToBeEdited = 0;

    }

    m_lastPara = para;
    m_lastPos = pos;
    m_newLinePressed = false;
    m_backspacePressed = false;

}

bool CodeEditor::paraIsNotSingleLine (int para)
{
    TextBlock * tBlock = m_textBlockList.at(para);
    if(tBlock)
    {
        int pstart = m_textBlockList.findRef(tBlock);
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        TQPtrList<ParaInfo> list = info->m_paraList;

        for(ParaInfo * item = list.first(); item; item=list.next())
            if((pstart+item->start) <= para && (item->start+pstart+item->size) >= para )
                if(item->size > 0)
                    return true;
    }
    return false;
}

bool CodeEditor::isParaEditable (int para) {

    if (para <0)
        return false;

    TextBlock * tBlock = m_textBlockList.at(para);
    if(tBlock)
    {
        int editStart = tBlock->firstEditableLine();
        int editEnd = tBlock->lastEditableLine();
        bool hasEditableRange = (editStart > 0 || editEnd < 0) ? true : false;
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        int pstart = m_textBlockList.findRef(tBlock);
        int relativeLine = para - pstart;
        TQPtrList<ParaInfo> list = info->m_paraList;
        for(ParaInfo * item = list.first(); item; item=list.next())
        {
            if((item->start+pstart) <= para && (item->start+pstart+item->size) >= para)
                if(item->isEditable && hasEditableRange)
                {
                    if ( relativeLine >= editStart && relativeLine <= (item->size + editEnd) )
                        return true;
                    else
                        return false;
                } else
                    return item->isEditable;
        }
    }
    return false;
}

void CodeEditor::changeTextBlockHighlighting(TextBlock * tBlock, bool selected) {

    if(tBlock)
    {
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        TQPtrList<ParaInfo> list = info->m_paraList;
        int pstart = m_textBlockList.findRef(tBlock);
        for(ParaInfo * item = list.first(); item; item=list.next())
            for(int p=(item->start+pstart);p<=(item->start+pstart+item->size);p++)
                if(selected)
                    if(info->isClickable)
                        setParagraphBackgroundColor(p,getState().selectedColor);
                    else
                        setParagraphBackgroundColor(p,getState().nonEditBlockColor);
                else if(m_isHighlighted)
                    setParagraphBackgroundColor(p,item->bgcolor);
                else
                    setParagraphBackgroundColor(p,getState().paperColor);
    }

}

void CodeEditor::changeShowHidden (int signal) {

    if(signal)
        m_showHiddenBlocks = true;
    else
        m_showHiddenBlocks = false;

    rebuildView(m_lastPara);

}

// colorizes/uncolorizes type for ALL paragraphs
void CodeEditor::changeHighlighting(int signal) {

    int total_para = paragraphs()-1;
    if(signal) {
        // we want to highlight
        m_isHighlighted = true;
        for(int para=0;para<total_para;para++)
        {
            TextBlock * tblock = m_textBlockList.at(para);
            changeTextBlockHighlighting(tblock,false);
        }


    } else {
        // we DON'T want to highlight
        m_isHighlighted = false;
        for(int para=0;para<total_para;para++)
            setParagraphBackgroundColor(para,getState().paperColor);
    }

    // now redo the "selected" para, should it exist
    if(m_selectedTextBlock)
        changeTextBlockHighlighting(m_selectedTextBlock,true);

}

void CodeEditor::contractSelectedParagraph( int paraToRemove ) {
    TextBlock * tBlock = m_textBlockList.at(paraToRemove);
    if(tBlock)
    {
        int pstart = m_textBlockList.findRef(tBlock);
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        TQPtrList<ParaInfo> list = info->m_paraList;

        bool lowerStartPosition = false;
        for(ParaInfo * item = list.first(); item; item=list.next())
        {
            if(lowerStartPosition)
                item->start -= 1;

            if((pstart+item->start) <= paraToRemove && (item->start+pstart+item->size) >= paraToRemove)
            {
                item->size -= 1;
                // a little cheat.. we don't want to remove last line as we need
                // to leave a place that can be 'edited' by the tool IF the user
                // changes their mind about method body content
                if(item->size < 0)
                    item->size = 0;
                lowerStartPosition = true;
            }
        }

        m_textBlockList.remove(paraToRemove);
    }
}

void CodeEditor::expandSelectedParagraph( int priorPara ) {


    TextBlock * tBlock = m_textBlockList.at(priorPara);
    if(tBlock)
    {
        // add this tBlock in
        m_textBlockList.insert(priorPara,tBlock);
        TextBlockInfo *info = (*m_tbInfoMap)[tBlock];
        TQPtrList<ParaInfo> list = info->m_paraList;
        int pstart = m_textBlockList.findRef(tBlock);

        // now update the paragraph information
        bool upStartPosition = false;
        for(ParaInfo * item = list.first(); item; item=list.next())
        {
            // AFTER we get a match, then following para's need to have start position upped too
            if(upStartPosition)
                item->start += 1;

            if((pstart+item->start) <= priorPara && (item->start+pstart+item->size) >= priorPara)
            {
                item->size += 1;
                cursorPositionChanged(m_lastPara, m_lastPos);
                upStartPosition = true;
            }
        }
    }

}

void CodeEditor::contentsMouseMoveEvent ( TQMouseEvent * e )
{

    int para = paragraphAt(e->pos());

    if (para < 0)
        return; // shouldn't happen..

    TextBlock * tblock = m_textBlockList.at(para);
    if (tblock && m_selectedTextBlock != tblock ) {
        TextBlockInfo * info = (*m_tbInfoMap)[tblock];

        // unhighlight old selected textblock regardless of whether
        // it was selected or not.
        changeTextBlockHighlighting(m_selectedTextBlock,false);

        // highlight new block
        changeTextBlockHighlighting(tblock,true);

        // FIX: update the label that shows what type of component this is
        getComponentLabel()->setText("<b>"+info->displayName+"</b>");

        m_selectedTextBlock = tblock;

        if(m_lastTextBlockToBeEdited)
        {
            updateTextBlockFromText (m_lastTextBlockToBeEdited);
            m_lastTextBlockToBeEdited = 0;
        }
    }

    // record this as the last paragraph

}


// Rebuild our view of the document. Happens whenever we change
// some field/aspect of an underlying UML object used to create
// the view.
// If connections are right, then the UMLObject will send out the modified()
// signal which will trigger a call to re-generate the appropriate code within
// the code document. Our burden is to appropriately prepare the tool: we clear
// out ALL the textblocks in the TQTextEdit widget and then re-show them
// after the dialog disappears
void CodeEditor::rebuildView( int startCursorPos ) {

    loadFromDocument();

    // make a minima attempt to leave the cursor (view of the code) where
    // we started
    int new_nrof_para = paragraphs() -1;
    setCursorPosition((startCursorPos < new_nrof_para ? startCursorPos : 0), 0);

}




#include "codeeditor.moc"