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