headingobj.cpp
changeset 82 920e6ed5889b
parent 0 7a96bd401351
child 2 608f976aa7bb
child 166 325958acb69b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/headingobj.cpp	Sun Jan 30 12:59:10 2005 +0000
     1.3 @@ -0,0 +1,246 @@
     1.4 +#include "headingobj.h"
     1.5 +
     1.6 +/////////////////////////////////////////////////////////////////
     1.7 +// HeadingObj
     1.8 +/////////////////////////////////////////////////////////////////
     1.9 +HeadingObj::HeadingObj() : MapObj()
    1.10 +{
    1.11 +//    cout << "Const HeadingObj ()\n";
    1.12 +    init ();
    1.13 +}
    1.14 +
    1.15 +HeadingObj::HeadingObj(QCanvas* c) :MapObj(c)
    1.16 +{
    1.17 +//    cout << "Const HeadingObj\n";
    1.18 +    init ();
    1.19 +}
    1.20 +
    1.21 +HeadingObj::~HeadingObj()
    1.22 +{
    1.23 +    textline.clear();
    1.24 +}
    1.25 +
    1.26 +void HeadingObj::init()
    1.27 +{
    1.28 +    textline.setAutoDelete (TRUE);
    1.29 +    textwidth=40;
    1.30 +    color=QColor ("black");
    1.31 +    font=QFont();
    1.32 +}
    1.33 +
    1.34 +void HeadingObj::copy(HeadingObj *other)
    1.35 +{
    1.36 +    MapObj::copy (other);
    1.37 +    textwidth=other->textwidth;
    1.38 +    color=other->color;
    1.39 +    font=other->font;
    1.40 +    setText (other->text() );
    1.41 +}
    1.42 +
    1.43 +void HeadingObj::move(double x, double y)
    1.44 +{
    1.45 +    MapObj::move(x,y);
    1.46 +    int h;	// height of a textline
    1.47 +    int ho;	// offset of height while drawing all lines
    1.48 +    if (textline.first() )
    1.49 +		h=textline.first()->boundingRect().height();
    1.50 +    else
    1.51 +		h=2;
    1.52 +    QCanvasText *t;
    1.53 +    ho=0;
    1.54 +    for (t=textline.first(); t; t=textline.next() )
    1.55 +    {
    1.56 +		t->move(x,y+ho);
    1.57 +		ho=ho+h;
    1.58 +    }	
    1.59 +}
    1.60 +
    1.61 +
    1.62 +void HeadingObj::moveBy(double x, double y)
    1.63 +{
    1.64 +    move (x+absPos.x(),y+absPos.y() );
    1.65 +}
    1.66 +
    1.67 +void HeadingObj::positionBBox()
    1.68 +{
    1.69 +    bbox.setX (absPos.x());
    1.70 +    bbox.setY (absPos.y());
    1.71 +}
    1.72 +
    1.73 +void HeadingObj::calcBBoxSize()
    1.74 +{	
    1.75 +	int w=0;
    1.76 +	int h=0;
    1.77 +	// Using Backspace an empty heading might easily be created, then there
    1.78 +	// would be textline.first()==NULL This can be worked around by the following, but
    1.79 +	// then no selection would be visible, thus we prevent it in ::setText()
    1.80 +	if (!textline.isEmpty() )
    1.81 +	{
    1.82 +		QCanvasText *t;
    1.83 +		for (t=textline.first(); t; t=textline.next() )
    1.84 +		{
    1.85 +			h+=t->boundingRect().height();
    1.86 +			if (w<t->boundingRect().width() )
    1.87 +				w=t->boundingRect().width();
    1.88 +		}	
    1.89 +	} 
    1.90 +    bbox.setSize (QSize(w,h));
    1.91 +}
    1.92 +
    1.93 +QCanvasText* HeadingObj::newLine(QString s)
    1.94 +{
    1.95 +    QCanvasText *t;
    1.96 +    t = new QCanvasText(canvas);
    1.97 +    t->setFont (font);
    1.98 +    t->setColor (color);
    1.99 +    t->setZ(Z_TEXT);
   1.100 +    t->setText(s);
   1.101 +    t->show();
   1.102 +    return t;
   1.103 +}
   1.104 +
   1.105 +void HeadingObj::setText (QString s)
   1.106 +{
   1.107 +    heading=s;
   1.108 +
   1.109 +    // remove old textlines and prepare generating new ones
   1.110 +    textline.clear();
   1.111 +
   1.112 +	// prevent empty textline, so at least a small selection stays
   1.113 +	// visible for this heading
   1.114 +	if (s.length()==0) s="  ";
   1.115 +
   1.116 +    int i=0;	// index for actual search for ws
   1.117 +    int j=0;	// index of last ws
   1.118 +	int k=0;	// index of "<br>" or similar linebreak
   1.119 +	int br=0;	// width of found break, e.g. for <br> it is 4
   1.120 +
   1.121 +    // set the text and wrap lines
   1.122 +    while (s.length()>0)
   1.123 +    {
   1.124 +		// ok, some people wanted manual linebreaks, here we go
   1.125 +		k=s.find ("<br>",i,false);
   1.126 +		if (k>=0)
   1.127 +		{
   1.128 +			br=4;
   1.129 +			i=k;
   1.130 +		} else
   1.131 +			i=s.find (" ",i,false);
   1.132 +		if (i<0 && j==0)
   1.133 +		{   // no ws found at all in s
   1.134 +			// append whole s
   1.135 +			textline.append (newLine(s));
   1.136 +			s="";
   1.137 +		} else
   1.138 +		{
   1.139 +			if (i<0 && j>0)
   1.140 +			{	// no ws found in actual search
   1.141 +				if (s.length()<=textwidth)
   1.142 +				{
   1.143 +					textline.append (newLine(s));
   1.144 +					s="";
   1.145 +				} else
   1.146 +				{
   1.147 +					textline.append (newLine(s.left(j)));
   1.148 +					s=s.mid(j+1,s.length());
   1.149 +					j=0;
   1.150 +				}		    
   1.151 +			} else
   1.152 +			{
   1.153 +				if (i>= 0 && i<=static_cast <int> (textwidth))
   1.154 +				{   // there is a ws in textwidth
   1.155 +					if (br>0)
   1.156 +					{
   1.157 +						// here is a linebreak
   1.158 +						textline.append (newLine(s.left(i)));
   1.159 +						s=s.mid(i+br,s.length());
   1.160 +						i=0;
   1.161 +						j=0;
   1.162 +						br=0;
   1.163 +					} else
   1.164 +					{
   1.165 +						j=i;
   1.166 +						i++;
   1.167 +					}
   1.168 +				} else
   1.169 +				{
   1.170 +					if (i>static_cast <int> (textwidth)  )
   1.171 +					{	
   1.172 +						if (j>0)
   1.173 +						{   // a ws out of textwidth, but we have also one in
   1.174 +							textline.append (newLine(s.left(j)));
   1.175 +							s=s.mid(j+1,s.length());
   1.176 +							i=0;
   1.177 +							j=0;
   1.178 +						} else
   1.179 +						{   // a ws out of text, but none in
   1.180 +							textline.append (newLine(s.left(i)));
   1.181 +							s=s.mid(i+1,s.length());
   1.182 +							i=0;
   1.183 +						}
   1.184 +					}
   1.185 +				} 
   1.186 +			}	  
   1.187 +		}		    
   1.188 +    }
   1.189 +	setVisibility (visible);
   1.190 +	calcBBoxSize();
   1.191 +}
   1.192 +
   1.193 +QString HeadingObj::text ()
   1.194 +{
   1.195 +    return heading;
   1.196 +}
   1.197 +
   1.198 +void HeadingObj::setFont (QFont f)
   1.199 +{
   1.200 +    if (font!=f) 
   1.201 +    {
   1.202 +		font=f;
   1.203 +		setText (text());
   1.204 +    }
   1.205 +}
   1.206 +
   1.207 +QFont HeadingObj::getFont()
   1.208 +{
   1.209 +    return font;
   1.210 +}    
   1.211 +	
   1.212 +	
   1.213 +void HeadingObj::setColor (QColor c)
   1.214 +{
   1.215 +    if (color!=c)
   1.216 +    {
   1.217 +		color=c;
   1.218 +		QCanvasText *t;
   1.219 +		for (t=textline.first(); t; t=textline.next() )
   1.220 +			t->setColor(c);
   1.221 +    }	    
   1.222 +}
   1.223 +
   1.224 +QColor HeadingObj::getColor()
   1.225 +{
   1.226 +    return color;
   1.227 +}    
   1.228 +
   1.229 +void HeadingObj::setVisibility (bool v)
   1.230 +{
   1.231 +    MapObj::setVisibility(v);
   1.232 +    QCanvasText *t;
   1.233 +    for (t=textline.first(); t; t=textline.next() )
   1.234 +		if (v)
   1.235 +			t->show();
   1.236 +		else
   1.237 +			t->hide();
   1.238 +}
   1.239 +
   1.240 +int HeadingObj::getHeight ()
   1.241 +{
   1.242 +	return bbox.height();
   1.243 +}
   1.244 +
   1.245 +int HeadingObj::getWidth()
   1.246 +{
   1.247 +	return bbox.width();
   1.248 +}
   1.249 +