• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

umbrello/umbrello

  • sources
  • kde-4.12
  • kdesdk
  • umbrello
  • umbrello
  • codegenerators
hierarchicalcodeblock.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This program is free software; you can redistribute it and/or modify *
3  * it under the terms of the GNU General Public License as published by *
4  * the Free Software Foundation; either version 2 of the License, or *
5  * (at your option) any later version. *
6  * *
7  * copyright (C) 2003 Brian Thomas <thomas@mail630.gsfc.nasa.gov> *
8  * copyright (C) 2004-2013 *
9  * Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
10  ***************************************************************************/
11 
12 // own header
13 #include "hierarchicalcodeblock.h"
14 
15 // local includes
16 #include "codedocument.h"
17 #include "classifiercodedocument.h"
18 #include "codeclassfield.h"
19 #include "codegenerationpolicy.h"
20 #include "codegenerators/codegenfactory.h"
21 #include "debug_utils.h"
22 #include "uml.h"
23 
27 HierarchicalCodeBlock::HierarchicalCodeBlock(CodeDocument * doc, const QString &start, const QString &endString, const QString &comment)
28  : CodeBlockWithComments (doc, start, comment),
29  CodeGenObjectWithTextBlocks(doc)
30 {
31  setEndText(endString);
32  initAttributes();
33 }
34 
35 HierarchicalCodeBlock::~HierarchicalCodeBlock()
36 {
37 }
38 
43 void HierarchicalCodeBlock::setEndText (const QString &new_var)
44 {
45  m_endText = new_var;
46 }
47 
52 QString HierarchicalCodeBlock::getEndText () const
53 {
54  return m_endText;
55 }
56 
60 QString HierarchicalCodeBlock::getUniqueTag()
61 {
62  return getUniqueTag("hblock_tag");
63 }
64 
68 QString HierarchicalCodeBlock::getUniqueTag(const QString& prefix)
69 {
70  return getParentDocument()->getUniqueTag(prefix);
71 }
72 
73 CodeBlock * HierarchicalCodeBlock::newCodeBlock()
74 {
75  return getParentDocument()->newCodeBlock();
76 }
77 
78 CodeBlockWithComments * HierarchicalCodeBlock::newCodeBlockWithComments()
79 {
80  return getParentDocument()->newCodeBlockWithComments();
81 }
82 
83 HierarchicalCodeBlock * HierarchicalCodeBlock::newHierarchicalCodeBlock()
84 {
85  HierarchicalCodeBlock *hb = new HierarchicalCodeBlock(getParentDocument());
86  //hb->update();
87  return hb;
88 }
89 
93 bool HierarchicalCodeBlock::addTextBlock(TextBlock* add_object)
94 {
95  if (CodeGenObjectWithTextBlocks::addTextBlock(add_object))
96  {
97  getParentDocument()->addChildTagToMap(add_object->getTag(), add_object);
98  return true;
99  }
100  return false;
101 }
102 
107 bool HierarchicalCodeBlock::insertTextBlock(TextBlock * newBlock, TextBlock * existingBlock, bool after)
108 {
109  if (!newBlock || !existingBlock)
110  return false;
111 
112  QString tag = existingBlock->getTag();
113  // FIX: just do a quick check if the parent DOCUMENT has this.
114  // IF it does, then the lack of an index will force us into
115  // a search of any child hierarchical codeblocks we may have
116  // Its not efficient, but works. I don't think speed is a problem
117  // right now for the current implementation, but in the future
118  // when code import/roundtripping is done, it *may* be. -b.t.
119  if (!getParentDocument()->findTextBlockByTag(tag, true))
120  return false;
121 
122  int index = m_textblockVector.indexOf(existingBlock);
123  if (index < 0)
124  {
125  // may be hiding in child hierarchical codeblock
126  foreach (TextBlock* tb, m_textblockVector)
127  {
128  HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(tb);
129  if (hb && hb->insertTextBlock(newBlock, existingBlock, after))
130  return true; // found, and inserted, otherwise keep going
131  }
132  uWarning() << "Warning: couldnt insert text block (tag:" << newBlock->getTag() <<
133  "). Reference text block (tag:" << existingBlock->getTag() << ") not found.";
134  return false;
135  }
136 
137  // if we get here.. it was in this object so insert
138 
139  // check for tag FIRST
140  QString new_tag = newBlock->getTag();
141 
142  // assign a tag if one doesn't already exist
143  if (new_tag.isEmpty())
144  {
145  new_tag = getUniqueTag();
146  newBlock->setTag(new_tag);
147  }
148 
149  if (m_textBlockTagMap.contains(new_tag)) {
150  return false; // return false, we already have some object with this tag in the list
151  }
152  else {
153  m_textBlockTagMap.insert(new_tag, newBlock);
154  getParentDocument()->addChildTagToMap(new_tag, newBlock);
155  }
156 
157  if(after)
158  index++;
159 
160  m_textblockVector.insert(index, newBlock);
161 
162  return true;
163 }
164 
169 bool HierarchicalCodeBlock::removeTextBlock (TextBlock * remove_object)
170 {
171  // try to remove from the list in this object
172  int indx = m_textblockVector.indexOf(remove_object);
173  if (indx > -1) {
174  m_textblockVector.removeAt(indx);
175  }
176  else {
177  // may be hiding in child hierarchical codeblock
178  foreach (TextBlock* tb, m_textblockVector)
179  {
180  HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(tb);
181  if (hb && hb->removeTextBlock(remove_object))
182  return true; // because we got in child hb;
183  }
184  return false;
185  }
186 
187  // IF we get here, the text block was in THIS object (and not a child)..
188  QString tag = remove_object->getTag();
189  if (!(tag.isEmpty())) {
190  m_textBlockTagMap.remove(tag);
191  getParentDocument()->removeChildTagFromMap(tag);
192  }
193  return true;
194 }
195 
199 void HierarchicalCodeBlock::setStartText (const QString &text)
200 {
201  m_startText = text;
202 }
203 
207 QString HierarchicalCodeBlock::getStartText () const
208 {
209  return m_startText;
210 }
211 
215 void HierarchicalCodeBlock::addCodeClassFieldMethods(CodeClassFieldList &list)
216 {
217  CodeClassFieldList::Iterator it = list.begin();
218  CodeClassFieldList::Iterator end = list.end();
219  for (; it != end; ++it)
220  {
221  CodeClassField * field = *it;
222  CodeAccessorMethodList list = field->getMethodList();
223  Q_FOREACH(CodeAccessorMethod *method, list)
224  {
225  QString tag = method->getTag();
226  if (tag.isEmpty())
227  {
228  tag = getUniqueTag();
229  method->setTag(tag);
230  }
231 
232  addTextBlock(method); // wont add if already exists in object;
233  }
234  }
235 }
236 
240 void HierarchicalCodeBlock::saveToXMI (QDomDocument & doc, QDomElement & root)
241 {
242  QDomElement blockElement = doc.createElement("hierarchicalcodeblock");
243  setAttributesOnNode(doc, blockElement);
244  root.appendChild(blockElement);
245 }
246 
251 void HierarchicalCodeBlock::setAttributesOnNode (QDomDocument & doc, QDomElement & elem)
252 {
253  // set super-class attributes
254  CodeBlockWithComments::setAttributesOnNode(doc, elem);
255  CodeGenObjectWithTextBlocks::setAttributesOnNode(doc, elem);
256 
257  // set local class attributes
258  if (contentType() != CodeBlock::AutoGenerated)
259  {
260  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
261  elem.setAttribute("startText", encodeText(getStartText(), endLine));
262  elem.setAttribute("endText", encodeText(getEndText(), endLine));
263  }
264 }
265 
269 void HierarchicalCodeBlock::loadFromXMI (QDomElement & root)
270 {
271  setAttributesFromNode(root);
272 }
273 
278 void HierarchicalCodeBlock::setAttributesFromNode (QDomElement & root)
279 {
280  // set attributes from the XMI
281  CodeBlockWithComments::setAttributesFromNode(root); // superclass load
282 
283  if (contentType() != CodeBlock::AutoGenerated)
284  {
285  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
286  setStartText(decodeText(root.attribute("startText",""), endLine));
287  setEndText(decodeText(root.attribute("endText",""), endLine));
288  }
289 
290  // do this *after* all other attributes saved
291  CodeGenObjectWithTextBlocks::setAttributesFromNode(root);
292 }
293 
297 void HierarchicalCodeBlock::setAttributesFromObject (TextBlock * obj)
298 {
299  CodeBlockWithComments::setAttributesFromObject(obj);
300 
301  HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(obj);
302  if (hb) {
303  setStartText(hb->getStartText());
304  setEndText(hb->getEndText());
305  CodeGenObjectWithTextBlocks *cgowtb = dynamic_cast<CodeGenObjectWithTextBlocks*>(obj);
306  CodeGenObjectWithTextBlocks::setAttributesFromObject(cgowtb);
307  }
308 }
309 
313 QString HierarchicalCodeBlock::toString() const
314 {
315  QString string;
316 
317  if (getWriteOutText()) {
318  QString indent = getIndentationString();
319  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
320  QString startText;
321  QString endText;
322  if (!getStartText().isEmpty())
323  startText = formatMultiLineText (getStartText(), indent, endLine);
324  if (!getEndText().isEmpty())
325  endText = formatMultiLineText (getEndText(), indent, endLine);
326 
327  QString body = childTextBlocksToString();
328  QString comment = getComment()->toString();
329 
330  // tack in text, if there is something there..
331  if (!comment.isEmpty() && getComment()->getWriteOutText())
332  string.append(comment);
333 
334  if (!startText.isEmpty())
335  string.append(startText);
336 
337  if (!body.isEmpty())
338  string.append(body);
339 
340  if (!endText.isEmpty())
341  string.append(endText);
342  }
343  return string;
344 }
345 
346 QString HierarchicalCodeBlock::childTextBlocksToString() const
347 {
348  TextBlockList* list = getTextBlockList();
349  QString retString;
350  foreach (TextBlock* block, *list)
351  {
352  QString blockValue = block->toString();
353  if (!blockValue.isEmpty())
354  retString.append(blockValue);
355  }
356  return retString;
357 }
358 
362 TextBlock * HierarchicalCodeBlock::findCodeClassFieldTextBlockByTag (const QString &tag)
363 {
364  ClassifierCodeDocument * cdoc = dynamic_cast<ClassifierCodeDocument*>(getParentDocument());
365  if(cdoc)
366  return cdoc->findCodeClassFieldTextBlockByTag(tag);
367  else
368  uError() << "HierarchicalCodeBlock: findCodeClassFieldTextBlockByTag() finds NO parent document! Badly constructed textblock?!?";
369 
370  // if we get here, we failed.
371  return (TextBlock*) NULL;
372 }
373 
374 void HierarchicalCodeBlock::initAttributes()
375 {
376  setCanDelete(false);
377  m_startText.clear();
378  m_endText.clear();
379 }
380 
386 void HierarchicalCodeBlock::release()
387 {
388  resetTextBlocks();
389  TextBlock::release();
390 }
HierarchicalCodeBlock::newCodeBlockWithComments
virtual CodeBlockWithComments * newCodeBlockWithComments()
Definition: hierarchicalcodeblock.cpp:78
CodeClassField
class CodeClassField a special type of parameter.
Definition: codeclassfield.h:29
CodeDocument::newCodeBlockWithComments
virtual CodeBlockWithComments * newCodeBlockWithComments()
Create a new CodeBlockWithComments object belonging to this CodeDocument.
Definition: codedocument.cpp:465
CodeDocument::newCodeBlock
virtual CodeBlock * newCodeBlock()
Create a new CodeBlock object belonging to this CodeDocument.
Definition: codedocument.cpp:456
HierarchicalCodeBlock::release
virtual void release()
causes the text block to release all of its connections and any other text blocks that it 'owns'...
Definition: hierarchicalcodeblock.cpp:386
ClassifierCodeDocument::findCodeClassFieldTextBlockByTag
TextBlock * findCodeClassFieldTextBlockByTag(const QString &tag)
Find a specific textblock held by any code class field in this document by its tag.
Definition: classifiercodedocument.cpp:766
CodeBlock
A "chunk" of code within the code document.
Definition: codeblock.h:20
CodeBlockWithComments::getComment
CodeComment * getComment() const
Get the Comment object.
Definition: codeblockwithcomments.cpp:46
CodeGenObjectWithTextBlocks::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &elem)
Set attributes of the node that represents this class in the XMI document.
Definition: codegenobjectwithtextblocks.cpp:406
HierarchicalCodeBlock::~HierarchicalCodeBlock
virtual ~HierarchicalCodeBlock()
Empty Destructor.
Definition: hierarchicalcodeblock.cpp:35
CodeGenObjectWithTextBlocks::setAttributesFromObject
void setAttributesFromObject(CodeGenObjectWithTextBlocks *obj)
Empty method.
Definition: codegenobjectwithtextblocks.cpp:390
ClassifierCodeDocument
class ClassifierCodeDocument A CodeDocument which represents a UMLClassifier (e.g.
Definition: classifiercodedocument.h:33
CodeBlockWithComments::setAttributesFromObject
virtual void setAttributesFromObject(TextBlock *obj)
Set the class attributes from a passed object.
Definition: codeblockwithcomments.cpp:83
codedocument.h
CodeBlockWithComments::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &blockElement)
Set attributes of the node that represents this class in the XMI document.
Definition: codeblockwithcomments.cpp:68
TextBlock::getTag
QString getTag() const
Get the tag of this text block.
Definition: textblock.cpp:110
CodeBlock::AutoGenerated
the content was generated by code generation itself
Definition: codeblock.h:28
CodeGenerationPolicy::getNewLineEndingChars
QString getNewLineEndingChars() const
Utility function to get the actual characters.
Definition: codegenerationpolicy.cpp:248
UMLApp::app
static UMLApp * app()
Get the last created instance of this class.
Definition: uml.cpp:206
HierarchicalCodeBlock::newCodeBlock
virtual CodeBlock * newCodeBlock()
Virtual methods that return a new code document objects.
Definition: hierarchicalcodeblock.cpp:73
HierarchicalCodeBlock::setEndText
void setEndText(const QString &new_var)
Set the value of m_endText.
Definition: hierarchicalcodeblock.cpp:43
CodeGenObjectWithTextBlocks::resetTextBlocks
virtual void resetTextBlocks()
Reset/clear the inventory text blocks held by this object.
Definition: codegenobjectwithtextblocks.cpp:333
CodeBlock::contentType
ContentType contentType() const
Get the value of m_contentType specifies whether the content (text) of this object was generated by t...
Definition: codeblock.cpp:54
uWarning
#define uWarning()
Definition: debug_utils.h:97
HierarchicalCodeBlock::getUniqueTag
QString getUniqueTag()
return a unique, and currently unallocated, text block tag for this hblock
Definition: hierarchicalcodeblock.cpp:60
CodeGenObjectWithTextBlocks::addTextBlock
virtual bool addTextBlock(TextBlock *add_object)
Add a TextBlock object to the m_textblockVector List.
Definition: codegenobjectwithtextblocks.cpp:57
debug_utils.h
HierarchicalCodeBlock::addCodeClassFieldMethods
void addCodeClassFieldMethods(CodeClassFieldList &list)
Utility method to add accessormethods in this object.
Definition: hierarchicalcodeblock.cpp:215
codegenerationpolicy.h
CodeGenObjectWithTextBlocks::m_textblockVector
TextBlockList m_textblockVector
Definition: codegenobjectwithtextblocks.h:96
HierarchicalCodeBlock::removeTextBlock
bool removeTextBlock(TextBlock *remove_object)
Remove a TextBlock object from m_textblockVector List returns boolean - true if successful.
Definition: hierarchicalcodeblock.cpp:169
HierarchicalCodeBlock::insertTextBlock
bool insertTextBlock(TextBlock *newBlock, TextBlock *existingBlock, bool after=true)
Insert a new text block before/after the existing text block.
Definition: hierarchicalcodeblock.cpp:107
CodeBlockWithComments
class CodeBlockWithComments A very common type of text block in any type of code. ...
Definition: codeblockwithcomments.h:24
HierarchicalCodeBlock::setAttributesFromObject
virtual void setAttributesFromObject(TextBlock *obj)
set the class attributes from a passed object
Definition: hierarchicalcodeblock.cpp:297
HierarchicalCodeBlock::saveToXMI
virtual void saveToXMI(QDomDocument &doc, QDomElement &root)
Save the XMI representation of this object.
Definition: hierarchicalcodeblock.cpp:240
TextBlock::setCanDelete
void setCanDelete(bool canDelete)
Set the attribute m_canDelete.
Definition: textblock.cpp:50
codegenfactory.h
HierarchicalCodeBlock::getEndText
QString getEndText() const
Get the value of m_endText.
Definition: hierarchicalcodeblock.cpp:52
HierarchicalCodeBlock::loadFromXMI
virtual void loadFromXMI(QDomElement &root)
load params from the appropriate XMI element node.
Definition: hierarchicalcodeblock.cpp:269
CodeDocument
A document containing the code for one file.
Definition: codedocument.h:32
HierarchicalCodeBlock::addTextBlock
bool addTextBlock(TextBlock *add_object)
Add a TextBlock object to the m_textblockVector List.
Definition: hierarchicalcodeblock.cpp:93
CodeDocument::addChildTagToMap
void addChildTagToMap(const QString &tag, TextBlock *tb)
Definition: codedocument.cpp:486
CodeAccessorMethod
Definition: codeaccessormethod.h:20
codeclassfield.h
CodeGenObjectWithTextBlocks
This abstract class is for code generator objects which 'own' text blocks.
Definition: codegenobjectwithtextblocks.h:31
HierarchicalCodeBlock::setStartText
void setStartText(const QString &text)
Definition: hierarchicalcodeblock.cpp:199
CodeBlockWithComments::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
Set the class attributes of this object from the passed element node.
Definition: codeblockwithcomments.cpp:105
CodeGenObjectWithTextBlocks::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
Set the class attributes of this object from the passed element node.
Definition: codegenobjectwithtextblocks.cpp:424
UMLApp::commonPolicy
CodeGenerationPolicy * commonPolicy() const
Returns the default code generation policy.
Definition: uml.cpp:2132
TextBlock::toString
virtual QString toString() const
Return the text in the right format.
Definition: textblock.cpp:404
CodeGenObjectWithTextBlocks::getTextBlockList
TextBlockList * getTextBlockList() const
Get the list of TextBlock objects held by m_textblockVector.
Definition: codegenobjectwithtextblocks.cpp:46
TextBlockList
QList< TextBlock * > TextBlockList
Definition: textblocklist.h:17
HierarchicalCodeBlock
Definition: hierarchicalcodeblock.h:22
TextBlock
The fundemental unit of text within an output file containing code.
Definition: textblock.h:24
HierarchicalCodeBlock::HierarchicalCodeBlock
HierarchicalCodeBlock(CodeDocument *doc, const QString &startString="", const QString &endString="", const QString &comment="")
Constructor.
Definition: hierarchicalcodeblock.cpp:27
HierarchicalCodeBlock::findCodeClassFieldTextBlockByTag
TextBlock * findCodeClassFieldTextBlockByTag(const QString &tag)
look for specific text blocks which belong to code classfields
Definition: hierarchicalcodeblock.cpp:362
HierarchicalCodeBlock::toString
virtual QString toString() const
Definition: hierarchicalcodeblock.cpp:313
HierarchicalCodeBlock::newHierarchicalCodeBlock
virtual HierarchicalCodeBlock * newHierarchicalCodeBlock()
Definition: hierarchicalcodeblock.cpp:83
TextBlock::getParentDocument
CodeDocument * getParentDocument() const
Get the value of m_parentDoc.
Definition: textblock.cpp:70
CodeAccessorMethodList
QList< CodeAccessorMethod * > CodeAccessorMethodList
Definition: codeaccessormethodlist.h:17
TextBlock::formatMultiLineText
static QString formatMultiLineText(const QString &work, const QString &linePrefix, const QString &breakStr, bool addBreak=true, bool lastLineHasBreak=true)
Format a long text string to be more readable.
Definition: textblock.cpp:288
uError
#define uError()
Definition: debug_utils.h:96
HierarchicalCodeBlock::getStartText
QString getStartText() const
Definition: hierarchicalcodeblock.cpp:207
TextBlock::getIndentationString
QString getIndentationString(int level=0) const
Get the actual amount of indentation for a given level of indentation.
Definition: textblock.cpp:193
TextBlock::decodeText
static QString decodeText(const QString &text, const QString &endLine)
Decode text from XML storage.
Definition: textblock.cpp:392
TextBlock::getWriteOutText
bool getWriteOutText() const
Get the value of m_writeOutText Whether or not to include the text of this TextBlock into a file...
Definition: textblock.cpp:141
hierarchicalcodeblock.h
CodeClassFieldList
QList< CodeClassField * > CodeClassFieldList
Definition: codeclassfieldlist.h:17
CodeClassField::getMethodList
CodeAccessorMethodList getMethodList() const
Get the list of Method objects held by m_methodVector.
Definition: codeclassfield.cpp:190
HierarchicalCodeBlock::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &elem)
set attributes of the node that represents this class in the XMI document.
Definition: hierarchicalcodeblock.cpp:251
CodeDocument::getUniqueTag
virtual QString getUniqueTag(const QString &prefix=QString(""))
Return a unique and currently unallocated text block tag for this document.
Definition: codedocument.cpp:192
classifiercodedocument.h
CodeGenObjectWithTextBlocks::findTextBlockByTag
TextBlock * findTextBlockByTag(const QString &tag)
Find the text block with a given tag.
Definition: codegenobjectwithtextblocks.cpp:126
CodeDocument::removeChildTagFromMap
void removeChildTagFromMap(const QString &tag)
Definition: codedocument.cpp:481
TextBlock::encodeText
static QString encodeText(const QString &text, const QString &endLine)
Encode text for XML storage.
Definition: textblock.cpp:378
HierarchicalCodeBlock::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
set the class attributes of this object from the passed element node.
Definition: hierarchicalcodeblock.cpp:278
TextBlock::release
virtual void release()
Causes the text block to release all of its connections and any other text blocks that it 'owns'...
Definition: textblock.cpp:275
uml.h
CodeGenObjectWithTextBlocks::m_textBlockTagMap
QMap< QString, TextBlock * > m_textBlockTagMap
Definition: codegenobjectwithtextblocks.h:95
TextBlock::setTag
void setTag(const QString &value)
Set the tag of this text block.
Definition: textblock.cpp:121
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:05:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

umbrello/umbrello

Skip menu "umbrello/umbrello"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal