textblock.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "textblock.h"
00020
00021
00022 #include <qregexp.h>
00023
00024
00025 #include "codedocument.h"
00026 #include "codegenerator.h"
00027 #include "codegenerationpolicy.h"
00028 #include "uml.h"
00029
00030
00031
00032
00033 TextBlock::TextBlock ( CodeDocument * parent, const QString & text )
00034 : QObject ( (QObject *)parent, "textBlock")
00035 {
00036 initFields(parent);
00037 setText(text);
00038 }
00039
00040 TextBlock::~TextBlock ( ) { }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00055 void TextBlock::setParentDocument ( CodeDocument * new_var ) {
00056 m_parentDocument = new_var;
00057 }
00058
00059 bool TextBlock::canDelete ( ) {
00060 return m_canDelete;
00061 }
00062
00067 CodeDocument * TextBlock::getParentDocument ( ) {
00068 return m_parentDocument;
00069 }
00070
00076 void TextBlock::setText ( const QString &new_var ) {
00077 m_text = new_var;
00078 }
00079
00084 void TextBlock::appendText ( const QString &new_text ) {
00085 m_text = m_text + new_text;
00086 }
00087
00093 QString TextBlock::getText ( ) const {
00094 return m_text;
00095 }
00096
00102 QString TextBlock::getTag( ) const {
00103 return m_tag;
00104 }
00105
00111 void TextBlock::setTag ( const QString &value ) {
00112 m_tag = value;
00113 }
00114
00120 void TextBlock::setWriteOutText ( bool new_var ) {
00121 m_writeOutText = new_var;
00122 }
00123
00129 bool TextBlock::getWriteOutText ( ) {
00130 return m_writeOutText;
00131 }
00132
00135 void TextBlock::setIndentationLevel ( int level ) {
00136 m_indentationLevel = level;
00137 }
00138
00143 int TextBlock::getIndentationLevel ( ) {
00144 return m_indentationLevel;
00145 }
00146
00147 QString TextBlock::getNewLineEndingChars ( ) {
00148 CodeGenerationPolicy * policy = UMLApp::app()->getCommonPolicy();
00149 return policy->getNewLineEndingChars();
00150 }
00151
00152 QString TextBlock::getIndentation() {
00153 CodeGenerationPolicy * policy = UMLApp::app()->getCommonPolicy();
00154 return policy->getIndentation();
00155 }
00156
00157 QString TextBlock::getIndentationString ( int level ) {
00158 if (!level)
00159 level = m_indentationLevel;
00160 QString indentAmount = getIndentation();
00161 QString indentation = "";
00162 for(int i=0; i<level; i++)
00163 indentation.append(indentAmount);
00164 return indentation;
00165 }
00166
00167
00168
00169
00177 int TextBlock::firstEditableLine() { return 0; }
00178 int TextBlock::lastEditableLine() { return 0; }
00179
00180 QString TextBlock::getNewEditorLine ( int amount ) {
00181 return getIndentationString(amount);
00182 }
00183
00184
00185 QString TextBlock::unformatText ( const QString & text, const QString & indent )
00186 {
00187 QString output = text;
00188 QString myIndent = indent;
00189 if(myIndent.isEmpty())
00190 myIndent = getIndentationString();
00191
00192 if(!output.isEmpty())
00193 output.remove(QRegExp('^'+myIndent));
00194
00195 return output;
00196 }
00197
00198 void TextBlock::release () {
00199 this->disconnect();
00200
00201 }
00202
00203 QString TextBlock::formatMultiLineText ( const QString &work, const QString &linePrefix,
00204 const QString& breakStr, bool addBreak, bool lastLineHasBreak ) {
00205 QString output = "";
00206 QString text = work;
00207 QString endLine = getNewLineEndingChars();
00208 int matches = text.contains(QRegExp(breakStr));
00209 if(matches)
00210 {
00211
00212
00213 if(!text.contains(QRegExp(breakStr+"\\$")))
00214 matches++;
00215
00216 for(int i=0; i < matches; i++)
00217 {
00218 QString line = text.section(QRegExp(breakStr),i,i);
00219 output += linePrefix + line;
00220 if((i != matches-1) || lastLineHasBreak)
00221 output += endLine;
00222 }
00223 } else {
00224 output = linePrefix + text;
00225 if(addBreak)
00226 output += breakStr;
00227 }
00228
00229 return output;
00230 }
00231
00232 void TextBlock::setAttributesOnNode ( QDomDocument & doc, QDomElement & blockElement)
00233 {
00234
00235 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00236
00237 if (&doc != 0 ) {
00238
00239 blockElement.setAttribute("tag",getTag());
00240
00241
00242 if(getIndentationLevel())
00243 blockElement.setAttribute("indentLevel",QString::number(getIndentationLevel()));
00244 if(!m_text.isEmpty())
00245 blockElement.setAttribute("text",encodeText(m_text,endLine));
00246 if(!getWriteOutText())
00247 blockElement.setAttribute("writeOutText",getWriteOutText()?"true":"false");
00248 if(!canDelete())
00249 blockElement.setAttribute("canDelete",canDelete()?"true":"false");
00250
00251 }
00252
00253 }
00254
00255 void TextBlock::setAttributesFromObject(TextBlock * obj)
00256 {
00257
00258
00259 setIndentationLevel(obj->getIndentationLevel());
00260 setText(obj->getText());
00261 setWriteOutText(obj->getWriteOutText());
00262 m_canDelete = obj->canDelete();
00263
00264 }
00265
00266 void TextBlock::setAttributesFromNode (QDomElement & root ) {
00267
00268 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00269
00270 setIndentationLevel(root.attribute("indentLevel","0").toInt());
00271 setTag(root.attribute("tag",""));
00272 setText(decodeText(root.attribute("text",""),endLine));
00273 setWriteOutText(root.attribute("writeOutText","true") == "true" ? true : false);
00274 m_canDelete = root.attribute("canDelete","true") == "true" ? true : false;
00275
00276 }
00277
00278
00279
00280
00281 QString TextBlock::encodeText(const QString& text, const QString &endLine) {
00282 QString encoded = text;
00283 encoded.replace(QRegExp(endLine),"
");
00284 return encoded;
00285 }
00286
00287
00288
00289
00290 QString TextBlock::decodeText(const QString& text, const QString &endLine) {
00291 QString decoded = text;
00292 decoded.replace(QRegExp("
"),endLine);
00293 return decoded;
00294 }
00295
00299 QString TextBlock::toString ( )
00300 {
00301
00302
00303 if(m_writeOutText && !m_text.isEmpty())
00304 {
00305 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00306 return formatMultiLineText(m_text, getIndentationString(), endLine);
00307 } else
00308 return "";
00309 }
00310
00311 void TextBlock::initFields ( CodeDocument * parent ) {
00312 m_canDelete = true;
00313 m_writeOutText = true;
00314 m_parentDocument = parent;
00315 m_text = "";
00316 m_tag = "";
00317 m_indentationLevel = 0;
00318 }
00319
00320 #include "textblock.moc"
This file is part of the documentation for umbrello Version 3.1.0.