headingobj.cpp
author insilmaril
Fri Jul 22 15:38:06 2005 +0000 (2005-07-22)
changeset 131 16b250a57c17
child 2 608f976aa7bb
child 166 325958acb69b
permissions -rw-r--r--
reactivated experimental ASCII export, more changes in undo system
     1 #include "headingobj.h"
     2 
     3 /////////////////////////////////////////////////////////////////
     4 // HeadingObj
     5 /////////////////////////////////////////////////////////////////
     6 HeadingObj::HeadingObj() : MapObj()
     7 {
     8 //    cout << "Const HeadingObj ()\n";
     9     init ();
    10 }
    11 
    12 HeadingObj::HeadingObj(QCanvas* c) :MapObj(c)
    13 {
    14 //    cout << "Const HeadingObj\n";
    15     init ();
    16 }
    17 
    18 HeadingObj::~HeadingObj()
    19 {
    20     textline.clear();
    21 }
    22 
    23 void HeadingObj::init()
    24 {
    25     textline.setAutoDelete (TRUE);
    26     textwidth=40;
    27     color=QColor ("black");
    28     font=QFont();
    29 }
    30 
    31 void HeadingObj::copy(HeadingObj *other)
    32 {
    33     MapObj::copy (other);
    34     textwidth=other->textwidth;
    35     color=other->color;
    36     font=other->font;
    37     setText (other->text() );
    38 }
    39 
    40 void HeadingObj::move(double x, double y)
    41 {
    42     MapObj::move(x,y);
    43     int h;	// height of a textline
    44     int ho;	// offset of height while drawing all lines
    45     if (textline.first() )
    46 		h=textline.first()->boundingRect().height();
    47     else
    48 		h=2;
    49     QCanvasText *t;
    50     ho=0;
    51     for (t=textline.first(); t; t=textline.next() )
    52     {
    53 		t->move(x,y+ho);
    54 		ho=ho+h;
    55     }	
    56 }
    57 
    58 
    59 void HeadingObj::moveBy(double x, double y)
    60 {
    61     move (x+absPos.x(),y+absPos.y() );
    62 }
    63 
    64 void HeadingObj::positionBBox()
    65 {
    66     bbox.setX (absPos.x());
    67     bbox.setY (absPos.y());
    68 }
    69 
    70 void HeadingObj::calcBBoxSize()
    71 {	
    72 	int w=0;
    73 	int h=0;
    74 	// Using Backspace an empty heading might easily be created, then there
    75 	// would be textline.first()==NULL This can be worked around by the following, but
    76 	// then no selection would be visible, thus we prevent it in ::setText()
    77 	if (!textline.isEmpty() )
    78 	{
    79 		QCanvasText *t;
    80 		for (t=textline.first(); t; t=textline.next() )
    81 		{
    82 			h+=t->boundingRect().height();
    83 			if (w<t->boundingRect().width() )
    84 				w=t->boundingRect().width();
    85 		}	
    86 	} 
    87     bbox.setSize (QSize(w,h));
    88 }
    89 
    90 QCanvasText* HeadingObj::newLine(QString s)
    91 {
    92     QCanvasText *t;
    93     t = new QCanvasText(canvas);
    94     t->setFont (font);
    95     t->setColor (color);
    96     t->setZ(Z_TEXT);
    97     t->setText(s);
    98     t->show();
    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     textline.clear();
   108 
   109 	// prevent empty textline, so at least a small selection stays
   110 	// visible for this heading
   111 	if (s.length()==0) s="  ";
   112 
   113     int i=0;	// index for actual search for ws
   114     int j=0;	// index of last ws
   115 	int k=0;	// index of "<br>" or similar linebreak
   116 	int br=0;	// width of found break, e.g. for <br> it is 4
   117 
   118     // set the text and wrap lines
   119     while (s.length()>0)
   120     {
   121 		// ok, some people wanted manual linebreaks, here we go
   122 		k=s.find ("<br>",i,false);
   123 		if (k>=0)
   124 		{
   125 			br=4;
   126 			i=k;
   127 		} else
   128 			i=s.find (" ",i,false);
   129 		if (i<0 && j==0)
   130 		{   // no ws found at all in s
   131 			// append whole s
   132 			textline.append (newLine(s));
   133 			s="";
   134 		} else
   135 		{
   136 			if (i<0 && j>0)
   137 			{	// no ws found in actual search
   138 				if (s.length()<=textwidth)
   139 				{
   140 					textline.append (newLine(s));
   141 					s="";
   142 				} else
   143 				{
   144 					textline.append (newLine(s.left(j)));
   145 					s=s.mid(j+1,s.length());
   146 					j=0;
   147 				}		    
   148 			} else
   149 			{
   150 				if (i>= 0 && i<=static_cast <int> (textwidth))
   151 				{   // there is a ws in textwidth
   152 					if (br>0)
   153 					{
   154 						// here is a linebreak
   155 						textline.append (newLine(s.left(i)));
   156 						s=s.mid(i+br,s.length());
   157 						i=0;
   158 						j=0;
   159 						br=0;
   160 					} else
   161 					{
   162 						j=i;
   163 						i++;
   164 					}
   165 				} else
   166 				{
   167 					if (i>static_cast <int> (textwidth)  )
   168 					{	
   169 						if (j>0)
   170 						{   // a ws out of textwidth, but we have also one in
   171 							textline.append (newLine(s.left(j)));
   172 							s=s.mid(j+1,s.length());
   173 							i=0;
   174 							j=0;
   175 						} else
   176 						{   // a ws out of text, but none in
   177 							textline.append (newLine(s.left(i)));
   178 							s=s.mid(i+1,s.length());
   179 							i=0;
   180 						}
   181 					}
   182 				} 
   183 			}	  
   184 		}		    
   185     }
   186 	setVisibility (visible);
   187 	calcBBoxSize();
   188 }
   189 
   190 QString HeadingObj::text ()
   191 {
   192     return heading;
   193 }
   194 
   195 void HeadingObj::setFont (QFont f)
   196 {
   197     if (font!=f) 
   198     {
   199 		font=f;
   200 		setText (text());
   201     }
   202 }
   203 
   204 QFont HeadingObj::getFont()
   205 {
   206     return font;
   207 }    
   208 	
   209 	
   210 void HeadingObj::setColor (QColor c)
   211 {
   212     if (color!=c)
   213     {
   214 		color=c;
   215 		QCanvasText *t;
   216 		for (t=textline.first(); t; t=textline.next() )
   217 			t->setColor(c);
   218     }	    
   219 }
   220 
   221 QColor HeadingObj::getColor()
   222 {
   223     return color;
   224 }    
   225 
   226 void HeadingObj::setVisibility (bool v)
   227 {
   228     MapObj::setVisibility(v);
   229     QCanvasText *t;
   230     for (t=textline.first(); t; t=textline.next() )
   231 		if (v)
   232 			t->show();
   233 		else
   234 			t->hide();
   235 }
   236 
   237 int HeadingObj::getHeight ()
   238 {
   239 	return bbox.height();
   240 }
   241 
   242 int HeadingObj::getWidth()
   243 {
   244 	return bbox.width();
   245 }
   246