headingobj.cpp
author insilmaril
Tue, 24 Jan 2006 15:09:48 +0000
changeset 182 2747c4145c71
parent 173 309609406650
child 185 6691000c3262
permissions -rw-r--r--
Introduced basic export to Open Document format
     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 
    44     int h;	// height of a textline
    45     int ho;	// offset of height while drawing all lines
    46 
    47     if (textline.first() )
    48 		h=textline.first()->boundingRect().height();
    49     else
    50 		h=2;
    51     QCanvasText *t;
    52     ho=0;
    53     for (t=textline.first(); t; t=textline.next() )
    54     {
    55 		t->move(x,y+ho);
    56 		ho=ho+h;
    57     }	
    58 }
    59 
    60 
    61 void HeadingObj::moveBy(double x, double y)
    62 {
    63     move (x+absPos.x(),y+absPos.y() );
    64 }
    65 
    66 void HeadingObj::positionBBox()
    67 {
    68     bbox.setX (absPos.x());
    69     bbox.setY (absPos.y());
    70 }
    71 
    72 void HeadingObj::calcBBoxSize()
    73 {	
    74 	int w=0;
    75 	int h=0;
    76 	// Using Backspace an empty heading might easily be created, then there
    77 	// would be textline.first()==NULL This can be worked around by the following, but
    78 	// then no selection would be visible, thus we prevent it in ::setText()
    79 	if (!textline.isEmpty() )
    80 	{
    81 		QCanvasText *t;
    82 		for (t=textline.first(); t; t=textline.next() )
    83 		{
    84 			h+=t->boundingRect().height();
    85 			if (w<t->boundingRect().width() )
    86 				w=t->boundingRect().width();
    87 		}	
    88 	} 
    89     bbox.setSize (QSize(w,h));
    90 }
    91 
    92 QCanvasText* HeadingObj::newLine(QString s)
    93 {
    94     QCanvasText *t;
    95     t = new QCanvasText(canvas);
    96     t->setFont (font);
    97     t->setColor (color);
    98     t->setZ(Z_TEXT);
    99     t->setText(s);
   100 	t->setTextFlags(Qt::AlignLeft);
   101     t->show();
   102     return t;
   103 }
   104 
   105 void HeadingObj::setText (QString s)
   106 {
   107     heading=s;
   108 
   109     // remove old textlines and prepare generating new ones
   110     textline.clear();
   111 
   112 	// prevent empty textline, so at least a small selection stays
   113 	// visible for this heading
   114 	if (s.length()==0) s="  ";
   115 
   116     int i=0;	// index for actual search for ws
   117     int j=0;	// index of last ws
   118 	int k=0;	// index of "<br>" or similar linebreak
   119 	int br=0;	// width of found break, e.g. for <br> it is 4
   120 
   121     // set the text and wrap lines
   122     while (s.length()>0)
   123     {
   124 		// ok, some people wanted manual linebreaks, here we go
   125 		k=s.find ("<br>",i,false);
   126 		if (k>=0)
   127 		{
   128 			br=4;
   129 			i=k;
   130 		} else
   131 			i=s.find (" ",i,false);
   132 		if (i<0 && j==0)
   133 		{   // no ws found at all in s
   134 			// append whole s
   135 			textline.append (newLine(s));
   136 			s="";
   137 		} else
   138 		{
   139 			if (i<0 && j>0)
   140 			{	// no ws found in actual search
   141 				if (s.length()<=textwidth)
   142 				{
   143 					textline.append (newLine(s));
   144 					s="";
   145 				} else
   146 				{
   147 					textline.append (newLine(s.left(j)));
   148 					s=s.mid(j+1,s.length());
   149 					j=0;
   150 				}		    
   151 			} else
   152 			{
   153 				if (i>= 0 && i<=static_cast <int> (textwidth))
   154 				{   // there is a ws in textwidth
   155 					if (br>0)
   156 					{
   157 						// here is a linebreak
   158 						textline.append (newLine(s.left(i)));
   159 						s=s.mid(i+br,s.length());
   160 						i=0;
   161 						j=0;
   162 						br=0;
   163 					} else
   164 					{
   165 						j=i;
   166 						i++;
   167 					}
   168 				} else
   169 				{
   170 					if (i>static_cast <int> (textwidth)  )
   171 					{	
   172 						if (j>0)
   173 						{   // a ws out of textwidth, but we have also one in
   174 							textline.append (newLine(s.left(j)));
   175 							s=s.mid(j+1,s.length());
   176 							i=0;
   177 							j=0;
   178 						} else
   179 						{   // a ws out of text, but none in
   180 							textline.append (newLine(s.left(i)));
   181 							s=s.mid(i+1,s.length());
   182 							i=0;
   183 						}
   184 					}
   185 				} 
   186 			}	  
   187 		}		    
   188     }
   189 	setVisibility (visible);
   190 	move (absPos.x(),absPos.y());
   191 	calcBBoxSize();
   192 }
   193 
   194 QString HeadingObj::text ()
   195 {
   196     return heading;
   197 }
   198 
   199 void HeadingObj::setFont (QFont f)
   200 {
   201     if (font!=f) 
   202     {
   203 		font=f;
   204 		setText (text());
   205     }
   206 }
   207 
   208 QFont HeadingObj::getFont()
   209 {
   210     return font;
   211 }    
   212 	
   213 	
   214 void HeadingObj::setColor (QColor c)
   215 {
   216     if (color!=c)
   217     {
   218 		color=c;
   219 		QCanvasText *t;
   220 		for (t=textline.first(); t; t=textline.next() )
   221 			t->setColor(c);
   222     }	    
   223 }
   224 
   225 QColor HeadingObj::getColor()
   226 {
   227     return color;
   228 }    
   229 
   230 void HeadingObj::setVisibility (bool v)
   231 {
   232     MapObj::setVisibility(v);
   233     QCanvasText *t;
   234     for (t=textline.first(); t; t=textline.next() )
   235 		if (v)
   236 			t->show();
   237 		else
   238 			t->hide();
   239 }
   240 
   241 int HeadingObj::getHeight ()
   242 {
   243 	return bbox.height();
   244 }
   245 
   246 int HeadingObj::getWidth()
   247 {
   248 	return bbox.width();
   249 }
   250