headingobj.cpp
author insilmaril
Mon, 12 Feb 2007 09:28:46 +0000
changeset 425 7014be3ac7d0
parent 421 5522d1da7e37
child 428 9ae68208e2ff
permissions -rw-r--r--
1.8.66 Various fixes
     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 QGraphicsTextItem* HeadingObj::newLine(QString s)
    94 {
    95     QGraphicsTextItem *t=scene->addText(s);
    96     t->setFont (font);
    97     t->setZValue(Z_TEXT);
    98     t->setDefaultTextColor(color);
    99     return t;
   100 }
   101 
   102 void HeadingObj::setText (QString s)
   103 {
   104     heading=s;
   105 
   106     // remove old textlines and prepare generating new ones
   107 	while (!textline.isEmpty())
   108 		delete textline.takeFirst();
   109 
   110 	// prevent empty textline, so at least a small selection stays
   111 	// visible for this heading
   112 	if (s.length()==0) s="  ";
   113 
   114     int i=0;	// index for actual search for ws
   115     int j=0;	// index of last ws
   116 	int k=0;	// index of "<br>" or similar linebreak
   117 	int br=0;	// width of found break, e.g. for <br> it is 4
   118 	QRegExp re("<br.*/>");
   119 	re.setMinimal (true);
   120 
   121     // set the text and wrap lines
   122     while (s.length()>0)
   123     {
   124 		// ok, some people wanted manual linebreaks, here we go
   125 		k=re.search (s,i);
   126 		if (k>=0)
   127 		{
   128 			br=re.cap(0).length();
   129 			i=k;
   130 		} else
   131 			i=s.find (" ",i,false);
   132 		if (i<0 && j==0)
   133 		{   // no ws found at all in s
   134 			// append whole s
   135 			textline.append (newLine(s));
   136 			s="";
   137 		} else
   138 		{
   139 			if (i<0 && j>0)
   140 			{	// no ws found in actual search
   141 				if (s.length()<=textwidth)
   142 				{
   143 					textline.append (newLine(s));
   144 					s="";
   145 				} else
   146 				{
   147 					textline.append (newLine(s.left(j)));
   148 					s=s.mid(j+1,s.length());
   149 					j=0;
   150 				}		    
   151 			} else
   152 			{
   153 				if (i>= 0 && i<=static_cast <int> (textwidth))
   154 				{   // there is a ws in textwidth
   155 					if (br>0)
   156 					{
   157 						// here is a linebreak
   158 						textline.append (newLine(s.left(i)));
   159 						s=s.mid(i+br,s.length());
   160 						i=0;
   161 						j=0;
   162 						br=0;
   163 					} else
   164 					{
   165 						j=i;
   166 						i++;
   167 					}
   168 				} else
   169 				{
   170 					if (i>static_cast <int> (textwidth)  )
   171 					{	
   172 						if (j>0)
   173 						{   // a ws out of textwidth, but we have also one in
   174 							textline.append (newLine(s.left(j)));
   175 							s=s.mid(j+1,s.length());
   176 							i=0;
   177 							j=0;
   178 						} else
   179 						{   // a ws out of text, but none in
   180 							textline.append (newLine(s.left(i)));
   181 							s=s.mid(i+1,s.length());
   182 							i=0;
   183 						}
   184 					}
   185 				} 
   186 			}	  
   187 		}		    
   188     }
   189 	setVisibility (visible);
   190 	move (absPos.x(),absPos.y());
   191 	calcBBoxSize();
   192 }
   193 
   194 QString HeadingObj::text ()
   195 {
   196     return heading;
   197 }
   198 
   199 void HeadingObj::setFont (QFont f)
   200 {
   201     if (font!=f) 
   202     {
   203 		font=f;
   204 		setText (text());
   205     }
   206 }
   207 
   208 QFont HeadingObj::getFont()
   209 {
   210     return font;
   211 }    
   212 	
   213 	
   214 void HeadingObj::setColor (QColor c)
   215 {
   216     if (color!=c)
   217     {
   218 		color=c;
   219 		for (int i=0; i<textline.size(); ++i)
   220 			textline.at(i)->setDefaultTextColor(c);
   221     }	    
   222 }
   223 
   224 QColor HeadingObj::getColor()
   225 {
   226     return color;
   227 }    
   228 
   229 void HeadingObj::setVisibility (bool v)
   230 {
   231     MapObj::setVisibility(v);
   232 	for (int i=0; i<textline.size(); ++i)
   233 		if (v)
   234 			textline.at(i)->show();
   235 		else
   236 			textline.at(i)->hide();
   237 }
   238 
   239 qreal HeadingObj::getHeight ()
   240 {
   241 	return bbox.height();
   242 }
   243 
   244 qreal HeadingObj::getWidth()
   245 {
   246 	return bbox.width();
   247 }
   248