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