headingobj.cpp
author insilmaril
Tue, 14 Mar 2006 14:27:04 +0000
changeset 239 bdeb503d2b7f
parent 224 bbb4eeefcc94
child 366 e95081c21da2
permissions -rw-r--r--
hideLinkInExport for Branches (Floats still missing). Floats are now OrnamentedObj.
     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(QCanvas* c) :MapObj(c)
    14 {
    15 //    cout << "Const HeadingObj\n";
    16     init ();
    17 }
    18 
    19 HeadingObj::~HeadingObj()
    20 {
    21     textline.clear();
    22 }
    23 
    24 void HeadingObj::init()
    25 {
    26     textline.setAutoDelete (TRUE);
    27     textwidth=40;
    28     color=QColor ("black");
    29     font=QFont();
    30 	heading="";
    31 }
    32 
    33 void HeadingObj::copy(HeadingObj *other)
    34 {
    35     MapObj::copy (other);
    36     textwidth=other->textwidth;
    37     color=other->color;
    38     font=other->font;
    39     setText (other->text() );
    40 }
    41 
    42 void HeadingObj::move(double x, double y)
    43 {
    44     MapObj::move(x,y);
    45 
    46     int h;	// height of a textline
    47     int ho;	// offset of height while drawing all lines
    48 
    49     if (textline.first() )
    50 		h=textline.first()->boundingRect().height();
    51     else
    52 		h=2;
    53     QCanvasText *t;
    54     ho=0;
    55     for (t=textline.first(); t; t=textline.next() )
    56     {
    57 		t->move(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 	int w=0;
    77 	int 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 		QCanvasText *t;
    84 		for (t=textline.first(); t; t=textline.next() )
    85 		{
    86 			h+=t->boundingRect().height();
    87 			if (w<t->boundingRect().width() )
    88 				w=t->boundingRect().width();
    89 		}	
    90 	} 
    91     bbox.setSize (QSize(w,h));
    92 }
    93 
    94 QCanvasText* HeadingObj::newLine(QString s)
    95 {
    96     QCanvasText *t;
    97     t = new QCanvasText(canvas);
    98     t->setFont (font);
    99     t->setColor (color);
   100     t->setZ(Z_TEXT);
   101     t->setText(s);
   102 	t->setTextFlags(Qt::AlignLeft);
   103     t->show();
   104     return t;
   105 }
   106 
   107 void HeadingObj::setText (QString s)
   108 {
   109     heading=s;
   110 
   111     // remove old textlines and prepare generating new ones
   112     textline.clear();
   113 
   114 	// prevent empty textline, so at least a small selection stays
   115 	// visible for this heading
   116 	if (s.length()==0) s="  ";
   117 
   118     int i=0;	// index for actual search for ws
   119     int j=0;	// index of last ws
   120 	int k=0;	// index of "<br>" or similar linebreak
   121 	int br=0;	// width of found break, e.g. for <br> it is 4
   122 	QRegExp re("<br.*/>");
   123 	re.setMinimal (true);
   124 
   125     // set the text and wrap lines
   126     while (s.length()>0)
   127     {
   128 		// ok, some people wanted manual linebreaks, here we go
   129 		k=re.search (s,i);
   130 		if (k>=0)
   131 		{
   132 			br=re.cap(0).length();
   133 			i=k;
   134 		} else
   135 			i=s.find (" ",i,false);
   136 		if (i<0 && j==0)
   137 		{   // no ws found at all in s
   138 			// append whole s
   139 			textline.append (newLine(s));
   140 			s="";
   141 		} else
   142 		{
   143 			if (i<0 && j>0)
   144 			{	// no ws found in actual search
   145 				if (s.length()<=textwidth)
   146 				{
   147 					textline.append (newLine(s));
   148 					s="";
   149 				} else
   150 				{
   151 					textline.append (newLine(s.left(j)));
   152 					s=s.mid(j+1,s.length());
   153 					j=0;
   154 				}		    
   155 			} else
   156 			{
   157 				if (i>= 0 && i<=static_cast <int> (textwidth))
   158 				{   // there is a ws in textwidth
   159 					if (br>0)
   160 					{
   161 						// here is a linebreak
   162 						textline.append (newLine(s.left(i)));
   163 						s=s.mid(i+br,s.length());
   164 						i=0;
   165 						j=0;
   166 						br=0;
   167 					} else
   168 					{
   169 						j=i;
   170 						i++;
   171 					}
   172 				} else
   173 				{
   174 					if (i>static_cast <int> (textwidth)  )
   175 					{	
   176 						if (j>0)
   177 						{   // a ws out of textwidth, but we have also one in
   178 							textline.append (newLine(s.left(j)));
   179 							s=s.mid(j+1,s.length());
   180 							i=0;
   181 							j=0;
   182 						} else
   183 						{   // a ws out of text, but none in
   184 							textline.append (newLine(s.left(i)));
   185 							s=s.mid(i+1,s.length());
   186 							i=0;
   187 						}
   188 					}
   189 				} 
   190 			}	  
   191 		}		    
   192     }
   193 	setVisibility (visible);
   194 	move (absPos.x(),absPos.y());
   195 	calcBBoxSize();
   196 }
   197 
   198 QString HeadingObj::text ()
   199 {
   200     return heading;
   201 }
   202 
   203 void HeadingObj::setFont (QFont f)
   204 {
   205     if (font!=f) 
   206     {
   207 		font=f;
   208 		setText (text());
   209     }
   210 }
   211 
   212 QFont HeadingObj::getFont()
   213 {
   214     return font;
   215 }    
   216 	
   217 	
   218 void HeadingObj::setColor (QColor c)
   219 {
   220     if (color!=c)
   221     {
   222 		color=c;
   223 		QCanvasText *t;
   224 		for (t=textline.first(); t; t=textline.next() )
   225 			t->setColor(c);
   226     }	    
   227 }
   228 
   229 QColor HeadingObj::getColor()
   230 {
   231     return color;
   232 }    
   233 
   234 void HeadingObj::setVisibility (bool v)
   235 {
   236     MapObj::setVisibility(v);
   237     QCanvasText *t;
   238     for (t=textline.first(); t; t=textline.next() )
   239 		if (v)
   240 			t->show();
   241 		else
   242 			t->hide();
   243 }
   244 
   245 int HeadingObj::getHeight ()
   246 {
   247 	return bbox.height();
   248 }
   249 
   250 int HeadingObj::getWidth()
   251 {
   252 	return bbox.width();
   253 }
   254