summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codeblockwithcomments.cpp
blob: 07709cf88a5c8007c2f05a5ed6a2b4ec2c162368 (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
/***************************************************************************
 *                                                                         *
 *   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>                  *
 ***************************************************************************/

/*  This code generated by:
 *      Author : thomas
 *      Date   : Wed Jun 18 2003
 */

// own header
#include "codeblockwithcomments.h"

// qt/kde includes
#include <kdebug.h>

// local includes
#include "codedocument.h"
#include "hierarchicalcodeblock.h"
#include "codegenerators/codegenfactory.h"

// Constructors/Destructors
//

CodeBlockWithComments::CodeBlockWithComments ( HierarchicalCodeBlock * hb, const TQString & body, const TQString & comment)
        : CodeBlock (hb, body)
{
    initFields(hb->getParentDocument(), comment);
}

CodeBlockWithComments::CodeBlockWithComments ( CodeDocument * parent , const TQString & body, const TQString & comment)
        : CodeBlock (parent, body)
{
    initFields(parent, comment);
}

CodeBlockWithComments::~CodeBlockWithComments ( ) { }

//
// Methods
//


// Accessor methods
//


/**
 * Set the comment on this code block.
 */
void CodeBlockWithComments::setComment ( CodeComment * object ) {
    m_comment = object;
}

/**
 * Remove a Comment object from m_commentVector List
 */
CodeComment * CodeBlockWithComments::getComment ( ) {
    return m_comment;
}

// Other methods
//

/**
 * Save the XMI representation of this object
 */
void CodeBlockWithComments::saveToXMI ( TQDomDocument & doc, TQDomElement & root ) {
    TQDomElement blockElement = doc.createElement( "codeblockwithcomments" );

    // set attributes
    setAttributesOnNode(doc, blockElement);

    root.appendChild( blockElement );
}

void CodeBlockWithComments::setAttributesOnNode ( TQDomDocument & doc, TQDomElement & blockElement)
{

    // set super-class attributes
    CodeBlock::setAttributesOnNode(doc, blockElement);

    // set local attributes now..e.g. a comment
    // which we will store in its own separate child node block
    TQDomElement commElement = doc.createElement( "header" );
    getComment()->saveToXMI(doc, commElement); // comment
    blockElement.appendChild( commElement);

}

void CodeBlockWithComments::setAttributesFromObject(TextBlock * obj)
{

    CodeBlock::setAttributesFromObject(obj);

    CodeBlockWithComments * cb = dynamic_cast<CodeBlockWithComments*>(obj);
    if(cb)
        getComment()->setAttributesFromObject((TextBlock*)cb->getComment());

}

/**
 * load params from the appropriate XMI element node.
 */
void CodeBlockWithComments::loadFromXMI ( TQDomElement & root )
{
    setAttributesFromNode(root);
}

void CodeBlockWithComments::setAttributesFromNode( TQDomElement & root)
{

    // set attributes from superclass method the XMI
    CodeBlock::setAttributesFromNode(root);

    // load comment now
    // by looking for our particular child element
    TQDomNode node = root.firstChild();
    TQDomElement element = node.toElement();
    bool gotComment = false;
    while( !element.isNull() ) {
        TQString tag = element.tagName();
        if( tag == "header" ) {
            TQDomNode cnode = element.firstChild();
            TQDomElement celem = cnode.toElement();
            getComment()->loadFromXMI(celem);
            gotComment = true;
            break;
        }
        node = element.nextSibling();
        element = node.toElement();
    }

    if(!gotComment)
        kWarning()<<" loadFromXMI : Warning: unable to initialize CodeComment in block:"<<getTag()<<endl;

}

/**
 * @return      TQString
 */
TQString CodeBlockWithComments::toString ( ) {

    TQString string = TQString();

    if(getWriteOutText()) {
        TQString indent = getIndentationString();
        TQString endLine = getNewLineEndingChars();
        TQString body = formatMultiLineText (getText(), indent, endLine);
        TQString comment = getComment()->toString();

        if(!comment.isEmpty() && getComment()->getWriteOutText())
            string.append(comment);
        if(!body.isEmpty())
            string.append(body);
    }

    return string;
}

// slave indentation level for both the header and text body
void CodeBlockWithComments::setOverallIndentationLevel ( int level )
{
    setIndentationLevel(level);
    m_comment->setIndentationLevel(level);
}

void CodeBlockWithComments::initFields(CodeDocument *parent, const TQString& comment)
{
    CodeComment * codecomment = CodeGenFactory::newCodeComment(parent);
    codecomment->setText(comment);
    m_comment = codecomment;
}

#include "codeblockwithcomments.moc"