• 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
codedocument.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 "codedocument.h"
14 
15 // local includes
16 #include "codegenerator.h"
17 #include "debug_utils.h"
18 #include "package.h"
19 #include "umldoc.h"
20 #include "uml.h"
21 
22 // qt includes
23 #include <QDateTime>
24 #include <QRegExp>
25 
29 CodeDocument::CodeDocument () : CodeGenObjectWithTextBlocks(this),
30  m_lastTagIndex(0), m_filename(QString()), m_fileExtension(QString()),
31  m_ID(QString()), m_pathName(QString()), m_package(NULL), m_writeOutCode(true)
32 {
33  setHeader(new CodeComment(this));
34  // m_dialog = new CodeDocumentDialog();
35 }
36 
40 CodeDocument::~CodeDocument ()
41 {
42  // delete all the text blocks we have
43  while (!m_textblockVector.isEmpty()) {
44  delete m_textblockVector.takeFirst();
45  }
46  delete m_header;
47 }
48 
53 void CodeDocument::setFileName (const QString &new_var)
54 {
55  m_filename = new_var;
56 }
57 
63 QString CodeDocument::getFileName () const
64 {
65  return m_filename;
66 }
67 
72 void CodeDocument::setFileExtension (const QString &new_var)
73 {
74  m_fileExtension = new_var;
75  updateHeader(); // because we are using new heading file
76 }
77 
82 QString CodeDocument::getFileExtension() const
83 {
84  return m_fileExtension;
85 }
86 
91 void CodeDocument::setPackage (UMLPackage *new_var)
92 {
93  m_package = new_var;
94 }
95 
100 QString CodeDocument::getPath ()
101 {
102  QString path = getPackage();
103 
104  // Replace all white spaces with blanks
105  path = path.simplified();
106 
107  // Replace all blanks with underscore
108  path.replace(QRegExp(" "), "_");
109 
110  // this allows multiple directory paths (ala Java, some other languages)
111  // in from the package specification
112  path.replace(QRegExp("\\."),"/"); // Simple hack!.. but this is more or less language
113  // dependant and should probably be commented out.
114  // Still, as a general default it may be useful -b.t.
115  return path;
116 }
117 
122 QString CodeDocument::getPackage () const
123 {
124  if (m_package)
125  return m_package->name();
126  return QString();
127 }
128 
133 void CodeDocument::setID (const QString &new_id)
134 {
135  m_ID = new_id;
136 }
137 
142 QString CodeDocument::ID () const
143 {
144  return m_ID;
145 }
146 
153 void CodeDocument::setWriteOutCode (bool new_var)
154 {
155  m_writeOutCode = new_var;
156 }
157 
164 bool CodeDocument::getWriteOutCode ()
165 {
166  return m_writeOutCode;
167 }
168 
173 void CodeDocument::setHeader (CodeComment * comment)
174 {
175  m_header = comment;
176 }
177 
182 CodeComment * CodeDocument::getHeader ()
183 {
184  return m_header;
185 }
186 
192 QString CodeDocument::getUniqueTag (const QString& prefix)
193 {
194  QString tag = prefix ;
195  if(tag.isEmpty())
196  tag += "tblock";
197 
198  tag = tag + "_0";
199  int number = m_lastTagIndex;
200  for (; findTextBlockByTag(tag, true); ++number) {
201  tag = prefix + '_' + QString::number(number);
202  }
203  m_lastTagIndex = number;
204  return tag;
205 }
206 
215 bool CodeDocument::insertTextBlock(TextBlock * newBlock, TextBlock * existingBlock, bool after)
216 {
217  if (!newBlock || !existingBlock)
218  return false;
219 
220  QString tag = existingBlock->getTag();
221  if (!findTextBlockByTag(tag, true))
222  return false;
223 
224  int index = m_textblockVector.indexOf(existingBlock);
225  if (index < 0)
226  {
227  // may be hiding in child hierarchical codeblock
228  foreach (TextBlock* tb, m_textblockVector)
229  {
230  HierarchicalCodeBlock * hb = dynamic_cast<HierarchicalCodeBlock*>(tb);
231  if (hb && hb->insertTextBlock(newBlock, existingBlock, after))
232  return true; // found, and inserted, otherwise keep going
233  }
234  // ugh. where is the child block?
235  uWarning() << " Warning: couldnt insert text block (tag:" << newBlock->getTag()
236  << "). Reference text block (tag:" << existingBlock->getTag() << ") not found.";
237  return false;
238  }
239 
240  // if we get here.. it was in this object so insert
241 
242  // check for tag FIRST
243  QString new_tag = newBlock->getTag();
244 
245  // assign a tag if one doesn't already exist
246  if (new_tag.isEmpty())
247  {
248  new_tag = getUniqueTag();
249  newBlock->setTag(new_tag);
250  }
251 
252  if (m_textBlockTagMap.contains(new_tag))
253  return false; // return false, we already have some object with this tag in the list
254  else
255  m_textBlockTagMap.insert(new_tag, newBlock);
256 
257  if (after)
258  index++;
259 
260  m_textblockVector.insert(index, newBlock);
261  return true;
262 }
263 
269 QString CodeDocument::cleanName (const QString &name)
270 {
271  return CodeGenerator::cleanName(name);
272 }
273 
278 void CodeDocument::updateHeader ()
279 {
280  //try to find a heading file (license, coments, etc) then extract its text
281  QString headingText = UMLApp::app()->commonPolicy()->getHeadingFile(getFileExtension());
282 
283  headingText.replace(QRegExp("%filename%"), getFileName()+getFileExtension());
284  headingText.replace(QRegExp("%filepath%"), getPath());
285  headingText.replace(QRegExp("%time%"), QTime::currentTime().toString());
286  headingText.replace(QRegExp("%date%"), QDate::currentDate().toString());
287 
288  getHeader()->setText(headingText);
289 
290  // update the write out status of the header
291  if (UMLApp::app()->commonPolicy()->getIncludeHeadings())
292  getHeader()->setWriteOutText(true);
293  else
294  getHeader()->setWriteOutText(false);
295 }
296 
301 QString CodeDocument::toString ()
302 {
303  // IF the whole document is turned "Off" then don't bother
304  // checking individual code blocks, just send back empty string
305  if (!getWriteOutCode())
306  return QString();
307 
308  QString content = getHeader()->toString();
309 
310  // update the time/date
311 
312  // comments, import, package codeblocks go next
313  TextBlockList * items = getTextBlockList();
314  foreach (TextBlock* c, *items)
315  {
316  if (c->getWriteOutText()) {
317  QString str = c->toString();
318  if (!str.isEmpty())
319  content.append(str);
320  }
321  }
322  return content;
323 }
324 
328 void CodeDocument::synchronize()
329 {
330  updateContent();
331 }
332 
337 void CodeDocument::resetTextBlocks()
338 {
339  CodeGenObjectWithTextBlocks::resetTextBlocks();
340  m_childTextBlockTagMap.clear();
341 }
342 
347 void CodeDocument::loadFromXMI (QDomElement & root)
348 {
349  setAttributesFromNode(root);
350 }
351 
356 void CodeDocument::setAttributesOnNode (QDomDocument & doc, QDomElement & docElement)
357 {
358  // superclass call
359  CodeGenObjectWithTextBlocks::setAttributesOnNode(doc, docElement);
360 
361  // now set local attributes/fields
362  docElement.setAttribute("fileName", getFileName());
363  docElement.setAttribute("fileExt", getFileExtension());
364  Uml::ID::Type pkgId = Uml::ID::None;
365  if (m_package)
366  pkgId = m_package->id();
367  docElement.setAttribute("package", Uml::ID::toString(pkgId));
368  docElement.setAttribute("writeOutCode", getWriteOutCode() ? "true" : "false");
369  docElement.setAttribute("id", ID());
370 
371  // set the a header
372  // which we will store in its own separate child node block
373  QDomElement commElement = doc.createElement("header");
374  getHeader()->saveToXMI(doc, commElement); // comment
375  docElement.appendChild(commElement);
376 
377  // doc codePolicy?
378  // FIX: store ONLY if different from the parent generator
379  // policy.. something which is not possible right now. -b.t.
380 }
381 
386 void CodeDocument::setAttributesFromNode (QDomElement & root)
387 {
388  // now set local attributes
389  setFileName(root.attribute("fileName",""));
390  setFileExtension(root.attribute("fileExt",""));
391  QString pkgStr = root.attribute("package","");
392  if (!pkgStr.isEmpty() && pkgStr != "-1") {
393  UMLDoc *umldoc = UMLApp::app()->document();
394  if (pkgStr.contains(QRegExp("\\D"))) {
395  // suspecting pre-1.5.3 file format where the package name was
396  // saved instead of the package ID.
397  UMLObject *o = umldoc->findUMLObject(pkgStr);
398  m_package = dynamic_cast<UMLPackage*>(o);
399  }
400  if (m_package == NULL) {
401  UMLObject *o = umldoc->findObjectById(Uml::ID::fromString(pkgStr));
402  m_package = dynamic_cast<UMLPackage*>(o);
403  }
404  }
405  setWriteOutCode(root.attribute("writeOutCode","true") == "true" ? true : false);
406  setID(root.attribute("id",""));
407 
408  // load comment now
409  // by looking for our particular child element
410  QDomNode node = root.firstChild();
411  QDomElement element = node.toElement();
412  while (!element.isNull()) {
413  QString tag = element.tagName();
414  if (tag == "header") {
415  QDomNode cnode = element.firstChild();
416  QDomElement celem = cnode.toElement();
417  getHeader()->loadFromXMI(celem);
418  break;
419  }
420  node = element.nextSibling();
421  element = node.toElement();
422  }
423 
424  // a rare case where the super-class load is AFTER local attributes
425  CodeGenObjectWithTextBlocks::setAttributesFromNode(root);
426 }
427 
433 void CodeDocument::saveToXMI (QDomDocument & doc, QDomElement & root)
434 {
435  QDomElement docElement = doc.createElement("codedocument");
436  setAttributesOnNode(doc, docElement);
437  root.appendChild(docElement);
438 }
439 
447 void CodeDocument::updateContent()
448 {
449  updateHeader(); // doing this insures time/date stamp is at the time of this call
450 }
451 
456 CodeBlock * CodeDocument::newCodeBlock ()
457 {
458  return new CodeBlock(this);
459 }
460 
465 CodeBlockWithComments * CodeDocument::newCodeBlockWithComments ()
466 {
467  return new CodeBlockWithComments(this);
468 }
469 
474 HierarchicalCodeBlock * CodeDocument::newHierarchicalCodeBlock ()
475 {
476  HierarchicalCodeBlock *hb = new HierarchicalCodeBlock(this);
477  //hb->update();
478  return hb;
479 }
480 
481 void CodeDocument::removeChildTagFromMap (const QString &tag)
482 {
483  m_childTextBlockTagMap.remove(tag);
484 }
485 
486 void CodeDocument::addChildTagToMap (const QString &tag, TextBlock * tb)
487 {
488  m_childTextBlockTagMap.insert(tag, tb);
489 }
490 
499 TextBlock * CodeDocument::findTextBlockByTag(const QString &tag, bool descendIntoChildren)
500 {
501  //if we already know to which file this class was written/should be written, just return it.
502  if (m_textBlockTagMap.contains(tag))
503  return m_textBlockTagMap[tag];
504 
505  if (descendIntoChildren)
506  if (m_childTextBlockTagMap.contains(tag))
507  return m_childTextBlockTagMap[tag];
508 
509  return NULL;
510 }
511 
516 TextBlock * CodeDocument::findCodeClassFieldTextBlockByTag (const QString &tag)
517 {
518  uWarning() << "Called findCodeClassFieldMethodByTag(" << tag << ") for a regular CodeDocument";
519  return NULL;
520 }
521 
522 QDebug operator<<(QDebug os, const CodeDocument& obj)
523 {
524  os.nospace() << "CodeDocument: id=" << obj.ID()
525  << ", file=" << obj.getFileName(); //:TODO: add all attributes
526  return os.space();
527 }
CodeDocument::getWriteOutCode
bool getWriteOutCode()
Get the value of m_writeOutCode.
Definition: codedocument.cpp:164
CodeDocument::updateContent
virtual void updateContent()
Update the content of this code document.
Definition: codedocument.cpp:447
CodeDocument::setHeader
void setHeader(CodeComment *comment)
Set a Header comment object.
Definition: codedocument.cpp:173
UMLPackage
This class contains the non-graphical information required for a UML Package.
Definition: package.h:32
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
CodeBlock
A "chunk" of code within the code document.
Definition: codeblock.h:20
CodeDocument::synchronize
virtual void synchronize()
Cause this code document to synchronize to current generator policy.
Definition: codedocument.cpp:328
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
CodeComment::loadFromXMI
virtual void loadFromXMI(QDomElement &root)
Load params from the appropriate XMI element node.
Definition: codecomment.cpp:42
codedocument.h
TextBlock::setText
void setText(const QString &text)
Set the value of m_text The actual text of this code block.
Definition: textblock.cpp:80
TextBlock::getTag
QString getTag() const
Get the tag of this text block.
Definition: textblock.cpp:110
CodeDocument::updateHeader
void updateHeader()
Update the header text of this codedocument (text and status of the head comment).
Definition: codedocument.cpp:278
CodeDocument::insertTextBlock
bool insertTextBlock(TextBlock *newBlock, TextBlock *existingBlock, bool after=true)
Insert a new text block after the existing text block.
Definition: codedocument.cpp:215
CodeDocument::loadFromXMI
virtual void loadFromXMI(QDomElement &root)
Load params from the appropriate XMI element node.
Definition: codedocument.cpp:347
UMLApp::app
static UMLApp * app()
Get the last created instance of this class.
Definition: uml.cpp:206
CodeDocument::newHierarchicalCodeBlock
virtual HierarchicalCodeBlock * newHierarchicalCodeBlock()
Create a new HierarchicalCodeBlock object belonging to this CodeDocument.
Definition: codedocument.cpp:474
CodeGenObjectWithTextBlocks::resetTextBlocks
virtual void resetTextBlocks()
Reset/clear the inventory text blocks held by this object.
Definition: codegenobjectwithtextblocks.cpp:333
uWarning
#define uWarning()
Definition: debug_utils.h:97
UMLApp::document
UMLDoc * document() const
Returns a pointer to the current document connected to the KMainWindow instance.
Definition: uml.cpp:872
debug_utils.h
CodeDocument::getPath
virtual QString getPath()
Get the value of the path to this code document.
Definition: codedocument.cpp:100
CodeGenObjectWithTextBlocks::m_textblockVector
TextBlockList m_textblockVector
Definition: codegenobjectwithtextblocks.h:96
UMLObject
This class is the non-graphical version of UMLWidget.
Definition: umlobject.h:41
CodeDocument::HierarchicalCodeBlock
friend class HierarchicalCodeBlock
Definition: codedocument.h:34
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
codegenerator.h
CodeBlockWithComments
class CodeBlockWithComments A very common type of text block in any type of code. ...
Definition: codeblockwithcomments.h:24
CodeDocument::setWriteOutCode
void setWriteOutCode(bool new_var)
Set the value of m_writeOutCode.
Definition: codedocument.cpp:153
CodeDocument::saveToXMI
virtual void saveToXMI(QDomDocument &doc, QDomElement &root)
Save the XMI representation of this object.
Definition: codedocument.cpp:433
CodeDocument::~CodeDocument
virtual ~CodeDocument()
Destructor.
Definition: codedocument.cpp:40
CodeDocument::resetTextBlocks
void resetTextBlocks()
Reset/clear our inventory of textblocks in this document.
Definition: codedocument.cpp:337
CodeDocument::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &blockElement)
Set attributes of the node that represents this class in the XMI document.
Definition: codedocument.cpp:356
TextBlock::setWriteOutText
void setWriteOutText(bool write)
Set the value of m_writeOutText Whether or not to include the text of this TextBlock into a file...
Definition: textblock.cpp:131
CodeDocument::ID
QString ID() const
Get the value of m_ID.
Definition: codedocument.cpp:142
CodeDocument::getFileExtension
QString getFileExtension() const
Get the value of m_fileExtension.
Definition: codedocument.cpp:82
CodeDocument
A document containing the code for one file.
Definition: codedocument.h:32
CodeComment::saveToXMI
virtual void saveToXMI(QDomDocument &doc, QDomElement &root)
Save the XMI representation of this object.
Definition: codecomment.cpp:31
CodeDocument::addChildTagToMap
void addChildTagToMap(const QString &tag, TextBlock *tb)
Definition: codedocument.cpp:486
CodeDocument::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
Set the class attributes of this object from the passed element node.
Definition: codedocument.cpp:386
CodeGenObjectWithTextBlocks
This abstract class is for code generator objects which 'own' text blocks.
Definition: codegenobjectwithtextblocks.h:31
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
CodeDocument::cleanName
QString cleanName(const QString &name)
A little utility method which calls CodeGenerator::cleanName.
Definition: codedocument.cpp:269
TextBlock::toString
virtual QString toString() const
Return the text in the right format.
Definition: textblock.cpp:404
CodeDocument::toString
virtual QString toString()
Create the string representation of this object.
Definition: codedocument.cpp:301
CodeGenObjectWithTextBlocks::getTextBlockList
TextBlockList * getTextBlockList() const
Get the list of TextBlock objects held by m_textblockVector.
Definition: codegenobjectwithtextblocks.cpp:46
CodeDocument::findTextBlockByTag
TextBlock * findTextBlockByTag(const QString &tag, bool descendIntoChildren=false)
Lookup a certain textblock by its tag value, returns NULL if it can not find the TextBlock with such ...
Definition: codedocument.cpp:499
CodeDocument::setFileName
void setFileName(const QString &new_var)
Set the complete value (name plus any extension) of m_filename.
Definition: codedocument.cpp:53
Uml::ID::Type
std::string Type
Definition: basictypes.h:317
TextBlockList
QList< TextBlock * > TextBlockList
Definition: textblocklist.h:17
Uml::ID::toString
QString toString(const ID::Type &id)
Definition: basictypes.cpp:1048
UMLDoc::findObjectById
UMLObject * findObjectById(Uml::ID::Type id)
Used to find a reference to a UMLObject by its ID.
Definition: umldoc.cpp:766
HierarchicalCodeBlock
Definition: hierarchicalcodeblock.h:22
umldoc.h
TextBlock
The fundemental unit of text within an output file containing code.
Definition: textblock.h:24
operator<<
QDebug operator<<(QDebug os, const CodeDocument &obj)
Definition: codedocument.cpp:522
package.h
CodeDocument::findCodeClassFieldTextBlockByTag
virtual TextBlock * findCodeClassFieldTextBlockByTag(const QString &tag)
Have to implement this for CodeObjectWithTextBlocks.
Definition: codedocument.cpp:516
CodeDocument::setPackage
void setPackage(UMLPackage *new_var)
Set the value of m_package.
Definition: codedocument.cpp:91
UMLObject::name
QString name() const
Returns a copy of m_name.
Definition: umlobject.cpp:185
CodeGenerator::cleanName
static QString cleanName(const QString &name)
Replaces spaces with underscores and capitalises as defined in m_modname.
Definition: codegenerator.cpp:609
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
Uml::ID::fromString
ID::Type fromString(const QString &id)
Definition: basictypes.cpp:1053
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
CodeDocument::removeChildTagFromMap
void removeChildTagFromMap(const QString &tag)
Definition: codedocument.cpp:481
CodeGenerationPolicy::getHeadingFile
QString getHeadingFile(const QString &str)
Gets the heading file (as a string) to be inserted at the beginning of the generated file...
Definition: codegenerationpolicy.cpp:540
CodeComment
Text which will be comments.
Definition: codecomment.h:23
CodeDocument::getFileName
QString getFileName() const
Get the value of m_filename.
Definition: codedocument.cpp:63
CodeDocument::setFileExtension
void setFileExtension(const QString &new_var)
Set the value of m_fileExtension.
Definition: codedocument.cpp:72
Uml::ID::None
const Type None
special value for uninitialized ID
Definition: basictypes.h:319
UMLDoc::findUMLObject
UMLObject * findUMLObject(const QString &name, UMLObject::ObjectType type=UMLObject::ot_UMLObject, UMLObject *currentObj=0)
Used to find a UMLObject by its type and name.
Definition: umldoc.cpp:809
CodeDocument::getPackage
QString getPackage() const
Get the value of the package of this code document.
Definition: codedocument.cpp:122
CodeDocument::setID
void setID(const QString &new_id)
Set the value of m_ID.
Definition: codedocument.cpp:133
CodeDocument::getHeader
CodeComment * getHeader()
Get the Header comment object.
Definition: codedocument.cpp:182
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
UMLDoc
UMLDoc provides a document object for a document-view model.
Definition: umldoc.h:63
CodeDocument::CodeDocument
CodeDocument()
Constructor.
Definition: codedocument.cpp:29
UMLObject::id
virtual Uml::ID::Type id() const
Returns the ID of the object.
Definition: umlobject.cpp:394
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