headingobj.cpp
author insilmaril
Wed, 25 Apr 2007 16:02:54 +0000
changeset 463 694f5e35e87c
parent 428 9ae68208e2ff
child 568 b44cc0d3ed4a
permissions -rw-r--r--
started doxygen documentation
     1 #include "headingobj.h"
     2 #include <qregexp.h>
     3 
     4 /////////////////////////////////////////////////////////////////
     5 // HeadingObj
     6 /////////////////////////////////////////////////////////////////
     7 HeadingObj::HeadingObj() : MapObj()
     8 {
     9 //    cout << "Const HeadingObj ()\n";
    10     init ();
    11 }
    12 
    13 HeadingObj::HeadingObj(QGraphicsScene *s) :MapObj(s)
    14 {
    15 //    cout << "Const HeadingObj (s)\n";
    16     init ();
    17 }
    18 
    19 HeadingObj::~HeadingObj()
    20 {
    21 //	cout << "Destr. HeadingObj "<<heading.ascii()<<endl;
    22 	while (!textline.isEmpty())
    23 		delete textline.takeFirst();
    24 }
    25 
    26 void HeadingObj::init()
    27 {
    28     textwidth=40;
    29     color=QColor ("black");
    30     font=QFont();
    31 	heading="";
    32 }
    33 
    34 void HeadingObj::copy(HeadingObj *other)
    35 {
    36     MapObj::copy (other);
    37     textwidth=other->textwidth;
    38     color=other->color;
    39     font=other->font;
    40     setText (other->text() );
    41 }
    42 
    43 void HeadingObj::move(double x, double y)
    44 {
    45     MapObj::move(x,y);
    46 
    47     qreal h;	// height of a textline
    48     qreal ho;	// offset of height while drawing all lines
    49 
    50     if (!textline.isEmpty() )
    51 		h=textline.first()->boundingRect().height();
    52     else
    53 		h=2;
    54     ho=0;
    55 	for (int i=0; i<textline.size(); ++i)
    56     {
    57 		textline.at(i)->setPos(x,y+ho);
    58 		ho=ho+h;
    59     }	
    60 }
    61 
    62 
    63 void HeadingObj::moveBy(double x, double y)
    64 {
    65     move (x+absPos.x(),y+absPos.y() );
    66 }
    67 
    68 void HeadingObj::positionBBox()
    69 {
    70     bbox.setX (absPos.x());
    71     bbox.setY (absPos.y());
    72 }
    73 
    74 void HeadingObj::calcBBoxSize()
    75 {	
    76 	qreal w=0;
    77 	qreal h=0;
    78 	// Using Backspace an empty heading might easily be created, then there
    79 	// would be textline.first()==NULL This can be worked around by the following, but
    80 	// then no selection would be visible, thus we prevent it in ::setText()
    81 	if (!textline.isEmpty() )
    82 	{
    83 		for (int i=0; i<textline.size(); ++i)
    84 		{
    85 			h+=textline.at(i)->boundingRect().height();
    86 			if (w<textline.at(i)->boundingRect().width() )
    87 				w=textline.at(i)->boundingRect().width();
    88 		}	
    89 	} 
    90     bbox.setSize (QSizeF(w,h));
    91 }
    92 
    93 QGraphicsSimpleTextItem* HeadingObj::newLine(QString s)
    94 {
    95     QGraphicsSimpleTextItem *t=new QGraphicsSimpleTextItem (s,0,scene);
    96     t->setFont (font);
    97     t->setZValue(Z_TEXT);
    98     //t->setDefaultTextColor(color);
    99     t->setBrush(color);
   100     return t;
   101 }
   102 
   103 void HeadingObj::setText (QString s)
   104 {
   105     heading=s;
   106 
   107     // remove old textlines and prepare generating new ones
   108 	while (!textline.isEmpty())
   109 		delete textline.takeFirst();
   110 
   111 	// prevent empty textline, so at least a small selection stays
   112 	// visible for this heading
   113 	if (s.length()==0) s="  ";
   114 
   115     int i=0;	// index for actual search for ws
   116     int j=0;	// index of last ws
   117 	int k=0;	// index of "<br>" or similar linebreak
   118 	int br=0;	// width of found break, e.g. for <br> it is 4
   119 	QRegExp re("<br.*/>");
   120 	re.setMinimal (true);
   121 
   122     // set the text and wrap lines
   123     while (s.length()>0)
   124     {
   125 		// ok, some people wanted manual linebreaks, here we go
   126 		k=re.search (s,i);
   127 		if (k>=0)
   128 		{
   129 			br=re.cap(0).length();
   130 			i=k;
   131 		} else
   132 			i=s.find (" ",i,false);
   133 		if (i<0 && j==0)
   134 		{   // no ws found at all in s
   135 			// append whole s
   136 			textline.append (newLine(s));
   137 			s="";
   138 		} else
   139 		{
   140 			if (i<0 && j>0)
   141 			{	// no ws found in actual search
   142 				if (s.length()<=textwidth)
   143 				{
   144 					textline.append (newLine(s));
   145 					s="";
   146 				} else
   147 				{
   148 					textline.append (newLine(s.left(j)));
   149 					s=s.mid(j+1,s.length());
   150 					j=0;
   151 				}		    
   152 			} else
   153 			{
   154 				if (i>= 0 && i<=static_cast <int> (textwidth))
   155 				{   // there is a ws in textwidth
   156 					if (br>0)
   157 					{
   158 						// here is a linebreak
   159 						textline.append (newLine(s.left(i)));
   160 						s=s.mid(i+br,s.length());
   161 						i=0;
   162 						j=0;
   163 						br=0;
   164 					} else
   165 					{
   166 						j=i;
   167 						i++;
   168 					}
   169 				} else
   170 				{
   171 					if (i>static_cast <int> (textwidth)  )
   172 					{	
   173 						if (j>0)
   174 						{   // a ws out of textwidth, but we have also one in
   175 							textline.append (newLine(s.left(j)));
   176 							s=s.mid(j+1,s.length());
   177 							i=0;
   178 							j=0;
   179 						} else
   180 						{   // a ws out of text, but none in
   181 							textline.append (newLine(s.left(i)));
   182 							s=s.mid(i+1,s.length());
   183 							i=0;
   184 						}
   185 					}
   186 				} 
   187 			}	  
   188 		}		    
   189     }
   190 	setVisibility (visible);
   191 	move (absPos.x(),absPos.y());
   192 	calcBBoxSize();
   193 }
   194 
   195 QString HeadingObj::text ()
   196 {
   197     return heading;
   198 }
   199 
   200 void HeadingObj::setFont (QFont f)
   201 {
   202     if (font!=f) 
   203     {
   204 		font=f;
   205 		setText (text());
   206     }
   207 }
   208 
   209 QFont HeadingObj::getFont()
   210 {
   211     return font;
   212 }    
   213 	
   214 	
   215 void HeadingObj::setColor (QColor c)
   216 {
   217     if (color!=c)
   218     {
   219 		color=c;
   220 		for (int i=0; i<textline.size(); ++i)
   221 			//textline.at(i)->setDefaultTextColor(c);
   222 			textline.at(i)->setBrush(c);
   223     }	    
   224 }
   225 
   226 QColor HeadingObj::getColor()
   227 {
   228     return color;
   229 }    
   230 
   231 void HeadingObj::setVisibility (bool v)
   232 {
   233     MapObj::setVisibility(v);
   234 	for (int i=0; i<textline.size(); ++i)
   235 		if (v)
   236 			textline.at(i)->show();
   237 		else
   238 			textline.at(i)->hide();
   239 }
   240 
   241 qreal HeadingObj::getHeight ()
   242 {
   243 	return bbox.height();
   244 }
   245 
   246 qreal HeadingObj::getWidth()
   247 {
   248 	return bbox.width();
   249 }
   250