• 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
textblock.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 "textblock.h"
14 
15 // local includes
16 #include "codedocument.h"
17 #include "codegenerationpolicy.h"
18 #include "debug_utils.h"
19 #include "uml.h"
20 
21 // qt includes
22 #include <QRegExp>
23 #include <QTextStream>
24 
28 TextBlock::TextBlock(CodeDocument * parent, const QString & text)
29  : m_text(QString()),
30  m_tag(QString()),
31  m_canDelete(true),
32  m_writeOutText(true),
33  m_indentationLevel(0),
34  m_parentDocument(parent)
35 {
36  setText(text);
37 }
38 
42 TextBlock::~TextBlock()
43 {
44 }
45 
50 void TextBlock::setCanDelete(bool canDelete)
51 {
52  m_canDelete = canDelete;
53 }
54 
61 bool TextBlock::canDelete() const
62 {
63  return m_canDelete;
64 }
65 
70 CodeDocument * TextBlock::getParentDocument() const
71 {
72  return m_parentDocument;
73 }
74 
80 void TextBlock::setText(const QString & text)
81 {
82  m_text = text;
83 }
84 
89 void TextBlock::appendText(const QString & text)
90 {
91  m_text = m_text + text;
92 }
93 
99 QString TextBlock::getText() const
100 {
101  return m_text;
102 }
103 
110 QString TextBlock::getTag() const
111 {
112  return m_tag;
113 }
114 
121 void TextBlock::setTag(const QString & value)
122 {
123  m_tag = value;
124 }
125 
131 void TextBlock::setWriteOutText(bool write)
132 {
133  m_writeOutText = write;
134 }
135 
141 bool TextBlock::getWriteOutText() const
142 {
143  return m_writeOutText;
144 }
145 
152 void TextBlock::setIndentationLevel(int level)
153 {
154  m_indentationLevel = level;
155 }
156 
163 int TextBlock::getIndentationLevel() const
164 {
165  return m_indentationLevel;
166 }
167 
172 QString TextBlock::getNewLineEndingChars()
173 {
174  CodeGenerationPolicy* policy = UMLApp::app()->commonPolicy();
175  return policy->getNewLineEndingChars();
176 }
177 
182 QString TextBlock::getIndentation()
183 {
184  CodeGenerationPolicy* policy = UMLApp::app()->commonPolicy();
185  return policy->getIndentation();
186 }
187 
193 QString TextBlock::getIndentationString(int level) const
194 {
195  if (!level) {
196  level = m_indentationLevel;
197  }
198  QString indentAmount = getIndentation();
199  QString indentation;
200  for (int i=0; i<level; ++i) {
201  indentation.append(indentAmount);
202  }
203  return indentation;
204 }
205 
215 int TextBlock::firstEditableLine()
216 {
217  return 0;
218 }
219 
223 int TextBlock::lastEditableLine()
224 {
225  return 0;
226 }
227 
240 QString TextBlock::getNewEditorLine(int amount)
241 {
242  return getIndentationString(amount);
243 }
244 
253 QString TextBlock::unformatText(const QString & text, const QString & indent)
254 {
255  QString output = text;
256  QString myIndent = indent;
257  if (myIndent.isEmpty()) {
258  myIndent = getIndentationString();
259  }
260 
261  if (!output.isEmpty()) {
262  // remove indenation from this text block.
263  output.remove(QRegExp('^' + myIndent));
264  }
265 
266  return output;
267 }
268 
275 void TextBlock::release()
276 {
277 }
278 
288 QString TextBlock::formatMultiLineText(const QString & work, const QString & linePrefix,
289  const QString & breakStr, bool addBreak, bool lastLineHasBreak)
290 {
291  QString output;
292  QString text = work;
293  QString endLine = getNewLineEndingChars();
294  int matches = text.indexOf(QRegExp(breakStr));
295  if (matches >= 0) {
296  // check that last part of string matches, if not, then
297  // we have to tack on extra match
298  if (!text.contains(QRegExp(breakStr + "\\$")))
299  matches++;
300 
301  for (int i=0; i < matches; ++i) {
302  QString line = text.section(QRegExp(breakStr), i, i);
303  output += linePrefix + line;
304  if ((i != matches-1) || lastLineHasBreak)
305  output += endLine; // add break to line
306  }
307  } else {
308  output = linePrefix + text;
309  if (addBreak)
310  output += breakStr;
311  }
312 
313  return output;
314 }
315 
322 void TextBlock::setAttributesOnNode(QDomDocument & doc, QDomElement & blockElement)
323 {
324  Q_UNUSED(doc);
325 
326  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
327 
328  blockElement.setAttribute("tag", getTag());
329 
330  // only write these if different from defaults
331  if (getIndentationLevel())
332  blockElement.setAttribute("indentLevel", QString::number(getIndentationLevel()));
333  if (!m_text.isEmpty())
334  blockElement.setAttribute("text", encodeText(m_text, endLine));
335  if (!getWriteOutText())
336  blockElement.setAttribute("writeOutText", getWriteOutText()?"true":"false");
337  if (!canDelete())
338  blockElement.setAttribute("canDelete", canDelete()?"true":"false");
339 }
340 
345 void TextBlock::setAttributesFromObject(TextBlock * obj)
346 {
347  // DON'T set tag here.
348  setIndentationLevel(obj->getIndentationLevel());
349  setText(obj->getText());
350  setWriteOutText(obj->getWriteOutText());
351  m_canDelete = obj->canDelete();
352 }
353 
359 void TextBlock::setAttributesFromNode(QDomElement & root)
360 {
361  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
362 
363  setIndentationLevel(root.attribute("indentLevel", "0").toInt());
364  setTag(root.attribute("tag", ""));
365  setText(decodeText(root.attribute("text", ""), endLine));
366  setWriteOutText(root.attribute("writeOutText", "true") == "true" ? true : false);
367  m_canDelete = root.attribute("canDelete", "true") == "true" ? true : false;
368 }
369 
378 QString TextBlock::encodeText(const QString & text, const QString & endLine)
379 {
380  QString encoded = text;
381  encoded.replace(QRegExp(endLine), "&#010;");
382  return encoded;
383 }
384 
392 QString TextBlock::decodeText(const QString & text, const QString & endLine)
393 {
394  QString decoded = text;
395  decoded.replace(QRegExp("&#010;"), endLine);
396  return decoded;
397 }
398 
404 QString TextBlock::toString() const
405 {
406  // simple output method
407  if (m_writeOutText && !m_text.isEmpty()) {
408  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
409  return formatMultiLineText(m_text, getIndentationString(), endLine);
410  }
411  else {
412  return QString();
413  }
414 }
415 
419 QDebug operator<<(QDebug os, const TextBlock& obj)
420 {
421  os.nospace() << "TextBlock: tag=" << obj.getTag()
422  << ", writeOutText=" << (obj.getWriteOutText() ? "true" : "false")
423  << ", canDelete=" << (obj.canDelete() ? "true" : "false")
424  << ", indentationLevel=" << obj.getIndentationLevel()
425  << ", parentDocument id=" << (obj.getParentDocument() ? obj.getParentDocument()->ID() : "null")
426  << ", text=" << obj.getText();
427  return os.space();
428 }
TextBlock::unformatText
virtual QString unformatText(const QString &text, const QString &indent="")
UnFormat a long text string.
Definition: textblock.cpp:253
TextBlock::getNewEditorLine
virtual QString getNewEditorLine(int amount=0)
Used by the CodeEditor.
Definition: textblock.cpp:240
codedocument.h
TextBlock::getTag
QString getTag() const
Get the tag of this text block.
Definition: textblock.cpp:110
TextBlock::setText
void setText(const QString &text)
Set the value of m_text The actual text of this code block.
Definition: textblock.cpp:80
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
TextBlock::firstEditableLine
virtual int firstEditableLine()
TODO: Ush.
Definition: textblock.cpp:215
CodeGenerationPolicy
class CodeGenerationPolicy This class describes the code generation policy for this project...
Definition: codegenerationpolicy.h:29
TextBlock::getIndentationLevel
int getIndentationLevel() const
Get how many times to indent this text block.
Definition: textblock.cpp:163
debug_utils.h
TextBlock::TextBlock
TextBlock(CodeDocument *parent, const QString &text="")
Constructor.
Definition: textblock.cpp:28
TextBlock::getNewLineEndingChars
static QString getNewLineEndingChars()
Get the new line chars which ends the line.
Definition: textblock.cpp:172
textblock.h
codegenerationpolicy.h
TextBlock::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &blockElement)
Set attributes of the node that represents this class in the XMI document.
Definition: textblock.cpp:322
TextBlock::setCanDelete
void setCanDelete(bool canDelete)
Set the attribute m_canDelete.
Definition: textblock.cpp:50
CodeGenerationPolicy::getIndentation
QString getIndentation() const
Utility method to get the amount (and type of whitespace) to indent with.
Definition: codegenerationpolicy.cpp:299
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
A document containing the code for one file.
Definition: codedocument.h:32
TextBlock::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &root)
Set the class attributes of this object from the passed element node.
Definition: textblock.cpp:359
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
TextBlock::canDelete
bool canDelete() const
Determine if its OK to delete this textblock from the document.
Definition: textblock.cpp:61
TextBlock::lastEditableLine
virtual int lastEditableLine()
Definition: textblock.cpp:223
TextBlock
The fundemental unit of text within an output file containing code.
Definition: textblock.h:24
TextBlock::appendText
void appendText(const QString &text)
Add text to this object.
Definition: textblock.cpp:89
TextBlock::getParentDocument
CodeDocument * getParentDocument() const
Get the value of m_parentDoc.
Definition: textblock.cpp:70
operator<<
QDebug operator<<(QDebug os, const TextBlock &obj)
Operator '<<' for TextBlock.
Definition: textblock.cpp:419
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
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
TextBlock::setIndentationLevel
void setIndentationLevel(int level)
Set how many times to indent this text block.
Definition: textblock.cpp:152
TextBlock::setAttributesFromObject
virtual void setAttributesFromObject(TextBlock *obj)
Set the class attributes from a passed object.
Definition: textblock.cpp:345
TextBlock::~TextBlock
virtual ~TextBlock()
Destructor.
Definition: textblock.cpp:42
TextBlock::encodeText
static QString encodeText(const QString &text, const QString &endLine)
Encode text for XML storage.
Definition: textblock.cpp:378
TextBlock::getText
QString getText() const
Get the value of m_text The actual text of this code block.
Definition: textblock.cpp:99
TextBlock::getIndentation
static QString getIndentation()
Get how much a single "level" of indentation will actually indent.
Definition: textblock.cpp:182
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
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:06:00 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