headingobj.cpp
author insilmaril
Thu, 23 Nov 2006 16:18:26 +0000
changeset 407 5db8dfd30ea2
parent 406 1c8ff1928b97
child 408 c2a05fa925a1
permissions -rw-r--r--
Removed even more QT3 stuff. Drag & Drop not 100% functional at the moment
     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(Q3Canvas* c) :MapObj(c)
    14 {
    15 //    cout << "Const HeadingObj\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     int h;	// height of a textline
    48     int 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)->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 		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 (QSize(w,h));
    91 }
    92 
    93 Q3CanvasText* HeadingObj::newLine(QString s)
    94 {
    95     Q3CanvasText *t;
    96     t = new Q3CanvasText(canvas);
    97     t->setFont (font);
    98     t->setColor (color);
    99     t->setZ(Z_TEXT);
   100     t->setText(s);
   101 	t->setTextFlags(Qt::AlignLeft);
   102     t->show();
   103     return t;
   104 }
   105 
   106 void HeadingObj::setText (QString s)
   107 {
   108     heading=s;
   109 
   110     // remove old textlines and prepare generating new ones
   111 	while (!textline.isEmpty())
   112 		delete textline.takeFirst();
   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 		for (int i=0; i<textline.size(); ++i)
   224 			textline.at(i)->setColor(c);
   225     }	    
   226 }
   227 
   228 QColor HeadingObj::getColor()
   229 {
   230     return color;
   231 }    
   232 
   233 void HeadingObj::setVisibility (bool v)
   234 {
   235     MapObj::setVisibility(v);
   236 	for (int i=0; i<textline.size(); ++i)
   237 		if (v)
   238 			textline.at(i)->show();
   239 		else
   240 			textline.at(i)->hide();
   241 }
   242 
   243 int HeadingObj::getHeight ()
   244 {
   245 	return bbox.height();
   246 }
   247 
   248 int HeadingObj::getWidth()
   249 {
   250 	return bbox.width();
   251 }
   252